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_DISPLAY_MGR_SERVICE_H 17 #define DISPLAYMGR_DISPLAY_MGR_SERVICE_H 18 19 #include <atomic> 20 #include <cstdint> 21 #include <string> 22 #include <vector> 23 #include <map> 24 #include <memory> 25 #include <functional> 26 #include <sys/types.h> 27 #include "delayed_sp_singleton.h" 28 #include "refbase.h" 29 #include "event_runner.h" 30 #include "iremote_object.h" 31 #include "sensor_agent_type.h" 32 #include "idisplay_power_callback.h" 33 #include "display_power_info.h" 34 #include "display_common.h" 35 #include "display_power_mgr_stub.h" 36 #include "screen_controller.h" 37 38 namespace OHOS { 39 namespace DisplayPowerMgr { 40 class DisplayPowerMgrService : public DisplayPowerMgrStub { 41 public: 42 virtual ~DisplayPowerMgrService() = default; 43 virtual bool SetDisplayState(uint32_t id, DisplayState state, uint32_t reason) override; 44 virtual DisplayState GetDisplayState(uint32_t id) override; 45 virtual std::vector<uint32_t> GetDisplayIds() override; 46 virtual uint32_t GetMainDisplayId() override; 47 virtual bool SetBrightness(uint32_t value, uint32_t displayId) override; 48 virtual bool DiscountBrightness(double discount, uint32_t displayId) override; 49 virtual bool OverrideBrightness(uint32_t value, uint32_t displayId) override; 50 virtual bool OverrideDisplayOffDelay(uint32_t delayMs) override; 51 virtual bool RestoreBrightness(uint32_t displayId) override; 52 virtual uint32_t GetBrightness(uint32_t displayId) override; 53 virtual uint32_t GetDefaultBrightness() override; 54 virtual uint32_t GetMaxBrightness() override; 55 virtual uint32_t GetMinBrightness() override; 56 virtual bool AdjustBrightness(uint32_t id, int32_t value, uint32_t duration) override; 57 virtual bool AutoAdjustBrightness(bool enable) override; 58 virtual bool IsAutoAdjustBrightness() override; 59 virtual bool RegisterCallback(sptr<IDisplayPowerCallback> callback) override; 60 virtual bool BoostBrightness(int32_t timeoutMs, uint32_t displayId) override; 61 virtual bool CancelBoostBrightness(uint32_t displayId) override; 62 virtual uint32_t GetDeviceBrightness(uint32_t displayId) override; 63 virtual int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override; 64 virtual DisplayErrors GetError() override; 65 void NotifyStateChangeCallback(uint32_t displayId, DisplayState state, uint32_t reason); 66 void Init(); 67 void Deinit(); 68 static uint32_t GetSafeBrightness(uint32_t value); 69 static double GetSafeDiscount(double discount, uint32_t brightness); 70 71 private: 72 class CallbackDeathRecipient : public IRemoteObject::DeathRecipient { 73 public: 74 CallbackDeathRecipient() = default; 75 virtual ~CallbackDeathRecipient() = default; 76 virtual void OnRemoteDied(const wptr<IRemoteObject>& remote); 77 private: 78 std::mutex callbackMutex_; 79 }; 80 81 static const uint32_t AUTO_ADJUST_BRIGHTNESS_STRIDE = 1; 82 static const uint32_t SAMPLING_RATE = 100000000; 83 static const int32_t BRIGHTNESS_CHANGE_MIN = 2; 84 static const int32_t LUX_TO_NIT_SQRT_RADIO = 5; 85 static const time_t LUX_STABLE_TIME = 1; 86 static constexpr float LUX_CHANGE_RATE_THRESHOLD = 10; 87 static constexpr float LUX_CHANGE_STABLE_MIN = 10.0; 88 static const int32_t NIT_MIN = 2; 89 static const int32_t NIT_MAX = 450; 90 static const uint32_t BRIGHTNESS_OFF = 0; 91 static const uint32_t BRIGHTNESS_MIN; 92 static const uint32_t BRIGHTNESS_DEFAULT; 93 static const uint32_t BRIGHTNESS_MAX; 94 static const uint32_t DELAY_TIME_UNSET = 0; 95 static constexpr const double DISCOUNT_MIN = 0.01; 96 static constexpr const double DISCOUNT_MAX = 1.00; 97 static void AmbientLightCallback(SensorEvent* event); 98 99 friend DelayedSpSingleton<DisplayPowerMgrService>; 100 101 DisplayPowerMgrService(); 102 void InitSensors(); 103 bool IsChangedLux(float scalar); 104 bool CalculateBrightness(float scalar, int32_t& brightness, int32_t& change); 105 int32_t GetBrightnessFromLightScalar(float scalar); 106 void ActivateAmbientSensor(); 107 void DeactivateAmbientSensor(); 108 static void RegisterBootCompletedCallback(); 109 static void SetBootCompletedBrightness(); 110 static void SetBootCompletedAutoBrightness(); 111 static void RegisterSettingObservers(); 112 static void UnregisterSettingObservers(); 113 static void RegisterSettingAutoBrightnessObserver(); 114 static void UnregisterSettingAutoBrightnessObserver(); 115 static void AutoBrightnessSettingUpdateFunc(const std::string& key); 116 static void SetSettingAutoBrightness(bool enable); 117 static bool GetSettingAutoBrightness(const std::string& key = SETTING_AUTO_ADJUST_BRIGHTNESS_KEY); 118 void ScreenOffDelay(uint32_t id, DisplayState state, uint32_t reason); 119 120 static constexpr const char* SETTING_AUTO_ADJUST_BRIGHTNESS_KEY {"settings.display.auto_screen_brightness"}; 121 std::map<uint64_t, std::shared_ptr<ScreenController>> controllerMap_; 122 bool supportLightSensor_ {false}; 123 bool autoBrightness_ {false}; 124 bool ambientSensorEnabled_ {false}; 125 SensorUser sensorUser_ {}; 126 sptr<IDisplayPowerCallback> callback_; 127 sptr<CallbackDeathRecipient> cbDeathRecipient_; 128 129 DisplayErrors lastError_ {DisplayErrors::ERR_OK}; 130 time_t lastLuxTime_ {0}; 131 float lastLux_ {0}; 132 bool luxChanged_ {false}; 133 std::mutex mutex_; 134 static std::atomic_bool isBootCompleted_; 135 uint32_t displayOffDelayMs_ {0}; 136 bool isDisplayDelayOff_ = false; 137 bool setDisplayStateRet_ = true; 138 uint32_t displayId_ {0}; 139 DisplayState displayState_ {DisplayState::DISPLAY_UNKNOWN}; 140 DisplayState tempState_ {DisplayState::DISPLAY_UNKNOWN}; 141 uint32_t displayReason_ {0}; 142 }; 143 } // namespace DisplayPowerMgr 144 } // namespace OHOS 145 #endif // DISPLAYMGR_DISPLAY_MGR_SERVICE_H 146