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 #define LOG_TAG "OpenGLRenderer"
18 
19 #include <Interpolator.h>
20 #include <cutils/log.h>
21 
22 #include "graphics_jni_helpers.h"
23 
24 namespace android {
25 
26 using namespace uirenderer;
27 
createAccelerateDecelerateInterpolator(JNIEnv * env,jobject clazz)28 static jlong createAccelerateDecelerateInterpolator(JNIEnv* env, jobject clazz) {
29     return reinterpret_cast<jlong>(new AccelerateDecelerateInterpolator());
30 }
31 
createAccelerateInterpolator(JNIEnv * env,jobject clazz,jfloat factor)32 static jlong createAccelerateInterpolator(JNIEnv* env, jobject clazz, jfloat factor) {
33     return reinterpret_cast<jlong>(new AccelerateInterpolator(factor));
34 }
35 
createAnticipateInterpolator(JNIEnv * env,jobject clazz,jfloat tension)36 static jlong createAnticipateInterpolator(JNIEnv* env, jobject clazz, jfloat tension) {
37     return reinterpret_cast<jlong>(new AnticipateInterpolator(tension));
38 }
39 
createAnticipateOvershootInterpolator(JNIEnv * env,jobject clazz,jfloat tension)40 static jlong createAnticipateOvershootInterpolator(JNIEnv* env, jobject clazz, jfloat tension) {
41     return reinterpret_cast<jlong>(new AnticipateOvershootInterpolator(tension));
42 }
43 
createBounceInterpolator(JNIEnv * env,jobject clazz)44 static jlong createBounceInterpolator(JNIEnv* env, jobject clazz) {
45     return reinterpret_cast<jlong>(new BounceInterpolator());
46 }
47 
createCycleInterpolator(JNIEnv * env,jobject clazz,jfloat cycles)48 static jlong createCycleInterpolator(JNIEnv* env, jobject clazz, jfloat cycles) {
49     return reinterpret_cast<jlong>(new CycleInterpolator(cycles));
50 }
51 
createDecelerateInterpolator(JNIEnv * env,jobject clazz,jfloat factor)52 static jlong createDecelerateInterpolator(JNIEnv* env, jobject clazz, jfloat factor) {
53     return reinterpret_cast<jlong>(new DecelerateInterpolator(factor));
54 }
55 
createLinearInterpolator(JNIEnv * env,jobject clazz)56 static jlong createLinearInterpolator(JNIEnv* env, jobject clazz) {
57     return reinterpret_cast<jlong>(new LinearInterpolator());
58 }
59 
createOvershootInterpolator(JNIEnv * env,jobject clazz,jfloat tension)60 static jlong createOvershootInterpolator(JNIEnv* env, jobject clazz, jfloat tension) {
61     return reinterpret_cast<jlong>(new OvershootInterpolator(tension));
62 }
63 
createPathInterpolator(JNIEnv * env,jobject clazz,jfloatArray jX,jfloatArray jY)64 static jlong createPathInterpolator(JNIEnv* env, jobject clazz, jfloatArray jX, jfloatArray jY) {
65     jsize lenX = env->GetArrayLength(jX);
66     jsize lenY = env->GetArrayLength(jY);
67     LOG_ALWAYS_FATAL_IF(lenX != lenY || lenX <= 0, "Invalid path interpolator, x size: %d,"
68             " y size: %d", lenX, lenY);
69     std::vector<float> x(lenX);
70     std::vector<float> y(lenY);
71     env->GetFloatArrayRegion(jX, 0, lenX, x.data());
72     env->GetFloatArrayRegion(jY, 0, lenX, y.data());
73 
74     return reinterpret_cast<jlong>(new PathInterpolator(std::move(x), std::move(y)));
75 }
76 
createLutInterpolator(JNIEnv * env,jobject clazz,jfloatArray jlut)77 static jlong createLutInterpolator(JNIEnv* env, jobject clazz, jfloatArray jlut) {
78     jsize len = env->GetArrayLength(jlut);
79     if (len <= 0) {
80         return 0;
81     }
82     float* lut = new float[len];
83     env->GetFloatArrayRegion(jlut, 0, len, lut);
84     return reinterpret_cast<jlong>(new LUTInterpolator(lut, len));
85 }
86 
87 // ----------------------------------------------------------------------------
88 // JNI Glue
89 // ----------------------------------------------------------------------------
90 
91 const char* const kClassPathName = "android/graphics/animation/NativeInterpolatorFactory";
92 
93 static const JNINativeMethod gMethods[] = {
94     { "createAccelerateDecelerateInterpolator", "()J", (void*) createAccelerateDecelerateInterpolator },
95     { "createAccelerateInterpolator", "(F)J", (void*) createAccelerateInterpolator },
96     { "createAnticipateInterpolator", "(F)J", (void*) createAnticipateInterpolator },
97     { "createAnticipateOvershootInterpolator", "(F)J", (void*) createAnticipateOvershootInterpolator },
98     { "createBounceInterpolator", "()J", (void*) createBounceInterpolator },
99     { "createCycleInterpolator", "(F)J", (void*) createCycleInterpolator },
100     { "createDecelerateInterpolator", "(F)J", (void*) createDecelerateInterpolator },
101     { "createLinearInterpolator", "()J", (void*) createLinearInterpolator },
102     { "createOvershootInterpolator", "(F)J", (void*) createOvershootInterpolator },
103     { "createPathInterpolator", "([F[F)J", (void*) createPathInterpolator },
104     { "createLutInterpolator", "([F)J", (void*) createLutInterpolator },
105 };
106 
register_android_graphics_animation_NativeInterpolatorFactory(JNIEnv * env)107 int register_android_graphics_animation_NativeInterpolatorFactory(JNIEnv* env) {
108     return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
109 }
110 
111 
112 } // namespace android
113