• 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_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() = default;
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 protected:
81     enum class AnimationState {
82         INITIALIZED,
83         RUNNING,
84         PAUSED,
85         FINISHED,
86     };
87 
88     RSAnimation();
89 
OnStart()90     virtual void OnStart() {}
91     virtual void OnReverse();
92     virtual void OnPause();
93     virtual void OnResume();
94     virtual void OnFinish();
95     virtual void OnSetFraction(float fraction);
OnUpdateStagingValue(bool isFirstStart)96     virtual void OnUpdateStagingValue(bool isFirstStart) {};
97     virtual PropertyId GetPropertyId() const;
98 
99     void StartInner(const std::shared_ptr<RSNode>& target);
100     bool IsReversed() const;
101     void UpdateParamToRenderAnimation(const std::shared_ptr<RSRenderAnimation>& animation);
OnCallFinishCallback()102     virtual void OnCallFinishCallback() {}
SetPropertyOnAllAnimationFinish()103     virtual void SetPropertyOnAllAnimationFinish() {}
104 
105     void StartCustomAnimation(const std::shared_ptr<RSRenderAnimation>& animation);
SetInitialVelocity(const std::shared_ptr<RSPropertyBase> & velocity)106     virtual void SetInitialVelocity(const std::shared_ptr<RSPropertyBase>& velocity) {};
107 
108 private:
109     static AnimationId GenerateId();
110     const AnimationId id_;
111 
112     void SetFinishCallback(const std::shared_ptr<AnimationFinishCallback>& finishCallback);
113     void SetRepeatCallback(const std::shared_ptr<AnimationRepeatCallback>& repeatCallback);
114     void UpdateStagingValue(bool isFirstStart);
115     void CallFinishCallback();
116     void CallRepeatCallback();
117     void CallLogicallyFinishCallback();
SetZeroThreshold(const float zeroThreshold)118     virtual void SetZeroThreshold(const float zeroThreshold) {};
119 
120     bool isReversed_ { false };
121     AnimationState state_ { AnimationState::INITIALIZED };
122     std::weak_ptr<RSNode> target_;
123     std::shared_ptr<AnimationFinishCallback> finishCallback_;
124     std::shared_ptr<AnimationRepeatCallback> repeatCallback_;
125     std::shared_ptr<RSRenderAnimation> uiAnimation_;
126 
127     friend class RSCurveImplicitAnimParam;
128     friend class RSAnimationGroup;
129     friend class RSNode;
130     friend class RSImplicitAnimator;
131 };
132 } // namespace Rosen
133 } // namespace OHOS
134 
135 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_ANIMATION_H
136