1 #include "jni.h"
2 #include <android_runtime/AndroidRuntime.h>
3 
4 #include "SkCamera.h"
5 
6 #include "Canvas.h"
7 #include "GraphicsJNI.h"
8 
9 static jfieldID gNativeInstanceFieldID;
10 
Camera_constructor(JNIEnv * env,jobject obj)11 static void Camera_constructor(JNIEnv* env, jobject obj) {
12     Sk3DView* view = new Sk3DView;
13     env->SetLongField(obj, gNativeInstanceFieldID, reinterpret_cast<jlong>(view));
14 }
15 
Camera_destructor(JNIEnv * env,jobject obj)16 static void Camera_destructor(JNIEnv* env, jobject obj) {
17     jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID);
18     Sk3DView* view = reinterpret_cast<Sk3DView*>(viewHandle);
19     delete view;
20 }
21 
Camera_save(JNIEnv * env,jobject obj)22 static void Camera_save(JNIEnv* env, jobject obj) {
23     jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID);
24     Sk3DView* v = reinterpret_cast<Sk3DView*>(viewHandle);
25     v->save();
26 }
27 
Camera_restore(JNIEnv * env,jobject obj)28 static void Camera_restore(JNIEnv* env, jobject obj) {
29     jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID);
30     Sk3DView* v = reinterpret_cast<Sk3DView*>(viewHandle);
31     v->restore();
32 }
33 
Camera_translate(JNIEnv * env,jobject obj,jfloat dx,jfloat dy,jfloat dz)34 static void Camera_translate(JNIEnv* env, jobject obj,
35                              jfloat dx, jfloat dy, jfloat dz) {
36     jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID);
37     Sk3DView* v = reinterpret_cast<Sk3DView*>(viewHandle);
38     v->translate(dx, dy, dz);
39 }
40 
Camera_rotateX(JNIEnv * env,jobject obj,jfloat degrees)41 static void Camera_rotateX(JNIEnv* env, jobject obj, jfloat degrees) {
42     jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID);
43     Sk3DView* v = reinterpret_cast<Sk3DView*>(viewHandle);
44     v->rotateX(degrees);
45 }
46 
Camera_rotateY(JNIEnv * env,jobject obj,jfloat degrees)47 static void Camera_rotateY(JNIEnv* env, jobject obj, jfloat degrees) {
48     jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID);
49     Sk3DView* v = reinterpret_cast<Sk3DView*>(viewHandle);
50     v->rotateY(degrees);
51 }
52 
Camera_rotateZ(JNIEnv * env,jobject obj,jfloat degrees)53 static void Camera_rotateZ(JNIEnv* env, jobject obj, jfloat degrees) {
54     jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID);
55     Sk3DView* v = reinterpret_cast<Sk3DView*>(viewHandle);
56     v->rotateZ(degrees);
57 }
58 
Camera_rotate(JNIEnv * env,jobject obj,jfloat x,jfloat y,jfloat z)59 static void Camera_rotate(JNIEnv* env, jobject obj, jfloat x, jfloat y, jfloat z) {
60     jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID);
61     Sk3DView* v = reinterpret_cast<Sk3DView*>(viewHandle);
62     v->rotateX(x);
63     v->rotateY(y);
64     v->rotateZ(z);
65 }
66 
Camera_setLocation(JNIEnv * env,jobject obj,jfloat x,jfloat y,jfloat z)67 static void Camera_setLocation(JNIEnv* env, jobject obj, jfloat x, jfloat y, jfloat z) {
68     jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID);
69     Sk3DView* v = reinterpret_cast<Sk3DView*>(viewHandle);
70     v->setCameraLocation(x, y, z);
71 }
72 
Camera_getLocationX(JNIEnv * env,jobject obj)73 static jfloat Camera_getLocationX(JNIEnv* env, jobject obj) {
74     jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID);
75     Sk3DView* v = reinterpret_cast<Sk3DView*>(viewHandle);
76     return SkScalarToFloat(v->getCameraLocationX());
77 }
78 
Camera_getLocationY(JNIEnv * env,jobject obj)79 static jfloat Camera_getLocationY(JNIEnv* env, jobject obj) {
80     jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID);
81     Sk3DView* v = reinterpret_cast<Sk3DView*>(viewHandle);
82     return SkScalarToFloat(v->getCameraLocationY());
83 }
84 
Camera_getLocationZ(JNIEnv * env,jobject obj)85 static jfloat Camera_getLocationZ(JNIEnv* env, jobject obj) {
86     jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID);
87     Sk3DView* v = reinterpret_cast<Sk3DView*>(viewHandle);
88     return SkScalarToFloat(v->getCameraLocationZ());
89 }
90 
Camera_getMatrix(JNIEnv * env,jobject obj,jlong matrixHandle)91 static void Camera_getMatrix(JNIEnv* env, jobject obj, jlong matrixHandle) {
92     SkMatrix* native_matrix =  reinterpret_cast<SkMatrix*>(matrixHandle);
93     jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID);
94     Sk3DView* v = reinterpret_cast<Sk3DView*>(viewHandle);
95     v->getMatrix(native_matrix);
96 }
97 
Camera_applyToCanvas(JNIEnv * env,jobject obj,jlong canvasHandle)98 static void Camera_applyToCanvas(JNIEnv* env, jobject obj, jlong canvasHandle) {
99     SkCanvas* canvas = reinterpret_cast<android::Canvas*>(canvasHandle)->getSkCanvas();
100     jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID);
101     Sk3DView* v = reinterpret_cast<Sk3DView*>(viewHandle);
102     v->applyToCanvas(canvas);
103 }
104 
Camera_dotWithNormal(JNIEnv * env,jobject obj,jfloat x,jfloat y,jfloat z)105 static jfloat Camera_dotWithNormal(JNIEnv* env, jobject obj,
106                                   jfloat x, jfloat y, jfloat z) {
107     jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID);
108     Sk3DView* v = reinterpret_cast<Sk3DView*>(viewHandle);
109     SkScalar dot = v->dotWithNormal(x, y, z);
110     return SkScalarToFloat(dot);
111 }
112 
113 // ----------------------------------------------------------------------------
114 
115 /*
116  * JNI registration.
117  */
118 static JNINativeMethod gCameraMethods[] = {
119     /* name, signature, funcPtr */
120 
121     { "nativeConstructor",   "()V",    (void*)Camera_constructor   },
122     { "nativeDestructor",    "()V",    (void*)Camera_destructor    },
123     { "save",                "()V",    (void*)Camera_save          },
124     { "restore",             "()V",    (void*)Camera_restore       },
125     { "translate",           "(FFF)V", (void*)Camera_translate     },
126     { "rotateX",             "(F)V",   (void*)Camera_rotateX       },
127     { "rotateY",             "(F)V",   (void*)Camera_rotateY       },
128     { "rotateZ",             "(F)V",   (void*)Camera_rotateZ       },
129     { "rotate",              "(FFF)V", (void*)Camera_rotate        },
130     { "setLocation",         "(FFF)V", (void*)Camera_setLocation   },
131     { "getLocationX",        "()F",    (void*)Camera_getLocationX  },
132     { "getLocationY",        "()F",    (void*)Camera_getLocationY  },
133     { "getLocationZ",        "()F",    (void*)Camera_getLocationZ  },
134     { "nativeGetMatrix",     "(J)V",   (void*)Camera_getMatrix     },
135     { "nativeApplyToCanvas", "(J)V",   (void*)Camera_applyToCanvas },
136     { "dotWithNormal",       "(FFF)F", (void*)Camera_dotWithNormal }
137 };
138 
register_android_graphics_Camera(JNIEnv * env)139 int register_android_graphics_Camera(JNIEnv* env) {
140     jclass clazz = env->FindClass("android/graphics/Camera");
141     if (clazz == 0) {
142         return -1;
143     }
144     gNativeInstanceFieldID = env->GetFieldID(clazz, "native_instance", "J");
145     if (gNativeInstanceFieldID == 0) {
146         return -1;
147     }
148     return android::AndroidRuntime::registerNativeMethods(env,
149                                                "android/graphics/Camera",
150                                                gCameraMethods,
151                                                SK_ARRAY_COUNT(gCameraMethods));
152 }
153