diff options
author | 2016-12-01 20:21:09 +0000 | |
---|---|---|
committer | 2016-12-01 20:21:13 +0000 | |
commit | e9bf7c843664c16aaeac4cd79550a7461ae00b5d (patch) | |
tree | e6040f06dab501e70dbac0888123b2f85b627c80 | |
parent | f7c77a3caea19bc0f9677e295428e9abb769ad5c (diff) | |
parent | 9e9eeeeb78d94804cda00c2b36e56fdaca5552d6 (diff) |
Merge "Introduce PathInterpolator to native animators"
-rw-r--r-- | core/java/android/view/animation/PathInterpolator.java | 13 | ||||
-rw-r--r-- | core/java/com/android/internal/view/animation/NativeInterpolatorFactoryHelper.java | 1 | ||||
-rw-r--r-- | core/jni/com_android_internal_view_animation_NativeInterpolatorFactoryHelper.cpp | 15 | ||||
-rw-r--r-- | libs/hwui/Android.mk | 1 | ||||
-rw-r--r-- | libs/hwui/Interpolator.cpp | 33 | ||||
-rw-r--r-- | libs/hwui/Interpolator.h | 11 | ||||
-rw-r--r-- | libs/hwui/tests/unit/PathInterpolatorTests.cpp | 110 |
7 files changed, 183 insertions, 1 deletions
diff --git a/core/java/android/view/animation/PathInterpolator.java b/core/java/android/view/animation/PathInterpolator.java index eec5555e6d79..924437a2a6d4 100644 --- a/core/java/android/view/animation/PathInterpolator.java +++ b/core/java/android/view/animation/PathInterpolator.java @@ -25,6 +25,9 @@ import android.util.PathParser; import android.view.InflateException; import com.android.internal.R; +import com.android.internal.view.animation.HasNativeInterpolator; +import com.android.internal.view.animation.NativeInterpolatorFactory; +import com.android.internal.view.animation.NativeInterpolatorFactoryHelper; /** * An interpolator that can traverse a Path that extends from <code>Point</code> @@ -42,7 +45,8 @@ import com.android.internal.R; * path.lineTo(1f, 1f); * </pre></blockquote></p> */ -public class PathInterpolator extends BaseInterpolator { +@HasNativeInterpolator +public class PathInterpolator extends BaseInterpolator implements NativeInterpolatorFactory { // This governs how accurate the approximation of the Path is. private static final float PRECISION = 0.002f; @@ -229,4 +233,11 @@ public class PathInterpolator extends BaseInterpolator { float endY = mY[endIndex]; return startY + (fraction * (endY - startY)); } + + /** @hide **/ + @Override + public long createNativeInterpolator() { + return NativeInterpolatorFactoryHelper.createPathInterpolator(mX, mY); + } + } diff --git a/core/java/com/android/internal/view/animation/NativeInterpolatorFactoryHelper.java b/core/java/com/android/internal/view/animation/NativeInterpolatorFactoryHelper.java index 7cd75f3f0c7e..ebeec40c7083 100644 --- a/core/java/com/android/internal/view/animation/NativeInterpolatorFactoryHelper.java +++ b/core/java/com/android/internal/view/animation/NativeInterpolatorFactoryHelper.java @@ -32,5 +32,6 @@ public final class NativeInterpolatorFactoryHelper { public static native long createDecelerateInterpolator(float factor); public static native long createLinearInterpolator(); public static native long createOvershootInterpolator(float tension); + public static native long createPathInterpolator(float[] x, float[] y); public static native long createLutInterpolator(float[] values); } diff --git a/core/jni/com_android_internal_view_animation_NativeInterpolatorFactoryHelper.cpp b/core/jni/com_android_internal_view_animation_NativeInterpolatorFactoryHelper.cpp index 6781e130c860..f4d2e7ba882b 100644 --- a/core/jni/com_android_internal_view_animation_NativeInterpolatorFactoryHelper.cpp +++ b/core/jni/com_android_internal_view_animation_NativeInterpolatorFactoryHelper.cpp @@ -18,6 +18,7 @@ #include "jni.h" #include <nativehelper/JNIHelp.h> +#include <cutils/log.h> #include "core_jni_helpers.h" #include <Interpolator.h> @@ -62,6 +63,19 @@ static jlong createOvershootInterpolator(JNIEnv* env, jobject clazz, jfloat tens return reinterpret_cast<jlong>(new OvershootInterpolator(tension)); } +static jlong createPathInterpolator(JNIEnv* env, jobject clazz, jfloatArray jX, jfloatArray jY) { + jsize lenX = env->GetArrayLength(jX); + jsize lenY = env->GetArrayLength(jY); + LOG_ALWAYS_FATAL_IF(lenX != lenY || lenX <= 0, "Invalid path interpolator, x size: %d," + " y size: %d", lenX, lenY); + std::vector<float> x(lenX); + std::vector<float> y(lenY); + env->GetFloatArrayRegion(jX, 0, lenX, x.data()); + env->GetFloatArrayRegion(jY, 0, lenX, y.data()); + + return reinterpret_cast<jlong>(new PathInterpolator(std::move(x), std::move(y))); +} + static jlong createLutInterpolator(JNIEnv* env, jobject clazz, jfloatArray jlut) { jsize len = env->GetArrayLength(jlut); if (len <= 0) { @@ -88,6 +102,7 @@ static const JNINativeMethod gMethods[] = { { "createDecelerateInterpolator", "(F)J", (void*) createDecelerateInterpolator }, { "createLinearInterpolator", "()J", (void*) createLinearInterpolator }, { "createOvershootInterpolator", "(F)J", (void*) createOvershootInterpolator }, + { "createPathInterpolator", "([F[F)J", (void*) createPathInterpolator }, { "createLutInterpolator", "([F)J", (void*) createLutInterpolator }, }; diff --git a/libs/hwui/Android.mk b/libs/hwui/Android.mk index 361e9018ccbd..cf571e97c1f9 100644 --- a/libs/hwui/Android.mk +++ b/libs/hwui/Android.mk @@ -298,6 +298,7 @@ LOCAL_SRC_FILES += \ tests/unit/MeshStateTests.cpp \ tests/unit/OffscreenBufferPoolTests.cpp \ tests/unit/OpDumperTests.cpp \ + tests/unit/PathInterpolatorTests.cpp \ tests/unit/RenderNodeDrawableTests.cpp \ tests/unit/RecordingCanvasTests.cpp \ tests/unit/RenderNodeTests.cpp \ diff --git a/libs/hwui/Interpolator.cpp b/libs/hwui/Interpolator.cpp index bddb01b97865..f94a22d51d9f 100644 --- a/libs/hwui/Interpolator.cpp +++ b/libs/hwui/Interpolator.cpp @@ -88,6 +88,39 @@ float OvershootInterpolator::interpolate(float t) { return t * t * ((mTension + 1) * t + mTension) + 1.0f; } +float PathInterpolator::interpolate(float t) { + if (t <= 0) { + return 0; + } else if (t >= 1) { + return 1; + } + // Do a binary search for the correct x to interpolate between. + size_t startIndex = 0; + size_t endIndex = mX.size() - 1; + + while (endIndex > startIndex + 1) { + int midIndex = (startIndex + endIndex) / 2; + if (t < mX[midIndex]) { + endIndex = midIndex; + } else { + startIndex = midIndex; + } + } + + float xRange = mX[endIndex] - mX[startIndex]; + if (xRange == 0) { + return mY[startIndex]; + } + + float tInRange = t - mX[startIndex]; + float fraction = tInRange / xRange; + + float startY = mY[startIndex]; + float endY = mY[endIndex]; + return startY + (fraction * (endY - startY)); + +} + LUTInterpolator::LUTInterpolator(float* values, size_t size) : mValues(values) , mSize(size) { diff --git a/libs/hwui/Interpolator.h b/libs/hwui/Interpolator.h index 65120087ff60..224cee70acc7 100644 --- a/libs/hwui/Interpolator.h +++ b/libs/hwui/Interpolator.h @@ -20,6 +20,7 @@ #include <memory> #include <cutils/compiler.h> +#include <vector> namespace android { namespace uirenderer { @@ -100,6 +101,16 @@ private: const float mTension; }; +class ANDROID_API PathInterpolator : public Interpolator { +public: + explicit PathInterpolator(std::vector<float>&& x, std::vector<float>&& y) + : mX (x), mY(y) {} + virtual float interpolate(float input) override; +private: + std::vector<float> mX; + std::vector<float> mY; +}; + class ANDROID_API LUTInterpolator : public Interpolator { public: LUTInterpolator(float* values, size_t size); diff --git a/libs/hwui/tests/unit/PathInterpolatorTests.cpp b/libs/hwui/tests/unit/PathInterpolatorTests.cpp new file mode 100644 index 000000000000..d7cb23a4b793 --- /dev/null +++ b/libs/hwui/tests/unit/PathInterpolatorTests.cpp @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <gtest/gtest.h> + +#include <Interpolator.h> + +namespace android { +namespace uirenderer { + +struct TestData { + const std::vector<float> x; + const std::vector<float> y; + const std::vector<float> inFraction; + const std::vector<float> outFraction; +}; + +const static TestData sTestDataSet[] = { + { + // Straight line as a path. + {0.0f, 1.0f}, + {0.0f, 1.0f}, + {0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0f}, + {0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0f}, + }, + + { + { + 0.0f, 0.5f, 0.5178955f, 0.5341797f, 0.5489991f, 0.5625f, 0.5748291f, + 0.5861328f, 0.60625005f, 0.62402344f, 0.640625f, 0.675f, 0.6951172f, + 0.71875f, 0.7470703f, 0.78125f, 0.82246095f, 0.84606934f, 0.871875f, + 0.9000244f, 0.93066406f, 0.96394044f, 1.0f + }, + { + 0.0f, 0.0f, 0.0028686523f, 0.011230469f, 0.024719238f, 0.04296875f, + 0.06561279f, 0.092285156f, 0.15625f, 0.2319336f, 0.31640625f, 0.5f, + 0.5932617f, 0.68359375f, 0.7680664f, 0.84375f, 0.90771484f, 0.9343872f, + 0.95703125f, 0.97528076f, 0.98876953f, 0.99713135f, 1.0f + }, + { + 0.0f, 0.03375840187072754f, 0.13503384590148926f, 0.23630905151367188f, + 0.336834192276001f, 0.4508626461029053f, 0.564141035079956f, + 0.6781694889068604f, 0.7921979427337646f, 0.9054763317108154f, 1.0f + }, + { + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0459827296435833f, + 0.5146934390068054f, 0.8607426285743713f, 0.9776809215545654f, 1.0f + + } + + }, + { + { + 0.0f, 0.017895509f, 0.034179688f, 0.048999026f, 0.0625f, 0.0748291f, + 0.08613282f, 0.10625f, 0.12402344f, 0.140625f, 0.17500001f, 0.19511719f, + 0.21875f, 0.24707031f, 0.28125f, 0.32246095f, 0.34606934f, 0.371875f, + 0.4000244f, 0.43066406f, 0.46394044f, 0.5f, 1.0f + }, + { + 0.0f, 0.0028686523f, 0.011230469f, 0.024719238f, 0.04296875f, 0.06561279f, + 0.092285156f, 0.15625f, 0.2319336f, 0.31640625f, 0.5f, 0.5932617f, + 0.68359375f, 0.7680664f, 0.84375f, 0.90771484f, 0.9343872f, 0.95703125f, + 0.97528076f, 0.98876953f, 0.99713135f, 1.0f, 1.0f + }, + { + 0.0f, 0.102020263671875f, 0.20330810546875f, 0.3165740966796875f, + 0.43060302734375f, 0.5318756103515625f, 0.6331634521484375f, + 0.746429443359375f, 0.84771728515625f, 0.9617462158203125f, 1.0f + }, + { + 0.0f, 0.14280107617378235f, 0.6245699524879456f, 0.8985776901245117f, + 0.9887426495552063f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f + } + }, + + +}; + +static std::vector<float> getX(const TestData& data) { + return data.x; +} + +static std::vector<float> getY(const TestData& data) { + return data.y; +} + +TEST(Interpolator, pathInterpolation) { + for (const TestData& data: sTestDataSet) { + PathInterpolator interpolator(getX(data), getY(data)); + for (size_t i = 0; i < data.inFraction.size(); i++) { + EXPECT_FLOAT_EQ(data.outFraction[i], interpolator.interpolate(data.inFraction[i])); + } + } +} + +} +} |