blob: beec351c4f26235d33df87a39447382cc4b7b75c [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001#include "jni.h"
2#include <android_runtime/AndroidRuntime.h>
3
4#include "GraphicsJNI.h"
5#include "SkInterpolator.h"
6#include "SkTemplates.h"
7
8static SkInterpolator* Interpolator_constructor(JNIEnv* env, jobject clazz, int valueCount, int frameCount)
9{
10 return new SkInterpolator(valueCount, frameCount);
11}
12
13static void Interpolator_destructor(JNIEnv* env, jobject clazz, SkInterpolator* interp)
14{
15 delete interp;
16}
17
18static void Interpolator_reset(JNIEnv* env, jobject clazz, SkInterpolator* interp, int valueCount, int frameCount)
19{
20 interp->reset(valueCount, frameCount);
21}
22
23static 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
49static 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
58static 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 */
81static 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
90int register_android_graphics_Interpolator(JNIEnv* env);
91int register_android_graphics_Interpolator(JNIEnv* env)
92{
93 return android::AndroidRuntime::registerNativeMethods(env,
94 "android/graphics/Interpolator",
95 gInterpolatorMethods,
96 SK_ARRAY_COUNT(gInterpolatorMethods));
97}