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/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    }
56    mGroup->mutateProperties()->setPropertyValue(mPropertyId, animatedValue);
57}
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);
75    mFullPath->mutateProperties()->setColorPropertyValue(mPropertyId, animatedValue);
76}
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    }
85    mFullPath->mutateProperties()->setPropertyValue(mPropertyId, animatedValue);
86}
87
88void PathDataPropertyValuesHolder::setFraction(float fraction) {
89    VectorDrawableUtils::interpolatePaths(&mPathData, mStartValue, mEndValue, fraction);
90    mPath->mutateProperties()->setData(mPathData);
91}
92
93void RootAlphaPropertyValuesHolder::setFraction(float fraction) {
94    float animatedValue = mStartValue * (1 - fraction) + mEndValue * fraction;
95    mTree->mutateProperties()->setRootAlpha(animatedValue);
96}
97
98} // namepace uirenderer
99} // namespace android
100