openxr_android_thread_settings_extension.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #include "openxr_android_thread_settings_extension.h"
  31. #include "core/object/callable_method_pointer.h"
  32. #include "core/string/print_string.h"
  33. #include "servers/rendering/rendering_server.h"
  34. #ifdef XR_USE_PLATFORM_ANDROID
  35. #include "../openxr_api.h"
  36. #include <unistd.h>
  37. #endif
  38. OpenXRAndroidThreadSettingsExtension *OpenXRAndroidThreadSettingsExtension::singleton = nullptr;
  39. OpenXRAndroidThreadSettingsExtension *OpenXRAndroidThreadSettingsExtension::get_singleton() {
  40. return singleton;
  41. }
  42. OpenXRAndroidThreadSettingsExtension::OpenXRAndroidThreadSettingsExtension() {
  43. singleton = this;
  44. }
  45. OpenXRAndroidThreadSettingsExtension::~OpenXRAndroidThreadSettingsExtension() {
  46. singleton = nullptr;
  47. }
  48. void OpenXRAndroidThreadSettingsExtension::_bind_methods() {
  49. ClassDB::bind_method(D_METHOD("set_application_thread_type", "thread_type", "thread_id"), &OpenXRAndroidThreadSettingsExtension::set_application_thread_type, DEFVAL(0));
  50. BIND_ENUM_CONSTANT(THREAD_TYPE_APPLICATION_MAIN);
  51. BIND_ENUM_CONSTANT(THREAD_TYPE_APPLICATION_WORKER);
  52. BIND_ENUM_CONSTANT(THREAD_TYPE_RENDERER_MAIN);
  53. BIND_ENUM_CONSTANT(THREAD_TYPE_RENDERER_WORKER);
  54. }
  55. HashMap<String, bool *> OpenXRAndroidThreadSettingsExtension::get_requested_extensions(XrVersion p_version) {
  56. HashMap<String, bool *> request_extensions;
  57. #ifdef XR_USE_PLATFORM_ANDROID
  58. request_extensions[XR_KHR_ANDROID_THREAD_SETTINGS_EXTENSION_NAME] = &available;
  59. #endif
  60. return request_extensions;
  61. }
  62. void OpenXRAndroidThreadSettingsExtension::on_instance_created(XrInstance p_instance) {
  63. if (!available) {
  64. return;
  65. }
  66. if (!_initialize_openxr_android_thread_settings_extension()) {
  67. print_error("OpenXR: Failed to initialize android thread settings extension");
  68. available = false;
  69. }
  70. }
  71. void OpenXRAndroidThreadSettingsExtension::on_session_created(XrSession p_session) {
  72. if (!available) {
  73. return;
  74. }
  75. // Attempt to mark this thread as the "main thread".
  76. set_application_thread_type(THREAD_TYPE_APPLICATION_MAIN);
  77. // Attempt to mark the render thread too.
  78. RenderingServer *rendering_server = RenderingServer::get_singleton();
  79. ERR_FAIL_NULL(rendering_server);
  80. rendering_server->call_on_render_thread(callable_mp(this, &OpenXRAndroidThreadSettingsExtension::_set_render_thread_type));
  81. }
  82. bool OpenXRAndroidThreadSettingsExtension::set_application_thread_type(ThreadType p_thread_type, uint32_t p_thread_id) {
  83. if (!available) {
  84. return false;
  85. }
  86. #ifdef XR_USE_PLATFORM_ANDROID
  87. XrAndroidThreadTypeKHR thread_type{};
  88. switch (p_thread_type) {
  89. case THREAD_TYPE_APPLICATION_MAIN:
  90. thread_type = XR_ANDROID_THREAD_TYPE_APPLICATION_MAIN_KHR;
  91. break;
  92. case THREAD_TYPE_APPLICATION_WORKER:
  93. thread_type = XR_ANDROID_THREAD_TYPE_APPLICATION_WORKER_KHR;
  94. break;
  95. case THREAD_TYPE_RENDERER_MAIN:
  96. thread_type = XR_ANDROID_THREAD_TYPE_RENDERER_MAIN_KHR;
  97. break;
  98. case THREAD_TYPE_RENDERER_WORKER:
  99. thread_type = XR_ANDROID_THREAD_TYPE_RENDERER_WORKER_KHR;
  100. break;
  101. default:
  102. print_error(vformat("OpenXR: Failed to set android application thread; invalid thread type %d", p_thread_type));
  103. return false;
  104. }
  105. XrResult result = xrSetAndroidApplicationThreadKHR(OpenXRAPI::get_singleton()->get_session(), thread_type, p_thread_id == 0 ? gettid() : p_thread_id);
  106. if (result != XR_SUCCESS) {
  107. print_error(vformat("OpenXR: Failed to set android application thread; %s", OpenXRAPI::get_singleton()->get_error_string(result)));
  108. return false;
  109. }
  110. #endif
  111. return true;
  112. }
  113. bool OpenXRAndroidThreadSettingsExtension::_initialize_openxr_android_thread_settings_extension() {
  114. #ifdef XR_USE_PLATFORM_ANDROID
  115. EXT_INIT_XR_FUNC_V(xrSetAndroidApplicationThreadKHR);
  116. #endif
  117. return true;
  118. }
  119. void OpenXRAndroidThreadSettingsExtension::_set_render_thread_type() {
  120. // Skip when the render thread == the main thread
  121. if (Thread::is_main_thread()) {
  122. return;
  123. }
  124. set_application_thread_type(THREAD_TYPE_RENDERER_MAIN);
  125. }