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_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 UpdateState(DisplayState state); 55 bool IsScreenOn(); 56 57 bool SetBrightness(uint32_t value, uint32_t gradualDuration = 0); 58 uint32_t GetBrightness(); 59 uint32_t GetDeviceBrightness(); 60 uint32_t GetCachedSettingBrightness() const; 61 62 bool DiscountBrightness(double discount, uint32_t gradualDuration = 0); 63 bool OverrideBrightness(uint32_t value, uint32_t gradualDuration = 0); 64 bool RestoreBrightness(uint32_t gradualDuration = 0); 65 bool IsBrightnessOverridden() const; 66 67 bool BoostBrightness(uint32_t timeoutMs, uint32_t gradualDuration = 0); 68 bool CancelBoostBrightness(uint32_t gradualDuration = 0); 69 bool IsBrightnessBoosted() const; 70 71 uint32_t GetScreenOnBrightness() const; 72 73 void RegisterSettingBrightnessObserver(); 74 void UnregisterSettingBrightnessObserver(); 75 double GetDiscount() const; 76 77 uint32_t GetAnimationUpdateTime() const; 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 UpdateBrightness(uint32_t value, uint32_t gradualDuration = 0, bool updateSetting = false); 86 void SetSettingBrightness(uint32_t value); 87 uint32_t GetSettingBrightness(const std::string& key = SETTING_BRIGHTNESS_KEY) const; 88 void BrightnessSettingUpdateFunc(const std::string& key); 89 90 static const constexpr char* SETTING_BRIGHTNESS_KEY {"settings.display.screen_brightness_status"}; 91 DisplayState state_; 92 std::mutex mutexState_; 93 uint32_t stateChangeReason_ {0}; 94 double discount_ {1.0}; 95 96 std::atomic<bool> isBrightnessOverridden_ {false}; 97 std::atomic<bool> isBrightnessBoosted_ {false}; 98 std::atomic<uint32_t> cachedSettingBrightness_ {102}; 99 uint32_t overriddenBrightness_ {102}; 100 std::shared_ptr<ScreenAction> action_ {nullptr}; 101 std::shared_ptr<AnimateCallback> animateCallback_ {nullptr}; 102 std::shared_ptr<GradualAnimator> animator_; 103 }; 104 } // namespace DisplayPowerMgr 105 } // namespace OHOS 106 #endif // DISPLAYMGR_SCREEN_CONTROLLER_H