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_ANIMATION_H
17 #define RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_ANIMATION_H
18 
19 #include <memory>
20 #include <unistd.h>
21 
22 #include "animation/rs_animation_common.h"
23 #include "animation/rs_animation_timing_protocol.h"
24 #include "common/rs_common_def.h"
25 #include "modifier/rs_modifier_type.h"
26 #include "modifier/rs_property.h"
27 
28 #ifdef _WIN32
29 #include <windows.h>
30 #define gettid GetCurrentThreadId
31 #endif
32 
33 #ifdef __APPLE__
34 #define gettid getpid
35 #endif
36 
37 #ifdef __gnu_linux__
38 #include <sys/types.h>
39 #include <sys/syscall.h>
40 #define gettid []() -> int32_t { return static_cast<int32_t>(syscall(SYS_gettid)); }
41 #endif
42 
43 namespace OHOS {
44 namespace Rosen {
45 class RSNode;
46 class AnimationFinishCallback;
47 class AnimationRepeatCallback;
48 class RSRenderAnimation;
49 
50 class RSC_EXPORT RSAnimation : public RSAnimationTimingProtocol, public std::enable_shared_from_this<RSAnimation> {
51 public:
52     virtual ~RSAnimation();
53 
54     AnimationId GetId() const;
55 
56     void SetFinishCallback(const std::function<void()>& finishCallback);
57 
58     void Start(const std::shared_ptr<RSNode>& target);
59 
60     const std::weak_ptr<RSNode> GetTarget() const;
61 
62     void Pause();
63 
64     void Resume();
65 
66     void Finish();
67 
68     void Reverse();
69 
70     void SetFraction(float fraction);
71 
72     bool IsStarted() const;
73 
74     bool IsRunning() const;
75 
76     bool IsPaused() const;
77 
78     bool IsFinished() const;
79 
80     bool IsUiAnimation() const;
81 
82     void InteractivePause();
83 
84     void InteractiveContinue();
85 
86     void InteractiveFinish(RSInteractiveAnimationPosition pos);
87 
88     void InteractiveReverse();
89 
90     void InteractiveSetFraction(float fraction);
91 
IsSupportInteractiveAnimator()92     virtual bool IsSupportInteractiveAnimator() { return true; }
93 protected:
94     enum class AnimationState {
95         INITIALIZED,
96         RUNNING,
97         PAUSED,
98         FINISHED,
99     };
100 
101     RSAnimation();
102 
OnStart()103     virtual void OnStart() {}
104     virtual void OnReverse();
105     virtual void OnPause();
106     virtual void OnResume();
107     virtual void OnFinish();
108     virtual void OnSetFraction(float fraction);
OnUpdateStagingValue(bool isFirstStart)109     virtual void OnUpdateStagingValue(bool isFirstStart) {};
UpdateStagingValueOnInteractiveFinish(RSInteractiveAnimationPosition pos)110     virtual void UpdateStagingValueOnInteractiveFinish(RSInteractiveAnimationPosition pos) {};
111     virtual PropertyId GetPropertyId() const;
112 
113     void StartInner(const std::shared_ptr<RSNode>& target);
114     bool IsReversed() const;
115     void UpdateParamToRenderAnimation(const std::shared_ptr<RSRenderAnimation>& animation);
OnCallFinishCallback()116     virtual void OnCallFinishCallback() {}
SetPropertyOnAllAnimationFinish()117     virtual void SetPropertyOnAllAnimationFinish() {}
118 
119     void StartCustomAnimation(const std::shared_ptr<RSRenderAnimation>& animation);
SetInitialVelocity(const std::shared_ptr<RSPropertyBase> & velocity)120     virtual void SetInitialVelocity(const std::shared_ptr<RSPropertyBase>& velocity) {};
121 
122 private:
123     static AnimationId GenerateId();
124     const AnimationId id_;
125 
126     void SetFinishCallback(const std::shared_ptr<AnimationFinishCallback>& finishCallback);
127     void SetRepeatCallback(const std::shared_ptr<AnimationRepeatCallback>& repeatCallback);
128     void SetInteractiveFinishCallback(const std::shared_ptr<InteractiveAnimatorFinishCallback>& finishCallback);
129     void UpdateStagingValue(bool isFirstStart);
130     void CallFinishCallback();
131     void CallRepeatCallback();
132     void CallLogicallyFinishCallback();
SetZeroThreshold(const float zeroThreshold)133     virtual void SetZeroThreshold(const float zeroThreshold) {};
134 
135     bool isReversed_ { false };
136     AnimationState state_ { AnimationState::INITIALIZED };
137     std::weak_ptr<RSNode> target_;
138     std::shared_ptr<AnimationFinishCallback> finishCallback_;
139     std::shared_ptr<AnimationRepeatCallback> repeatCallback_;
140     std::shared_ptr<InteractiveAnimatorFinishCallback> interactiveFinishCallback_;
141     std::shared_ptr<RSRenderAnimation> uiAnimation_;
142 
143     friend class RSCurveImplicitAnimParam;
144     friend class RSAnimationGroup;
145     friend class RSNode;
146     friend class RSImplicitAnimator;
147     friend class RSInteractiveImplictAnimator;
148 };
149 } // namespace Rosen
150 } // namespace OHOS
151 
152 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_ANIMATION_H
153