Debian Codegraph @

modules/openxr/extensions/openxr_android_thread_settings_extension.cpp

raw symbols SCIP
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(XrVersion p_version) {
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 }