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 Pause(); 48 void Resume(); 49 void SetFraction(float fraction); 50 void SetReversed(bool isReversed); 51 bool Marshalling(Parcel& parcel) const override; 52 virtual bool Animate(int64_t time); 53 54 bool IsStarted() const; 55 bool IsRunning() const; 56 bool IsPaused() const; 57 bool IsFinished() const; 58 SetAnimationId(AnimationId id)59 void SetAnimationId(AnimationId id) 60 { 61 id_ = id; 62 } 63 SetDuration(int value)64 void SetDuration(int value) 65 { 66 animationFraction_.SetDuration(value); 67 } 68 GetDuration()69 int GetDuration() const 70 { 71 return animationFraction_.GetDuration(); 72 } 73 SetStartDelay(int value)74 void SetStartDelay(int value) 75 { 76 animationFraction_.SetStartDelay(value); 77 } 78 GetStartDelay()79 int GetStartDelay() const 80 { 81 return animationFraction_.GetStartDelay(); 82 } 83 SetRepeatCount(int value)84 void SetRepeatCount(int value) 85 { 86 animationFraction_.SetRepeatCount(value); 87 } 88 GetRepeatCount()89 int GetRepeatCount() const 90 { 91 return animationFraction_.GetRepeatCount(); 92 } 93 SetSpeed(float value)94 void SetSpeed(float value) 95 { 96 animationFraction_.SetSpeed(value); 97 } 98 GetSpeed()99 float GetSpeed() const 100 { 101 return animationFraction_.GetSpeed(); 102 } 103 SetAutoReverse(bool value)104 void SetAutoReverse(bool value) 105 { 106 animationFraction_.SetAutoReverse(value); 107 } 108 GetAutoReverse()109 bool GetAutoReverse() const 110 { 111 return animationFraction_.GetAutoReverse(); 112 } 113 SetFillMode(const FillMode & value)114 void SetFillMode(const FillMode& value) 115 { 116 animationFraction_.SetFillMode(value); 117 } 118 GetFillMode()119 const FillMode& GetFillMode() const 120 { 121 return animationFraction_.GetFillMode(); 122 } 123 SetDirection(bool isForward)124 void SetDirection(bool isForward) 125 { 126 animationFraction_.SetDirection(isForward); 127 } 128 GetDirection()129 bool GetDirection() const 130 { 131 return animationFraction_.GetDirection(); 132 } 133 SetFrameRateRange(FrameRateRange range)134 void SetFrameRateRange(FrameRateRange range) 135 { 136 animationFraction_.SetFrameRateRange(range); 137 } 138 GetFrameRateRange()139 FrameRateRange GetFrameRateRange() const 140 { 141 return animationFraction_.GetFrameRateRange(); 142 } 143 144 void Attach(RSRenderNode* renderNode); 145 void Detach(); 146 RSRenderNode* GetTarget() const; 147 148 NodeId GetTargetId() const; 149 150 virtual PropertyId GetPropertyId() const; 151 AttachRenderProperty(const std::shared_ptr<RSRenderPropertyBase> & property)152 virtual void AttachRenderProperty(const std::shared_ptr<RSRenderPropertyBase>& property) {}; 153 154 void SetStartTime(int64_t); 155 156 protected: 157 explicit RSRenderAnimation(AnimationId id); 158 RSRenderAnimation() = default; 159 virtual bool ParseParam(Parcel& parcel); 160 void SetFractionInner(float fraction); 161 162 virtual void OnSetFraction(float fraction); 163 OnAttach()164 virtual void OnAttach() {} 165 OnDetach()166 virtual void OnDetach() {} 167 OnInitialize(int64_t time)168 virtual void OnInitialize(int64_t time) 169 { 170 needInitialize_ = false; 171 } 172 OnAnimate(float fraction)173 virtual void OnAnimate(float fraction) {} 174 OnRemoveOnCompletion()175 virtual void OnRemoveOnCompletion() {} 176 177 void FinishOnCurrentPosition(); 178 179 RSAnimationFraction animationFraction_; 180 181 private: 182 void ProcessFillModeOnStart(float startFraction); 183 184 void ProcessFillModeOnFinish(float endFraction); 185 186 void ProcessOnRepeatFinish(); 187 SetRepeatCallbackEnable(bool isEnable)188 void SetRepeatCallbackEnable(bool isEnable) 189 { 190 animationFraction_.SetRepeatCallbackEnable(isEnable); 191 } 192 GetRepeatCallbackEnable()193 bool GetRepeatCallbackEnable() const 194 { 195 return animationFraction_.GetRepeatCallbackEnable(); 196 } 197 198 AnimationId id_ = 0; 199 NodeId targetId_ = 0; 200 AnimationState state_ { AnimationState::INITIALIZED }; 201 bool needUpdateStartTime_ { true }; 202 bool needInitialize_ { true }; 203 RSRenderNode* target_ { nullptr }; 204 205 friend class RSAnimation; 206 }; 207 } // namespace Rosen 208 } // namespace OHOS 209 210 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_ANIMATION_H 211