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_property.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(); 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 bool IsUiAnimation() const; 80 81 void InteractivePause(); 82 83 void InteractiveContinue(); 84 85 void InteractiveFinish(RSInteractiveAnimationPosition pos); 86 87 void InteractiveReverse(); 88 89 void InteractiveSetFraction(float fraction); 90 IsSupportInteractiveAnimator()91 virtual bool IsSupportInteractiveAnimator() { return true; } 92 GetPropertyType()93 virtual ModifierNG::RSPropertyType GetPropertyType() const 94 { 95 return ModifierNG::RSPropertyType::INVALID; 96 } 97 98 std::string DumpAnimation() const; DumpAnimationInfo(std::string & dumpInfo)99 virtual void DumpAnimationInfo(std::string& dumpInfo) const {} 100 protected: 101 enum class AnimationState { 102 INITIALIZED, 103 RUNNING, 104 PAUSED, 105 FINISHED, 106 }; 107 108 RSAnimation(); 109 OnStart()110 virtual void OnStart() {} 111 virtual void OnReverse(); 112 virtual void OnPause(); 113 virtual void OnResume(); 114 virtual void OnFinish(); 115 virtual void OnSetFraction(float fraction); OnUpdateStagingValue(bool isFirstStart)116 virtual void OnUpdateStagingValue(bool isFirstStart) {}; UpdateStagingValueOnInteractiveFinish(RSInteractiveAnimationPosition pos)117 virtual void UpdateStagingValueOnInteractiveFinish(RSInteractiveAnimationPosition pos) {}; 118 virtual PropertyId GetPropertyId() const; 119 120 void StartInner(const std::shared_ptr<RSNode>& target); 121 bool IsReversed() const; 122 void UpdateParamToRenderAnimation(const std::shared_ptr<RSRenderAnimation>& animation); OnCallFinishCallback()123 virtual void OnCallFinishCallback() {} SetPropertyOnAllAnimationFinish()124 virtual void SetPropertyOnAllAnimationFinish() {} 125 126 void StartCustomAnimation(const std::shared_ptr<RSRenderAnimation>& animation); SetInitialVelocity(const std::shared_ptr<RSPropertyBase> & velocity)127 virtual void SetInitialVelocity(const std::shared_ptr<RSPropertyBase>& velocity) {}; 128 129 private: 130 static AnimationId GenerateId(); 131 const AnimationId id_; 132 133 void SetFinishCallback(const std::shared_ptr<AnimationFinishCallback>& finishCallback); 134 void SetRepeatCallback(const std::shared_ptr<AnimationRepeatCallback>& repeatCallback); 135 void SetInteractiveFinishCallback(const std::shared_ptr<InteractiveAnimatorFinishCallback>& finishCallback); 136 void UpdateStagingValue(bool isFirstStart); 137 void CallFinishCallback(); 138 void CallRepeatCallback(); 139 void CallLogicallyFinishCallback(); SetZeroThreshold(const float zeroThreshold)140 virtual void SetZeroThreshold(const float zeroThreshold) {}; 141 142 bool isReversed_ { false }; 143 AnimationState state_ { AnimationState::INITIALIZED }; 144 std::weak_ptr<RSNode> target_; 145 std::shared_ptr<AnimationFinishCallback> finishCallback_; 146 std::shared_ptr<AnimationRepeatCallback> repeatCallback_; 147 std::shared_ptr<InteractiveAnimatorFinishCallback> interactiveFinishCallback_; 148 std::shared_ptr<RSRenderAnimation> uiAnimation_; 149 150 friend class RSCurveImplicitAnimParam; 151 friend class RSAnimationGroup; 152 friend class RSNode; 153 friend class RSImplicitAnimator; 154 friend class RSInteractiveImplictAnimator; 155 friend class RSUIContext; 156 }; 157 } // namespace Rosen 158 } // namespace OHOS 159 160 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_ANIMATION_H 161