blob: 3593d1a4ab3267ed20190da0da05f8989db92499 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001#include "GraphicsJNI.h"
2#include "SkInterpolator.h"
Ben Wagner60126ef2015-08-07 12:13:48 -04003#include "core_jni_helpers.h"
4
5#include <jni.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006
Ashok Bhata2f90422014-01-13 20:45:30 +00007static jlong Interpolator_constructor(JNIEnv* env, jobject clazz, jint valueCount, jint frameCount)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008{
Ashok Bhata2f90422014-01-13 20:45:30 +00009 return reinterpret_cast<jlong>(new SkInterpolator(valueCount, frameCount));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010}
11
Ashok Bhata2f90422014-01-13 20:45:30 +000012static void Interpolator_destructor(JNIEnv* env, jobject clazz, jlong interpHandle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013{
Ashok Bhata2f90422014-01-13 20:45:30 +000014 SkInterpolator* interp = reinterpret_cast<SkInterpolator*>(interpHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015 delete interp;
16}
17
Ashok Bhata2f90422014-01-13 20:45:30 +000018static void Interpolator_reset(JNIEnv* env, jobject clazz, jlong interpHandle, jint valueCount, jint frameCount)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019{
Ashok Bhata2f90422014-01-13 20:45:30 +000020 SkInterpolator* interp = reinterpret_cast<SkInterpolator*>(interpHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021 interp->reset(valueCount, frameCount);
22}
23
Ashok Bhata2f90422014-01-13 20:45:30 +000024static 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 -080025{
Ashok Bhata2f90422014-01-13 20:45:30 +000026 SkInterpolator* interp = reinterpret_cast<SkInterpolator*>(interpHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027
Leon Scroggins III2e0103e2014-04-04 17:05:24 -040028 AutoJavaFloatArray autoValues(env, valueArray);
29 AutoJavaFloatArray autoBlend(env, blendArray, 4);
30#ifdef SK_SCALAR_IS_FLOAT
31 SkScalar* scalars = autoValues.ptr();
32 SkScalar* blend = autoBlend.ptr();
33#else
34 #error Need to convert float array to SkScalar array before calling the following function.
35#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
37 interp->setKeyFrame(index, msec, scalars, blend);
38}
39
Ashok Bhata2f90422014-01-13 20:45:30 +000040static void Interpolator_setRepeatMirror(JNIEnv* env, jobject clazz, jlong interpHandle, jfloat repeatCount, jboolean mirror)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041{
Ashok Bhata2f90422014-01-13 20:45:30 +000042 SkInterpolator* interp = reinterpret_cast<SkInterpolator*>(interpHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 if (repeatCount > 32000)
44 repeatCount = 32000;
45
Leon Scroggins III2e0103e2014-04-04 17:05:24 -040046 interp->setRepeatCount(repeatCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 interp->setMirror(mirror != 0);
48}
49
Ashok Bhata2f90422014-01-13 20:45:30 +000050static jint Interpolator_timeToValues(JNIEnv* env, jobject clazz, jlong interpHandle, jint msec, jfloatArray valueArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051{
Ashok Bhata2f90422014-01-13 20:45:30 +000052 SkInterpolator* interp = reinterpret_cast<SkInterpolator*>(interpHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 SkInterpolatorBase::Result result;
54
55 float* values = valueArray ? env->GetFloatArrayElements(valueArray, NULL) : NULL;
56 result = interp->timeToValues(msec, (SkScalar*)values);
Elliott Hughes4cb17532011-04-12 16:10:26 -070057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 if (valueArray) {
59 int n = env->GetArrayLength(valueArray);
60 for (int i = 0; i < n; i++) {
61 values[i] = SkScalarToFloat(*(SkScalar*)&values[i]);
62 }
63 env->ReleaseFloatArrayElements(valueArray, values, 0);
64 }
Elliott Hughes4cb17532011-04-12 16:10:26 -070065
Ashok Bhata2f90422014-01-13 20:45:30 +000066 return static_cast<jint>(result);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067}
68
69// ----------------------------------------------------------------------------
70
71/*
72 * JNI registration.
73 */
74static JNINativeMethod gInterpolatorMethods[] = {
Ashok Bhata2f90422014-01-13 20:45:30 +000075 { "nativeConstructor", "(II)J", (void*)Interpolator_constructor },
76 { "nativeDestructor", "(J)V", (void*)Interpolator_destructor },
77 { "nativeReset", "(JII)V", (void*)Interpolator_reset },
78 { "nativeSetKeyFrame", "(JII[F[F)V", (void*)Interpolator_setKeyFrame },
79 { "nativeSetRepeatMirror", "(JFZ)V", (void*)Interpolator_setRepeatMirror },
80 { "nativeTimeToValues", "(JI[F)I", (void*)Interpolator_timeToValues }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081};
82
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083int register_android_graphics_Interpolator(JNIEnv* env)
84{
Andreas Gampeed6b9df2014-11-20 22:02:20 -080085 return android::RegisterMethodsOrDie(env, "android/graphics/Interpolator",
86 gInterpolatorMethods, NELEM(gInterpolatorMethods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087}