blob: 0932d653fd5ec128994f03b4032511372b482a4b [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
28float PropertyValuesHolder::getValueFromData(float fraction) {
29 if (mDataSource.size() == 0) {
30 LOG_ALWAYS_FATAL("No data source is defined");
31 return 0;
32 }
33 if (fraction <= 0.0f) {
34 return mDataSource.front();
35 }
36 if (fraction >= 1.0f) {
37 return mDataSource.back();
38 }
39
40 fraction *= mDataSource.size() - 1;
41 int lowIndex = floor(fraction);
42 fraction -= lowIndex;
43
44 float value = mDataSource[lowIndex] * (1.0f - fraction)
45 + mDataSource[lowIndex + 1] * fraction;
46 return value;
47}
48
49void GroupPropertyValuesHolder::setFraction(float fraction) {
50 float animatedValue;
51 if (mDataSource.size() > 0) {
52 animatedValue = getValueFromData(fraction);
53 } else {
54 animatedValue = mStartValue * (1 - fraction) + mEndValue * fraction;
55 }
Doris Liu1d8e1942016-03-02 15:16:28 -080056 mGroup->mutateProperties()->setPropertyValue(mPropertyId, animatedValue);
Doris Liu766431a2016-02-04 22:17:11 +000057}
58
59inline U8CPU lerp(U8CPU fromValue, U8CPU toValue, float fraction) {
60 return (U8CPU) (fromValue * (1 - fraction) + toValue * fraction);
61}
62
63// TODO: Add a test for this
64SkColor FullPathColorPropertyValuesHolder::interpolateColors(SkColor fromColor, SkColor toColor,
65 float fraction) {
66 U8CPU alpha = lerp(SkColorGetA(fromColor), SkColorGetA(toColor), fraction);
67 U8CPU red = lerp(SkColorGetR(fromColor), SkColorGetR(toColor), fraction);
68 U8CPU green = lerp(SkColorGetG(fromColor), SkColorGetG(toColor), fraction);
69 U8CPU blue = lerp(SkColorGetB(fromColor), SkColorGetB(toColor), fraction);
70 return SkColorSetARGB(alpha, red, green, blue);
71}
72
73void FullPathColorPropertyValuesHolder::setFraction(float fraction) {
74 SkColor animatedValue = interpolateColors(mStartValue, mEndValue, fraction);
Doris Liu1d8e1942016-03-02 15:16:28 -080075 mFullPath->mutateProperties()->setColorPropertyValue(mPropertyId, animatedValue);
Doris Liu766431a2016-02-04 22:17:11 +000076}
77
78void FullPathPropertyValuesHolder::setFraction(float fraction) {
79 float animatedValue;
80 if (mDataSource.size() > 0) {
81 animatedValue = getValueFromData(fraction);
82 } else {
83 animatedValue = mStartValue * (1 - fraction) + mEndValue * fraction;
84 }
Doris Liu1d8e1942016-03-02 15:16:28 -080085 mFullPath->mutateProperties()->setPropertyValue(mPropertyId, animatedValue);
Doris Liu766431a2016-02-04 22:17:11 +000086}
87
88void PathDataPropertyValuesHolder::setFraction(float fraction) {
89 VectorDrawableUtils::interpolatePaths(&mPathData, mStartValue, mEndValue, fraction);
Doris Liu1d8e1942016-03-02 15:16:28 -080090 mPath->mutateProperties()->setData(mPathData);
Doris Liu766431a2016-02-04 22:17:11 +000091}
92
93void RootAlphaPropertyValuesHolder::setFraction(float fraction) {
94 float animatedValue = mStartValue * (1 - fraction) + mEndValue * fraction;
Doris Liu1d8e1942016-03-02 15:16:28 -080095 mTree->mutateProperties()->setRootAlpha(animatedValue);
Doris Liu766431a2016-02-04 22:17:11 +000096}
97
98} // namepace uirenderer
99} // namespace android