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 <mutex> 20 #include <system_ability.h> 21 #include <system_ability_definition.h> 22 23 #include "delayed_sp_singleton.h" 24 #include "display_common.h" 25 #include "display_power_mgr_stub.h" 26 #include "screen_controller.h" 27 #include "sensor_agent.h" 28 29 namespace OHOS { 30 namespace DisplayPowerMgr { 31 class DisplayPowerMgrService : public DisplayPowerMgrStub { 32 public: 33 virtual ~DisplayPowerMgrService(); 34 virtual bool SetDisplayState(uint32_t id, DisplayState state, uint32_t reason) override; 35 virtual DisplayState GetDisplayState(uint32_t id) override; 36 virtual std::vector<uint32_t> GetDisplayIds() override; 37 virtual uint32_t GetMainDisplayId() override; 38 virtual bool SetBrightness(uint32_t value, uint32_t displayId) override; 39 virtual bool OverrideBrightness(uint32_t value, uint32_t displayId) override; 40 virtual bool RestoreBrightness(uint32_t displayId) override; 41 virtual uint32_t GetBrightness(uint32_t displayId) override; 42 virtual bool AdjustBrightness(uint32_t id, int32_t value, uint32_t duration) override; 43 virtual bool AutoAdjustBrightness(bool enable) override; 44 virtual bool IsAutoAdjustBrightness() override; 45 virtual bool SetStateConfig(uint32_t id, DisplayState state, int32_t value) override; 46 virtual bool RegisterCallback(sptr<IDisplayPowerCallback> callback) override; 47 virtual int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override; 48 void NotifyStateChangeCallback(uint32_t displayId, DisplayState state); 49 50 private: 51 class CallbackDeathRecipient : public IRemoteObject::DeathRecipient { 52 public: 53 CallbackDeathRecipient() = default; 54 virtual ~CallbackDeathRecipient() = default; 55 virtual void OnRemoteDied(const wptr<IRemoteObject>& remote); 56 }; 57 static const uint32_t AUTO_ADJUST_BRIGHTNESS_DURATION = 1000; 58 static const uint32_t SAMPLING_RATE = 100000000; 59 static const int32_t BRIGHTNESS_CHANGE_MIN = 5; 60 static const int32_t LUX_TO_NIT_SQRT_RADIO = 5; 61 static const time_t LUX_STABLE_TIME = 1; 62 static constexpr float LUX_CHANGE_RATE_THRESHOLD = 10; 63 static constexpr float LUX_CHANGE_STABLE_MIN = 100.0; 64 static const int32_t NIT_MIN = 2; 65 static const int32_t NIT_MAX = 450; 66 static const uint32_t BRIGHTNESS_OFF = 0; 67 static const uint32_t BRIGHTNESS_MIN = 1; 68 static const uint32_t BRIGHTNESS_MAX = 255; 69 static void AmbientLightCallback(SensorEvent *event); 70 71 friend DelayedSpSingleton<DisplayPowerMgrService>; 72 73 DisplayPowerMgrService(); 74 void InitSensors(); 75 bool IsChangedLux(float scalar); 76 static uint32_t GetSafeBrightness(uint32_t value); 77 bool CalculateBrightness(float scalar, int32_t& brightness); 78 int32_t GetBrightnessFromLightScalar(float scalar); 79 void ActivateAmbientSensor(); 80 void DeactivateAmbientSensor(); 81 82 std::shared_ptr<ScreenAction> action_; 83 84 std::map<uint64_t, std::shared_ptr<ScreenController>> controllerMap_; 85 bool supportLightSensor_ {false}; 86 bool autoBrightness_ {false}; 87 bool ambientSensorEnabled_ {false}; 88 SensorUser user_; 89 sptr<IDisplayPowerCallback> callback_; 90 sptr<CallbackDeathRecipient> cbDeathRecipient_; 91 92 time_t lastLuxTime_ {0}; 93 float lastLux_ {0}; 94 bool luxChanged_ {false}; 95 }; 96 } // namespace DisplayPowerMgr 97 } // namespace OHOS 98 #endif // DISPLAYMGR_DISPLAY_MGR_SERVICE_H 99