1 /* 2 * Copyright (C) 2023 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 17 #pragma once 18 19 #include <utils/Mutex.h> 20 #include <condition_variable> 21 #include <list> 22 #include <map> 23 #include <optional> 24 #include <queue> 25 #include <thread> 26 27 #include "../libdevice/ExynosDisplay.h" 28 #include "../libdevice/ExynosLayer.h" 29 #include "EventQueue.h" 30 #include "ExternalEventHandlerLoader.h" 31 #include "FileNode.h" 32 #include "Power/DisplayStateResidencyWatcher.h" 33 #include "RefreshRateCalculator/RefreshRateCalculator.h" 34 #include "RingBuffer.h" 35 #include "Statistics/VariableRefreshRateStatistic.h" 36 #include "Utils.h" 37 #include "display/common/DisplayConfigurationOwner.h" 38 #include "interface/DisplayContextProvider.h" 39 #include "interface/VariableRefreshRateInterface.h" 40 41 namespace android::hardware::graphics::composer { 42 43 class VariableRefreshRateController : public VsyncListener, 44 public RefreshListener, 45 public DisplayContextProvider, 46 public DisplayConfigurationsOwner { 47 public: 48 ~VariableRefreshRateController(); 49 50 auto static CreateInstance(ExynosDisplay* display, const std::string& panelName) 51 -> std::shared_ptr<VariableRefreshRateController>; 52 getCurrentDisplayConfiguration()53 const displayConfigs_t* getCurrentDisplayConfiguration() const override { 54 auto configs = mDisplayContextProvider->getDisplayConfigs(); 55 if (configs) { 56 const auto& it = configs->find(mVrrActiveConfig); 57 if (it != configs->end()) { 58 return &(it->second); 59 } 60 } 61 return nullptr; 62 } 63 enableRefreshRateCalculator(bool enabled)64 void enableRefreshRateCalculator(bool enabled) { 65 const std::lock_guard<std::mutex> lock(mMutex); 66 67 if (mRefreshRateCalculator) { 68 mRefreshRateCalculatorEnabled = enabled; 69 if (mRefreshRateCalculatorEnabled) { 70 if (mDisplay->isVrrSupported()) { 71 reportRefreshRateIndicator(); 72 } else { 73 // This is a VTS hack for MRRv2 74 mDisplay->mDevice->onRefreshRateChangedDebug(mDisplay->mDisplayId, 75 freqToDurationNs( 76 mLastRefreshRate)); 77 } 78 } 79 } 80 }; 81 82 int notifyExpectedPresent(int64_t timestamp, int32_t frameIntervalNs); 83 84 // Clear historical record data. 85 void reset(); 86 87 // After setting the active Vrr configuration, we will automatically transition into the 88 // rendering state and post the timeout event. 89 void setActiveVrrConfiguration(hwc2_config_t config); 90 91 void setEnable(bool isEnabled); 92 93 // |preSetPowerMode| is called before the power mode is configured. 94 void preSetPowerMode(int32_t mode); 95 //|postSetPowerMode| is called after the setting to new power mode has been done. 96 void postSetPowerMode(int32_t mode); 97 98 void setVrrConfigurations(std::unordered_map<hwc2_config_t, VrrConfig_t> configs); 99 100 // Inherit from DisplayContextProvider. 101 int getAmbientLightSensorOutput() const override; 102 BrightnessMode getBrightnessMode() const override; 103 int getBrightnessNits() const override; 104 const char* getDisplayFileNodePath() const override; 105 int getEstimatedVideoFrameRate() const override; 106 OperationSpeedMode getOperationSpeedMode() const override; 107 bool isProximityThrottlingEnabled() const override; 108 getDisplayContextProviderInterface()109 const DisplayContextProviderInterface* getDisplayContextProviderInterface() const { 110 return &mDisplayContextProviderInterface; 111 } 112 registerRefreshRateChangeListener(std::shared_ptr<RefreshRateChangeListener> listener)113 void registerRefreshRateChangeListener(std::shared_ptr<RefreshRateChangeListener> listener) { 114 mRefreshRateChangeListeners.emplace_back(listener); 115 } 116 117 void setPresentTimeoutParameters(int timeoutNs, 118 const std::vector<std::pair<uint32_t, uint32_t>>& settings); 119 120 void setPresentTimeoutController(uint32_t controllerType); 121 122 // Set refresh rate within the range [minimumRefreshRate, maximumRefreshRateOfCurrentConfig]. 123 // The maximum refresh rate, |maximumRefreshRateOfCurrentConfig|, is intrinsic to the current 124 // configuration, hence only |minimumRefreshRate| needs to be specified. If 125 // |minLockTimeForPeakRefreshRate| does not equal zero, upon arrival of new frames, the 126 // current refresh rate will be set to |maximumRefreshRateOfCurrentConfig| and will remain so 127 // for |minLockTimeForPeakRefreshRate| duration. Afterward, the current refresh rate will 128 // revert to |minimumRefreshRate|. Alternatively, when |minLockTimeForPeakRefreshRate| equals 129 // zero, if no new frame update, refresh rate will drop to |minimumRefreshRate| immediately. 130 int setFixedRefreshRateRange(uint32_t minimumRefreshRate, 131 uint64_t minLockTimeForPeakRefreshRate); 132 133 void dump(String8& result, const std::vector<std::string>& args = {}); 134 135 private: 136 static constexpr char kMinimumRefreshRateRequestTraceName[] = "MinimumRefreshRateRequest"; 137 static constexpr char kMinimumRefreshRateConfiguredTraceName[] = "MinimumRefreshRateConfigured"; 138 139 static constexpr int kMaxFrameRate = 120; 140 static constexpr int kMaxTefrequency = 240; 141 142 static constexpr int64_t kWaitForConfigTimeoutNs = std::nano::den; // 1 second. 143 144 static constexpr int kDefaultRingBufferCapacity = 128; 145 static constexpr int64_t kDefaultWakeUpTimeInPowerSaving = 146 500 * (std::nano::den / std::milli::den); // 500 ms 147 static constexpr int64_t SIGNAL_TIME_PENDING = INT64_MAX; 148 static constexpr int64_t SIGNAL_TIME_INVALID = -1; 149 150 static constexpr int64_t kDefaultSystemPresentTimeoutNs = 151 500 * (std::nano::den / std::milli::den); // 500 ms 152 153 static constexpr int64_t kDefaultVendorPresentTimeoutNs = 154 33 * (std::nano::den / std::milli::den); // 33 ms 155 156 static constexpr std::string_view kVendorDisplayPanelLibrary = "libdisplaypanel.so"; 157 158 static constexpr int64_t kDefaultAheadOfTimeNs = 1000000; // 1 ms; 159 160 enum class VrrControllerState { 161 kDisable = 0, 162 kRendering, 163 kHibernate, 164 }; 165 166 typedef struct PendingVendorRenderingTimeoutTasks { PendingVendorRenderingTimeoutTasksPendingVendorRenderingTimeoutTasks167 PendingVendorRenderingTimeoutTasks(VariableRefreshRateController* controller) 168 : host(controller), taskExecutionTimeNs(kDefaultMaximumNumberOfTasks, 0) {} 169 addTaskPendingVendorRenderingTimeoutTasks170 void addTask(int64_t executionIntervalNs) { 171 taskExecutionTimeNs[numberOfTasks++] = baseTimeNs + executionIntervalNs; 172 } 173 scheduleNextTaskPendingVendorRenderingTimeoutTasks174 void scheduleNextTask() { 175 if (!isDone()) { 176 host->postEvent(VrrControllerEventType::kVendorRenderingTimeoutPost, 177 std::max(getSteadyClockTimeNs(), 178 taskExecutionTimeNs[nextTaskIndex++] - 179 kDefaultAheadOfTimeNs)); 180 } 181 } 182 isDonePendingVendorRenderingTimeoutTasks183 bool isDone() const { return (numberOfTasks == nextTaskIndex); } 184 reserveSpacePendingVendorRenderingTimeoutTasks185 void reserveSpace(size_t size) { 186 if (size > taskExecutionTimeNs.size()) { 187 taskExecutionTimeNs.resize(size); 188 } 189 } 190 resetPendingVendorRenderingTimeoutTasks191 void reset() { numberOfTasks = nextTaskIndex = 0; } 192 193 static constexpr size_t kDefaultMaximumNumberOfTasks = 10; 194 195 VariableRefreshRateController* host; 196 int64_t baseTimeNs = 0; 197 int numberOfTasks = 0; 198 int nextTaskIndex = 0; 199 std::vector<int64_t> taskExecutionTimeNs; 200 } PendingVendorRenderingTimeoutTasks; 201 202 typedef struct PresentEvent { 203 hwc2_config_t config; 204 int64_t mTime; 205 int mDuration; 206 } PresentEvent; 207 208 typedef struct PresentTimeoutSettings { 209 PresentTimeoutSettings() = default; 210 int mTimeoutNs = 0; 211 std::vector<std::pair<uint32_t, uint32_t>> mSchedule; 212 std::function<int()> mFunctor; 213 } PresentTimeoutSettings; 214 215 enum PresentTimeoutControllerType { 216 kNone = 0, 217 kSoftware, 218 kHardware, 219 }; 220 221 // 0: If the minimum refresh rate is unset, the state is set to |kMinRefreshRateUnset|. 222 // 223 // Otherwise, if the minimum refresh rate has been set: 224 // 1: The state is set to |kAtMinRefreshRate|. 225 // 2: If a presentation occurs when the state is |kAtMinRefreshRate|. 226 // 2.1: If |mMaximumRefreshRateTimeoutNs| = 0, no action is taken. 227 // 2.2: If |mMaximumRefreshRateTimeoutNs| > 0, the frame rate is promoted to the maximum refresh 228 // rate and maintained for |mMaximumRefreshRateTimeoutNs| by setting a timer. During this 229 // period, the state is set to |kAtMaximumRefreshRate|. 230 // 3: When a timeout occurs at step 2.2, state is set to |kTransitionToMinimumRefreshRate|. 231 // It remains in this state until there are no further presentations after a period = 232 // |kGotoMinimumRefreshRateIfNoPresentTimeout|. 233 // 4. Then, frame rate reverts to the minimum refresh rate, state is set to |kAtMinRefreshRate|. 234 // Returns to step 1. 235 // Steps 1, 2, 3 and 4 continue until the minimum refresh rate is unset (by inputting 0 or 1); 236 // at this point, the state is set to |kMinRefreshRateUnset| and goto 0. 237 enum MinimumRefreshRatePresentStates { 238 kMinRefreshRateUnset = 0, 239 kAtMinimumRefreshRate, 240 kAtMaximumRefreshRate, 241 kTransitionToMinimumRefreshRate, 242 }; 243 244 typedef struct VsyncEvent { 245 enum class Type { 246 kVblank, 247 kReleaseFence, 248 }; 249 Type mType; 250 int64_t mTime; 251 } VsyncEvent; 252 253 typedef struct VrrRecord { 254 static constexpr int kDefaultRingBufferCapacity = 128; 255 clearVrrRecord256 void clear() { 257 mNextExpectedPresentTime = std::nullopt; 258 mPendingCurrentPresentTime = std::nullopt; 259 mPresentHistory.clear(); 260 mVsyncHistory.clear(); 261 } 262 263 std::optional<PresentEvent> mNextExpectedPresentTime = std::nullopt; 264 std::optional<PresentEvent> mPendingCurrentPresentTime = std::nullopt; 265 266 typedef RingBuffer<PresentEvent, kDefaultRingBufferCapacity> PresentTimeRecord; 267 typedef RingBuffer<VsyncEvent, kDefaultRingBufferCapacity> VsyncRecord; 268 PresentTimeRecord mPresentHistory; 269 VsyncRecord mVsyncHistory; 270 } VrrRecord; 271 272 VariableRefreshRateController(ExynosDisplay* display, const std::string& panelName); 273 274 // Implement interface RefreshListener. 275 virtual void onPresent(int32_t fence) override; 276 virtual void setExpectedPresentTime(int64_t timestampNanos, int frameIntervalNs) override; 277 278 // Implement interface VsyncListener. 279 virtual void onVsync(int64_t timestamp, int32_t vsyncPeriodNanos) override; 280 281 void cancelPresentTimeoutHandlingLocked(); 282 283 void createMinimumRefreshRateTimeoutEventLocked(); 284 285 void dropEventLocked(); 286 void dropEventLocked(VrrControllerEventType eventType); 287 288 std::string dumpEventQueueLocked(); 289 290 uint32_t getCurrentRefreshControlStateLocked() const; 291 292 int64_t getLastFenceSignalTimeUnlocked(int fd); 293 294 int64_t getNextEventTimeLocked() const; 295 getPresentFrameFlag()296 int getPresentFrameFlag() const { 297 int flag = 0; 298 // Is Yuv. 299 for (size_t i = 0; i < mDisplay->mLayers.size(); i++) { 300 auto layer = mDisplay->mLayers[i]; 301 if (layer->isLayerFormatYuv()) { 302 flag |= static_cast<int>(PresentFrameFlag::kIsYuv); 303 } 304 } 305 if (mDisplay->isUpdateRRIndicatorOnly()) { 306 flag |= static_cast<int>(PresentFrameFlag::kUpdateRefreshRateIndicatorLayerOnly); 307 } 308 // Present when doze. 309 if ((mPowerMode == HWC_POWER_MODE_DOZE) || (mPowerMode == HWC_POWER_MODE_DOZE_SUSPEND)) { 310 flag |= static_cast<int>(PresentFrameFlag::kPresentingWhenDoze); 311 } 312 return flag; 313 } 314 315 std::string getStateName(VrrControllerState state) const; 316 317 // Functions responsible for state machine transitions. 318 void handleCadenceChange(); 319 void handleResume(); 320 void handleHibernate(); 321 void handleStayHibernate(); 322 handleCallbackEventLocked(VrrControllerEvent & event)323 void handleCallbackEventLocked(VrrControllerEvent& event) { 324 if (event.mFunctor) { 325 event.mFunctor(); 326 } 327 } 328 329 void handlePresentTimeout(); 330 isMinimumRefreshRateActive()331 inline bool isMinimumRefreshRateActive() const { return (mMinimumRefreshRate > 1); } 332 333 // Report frame frequency changes to the kernel via the sysfs node. 334 void onFrameRateChangedForDBI(int refreshRate); 335 // Report refresh rate changes to the framework(SurfaceFlinger) or other display HWC components. 336 void onRefreshRateChanged(int refreshRate); 337 void onRefreshRateChangedInternal(int refreshRate); 338 void reportRefreshRateIndicator(); 339 std::vector<int> generateValidRefreshRates(const VrrConfig_t& config) const; 340 int convertToValidRefreshRate(int refreshRate); 341 342 void postEvent(VrrControllerEventType type, TimedEvent& timedEvent); 343 void postEvent(VrrControllerEventType type, int64_t when); 344 345 int setFixedRefreshRateRangeWorker(); 346 347 bool shouldHandleVendorRenderingTimeout() const; 348 349 void stopThread(bool exit); 350 351 // The core function of the VRR controller thread. 352 void threadBody(); 353 354 void updateVsyncHistory(); 355 356 ExynosDisplay* mDisplay; 357 358 // The subsequent variables must be guarded by mMutex when accessed. 359 EventQueue mEventQueue; 360 VrrRecord mRecord; 361 362 int32_t mPowerMode = -1; 363 std::vector<PowerModeListener*> mPowerModeListeners; 364 365 VrrControllerState mState; 366 hwc2_config_t mVrrActiveConfig = -1; 367 std::unordered_map<hwc2_config_t, VrrConfig_t> mVrrConfigs; 368 std::optional<int> mLastPresentFence; 369 uint32_t mFrameRate = 0; 370 371 std::shared_ptr<FileNode> mFileNode; 372 373 DisplayContextProviderInterface mDisplayContextProviderInterface; 374 std::unique_ptr<ExternalEventHandlerLoader> mPresentTimeoutEventHandlerLoader; 375 ExternalEventHandler* mPresentTimeoutEventHandler = nullptr; 376 std::optional<PresentTimeoutSettings> mVendorPresentTimeoutOverride; 377 378 std::string mPanelName; 379 380 // Refresh rate indicator. 381 bool mRefreshRateCalculatorEnabled = false; 382 383 std::shared_ptr<RefreshRateCalculator> mRefreshRateCalculator; 384 int mLastRefreshRate = kDefaultInvalidRefreshRate; 385 std::unordered_map<hwc2_config_t, std::vector<int>> mValidRefreshRates; 386 387 std::shared_ptr<RefreshRateCalculator> mFrameRateReporter; 388 389 // Power stats. 390 std::shared_ptr<DisplayStateResidencyWatcher> mResidencyWatcher; 391 std::shared_ptr<VariableRefreshRateStatistic> mVariableRefreshRateStatistic; 392 393 std::shared_ptr<CommonDisplayContextProvider> mDisplayContextProvider; 394 395 bool mEnabled = false; 396 bool mThreadExit = false; 397 398 PresentTimeoutControllerType mDefaultPresentTimeoutController = 399 PresentTimeoutControllerType::kSoftware; 400 PresentTimeoutControllerType mPresentTimeoutController = 401 PresentTimeoutControllerType::kSoftware; 402 403 // When |mMinimumRefreshRate| is 0 or equal to 1, we are in normal mode. 404 // when |mMinimumRefreshRate| is greater than 1. we are in a special mode where the minimum idle 405 // refresh rate is |mMinimumRefreshRate|. 406 uint32_t mMinimumRefreshRate = 0; 407 // |mMaximumRefreshRateTimeoutNs| sets the minimum duration for which we should maintain the 408 // peak refresh rate when transitioning to idle. |mMaximumRefreshRateTimeoutNs| takes effect 409 // only when |mMinimumRefreshRate| is greater than 1. 410 uint64_t mMaximumRefreshRateTimeoutNs = 0; 411 std::optional<TimedEvent> mMinimumRefreshRateTimeoutEvent; 412 MinimumRefreshRatePresentStates mMinimumRefreshRatePresentState = kMinRefreshRateUnset; 413 std::optional<uint32_t> mPendingMinimumRefreshRateRequest = std::nullopt; 414 415 std::vector<std::shared_ptr<RefreshRateChangeListener>> mRefreshRateChangeListeners; 416 417 PendingVendorRenderingTimeoutTasks mPendingVendorRenderingTimeoutTasks; 418 419 // It stores the last present time as a cadence hint. Note that it does not update when 420 // notifyExpectedPresent is called, as notifyExpectedPresent may not result in an actual 421 // display. 422 int64_t mLastExpectedPresentTimeNs = -1; 423 424 std::mutex mMutex; 425 std::condition_variable mCondition; 426 }; 427 428 } // namespace android::hardware::graphics::composer 429