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 28inline U8CPU lerp(U8CPU fromValue, U8CPU toValue, float fraction) { 29 return (U8CPU) (fromValue * (1 - fraction) + toValue * fraction); 30} 31 32// TODO: Add a test for this 33void 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); 40} 41 42void PathEvaluator::evaluate(PathData* out, 43 const PathData& from, const PathData& to, float fraction) const { 44 VectorDrawableUtils::interpolatePaths(out, from, to, fraction); 45} 46 47template<typename T> 48const T PropertyValuesHolderImpl<T>::getValueFromData(float fraction) const { 49 if (mDataSource.size() == 0) { 50 LOG_ALWAYS_FATAL("No data source is defined"); 51 return 0; 52 } 53 if (fraction <= 0.0f) { 54 return mDataSource.front(); 55 } 56 if (fraction >= 1.0f) { 57 return mDataSource.back(); 58 } 59 60 fraction *= mDataSource.size() - 1; 61 int lowIndex = floor(fraction); 62 fraction -= lowIndex; 63 64 T value; 65 mEvaluator->evaluate(&value, mDataSource[lowIndex], mDataSource[lowIndex + 1], fraction); 66 return value; 67} 68 69template<typename T> 70const T PropertyValuesHolderImpl<T>::calculateAnimatedValue(float fraction) const { 71 if (mDataSource.size() > 0) { 72 return getValueFromData(fraction); 73 } else { 74 T value; 75 mEvaluator->evaluate(&value, mStartValue, mEndValue, fraction); 76 return value; 77 } 78} 79 80void GroupPropertyValuesHolder::setFraction(float fraction) { 81 float animatedValue = calculateAnimatedValue(fraction); 82 mGroup->mutateProperties()->setPropertyValue(mPropertyId, animatedValue); 83} 84 85void FullPathColorPropertyValuesHolder::setFraction(float fraction) { 86 SkColor animatedValue = calculateAnimatedValue(fraction); 87 mFullPath->mutateProperties()->setColorPropertyValue(mPropertyId, animatedValue); 88} 89 90void FullPathPropertyValuesHolder::setFraction(float fraction) { 91 float animatedValue = calculateAnimatedValue(fraction); 92 mFullPath->mutateProperties()->setPropertyValue(mPropertyId, animatedValue); 93} 94 95void PathDataPropertyValuesHolder::setFraction(float fraction) { 96 mEvaluator->evaluate(&mPathData, mStartValue, mEndValue, fraction); 97 mPath->mutateProperties()->setData(mPathData); 98} 99 100void RootAlphaPropertyValuesHolder::setFraction(float fraction) { 101 float animatedValue = calculateAnimatedValue(fraction); 102 mTree->mutateProperties()->setRootAlpha(animatedValue); 103} 104 105} // namepace uirenderer 106} // namespace android 107