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_IMPLICIT_ANIMATOR_H
17 #define RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_IMPLICIT_ANIMATOR_H
18 
19 #include <stack>
20 #include <utility>
21 #include <vector>
22 
23 #include "animation/rs_animation_timing_curve.h"
24 #include "animation/rs_animation_timing_protocol.h"
25 #include "common/rs_macros.h"
26 
27 namespace OHOS {
28 namespace Rosen {
29 class AnimationFinishCallback;
30 class AnimationRepeatCallback;
31 class RSAnimation;
32 class RSPropertyBase;
33 class RSImplicitAnimationParam;
34 class RSTransitionEffect;
35 class RSMotionPathOption;
36 class RSNode;
37 
38 class RSC_EXPORT RSImplicitAnimator {
39 public:
40     RSImplicitAnimator() = default;
41     virtual ~RSImplicitAnimator() = default;
42 
43     // open implicit animation with given animation options, finish callback and repeatcallback
44     int OpenImplicitAnimation(const RSAnimationTimingProtocol& timingProtocol,
45         const RSAnimationTimingCurve& timingCurve, std::shared_ptr<AnimationFinishCallback>&& finishCallback,
46         std::shared_ptr<AnimationRepeatCallback>&& repeatCallback);
47     // open implicit animation with given animation options and finish callback
48     int OpenImplicitAnimation(const RSAnimationTimingProtocol& timingProtocol,
49         const RSAnimationTimingCurve& timingCurve, std::shared_ptr<AnimationFinishCallback>&& finishCallback);
50     // open implicit animation with current options and given finish callback
51     int OpenImplicitAnimation(std::shared_ptr<AnimationFinishCallback>&& finishCallback);
52     // open implicit animation with current callback and given timing protocol & curve
53     int OpenImplicitAnimation(
54         const RSAnimationTimingProtocol& timingProtocol, const RSAnimationTimingCurve& timingCurve);
55 
56     // close implicit animation and return all animations
57     std::vector<std::shared_ptr<RSAnimation>> CloseImplicitAnimation();
58     // close implicit cancel animation and return whether the synchronization was successful
59     bool CloseImplicitCancelAnimation();
60 
61     // open implicit animation with given animation options and finish callback
62     int OpenInterActiveImplicitAnimation(bool isAddImplictAnimation, const RSAnimationTimingProtocol& timingProtocol,
63         const RSAnimationTimingCurve& timingCurve, std::shared_ptr<AnimationFinishCallback>&& finishCallback);
64     // interactive animator close implicit animation and return all animations
65     std::vector<std::pair<std::shared_ptr<RSAnimation>, NodeId>> CloseInterActiveImplicitAnimation(
66         bool isAddImplictAnimation);
67 
68     void BeginImplicitKeyFrameAnimation(float fraction, const RSAnimationTimingCurve& timingCurve);
69     void BeginImplicitKeyFrameAnimation(float fraction);
70     void EndImplicitKeyFrameAnimation();
71 
72     void BeginImplicitDurationKeyFrameAnimation(int duration, const RSAnimationTimingCurve& timingCurve);
73     void EndImplicitDurationKeyFrameAnimation();
74 
75     void BeginImplicitTransition(const std::shared_ptr<const RSTransitionEffect>& effect, bool isTransitionIn);
76     void EndImplicitTransition();
77 
78     void BeginImplicitPathAnimation(const std::shared_ptr<RSMotionPathOption>& motionPathOption);
79     void EndImplicitPathAnimation();
80 
81     bool NeedImplicitAnimation();
82 
83     void CreateImplicitAnimation(const std::shared_ptr<RSNode>& target, std::shared_ptr<RSPropertyBase> property,
84         const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue);
85 
86     void CreateImplicitAnimationWithInitialVelocity(const std::shared_ptr<RSNode>& target,
87         const std::shared_ptr<RSPropertyBase>& property, const std::shared_ptr<RSPropertyBase>& startValue,
88         const std::shared_ptr<RSPropertyBase>& endValue, const std::shared_ptr<RSPropertyBase>& velocity);
89 
90     void CreateImplicitTransition(RSNode& target);
91     void CancelImplicitAnimation(
92         const std::shared_ptr<RSNode>& target, const std::shared_ptr<RSPropertyBase>& property);
93 
94 private:
95     void EndImplicitAnimation();
96     void BeginImplicitCurveAnimation();
97     void BeginImplicitSpringAnimation();
98     void BeginImplicitInterpolatingSpringAnimation();
99     void BeginImplicitCancelAnimation();
100 
101     void CloseImplicitAnimationInner();
102     void ProcessEmptyAnimations(const std::shared_ptr<AnimationFinishCallback>& finishCallback);
103 
104     void PushImplicitParam(const std::shared_ptr<RSImplicitAnimationParam>& implicitParam);
105     void PopImplicitParam();
106 
107     void SetPropertyValue(std::shared_ptr<RSPropertyBase> property, const std::shared_ptr<RSPropertyBase>& value);
108 
109     void ExecuteWithoutAnimation(const std::function<void()>& callback);
110 
111     std::stack<std::tuple<RSAnimationTimingProtocol, RSAnimationTimingCurve,
112         const std::shared_ptr<AnimationFinishCallback>, std::shared_ptr<AnimationRepeatCallback>>>
113         globalImplicitParams_;
114     std::stack<std::shared_ptr<RSImplicitAnimationParam>> implicitAnimationParams_;
115     std::stack<std::vector<std::pair<std::shared_ptr<RSAnimation>, NodeId>>> implicitAnimations_;
116     std::stack<std::map<std::pair<NodeId, PropertyId>, std::shared_ptr<RSAnimation>>> keyframeAnimations_;
117     std::stack<std::tuple<bool, int, int>> durationKeyframeParams_;
118 
119     bool implicitAnimationDisabled_ { false };
120 
121     std::stack<std::vector<std::pair<std::shared_ptr<RSAnimation>, NodeId>>> interactiveImplicitAnimations_;
122     bool isAddInteractiveAnimator_ { false };
123     friend class RSNode;
124 };
125 } // namespace Rosen
126 } // namespace OHOS
127 
128 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_IMPLICIT_ANIMATOR_H
129