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