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 
24 using android::hardware::graphics::composer::PresentListener;
25 using android::hardware::graphics::composer::VariableRefreshRateController;
26 using android::hardware::graphics::composer::VsyncListener;
27 using namespace displaycolor;
28 
29 class ExynosPrimaryDisplay : public ExynosDisplay {
30     public:
31         /* Methods */
32         ExynosPrimaryDisplay(uint32_t index, ExynosDevice* device, const std::string& displayName);
33         ~ExynosPrimaryDisplay();
34         virtual void setDDIScalerEnable(int width, int height);
35         virtual int getDDIScalerMode(int width, int height);
36         virtual int32_t SetCurrentPanelGammaSource(const displaycolor::DisplayType type,
37                                                    const PanelGammaSource& source) override;
GetCurrentPanelGammaSource()38         virtual PanelGammaSource GetCurrentPanelGammaSource() const override {
39             return currentPanelGammaSource;
40         }
41 
42         virtual bool isLhbmSupported();
43         virtual int32_t setLhbmState(bool enabled);
44 
45         virtual bool getLhbmState();
46         virtual void setEarlyWakeupDisplay();
47         virtual void setExpectedPresentTime(uint64_t timestamp, int frameIntervalNs) override;
48         virtual uint64_t getPendingExpectedPresentTime() override;
49         virtual int getPendingFrameInterval() override;
50         virtual void applyExpectedPresentTime();
51         virtual int32_t setDisplayIdleTimer(const int32_t timeoutMs) override;
52         virtual void handleDisplayIdleEnter(const uint32_t idleTeRefreshRate) override;
53 
54         virtual void initDisplayInterface(uint32_t interfaceType);
55         virtual int32_t doDisplayConfigInternal(hwc2_config_t config) override;
56 
57         virtual int32_t setMinIdleRefreshRate(const int fps,
58                                               const RrThrottleRequester requester) override;
59         virtual int32_t setRefreshRateThrottleNanos(const int64_t delayNs,
60                                                     const RrThrottleRequester requester) override;
61         virtual bool isDbmSupported() override;
62         virtual int32_t setDbmState(bool enabled) override;
63 
64         virtual void dump(String8& result) override;
65         virtual void updateAppliedActiveConfig(const hwc2_config_t newConfig,
66                                                const int64_t ts) override;
67         virtual void checkBtsReassignResource(const int32_t vsyncPeriod,
68                                               const int32_t btsVsyncPeriod) override;
69 
70         virtual int32_t setBootDisplayConfig(int32_t config) override;
71         virtual int32_t clearBootDisplayConfig() override;
72         virtual int32_t getPreferredDisplayConfigInternal(int32_t* outConfig) override;
73         virtual bool isConfigSettingEnabled() override;
74         virtual void enableConfigSetting(bool en) override;
75 
76         virtual int32_t getDisplayConfigs(uint32_t* outNumConfigs,
77                                           hwc2_config_t* outConfigs) override;
78         virtual int32_t presentDisplay(int32_t* outRetireFence) override;
79 
80         virtual void onVsync(int64_t timestamp) override;
81 
82         virtual int32_t setFixedTe2Rate(const int rateHz) override;
83 
84         const std::string& getPanelName() final;
85 
86         int32_t notifyExpectedPresent(int64_t timestamp, int32_t frameIntervalNs) override;
87 
88         int32_t setPresentTimeoutParameters(
89                 int timeoutNs, const std::vector<std::pair<uint32_t, uint32_t>>& settings) override;
90 
91         int32_t setPresentTimeoutController(uint32_t controllerType) override;
92 
93         int32_t registerRefreshRateChangeListener(
94                 std::shared_ptr<RefreshRateChangeListener> listener) override;
95 
96         virtual int32_t setRefreshRateChangedCallbackDebugEnabled(bool enabled) final;
97 
98     protected:
99         /* setPowerMode(int32_t mode)
100          * Descriptor: HWC2_FUNCTION_SET_POWER_MODE
101          * Parameters:
102          *   mode - hwc2_power_mode_t and ext_hwc2_power_mode_t
103          *
104          * Returns HWC2_ERROR_NONE or the following error:
105          *   HWC2_ERROR_UNSUPPORTED when DOZE mode not support
106          */
107         virtual int32_t setPowerMode(int32_t mode) override;
108         virtual bool getHDRException(ExynosLayer* __unused layer);
109         virtual int32_t setActiveConfigInternal(hwc2_config_t config, bool force) override;
110         virtual int32_t getActiveConfigInternal(hwc2_config_t* outConfig) override;
111 
112     public:
113         // Prepare multi resolution
114         ResolutionInfo mResolutionInfo;
getPanelSysfsPath()115         std::string getPanelSysfsPath() const override {
116             return getPanelSysfsPath(getDcDisplayType());
117         }
118         std::string getPanelSysfsPath(const displaycolor::DisplayType& type) const;
119 
isVrrSupported()120         virtual bool isVrrSupported() const override { return mXrrSettings.versionInfo.isVrr(); }
121 
122         uint32_t mRcdId = -1;
123 
124     private:
125         static constexpr const char* kDisplayCalFilePath = "/mnt/vendor/persist/display/";
126         static constexpr const char* kPanelGammaCalFilePrefix = "gamma_calib_data";
127         enum PanelGammaSource currentPanelGammaSource = PanelGammaSource::GAMMA_DEFAULT;
128 
129         bool checkLhbmMode(bool status, nsecs_t timoutNs);
130         void setLHBMRefreshRateThrottle(const uint32_t delayMs);
131 
132         bool mFirstPowerOn = true;
133         bool mNotifyPowerOn = false;
134         std::mutex mPowerModeMutex;
135         std::condition_variable mPowerOnCondition;
136 
137         int32_t applyPendingConfig();
138         int32_t setPowerOn();
139         int32_t setPowerOff();
140         int32_t setPowerDoze(hwc2_power_mode_t mode);
141         void firstPowerOn();
142         int32_t setDisplayIdleTimerEnabled(const bool enabled);
143         int32_t getDisplayIdleTimerEnabled(bool& enabled);
144         void setDisplayNeedHandleIdleExit(const bool needed, const bool force);
145         int32_t setDisplayIdleDelayNanos(int32_t delayNanos,
146                                          const DispIdleTimerRequester requester);
147         void initDisplayHandleIdleExit();
148         int32_t setLhbmDisplayConfigLocked(uint32_t peakRate);
149         void restoreLhbmDisplayConfigLocked();
150 
151         void onConfigChange(int configId);
152 
153         // LHBM
154         FILE* mLhbmFd;
155         std::atomic<bool> mLhbmOn;
156         int32_t mFramesToReachLhbmPeakBrightness;
157         bool mConfigSettingDisabled = false;
158         int64_t mConfigSettingDisabledTimestamp = 0;
159         // timeout value of waiting for peak refresh rate
160         static constexpr uint32_t kLhbmWaitForPeakRefreshRateMs = 100U;
161         static constexpr uint32_t kLhbmRefreshRateThrottleMs = 1000U;
162         static constexpr uint32_t kConfigDisablingMaxDurationMs = 1000U;
163         static constexpr uint32_t kSysfsCheckTimeoutMs = 500U;
164 
getTimestampDeltaMs(int64_t endNs,int64_t beginNs)165         int32_t getTimestampDeltaMs(int64_t endNs, int64_t beginNs) {
166             if (endNs == 0) endNs = systemTime(SYSTEM_TIME_MONOTONIC);
167             return (endNs - beginNs) / 1000000;
168         }
169 
170         FILE* mEarlyWakeupDispFd;
171         static constexpr const char* kWakeupDispFilePath =
172                 "/sys/devices/platform/1c300000.drmdecon/early_wakeup";
173 
174         CtrlValue<std::tuple<int64_t, int>> mExpectedPresentTimeAndInterval;
175 
176         virtual void calculateTimelineLocked(
177                 hwc2_config_t config,
178                 hwc_vsync_period_change_constraints_t* vsyncPeriodChangeConstraints,
179                 hwc_vsync_period_change_timeline_t* outTimeline) override;
180         void recalculateTimelineLocked(int64_t refreshRateDelayNanos);
181 
182         std::map<int, int> mBrightnessBlockingZonesLookupTable;
183 
184         int mMinIdleRefreshRate;
185         const int kMinIdleRefreshRateForDozeMode = 1;
186         int mRrThrottleFps[toUnderlying(RrThrottleRequester::MAX)];
187         std::mutex mMinIdleRefreshRateMutex;
188 
189         std::mutex mIdleRefreshRateThrottleMutex;
190         int64_t mRrThrottleNanos[toUnderlying(RrThrottleRequester::MAX)];
191         int64_t mRefreshRateDelayNanos;
192         int64_t mRrUseDelayNanos;
193         int64_t mLastRefreshRateAppliedNanos;
194         bool mIsRrNeedCheckDelay;
195         hwc2_config_t mAppliedActiveConfig;
196 
197         std::mutex mDisplayIdleDelayMutex;
198         bool mDisplayIdleTimerEnabled;
199         int64_t mDisplayIdleTimerNanos[toUnderlying(DispIdleTimerRequester::MAX)];
200         std::ofstream mDisplayNeedHandleIdleExitOfs;
201         int64_t mDisplayIdleDelayNanos;
202         bool mDisplayNeedHandleIdleExit;
203 
204         // Function and variables related to Vrr.
205         PresentListener* getPresentListener();
206         VsyncListener* getVsyncListener();
207 
208         XrrSettings_t mXrrSettings;
209         std::shared_ptr<VariableRefreshRateController> mVariableRefreshRateController;
210 };
211 
212 #endif
213