openxr_android_extension.cpp 4.8 KB

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