• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_VALUE_ESTIMATOR_H
17 #define RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_VALUE_ESTIMATOR_H
18 
19 #include <memory>
20 
21 #include "animation/rs_animation_common.h"
22 #include "animation/rs_interpolator.h"
23 #include "common/rs_color.h"
24 #include "common/rs_macros.h"
25 #include "common/rs_matrix3.h"
26 #include "common/rs_vector2.h"
27 #include "common/rs_vector4.h"
28 #include "modifier/rs_modifier_type.h"
29 #include "render/rs_filter.h"
30 
31 namespace OHOS {
32 namespace Rosen {
33 class RSRenderPropertyBase;
34 template<typename T>
35 class RSRenderAnimatableProperty;
36 
37 class RSB_EXPORT RSValueEstimator {
38 public:
39     template<typename T>
Estimate(float fraction,const T & startValue,const T & endValue)40     T Estimate(float fraction, const T& startValue, const T& endValue)
41     {
42         return startValue * (1.0f - fraction) + endValue * fraction;
43     }
44 
45     Quaternion Estimate(float fraction, const Quaternion& startValue, const Quaternion& endValue);
46 
47     std::shared_ptr<RSFilter> Estimate(
48         float fraction, const std::shared_ptr<RSFilter>& startValue, const std::shared_ptr<RSFilter>& endValue);
49 
EstimateFraction(const std::shared_ptr<RSInterpolator> & interpolator)50     virtual float EstimateFraction(const std::shared_ptr<RSInterpolator>& interpolator)
51     {
52         return 0.0f;
53     }
54 
InitCurveAnimationValue(const std::shared_ptr<RSRenderPropertyBase> & property,const std::shared_ptr<RSRenderPropertyBase> & startValue,const std::shared_ptr<RSRenderPropertyBase> & endValue,const std::shared_ptr<RSRenderPropertyBase> & lastValue)55     virtual void InitCurveAnimationValue(const std::shared_ptr<RSRenderPropertyBase>& property,
56         const std::shared_ptr<RSRenderPropertyBase>& startValue,
57         const std::shared_ptr<RSRenderPropertyBase>& endValue,
58         const std::shared_ptr<RSRenderPropertyBase>& lastValue) {}
59 
InitKeyframeAnimationValue(const std::shared_ptr<RSRenderPropertyBase> & property,std::vector<std::tuple<float,std::shared_ptr<RSRenderPropertyBase>,std::shared_ptr<RSInterpolator>>> & keyframes,const std::shared_ptr<RSRenderPropertyBase> & lastValue)60     virtual void InitKeyframeAnimationValue(const std::shared_ptr<RSRenderPropertyBase>& property,
61         std::vector<std::tuple<float, std::shared_ptr<RSRenderPropertyBase>,
62         std::shared_ptr<RSInterpolator>>>& keyframes,
63         const std::shared_ptr<RSRenderPropertyBase>& lastValue) {}
64 
65     virtual void UpdateAnimationValue(const float fraction, const bool isAdditive) = 0;
66 };
67 
68 template<typename T>
69 class RSB_EXPORT_TMP RSCurveValueEstimator : public RSValueEstimator {
70 public:
71     RSCurveValueEstimator() = default;
72     virtual ~RSCurveValueEstimator() = default;
73 
InitCurveAnimationValue(const std::shared_ptr<RSRenderPropertyBase> & property,const std::shared_ptr<RSRenderPropertyBase> & startValue,const std::shared_ptr<RSRenderPropertyBase> & endValue,const std::shared_ptr<RSRenderPropertyBase> & lastValue)74     void InitCurveAnimationValue(const std::shared_ptr<RSRenderPropertyBase>& property,
75         const std::shared_ptr<RSRenderPropertyBase>& startValue,
76         const std::shared_ptr<RSRenderPropertyBase>& endValue,
77         const std::shared_ptr<RSRenderPropertyBase>& lastValue) override
78     {
79         auto animatableProperty = std::static_pointer_cast<RSRenderAnimatableProperty<T>>(property);
80         auto animatableStartValue = std::static_pointer_cast<RSRenderAnimatableProperty<T>>(startValue);
81         auto animatableEndValue = std::static_pointer_cast<RSRenderAnimatableProperty<T>>(endValue);
82         auto animatableLastValue = std::static_pointer_cast<RSRenderAnimatableProperty<T>>(lastValue);
83         if (animatableProperty && animatableStartValue && animatableEndValue && animatableLastValue) {
84             property_ = animatableProperty;
85             startValue_ = animatableStartValue->Get();
86             endValue_ = animatableEndValue->Get();
87             lastValue_ = animatableLastValue->Get();
88         }
89     }
90 
UpdateAnimationValue(const float fraction,const bool isAdditive)91     void UpdateAnimationValue(const float fraction, const bool isAdditive) override
92     {
93         auto animationValue = GetAnimationValue(fraction, isAdditive);
94         if (property_ != nullptr) {
95             property_->Set(animationValue);
96         }
97     }
98 
GetAnimationValue(const float fraction,const bool isAdditive)99     T GetAnimationValue(const float fraction, const bool isAdditive)
100     {
101         auto interpolationValue = RSValueEstimator::Estimate(fraction, startValue_, endValue_);
102         auto animationValue = interpolationValue;
103         if (isAdditive && property_ != nullptr) {
104             animationValue = property_->Get() + interpolationValue - lastValue_;
105         }
106         lastValue_ = interpolationValue;
107         return animationValue;
108     }
109 
EstimateFraction(const std::shared_ptr<RSInterpolator> & interpolator)110     float EstimateFraction(const std::shared_ptr<RSInterpolator>& interpolator) override
111     {
112         return 0.0f;
113     }
114 
115 private:
116     T startValue_ {};
117     T endValue_ {};
118     T lastValue_ {};
119     std::shared_ptr<RSRenderAnimatableProperty<T>> property_;
120 };
121 
122 template<>
123 float RSCurveValueEstimator<float>::EstimateFraction(const std::shared_ptr<RSInterpolator>& interpolator);
124 
125 extern template class RSCurveValueEstimator<float>;
126 
127 template<typename T>
128 class RSKeyframeValueEstimator : public RSValueEstimator {
129 public:
130     RSKeyframeValueEstimator() = default;
131     virtual ~RSKeyframeValueEstimator() = default;
132 
InitKeyframeAnimationValue(const std::shared_ptr<RSRenderPropertyBase> & property,std::vector<std::tuple<float,std::shared_ptr<RSRenderPropertyBase>,std::shared_ptr<RSInterpolator>>> & keyframes,const std::shared_ptr<RSRenderPropertyBase> & lastValue)133     void InitKeyframeAnimationValue(const std::shared_ptr<RSRenderPropertyBase>& property,
134         std::vector<std::tuple<float, std::shared_ptr<RSRenderPropertyBase>,
135         std::shared_ptr<RSInterpolator>>>& keyframes,
136         const std::shared_ptr<RSRenderPropertyBase>& lastValue) override
137     {
138         auto animatableProperty = std::static_pointer_cast<RSRenderAnimatableProperty<T>>(property);
139         auto animatableLastValue = std::static_pointer_cast<RSRenderAnimatableProperty<T>>(lastValue);
140         if (animatableProperty && animatableLastValue) {
141             property_ = animatableProperty;
142             lastValue_ = animatableLastValue->Get();
143         }
144         for (const auto& keyframe : keyframes) {
145             auto keyframeValue = std::static_pointer_cast<RSRenderAnimatableProperty<T>>(std::get<1>(keyframe));
146             if (keyframeValue != nullptr) {
147                 keyframes_.push_back({ std::get<0>(keyframe), keyframeValue->Get(), std::get<2>(keyframe) });
148             }
149         }
150     }
151 
UpdateAnimationValue(const float fraction,const bool isAdditive)152     void UpdateAnimationValue(const float fraction, const bool isAdditive) override
153     {
154         auto animationValue = GetAnimationValue(fraction, isAdditive);
155         if (property_ != nullptr) {
156             property_->Set(animationValue);
157         }
158     }
159 
GetAnimationValue(const float fraction,const bool isAdditive)160     T GetAnimationValue(const float fraction, const bool isAdditive)
161     {
162         float preKeyframeFraction = std::get<0>(keyframes_.front());
163         auto preKeyframeValue = std::get<1>(keyframes_.front());
164         for (const auto& keyframe : keyframes_) {
165             // the index of tuple
166             float keyframeFraction = std::get<0>(keyframe);
167             auto keyframeValue = std::get<1>(keyframe);
168             auto keyframeInterpolator = std::get<2>(keyframe);
169             if (fraction <= keyframeFraction) {
170                 if (ROSEN_EQ(keyframeFraction, preKeyframeFraction)) {
171                     continue;
172                 }
173 
174                 float intervalFraction = (fraction - preKeyframeFraction) / (keyframeFraction - preKeyframeFraction);
175                 auto interpolationValue = RSValueEstimator::Estimate(
176                     keyframeInterpolator->Interpolate(intervalFraction), preKeyframeValue, keyframeValue);
177                 auto animationValue = interpolationValue;
178                 if (isAdditive && property_ != nullptr) {
179                     animationValue = property_->Get() + interpolationValue - lastValue_;
180                 }
181                 lastValue_ = interpolationValue;
182                 return animationValue;
183             }
184 
185             preKeyframeFraction = keyframeFraction;
186             preKeyframeValue = keyframeValue;
187         }
188         return preKeyframeValue;
189     }
190 
191 private:
192     std::vector<std::tuple<float, T, std::shared_ptr<RSInterpolator>>> keyframes_;
193     T lastValue_ {};
194     std::shared_ptr<RSRenderAnimatableProperty<T>> property_;
195 };
196 
197 enum class RSValueEstimatorType : int16_t {
198     INVALID = 0,
199     CURVE_VALUE_ESTIMATOR,
200     KEYFRAME_VALUE_ESTIMATOR,
201     PATH_VALUE_ESTIMATOR,
202 };
203 } // namespace Rosen
204 } // namespace OHOS
205 
206 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_VALUE_ESTIMATOR_H
207