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 SkInterpolator* Interpolator_constructor(JNIEnv* env, jobject clazz, int valueCount, int frameCount) 9 { 10 return new SkInterpolator(valueCount, frameCount); 11 } 12 13 static void Interpolator_destructor(JNIEnv* env, jobject clazz, SkInterpolator* interp) 14 { 15 delete interp; 16 } 17 18 static void Interpolator_reset(JNIEnv* env, jobject clazz, SkInterpolator* interp, int valueCount, int frameCount) 19 { 20 interp->reset(valueCount, frameCount); 21 } 22 23 static void Interpolator_setKeyFrame(JNIEnv* env, jobject clazz, SkInterpolator* interp, int index, int msec, jfloatArray valueArray, jfloatArray blendArray) 24 { 25 SkScalar blendStorage[4]; 26 SkScalar* blend = NULL; 27 28 AutoJavaFloatArray autoValues(env, valueArray); 29 float* values = autoValues.ptr(); 30 int i, n = autoValues.length(); 31 32 SkAutoSTMalloc<16, SkScalar> storage(n); 33 SkScalar* scalars = storage.get(); 34 35 for (i = 0; i < n; i++) 36 scalars[i] = SkFloatToScalar(values[i]); 37 38 if (blendArray != NULL) { 39 AutoJavaFloatArray autoBlend(env, blendArray, 4); 40 values = autoBlend.ptr(); 41 for (i = 0; i < 4; i++) 42 blendStorage[i] = SkFloatToScalar(values[i]); 43 blend = blendStorage; 44 } 45 46 interp->setKeyFrame(index, msec, scalars, blend); 47 } 48 49 static void Interpolator_setRepeatMirror(JNIEnv* env, jobject clazz, SkInterpolator* interp, float repeatCount, jboolean mirror) 50 { 51 if (repeatCount > 32000) 52 repeatCount = 32000; 53 54 interp->setRepeatCount(SkFloatToScalar(repeatCount)); 55 interp->setMirror(mirror != 0); 56 } 57 58 static int Interpolator_timeToValues(JNIEnv* env, jobject clazz, SkInterpolator* interp, int msec, jfloatArray valueArray) 59 { 60 SkInterpolatorBase::Result result; 61 62 float* values = valueArray ? env->GetFloatArrayElements(valueArray, NULL) : NULL; 63 result = interp->timeToValues(msec, (SkScalar*)values); 64 65 if (valueArray) { 66 int n = env->GetArrayLength(valueArray); 67 for (int i = 0; i < n; i++) { 68 values[i] = SkScalarToFloat(*(SkScalar*)&values[i]); 69 } 70 env->ReleaseFloatArrayElements(valueArray, values, 0); 71 } 72 73 return result; 74 } 75 76 // ---------------------------------------------------------------------------- 77 78 /* 79 * JNI registration. 80 */ 81 static JNINativeMethod gInterpolatorMethods[] = { 82 { "nativeConstructor", "(II)I", (void*)Interpolator_constructor }, 83 { "nativeDestructor", "(I)V", (void*)Interpolator_destructor }, 84 { "nativeReset", "(III)V", (void*)Interpolator_reset }, 85 { "nativeSetKeyFrame", "(III[F[F)V", (void*)Interpolator_setKeyFrame }, 86 { "nativeSetRepeatMirror", "(IFZ)V", (void*)Interpolator_setRepeatMirror }, 87 { "nativeTimeToValues", "(II[F)I", (void*)Interpolator_timeToValues } 88 }; 89 90 int register_android_graphics_Interpolator(JNIEnv* env) 91 { 92 return android::AndroidRuntime::registerNativeMethods(env, 93 "android/graphics/Interpolator", 94 gInterpolatorMethods, 95 SK_ARRAY_COUNT(gInterpolatorMethods)); 96 } 97