evan 2026.1.4
Evan
Loading...
Searching...
No Matches
AndroidXrPlatform.cpp
1/*
2** ETIB PROJECT, 2026
3** evan
4** File description:
5** AndroidXrPlatform
6*/
7
8#include "evan/openxr/platform/AndroidXrPlatform.hpp"
9#include "evan/openxr/XrDeviceBackend.hpp"
10
11#ifdef __ANDROID__
12 #include <jni.h>
13#endif
14
15namespace
16{
17#ifdef __ANDROID__
29 std::filesystem::path queryJavaCacheDirectory(JNIEnv *environment,
30 jobject activity)
31 {
32 jclass contextClass = environment->GetObjectClass(activity);
33 if (contextClass == nullptr) {
34 return {};
35 }
36
37 jmethodID getCacheDir = environment->GetMethodID(
38 contextClass, "getCacheDir", "()Ljava/io/File;");
39 if (getCacheDir == nullptr || environment->ExceptionCheck()) {
40 return {};
41 }
42
43 jobject cacheFile =
44 environment->CallObjectMethod(activity, getCacheDir);
45 if (cacheFile == nullptr) {
46 return {};
47 }
48
49 jclass fileClass = environment->GetObjectClass(cacheFile);
50 if (fileClass == nullptr) {
51 return {};
52 }
53
54 jmethodID getAbsolutePath = environment->GetMethodID(
55 fileClass, "getAbsolutePath", "()Ljava/lang/String;");
56 if (getAbsolutePath == nullptr || environment->ExceptionCheck()) {
57 return {};
58 }
59
60 auto path = reinterpret_cast<jstring>(
61 environment->CallObjectMethod(cacheFile, getAbsolutePath));
62 if (path == nullptr) {
63 return {};
64 }
65
66 const char *characters = environment->GetStringUTFChars(path, nullptr);
67 if (characters == nullptr) {
68 return {};
69 }
70
71 std::filesystem::path directory(characters);
72 environment->ReleaseStringUTFChars(path, characters);
73 return directory;
74 }
75
91 std::filesystem::path getAndroidCacheDirectory(void *applicationVM,
92 void *activity)
93 {
94 if (applicationVM == nullptr || activity == nullptr) {
95 return {};
96 }
97
98 auto *vm = static_cast<JavaVM *>(applicationVM);
99 auto *instance = static_cast<jobject>(activity);
100 JNIEnv *environment = nullptr;
101 bool attached = false;
102
103 if (vm->GetEnv(reinterpret_cast<void **>(&environment),
104 JNI_VERSION_1_6)
105 != JNI_OK) {
106 if (vm->AttachCurrentThread(&environment, nullptr) != JNI_OK) {
107 return {};
108 }
109 attached = true;
110 }
111
112 std::filesystem::path directory;
113 if (environment->PushLocalFrame(16) == JNI_OK) {
114 directory = queryJavaCacheDirectory(environment, instance);
115 environment->PopLocalFrame(nullptr);
116 }
117
118 if (environment->ExceptionCheck()) {
119 environment->ExceptionClear();
120 directory.clear();
121 }
122
123 if (attached) {
124 vm->DetachCurrentThread();
125 }
126
127 return directory;
128 }
129#endif
130} // namespace
131
134{
135 this->getLogger().info() << "Initializing AndroidXrPlatform with provided "
136 "Android platform data.";
137 _platformData = platformData;
138#ifdef __ANDROID__
140
142 XR_NULL_HANDLE, "xrInitializeLoaderKHR",
143 reinterpret_cast<PFN_xrVoidFunction *>(&initializeLoader))
144 == XR_SUCCESS) {
147 loaderInitInfoAndroid.next = nullptr;
148 loaderInitInfoAndroid.applicationVM = platformData.applicationVM;
149 loaderInitInfoAndroid.applicationContext =
150 platformData.applicationActivity;
152 reinterpret_cast<const XrLoaderInitInfoBaseHeaderKHR *>(
154 }
156 _instanceCreateInfoAndroid.applicationVM = platformData.applicationVM;
157 _instanceCreateInfoAndroid.applicationActivity =
158 platformData.applicationActivity;
159#endif
160}
161
163// Public Methods //
165
166std::vector<std::string>
168{
169 std::vector<std::string> extensions = {
171#ifdef XR_USE_PLATFORM_ANDROID
173#endif
174 };
175 return extensions;
176}
177
179{
180#ifdef __ANDROID__
181 return reinterpret_cast<const XrBaseInStructure *>(
183#else
184 return nullptr;
185#endif
186}
187
189// Protected Methods //
191
193{
194#ifdef __ANDROID__
195 return getAndroidCacheDirectory(_platformData.applicationVM,
196 _platformData.applicationActivity);
197#else
198 return {};
199#endif
200}
AndroidXrPlatform(const AndroidPlatformData &platformData)
Constructs an AndroidXrPlatform instance with the specified Android platform data.
const XrBaseInStructure * getInstanceCreateInfo() const override
Retrieves the Android-specific XR instance creation information.
std::filesystem::path getDefaultCacheRoot() const override
Get the cache root directory of the Android application.
std::vector< std::string > getRequiredInstanceExtensions() const override
Retrieves the list of required OpenXR instance extensions for Android platform.
Struct to hold Android-specific platform data for OpenXR initialization.
Small result wrapper used by helpers that must return both a status and a value (e....
Definition Error.hpp:89