Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 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 | |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 19 | #include "utils/Color.h" |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 20 | #include "utils/VectorDrawableUtils.h" |
| 21 | |
| 22 | #include <utils/Log.h> |
| 23 | |
| 24 | namespace android { |
| 25 | namespace uirenderer { |
| 26 | |
| 27 | using namespace VectorDrawable; |
| 28 | |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 29 | inline constexpr float lerp(float fromValue, float toValue, float fraction) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 30 | return float(fromValue * (1 - fraction) + toValue * fraction); |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | inline constexpr float linearize(U8CPU component) { |
| 34 | return EOCF_sRGB(component / 255.0f); |
Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // TODO: Add a test for this |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 38 | void ColorEvaluator::evaluate(SkColor* outColor, const SkColor& fromColor, const SkColor& toColor, |
| 39 | float fraction) const { |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 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); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 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)); |
Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 47 | } |
| 48 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 49 | void PathEvaluator::evaluate(PathData* out, const PathData& from, const PathData& to, |
| 50 | float fraction) const { |
Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 51 | VectorDrawableUtils::interpolatePaths(out, from, to, fraction); |
| 52 | } |
| 53 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 54 | template <typename T> |
Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 55 | const T PropertyValuesHolderImpl<T>::getValueFromData(float fraction) const { |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 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 | |
Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 71 | T value; |
| 72 | mEvaluator->evaluate(&value, mDataSource[lowIndex], mDataSource[lowIndex + 1], fraction); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 73 | return value; |
| 74 | } |
| 75 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 76 | template <typename T> |
Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 77 | const T PropertyValuesHolderImpl<T>::calculateAnimatedValue(float fraction) const { |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 78 | if (mDataSource.size() > 0) { |
Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 79 | return getValueFromData(fraction); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 80 | } else { |
Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 81 | T value; |
| 82 | mEvaluator->evaluate(&value, mStartValue, mEndValue, fraction); |
| 83 | return value; |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 84 | } |
Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void GroupPropertyValuesHolder::setFraction(float fraction) { |
| 88 | float animatedValue = calculateAnimatedValue(fraction); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 89 | mGroup->mutateProperties()->setPropertyValue(mPropertyId, animatedValue); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 92 | void FullPathColorPropertyValuesHolder::setFraction(float fraction) { |
Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 93 | SkColor animatedValue = calculateAnimatedValue(fraction); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 94 | mFullPath->mutateProperties()->setColorPropertyValue(mPropertyId, animatedValue); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void FullPathPropertyValuesHolder::setFraction(float fraction) { |
Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 98 | float animatedValue = calculateAnimatedValue(fraction); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 99 | mFullPath->mutateProperties()->setPropertyValue(mPropertyId, animatedValue); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void PathDataPropertyValuesHolder::setFraction(float fraction) { |
Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 103 | mEvaluator->evaluate(&mPathData, mStartValue, mEndValue, fraction); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 104 | mPath->mutateProperties()->setData(mPathData); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void RootAlphaPropertyValuesHolder::setFraction(float fraction) { |
Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 108 | float animatedValue = calculateAnimatedValue(fraction); |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 109 | mTree->mutateProperties()->setRootAlpha(animatedValue); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 110 | } |
| 111 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 112 | } // namepace uirenderer |
| 113 | } // namespace android |