#include "jni.h" #include #include "SkCamera.h" #include "Canvas.h" #include "GraphicsJNI.h" static jfieldID gNativeInstanceFieldID; static void Camera_constructor(JNIEnv* env, jobject obj) { Sk3DView* view = new Sk3DView; env->SetLongField(obj, gNativeInstanceFieldID, reinterpret_cast(view)); } static void Camera_destructor(JNIEnv* env, jobject obj) { jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID); Sk3DView* view = reinterpret_cast(viewHandle); delete view; } static void Camera_save(JNIEnv* env, jobject obj) { jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID); Sk3DView* v = reinterpret_cast(viewHandle); v->save(); } static void Camera_restore(JNIEnv* env, jobject obj) { jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID); Sk3DView* v = reinterpret_cast(viewHandle); v->restore(); } static void Camera_translate(JNIEnv* env, jobject obj, jfloat dx, jfloat dy, jfloat dz) { jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID); Sk3DView* v = reinterpret_cast(viewHandle); v->translate(dx, dy, dz); } static void Camera_rotateX(JNIEnv* env, jobject obj, jfloat degrees) { jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID); Sk3DView* v = reinterpret_cast(viewHandle); v->rotateX(degrees); } static void Camera_rotateY(JNIEnv* env, jobject obj, jfloat degrees) { jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID); Sk3DView* v = reinterpret_cast(viewHandle); v->rotateY(degrees); } static void Camera_rotateZ(JNIEnv* env, jobject obj, jfloat degrees) { jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID); Sk3DView* v = reinterpret_cast(viewHandle); v->rotateZ(degrees); } static void Camera_rotate(JNIEnv* env, jobject obj, jfloat x, jfloat y, jfloat z) { jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID); Sk3DView* v = reinterpret_cast(viewHandle); v->rotateX(x); v->rotateY(y); v->rotateZ(z); } static void Camera_setLocation(JNIEnv* env, jobject obj, jfloat x, jfloat y, jfloat z) { jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID); Sk3DView* v = reinterpret_cast(viewHandle); v->setCameraLocation(x, y, z); } static jfloat Camera_getLocationX(JNIEnv* env, jobject obj) { jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID); Sk3DView* v = reinterpret_cast(viewHandle); return SkScalarToFloat(v->getCameraLocationX()); } static jfloat Camera_getLocationY(JNIEnv* env, jobject obj) { jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID); Sk3DView* v = reinterpret_cast(viewHandle); return SkScalarToFloat(v->getCameraLocationY()); } static jfloat Camera_getLocationZ(JNIEnv* env, jobject obj) { jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID); Sk3DView* v = reinterpret_cast(viewHandle); return SkScalarToFloat(v->getCameraLocationZ()); } static void Camera_getMatrix(JNIEnv* env, jobject obj, jlong matrixHandle) { SkMatrix* native_matrix = reinterpret_cast(matrixHandle); jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID); Sk3DView* v = reinterpret_cast(viewHandle); v->getMatrix(native_matrix); } static void Camera_applyToCanvas(JNIEnv* env, jobject obj, jlong canvasHandle) { SkCanvas* canvas = reinterpret_cast(canvasHandle)->getSkCanvas(); jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID); Sk3DView* v = reinterpret_cast(viewHandle); v->applyToCanvas(canvas); } static jfloat Camera_dotWithNormal(JNIEnv* env, jobject obj, jfloat x, jfloat y, jfloat z) { jlong viewHandle = env->GetLongField(obj, gNativeInstanceFieldID); Sk3DView* v = reinterpret_cast(viewHandle); SkScalar dot = v->dotWithNormal(x, y, z); return SkScalarToFloat(dot); } // ---------------------------------------------------------------------------- /* * JNI registration. */ static JNINativeMethod gCameraMethods[] = { /* name, signature, funcPtr */ { "nativeConstructor", "()V", (void*)Camera_constructor }, { "nativeDestructor", "()V", (void*)Camera_destructor }, { "save", "()V", (void*)Camera_save }, { "restore", "()V", (void*)Camera_restore }, { "translate", "(FFF)V", (void*)Camera_translate }, { "rotateX", "(F)V", (void*)Camera_rotateX }, { "rotateY", "(F)V", (void*)Camera_rotateY }, { "rotateZ", "(F)V", (void*)Camera_rotateZ }, { "rotate", "(FFF)V", (void*)Camera_rotate }, { "setLocation", "(FFF)V", (void*)Camera_setLocation }, { "getLocationX", "()F", (void*)Camera_getLocationX }, { "getLocationY", "()F", (void*)Camera_getLocationY }, { "getLocationZ", "()F", (void*)Camera_getLocationZ }, { "nativeGetMatrix", "(J)V", (void*)Camera_getMatrix }, { "nativeApplyToCanvas", "(J)V", (void*)Camera_applyToCanvas }, { "dotWithNormal", "(FFF)F", (void*)Camera_dotWithNormal } }; int register_android_graphics_Camera(JNIEnv* env) { jclass clazz = env->FindClass("android/graphics/Camera"); if (clazz == 0) { return -1; } gNativeInstanceFieldID = env->GetFieldID(clazz, "native_instance", "J"); if (gNativeInstanceFieldID == 0) { return -1; } return android::AndroidRuntime::registerNativeMethods(env, "android/graphics/Camera", gCameraMethods, SK_ARRAY_COUNT(gCameraMethods)); }