1595d5899Sopenharmony_ci/*
2595d5899Sopenharmony_ci * Copyright (c) 2023-2023 Huawei Device Co., Ltd.
3595d5899Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4595d5899Sopenharmony_ci * you may not use this file except in compliance with the License.
5595d5899Sopenharmony_ci * You may obtain a copy of the License at
6595d5899Sopenharmony_ci *
7595d5899Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8595d5899Sopenharmony_ci *
9595d5899Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10595d5899Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11595d5899Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12595d5899Sopenharmony_ci * See the License for the specific language governing permissions and
13595d5899Sopenharmony_ci * limitations under the License.
14595d5899Sopenharmony_ci */
15595d5899Sopenharmony_ci
16595d5899Sopenharmony_ci#ifndef BRIGHTNESS_CONTROLLER_H
17595d5899Sopenharmony_ci#define BRIGHTNESS_CONTROLLER_H
18595d5899Sopenharmony_ci
19595d5899Sopenharmony_ci#include <atomic>
20595d5899Sopenharmony_ci#include <cstdint>
21595d5899Sopenharmony_ci#include <functional>
22595d5899Sopenharmony_ci#include <map>
23595d5899Sopenharmony_ci#include <memory>
24595d5899Sopenharmony_ci#include <mutex>
25595d5899Sopenharmony_ci#include <string>
26595d5899Sopenharmony_ci#include <vector>
27595d5899Sopenharmony_ci
28595d5899Sopenharmony_ci#include "brightness_action.h"
29595d5899Sopenharmony_ci#include "brightness_dimming.h"
30595d5899Sopenharmony_ci#include "brightness_base.h"
31595d5899Sopenharmony_ci#include "brightness_param_helper.h"
32595d5899Sopenharmony_ci#include "calculation_manager.h"
33595d5899Sopenharmony_ci#include "display_common.h"
34595d5899Sopenharmony_ci#include "display_power_info.h"
35595d5899Sopenharmony_ci#include "display_manager_lite.h"
36595d5899Sopenharmony_ci#include "dm_common.h"
37595d5899Sopenharmony_ci#include "iremote_object.h"
38595d5899Sopenharmony_ci#include "idisplay_brightness_callback.h"
39595d5899Sopenharmony_ci#include "light_lux_manager.h"
40595d5899Sopenharmony_ci#include "refbase.h"
41595d5899Sopenharmony_ci#include "ffrt_utils.h"
42595d5899Sopenharmony_ci#ifdef ENABLE_SENSOR_PART
43595d5899Sopenharmony_ci#include "sensor_agent_type.h"
44595d5899Sopenharmony_ci#endif
45595d5899Sopenharmony_ci
46595d5899Sopenharmony_ci#include <sys/types.h>
47595d5899Sopenharmony_ci
48595d5899Sopenharmony_cinamespace OHOS {
49595d5899Sopenharmony_cinamespace DisplayPowerMgr {
50595d5899Sopenharmony_ciclass BrightnessService {
51595d5899Sopenharmony_cipublic:
52595d5899Sopenharmony_ci    class DimmingCallbackImpl : public BrightnessDimmingCallback {
53595d5899Sopenharmony_ci    public:
54595d5899Sopenharmony_ci        DimmingCallbackImpl(const std::shared_ptr<BrightnessAction>& action,
55595d5899Sopenharmony_ci            std::function<void(uint32_t)> callback);
56595d5899Sopenharmony_ci        ~DimmingCallbackImpl() override = default;
57595d5899Sopenharmony_ci        DimmingCallbackImpl(const DimmingCallbackImpl&) = delete;
58595d5899Sopenharmony_ci        DimmingCallbackImpl& operator=(const DimmingCallbackImpl&) = delete;
59595d5899Sopenharmony_ci        DimmingCallbackImpl(DimmingCallbackImpl&&) = delete;
60595d5899Sopenharmony_ci        DimmingCallbackImpl& operator=(DimmingCallbackImpl&&) = delete;
61595d5899Sopenharmony_ci
62595d5899Sopenharmony_ci        void OnStart() override;
63595d5899Sopenharmony_ci        void OnChanged(uint32_t currentValue) override;
64595d5899Sopenharmony_ci        void OnEnd() override;
65595d5899Sopenharmony_ci        void DiscountBrightness(double discount) override;
66595d5899Sopenharmony_ci
67595d5899Sopenharmony_ci    private:
68595d5899Sopenharmony_ci        const std::shared_ptr<BrightnessAction> mAction{};
69595d5899Sopenharmony_ci        std::function<void(uint32_t)> mCallback{};
70595d5899Sopenharmony_ci        double mDiscount{1.0};
71595d5899Sopenharmony_ci    };
72595d5899Sopenharmony_ci
73595d5899Sopenharmony_ci    class FoldStatusLisener : public Rosen::DisplayManagerLite::IFoldStatusListener {
74595d5899Sopenharmony_ci    public:
75595d5899Sopenharmony_ci        FoldStatusLisener() = default;
76595d5899Sopenharmony_ci        virtual ~FoldStatusLisener() = default;
77595d5899Sopenharmony_ci
78595d5899Sopenharmony_ci        FoldStatusLisener(const FoldStatusLisener&) = delete;
79595d5899Sopenharmony_ci        FoldStatusLisener& operator=(const FoldStatusLisener&) = delete;
80595d5899Sopenharmony_ci        FoldStatusLisener(FoldStatusLisener&&) = delete;
81595d5899Sopenharmony_ci        FoldStatusLisener& operator=(FoldStatusLisener&&) = delete;
82595d5899Sopenharmony_ci
83595d5899Sopenharmony_ci        /**
84595d5899Sopenharmony_ci        * @param FoldStatus uint32_t; UNKNOWN = 0, EXPAND = 1,  FOLDED = 2,  HALF_FOLD = 3;
85595d5899Sopenharmony_ci        */
86595d5899Sopenharmony_ci        void OnFoldStatusChanged(Rosen::FoldStatus foldStatus) override;
87595d5899Sopenharmony_ci
88595d5899Sopenharmony_ci    private:
89595d5899Sopenharmony_ci        Rosen::FoldStatus mLastFoldStatus = Rosen::FoldStatus::UNKNOWN;
90595d5899Sopenharmony_ci    };
91595d5899Sopenharmony_ci
92595d5899Sopenharmony_ci    static constexpr const char* SETTING_AUTO_ADJUST_BRIGHTNESS_KEY {"settings.display.auto_screen_brightness"};
93595d5899Sopenharmony_ci    static const int LUX_LEVEL_LENGTH = 23;
94595d5899Sopenharmony_ci
95595d5899Sopenharmony_ci    BrightnessService(const BrightnessService&) = delete;
96595d5899Sopenharmony_ci    BrightnessService& operator=(const BrightnessService&) = delete;
97595d5899Sopenharmony_ci    BrightnessService(BrightnessService&&) = delete;
98595d5899Sopenharmony_ci    BrightnessService& operator=(BrightnessService&&) = delete;
99595d5899Sopenharmony_ci
100595d5899Sopenharmony_ci    static BrightnessService& Get();
101595d5899Sopenharmony_ci    static uint32_t GetMappingBrightnessLevel(uint32_t level);
102595d5899Sopenharmony_ci    static uint32_t GetOrigBrightnessLevel(uint32_t level);
103595d5899Sopenharmony_ci    static void SetSettingAutoBrightness(bool enable);
104595d5899Sopenharmony_ci    static bool GetSettingAutoBrightness(const std::string& key = SETTING_AUTO_ADJUST_BRIGHTNESS_KEY);
105595d5899Sopenharmony_ci
106595d5899Sopenharmony_ci    void Init(uint32_t defaultMax, uint32_t defaultMin);
107595d5899Sopenharmony_ci    void DeInit();
108595d5899Sopenharmony_ci    void SetDisplayState(uint32_t id, DisplayState state);
109595d5899Sopenharmony_ci    DisplayState GetDisplayState();
110595d5899Sopenharmony_ci    bool IsScreenOnState(DisplayState state);
111595d5899Sopenharmony_ci    bool AutoAdjustBrightness(bool enable);
112595d5899Sopenharmony_ci    bool StateChangedSetAutoBrightness(bool enable);
113595d5899Sopenharmony_ci    bool IsAutoAdjustBrightness();
114595d5899Sopenharmony_ci    void ProcessLightLux(float lux);
115595d5899Sopenharmony_ci    void RegisterSettingBrightnessObserver();
116595d5899Sopenharmony_ci    void UnregisterSettingBrightnessObserver();
117595d5899Sopenharmony_ci    uint32_t GetBrightness();
118595d5899Sopenharmony_ci    uint32_t GetDeviceBrightness();
119595d5899Sopenharmony_ci    uint32_t GetCachedSettingBrightness();
120595d5899Sopenharmony_ci    uint32_t GetScreenOnBrightness(bool isUpdateTarget);
121595d5899Sopenharmony_ci    uint32_t GetBrightnessLevel(float lux);
122595d5899Sopenharmony_ci    uint32_t GetBrightnessHighLevel(uint32_t level);
123595d5899Sopenharmony_ci    uint32_t GetMappingBrightnessNit(uint32_t level);
124595d5899Sopenharmony_ci    uint32_t GetBrightnessLevelFromNit(uint32_t nit);
125595d5899Sopenharmony_ci    uint32_t GetMappingHighBrightnessLevel(uint32_t level);
126595d5899Sopenharmony_ci    bool SetBrightness(uint32_t value, uint32_t gradualDuration = 0, bool continuous = false);
127595d5899Sopenharmony_ci    void SetScreenOnBrightness();
128595d5899Sopenharmony_ci    bool OverrideBrightness(uint32_t value, uint32_t gradualDuration = 0);
129595d5899Sopenharmony_ci    bool RestoreBrightness(uint32_t gradualDuration = 0);
130595d5899Sopenharmony_ci    bool IsBrightnessOverridden();
131595d5899Sopenharmony_ci    bool BoostBrightness(uint32_t timeoutMs, uint32_t gradualDuration = 0);
132595d5899Sopenharmony_ci    bool CancelBoostBrightness(uint32_t gradualDuration = 0);
133595d5899Sopenharmony_ci    bool IsBrightnessBoosted();
134595d5899Sopenharmony_ci    bool DiscountBrightness(double discount, uint32_t gradualDuration = 0);
135595d5899Sopenharmony_ci    double GetDiscount() const;
136595d5899Sopenharmony_ci    uint32_t GetDimmingUpdateTime() const;
137595d5899Sopenharmony_ci    void ClearOffset();
138595d5899Sopenharmony_ci    void UpdateBrightnessSceneMode(BrightnessSceneMode mode);
139595d5899Sopenharmony_ci    uint32_t GetDisplayId();
140595d5899Sopenharmony_ci    void SetDisplayId(uint32_t displayId);
141595d5899Sopenharmony_ci    uint32_t SetLightBrightnessThreshold(std::vector<int32_t> threshold, sptr<IDisplayBrightnessCallback> callback);
142595d5899Sopenharmony_ci    uint32_t GetCurrentDisplayId(uint32_t defaultId);
143595d5899Sopenharmony_ci    bool IsDimming();
144595d5899Sopenharmony_ci    void ReportBrightnessBigData(uint32_t brightness);
145595d5899Sopenharmony_ci    bool IsSleepStatus();
146595d5899Sopenharmony_ci    void SetSleepBrightness();
147595d5899Sopenharmony_ci    int GetDisplayIdWithFoldstatus(Rosen::FoldStatus foldStatus);
148595d5899Sopenharmony_ci    int GetSensorIdWithFoldstatus(Rosen::FoldStatus foldStatus);
149595d5899Sopenharmony_ci    int GetDisplayIdWithDisplayMode(Rosen::FoldDisplayMode mode);
150595d5899Sopenharmony_ci    int GetSensorIdWithDisplayMode(Rosen::FoldDisplayMode mode);
151595d5899Sopenharmony_ci    uint32_t GetCurrentSensorId();
152595d5899Sopenharmony_ci    void SetCurrentSensorId(uint32_t sensorId);
153595d5899Sopenharmony_ci
154595d5899Sopenharmony_ci    static uint32_t GetSafeBrightness(uint32_t value);
155595d5899Sopenharmony_ci    bool SetMaxBrightness(double value);
156595d5899Sopenharmony_ci    bool SetMaxBrightnessNit(uint32_t maxNit);
157595d5899Sopenharmony_ciprivate:
158595d5899Sopenharmony_ci    static const constexpr char* SETTING_BRIGHTNESS_KEY{"settings.display.screen_brightness_status"};
159595d5899Sopenharmony_ci    static const uint32_t SAMPLING_RATE = 100000000;
160595d5899Sopenharmony_ci    static constexpr uint32_t DEFAULT_DISPLAY_ID = 0;
161595d5899Sopenharmony_ci    static constexpr uint32_t SECOND_DISPLAY_ID = 1;
162595d5899Sopenharmony_ci    static constexpr uint32_t DEFAULT_BRIGHTNESS = 50;
163595d5899Sopenharmony_ci    static constexpr const double DISCOUNT_MIN = 0.01;
164595d5899Sopenharmony_ci    static constexpr const double DISCOUNT_MAX = 1.00;
165595d5899Sopenharmony_ci    static const uint32_t AMBIENT_LUX_LEVELS[LUX_LEVEL_LENGTH];
166595d5899Sopenharmony_ci    static const uint32_t WAIT_FOR_FIRST_LUX_MAX_TIME = 200;
167595d5899Sopenharmony_ci    static const uint32_t WAIT_FOR_FIRST_LUX_STEP = 10;
168595d5899Sopenharmony_ci    static uint32_t brightnessValueMin;
169595d5899Sopenharmony_ci    static uint32_t brightnessValueMax;
170595d5899Sopenharmony_ci
171595d5899Sopenharmony_ci    BrightnessService();
172595d5899Sopenharmony_ci    virtual ~BrightnessService() = default;
173595d5899Sopenharmony_ci
174595d5899Sopenharmony_ci    static uint32_t GetSettingBrightness(const std::string& key = SETTING_BRIGHTNESS_KEY);
175595d5899Sopenharmony_ci    bool mIsLuxActiveWithLog{true};
176595d5899Sopenharmony_ci#ifdef ENABLE_SENSOR_PART
177595d5899Sopenharmony_ci    static void AmbientLightCallback(SensorEvent* event);
178595d5899Sopenharmony_ci    void InitSensors();
179595d5899Sopenharmony_ci    void ActivateAmbientSensor();
180595d5899Sopenharmony_ci    void DeactivateAmbientSensor();
181595d5899Sopenharmony_ci    void ActivateAmbientSensor1();
182595d5899Sopenharmony_ci    void DeactivateAmbientSensor1();
183595d5899Sopenharmony_ci    void ActivateValidAmbientSensor();
184595d5899Sopenharmony_ci    void DeactivateValidAmbientSensor();
185595d5899Sopenharmony_ci    void DeactivateAllAmbientSensor();
186595d5899Sopenharmony_ci    bool mIsSupportLightSensor{false};
187595d5899Sopenharmony_ci    SensorUser mSensorUser{};
188595d5899Sopenharmony_ci    SensorUser mSensorUser1{};
189595d5899Sopenharmony_ci#endif
190595d5899Sopenharmony_ci    bool mIsLightSensorEnabled{false};
191595d5899Sopenharmony_ci    bool mIsLightSensor1Enabled{false};
192595d5899Sopenharmony_ci
193595d5899Sopenharmony_ci    void UpdateCurrentBrightnessLevel(float lux, bool isFastDuration);
194595d5899Sopenharmony_ci    void SetBrightnessLevel(uint32_t value, uint32_t duration);
195595d5899Sopenharmony_ci    bool IsScreenOn();
196595d5899Sopenharmony_ci    bool CanSetBrightness();
197595d5899Sopenharmony_ci    bool CanDiscountBrightness();
198595d5899Sopenharmony_ci    bool CanOverrideBrightness();
199595d5899Sopenharmony_ci    bool CanBoostBrightness();
200595d5899Sopenharmony_ci    bool UpdateBrightness(uint32_t value, uint32_t gradualDuration = 0, bool updateSetting = false);
201595d5899Sopenharmony_ci    void SetSettingBrightness(uint32_t value);
202595d5899Sopenharmony_ci    void UpdateBrightnessSettingFunc(const std::string& key);
203595d5899Sopenharmony_ci    void RegisterFoldStatusListener();
204595d5899Sopenharmony_ci    void UnRegisterFoldStatusListener();
205595d5899Sopenharmony_ci    std::string GetReason();
206595d5899Sopenharmony_ci    void NotifyLightChangeToAps(uint32_t type, float value);
207595d5899Sopenharmony_ci    bool GetIsSupportLightSensor();
208595d5899Sopenharmony_ci    bool IsCurrentSensorEnable();
209595d5899Sopenharmony_ci
210595d5899Sopenharmony_ci    bool mIsFoldDevice{false};
211595d5899Sopenharmony_ci    bool mIsAutoBrightnessEnabled{false};
212595d5899Sopenharmony_ci    DisplayState mState{DisplayState::DISPLAY_UNKNOWN};
213595d5899Sopenharmony_ci    uint32_t mBrightnessLevel{0};
214595d5899Sopenharmony_ci    std::atomic<uint32_t> mBrightnessTarget{0};
215595d5899Sopenharmony_ci    uint32_t mDisplayId{0};
216595d5899Sopenharmony_ci    uint32_t mCurrentSensorId{5};
217595d5899Sopenharmony_ci    int mLuxLevel{-1};
218595d5899Sopenharmony_ci    double mDiscount{1.0f};
219595d5899Sopenharmony_ci    std::atomic<bool> mIsBrightnessOverridden{false};
220595d5899Sopenharmony_ci    std::atomic<bool> mIsBrightnessBoosted{false};
221595d5899Sopenharmony_ci    uint32_t mCachedSettingBrightness{DEFAULT_BRIGHTNESS};
222595d5899Sopenharmony_ci    uint32_t mOverriddenBrightness{DEFAULT_BRIGHTNESS};
223595d5899Sopenharmony_ci    uint32_t mBeforeOverriddenBrightness{DEFAULT_BRIGHTNESS};
224595d5899Sopenharmony_ci    std::shared_ptr<BrightnessAction> mAction{nullptr};
225595d5899Sopenharmony_ci    std::shared_ptr<BrightnessDimmingCallback> mDimmingCallback{nullptr};
226595d5899Sopenharmony_ci    std::shared_ptr<BrightnessDimming> mDimming;
227595d5899Sopenharmony_ci    LightLuxManager mLightLuxManager{};
228595d5899Sopenharmony_ci    BrightnessCalculationManager mBrightnessCalculationManager{};
229595d5899Sopenharmony_ci    sptr<Rosen::DisplayManagerLite::IFoldStatusListener> mFoldStatusistener;
230595d5899Sopenharmony_ci    std::shared_ptr<PowerMgr::FFRTQueue> queue_;
231595d5899Sopenharmony_ci    bool mIsUserMode{false};
232595d5899Sopenharmony_ci    std::atomic<bool> mIsSleepStatus{false};
233595d5899Sopenharmony_ci    std::vector<int32_t> mLightBrightnessThreshold;
234595d5899Sopenharmony_ci    sptr<IDisplayBrightnessCallback> mApsListenLightChangeCallback = nullptr;
235595d5899Sopenharmony_ci    bool mIsBrightnessValidate = false;
236595d5899Sopenharmony_ci    bool mIsLightValidate = false;
237595d5899Sopenharmony_ci    time_t mLastCallApsTime {0};
238595d5899Sopenharmony_ci    std::atomic<bool> mIsDisplayOnWhenFirstLuxReport{false};
239595d5899Sopenharmony_ci    std::atomic<bool> mWaitForFirstLux{false};
240595d5899Sopenharmony_ci    std::atomic<uint32_t> mCurrentBrightness{DEFAULT_BRIGHTNESS};
241595d5899Sopenharmony_ci};
242595d5899Sopenharmony_ci} // namespace DisplayPowerMgr
243595d5899Sopenharmony_ci} // namespace OHOS
244595d5899Sopenharmony_ci#endif // BRIGHTNESS_CONTROLLER_H