• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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_event_handler.h"
25 #include "display_power_info.h"
26 #include "gradual_animator.h"
27 #include "screen_action.h"
28 
29 namespace OHOS {
30 namespace DisplayPowerMgr {
31 class ScreenController {
32 public:
33     ScreenController(uint32_t displayId, const std::shared_ptr<DisplayEventHandler>& handler);
34     virtual ~ScreenController() = default;
35 
36     class AnimateCallbackImpl : public AnimateCallback {
37     public:
38         explicit AnimateCallbackImpl(const std::shared_ptr<ScreenAction>& action,
39             const std::shared_ptr<DisplayEventHandler>& handler);
40         ~AnimateCallbackImpl() override = default;
41         void OnStart() override;
42         void OnChanged(uint32_t currentValue) override;
43         void OnEnd() override;
44         void DiscountBrightness(double discount) override;
45     private:
46         const std::shared_ptr<ScreenAction>& action_;
47         const std::shared_ptr<DisplayEventHandler>& handler_;
48         double discount_ {1.0};
49     };
50 
51     DisplayState GetState();
52     bool UpdateState(DisplayState state, uint32_t reason);
53     bool IsScreenOn();
54 
55     bool SetBrightness(uint32_t value, uint32_t gradualDuration = 0);
56     uint32_t GetBrightness();
57     uint32_t GetDeviceBrightness();
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 private:
76     void OnStateChanged(DisplayState state);
77 
78     bool CanSetBrightness();
79     bool CanDiscountBrightness();
80     bool CanOverrideBrightness();
81     bool CanBoostBrightness();
82     bool UpdateBrightness(uint32_t value, uint32_t gradualDuration = 0, bool updateSetting = false);
83     void SetSettingBrightness(uint32_t value);
84     uint32_t GetSettingBrightness(const std::string& key = SETTING_BRIGHTNESS_KEY) const;
85     void BrightnessSettingUpdateFunc(const std::string& key);
86 
87     static const constexpr char* SETTING_BRIGHTNESS_KEY {"settings.display.screen_brightness_status"};
88     DisplayState state_;
89     std::mutex mutexState_;
90     uint32_t stateChangeReason_ {0};
91     double discount_ {1.0};
92 
93     std::atomic<bool> isBrightnessOverridden_ {false};
94     std::atomic<bool> isBrightnessBoosted_ {false};
95     uint32_t cachedSettingBrightness_ {102};
96     uint32_t overriddenBrightness_ {102};
97     std::shared_ptr<ScreenAction> action_ {nullptr};
98     std::shared_ptr<AnimateCallback> animateCallback_ {nullptr};
99     std::shared_ptr<GradualAnimator> animator_;
100     const std::shared_ptr<DisplayEventHandler>& handler_;
101 };
102 } // namespace DisplayPowerMgr
103 } // namespace OHOS
104 #endif // DISPLAYMGR_SCREEN_CONTROLLER_H