1/*
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/Color.h"
20#include "utils/VectorDrawableUtils.h"
21
22#include <utils/Log.h>
23
24namespace android {
25namespace uirenderer {
26
27using namespace VectorDrawable;
28
29inline constexpr float lerp(float fromValue, float toValue, float fraction) {
30    return float (fromValue * (1 - fraction) + toValue * fraction);
31}
32
33inline constexpr float linearize(U8CPU component) {
34    return EOCF_sRGB(component / 255.0f);
35}
36
37// TODO: Add a test for this
38void ColorEvaluator::evaluate(SkColor* outColor,
39        const SkColor& fromColor, const SkColor& toColor, float fraction) const {
40    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);
44    *outColor = SkColorSetARGB(
45            (U8CPU) roundf(a * 255.0f),
46            (U8CPU) roundf(OECF_sRGB(r) * 255.0f),
47            (U8CPU) roundf(OECF_sRGB(g) * 255.0f),
48            (U8CPU) roundf(OECF_sRGB(b) * 255.0f));
49}
50
51void PathEvaluator::evaluate(PathData* out,
52        const PathData& from, const PathData& to, float fraction) const {
53    VectorDrawableUtils::interpolatePaths(out, from, to, fraction);
54}
55
56template<typename T>
57const T PropertyValuesHolderImpl<T>::getValueFromData(float fraction) const {
58    if (mDataSource.size() == 0) {
59        LOG_ALWAYS_FATAL("No data source is defined");
60        return 0;
61    }
62    if (fraction <= 0.0f) {
63        return mDataSource.front();
64    }
65    if (fraction >= 1.0f) {
66        return mDataSource.back();
67    }
68
69    fraction *= mDataSource.size() - 1;
70    int lowIndex = floor(fraction);
71    fraction -= lowIndex;
72
73    T value;
74    mEvaluator->evaluate(&value, mDataSource[lowIndex], mDataSource[lowIndex + 1], fraction);
75    return value;
76}
77
78template<typename T>
79const T PropertyValuesHolderImpl<T>::calculateAnimatedValue(float fraction) const {
80    if (mDataSource.size() > 0) {
81        return getValueFromData(fraction);
82    } else {
83        T value;
84        mEvaluator->evaluate(&value, mStartValue, mEndValue, fraction);
85        return value;
86    }
87}
88
89void GroupPropertyValuesHolder::setFraction(float fraction) {
90    float animatedValue = calculateAnimatedValue(fraction);
91    mGroup->mutateProperties()->setPropertyValue(mPropertyId, animatedValue);
92}
93
94void FullPathColorPropertyValuesHolder::setFraction(float fraction) {
95    SkColor animatedValue = calculateAnimatedValue(fraction);
96    mFullPath->mutateProperties()->setColorPropertyValue(mPropertyId, animatedValue);
97}
98
99void FullPathPropertyValuesHolder::setFraction(float fraction) {
100    float animatedValue = calculateAnimatedValue(fraction);
101    mFullPath->mutateProperties()->setPropertyValue(mPropertyId, animatedValue);
102}
103
104void PathDataPropertyValuesHolder::setFraction(float fraction) {
105    mEvaluator->evaluate(&mPathData, mStartValue, mEndValue, fraction);
106    mPath->mutateProperties()->setData(mPathData);
107}
108
109void RootAlphaPropertyValuesHolder::setFraction(float fraction) {
110    float animatedValue = calculateAnimatedValue(fraction);
111    mTree->mutateProperties()->setRootAlpha(animatedValue);
112}
113
114} // namepace uirenderer
115} // namespace android
116