Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 19 | #include <android-base/stringprintf.h> |
| 20 | |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 21 | #include <algorithm> |
| 22 | #include <numeric> |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 23 | #include <type_traits> |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 24 | |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 25 | #include "DisplayHardware/HWComposer.h" |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 26 | #include "HwcStrongTypes.h" |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 27 | #include "Scheduler/SchedulerUtils.h" |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 28 | #include "Scheduler/StrongTyping.h" |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 29 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 30 | namespace android::scheduler { |
| 31 | |
| 32 | enum class RefreshRateConfigEvent : unsigned { None = 0b0, Changed = 0b1 }; |
| 33 | |
| 34 | inline RefreshRateConfigEvent operator|(RefreshRateConfigEvent lhs, RefreshRateConfigEvent rhs) { |
| 35 | using T = std::underlying_type_t<RefreshRateConfigEvent>; |
| 36 | return static_cast<RefreshRateConfigEvent>(static_cast<T>(lhs) | static_cast<T>(rhs)); |
| 37 | } |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
Ady Abraham | 1902d07 | 2019-03-01 17:18:59 -0800 | [diff] [blame] | 40 | * This class is used to encapsulate configuration for refresh rates. It holds information |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 41 | * about available refresh rates on the device, and the mapping between the numbers and human |
| 42 | * readable names. |
| 43 | */ |
| 44 | class RefreshRateConfigs { |
| 45 | public: |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 46 | struct RefreshRate { |
Ana Krulec | 72f0d6e | 2020-01-06 15:24:47 -0800 | [diff] [blame] | 47 | // The tolerance within which we consider FPS approximately equals. |
| 48 | static constexpr float FPS_EPSILON = 0.001f; |
| 49 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 50 | RefreshRate(HwcConfigIndexType configId, nsecs_t vsyncPeriod, |
| 51 | HwcConfigGroupType configGroup, std::string name, float fps) |
| 52 | : configId(configId), |
| 53 | vsyncPeriod(vsyncPeriod), |
| 54 | configGroup(configGroup), |
| 55 | name(std::move(name)), |
| 56 | fps(fps) {} |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 57 | // This config ID corresponds to the position of the config in the vector that is stored |
| 58 | // on the device. |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 59 | const HwcConfigIndexType configId; |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 60 | // Vsync period in nanoseconds. |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 61 | const nsecs_t vsyncPeriod; |
| 62 | // This configGroup for the config. |
| 63 | const HwcConfigGroupType configGroup; |
| 64 | // Human readable name of the refresh rate. |
| 65 | const std::string name; |
| 66 | // Refresh rate in frames per second |
| 67 | const float fps = 0; |
| 68 | |
Ana Krulec | 72f0d6e | 2020-01-06 15:24:47 -0800 | [diff] [blame] | 69 | // Checks whether the fps of this RefreshRate struct is within a given min and max refresh |
| 70 | // rate passed in. FPS_EPSILON is applied to the boundaries for approximation. |
| 71 | bool inPolicy(float minRefreshRate, float maxRefreshRate) const { |
| 72 | return (fps >= (minRefreshRate - FPS_EPSILON) && fps <= (maxRefreshRate + FPS_EPSILON)); |
| 73 | } |
| 74 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 75 | bool operator!=(const RefreshRate& other) const { |
| 76 | return configId != other.configId || vsyncPeriod != other.vsyncPeriod || |
| 77 | configGroup != other.configGroup; |
| 78 | } |
| 79 | |
| 80 | bool operator==(const RefreshRate& other) const { return !(*this != other); } |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 81 | }; |
| 82 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 83 | using AllRefreshRatesMapType = std::unordered_map<HwcConfigIndexType, const RefreshRate>; |
| 84 | |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 85 | // Sets the current policy to choose refresh rates. Returns NO_ERROR if the requested policy is |
| 86 | // valid, or a negative error value otherwise. policyChanged, if non-null, will be set to true |
| 87 | // if the new policy is different from the old policy. |
| 88 | status_t setPolicy(HwcConfigIndexType defaultConfigId, float minRefreshRate, |
| 89 | float maxRefreshRate, bool* policyChanged) EXCLUDES(mLock); |
| 90 | // Gets the current policy. |
| 91 | void getPolicy(HwcConfigIndexType* defaultConfigId, float* minRefreshRate, |
| 92 | float* maxRefreshRate) const EXCLUDES(mLock); |
| 93 | |
| 94 | // Returns true if config is allowed by the current policy. |
| 95 | bool isConfigAllowed(HwcConfigIndexType config) const EXCLUDES(mLock); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 96 | |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 97 | // Returns true if this device is doing refresh rate switching. This won't change at runtime. |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 98 | bool refreshRateSwitchingSupported() const { return mRefreshRateSwitching; } |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 99 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 100 | // Returns all available refresh rates according to the current policy. |
| 101 | const RefreshRate& getRefreshRateForContent(float contentFramerate) const EXCLUDES(mLock); |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 102 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 103 | // Returns all the refresh rates supported by the device. This won't change at runtime. |
| 104 | const AllRefreshRatesMapType& getAllRefreshRates() const EXCLUDES(mLock); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 105 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 106 | // Returns the lowest refresh rate supported by the device. This won't change at runtime. |
| 107 | const RefreshRate& getMinRefreshRate() const { return *mMinSupportedRefreshRate; } |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 108 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 109 | // Returns the lowest refresh rate according to the current policy. May change in runtime. |
| 110 | const RefreshRate& getMinRefreshRateByPolicy() const EXCLUDES(mLock); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 111 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 112 | // Returns the highest refresh rate supported by the device. This won't change at runtime. |
| 113 | const RefreshRate& getMaxRefreshRate() const { return *mMaxSupportedRefreshRate; } |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 114 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 115 | // Returns the highest refresh rate according to the current policy. May change in runtime. |
| 116 | const RefreshRate& getMaxRefreshRateByPolicy() const EXCLUDES(mLock); |
| 117 | |
| 118 | // Returns the current refresh rate |
| 119 | const RefreshRate& getCurrentRefreshRate() const EXCLUDES(mLock); |
| 120 | |
| 121 | // Returns the refresh rate that corresponds to a HwcConfigIndexType. This won't change at |
| 122 | // runtime. |
| 123 | const RefreshRate& getRefreshRateFromConfigId(HwcConfigIndexType configId) const { |
| 124 | return mRefreshRates.at(configId); |
| 125 | }; |
| 126 | |
| 127 | // Stores the current configId the device operates at |
| 128 | void setCurrentConfigId(HwcConfigIndexType configId) EXCLUDES(mLock); |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 129 | |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 130 | struct InputConfig { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 131 | HwcConfigIndexType configId = HwcConfigIndexType(0); |
| 132 | HwcConfigGroupType configGroup = HwcConfigGroupType(0); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 133 | nsecs_t vsyncPeriod = 0; |
| 134 | }; |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 135 | |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 136 | RefreshRateConfigs(bool refreshRateSwitching, const std::vector<InputConfig>& configs, |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 137 | HwcConfigIndexType currentHwcConfig); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 138 | RefreshRateConfigs(bool refreshRateSwitching, |
| 139 | const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs, |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 140 | HwcConfigIndexType currentConfigId); |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 141 | |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 142 | private: |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 143 | void init(const std::vector<InputConfig>& configs, HwcConfigIndexType currentHwcConfig); |
| 144 | |
| 145 | void constructAvailableRefreshRates() REQUIRES(mLock); |
| 146 | |
| 147 | void getSortedRefreshRateList( |
| 148 | const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate, |
| 149 | std::vector<const RefreshRate*>* outRefreshRates); |
| 150 | |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 151 | // The list of refresh rates, indexed by display config ID. This must not change after this |
| 152 | // object is initialized. |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 153 | AllRefreshRatesMapType mRefreshRates; |
| 154 | |
| 155 | // The list of refresh rates which are available in the current policy, ordered by vsyncPeriod |
| 156 | // (the first element is the lowest refresh rate) |
| 157 | std::vector<const RefreshRate*> mAvailableRefreshRates GUARDED_BY(mLock); |
| 158 | |
| 159 | // The current config. This will change at runtime. This is set by SurfaceFlinger on |
| 160 | // the main thread, and read by the Scheduler (and other objects) on other threads. |
| 161 | const RefreshRate* mCurrentRefreshRate GUARDED_BY(mLock); |
| 162 | |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 163 | // The default config. This will change at runtime. This is set by SurfaceFlinger on |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 164 | // the main thread, and read by the Scheduler (and other objects) on other threads. |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 165 | HwcConfigIndexType mDefaultConfig GUARDED_BY(mLock); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 166 | |
| 167 | // The min and max FPS allowed by the policy. This will change at runtime and set by |
| 168 | // SurfaceFlinger on the main thread. |
| 169 | float mMinRefreshRateFps GUARDED_BY(mLock) = 0; |
| 170 | float mMaxRefreshRateFps GUARDED_BY(mLock) = std::numeric_limits<float>::max(); |
| 171 | |
| 172 | // The min and max refresh rates supported by the device. |
| 173 | // This will not change at runtime. |
| 174 | const RefreshRate* mMinSupportedRefreshRate; |
| 175 | const RefreshRate* mMaxSupportedRefreshRate; |
| 176 | |
| 177 | const bool mRefreshRateSwitching; |
| 178 | |
| 179 | mutable std::mutex mLock; |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 180 | }; |
| 181 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 182 | } // namespace android::scheduler |