openxr_android_extension.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**************************************************************************/
  2. /* openxr_android_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_extension.h"
  31. #include "../../openxr_api.h"
  32. #include "java_godot_wrapper.h"
  33. #include "os_android.h"
  34. #include "thread_jandroid.h"
  35. #include <openxr/openxr.h>
  36. #include <openxr/openxr_platform.h>
  37. OpenXRAndroidExtension *OpenXRAndroidExtension::singleton = nullptr;
  38. OpenXRAndroidExtension *OpenXRAndroidExtension::get_singleton() {
  39. return singleton;
  40. }
  41. OpenXRAndroidExtension::OpenXRAndroidExtension() {
  42. singleton = this;
  43. JNIEnv *env = get_jni_env();
  44. ERR_FAIL_NULL(env);
  45. env->GetJavaVM(&vm);
  46. activity_object = env->NewGlobalRef(static_cast<OS_Android *>(OS::get_singleton())->get_godot_java()->get_activity());
  47. }
  48. HashMap<String, bool *> OpenXRAndroidExtension::get_requested_extensions() {
  49. HashMap<String, bool *> request_extensions;
  50. // XR_KHR_LOADER_INIT_EXTENSION_NAME is a dependency of XR_KHR_LOADER_INIT_ANDROID_EXTENSION_NAME
  51. request_extensions[XR_KHR_LOADER_INIT_EXTENSION_NAME] = &loader_init_extension_available;
  52. request_extensions[XR_KHR_LOADER_INIT_ANDROID_EXTENSION_NAME] = &loader_init_android_extension_available;
  53. request_extensions[XR_KHR_ANDROID_CREATE_INSTANCE_EXTENSION_NAME] = &create_instance_extension_available;
  54. return request_extensions;
  55. }
  56. void OpenXRAndroidExtension::on_before_instance_created() {
  57. if (XR_FAILED(EXT_TRY_INIT_XR_FUNC(xrInitializeLoaderKHR))) {
  58. // XR_KHR_loader_init not supported on this platform
  59. return;
  60. }
  61. loader_init_android_extension_available = true;
  62. XrLoaderInitInfoAndroidKHR loader_init_info_android = {
  63. .type = XR_TYPE_LOADER_INIT_INFO_ANDROID_KHR,
  64. .next = nullptr,
  65. .applicationVM = vm,
  66. .applicationContext = activity_object
  67. };
  68. XrResult result = xrInitializeLoaderKHR((const XrLoaderInitInfoBaseHeaderKHR *)&loader_init_info_android);
  69. ERR_FAIL_COND_MSG(XR_FAILED(result), "Failed to call xrInitializeLoaderKHR");
  70. }
  71. // We're keeping the Android create info struct here to avoid including openxr_platform.h in a header, which would break other extensions.
  72. // This is reasonably safe as the struct is only used during initialization and the extension is a singleton.
  73. static XrInstanceCreateInfoAndroidKHR instance_create_info;
  74. void *OpenXRAndroidExtension::set_instance_create_info_and_get_next_pointer(void *p_next_pointer) {
  75. if (!create_instance_extension_available) {
  76. if (!loader_init_android_extension_available) {
  77. WARN_PRINT("No Android extensions available, couldn't pass JVM and Activity to OpenXR");
  78. }
  79. return nullptr;
  80. }
  81. instance_create_info = {
  82. .type = XR_TYPE_INSTANCE_CREATE_INFO_ANDROID_KHR,
  83. .next = p_next_pointer,
  84. .applicationVM = vm,
  85. .applicationActivity = activity_object
  86. };
  87. return &instance_create_info;
  88. }
  89. OpenXRAndroidExtension::~OpenXRAndroidExtension() {
  90. singleton = nullptr;
  91. JNIEnv *env = get_jni_env();
  92. ERR_FAIL_NULL(env);
  93. env->DeleteGlobalRef(activity_object);
  94. }