1 /*
2  * Copyright (c) 2021 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 FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SCHEDULER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SCHEDULER_H
18 
19 #include <functional>
20 
21 #include "core/animation/schedule_task.h"
22 #include "core/components/common/properties/animation_option.h"
23 #include "core/components_ng/manager/display_sync/ui_display_sync.h"
24 
25 namespace OHOS::Ace {
26 
27 class PipelineBase;
28 
29 class ACE_FORCE_EXPORT Scheduler : public ScheduleTask {
30     DECLARE_ACE_TYPE(Scheduler, ScheduleTask);
31 
32 public:
33     using OnFrameCallback = std::function<void(uint64_t)>;
34 
Scheduler(OnFrameCallback && callback,const WeakPtr<PipelineBase> & context)35     Scheduler(OnFrameCallback&& callback, const WeakPtr<PipelineBase>& context)
36         : callback_(std::move(callback)), context_(context)
37     {}
38 
39     Scheduler() = delete;
40 
~Scheduler()41     ~Scheduler() override
42     {
43         if (IsActive()) {
44             Stop();
45         }
46     }
47 
48     void OnFrame(uint64_t nanoTimestamp) override;
49 
50     void Start();
51 
52     void Stop();
53 
IsActive()54     bool IsActive() const
55     {
56         return isRunning_;
57     }
58 
GetContext()59     WeakPtr<PipelineBase> GetContext()
60     {
61         return context_;
62     }
63 
64     bool Animate(const AnimationOption& option, const RefPtr<Curve>& curve,
65         const std::function<void()> propertyCallback, const std::function<void()>& finishCallBack = nullptr);
66 
67     void OpenImplicitAnimation(const AnimationOption& option, const RefPtr<Curve>& curve,
68         const std::function<void()>& finishCallBack = nullptr);
69 
70     bool CloseImplicitAnimation();
71 
72     void AddKeyFrame(
73         float fraction, const RefPtr<Curve>& curve, const std::function<void()>& propertyCallback);
74 
75     void AddKeyFrame(float fraction, const std::function<void()>& propertyCallback);
76 
77     bool PrintVsyncInfoIfNeed() const;
78 
79     void SetExpectedFrameRateRange(const FrameRateRange& frameRateRange);
80 
81 private:
82     int32_t scheduleId_ = 0;
83     bool isRunning_ = false;
84     uint64_t startupTimestamp_ = 0;
85     OnFrameCallback callback_ = nullptr;
86     WeakPtr<PipelineBase> context_;
87     RefPtr<UIDisplaySync> displaySync_ = AceType::MakeRefPtr<UIDisplaySync>(UIObjectType::DISPLAYSYNC_ANIMATOR);
88 };
89 
90 class SchedulerBuilder {
91 public:
Build(Scheduler::OnFrameCallback && callback,const WeakPtr<PipelineBase> & context)92     static RefPtr<Scheduler> Build(Scheduler::OnFrameCallback&& callback, const WeakPtr<PipelineBase>& context)
93     {
94         return AceType::MakeRefPtr<Scheduler>(std::move(callback), context);
95     }
96 };
97 
98 } // namespace OHOS::Ace
99 
100 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SCHEDULER_H
101