1 #include "jni.h" 2 #include <android_runtime/AndroidRuntime.h> 3 4 #include "GraphicsJNI.h" 5 #include "SkInterpolator.h" 6 #include "SkTemplates.h" 7 8 static jlong Interpolator_constructor(JNIEnv* env, jobject clazz, jint valueCount, jint frameCount) 9 { 10 return reinterpret_cast<jlong>(new SkInterpolator(valueCount, frameCount)); 11 } 12 13 static void Interpolator_destructor(JNIEnv* env, jobject clazz, jlong interpHandle) 14 { 15 SkInterpolator* interp = reinterpret_cast<SkInterpolator*>(interpHandle); 16 delete interp; 17 } 18 19 static void Interpolator_reset(JNIEnv* env, jobject clazz, jlong interpHandle, jint valueCount, jint frameCount) 20 { 21 SkInterpolator* interp = reinterpret_cast<SkInterpolator*>(interpHandle); 22 interp->reset(valueCount, frameCount); 23 } 24 25 static void Interpolator_setKeyFrame(JNIEnv* env, jobject clazz, jlong interpHandle, jint index, jint msec, jfloatArray valueArray, jfloatArray blendArray) 26 { 27 SkInterpolator* interp = reinterpret_cast<SkInterpolator*>(interpHandle); 28 29 AutoJavaFloatArray autoValues(env, valueArray); 30 AutoJavaFloatArray autoBlend(env, blendArray, 4); 31 #ifdef SK_SCALAR_IS_FLOAT 32 SkScalar* scalars = autoValues.ptr(); 33 SkScalar* blend = autoBlend.ptr(); 34 #else 35 #error Need to convert float array to SkScalar array before calling the following function. 36 #endif 37 38 interp->setKeyFrame(index, msec, scalars, blend); 39 } 40 41 static void Interpolator_setRepeatMirror(JNIEnv* env, jobject clazz, jlong interpHandle, jfloat repeatCount, jboolean mirror) 42 { 43 SkInterpolator* interp = reinterpret_cast<SkInterpolator*>(interpHandle); 44 if (repeatCount > 32000) 45 repeatCount = 32000; 46 47 interp->setRepeatCount(repeatCount); 48 interp->setMirror(mirror != 0); 49 } 50 51 static jint Interpolator_timeToValues(JNIEnv* env, jobject clazz, jlong interpHandle, jint msec, jfloatArray valueArray) 52 { 53 SkInterpolator* interp = reinterpret_cast<SkInterpolator*>(interpHandle); 54 SkInterpolatorBase::Result result; 55 56 float* values = valueArray ? env->GetFloatArrayElements(valueArray, NULL) : NULL; 57 result = interp->timeToValues(msec, (SkScalar*)values); 58 59 if (valueArray) { 60 int n = env->GetArrayLength(valueArray); 61 for (int i = 0; i < n; i++) { 62 values[i] = SkScalarToFloat(*(SkScalar*)&values[i]); 63 } 64 env->ReleaseFloatArrayElements(valueArray, values, 0); 65 } 66 67 return static_cast<jint>(result); 68 } 69 70 // ---------------------------------------------------------------------------- 71 72 /* 73 * JNI registration. 74 */ 75 static JNINativeMethod gInterpolatorMethods[] = { 76 { "nativeConstructor", "(II)J", (void*)Interpolator_constructor }, 77 { "nativeDestructor", "(J)V", (void*)Interpolator_destructor }, 78 { "nativeReset", "(JII)V", (void*)Interpolator_reset }, 79 { "nativeSetKeyFrame", "(JII[F[F)V", (void*)Interpolator_setKeyFrame }, 80 { "nativeSetRepeatMirror", "(JFZ)V", (void*)Interpolator_setRepeatMirror }, 81 { "nativeTimeToValues", "(JI[F)I", (void*)Interpolator_timeToValues } 82 }; 83 84 int register_android_graphics_Interpolator(JNIEnv* env) 85 { 86 return android::AndroidRuntime::registerNativeMethods(env, 87 "android/graphics/Interpolator", 88 gInterpolatorMethods, 89 SK_ARRAY_COUNT(gInterpolatorMethods)); 90 } 91