1 /*
2  * Copyright (c) 2021-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 DISPLAYMGR_SCREEN_CONTROLLER_H
17 #define DISPLAYMGR_SCREEN_CONTROLLER_H
18 
19 #include <atomic>
20 #include <memory>
21 #include <mutex>
22 #include <cstdint>
23 
24 #include "display_power_info.h"
25 #include "gradual_animator.h"
26 #include "screen_action.h"
27 
28 namespace OHOS {
29 namespace DisplayPowerMgr {
30 class ScreenController {
31 public:
32     ScreenController(uint32_t displayId);
33     virtual ~ScreenController() = default;
34 
35     class AnimateCallbackImpl : public AnimateCallback {
36     public:
37         explicit AnimateCallbackImpl(const std::shared_ptr<ScreenAction>& action,
38             std::function<void(uint32_t)> callback);
39         ~AnimateCallbackImpl() override = default;
40         void OnStart() override;
41         void OnChanged(uint32_t currentValue) override;
42         void OnEnd() override;
43         void DiscountBrightness(double discount) override;
44     private:
45         const std::shared_ptr<ScreenAction>& action_;
46         std::function<void(uint32_t)> callback_;
47         double discount_ {1.0};
48     };
49 
50     DisplayState GetState();
51     DisplayState SetDelayOffState();
52     DisplayState SetOnState();
53     bool UpdateState(DisplayState state, uint32_t reason);
54     bool IsScreenOn();
55 
56     bool SetBrightness(uint32_t value, uint32_t gradualDuration = 0, bool continuous = false);
57     uint32_t GetBrightness();
58     uint32_t GetDeviceBrightness();
59     uint32_t GetCachedSettingBrightness() const;
60 
61     bool DiscountBrightness(double discount, uint32_t gradualDuration = 0);
62     bool OverrideBrightness(uint32_t value, uint32_t gradualDuration = 0);
63     bool RestoreBrightness(uint32_t gradualDuration = 0);
64     bool IsBrightnessOverridden() const;
65 
66     bool BoostBrightness(uint32_t timeoutMs, uint32_t gradualDuration = 0);
67     bool CancelBoostBrightness(uint32_t gradualDuration = 0);
68     bool IsBrightnessBoosted() const;
69 
70     uint32_t GetScreenOnBrightness() const;
71 
72     void RegisterSettingBrightnessObserver();
73     void UnregisterSettingBrightnessObserver();
74     double GetDiscount() const;
75 
76     uint32_t GetAnimationUpdateTime() const;
77     void SetCoordinated(bool coordinated);
78 private:
79     void OnStateChanged(DisplayState state, uint32_t reason);
80 
81     bool CanSetBrightness();
82     bool CanDiscountBrightness();
83     bool CanOverrideBrightness();
84     bool CanBoostBrightness();
85     bool IsNeedSkipNextProc(uint32_t reason);
86     bool UpdateBrightness(uint32_t value, uint32_t gradualDuration = 0, bool updateSetting = false);
87     void SetSettingBrightness(uint32_t value);
88     uint32_t GetSettingBrightness(const std::string& key = SETTING_BRIGHTNESS_KEY) const;
89     void BrightnessSettingUpdateFunc(const std::string& key);
90 
91     static const constexpr char* SETTING_BRIGHTNESS_KEY {"settings.display.screen_brightness_status"};
92     DisplayState state_;
93     std::mutex mutexState_;
94     uint32_t stateChangeReason_ {0};
95     double discount_ {1.0};
96 
97     std::atomic<bool> isBrightnessOverridden_ {false};
98     std::atomic<bool> isBrightnessBoosted_ {false};
99     std::atomic<uint32_t> cachedSettingBrightness_ {102};
100     uint32_t overriddenBrightness_ {102};
101     std::shared_ptr<ScreenAction> action_ {nullptr};
102     std::shared_ptr<AnimateCallback> animateCallback_ {nullptr};
103     std::shared_ptr<GradualAnimator> animator_;
104 };
105 } // namespace DisplayPowerMgr
106 } // namespace OHOS
107 #endif // DISPLAYMGR_SCREEN_CONTROLLER_H
108