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