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 #ifndef RENDER_SERVICE_CLIENT_CORE_MODIFIER_RS_MODIFIER_MANAGER_H
17 #define RENDER_SERVICE_CLIENT_CORE_MODIFIER_RS_MODIFIER_MANAGER_H
18 
19 #include <memory>
20 #include <set>
21 #include <unordered_map>
22 
23 #include "animation/rs_animation_rate_decider.h"
24 #include "common/rs_common_def.h"
25 #include "common/rs_macros.h"
26 #include "render_service_base/include/pipeline/rs_render_display_sync.h"
27 
28 namespace OHOS {
29 namespace Rosen {
30 class RSModifier;
31 class RSRenderAnimation;
32 
33 class RSC_EXPORT RSModifierManager {
34 public:
35     RSModifierManager() = default;
36     virtual ~RSModifierManager() = default;
37 
38     void AddModifier(const std::shared_ptr<RSModifier>& modifier);
39     void AddAnimation(const std::shared_ptr<RSRenderAnimation>& animation);
40     void RemoveAnimation(const AnimationId keyId);
41     bool HasUIRunningAnimation();
42 
43     bool Animate(int64_t time, int64_t vsyncPeriod = 0);
44     void Draw();
45 
46     void SetFrameRateGetFunc(const FrameRateGetFunc& func);
47     const FrameRateRange GetFrameRateRange() const;
48 
49     // spring animation related
50     void RegisterSpringAnimation(PropertyId propertyId, AnimationId animId);
51     void UnregisterSpringAnimation(PropertyId propertyId, AnimationId animId);
52     std::shared_ptr<RSRenderAnimation> QuerySpringAnimation(PropertyId propertyId);
53 
54     bool JudgeAnimateWhetherSkip(AnimationId animId, int64_t time, int64_t vsyncPeriod);
55     void SetDisplaySyncEnable(bool isDisplaySyncEnabled);
56     bool IsDisplaySyncEnabled() const;
57     void FlushStartAnimation(int64_t time);
58 
59 private:
60     void OnAnimationFinished(const std::shared_ptr<RSRenderAnimation>& animation);
61     const std::shared_ptr<RSRenderAnimation> GetAnimation(AnimationId id) const;
62 
63     std::set<std::shared_ptr<RSModifier>> modifiers_;
64     std::unordered_map<AnimationId, std::weak_ptr<RSRenderAnimation>> animations_;
65     std::unordered_map<PropertyId, AnimationId> springAnimations_;
66 
67     RSAnimationRateDecider rateDecider_;
68     FrameRateGetFunc frameRateGetFunc_;
69 
70     std::unordered_map<AnimationId, std::shared_ptr<RSRenderDisplaySync>> displaySyncs_;
71     bool isDisplaySyncEnabled_ = false;
72 
73     template <typename T>
74     friend class RSAnimatableProperty;
75 };
76 } // namespace Rosen
77 } // namespace OHOS
78 
79 #endif // RENDER_SERVICE_CLIENT_CORE_MODIFIER_RS_MODIFIER_MANAGER_H
80