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_RENDER_ANIMATION_H 17 #define RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_ANIMATION_H 18 19 #include <parcel.h> 20 #include <refbase.h> 21 #include "animation/rs_animation_common.h" 22 #include "animation/rs_animation_fraction.h" 23 #include "common/rs_macros.h" 24 #include "modifier/rs_render_property.h" 25 26 namespace OHOS { 27 namespace Rosen { 28 class RSRenderNode; 29 30 enum class AnimationState { 31 INITIALIZED, 32 RUNNING, 33 PAUSED, 34 FINISHED, 35 }; 36 37 class RSB_EXPORT RSRenderAnimation : public Parcelable { 38 public: 39 RSRenderAnimation(const RSRenderAnimation&) = delete; 40 RSRenderAnimation(const RSRenderAnimation&&) = delete; 41 RSRenderAnimation& operator=(const RSRenderAnimation&) = delete; 42 RSRenderAnimation& operator=(const RSRenderAnimation&&) = delete; 43 ~RSRenderAnimation() override = default; 44 AnimationId GetAnimationId() const; 45 void Start(); 46 void Finish(); 47 void FinishOnPosition(RSInteractiveAnimationPosition pos); 48 void SetReversedAndContinue(); 49 void Pause(); 50 void Resume(); 51 void SetFraction(float fraction); 52 void SetReversed(bool isReversed); 53 bool Marshalling(Parcel& parcel) const override; 54 virtual bool Animate(int64_t time, int64_t& minLeftDelayTime); 55 56 bool IsStarted() const; 57 bool IsRunning() const; 58 bool IsPaused() const; 59 bool IsFinished() const; 60 void DumpAnimation(std::string& out) const; 61 virtual void DumpAnimationInfo(std::string& out) const; 62 SetAnimationId(AnimationId id)63 void SetAnimationId(AnimationId id) 64 { 65 id_ = id; 66 } 67 SetDuration(int value)68 void SetDuration(int value) 69 { 70 animationFraction_.SetDuration(value); 71 } 72 GetDuration()73 int GetDuration() const 74 { 75 return animationFraction_.GetDuration(); 76 } 77 SetStartDelay(int value)78 void SetStartDelay(int value) 79 { 80 animationFraction_.SetStartDelay(value); 81 } 82 GetStartDelay()83 int GetStartDelay() const 84 { 85 return animationFraction_.GetStartDelay(); 86 } 87 SetRepeatCount(int value)88 void SetRepeatCount(int value) 89 { 90 animationFraction_.SetRepeatCount(value); 91 } 92 GetRepeatCount()93 int GetRepeatCount() const 94 { 95 return animationFraction_.GetRepeatCount(); 96 } 97 SetSpeed(float value)98 void SetSpeed(float value) 99 { 100 animationFraction_.SetSpeed(value); 101 } 102 GetSpeed()103 float GetSpeed() const 104 { 105 return animationFraction_.GetSpeed(); 106 } 107 SetAutoReverse(bool value)108 void SetAutoReverse(bool value) 109 { 110 animationFraction_.SetAutoReverse(value); 111 } 112 GetAutoReverse()113 bool GetAutoReverse() const 114 { 115 return animationFraction_.GetAutoReverse(); 116 } 117 SetFillMode(const FillMode & value)118 void SetFillMode(const FillMode& value) 119 { 120 animationFraction_.SetFillMode(value); 121 } 122 GetFillMode()123 const FillMode& GetFillMode() const 124 { 125 return animationFraction_.GetFillMode(); 126 } 127 SetDirection(bool isForward)128 void SetDirection(bool isForward) 129 { 130 animationFraction_.SetDirection(isForward); 131 } 132 GetDirection()133 bool GetDirection() const 134 { 135 return animationFraction_.GetDirection(); 136 } 137 SetFrameRateRange(FrameRateRange range)138 void SetFrameRateRange(FrameRateRange range) 139 { 140 animationFraction_.SetFrameRateRange(range); 141 } 142 GetFrameRateRange()143 FrameRateRange GetFrameRateRange() const 144 { 145 return animationFraction_.GetFrameRateRange(); 146 } 147 IsCalculateAniamtionValue()148 bool IsCalculateAniamtionValue() const 149 { 150 return calculateAnimationValue_; 151 } 152 GetNeedUpdateStartTime()153 bool GetNeedUpdateStartTime() const 154 { 155 return needUpdateStartTime_; 156 } 157 SetValueFraction(float fraction)158 void SetValueFraction(float fraction) 159 { 160 lastValueFraction_ = fraction; 161 } 162 GetValueFraction()163 float GetValueFraction() const 164 { 165 return lastValueFraction_; 166 } 167 GetToken()168 uint64_t GetToken() const 169 { 170 return token_; 171 } 172 void Attach(RSRenderNode* renderNode); 173 void Detach(bool forceDetach = false); 174 RSRenderNode* GetTarget() const; 175 176 NodeId GetTargetId() const; 177 const std::string GetTargetName() const; 178 179 virtual PropertyId GetPropertyId() const; 180 AttachRenderProperty(const std::shared_ptr<RSRenderPropertyBase> & property)181 virtual void AttachRenderProperty(const std::shared_ptr<RSRenderPropertyBase>& property) {}; 182 183 void SetStartTime(int64_t); 184 185 const std::shared_ptr<RSRenderPropertyBase>& GetAnimateVelocity() const; 186 187 static bool isCalcAnimateVelocity_; 188 189 protected: 190 explicit RSRenderAnimation(AnimationId id); 191 RSRenderAnimation() = default; 192 virtual bool ParseParam(Parcel& parcel); 193 void SetFractionInner(float fraction); 194 OnSetFraction(float fraction)195 virtual void OnSetFraction(float fraction) {} 196 UpdateFractionAfterContinue()197 virtual void UpdateFractionAfterContinue() {} 198 OnAttach()199 virtual void OnAttach() {} 200 OnDetach()201 virtual void OnDetach() {} 202 OnInitialize(int64_t time)203 virtual void OnInitialize(int64_t time) 204 { 205 needInitialize_ = false; 206 } 207 OnAnimate(float fraction)208 virtual void OnAnimate(float fraction) {} 209 DumpFraction(float fraction,int64_t time)210 virtual void DumpFraction(float fraction, int64_t time) {} 211 OnRemoveOnCompletion()212 virtual void OnRemoveOnCompletion() {} 213 RecordLastAnimateValue()214 virtual void RecordLastAnimateValue() {} 215 UpdateAnimateVelocity(float frameInterval)216 virtual void UpdateAnimateVelocity(float frameInterval) {} 217 ProcessAnimateVelocityUnderAngleRotation(float frameInterval)218 virtual void ProcessAnimateVelocityUnderAngleRotation(float frameInterval) {} 219 220 void FinishOnCurrentPosition(); 221 222 RSAnimationFraction animationFraction_; 223 224 // calculateAnimationValue_ is embedded modify for stat animate frame drop 225 bool calculateAnimationValue_ { true }; 226 227 std::shared_ptr<RSRenderPropertyBase> animateVelocity_; 228 229 std::pair<bool, float> fractionChangeInfo_ = { false, 0.0f }; 230 231 private: 232 void ProcessFillModeOnStart(float startFraction); 233 234 void ProcessFillModeOnFinish(float endFraction); 235 236 void ProcessOnRepeatFinish(); 237 SetRepeatCallbackEnable(bool isEnable)238 void SetRepeatCallbackEnable(bool isEnable) 239 { 240 animationFraction_.SetRepeatCallbackEnable(isEnable); 241 } 242 GetRepeatCallbackEnable()243 bool GetRepeatCallbackEnable() const 244 { 245 return animationFraction_.GetRepeatCallbackEnable(); 246 } 247 SetToken(uint64_t token)248 void SetToken(uint64_t token) 249 { 250 token_ = token; 251 } 252 253 AnimationId id_ = 0; 254 NodeId targetId_ = 0; 255 std::string targetName_ = ""; 256 AnimationState state_ { AnimationState::INITIALIZED }; 257 bool needUpdateStartTime_ { true }; 258 bool needInitialize_ { true }; 259 RSRenderNode* target_ { nullptr }; 260 float lastValueFraction_ { 0.0f }; 261 uint64_t token_ = 0; 262 263 friend class RSAnimation; 264 friend class RSModifierManager; 265 #ifdef RS_PROFILER_ENABLED 266 friend class RSProfiler; 267 #endif 268 }; 269 } // namespace Rosen 270 } // namespace OHOS 271 272 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_ANIMATION_H 273