• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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 "brightness_dimming.h"
17 
18 #include "brightness_dimming_callback.h"
19 #include "display_log.h"
20 
21 namespace OHOS {
22 namespace DisplayPowerMgr {
23 using namespace PowerMgr;
24 namespace {
25 FFRTHandle g_animatorTaskHandle;
26 }
27 
BrightnessDimming(const std::string & name,std::shared_ptr<BrightnessDimmingCallback> & callback)28 BrightnessDimming::BrightnessDimming(const std::string& name, std::shared_ptr<BrightnessDimmingCallback>& callback)
29     : mName(name), mCallback(callback)
30 {
31     mFromBrightness = 0;
32     mToBrightness = 0;
33     mCurrentBrightness = 0;
34     mDuration = 0;
35     mTotalSteps = 0;
36     mStride = 0;
37     mUpdateTime = DEFAULT_UPDATE_TIME;
38 }
39 
Init()40 bool BrightnessDimming::Init()
41 {
42     mQueue = std::make_shared<FFRTQueue> ("brightness_animator_queue");
43     if (mQueue == nullptr) {
44         return false;
45     }
46     return true;
47 }
48 
Reset()49 void BrightnessDimming::Reset()
50 {
51     DISPLAY_HILOGI(FEAT_BRIGHTNESS, "start dimming queue");
52     if (mQueue) {
53         mQueue.reset();
54         g_animatorTaskHandle = nullptr;
55         DISPLAY_HILOGI(FEAT_BRIGHTNESS, "destruct dimming_queue");
56     }
57 }
58 
StartDimming(uint32_t from,uint32_t to,uint32_t duration)59 void BrightnessDimming::StartDimming(uint32_t from, uint32_t to, uint32_t duration)
60 {
61     if (mDimming) {
62         DISPLAY_HILOGI(FEAT_BRIGHTNESS, "animation is running, no need to start again, from=%{public}u, "\
63             " to=%{public}u, duration=%{public}u", from, to, duration);
64         return;
65     }
66     DISPLAY_HILOGI(FEAT_BRIGHTNESS, "animation from=%{public}u, to=%{public}u, duration=%{public}u",
67         from, to, duration);
68     if (mCallback == nullptr) {
69         DISPLAY_HILOGW(FEAT_BRIGHTNESS, "mCallback is nullptr");
70         return;
71     }
72     mFromBrightness = from;
73     mToBrightness = to;
74     mCurrentBrightness = mFromBrightness.load();
75     mDuration = duration;
76     mTotalSteps = mDuration / mUpdateTime;
77     if (mTotalSteps < 1) {
78         mTotalSteps = 1;
79     }
80     int32_t changeBrightness = static_cast<int32_t>(mToBrightness) - static_cast<int32_t>(mFromBrightness);
81     if (changeBrightness == 0) {
82         return;
83     }
84     mStride = changeBrightness / static_cast<int32_t>(mTotalSteps);
85     if (abs(mStride) < STRIDE_ABSOLUTE_MIN) {
86         mStride = (changeBrightness / abs(changeBrightness)) * STRIDE_ABSOLUTE_MIN;
87     }
88     DISPLAY_HILOGI(FEAT_BRIGHTNESS, "animation orig mTotalSteps=%{public}d", static_cast<int32_t>(mTotalSteps));
89     if (mTotalSteps > 1 && abs(mStride) >= 1) {
90         mTotalSteps = static_cast<uint32_t>(static_cast<uint32_t>(abs(changeBrightness)) -
91             static_cast<uint32_t>(abs(mStride)) * mTotalSteps) / static_cast<uint32_t>(abs(mStride)) + mTotalSteps;
92         DISPLAY_HILOGI(FEAT_BRIGHTNESS, "animation update mTotalSteps=%{public}d", static_cast<int32_t>(mTotalSteps));
93     }
94     mCurrentStep = 0;
95     mDimming = true;
96     FFRTTask task = [this] { this->NextStep(); };
97     std::lock_guard<std::mutex> lock(mAnimatorHandleLock);
98     g_animatorTaskHandle = FFRTUtils::SubmitDelayTask(task, mUpdateTime, mQueue);
99 }
100 
StopDimming()101 void BrightnessDimming::StopDimming()
102 {
103     mDimming = false;
104     mAnimatorHandleLock.lock();
105     FFRT_CANCEL(g_animatorTaskHandle, mQueue);
106     mAnimatorHandleLock.unlock();
107     if (mCallback == nullptr) {
108         DISPLAY_HILOGW(FEAT_BRIGHTNESS, "Callback is nullptr");
109         return;
110     }
111     mCallback->OnEnd();
112     DISPLAY_HILOGI(FEAT_BRIGHTNESS, "animation stopped");
113 }
114 
IsDimming() const115 bool BrightnessDimming::IsDimming() const
116 {
117     return mDimming;
118 }
119 
GetDimmingUpdateTime() const120 uint32_t BrightnessDimming::GetDimmingUpdateTime() const
121 {
122     return mUpdateTime;
123 }
124 
NextStep()125 void BrightnessDimming::NextStep()
126 {
127     if (!mDimming) {
128         DISPLAY_HILOGW(FEAT_BRIGHTNESS, "is not animating, return");
129         return;
130     }
131     if (mCallback == nullptr) {
132         DISPLAY_HILOGW(FEAT_BRIGHTNESS, "Callback is nullptr");
133         return;
134     }
135     mCurrentStep++;
136     if (mCurrentStep == 1) {
137         mCallback->OnStart();
138     }
139     if (mCurrentStep <= mTotalSteps) {
140         uint32_t nextBrightness = mCurrentBrightness + static_cast<uint32_t>(mStride);
141         bool isOutRange = (mStride > 0 ? (nextBrightness >= mToBrightness) : (nextBrightness <= mToBrightness));
142         if (isOutRange) {
143             DISPLAY_HILOGI(FEAT_BRIGHTNESS, "next step brightness is out range, brightness=%{public}u",
144                 nextBrightness);
145             mCurrentBrightness = mToBrightness.load();
146             mCallback->OnChanged(mCurrentBrightness);
147             mCallback->OnEnd();
148             mDimming = false;
149         } else {
150             DISPLAY_HILOGD(FEAT_BRIGHTNESS, "next step last mCurrentBrightness=%{public}u, next=%{public}d",
151                 mCurrentBrightness.load(), nextBrightness);
152             mCurrentBrightness = nextBrightness;
153             mCallback->OnChanged(mCurrentBrightness);
154             FFRTTask task = [this] { this->NextStep(); };
155             g_animatorTaskHandle = FFRTUtils::SubmitDelayTask(task, mUpdateTime, mQueue);
156         }
157     } else {
158         DISPLAY_HILOGD(FEAT_BRIGHTNESS, "next step last mCurrentBrightness=%{public}u, mToBrightness=%{public}u",
159             mCurrentBrightness.load(), mToBrightness.load());
160         mCurrentBrightness = mToBrightness.load();
161         mCallback->OnChanged(mCurrentBrightness);
162         mCallback->OnEnd();
163         mDimming = false;
164     }
165     DISPLAY_HILOGD(FEAT_BRIGHTNESS, "animating next step, step=%{public}u, brightness=%{public}u, stride=%{public}d",
166         mCurrentStep.load(), mCurrentBrightness.load(), mStride.load());
167 }
168 } // namespace DisplayPowerMgr
169 } // namespace OHOS
170