• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_KEYFRAME_ANIMATION_H
17 #define RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_KEYFRAME_ANIMATION_H
18 
19 #include <memory>
20 
21 #include "animation/rs_animation_common.h"
22 #include "animation/rs_animation_timing_curve.h"
23 #include "animation/rs_property_animation.h"
24 #include "common/rs_color.h"
25 #include "common/rs_matrix3.h"
26 #include "common/rs_vector4.h"
27 
28 namespace OHOS {
29 namespace Rosen {
30 static constexpr int FRACTION_INDEX = 0;
31 static constexpr int VALUE_INDEX = 1;
32 static constexpr int INTERPOLATOR_INDEX = 2;
33 
34 template<typename T>
35 class RS_EXPORT RSKeyframeAnimation : public RSPropertyAnimation<T> {
36 public:
RSKeyframeAnimation(const RSAnimatableProperty & property)37     RSKeyframeAnimation(const RSAnimatableProperty& property) : RSPropertyAnimation<T>(property) {}
38     virtual ~RSKeyframeAnimation() = default;
39 
AddKeyFrame(float fraction,const T & value,const RSAnimationTimingCurve & timingCurve)40     void AddKeyFrame(float fraction, const T& value, const RSAnimationTimingCurve& timingCurve)
41     {
42         if (fraction < FRACTION_MIN || fraction > FRACTION_MAX) {
43             return;
44         }
45 
46         if (RSAnimation::IsStarted()) {
47             return;
48         }
49 
50         keyframes_.push_back({ fraction, value, timingCurve });
51     }
52 
AddKeyFrames(const std::vector<std::tuple<float,T,RSAnimationTimingCurve>> & keyframes)53     void AddKeyFrames(const std::vector<std::tuple<float, T, RSAnimationTimingCurve>>& keyframes)
54     {
55         if (RSAnimation::IsStarted()) {
56             return;
57         }
58 
59         keyframes_ = keyframes;
60     }
61 
62 protected:
63     void OnStart() override;
64 
InitInterpolationValue()65     void InitInterpolationValue() override
66     {
67         if (keyframes_.empty()) {
68             return;
69         }
70 
71         auto beginKeyframe = keyframes_.front();
72         if (std::abs(std::get<FRACTION_INDEX>(beginKeyframe) - FRACTION_MIN) > EPSILON) {
73             keyframes_.insert(keyframes_.begin(),
74                 { FRACTION_MIN, RSPropertyAnimation<T>::GetOriginValue(), RSAnimationTimingCurve::LINEAR });
75         }
76 
77         RSPropertyAnimation<T>::startValue_ = std::get<VALUE_INDEX>(keyframes_.front());
78         RSPropertyAnimation<T>::endValue_ = std::get<VALUE_INDEX>(keyframes_.back());
79         RSPropertyAnimation<T>::InitInterpolationValue();
80     }
81 
82 private:
83     std::vector<std::tuple<float, T, RSAnimationTimingCurve>> keyframes_;
84 
85     friend class RSImplicitKeyframeAnimationParam;
86 };
87 
88 
89 template class RSKeyframeAnimation<int>;
90 template class RSKeyframeAnimation<float>;
91 template class RSKeyframeAnimation<Color>;
92 template class RSKeyframeAnimation<Matrix3f>;
93 template class RSKeyframeAnimation<Vector2f>;
94 template class RSKeyframeAnimation<Vector4f>;
95 template class RSKeyframeAnimation<Quaternion>;
96 template class RSKeyframeAnimation<std::shared_ptr<RSFilter>>;
97 
98 } // namespace Rosen
99 } // namespace OHOS
100 
101 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_KEYFRAME_ANIMATION_H
102