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