openxr_android_extension.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*************************************************************************/
  2. /* openxr_android_extension.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "java_godot_wrapper.h"
  32. #include "os_android.h"
  33. #include "thread_jandroid.h"
  34. #include <jni.h>
  35. #include <modules/openxr/openxr_api.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(OpenXRAPI *p_openxr_api) :
  43. OpenXRExtensionWrapper(p_openxr_api) {
  44. singleton = this;
  45. request_extensions[XR_KHR_LOADER_INIT_ANDROID_EXTENSION_NAME] = &loader_init_extension_available;
  46. request_extensions[XR_KHR_ANDROID_CREATE_INSTANCE_EXTENSION_NAME] = &create_instance_extension_available;
  47. }
  48. void OpenXRAndroidExtension::on_before_instance_created() {
  49. if (!loader_init_extension_available) {
  50. print_line("OpenXR: XR_KHR_loader_init_android is not reported as available - trying to initialize anyway...");
  51. }
  52. EXT_INIT_XR_FUNC(xrInitializeLoaderKHR);
  53. JNIEnv *env = get_jni_env();
  54. JavaVM *vm;
  55. env->GetJavaVM(&vm);
  56. jobject activity_object = env->NewGlobalRef(static_cast<OS_Android *>(OS::get_singleton())->get_godot_java()->get_activity());
  57. XrLoaderInitInfoAndroidKHR loader_init_info_android = {
  58. .type = XR_TYPE_LOADER_INIT_INFO_ANDROID_KHR,
  59. .next = nullptr,
  60. .applicationVM = vm,
  61. .applicationContext = activity_object
  62. };
  63. XrResult result = xrInitializeLoaderKHR((const XrLoaderInitInfoBaseHeaderKHR *)&loader_init_info_android);
  64. ERR_FAIL_COND_MSG(XR_FAILED(result), "Failed to call xrInitializeLoaderKHR");
  65. }
  66. // We're keeping the Android create info struct here to avoid including openxr_platform.h in a header, which would break other extensions.
  67. // This is reasonably safe as the struct is only used during intialization and the extension is a singleton.
  68. static XrInstanceCreateInfoAndroidKHR instance_create_info;
  69. void *OpenXRAndroidExtension::set_instance_create_info_and_get_next_pointer(void *p_next_pointer) {
  70. if (!create_instance_extension_available) {
  71. return nullptr;
  72. }
  73. JNIEnv *env = get_jni_env();
  74. JavaVM *vm;
  75. env->GetJavaVM(&vm);
  76. jobject activity_object = env->NewGlobalRef(static_cast<OS_Android *>(OS::get_singleton())->get_godot_java()->get_activity());
  77. instance_create_info = {
  78. .type = XR_TYPE_INSTANCE_CREATE_INFO_ANDROID_KHR,
  79. .next = p_next_pointer,
  80. .applicationVM = vm,
  81. .applicationActivity = activity_object
  82. };
  83. return &instance_create_info;
  84. }
  85. OpenXRAndroidExtension::~OpenXRAndroidExtension() {
  86. singleton = nullptr;
  87. }