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 27 #ifdef _WIN32 28 #include <windows.h> 29 #define gettid GetCurrentThreadId 30 #endif 31 32 #ifdef __APPLE__ 33 #define gettid getpid 34 #endif 35 36 #ifdef __gnu_linux__ 37 #include <sys/types.h> 38 #include <sys/syscall.h> 39 #define gettid []() -> int32_t { return static_cast<int32_t>(syscall(SYS_gettid)); } 40 #endif 41 42 namespace OHOS { 43 namespace Rosen { 44 class RSNode; 45 class AnimationFinishCallback; 46 class AnimationRepeatCallback; 47 class RSRenderAnimation; 48 49 class RSC_EXPORT RSAnimation : public RSAnimationTimingProtocol, public std::enable_shared_from_this<RSAnimation> { 50 public: 51 virtual ~RSAnimation() = default; 52 53 AnimationId GetId() const; 54 55 void SetFinishCallback(const std::function<void()>& finishCallback); 56 57 void Start(const std::shared_ptr<RSNode>& target); 58 59 const std::weak_ptr<RSNode> GetTarget() const; 60 61 void Pause(); 62 63 void Resume(); 64 65 void Finish(); 66 67 void Reverse(); 68 69 void SetFraction(float fraction); 70 71 bool IsStarted() const; 72 73 bool IsRunning() const; 74 75 bool IsPaused() const; 76 77 bool IsFinished() const; 78 79 protected: 80 enum class AnimationState { 81 INITIALIZED, 82 RUNNING, 83 PAUSED, 84 FINISHED, 85 }; 86 87 RSAnimation(); 88 OnStart()89 virtual void OnStart() {} 90 virtual void OnReverse(); 91 virtual void OnPause(); 92 virtual void OnResume(); 93 virtual void OnFinish(); 94 virtual void OnSetFraction(float fraction); OnUpdateStagingValue(bool isFirstStart)95 virtual void OnUpdateStagingValue(bool isFirstStart) {}; 96 virtual PropertyId GetPropertyId() const; 97 98 void StartInner(const std::shared_ptr<RSNode>& target); 99 bool IsReversed() const; 100 void UpdateParamToRenderAnimation(const std::shared_ptr<RSRenderAnimation>& animation); OnCallFinishCallback()101 virtual void OnCallFinishCallback() {} SetPropertyOnAllAnimationFinish()102 virtual void SetPropertyOnAllAnimationFinish() {} 103 104 void StartCustomAnimation(const std::shared_ptr<RSRenderAnimation>& animation); 105 106 private: 107 static AnimationId GenerateId(); 108 const AnimationId id_; 109 110 void SetFinishCallback(const std::shared_ptr<AnimationFinishCallback>& finishCallback); 111 void SetRepeatCallback(const std::shared_ptr<AnimationRepeatCallback>& repeatCallback); 112 void UpdateStagingValue(bool isFirstStart); 113 void CallFinishCallback(); 114 void CallRepeatCallback(); 115 116 bool isReversed_ { false }; 117 AnimationState state_ { AnimationState::INITIALIZED }; 118 std::weak_ptr<RSNode> target_; 119 std::shared_ptr<AnimationFinishCallback> finishCallback_; 120 std::shared_ptr<AnimationRepeatCallback> repeatCallback_; 121 std::shared_ptr<RSRenderAnimation> uiAnimation_; 122 123 friend class RSCurveImplicitAnimParam; 124 friend class RSAnimationGroup; 125 friend class RSNode; 126 friend class RSImplicitAnimator; 127 }; 128 } // namespace Rosen 129 } // namespace OHOS 130 131 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_ANIMATION_H 132