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