blob: f7f3511d15a91d40cb4d397eb7c1c78843a2449d [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001#include "jni.h"
Andreas Gampeed6b9df2014-11-20 22:02:20 -08002#include "core_jni_helpers.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003
4#include "GraphicsJNI.h"
5#include "SkInterpolator.h"
6#include "SkTemplates.h"
7
Ashok Bhata2f90422014-01-13 20:45:30 +00008static jlong Interpolator_constructor(JNIEnv* env, jobject clazz, jint valueCount, jint frameCount)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009{
Ashok Bhata2f90422014-01-13 20:45:30 +000010 return reinterpret_cast<jlong>(new SkInterpolator(valueCount, frameCount));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011}
12
Ashok Bhata2f90422014-01-13 20:45:30 +000013static void Interpolator_destructor(JNIEnv* env, jobject clazz, jlong interpHandle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014{
Ashok Bhata2f90422014-01-13 20:45:30 +000015 SkInterpolator* interp = reinterpret_cast<SkInterpolator*>(interpHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016 delete interp;
17}
18
Ashok Bhata2f90422014-01-13 20:45:30 +000019static void Interpolator_reset(JNIEnv* env, jobject clazz, jlong interpHandle, jint valueCount, jint frameCount)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020{
Ashok Bhata2f90422014-01-13 20:45:30 +000021 SkInterpolator* interp = reinterpret_cast<SkInterpolator*>(interpHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022 interp->reset(valueCount, frameCount);
23}
24
Ashok Bhata2f90422014-01-13 20:45:30 +000025static void Interpolator_setKeyFrame(JNIEnv* env, jobject clazz, jlong interpHandle, jint index, jint msec, jfloatArray valueArray, jfloatArray blendArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026{
Ashok Bhata2f90422014-01-13 20:45:30 +000027 SkInterpolator* interp = reinterpret_cast<SkInterpolator*>(interpHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
Leon Scroggins III2e0103e2014-04-04 17:05:24 -040029 AutoJavaFloatArray autoValues(env, valueArray);
30 AutoJavaFloatArray autoBlend(env, blendArray, 4);
31#ifdef SK_SCALAR_IS_FLOAT
32 SkScalar* scalars = autoValues.ptr();
33 SkScalar* blend = autoBlend.ptr();
34#else
35 #error Need to convert float array to SkScalar array before calling the following function.
36#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
38 interp->setKeyFrame(index, msec, scalars, blend);
39}
40
Ashok Bhata2f90422014-01-13 20:45:30 +000041static void Interpolator_setRepeatMirror(JNIEnv* env, jobject clazz, jlong interpHandle, jfloat repeatCount, jboolean mirror)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042{
Ashok Bhata2f90422014-01-13 20:45:30 +000043 SkInterpolator* interp = reinterpret_cast<SkInterpolator*>(interpHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044 if (repeatCount > 32000)
45 repeatCount = 32000;
46
Leon Scroggins III2e0103e2014-04-04 17:05:24 -040047 interp->setRepeatCount(repeatCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 interp->setMirror(mirror != 0);
49}
50
Ashok Bhata2f90422014-01-13 20:45:30 +000051static jint Interpolator_timeToValues(JNIEnv* env, jobject clazz, jlong interpHandle, jint msec, jfloatArray valueArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052{
Ashok Bhata2f90422014-01-13 20:45:30 +000053 SkInterpolator* interp = reinterpret_cast<SkInterpolator*>(interpHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 SkInterpolatorBase::Result result;
55
56 float* values = valueArray ? env->GetFloatArrayElements(valueArray, NULL) : NULL;
57 result = interp->timeToValues(msec, (SkScalar*)values);
Elliott Hughes4cb17532011-04-12 16:10:26 -070058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 if (valueArray) {
60 int n = env->GetArrayLength(valueArray);
61 for (int i = 0; i < n; i++) {
62 values[i] = SkScalarToFloat(*(SkScalar*)&values[i]);
63 }
64 env->ReleaseFloatArrayElements(valueArray, values, 0);
65 }
Elliott Hughes4cb17532011-04-12 16:10:26 -070066
Ashok Bhata2f90422014-01-13 20:45:30 +000067 return static_cast<jint>(result);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068}
69
70// ----------------------------------------------------------------------------
71
72/*
73 * JNI registration.
74 */
75static JNINativeMethod gInterpolatorMethods[] = {
Ashok Bhata2f90422014-01-13 20:45:30 +000076 { "nativeConstructor", "(II)J", (void*)Interpolator_constructor },
77 { "nativeDestructor", "(J)V", (void*)Interpolator_destructor },
78 { "nativeReset", "(JII)V", (void*)Interpolator_reset },
79 { "nativeSetKeyFrame", "(JII[F[F)V", (void*)Interpolator_setKeyFrame },
80 { "nativeSetRepeatMirror", "(JFZ)V", (void*)Interpolator_setRepeatMirror },
81 { "nativeTimeToValues", "(JI[F)I", (void*)Interpolator_timeToValues }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082};
83
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084int register_android_graphics_Interpolator(JNIEnv* env)
85{
Andreas Gampeed6b9df2014-11-20 22:02:20 -080086 return android::RegisterMethodsOrDie(env, "android/graphics/Interpolator",
87 gInterpolatorMethods, NELEM(gInterpolatorMethods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088}