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