Skip to content

Commit 665238e

Browse files
committed
Implement XR_KHR_android_thread_settings
1 parent 68410ac commit 665238e

File tree

5 files changed

+275
-0
lines changed

5 files changed

+275
-0
lines changed

modules/openxr/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def get_doc_classes():
7474
"OpenXRSpatialAnchorCapability",
7575
"OpenXRSpatialPlaneTrackingCapability",
7676
"OpenXRSpatialMarkerTrackingCapability",
77+
"OpenXRAndroidThreadSettingsExtension",
7778
]
7879

7980

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<class name="OpenXRAndroidThreadSettingsExtension" inherits="OpenXRExtensionWrapper" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
3+
<brief_description>
4+
Wraps the [url=https://registry.khronos.org/OpenXR/specs/1.1/html/xrspec.html#XR_KHR_android_thread_settings]XR_KHR_android_thread_settings[/url] extension.
5+
</brief_description>
6+
<description>
7+
For XR to be comfortable, it is important for applications to deliver frames quickly and consistently. In order to make sure the important application threads get their full share of time, these threads must be identified to the system, which will adjust their scheduling priority accordingly.
8+
</description>
9+
<tutorials>
10+
</tutorials>
11+
<methods>
12+
<method name="set_application_thread_type">
13+
<return type="bool" />
14+
<param index="0" name="thread_type" type="int" enum="OpenXRAndroidThreadSettingsExtension.ThreadType" />
15+
<param index="1" name="thread_id" type="int" default="0" />
16+
<description>
17+
Sets the thread type of the given thread, so that the XR runtime can adjust its scheduling priority accordingly.
18+
[param thread_id] refers to the OS thread id (ie from [code]gettid()[/code]). When [param thread_id] is [code]0[/code], it will set the thread type of the current thread.
19+
[b]NOTE:[/b] The id returned by [method Thread.get_id] is incompatible with [param thread_id].
20+
</description>
21+
</method>
22+
</methods>
23+
<constants>
24+
<constant name="THREAD_TYPE_APPLICATION_MAIN" value="0" enum="ThreadType">
25+
Hints to the XR runtime that the thread is doing time critical CPU tasks.
26+
</constant>
27+
<constant name="THREAD_TYPE_APPLICATION_WORKER" value="1" enum="ThreadType">
28+
Hints to the XR runtime that the thread is doing background CPU tasks.
29+
</constant>
30+
<constant name="THREAD_TYPE_RENDERER_MAIN" value="2" enum="ThreadType">
31+
Hints to the XR runtime that the thread is doing time critical graphics device tasks.
32+
</constant>
33+
<constant name="THREAD_TYPE_RENDERER_WORKER" value="3" enum="ThreadType">
34+
Hints to the XR runtime that the thread is doing background graphics device tasks.
35+
</constant>
36+
</constants>
37+
</class>
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/**************************************************************************/
2+
/* openxr_android_thread_settings_extension.cpp */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#include "openxr_android_thread_settings_extension.h"
32+
33+
#include "core/object/callable_method_pointer.h"
34+
#include "core/string/print_string.h"
35+
#include "servers/rendering/rendering_server.h"
36+
37+
#ifdef XR_USE_PLATFORM_ANDROID
38+
#include "../openxr_api.h"
39+
#include <unistd.h>
40+
#endif
41+
42+
OpenXRAndroidThreadSettingsExtension *OpenXRAndroidThreadSettingsExtension::singleton = nullptr;
43+
44+
OpenXRAndroidThreadSettingsExtension *OpenXRAndroidThreadSettingsExtension::get_singleton() {
45+
return singleton;
46+
}
47+
48+
OpenXRAndroidThreadSettingsExtension::OpenXRAndroidThreadSettingsExtension() {
49+
singleton = this;
50+
}
51+
52+
OpenXRAndroidThreadSettingsExtension::~OpenXRAndroidThreadSettingsExtension() {
53+
singleton = nullptr;
54+
}
55+
56+
void OpenXRAndroidThreadSettingsExtension::_bind_methods() {
57+
ClassDB::bind_method(D_METHOD("set_application_thread_type", "thread_type", "thread_id"), &OpenXRAndroidThreadSettingsExtension::set_application_thread_type, DEFVAL(0));
58+
59+
BIND_ENUM_CONSTANT(THREAD_TYPE_APPLICATION_MAIN);
60+
BIND_ENUM_CONSTANT(THREAD_TYPE_APPLICATION_WORKER);
61+
BIND_ENUM_CONSTANT(THREAD_TYPE_RENDERER_MAIN);
62+
BIND_ENUM_CONSTANT(THREAD_TYPE_RENDERER_WORKER);
63+
}
64+
65+
HashMap<String, bool *> OpenXRAndroidThreadSettingsExtension::get_requested_extensions() {
66+
HashMap<String, bool *> request_extensions;
67+
68+
#ifdef XR_USE_PLATFORM_ANDROID
69+
request_extensions[XR_KHR_ANDROID_THREAD_SETTINGS_EXTENSION_NAME] = &available;
70+
#endif
71+
72+
return request_extensions;
73+
}
74+
75+
void OpenXRAndroidThreadSettingsExtension::on_instance_created(XrInstance p_instance) {
76+
if (!available) {
77+
return;
78+
}
79+
80+
if (!_initialize_openxr_android_thread_settings_extension()) {
81+
print_error("OpenXR: Failed to initialize android thread settings extension");
82+
available = false;
83+
}
84+
}
85+
86+
void OpenXRAndroidThreadSettingsExtension::on_session_created(XrSession p_session) {
87+
if (!available) {
88+
return;
89+
}
90+
91+
// Attempt to mark this thread as the "main thread".
92+
set_application_thread_type(THREAD_TYPE_APPLICATION_MAIN);
93+
94+
// Attempt to mark the render thread too.
95+
RenderingServer *rendering_server = RenderingServer::get_singleton();
96+
ERR_FAIL_NULL(rendering_server);
97+
rendering_server->call_on_render_thread(callable_mp(this, &OpenXRAndroidThreadSettingsExtension::_set_render_thread_type));
98+
}
99+
100+
bool OpenXRAndroidThreadSettingsExtension::set_application_thread_type(ThreadType p_thread_type, uint32_t p_thread_id) {
101+
if (!available) {
102+
return false;
103+
}
104+
105+
#ifdef XR_USE_PLATFORM_ANDROID
106+
XrAndroidThreadTypeKHR thread_type{};
107+
switch (p_thread_type) {
108+
case THREAD_TYPE_APPLICATION_MAIN:
109+
thread_type = XR_ANDROID_THREAD_TYPE_APPLICATION_MAIN_KHR;
110+
break;
111+
case THREAD_TYPE_APPLICATION_WORKER:
112+
thread_type = XR_ANDROID_THREAD_TYPE_APPLICATION_WORKER_KHR;
113+
break;
114+
case THREAD_TYPE_RENDERER_MAIN:
115+
thread_type = XR_ANDROID_THREAD_TYPE_RENDERER_MAIN_KHR;
116+
break;
117+
case THREAD_TYPE_RENDERER_WORKER:
118+
thread_type = XR_ANDROID_THREAD_TYPE_RENDERER_WORKER_KHR;
119+
break;
120+
default:
121+
print_error(vformat("OpenXR: Failed to set android application thread; invalid thread type %d", p_thread_type));
122+
return false;
123+
}
124+
125+
XrResult result = xrSetAndroidApplicationThreadKHR(OpenXRAPI::get_singleton()->get_session(), thread_type, p_thread_id == 0 ? gettid() : p_thread_id);
126+
if (result != XR_SUCCESS) {
127+
print_error(vformat("OpenXR: Failed to set android application thread; %s", OpenXRAPI::get_singleton()->get_error_string(result)));
128+
return false;
129+
}
130+
#endif
131+
132+
return true;
133+
}
134+
135+
bool OpenXRAndroidThreadSettingsExtension::_initialize_openxr_android_thread_settings_extension() {
136+
#ifdef XR_USE_PLATFORM_ANDROID
137+
EXT_INIT_XR_FUNC_V(xrSetAndroidApplicationThreadKHR);
138+
#endif
139+
return true;
140+
}
141+
142+
void OpenXRAndroidThreadSettingsExtension::_set_render_thread_type() {
143+
// Skip when the render thread == the main thread
144+
if (Thread::is_main_thread()) {
145+
return;
146+
}
147+
148+
set_application_thread_type(THREAD_TYPE_RENDERER_MAIN);
149+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**************************************************************************/
2+
/* openxr_android_thread_settings_extension.h */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#pragma once
32+
33+
#include "../openxr_interface.h"
34+
#include "core/templates/hash_map.h"
35+
36+
#include "openxr_extension_wrapper.h"
37+
38+
#ifdef XR_USE_PLATFORM_ANDROID
39+
#include "../util.h"
40+
#include <jni.h>
41+
#include <openxr/openxr_platform.h>
42+
#endif
43+
44+
class OpenXRAndroidThreadSettingsExtension : public OpenXRExtensionWrapper {
45+
GDCLASS(OpenXRAndroidThreadSettingsExtension, OpenXRExtensionWrapper);
46+
47+
public:
48+
static OpenXRAndroidThreadSettingsExtension *get_singleton();
49+
50+
OpenXRAndroidThreadSettingsExtension();
51+
virtual ~OpenXRAndroidThreadSettingsExtension() override;
52+
53+
virtual HashMap<String, bool *> get_requested_extensions() override;
54+
virtual void on_instance_created(XrInstance p_instance) override;
55+
virtual void on_session_created(XrSession p_session) override;
56+
57+
enum ThreadType {
58+
THREAD_TYPE_APPLICATION_MAIN,
59+
THREAD_TYPE_APPLICATION_WORKER,
60+
THREAD_TYPE_RENDERER_MAIN,
61+
THREAD_TYPE_RENDERER_WORKER,
62+
};
63+
bool set_application_thread_type(ThreadType p_thread_type, uint32_t p_thread_id = 0);
64+
65+
protected:
66+
static void _bind_methods();
67+
68+
private:
69+
static OpenXRAndroidThreadSettingsExtension *singleton;
70+
71+
bool _initialize_openxr_android_thread_settings_extension();
72+
void _set_render_thread_type();
73+
74+
bool available = false;
75+
76+
#ifdef XR_USE_PLATFORM_ANDROID
77+
EXT_PROTO_XRRESULT_FUNC3(xrSetAndroidApplicationThreadKHR, (XrSession), session, (XrAndroidThreadTypeKHR), threadType, (uint32_t), threadId);
78+
#endif
79+
};
80+
81+
VARIANT_ENUM_CAST(OpenXRAndroidThreadSettingsExtension::ThreadType)

modules/openxr/register_types.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include "scene/openxr_render_model_manager.h"
5353
#include "scene/openxr_visibility_mask.h"
5454

55+
#include "extensions/openxr_android_thread_settings_extension.h"
5556
#include "extensions/openxr_composition_layer_depth_extension.h"
5657
#include "extensions/openxr_composition_layer_extension.h"
5758
#include "extensions/openxr_debug_utils_extension.h"
@@ -133,6 +134,7 @@ void initialize_openxr_module(ModuleInitializationLevel p_level) {
133134
GDREGISTER_CLASS(OpenXRFutureExtension);
134135
GDREGISTER_CLASS(OpenXRAPIExtension);
135136
GDREGISTER_CLASS(OpenXRRenderModelExtension);
137+
GDREGISTER_CLASS(OpenXRAndroidThreadSettingsExtension);
136138

137139
// Note, we're not registering all wrapper classes here, there is no point in exposing them
138140
// if there isn't specific logic to expose.
@@ -197,6 +199,11 @@ void initialize_openxr_module(ModuleInitializationLevel p_level) {
197199
OpenXRAPI::register_extension_wrapper(frame_synthesis_extension);
198200
Engine::get_singleton()->add_singleton(Engine::Singleton("OpenXRFrameSynthesisExtension", frame_synthesis_extension));
199201

202+
// Register android thread settings extension as a singleton.
203+
OpenXRAndroidThreadSettingsExtension *android_thread_settings = memnew(OpenXRAndroidThreadSettingsExtension);
204+
OpenXRAPI::register_extension_wrapper(android_thread_settings);
205+
Engine::get_singleton()->add_singleton(Engine::Singleton("OpenXRAndroidThreadSettingsExtension", android_thread_settings));
206+
200207
// register gated extensions
201208
if (int(GLOBAL_GET("xr/openxr/extensions/debug_utils")) > 0) {
202209
OpenXRAPI::register_extension_wrapper(memnew(OpenXRDebugUtilsExtension));

0 commit comments

Comments
 (0)