blob: 6ba0ab59a88c41ce176935cebd02bb85ee6aa895 [file] [log] [blame]
Doris Liu766431a2016-02-04 22:17:11 +00001/*
2 * Copyright (C) 2016 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 "PropertyValuesHolder.h"
18
19#include "utils/VectorDrawableUtils.h"
20
21#include <utils/Log.h>
22
23namespace android {
24namespace uirenderer {
25
26using namespace VectorDrawable;
27
Doris Liua6b967c2016-06-02 16:20:18 -070028inline U8CPU lerp(U8CPU fromValue, U8CPU toValue, float fraction) {
29 return (U8CPU) (fromValue * (1 - fraction) + toValue * fraction);
30}
31
32// TODO: Add a test for this
33void ColorEvaluator::evaluate(SkColor* outColor,
34 const SkColor& fromColor, const SkColor& toColor, float fraction) const {
35 U8CPU alpha = lerp(SkColorGetA(fromColor), SkColorGetA(toColor), fraction);
36 U8CPU red = lerp(SkColorGetR(fromColor), SkColorGetR(toColor), fraction);
37 U8CPU green = lerp(SkColorGetG(fromColor), SkColorGetG(toColor), fraction);
38 U8CPU blue = lerp(SkColorGetB(fromColor), SkColorGetB(toColor), fraction);
39 *outColor = SkColorSetARGB(alpha, red, green, blue);
40}
41
42void PathEvaluator::evaluate(PathData* out,
43 const PathData& from, const PathData& to, float fraction) const {
44 VectorDrawableUtils::interpolatePaths(out, from, to, fraction);
45}
46
47template<typename T>
48const T PropertyValuesHolderImpl<T>::getValueFromData(float fraction) const {
Doris Liu766431a2016-02-04 22:17:11 +000049 if (mDataSource.size() == 0) {
50 LOG_ALWAYS_FATAL("No data source is defined");
51 return 0;
52 }
53 if (fraction <= 0.0f) {
54 return mDataSource.front();
55 }
56 if (fraction >= 1.0f) {
57 return mDataSource.back();
58 }
59
60 fraction *= mDataSource.size() - 1;
61 int lowIndex = floor(fraction);
62 fraction -= lowIndex;
63
Doris Liua6b967c2016-06-02 16:20:18 -070064 T value;
65 mEvaluator->evaluate(&value, mDataSource[lowIndex], mDataSource[lowIndex + 1], fraction);
Doris Liu766431a2016-02-04 22:17:11 +000066 return value;
67}
68
Doris Liua6b967c2016-06-02 16:20:18 -070069template<typename T>
70const T PropertyValuesHolderImpl<T>::calculateAnimatedValue(float fraction) const {
Doris Liu766431a2016-02-04 22:17:11 +000071 if (mDataSource.size() > 0) {
Doris Liua6b967c2016-06-02 16:20:18 -070072 return getValueFromData(fraction);
Doris Liu766431a2016-02-04 22:17:11 +000073 } else {
Doris Liua6b967c2016-06-02 16:20:18 -070074 T value;
75 mEvaluator->evaluate(&value, mStartValue, mEndValue, fraction);
76 return value;
Doris Liu766431a2016-02-04 22:17:11 +000077 }
Doris Liua6b967c2016-06-02 16:20:18 -070078}
79
80void GroupPropertyValuesHolder::setFraction(float fraction) {
81 float animatedValue = calculateAnimatedValue(fraction);
Doris Liu1d8e1942016-03-02 15:16:28 -080082 mGroup->mutateProperties()->setPropertyValue(mPropertyId, animatedValue);
Doris Liu766431a2016-02-04 22:17:11 +000083}
84
Doris Liu766431a2016-02-04 22:17:11 +000085void FullPathColorPropertyValuesHolder::setFraction(float fraction) {
Doris Liua6b967c2016-06-02 16:20:18 -070086 SkColor animatedValue = calculateAnimatedValue(fraction);
Doris Liu1d8e1942016-03-02 15:16:28 -080087 mFullPath->mutateProperties()->setColorPropertyValue(mPropertyId, animatedValue);
Doris Liu766431a2016-02-04 22:17:11 +000088}
89
90void FullPathPropertyValuesHolder::setFraction(float fraction) {
Doris Liua6b967c2016-06-02 16:20:18 -070091 float animatedValue = calculateAnimatedValue(fraction);
Doris Liu1d8e1942016-03-02 15:16:28 -080092 mFullPath->mutateProperties()->setPropertyValue(mPropertyId, animatedValue);
Doris Liu766431a2016-02-04 22:17:11 +000093}
94
95void PathDataPropertyValuesHolder::setFraction(float fraction) {
Doris Liua6b967c2016-06-02 16:20:18 -070096 mEvaluator->evaluate(&mPathData, mStartValue, mEndValue, fraction);
Doris Liu1d8e1942016-03-02 15:16:28 -080097 mPath->mutateProperties()->setData(mPathData);
Doris Liu766431a2016-02-04 22:17:11 +000098}
99
100void RootAlphaPropertyValuesHolder::setFraction(float fraction) {
Doris Liua6b967c2016-06-02 16:20:18 -0700101 float animatedValue = calculateAnimatedValue(fraction);
Doris Liu1d8e1942016-03-02 15:16:28 -0800102 mTree->mutateProperties()->setRootAlpha(animatedValue);
Doris Liu766431a2016-02-04 22:17:11 +0000103}
104
105} // namepace uirenderer
106} // namespace android