2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "PropertyValuesHolder.h"
19 #include "utils/VectorDrawableUtils.h"
21 #include <utils/Log.h>
24 namespace uirenderer {
26 using namespace VectorDrawable;
28 inline U8CPU lerp(U8CPU fromValue, U8CPU toValue, float fraction) {
29 return (U8CPU) (fromValue * (1 - fraction) + toValue * fraction);
32 // TODO: Add a test for this
33 void ColorEvaluator::evaluate(SkColor* outColor,
34 const SkColor& fromColor, const SkColor& toColor, float fraction) const {
35 U8CPU alpha = lerp(SkColorGetA(fromColor), SkColorGetA(toColor), fraction);
36 U8CPU red = lerp(SkColorGetR(fromColor), SkColorGetR(toColor), fraction);
37 U8CPU green = lerp(SkColorGetG(fromColor), SkColorGetG(toColor), fraction);
38 U8CPU blue = lerp(SkColorGetB(fromColor), SkColorGetB(toColor), fraction);
39 *outColor = SkColorSetARGB(alpha, red, green, blue);
42 void PathEvaluator::evaluate(PathData* out,
43 const PathData& from, const PathData& to, float fraction) const {
44 VectorDrawableUtils::interpolatePaths(out, from, to, fraction);
48 const T PropertyValuesHolderImpl<T>::getValueFromData(float fraction) const {
49 if (mDataSource.size() == 0) {
50 LOG_ALWAYS_FATAL("No data source is defined");
53 if (fraction <= 0.0f) {
54 return mDataSource.front();
56 if (fraction >= 1.0f) {
57 return mDataSource.back();
60 fraction *= mDataSource.size() - 1;
61 int lowIndex = floor(fraction);
65 mEvaluator->evaluate(&value, mDataSource[lowIndex], mDataSource[lowIndex + 1], fraction);
70 const T PropertyValuesHolderImpl<T>::calculateAnimatedValue(float fraction) const {
71 if (mDataSource.size() > 0) {
72 return getValueFromData(fraction);
75 mEvaluator->evaluate(&value, mStartValue, mEndValue, fraction);
80 void GroupPropertyValuesHolder::setFraction(float fraction) {
81 float animatedValue = calculateAnimatedValue(fraction);
82 mGroup->mutateProperties()->setPropertyValue(mPropertyId, animatedValue);
85 void FullPathColorPropertyValuesHolder::setFraction(float fraction) {
86 SkColor animatedValue = calculateAnimatedValue(fraction);
87 mFullPath->mutateProperties()->setColorPropertyValue(mPropertyId, animatedValue);
90 void FullPathPropertyValuesHolder::setFraction(float fraction) {
91 float animatedValue = calculateAnimatedValue(fraction);
92 mFullPath->mutateProperties()->setPropertyValue(mPropertyId, animatedValue);
95 void PathDataPropertyValuesHolder::setFraction(float fraction) {
96 mEvaluator->evaluate(&mPathData, mStartValue, mEndValue, fraction);
97 mPath->mutateProperties()->setData(mPathData);
100 void RootAlphaPropertyValuesHolder::setFraction(float fraction) {
101 float animatedValue = calculateAnimatedValue(fraction);
102 mTree->mutateProperties()->setRootAlpha(animatedValue);
105 } // namepace uirenderer
106 } // namespace android