1 /*
2 * Copyright (c) 2021-2022 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 "gradual_animator.h"
17
18 #include "display_log.h"
19 #include "watchdog.h"
20
21 namespace OHOS {
22 namespace DisplayPowerMgr {
GradualAnimator(const std::string & name,std::shared_ptr<AnimateCallback> & callback)23 GradualAnimator::GradualAnimator(const std::string& name, std::shared_ptr<AnimateCallback>& callback)
24 : name_(name), callback_(callback)
25 {
26 fromBrightness_ = 0;
27 toBrightness_ = 0;
28 currentBrightness_ = 0;
29 duration_ = 0;
30 totalSteps_ = 0;
31 stride_ = 0;
32 updateTime_ = DEFAULT_UPDATE_TIME;
33 handler_ = nullptr;
34 }
35
StartAnimation(uint32_t from,uint32_t to,uint32_t duration)36 void GradualAnimator::StartAnimation(uint32_t from, uint32_t to, uint32_t duration)
37 {
38 if (animating_) {
39 DISPLAY_HILOGD(FEAT_BRIGHTNESS, "animation is running, no need to start again");
40 return;
41 }
42 DISPLAY_HILOGD(FEAT_BRIGHTNESS, "animation from=%{public}u, to=%{public}u, duration=%{public}u",
43 from, to, duration);
44 if (callback_ == nullptr) {
45 DISPLAY_HILOGW(FEAT_BRIGHTNESS, "callback_ is nullptr");
46 return;
47 }
48 fromBrightness_ = from;
49 toBrightness_ = to;
50 currentBrightness_ = fromBrightness_;
51 duration_ = duration;
52 totalSteps_ = duration_ / updateTime_;
53 if (totalSteps_ < 1) {
54 totalSteps_ = 1;
55 }
56 int32_t changeBrightness = static_cast<int32_t>(toBrightness_) - static_cast<int32_t>(fromBrightness_);
57 stride_ = changeBrightness / static_cast<int32_t>(totalSteps_);
58 if (abs(stride_) < STRIDE_ABSOLUTE_MIN) {
59 stride_ = (changeBrightness / abs(changeBrightness)) * STRIDE_ABSOLUTE_MIN;
60 }
61 currentStep_ = 0;
62 if (handler_ == nullptr) {
63 eventRunner_ = AppExecFwk::EventRunner::Create(name_);
64 if (eventRunner_ == nullptr) {
65 DISPLAY_HILOGW(FEAT_BRIGHTNESS, "GradualAnimator failed due to create EventRunner");
66 return;
67 }
68 handler_ = std::make_shared<AnimatorHandler>(eventRunner_, shared_from_this());
69 HiviewDFX::Watchdog::GetInstance().AddThread(name_, handler_);
70 }
71 animating_ = true;
72 handler_->SendEvent(EVENT_STEP, 0, updateTime_);
73 DISPLAY_HILOGD(FEAT_BRIGHTNESS, "animation started");
74 }
75
StopAnimation()76 void GradualAnimator::StopAnimation()
77 {
78 animating_ = false;
79 handler_->RemoveEvent(EVENT_STEP);
80 if (callback_ == nullptr) {
81 DISPLAY_HILOGW(FEAT_BRIGHTNESS, "Callback is nullptr");
82 return;
83 }
84 callback_->OnEnd();
85 DISPLAY_HILOGD(FEAT_BRIGHTNESS, "animation stopped");
86 }
87
IsAnimating() const88 bool GradualAnimator::IsAnimating() const
89 {
90 return animating_;
91 }
92
GetAnimationUpdateTime() const93 uint32_t GradualAnimator::GetAnimationUpdateTime() const
94 {
95 return updateTime_;
96 }
97
NextStep()98 void GradualAnimator::NextStep()
99 {
100 if (!animating_) {
101 DISPLAY_HILOGW(FEAT_BRIGHTNESS, "is not animating, return");
102 return;
103 }
104 if (callback_ == nullptr) {
105 DISPLAY_HILOGW(FEAT_BRIGHTNESS, "Callback is nullptr");
106 return;
107 }
108 currentStep_++;
109 if (currentStep_ == 1) {
110 callback_->OnStart();
111 }
112 if (currentStep_ < totalSteps_) {
113 uint32_t nextBrightness = currentBrightness_ + stride_;
114 bool isOutRange = stride_ > 0 ? (nextBrightness >= toBrightness_) : (nextBrightness <= toBrightness_);
115 if (isOutRange) {
116 currentBrightness_ = toBrightness_;
117 callback_->OnChanged(currentBrightness_);
118 callback_->OnEnd();
119 animating_ = false;
120 DISPLAY_HILOGD(FEAT_BRIGHTNESS, "next step brightness is out range, brightness=%{public}u", nextBrightness);
121 } else {
122 currentBrightness_ = nextBrightness;
123 callback_->OnChanged(currentBrightness_);
124 handler_->SendEvent(EVENT_STEP, 0, updateTime_);
125 }
126 } else {
127 currentBrightness_ = toBrightness_;
128 callback_->OnChanged(currentBrightness_);
129 callback_->OnEnd();
130 animating_ = false;
131 }
132 DISPLAY_HILOGD(FEAT_BRIGHTNESS, "animating next step, step=%{public}u, brightness=%{public}u, stride=%{public}d",
133 currentStep_, currentBrightness_, stride_);
134 }
135
AnimatorHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner,const std::shared_ptr<GradualAnimator> & owner)136 GradualAnimator::AnimatorHandler::AnimatorHandler(
137 const std::shared_ptr<AppExecFwk::EventRunner>& runner,
138 const std::shared_ptr<GradualAnimator>& owner)
139 : AppExecFwk::EventHandler(runner), owner_(owner)
140 {
141 DISPLAY_HILOGD(FEAT_BRIGHTNESS, "AnimatorHandler is created");
142 }
143
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)144 void GradualAnimator::AnimatorHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event)
145 {
146 DISPLAY_HILOGD(FEAT_BRIGHTNESS, "process animator event ,eventId = %{public}d", event->GetInnerEventId());
147 std::shared_ptr<GradualAnimator> animator = owner_.lock();
148 if (animator == nullptr) {
149 DISPLAY_HILOGD(FEAT_BRIGHTNESS, "animator is nullptr");
150 return;
151 }
152 switch (event->GetInnerEventId()) {
153 case EVENT_STEP: {
154 animator->NextStep();
155 break;
156 }
157 default:
158 break;
159 }
160 }
161 } // namespace DisplayPowerMgr
162 } // namespace OHOS
163