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