1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <Interpolator.h>
18 #include <cutils/log.h>
19 
20 #include "graphics_jni_helpers.h"
21 
22 namespace android {
23 
24 using namespace uirenderer;
25 
createAccelerateDecelerateInterpolator(JNIEnv * env,jobject clazz)26 static jlong createAccelerateDecelerateInterpolator(JNIEnv* env, jobject clazz) {
27     return reinterpret_cast<jlong>(new AccelerateDecelerateInterpolator());
28 }
29 
createAccelerateInterpolator(JNIEnv * env,jobject clazz,jfloat factor)30 static jlong createAccelerateInterpolator(JNIEnv* env, jobject clazz, jfloat factor) {
31     return reinterpret_cast<jlong>(new AccelerateInterpolator(factor));
32 }
33 
createAnticipateInterpolator(JNIEnv * env,jobject clazz,jfloat tension)34 static jlong createAnticipateInterpolator(JNIEnv* env, jobject clazz, jfloat tension) {
35     return reinterpret_cast<jlong>(new AnticipateInterpolator(tension));
36 }
37 
createAnticipateOvershootInterpolator(JNIEnv * env,jobject clazz,jfloat tension)38 static jlong createAnticipateOvershootInterpolator(JNIEnv* env, jobject clazz, jfloat tension) {
39     return reinterpret_cast<jlong>(new AnticipateOvershootInterpolator(tension));
40 }
41 
createBounceInterpolator(JNIEnv * env,jobject clazz)42 static jlong createBounceInterpolator(JNIEnv* env, jobject clazz) {
43     return reinterpret_cast<jlong>(new BounceInterpolator());
44 }
45 
createCycleInterpolator(JNIEnv * env,jobject clazz,jfloat cycles)46 static jlong createCycleInterpolator(JNIEnv* env, jobject clazz, jfloat cycles) {
47     return reinterpret_cast<jlong>(new CycleInterpolator(cycles));
48 }
49 
createDecelerateInterpolator(JNIEnv * env,jobject clazz,jfloat factor)50 static jlong createDecelerateInterpolator(JNIEnv* env, jobject clazz, jfloat factor) {
51     return reinterpret_cast<jlong>(new DecelerateInterpolator(factor));
52 }
53 
createLinearInterpolator(JNIEnv * env,jobject clazz)54 static jlong createLinearInterpolator(JNIEnv* env, jobject clazz) {
55     return reinterpret_cast<jlong>(new LinearInterpolator());
56 }
57 
createOvershootInterpolator(JNIEnv * env,jobject clazz,jfloat tension)58 static jlong createOvershootInterpolator(JNIEnv* env, jobject clazz, jfloat tension) {
59     return reinterpret_cast<jlong>(new OvershootInterpolator(tension));
60 }
61 
createPathInterpolator(JNIEnv * env,jobject clazz,jfloatArray jX,jfloatArray jY)62 static jlong createPathInterpolator(JNIEnv* env, jobject clazz, jfloatArray jX, jfloatArray jY) {
63     jsize lenX = env->GetArrayLength(jX);
64     jsize lenY = env->GetArrayLength(jY);
65     LOG_ALWAYS_FATAL_IF(lenX != lenY || lenX <= 0, "Invalid path interpolator, x size: %d,"
66             " y size: %d", lenX, lenY);
67     std::vector<float> x(lenX);
68     std::vector<float> y(lenY);
69     env->GetFloatArrayRegion(jX, 0, lenX, x.data());
70     env->GetFloatArrayRegion(jY, 0, lenX, y.data());
71 
72     return reinterpret_cast<jlong>(new PathInterpolator(std::move(x), std::move(y)));
73 }
74 
createLutInterpolator(JNIEnv * env,jobject clazz,jfloatArray jlut)75 static jlong createLutInterpolator(JNIEnv* env, jobject clazz, jfloatArray jlut) {
76     jsize len = env->GetArrayLength(jlut);
77     if (len <= 0) {
78         return 0;
79     }
80     float* lut = new float[len];
81     env->GetFloatArrayRegion(jlut, 0, len, lut);
82     return reinterpret_cast<jlong>(new LUTInterpolator(lut, len));
83 }
84 
85 // ----------------------------------------------------------------------------
86 // JNI Glue
87 // ----------------------------------------------------------------------------
88 
89 const char* const kClassPathName = "android/graphics/animation/NativeInterpolatorFactory";
90 
91 static const JNINativeMethod gMethods[] = {
92     { "createAccelerateDecelerateInterpolator", "()J", (void*) createAccelerateDecelerateInterpolator },
93     { "createAccelerateInterpolator", "(F)J", (void*) createAccelerateInterpolator },
94     { "createAnticipateInterpolator", "(F)J", (void*) createAnticipateInterpolator },
95     { "createAnticipateOvershootInterpolator", "(F)J", (void*) createAnticipateOvershootInterpolator },
96     { "createBounceInterpolator", "()J", (void*) createBounceInterpolator },
97     { "createCycleInterpolator", "(F)J", (void*) createCycleInterpolator },
98     { "createDecelerateInterpolator", "(F)J", (void*) createDecelerateInterpolator },
99     { "createLinearInterpolator", "()J", (void*) createLinearInterpolator },
100     { "createOvershootInterpolator", "(F)J", (void*) createOvershootInterpolator },
101     { "createPathInterpolator", "([F[F)J", (void*) createPathInterpolator },
102     { "createLutInterpolator", "([F)J", (void*) createLutInterpolator },
103 };
104 
register_android_graphics_animation_NativeInterpolatorFactory(JNIEnv * env)105 int register_android_graphics_animation_NativeInterpolatorFactory(JNIEnv* env) {
106     return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
107 }
108 
109 
110 } // namespace android
111