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 #include "core/animation/scheduler.h"
17
18 #include "base/log/log.h"
19 #include "core/pipeline/pipeline_base.h"
20
21 namespace OHOS::Ace {
22
Start()23 void Scheduler::Start()
24 {
25 if (isRunning_) {
26 LOGW("Already running, no need to start again.");
27 return;
28 }
29 auto context = context_.Upgrade();
30 if (!context) {
31 LOGE("Start failed, context is null.");
32 return;
33 }
34 isRunning_ = true;
35 startupTimestamp_ = context->GetTimeFromExternalTimer();
36 scheduleId_ = static_cast<int32_t>(context->AddScheduleTask(AceType::Claim(this)));
37 }
38
Stop()39 void Scheduler::Stop()
40 {
41 if (!isRunning_) {
42 LOGD("Already stopped, no need to stop again.");
43 return;
44 }
45 auto context = context_.Upgrade();
46 if (!context) {
47 LOGE("Stop failed, context is null.");
48 return;
49 }
50 isRunning_ = false;
51 context->RemoveScheduleTask(scheduleId_);
52 scheduleId_ = 0;
53 }
54
OnFrame(uint64_t nanoTimestamp)55 void Scheduler::OnFrame(uint64_t nanoTimestamp)
56 {
57 if (!isRunning_) {
58 LOGD("Already stopped, no need to send frame event.");
59 return;
60 }
61
62 // Refresh the startup time every frame.
63 uint64_t elapsedTimeMs = (nanoTimestamp - startupTimestamp_) / 1000000;
64 startupTimestamp_ += elapsedTimeMs * 1000000;
65
66 // Consume previous schedule as default.
67 scheduleId_ = 0;
68 if (callback_) {
69 // Need to convert nanoseconds to milliseconds
70 callback_(elapsedTimeMs);
71 }
72
73 // Schedule next frame task.
74 auto context = context_.Upgrade();
75 if (!context) {
76 LOGE("Schedule next frame task failed, context is null.");
77 return;
78 }
79 if (IsActive()) {
80 scheduleId_ = static_cast<int32_t>(context->AddScheduleTask(AceType::Claim(this)));
81 }
82 }
83
Animate(const AnimationOption & option,const RefPtr<Curve> & curve,const std::function<void ()> propertyCallback,const std::function<void ()> & finishCallBack)84 bool Scheduler::Animate(const AnimationOption& option, const RefPtr<Curve>& curve,
85 const std::function<void()> propertyCallback, const std::function<void()>& finishCallBack)
86 {
87 auto context = context_.Upgrade();
88 if (context == nullptr) {
89 LOGE("Failed to animate asynchronously, context is null!");
90 return false;
91 }
92
93 return context->Animate(option, curve, propertyCallback, finishCallBack);
94 }
95
OpenImplicitAnimation(const AnimationOption & option,const RefPtr<Curve> & curve,const std::function<void ()> & finishCallBack)96 void Scheduler::OpenImplicitAnimation(const AnimationOption& option, const RefPtr<Curve>& curve,
97 const std::function<void()>& finishCallBack)
98 {
99 auto context = context_.Upgrade();
100 if (context == nullptr) {
101 LOGE("Failed to open implicit animation, context is null!");
102 return;
103 }
104
105 return context->OpenImplicitAnimation(option, curve, finishCallBack);
106 }
107
CloseImplicitAnimation()108 bool Scheduler::CloseImplicitAnimation()
109 {
110 auto context = context_.Upgrade();
111 if (context == nullptr) {
112 LOGE("Failed to close implicit animation, context is null!");
113 return false;
114 }
115
116 return context->CloseImplicitAnimation();
117 }
118
AddKeyFrame(float fraction,const RefPtr<Curve> & curve,const std::function<void ()> & propertyCallback)119 void Scheduler::AddKeyFrame(
120 float fraction, const RefPtr<Curve>& curve, const std::function<void()>& propertyCallback)
121 {
122 auto context = context_.Upgrade();
123 if (context == nullptr) {
124 LOGE("Failed to add keyframe, context is null!");
125 return;
126 }
127
128 return context->AddKeyFrame(fraction, curve, propertyCallback);
129 }
130
AddKeyFrame(float fraction,const std::function<void ()> & propertyCallback)131 void Scheduler::AddKeyFrame(float fraction, const std::function<void()>& propertyCallback)
132 {
133 auto context = context_.Upgrade();
134 if (context == nullptr) {
135 LOGE("Failed to add keyframe, context is null!");
136 return;
137 }
138
139 return context->AddKeyFrame(fraction, propertyCallback);
140 }
141
142 } // namespace OHOS::Ace
143