1 #include "jni.h"
2 #include "core_jni_helpers.h"
3
4 #include "SkCamera.h"
5
6 #include "GraphicsJNI.h"
7 #include <hwui/Canvas.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 android::Canvas* canvas = reinterpret_cast<android::Canvas*>(canvasHandle);
100 jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID);
101 Sk3DView* v = reinterpret_cast<Sk3DView*>(viewHandle);
102 SkMatrix matrix;
103 v->getMatrix(&matrix);
104 canvas->concat(matrix);
105 }
106
Camera_dotWithNormal(JNIEnv * env,jobject obj,jfloat x,jfloat y,jfloat z)107 static jfloat Camera_dotWithNormal(JNIEnv* env, jobject obj,
108 jfloat x, jfloat y, jfloat z) {
109 jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID);
110 Sk3DView* v = reinterpret_cast<Sk3DView*>(viewHandle);
111 SkScalar dot = v->dotWithNormal(x, y, z);
112 return SkScalarToFloat(dot);
113 }
114
115 // ----------------------------------------------------------------------------
116
117 /*
118 * JNI registration.
119 */
120 static const JNINativeMethod gCameraMethods[] = {
121 /* name, signature, funcPtr */
122
123 { "nativeConstructor", "()V", (void*)Camera_constructor },
124 { "nativeDestructor", "()V", (void*)Camera_destructor },
125 { "save", "()V", (void*)Camera_save },
126 { "restore", "()V", (void*)Camera_restore },
127 { "translate", "(FFF)V", (void*)Camera_translate },
128 { "rotateX", "(F)V", (void*)Camera_rotateX },
129 { "rotateY", "(F)V", (void*)Camera_rotateY },
130 { "rotateZ", "(F)V", (void*)Camera_rotateZ },
131 { "rotate", "(FFF)V", (void*)Camera_rotate },
132 { "setLocation", "(FFF)V", (void*)Camera_setLocation },
133 { "getLocationX", "()F", (void*)Camera_getLocationX },
134 { "getLocationY", "()F", (void*)Camera_getLocationY },
135 { "getLocationZ", "()F", (void*)Camera_getLocationZ },
136 { "nativeGetMatrix", "(J)V", (void*)Camera_getMatrix },
137 { "nativeApplyToCanvas", "(J)V", (void*)Camera_applyToCanvas },
138 { "dotWithNormal", "(FFF)F", (void*)Camera_dotWithNormal }
139 };
140
register_android_graphics_Camera(JNIEnv * env)141 int register_android_graphics_Camera(JNIEnv* env) {
142 jclass clazz = android::FindClassOrDie(env, "android/graphics/Camera");
143 gNativeInstanceFieldID = android::GetFieldIDOrDie(env, clazz, "native_instance", "J");
144 return android::RegisterMethodsOrDie(env, "android/graphics/Camera", gCameraMethods,
145 NELEM(gCameraMethods));
146 }
147