| 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 | #pragma once | 
|  | 18 |  | 
|  | 19 | #include "VectorDrawable.h" | 
|  | 20 |  | 
|  | 21 | #include <SkColor.h> | 
|  | 22 |  | 
|  | 23 | namespace android { | 
|  | 24 | namespace uirenderer { | 
|  | 25 |  | 
|  | 26 | /** | 
|  | 27 | * PropertyValues holder contains data needed to change a property of a Vector Drawable object. | 
|  | 28 | * When a fraction in [0f, 1f] is provided, the holder will calculate an interpolated value based | 
|  | 29 | * on its start and end value, and set the new value on the VectorDrawble's corresponding property. | 
|  | 30 | */ | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 31 | class PropertyValuesHolder { | 
| Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 32 | public: | 
|  | 33 | virtual void setFraction(float fraction) = 0; | 
| Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 34 | virtual ~PropertyValuesHolder() {} | 
| Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 35 | }; | 
|  | 36 |  | 
| Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 37 | template <typename T> | 
|  | 38 | class Evaluator { | 
|  | 39 | public: | 
|  | 40 | virtual void evaluate(T* out, const T& from, const T& to, float fraction) const {}; | 
|  | 41 | virtual ~Evaluator() {} | 
|  | 42 | }; | 
|  | 43 |  | 
|  | 44 | class FloatEvaluator : public Evaluator<float> { | 
|  | 45 | public: | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 46 | virtual void evaluate(float* out, const float& from, const float& to, | 
|  | 47 | float fraction) const override { | 
| Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 48 | *out = from * (1 - fraction) + to * fraction; | 
|  | 49 | } | 
|  | 50 | }; | 
|  | 51 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 52 | class ColorEvaluator : public Evaluator<SkColor> { | 
| Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 53 | public: | 
|  | 54 | virtual void evaluate(SkColor* outColor, const SkColor& from, const SkColor& to, | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 55 | float fraction) const override; | 
| Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 56 | }; | 
|  | 57 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 58 | class PathEvaluator : public Evaluator<PathData> { | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 59 | virtual void evaluate(PathData* out, const PathData& from, const PathData& to, | 
|  | 60 | float fraction) const override; | 
| Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 61 | }; | 
|  | 62 |  | 
|  | 63 | template <typename T> | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 64 | class PropertyValuesHolderImpl : public PropertyValuesHolder { | 
| Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 65 | public: | 
|  | 66 | PropertyValuesHolderImpl(const T& startValue, const T& endValue) | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 67 | : mStartValue(startValue), mEndValue(endValue) {} | 
| Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 68 | void setPropertyDataSource(T* dataSource, int length) { | 
|  | 69 | mDataSource.insert(mDataSource.begin(), dataSource, dataSource + length); | 
|  | 70 | } | 
|  | 71 | // Calculate the animated value from the data source. | 
|  | 72 | const T getValueFromData(float fraction) const; | 
|  | 73 | // Convenient method to favor getting animated value from data source. If no data source is set | 
|  | 74 | // fall back to linear interpolation. | 
|  | 75 | const T calculateAnimatedValue(float fraction) const; | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 76 |  | 
| Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 77 | protected: | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 78 | std::unique_ptr<Evaluator<T>> mEvaluator = nullptr; | 
|  | 79 | // This contains uniformly sampled data throughout the animation duration. The first element | 
|  | 80 | // should be the start value and the last should be the end value of the animation. When the | 
|  | 81 | // data source is set, we'll favor data source over the linear interpolation of start/end value | 
|  | 82 | // for calculation of animated value. | 
|  | 83 | std::vector<T> mDataSource; | 
|  | 84 | T mStartValue; | 
|  | 85 | T mEndValue; | 
| Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 86 | }; | 
|  | 87 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 88 | class GroupPropertyValuesHolder : public PropertyValuesHolderImpl<float> { | 
| Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 89 | public: | 
|  | 90 | GroupPropertyValuesHolder(VectorDrawable::Group* ptr, int propertyId, float startValue, | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 91 | float endValue) | 
|  | 92 | : PropertyValuesHolderImpl(startValue, endValue), mGroup(ptr), mPropertyId(propertyId) { | 
| Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 93 | mEvaluator.reset(new FloatEvaluator()); | 
| Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 94 | } | 
|  | 95 | void setFraction(float fraction) override; | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 96 |  | 
| Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 97 | private: | 
|  | 98 | VectorDrawable::Group* mGroup; | 
|  | 99 | int mPropertyId; | 
| Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 100 | }; | 
|  | 101 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 102 | class FullPathColorPropertyValuesHolder : public PropertyValuesHolderImpl<SkColor> { | 
| Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 103 | public: | 
| Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 104 | FullPathColorPropertyValuesHolder(VectorDrawable::FullPath* ptr, int propertyId, | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 105 | SkColor startValue, SkColor endValue) | 
| Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 106 | : PropertyValuesHolderImpl(startValue, endValue) | 
|  | 107 | , mFullPath(ptr) | 
|  | 108 | , mPropertyId(propertyId) { | 
|  | 109 | mEvaluator.reset(new ColorEvaluator()); | 
|  | 110 | } | 
| Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 111 | void setFraction(float fraction) override; | 
|  | 112 | static SkColor interpolateColors(SkColor fromColor, SkColor toColor, float fraction); | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 113 |  | 
| Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 114 | private: | 
|  | 115 | VectorDrawable::FullPath* mFullPath; | 
|  | 116 | int mPropertyId; | 
| Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 117 | }; | 
|  | 118 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 119 | class FullPathPropertyValuesHolder : public PropertyValuesHolderImpl<float> { | 
| Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 120 | public: | 
|  | 121 | FullPathPropertyValuesHolder(VectorDrawable::FullPath* ptr, int propertyId, float startValue, | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 122 | float endValue) | 
| Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 123 | : PropertyValuesHolderImpl(startValue, endValue) | 
|  | 124 | , mFullPath(ptr) | 
|  | 125 | , mPropertyId(propertyId) { | 
|  | 126 | mEvaluator.reset(new FloatEvaluator()); | 
|  | 127 | }; | 
| Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 128 | void setFraction(float fraction) override; | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 129 |  | 
| Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 130 | private: | 
|  | 131 | VectorDrawable::FullPath* mFullPath; | 
|  | 132 | int mPropertyId; | 
| Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 133 | }; | 
|  | 134 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 135 | class PathDataPropertyValuesHolder : public PropertyValuesHolderImpl<PathData> { | 
| Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 136 | public: | 
|  | 137 | PathDataPropertyValuesHolder(VectorDrawable::Path* ptr, PathData* startValue, | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 138 | PathData* endValue) | 
|  | 139 | : PropertyValuesHolderImpl(*startValue, *endValue), mPath(ptr) { | 
| Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 140 | mEvaluator.reset(new PathEvaluator()); | 
|  | 141 | }; | 
| Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 142 | void setFraction(float fraction) override; | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 143 |  | 
| Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 144 | private: | 
|  | 145 | VectorDrawable::Path* mPath; | 
|  | 146 | PathData mPathData; | 
| Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 147 | }; | 
|  | 148 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 149 | class RootAlphaPropertyValuesHolder : public PropertyValuesHolderImpl<float> { | 
| Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 150 | public: | 
|  | 151 | RootAlphaPropertyValuesHolder(VectorDrawable::Tree* tree, float startValue, float endValue) | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 152 | : PropertyValuesHolderImpl(startValue, endValue), mTree(tree) { | 
| Doris Liu | a6b967c | 2016-06-02 16:20:18 -0700 | [diff] [blame] | 153 | mEvaluator.reset(new FloatEvaluator()); | 
|  | 154 | } | 
| Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 155 | void setFraction(float fraction) override; | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 156 |  | 
| Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 157 | private: | 
|  | 158 | VectorDrawable::Tree* mTree; | 
| Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 159 | }; | 
|  | 160 | } | 
|  | 161 | } |