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, const SkColor& fromColor, const SkColor& toColor,
39                              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((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));
47}
48
49void PathEvaluator::evaluate(PathData* out, const PathData& from, const PathData& to,
50                             float fraction) const {
51    VectorDrawableUtils::interpolatePaths(out, from, to, fraction);
52}
53
54template <typename T>
55const T PropertyValuesHolderImpl<T>::getValueFromData(float fraction) const {
56    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
71    T value;
72    mEvaluator->evaluate(&value, mDataSource[lowIndex], mDataSource[lowIndex + 1], fraction);
73    return value;
74}
75
76template <typename T>
77const T PropertyValuesHolderImpl<T>::calculateAnimatedValue(float fraction) const {
78    if (mDataSource.size() > 0) {
79        return getValueFromData(fraction);
80    } else {
81        T value;
82        mEvaluator->evaluate(&value, mStartValue, mEndValue, fraction);
83        return value;
84    }
85}
86
87void GroupPropertyValuesHolder::setFraction(float fraction) {
88    float animatedValue = calculateAnimatedValue(fraction);
89    mGroup->mutateProperties()->setPropertyValue(mPropertyId, animatedValue);
90}
91
92void FullPathColorPropertyValuesHolder::setFraction(float fraction) {
93    SkColor animatedValue = calculateAnimatedValue(fraction);
94    mFullPath->mutateProperties()->setColorPropertyValue(mPropertyId, animatedValue);
95}
96
97void FullPathPropertyValuesHolder::setFraction(float fraction) {
98    float animatedValue = calculateAnimatedValue(fraction);
99    mFullPath->mutateProperties()->setPropertyValue(mPropertyId, animatedValue);
100}
101
102void PathDataPropertyValuesHolder::setFraction(float fraction) {
103    mEvaluator->evaluate(&mPathData, mStartValue, mEndValue, fraction);
104    mPath->mutateProperties()->setData(mPathData);
105}
106
107void RootAlphaPropertyValuesHolder::setFraction(float fraction) {
108    float animatedValue = calculateAnimatedValue(fraction);
109    mTree->mutateProperties()->setRootAlpha(animatedValue);
110}
111
112}  // namepace uirenderer
113}  // namespace android
114