1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef EXYNOS_PRIMARY_DISPLAY_H
17 #define EXYNOS_PRIMARY_DISPLAY_H
18 
19 #include <map>
20 
21 #include "../libdevice/ExynosDisplay.h"
22 #include "../libvrr/VariableRefreshRateController.h"
23 #include <cutils/properties.h>
24 
25 using android::hardware::graphics::composer::RefreshListener;
26 using android::hardware::graphics::composer::VariableRefreshRateController;
27 using android::hardware::graphics::composer::VsyncListener;
28 using namespace displaycolor;
29 
30 class ExynosPrimaryDisplay : public ExynosDisplay {
31     public:
32         /* Methods */
33         ExynosPrimaryDisplay(uint32_t index, ExynosDevice* device, const std::string& displayName);
34         ~ExynosPrimaryDisplay();
35         virtual void setDDIScalerEnable(int width, int height);
36         virtual int getDDIScalerMode(int width, int height);
37         virtual int32_t SetCurrentPanelGammaSource(const displaycolor::DisplayType type,
38                                                    const PanelGammaSource& source) override;
GetCurrentPanelGammaSource()39         virtual PanelGammaSource GetCurrentPanelGammaSource() const override {
40             return currentPanelGammaSource;
41         }
42 
43         virtual bool isLhbmSupported();
44         virtual int32_t setLhbmState(bool enabled);
45 
46         virtual bool getLhbmState();
47         virtual void setEarlyWakeupDisplay();
48         virtual void setExpectedPresentTime(uint64_t timestamp, int frameIntervalNs) override;
49         virtual uint64_t getPendingExpectedPresentTime() override;
50         virtual int getPendingFrameInterval() override;
51         virtual void applyExpectedPresentTime();
52         virtual int32_t setDisplayIdleTimer(const int32_t timeoutMs) override;
53         virtual void handleDisplayIdleEnter(const uint32_t idleTeRefreshRate) override;
54 
55         virtual void initDisplayInterface(uint32_t interfaceType);
56         virtual int32_t doDisplayConfigInternal(hwc2_config_t config) override;
57 
58         virtual int32_t setMinIdleRefreshRate(const int fps,
59                                               const RrThrottleRequester requester) override;
60         virtual int32_t setRefreshRateThrottleNanos(const int64_t delayNs,
61                                                     const RrThrottleRequester requester) override;
62         virtual bool isDbmSupported() override;
63         virtual int32_t setDbmState(bool enabled) override;
64 
65         virtual void dump(String8& result, const std::vector<std::string>& args = {}) override;
66         virtual void updateAppliedActiveConfig(const hwc2_config_t newConfig,
67                                                const int64_t ts) override;
68         virtual void checkBtsReassignResource(const int32_t vsyncPeriod,
69                                               const int32_t btsVsyncPeriod) override;
70 
71         virtual int32_t setBootDisplayConfig(int32_t config) override;
72         virtual int32_t clearBootDisplayConfig() override;
73         virtual int32_t getPreferredDisplayConfigInternal(int32_t* outConfig) override;
74         virtual bool isConfigSettingEnabled() override;
75         virtual void enableConfigSetting(bool en) override;
76 
77         virtual int32_t getDisplayConfigs(uint32_t* outNumConfigs,
78                                           hwc2_config_t* outConfigs) override;
79         virtual int32_t presentDisplay(int32_t* outRetireFence) override;
80 
81         virtual void onVsync(int64_t timestamp) override;
82 
83         virtual int32_t setFixedTe2Rate(const int rateHz) override;
84 
85         virtual void onProximitySensorStateChanged(bool active) override;
86 
87         virtual int32_t setDisplayTemperature(const int temperatue) override;
88 
89         const std::string& getPanelName() final;
90 
91         int32_t notifyExpectedPresent(int64_t timestamp, int32_t frameIntervalNs) override;
92 
93         int32_t setPresentTimeoutParameters(
94                 int timeoutNs, const std::vector<std::pair<uint32_t, uint32_t>>& settings) override;
95 
96         int32_t setPresentTimeoutController(uint32_t controllerType) override;
97 
98         int32_t registerRefreshRateChangeListener(
99                 std::shared_ptr<RefreshRateChangeListener> listener) override;
100 
101         virtual int32_t setRefreshRateChangedCallbackDebugEnabled(bool enabled) final;
102 
103     protected:
104         /* setPowerMode(int32_t mode)
105          * Descriptor: HWC2_FUNCTION_SET_POWER_MODE
106          * Parameters:
107          *   mode - hwc2_power_mode_t and ext_hwc2_power_mode_t
108          *
109          * Returns HWC2_ERROR_NONE or the following error:
110          *   HWC2_ERROR_UNSUPPORTED when DOZE mode not support
111          */
112         virtual int32_t setPowerMode(int32_t mode) override;
113         virtual bool getHDRException(ExynosLayer* __unused layer);
114         virtual int32_t setActiveConfigInternal(hwc2_config_t config, bool force) override;
115         virtual int32_t getActiveConfigInternal(hwc2_config_t* outConfig) override;
116 
117     public:
118         // Prepare multi resolution
119         ResolutionInfo mResolutionInfo;
getPanelSysfsPath()120         std::string getPanelSysfsPath() const override {
121             return getPanelSysfsPath(getDcDisplayType());
122         }
123         std::string getPanelSysfsPath(const displaycolor::DisplayType& type) const;
124 
isVrrSupported()125         virtual bool isVrrSupported() const override { return mXrrSettings.versionInfo.isVrr(); }
126 
127         uint32_t mRcdId = -1;
getDisplayTemperatue()128         uint32_t getDisplayTemperatue() { return mDisplayTemperature; };
129 
130     private:
131         static constexpr const char* kDisplayCalFilePath = "/mnt/vendor/persist/display/";
132         static constexpr const char* kPanelGammaCalFilePrefix = "gamma_calib_data";
133         static constexpr const char* kDisplayTempIntervalSec =
134                 "ro.vendor.display.read_temp_interval";
135         enum PanelGammaSource currentPanelGammaSource = PanelGammaSource::GAMMA_DEFAULT;
136 
137         bool checkLhbmMode(bool status, nsecs_t timoutNs);
138         void setLHBMRefreshRateThrottle(const uint32_t delayMs);
139 
140         bool mFirstPowerOn = true;
141         bool mNotifyPowerOn = false;
142         std::mutex mPowerModeMutex;
143         std::condition_variable mPowerOnCondition;
144 
145         int32_t applyPendingConfig();
146         int32_t setPowerOn();
147         int32_t setPowerOff();
148         int32_t setPowerDoze(hwc2_power_mode_t mode);
149         void firstPowerOn();
150         int32_t setDisplayIdleTimerEnabled(const bool enabled);
151         int32_t getDisplayIdleTimerEnabled(bool& enabled);
152         void setDisplayNeedHandleIdleExit(const bool needed, const bool force);
153         int32_t setDisplayIdleDelayNanos(int32_t delayNanos,
154                                          const DispIdleTimerRequester requester);
155         void initDisplayHandleIdleExit();
156         int32_t setLhbmDisplayConfigLocked(uint32_t peakRate);
157         void restoreLhbmDisplayConfigLocked();
158 
159 
160         // monitor display thermal temperature
161         int32_t getDisplayTemperature();
162         bool initDisplayTempMonitor(const std::string& display);
163         bool isTemperatureMonitorThreadRunning();
164         void checkTemperatureMonitorThread(bool shouldRun);
165         void temperatureMonitorThreadCreate();
166         void* temperatureMonitorThreadLoop();
167         bool mIsDisplayTempMonitorSupported = false;
168         volatile int32_t mTMThreadStatus;
169         std::atomic<bool> mTMLoopStatus;
170         std::condition_variable mTMCondition;
171         std::thread mTMThread;
172         std::mutex mThreadMutex;
173         int32_t mDisplayTempInterval;
174         String8 mDisplayTempSysfsNode;
getPropertyDisplayTemperatureStr(const std::string & display)175         std::string getPropertyDisplayTemperatureStr(const std::string& display) {
176             return "ro.vendor." + display + "." + getPanelName() + ".temperature_path";
177         }
178 
179         void onConfigChange(int configId);
180 
181         // LHBM
182         FILE* mLhbmFd;
183         std::atomic<bool> mLhbmOn;
184         int32_t mFramesToReachLhbmPeakBrightness;
185         bool mConfigSettingDisabled = false;
186         int64_t mConfigSettingDisabledTimestamp = 0;
187         // timeout value of waiting for peak refresh rate
188         static constexpr uint32_t kLhbmWaitForPeakRefreshRateMs = 100U;
189         static constexpr uint32_t kLhbmRefreshRateThrottleMs = 1000U;
190         static constexpr uint32_t kConfigDisablingMaxDurationMs = 1000U;
191         static constexpr uint32_t kSysfsCheckTimeoutMs = 500U;
192 
getTimestampDeltaMs(int64_t endNs,int64_t beginNs)193         int32_t getTimestampDeltaMs(int64_t endNs, int64_t beginNs) {
194             if (endNs == 0) endNs = systemTime(SYSTEM_TIME_MONOTONIC);
195             return (endNs - beginNs) / 1000000;
196         }
197 
198         FILE* mEarlyWakeupDispFd;
199         static constexpr const char* kWakeupDispFilePath =
200                 "/sys/devices/platform/1c300000.drmdecon/early_wakeup";
201 
202         CtrlValue<std::tuple<int64_t, int>> mExpectedPresentTimeAndInterval;
203 
204         virtual void calculateTimelineLocked(
205                 hwc2_config_t config,
206                 hwc_vsync_period_change_constraints_t* vsyncPeriodChangeConstraints,
207                 hwc_vsync_period_change_timeline_t* outTimeline) override;
208         void recalculateTimelineLocked(int64_t refreshRateDelayNanos);
209 
210         std::map<int, int> mBrightnessBlockingZonesLookupTable;
211 
212         int mMinIdleRefreshRate;
213         const int kMinIdleRefreshRateForDozeMode = 1;
214         int mRrThrottleFps[toUnderlying(RrThrottleRequester::MAX)];
215         std::mutex mMinIdleRefreshRateMutex;
216 
217         std::mutex mIdleRefreshRateThrottleMutex;
218         int64_t mRrThrottleNanos[toUnderlying(RrThrottleRequester::MAX)];
219         int64_t mRefreshRateDelayNanos;
220         int64_t mRrUseDelayNanos;
221         int64_t mLastRefreshRateAppliedNanos;
222         bool mIsRrNeedCheckDelay;
223         hwc2_config_t mAppliedActiveConfig;
224 
225         std::mutex mDisplayIdleDelayMutex;
226         bool mDisplayIdleTimerEnabled;
227         int64_t mDisplayIdleTimerNanos[toUnderlying(DispIdleTimerRequester::MAX)];
228         std::ofstream mDisplayNeedHandleIdleExitOfs;
229         int64_t mDisplayIdleDelayNanos;
230         bool mDisplayNeedHandleIdleExit;
231 
232         // Function and variables related to Vrr.
233         RefreshListener* getRefreshListener();
234         VsyncListener* getVsyncListener();
235 
236         XrrSettings_t mXrrSettings;
237         std::shared_ptr<VariableRefreshRateController> mVariableRefreshRateController;
238         uint32_t mDisplayTemperature = UINT_MAX;
239 };
240 
241 #endif
242