• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 PresentListener,
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  private:
134      static constexpr int kMaxFrameRate = 120;
135      static constexpr int kMaxTefrequency = 240;
136  
137      static constexpr int kDefaultRingBufferCapacity = 128;
138      static constexpr int64_t kDefaultWakeUpTimeInPowerSaving =
139              500 * (std::nano::den / std::milli::den); // 500 ms
140      static constexpr int64_t SIGNAL_TIME_PENDING = INT64_MAX;
141      static constexpr int64_t SIGNAL_TIME_INVALID = -1;
142  
143      static constexpr int64_t kDefaultSystemPresentTimeoutNs =
144              500 * (std::nano::den / std::milli::den); // 500 ms
145  
146      static constexpr int64_t kDefaultVendorPresentTimeoutNs =
147              33 * (std::nano::den / std::milli::den); // 33 ms
148  
149      static constexpr std::string_view kVendorDisplayPanelLibrary = "libdisplaypanel.so";
150  
151      enum class VrrControllerState {
152          kDisable = 0,
153          kRendering,
154          kHibernate,
155      };
156  
157      typedef struct PresentEvent {
158          hwc2_config_t config;
159          int64_t mTime;
160          int mDuration;
161      } PresentEvent;
162  
163      typedef struct PresentTimeoutSettings {
164          PresentTimeoutSettings() = default;
165          int mTimeoutNs = 0;
166          std::vector<std::pair<uint32_t, uint32_t>> mSchedule;
167          std::function<int()> mFunctor;
168      } PresentTimeoutSettings;
169  
170      enum PresentTimeoutControllerType {
171          kNone = 0,
172          kSoftware,
173          kHardware,
174      };
175  
176      // 0: If the minimum refresh rate is unset, the state is set to |kMinRefreshRateUnset|.
177      //
178      // Otherwise, if the minimum refresh rate has been set:
179      // 1: The state is set to |kAtMinRefreshRate|.
180      // 2: If a presentation occurs when the state is |kAtMinRefreshRate|.
181      // 2.1: If |mMaximumRefreshRateTimeoutNs| = 0, no action is taken.
182      // 2.2: If |mMaximumRefreshRateTimeoutNs| > 0, the frame rate is promoted to the maximum refresh
183      //      rate and maintained for |mMaximumRefreshRateTimeoutNs| by setting a timer. During this
184      //      period, the state is set to |kAtMaximumRefreshRate|.
185      // 3: When a timeout occurs at step 2.2, state is set to |kTransitionToMinimumRefreshRate|.
186      //    It remains in this state until there are no further presentations after a period =
187      //    |kGotoMinimumRefreshRateIfNoPresentTimeout|.
188      // 4. Then, frame rate reverts to the minimum refresh rate, state is set to |kAtMinRefreshRate|.
189      //    Returns to step 1.
190      // Steps 1, 2, 3 and 4 continue until the minimum refresh rate is unset (by inputting 0 or 1);
191      // at this point, the state is set to |kMinRefreshRateUnset| and goto 0.
192      enum MinimumRefreshRatePresentStates {
193          kMinRefreshRateUnset = 0,
194          kAtMinimumRefreshRate,
195          kAtMaximumRefreshRate,
196          kTransitionToMinimumRefreshRate,
197      };
198  
199      typedef struct VsyncEvent {
200          enum class Type {
201              kVblank,
202              kReleaseFence,
203          };
204          Type mType;
205          int64_t mTime;
206      } VsyncEvent;
207  
208      typedef struct VrrRecord {
209          static constexpr int kDefaultRingBufferCapacity = 128;
210  
clearVrrRecord211          void clear() {
212              mNextExpectedPresentTime = std::nullopt;
213              mPendingCurrentPresentTime = std::nullopt;
214              mPresentHistory.clear();
215              mVsyncHistory.clear();
216          }
217  
218          std::optional<PresentEvent> mNextExpectedPresentTime = std::nullopt;
219          std::optional<PresentEvent> mPendingCurrentPresentTime = std::nullopt;
220  
221          typedef RingBuffer<PresentEvent, kDefaultRingBufferCapacity> PresentTimeRecord;
222          typedef RingBuffer<VsyncEvent, kDefaultRingBufferCapacity> VsyncRecord;
223          PresentTimeRecord mPresentHistory;
224          VsyncRecord mVsyncHistory;
225      } VrrRecord;
226  
227      VariableRefreshRateController(ExynosDisplay* display, const std::string& panelName);
228  
229      // Implement interface PresentListener.
230      virtual void onPresent(int32_t fence) override;
231      virtual void setExpectedPresentTime(int64_t timestampNanos, int frameIntervalNs) override;
232  
233      // Implement interface VsyncListener.
234      virtual void onVsync(int64_t timestamp, int32_t vsyncPeriodNanos) override;
235  
236      void cancelPresentTimeoutHandlingLocked();
237  
238      void dropEventLocked();
239      void dropEventLocked(VrrControllerEventType eventType);
240  
241      std::string dumpEventQueueLocked();
242  
243      uint32_t getCurrentRefreshControlStateLocked() const;
244  
245      int64_t getLastFenceSignalTimeUnlocked(int fd);
246  
247      int64_t getNextEventTimeLocked() const;
248  
getPresentFrameFlag()249      int getPresentFrameFlag() const {
250          int flag = 0;
251          // Is Yuv.
252          for (size_t i = 0; i < mDisplay->mLayers.size(); i++) {
253              auto layer = mDisplay->mLayers[i];
254              if (layer->isLayerFormatYuv()) {
255                  flag |= static_cast<int>(PresentFrameFlag::kIsYuv);
256              }
257          }
258          if (mDisplay->isUpdateRRIndicatorOnly()) {
259              flag |= static_cast<int>(PresentFrameFlag::kUpdateRefreshRateIndicatorLayerOnly);
260          }
261          // Present when doze.
262          if ((mPowerMode == HWC_POWER_MODE_DOZE) || (mPowerMode == HWC_POWER_MODE_DOZE_SUSPEND)) {
263              flag |= static_cast<int>(PresentFrameFlag::kPresentingWhenDoze);
264          }
265          return flag;
266      }
267  
268      std::string getStateName(VrrControllerState state) const;
269  
270      // Functions responsible for state machine transitions.
271      void handleCadenceChange();
272      void handleResume();
273      void handleHibernate();
274      void handleStayHibernate();
275  
handleCallbackEventLocked(VrrControllerEvent & event)276      void handleCallbackEventLocked(VrrControllerEvent& event) {
277          if (event.mFunctor) {
278              event.mFunctor();
279          }
280      }
281  
282      void handlePresentTimeout(const VrrControllerEvent& event);
283  
isMinimumRefreshRateActive()284      inline bool isMinimumRefreshRateActive() const { return (mMinimumRefreshRate > 1); }
285  
286      // Report frame frequency changes to the kernel via the sysfs node.
287      void onFrameRateChangedForDBI(int refreshRate);
288      // Report refresh rate changes to the framework(SurfaceFlinger) or other display HWC components.
289      void onRefreshRateChanged(int refreshRate);
290      void onRefreshRateChangedInternal(int refreshRate);
291      void reportRefreshRateIndicator();
292      std::vector<int> generateValidRefreshRates(const VrrConfig_t& config) const;
293      int convertToValidRefreshRate(int refreshRate);
294  
295      void postEvent(VrrControllerEventType type, TimedEvent& timedEvent);
296      void postEvent(VrrControllerEventType type, int64_t when);
297  
298      bool shouldHandleVendorRenderingTimeout() const;
299  
300      void stopThread(bool exit);
301  
302      // The core function of the VRR controller thread.
303      void threadBody();
304  
305      void updateVsyncHistory();
306  
307      ExynosDisplay* mDisplay;
308  
309      // The subsequent variables must be guarded by mMutex when accessed.
310      EventQueue mEventQueue;
311      VrrRecord mRecord;
312  
313      int32_t mPowerMode = -1;
314      std::vector<PowerModeListener*> mPowerModeListeners;
315  
316      VrrControllerState mState;
317      hwc2_config_t mVrrActiveConfig = -1;
318      std::unordered_map<hwc2_config_t, VrrConfig_t> mVrrConfigs;
319      std::optional<int> mLastPresentFence;
320  
321      std::shared_ptr<FileNode> mFileNode;
322  
323      DisplayContextProviderInterface mDisplayContextProviderInterface;
324      std::unique_ptr<ExternalEventHandlerLoader> mPresentTimeoutEventHandlerLoader;
325      ExternalEventHandler* mPresentTimeoutEventHandler = nullptr;
326      std::optional<PresentTimeoutSettings> mVendorPresentTimeoutOverride;
327  
328      std::string mPanelName;
329  
330      // Refresh rate indicator.
331      bool mRefreshRateCalculatorEnabled = false;
332  
333      std::shared_ptr<RefreshRateCalculator> mRefreshRateCalculator;
334      int mLastRefreshRate = kDefaultInvalidRefreshRate;
335      std::unordered_map<hwc2_config_t, std::vector<int>> mValidRefreshRates;
336  
337      std::shared_ptr<RefreshRateCalculator> mFrameRateReporter;
338  
339      // Power stats.
340      std::shared_ptr<DisplayStateResidencyWatcher> mResidencyWatcher;
341      std::shared_ptr<VariableRefreshRateStatistic> mVariableRefreshRateStatistic;
342  
343      std::shared_ptr<CommonDisplayContextProvider> mDisplayContextProvider;
344  
345      bool mEnabled = false;
346      bool mThreadExit = false;
347  
348      PresentTimeoutControllerType mPresentTimeoutController =
349              PresentTimeoutControllerType::kSoftware;
350  
351      // When |mMinimumRefreshRate| is 0 or equal to 1, we are in normal mode.
352      // when |mMinimumRefreshRate| is greater than 1. we are in a special mode where the minimum idle
353      // refresh rate is |mMinimumRefreshRate|.
354      uint32_t mMinimumRefreshRate = 0;
355      // |mMaximumRefreshRateTimeoutNs| sets the minimum duration for which we should maintain the
356      // peak refresh rate when transitioning to idle. |mMaximumRefreshRateTimeoutNs| takes effect
357      // only when |mMinimumRefreshRate| is greater than 1.
358      uint64_t mMaximumRefreshRateTimeoutNs = 0;
359      std::optional<TimedEvent> mMinimumRefreshRateTimeoutEvent;
360      MinimumRefreshRatePresentStates mMinimumRefreshRatePresentStates = kMinRefreshRateUnset;
361  
362      std::vector<std::shared_ptr<RefreshRateChangeListener>> mRefreshRateChangeListeners;
363  
364      std::mutex mMutex;
365      std::condition_variable mCondition;
366  };
367  
368  } // namespace android::hardware::graphics::composer
369