Skip to content

Commit bf060e6

Browse files
committed
Implement access to Java methods for C++ side
This commit implements the needed methods for the ARCoreInterface class such as * `get_activity()` * `get_surface()` * `is_activity_resumed()` * `get_display_rotation()` -> will be added in ongoing upstream PR: godotengine/godot#77559
1 parent 2210b8d commit bf060e6

File tree

7 files changed

+90
-13
lines changed

7 files changed

+90
-13
lines changed

plugin/src/main/cpp/android_util.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include <android/log.h>
55

6-
#define LOG_TAG "GodotOpenXRPlugin"
6+
#define LOG_TAG "ARCorePlugin"
77

88
#define ALOG_ASSERT(_cond, ...) \
99
if (!(_cond)) \

plugin/src/main/cpp/jni/arcore_jni.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
#define JNI_CLASS_NAME ARCorePlugin
1111

1212
extern "C" {
13-
JNIEXPORT void JNICALL JNI_METHOD(initializeWrapper)(JNIEnv *env, jobject object) {
14-
ARCorePluginWrapper::initialize_wrapper(env, object);
13+
JNIEXPORT void JNICALL JNI_METHOD(initializeWrapper)(JNIEnv *env, jobject arcore_plugin, jobject activity, jobject godot_instance) {
14+
ARCorePluginWrapper::initialize_wrapper(env, activity, godot_instance, arcore_plugin);
1515
}
1616

1717
JNIEXPORT void JNICALL JNI_METHOD(uninitializeWrapper)(JNIEnv *env, jobject) {

plugin/src/main/cpp/jni/arcore_plugin_wrapper.cpp

+66-4
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,87 @@
22

33
#include "../android_util.h"
44
#include <godot_cpp/godot.hpp>
5+
#include <godot_cpp/classes/os.hpp>
56

67
jobject ARCorePluginWrapper::arcore_plugin_instance = nullptr;
78

8-
ARCorePluginWrapper::ARCorePluginWrapper() {}
9+
ARCorePluginWrapper::ARCorePluginWrapper() {
10+
_get_surface = nullptr;
11+
_is_activity_resumed = nullptr;
12+
_get_display_rotation = nullptr;
13+
}
914

1015
ARCorePluginWrapper::~ARCorePluginWrapper() {}
1116

12-
void ARCorePluginWrapper::initialize_wrapper(JNIEnv *env, jobject arcore_plugin) {
13-
arcore_plugin_instance = env->NewGlobalRef(arcore_plugin);
17+
void ARCorePluginWrapper::initialize_wrapper(JNIEnv *p_env, jobject p_activity, jobject p_godot_instance, jobject p_arcore_plugin) {
18+
arcore_plugin_instance = p_env->NewGlobalRef(p_arcore_plugin);
1419
ALOG_ASSERT(arcore_plugin_instance, "Invalid jobject value.");
1520

16-
jclass arcore_plugin_class = env->GetObjectClass(arcore_plugin_instance);
21+
jclass arcore_plugin_class = p_env->GetObjectClass(arcore_plugin_instance);
1722
ALOG_ASSERT(arcore_plugin_class, "Invalid jclass value.");
23+
24+
godot_instance = p_env->NewGlobalRef(p_godot_instance);
25+
activity = p_env->NewGlobalRef(p_activity);
26+
27+
// get info about our Godot class so we can get pointers and stuff...
28+
godot_class = p_env->FindClass("org/godotengine/godot/Godot");
29+
if (godot_class) {
30+
godot_class = (jclass)p_env->NewGlobalRef(godot_class);
31+
} else {
32+
// this is a pretty serious fail.. bail... pointers will stay 0
33+
return;
34+
}
35+
activity_class = p_env->FindClass("android/app/Activity");
36+
if (activity_class) {
37+
activity_class = (jclass)p_env->NewGlobalRef(activity_class);
38+
} else {
39+
// this is a pretty serious fail.. bail... pointers will stay 0
40+
return;
41+
}
42+
43+
// get some Godot method pointers...
44+
_get_surface = p_env->GetMethodID(godot_class, "getSurface", "()Landroid/view/Surface;");
45+
_is_activity_resumed = p_env->GetMethodID(godot_class, "isActivityResumed", "()Z");
46+
_get_display_rotation = p_env->GetMethodID(godot_class, "getDisplayRotation", "()I");
1847
}
1948

2049
void ARCorePluginWrapper::uninitialize_wrapper(JNIEnv *env) {
2150
if (arcore_plugin_instance) {
2251
env->DeleteGlobalRef(arcore_plugin_instance);
2352

2453
arcore_plugin_instance = nullptr;
54+
55+
env->DeleteGlobalRef(godot_instance);
56+
env->DeleteGlobalRef(godot_class);
57+
env->DeleteGlobalRef(activity);
58+
env->DeleteGlobalRef(activity_class);
59+
}
60+
}
61+
62+
jobject ARCorePluginWrapper::get_activity() {
63+
return activity;
64+
}
65+
66+
jobject ARCorePluginWrapper::get_surface(JNIEnv *p_env) {
67+
if (_get_surface) {
68+
return p_env->CallObjectMethod(godot_instance, _get_surface);
69+
} else {
70+
return nullptr;
71+
}
72+
}
73+
74+
bool ARCorePluginWrapper::is_activity_resumed(JNIEnv *p_env) {
75+
if (_is_activity_resumed) {
76+
return p_env->CallBooleanMethod(godot_instance, _is_activity_resumed);
77+
} else {
78+
return false;
79+
}
80+
}
81+
82+
int ARCorePluginWrapper::get_display_rotation(JNIEnv *p_env) {
83+
if (_get_display_rotation) {
84+
return p_env->CallIntMethod(godot_instance, _get_display_rotation);
85+
} else {
86+
return 0;
2587
}
2688
}

plugin/src/main/cpp/jni/arcore_plugin_wrapper.h

+16-3
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,29 @@
55

66
class ARCorePluginWrapper {
77
public:
8-
static void initialize_wrapper(JNIEnv *env, jobject arcore_plugin);
8+
static void initialize_wrapper(JNIEnv *env, jobject activity, jobject p_godot_instance, jobject arcore_plugin);
99

1010
static void uninitialize_wrapper(JNIEnv *env);
1111

12-
1312
private:
13+
static jobject arcore_plugin_instance;
14+
static jobject godot_instance;
15+
static jobject activity;
16+
static jclass godot_class;
17+
static jclass activity_class;
18+
19+
static jmethodID _get_surface;
20+
static jmethodID _is_activity_resumed;
21+
static jmethodID _get_display_rotation;
22+
23+
public:
1424
ARCorePluginWrapper();
1525
~ARCorePluginWrapper();
1626

17-
static jobject arcore_plugin_instance;
27+
jobject get_activity();
28+
jobject get_surface(JNIEnv *p_env);
29+
bool is_activity_resumed(JNIEnv *p_env);
30+
int get_display_rotation(JNIEnv *p_env);
1831
};
1932

2033
#endif // ARCORE_PLUGIN_WRAPPER_H

plugin/src/main/cpp/jni/jni_util.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
#include <jni.h>
55
#include <godot_cpp/variant/array.hpp>
6-
#include <godot_cpp/core/variant/string.hpp>
7-
#include <godot_cpp/core/variant/variant.hpp>
6+
#include <godot_cpp/variant/string.hpp>
7+
#include <godot_cpp/variant/variant.hpp>
88
#include <vector>
99

1010
/** Auxiliary macros */

plugin/src/main/java/org/godotengine/plugin/arcore/ARCorePlugin.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public Set<String> getPluginGDExtensionLibrariesPaths() {
4040
return Set.of("addons/godot_arcore/godot_arcore.gdextension");
4141
}
4242

43-
private native void initializeWrapper();
43+
private native void initializeWrapper(Activity activity, Godot godot_instance);
4444

4545
private native void uninitializeWrapper();
4646
}

plugin/src/main/java/org/godotengine/plugin/arcore/ARCoreSetupActivity.java

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
/**
1414
* Activity used to setup ARCore prior to initializing the Godot engine.
15+
*
16+
* Needs to be refactored since Godot itself won't be an activity itself.
1517
*/
1618
public class ARCoreSetupActivity extends Activity {
1719
static final String PREVIOUS_ACTIVITY_START_INTENT_KEY = "previous_activity_start_intent";

0 commit comments

Comments
 (0)