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