blob: 35526255759ff75819eded2d04b3b94126b3b164 [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
Romain Guy253f2c22016-09-28 17:34:42 -070019#include "utils/Color.h"
Doris Liu766431a2016-02-04 22:17:11 +000020#include "utils/VectorDrawableUtils.h"
21
22#include <utils/Log.h>
23
24namespace android {
25namespace uirenderer {
26
27using namespace VectorDrawable;
28
Romain Guy253f2c22016-09-28 17:34:42 -070029inline constexpr float lerp(float fromValue, float toValue, float fraction) {
John Reck1bcacfd2017-11-03 10:12:19 -070030 return float(fromValue * (1 - fraction) + toValue * fraction);
Romain Guy253f2c22016-09-28 17:34:42 -070031}
32
33inline constexpr float linearize(U8CPU component) {
34 return EOCF_sRGB(component / 255.0f);
Doris Liua6b967c2016-06-02 16:20:18 -070035}
36
37// TODO: Add a test for this
John Reck1bcacfd2017-11-03 10:12:19 -070038void ColorEvaluator::evaluate(SkColor* outColor, const SkColor& fromColor, const SkColor& toColor,
39 float fraction) const {
Romain Guy253f2c22016-09-28 17:34:42 -070040 float a = lerp(SkColorGetA(fromColor) / 255.0f, SkColorGetA(toColor) / 255.0f, fraction);
41 float r = lerp(linearize(SkColorGetR(fromColor)), linearize(SkColorGetR(toColor)), fraction);
42 float g = lerp(linearize(SkColorGetG(fromColor)), linearize(SkColorGetG(toColor)), fraction);
43 float b = lerp(linearize(SkColorGetB(fromColor)), linearize(SkColorGetB(toColor)), fraction);
John Reck1bcacfd2017-11-03 10:12:19 -070044 *outColor = SkColorSetARGB((U8CPU)roundf(a * 255.0f), (U8CPU)roundf(OECF_sRGB(r) * 255.0f),
45 (U8CPU)roundf(OECF_sRGB(g) * 255.0f),
46 (U8CPU)roundf(OECF_sRGB(b) * 255.0f));
Doris Liua6b967c2016-06-02 16:20:18 -070047}
48
John Reck1bcacfd2017-11-03 10:12:19 -070049void PathEvaluator::evaluate(PathData* out, const PathData& from, const PathData& to,
50 float fraction) const {
Doris Liua6b967c2016-06-02 16:20:18 -070051 VectorDrawableUtils::interpolatePaths(out, from, to, fraction);
52}
53
John Reck1bcacfd2017-11-03 10:12:19 -070054template <typename T>
Doris Liua6b967c2016-06-02 16:20:18 -070055const T PropertyValuesHolderImpl<T>::getValueFromData(float fraction) const {
Doris Liu766431a2016-02-04 22:17:11 +000056 if (mDataSource.size() == 0) {
57 LOG_ALWAYS_FATAL("No data source is defined");
58 return 0;
59 }
60 if (fraction <= 0.0f) {
61 return mDataSource.front();
62 }
63 if (fraction >= 1.0f) {
64 return mDataSource.back();
65 }
66
67 fraction *= mDataSource.size() - 1;
68 int lowIndex = floor(fraction);
69 fraction -= lowIndex;
70
Doris Liua6b967c2016-06-02 16:20:18 -070071 T value;
72 mEvaluator->evaluate(&value, mDataSource[lowIndex], mDataSource[lowIndex + 1], fraction);
Doris Liu766431a2016-02-04 22:17:11 +000073 return value;
74}
75
John Reck1bcacfd2017-11-03 10:12:19 -070076template <typename T>
Doris Liua6b967c2016-06-02 16:20:18 -070077const T PropertyValuesHolderImpl<T>::calculateAnimatedValue(float fraction) const {
Doris Liu766431a2016-02-04 22:17:11 +000078 if (mDataSource.size() > 0) {
Doris Liua6b967c2016-06-02 16:20:18 -070079 return getValueFromData(fraction);
Doris Liu766431a2016-02-04 22:17:11 +000080 } else {
Doris Liua6b967c2016-06-02 16:20:18 -070081 T value;
82 mEvaluator->evaluate(&value, mStartValue, mEndValue, fraction);
83 return value;
Doris Liu766431a2016-02-04 22:17:11 +000084 }
Doris Liua6b967c2016-06-02 16:20:18 -070085}
86
87void GroupPropertyValuesHolder::setFraction(float fraction) {
88 float animatedValue = calculateAnimatedValue(fraction);
Doris Liu1d8e1942016-03-02 15:16:28 -080089 mGroup->mutateProperties()->setPropertyValue(mPropertyId, animatedValue);
Doris Liu766431a2016-02-04 22:17:11 +000090}
91
Doris Liu766431a2016-02-04 22:17:11 +000092void FullPathColorPropertyValuesHolder::setFraction(float fraction) {
Doris Liua6b967c2016-06-02 16:20:18 -070093 SkColor animatedValue = calculateAnimatedValue(fraction);
Doris Liu1d8e1942016-03-02 15:16:28 -080094 mFullPath->mutateProperties()->setColorPropertyValue(mPropertyId, animatedValue);
Doris Liu766431a2016-02-04 22:17:11 +000095}
96
97void FullPathPropertyValuesHolder::setFraction(float fraction) {
Doris Liua6b967c2016-06-02 16:20:18 -070098 float animatedValue = calculateAnimatedValue(fraction);
Doris Liu1d8e1942016-03-02 15:16:28 -080099 mFullPath->mutateProperties()->setPropertyValue(mPropertyId, animatedValue);
Doris Liu766431a2016-02-04 22:17:11 +0000100}
101
102void PathDataPropertyValuesHolder::setFraction(float fraction) {
Doris Liua6b967c2016-06-02 16:20:18 -0700103 mEvaluator->evaluate(&mPathData, mStartValue, mEndValue, fraction);
Doris Liu1d8e1942016-03-02 15:16:28 -0800104 mPath->mutateProperties()->setData(mPathData);
Doris Liu766431a2016-02-04 22:17:11 +0000105}
106
107void RootAlphaPropertyValuesHolder::setFraction(float fraction) {
Doris Liua6b967c2016-06-02 16:20:18 -0700108 float animatedValue = calculateAnimatedValue(fraction);
Doris Liu1d8e1942016-03-02 15:16:28 -0800109 mTree->mutateProperties()->setRootAlpha(animatedValue);
Doris Liu766431a2016-02-04 22:17:11 +0000110}
111
John Reck1bcacfd2017-11-03 10:12:19 -0700112} // namepace uirenderer
113} // namespace android