1 /* 2 * Copyright (c) 2022-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 /** 17 * @addtogroup RenderNodeDisplay 18 * @{ 19 * 20 * @brief Display render nodes. 21 */ 22 23 /** 24 * @file rs_modifier_manager.h 25 * 26 * @brief Defines the properties and methods for RSModifierManager class. 27 */ 28 29 #ifndef RENDER_SERVICE_CLIENT_CORE_MODIFIER_RS_MODIFIER_MANAGER_H 30 #define RENDER_SERVICE_CLIENT_CORE_MODIFIER_RS_MODIFIER_MANAGER_H 31 32 #include <memory> 33 #include <set> 34 #include <unordered_map> 35 36 #include "animation/rs_animation_rate_decider.h" 37 #include "common/rs_common_def.h" 38 #include "common/rs_macros.h" 39 #include "render_service_base/include/pipeline/rs_render_display_sync.h" 40 41 namespace OHOS { 42 namespace Rosen { 43 class RSRenderAnimation; 44 namespace ModifierNG { 45 class RSModifier; 46 } 47 48 using Modifier = ModifierNG::RSModifier; 49 50 /** 51 * @class RSModifierManager 52 * 53 * @brief The class for managing RSModifier instances. 54 */ 55 class RSC_EXPORT RSModifierManager { 56 public: 57 /** 58 * @brief Constructor for RSModifierManager. 59 */ 60 RSModifierManager() = default; 61 62 /** 63 * @brief Destructor for RSModifierManager. 64 */ 65 virtual ~RSModifierManager() = default; 66 67 /** 68 * @brief Adds a modifier. 69 * 70 * @param modifier A shared pointer to the RSModifier to be added. 71 */ 72 void AddModifier(const std::shared_ptr<Modifier>& modifier); 73 74 /** 75 * @brief Adds a animation. 76 * 77 * @param animation A shared pointer to the RSRenderAnimation to be added. 78 */ 79 void AddAnimation(const std::shared_ptr<RSRenderAnimation>& animation); 80 81 /** 82 * @brief Removes a animation. 83 * 84 * @param keyId The unique identifier of the animation to be removed. 85 */ 86 void RemoveAnimation(const AnimationId keyId); 87 88 /** 89 * @brief Checks if there are any UI animations currently running. 90 * 91 * @return true if at least one UI animation is running; false otherwise. 92 */ 93 bool HasUIRunningAnimation(); 94 95 /** 96 * @brief Animates the modifier manager based on the given time and optional vsync period. 97 * 98 * @param time The current time used to update the animation state. 99 * @param vsyncPeriod (Optional) The vertical synchronization period. Default is 0. 100 * @return true if the animation state was updated successfully; false otherwise. 101 */ 102 bool Animate(int64_t time, int64_t vsyncPeriod = 0); 103 104 /** 105 * @brief Draws the modifiers. 106 */ 107 void Draw(); 108 109 /** 110 * @brief Gets the first frame animation state and resets it. 111 * 112 * UI animation need this info to get expected frame rate, each window will call it once per frame 113 * 114 * @return true if the first frame animation was active before the reset; false otherwise. 115 */ 116 bool GetAndResetFirstFrameAnimationState(); 117 118 /** 119 * @brief Sets the function to get frame rate. 120 * 121 * @param func The function to be set. 122 */ 123 void SetFrameRateGetFunc(const FrameRateGetFunc& func); 124 125 /** 126 * @brief Gets the range of frame rate. 127 * 128 * @return The range of frame rate. 129 */ 130 const FrameRateRange GetFrameRateRange() const; 131 132 /** 133 * @brief Registers a spring animation. 134 * 135 * @param propertyId The ID of the property associated with the spring animation. 136 * @param animId The ID of the animation to be registered. 137 */ 138 void RegisterSpringAnimation(PropertyId propertyId, AnimationId animId); 139 140 /** 141 * @brief Unregisters a spring animation. 142 * 143 * @param propertyId The ID of the property associated with the spring animation. 144 * @param animId The ID of the animation to be unregistered. 145 */ 146 void UnregisterSpringAnimation(PropertyId propertyId, AnimationId animId); 147 148 /** 149 * @brief Queries a spring animation. 150 * 151 * @param propertyId The ID of the property associated with the spring animation. 152 * @return A shared pointer to the RSRenderAnimation if found; otherwise, nullptr. 153 */ 154 std::shared_ptr<RSRenderAnimation> QuerySpringAnimation(PropertyId propertyId); 155 156 /** 157 * @brief Judges whether the animation should be skipped. 158 * 159 * @param animId The unique identifier of the animation. 160 * @param time The current timestamp. 161 * @param vsyncPeriod The duration of a single vsync period. 162 * @return true if the animation should be skipped for this frame; false otherwise. 163 */ 164 bool JudgeAnimateWhetherSkip(AnimationId animId, int64_t time, int64_t vsyncPeriod); 165 166 /** 167 * @brief Sets the display sync enable state. 168 * 169 * @param isDisplaySyncEnabled true if display sync is enabled; false otherwise. 170 */ 171 void SetDisplaySyncEnable(bool isDisplaySyncEnabled); 172 173 /** 174 * @brief Gets the display sync enable state. 175 * 176 * @return true if display sync is enabled; false otherwise. 177 */ 178 bool IsDisplaySyncEnabled() const; 179 180 /** 181 * @brief Refresh the start time of the animation 182 * 183 * If the animation needs to update its start time, its start time will be set to time. 184 * 185 * @param time The new start time for the animation. 186 */ 187 void FlushStartAnimation(int64_t time); 188 189 private: 190 /** 191 * @brief Callback invoked when a animation has finished. 192 * 193 * @param animation A shared pointer to the RSRenderAnimation that has finished. 194 */ 195 void OnAnimationFinished(const std::shared_ptr<RSRenderAnimation>& animation); 196 197 /** 198 * @brief Gets the animation associated with the specified ID. 199 * 200 * @param id The ID of the animation. 201 * @return A shared pointer to the RSRenderAnimation if found; otherwise, nullptr. 202 */ 203 const std::shared_ptr<RSRenderAnimation> GetAnimation(AnimationId id) const; 204 205 std::set<std::shared_ptr<Modifier>> modifiers_; 206 std::unordered_map<AnimationId, std::weak_ptr<RSRenderAnimation>> animations_; 207 std::unordered_map<PropertyId, AnimationId> springAnimations_; 208 209 RSAnimationRateDecider rateDecider_; 210 FrameRateGetFunc frameRateGetFunc_; 211 bool hasFirstFrameAnimation_ = false; 212 213 std::unordered_map<AnimationId, std::shared_ptr<RSRenderDisplaySync>> displaySyncs_; 214 bool isDisplaySyncEnabled_ = false; 215 216 template <typename T> 217 friend class RSAnimatableProperty; 218 }; 219 } // namespace Rosen 220 } // namespace OHOS 221 222 /** @} */ 223 #endif // RENDER_SERVICE_CLIENT_CORE_MODIFIER_RS_MODIFIER_MANAGER_H 224