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