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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 98 static void Camera_applyToCanvas(JNIEnv* env, jobject obj, jlong canvasHandle) { 99 SkCanvas* canvas = reinterpret_cast<android::Canvas*>(canvasHandle)->asSkCanvas(); 100 jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID); 101 Sk3DView* v = reinterpret_cast<Sk3DView*>(viewHandle); 102 v->applyToCanvas(canvas); 103 } 104 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 const 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 139 int register_android_graphics_Camera(JNIEnv* env) { 140 jclass clazz = android::FindClassOrDie(env, "android/graphics/Camera"); 141 gNativeInstanceFieldID = android::GetFieldIDOrDie(env, clazz, "native_instance", "J"); 142 return android::RegisterMethodsOrDie(env, "android/graphics/Camera", gCameraMethods, 143 NELEM(gCameraMethods)); 144 } 145