• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 Google Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <android/asset_manager.h>
18 #include <android/asset_manager_jni.h>
19 #include <jni.h>
20 
21 #include "platform_tools/android/apps/arcore/src/main/cpp/hello_ar_application.h"
22 
23 #define JNI_METHOD(return_type, method_name) \
24   JNIEXPORT return_type JNICALL              \
25       Java_org_skia_arcore_JniInterface_##method_name
26 
27 extern "C" {
28 
29 namespace {
30 // maintain a reference to the JVM so we can use it later.
31     static JavaVM *g_vm = nullptr;
32 
jptr(hello_ar::HelloArApplication * native_hello_ar_application)33     inline jlong jptr(hello_ar::HelloArApplication *native_hello_ar_application) {
34         return reinterpret_cast<intptr_t>(native_hello_ar_application);
35     }
36 
native(jlong ptr)37     inline hello_ar::HelloArApplication *native(jlong ptr) {
38         return reinterpret_cast<hello_ar::HelloArApplication *>(ptr);
39     }
40 
41 }  // namespace
42 
JNI_OnLoad(JavaVM * vm,void *)43 jint JNI_OnLoad(JavaVM *vm, void *) {
44     g_vm = vm;
45     return JNI_VERSION_1_6;
46 }
47 
JNI_METHOD(jlong,createNativeApplication)48 JNI_METHOD(jlong, createNativeApplication)
49 (JNIEnv *env, jclass, jobject j_asset_manager) {
50     AAssetManager *asset_manager = AAssetManager_fromJava(env, j_asset_manager);
51     return jptr(new hello_ar::HelloArApplication(asset_manager));
52 }
53 
JNI_METHOD(void,destroyNativeApplication)54 JNI_METHOD(void, destroyNativeApplication)
55 (JNIEnv *, jclass, jlong native_application) {
56     delete native(native_application);
57 }
58 
JNI_METHOD(void,onPause)59 JNI_METHOD(void, onPause)
60 (JNIEnv *, jclass, jlong native_application) {
61     native(native_application)->OnPause();
62 }
63 
JNI_METHOD(void,onResume)64 JNI_METHOD(void, onResume)
65 (JNIEnv *env, jclass, jlong native_application, jobject context,
66  jobject activity) {
67     native(native_application)->OnResume(env, context, activity);
68 }
69 
JNI_METHOD(void,onGlSurfaceCreated)70 JNI_METHOD(void, onGlSurfaceCreated)
71 (JNIEnv *, jclass, jlong native_application) {
72     native(native_application)->OnSurfaceCreated();
73 }
74 
JNI_METHOD(void,onDisplayGeometryChanged)75 JNI_METHOD(void, onDisplayGeometryChanged)
76 (JNIEnv *, jobject, jlong native_application, int display_rotation, int width,
77  int height) {
78     native(native_application)
79             ->OnDisplayGeometryChanged(display_rotation, width, height);
80 }
81 
JNI_METHOD(void,onObjectRotationChanged)82 JNI_METHOD(void, onObjectRotationChanged)
83 (JNIEnv *, jobject, jlong native_application, int rotation) {
84     native(native_application)
85             ->OnObjectRotationChanged(rotation);
86 }
87 
JNI_METHOD(void,onAction)88 JNI_METHOD(void, onAction)
89 (JNIEnv *, jobject, jlong native_application, jfloat value) {
90     native(native_application)->OnAction(value);
91 }
92 
JNI_METHOD(void,onGlSurfaceDrawFrame)93 JNI_METHOD(void, onGlSurfaceDrawFrame)
94 (JNIEnv *, jclass, jlong native_application) {
95     native(native_application)->OnDrawFrame();
96 }
97 
JNI_METHOD(void,onTouchTranslate)98 JNI_METHOD(void, onTouchTranslate)
99 (JNIEnv *, jclass, jlong native_application, jfloat x, jfloat y) {
100     return native(native_application)->OnTouchTranslate(x, y);
101 }
102 
JNI_METHOD(bool,onTouchedFirst)103 JNI_METHOD(bool, onTouchedFirst)
104 (JNIEnv *, jclass, jlong native_application, jfloat x, jfloat y, int drawMode) {
105     return native(native_application)->OnTouchedFirst(x, y, drawMode);
106 }
107 
JNI_METHOD(void,onTouchedFinal)108 JNI_METHOD(void, onTouchedFinal)
109 (JNIEnv *, jclass, jlong native_application, int type) {
110     native(native_application)->OnTouchedFinal(type);
111 }
112 
JNI_METHOD(jboolean,hasDetectedPlanes)113 JNI_METHOD(jboolean, hasDetectedPlanes)
114 (JNIEnv *, jclass, jlong native_application) {
115     return static_cast<jboolean>(
116             native(native_application)->HasDetectedPlanes() ? JNI_TRUE : JNI_FALSE);
117 }
118 
GetJniEnv()119 JNIEnv *GetJniEnv() {
120     JNIEnv *env;
121     jint result = g_vm->AttachCurrentThread(&env, nullptr);
122     return result == JNI_OK ? env : nullptr;
123 }
124 
FindClass(const char * classname)125 jclass FindClass(const char *classname) {
126     JNIEnv *env = GetJniEnv();
127     return env->FindClass(classname);
128 }
129 
130 }  // extern "C"
131