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