openxr_android_extension.cpp 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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] = nullptr; // must be available
  46. request_extensions[XR_KHR_ANDROID_CREATE_INSTANCE_EXTENSION_NAME] = &create_instance_extension_available;
  47. }
  48. void OpenXRAndroidExtension::on_before_instance_created() {
  49. EXT_INIT_XR_FUNC(xrInitializeLoaderKHR);
  50. JNIEnv *env = get_jni_env();
  51. JavaVM *vm;
  52. env->GetJavaVM(&vm);
  53. jobject activity_object = env->NewGlobalRef(static_cast<OS_Android *>(OS::get_singleton())->get_godot_java()->get_activity());
  54. XrLoaderInitInfoAndroidKHR loader_init_info_android = {
  55. .type = XR_TYPE_LOADER_INIT_INFO_ANDROID_KHR,
  56. .next = nullptr,
  57. .applicationVM = vm,
  58. .applicationContext = activity_object
  59. };
  60. XrResult result = xrInitializeLoaderKHR((const XrLoaderInitInfoBaseHeaderKHR *)&loader_init_info_android);
  61. ERR_FAIL_COND_MSG(XR_FAILED(result), "Failed to call xrInitializeLoaderKHR");
  62. }
  63. // We're keeping the Android create info struct here to avoid including openxr_platform.h in a header, which would break other extensions.
  64. // This is reasonably safe as the struct is only used during intialization and the extension is a singleton.
  65. static XrInstanceCreateInfoAndroidKHR instance_create_info;
  66. void *OpenXRAndroidExtension::set_instance_create_info_and_get_next_pointer(void *p_next_pointer) {
  67. if (!create_instance_extension_available) {
  68. return nullptr;
  69. }
  70. JNIEnv *env = get_jni_env();
  71. JavaVM *vm;
  72. env->GetJavaVM(&vm);
  73. jobject activity_object = env->NewGlobalRef(static_cast<OS_Android *>(OS::get_singleton())->get_godot_java()->get_activity());
  74. instance_create_info = {
  75. .type = XR_TYPE_INSTANCE_CREATE_INFO_ANDROID_KHR,
  76. .next = p_next_pointer,
  77. .applicationVM = vm,
  78. .applicationActivity = activity_object
  79. };
  80. return &instance_create_info;
  81. }
  82. OpenXRAndroidExtension::~OpenXRAndroidExtension() {
  83. singleton = nullptr;
  84. }