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 { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 47 | RefreshRate(HwcConfigIndexType configId, nsecs_t vsyncPeriod, |
| 48 | HwcConfigGroupType configGroup, std::string name, float fps) |
| 49 | : configId(configId), |
| 50 | vsyncPeriod(vsyncPeriod), |
| 51 | configGroup(configGroup), |
| 52 | name(std::move(name)), |
| 53 | fps(fps) {} |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 54 | // This config ID corresponds to the position of the config in the vector that is stored |
| 55 | // on the device. |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 56 | const HwcConfigIndexType configId; |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 57 | // Vsync period in nanoseconds. |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 58 | const nsecs_t vsyncPeriod; |
| 59 | // This configGroup for the config. |
| 60 | const HwcConfigGroupType configGroup; |
| 61 | // Human readable name of the refresh rate. |
| 62 | const std::string name; |
| 63 | // Refresh rate in frames per second |
| 64 | const float fps = 0; |
| 65 | |
| 66 | bool operator!=(const RefreshRate& other) const { |
| 67 | return configId != other.configId || vsyncPeriod != other.vsyncPeriod || |
| 68 | configGroup != other.configGroup; |
| 69 | } |
| 70 | |
| 71 | bool operator==(const RefreshRate& other) const { return !(*this != other); } |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 72 | }; |
| 73 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 74 | using AllRefreshRatesMapType = std::unordered_map<HwcConfigIndexType, const RefreshRate>; |
| 75 | |
| 76 | // Sets the current policy to choose refresh rates. |
| 77 | void setPolicy(HwcConfigIndexType defaultConfigId, float minRefreshRate, float maxRefreshRate) |
| 78 | EXCLUDES(mLock); |
| 79 | |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 80 | // 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] | 81 | bool refreshRateSwitchingSupported() const { return mRefreshRateSwitching; } |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 82 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 83 | // Returns all available refresh rates according to the current policy. |
| 84 | const RefreshRate& getRefreshRateForContent(float contentFramerate) const EXCLUDES(mLock); |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 85 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 86 | // Returns all the refresh rates supported by the device. This won't change at runtime. |
| 87 | const AllRefreshRatesMapType& getAllRefreshRates() const EXCLUDES(mLock); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 88 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 89 | // Returns the lowest refresh rate supported by the device. This won't change at runtime. |
| 90 | const RefreshRate& getMinRefreshRate() const { return *mMinSupportedRefreshRate; } |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 91 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 92 | // Returns the lowest refresh rate according to the current policy. May change in runtime. |
| 93 | const RefreshRate& getMinRefreshRateByPolicy() const EXCLUDES(mLock); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 94 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 95 | // Returns the highest refresh rate supported by the device. This won't change at runtime. |
| 96 | const RefreshRate& getMaxRefreshRate() const { return *mMaxSupportedRefreshRate; } |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 97 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 98 | // Returns the highest refresh rate according to the current policy. May change in runtime. |
| 99 | const RefreshRate& getMaxRefreshRateByPolicy() const EXCLUDES(mLock); |
| 100 | |
| 101 | // Returns the current refresh rate |
| 102 | const RefreshRate& getCurrentRefreshRate() const EXCLUDES(mLock); |
| 103 | |
| 104 | // Returns the refresh rate that corresponds to a HwcConfigIndexType. This won't change at |
| 105 | // runtime. |
| 106 | const RefreshRate& getRefreshRateFromConfigId(HwcConfigIndexType configId) const { |
| 107 | return mRefreshRates.at(configId); |
| 108 | }; |
| 109 | |
| 110 | // Stores the current configId the device operates at |
| 111 | void setCurrentConfigId(HwcConfigIndexType configId) EXCLUDES(mLock); |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 112 | |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 113 | struct InputConfig { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 114 | HwcConfigIndexType configId = HwcConfigIndexType(0); |
| 115 | HwcConfigGroupType configGroup = HwcConfigGroupType(0); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 116 | nsecs_t vsyncPeriod = 0; |
| 117 | }; |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 118 | |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 119 | RefreshRateConfigs(bool refreshRateSwitching, const std::vector<InputConfig>& configs, |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 120 | HwcConfigIndexType currentHwcConfig); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 121 | RefreshRateConfigs(bool refreshRateSwitching, |
| 122 | const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs, |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 123 | HwcConfigIndexType currentConfigId); |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 124 | |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 125 | private: |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 126 | void init(const std::vector<InputConfig>& configs, HwcConfigIndexType currentHwcConfig); |
| 127 | |
| 128 | void constructAvailableRefreshRates() REQUIRES(mLock); |
| 129 | |
| 130 | void getSortedRefreshRateList( |
| 131 | const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate, |
| 132 | std::vector<const RefreshRate*>* outRefreshRates); |
| 133 | |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 134 | // The list of refresh rates, indexed by display config ID. This must not change after this |
| 135 | // object is initialized. |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 136 | AllRefreshRatesMapType mRefreshRates; |
| 137 | |
| 138 | // The list of refresh rates which are available in the current policy, ordered by vsyncPeriod |
| 139 | // (the first element is the lowest refresh rate) |
| 140 | std::vector<const RefreshRate*> mAvailableRefreshRates GUARDED_BY(mLock); |
| 141 | |
| 142 | // The current config. This will change at runtime. This is set by SurfaceFlinger on |
| 143 | // the main thread, and read by the Scheduler (and other objects) on other threads. |
| 144 | const RefreshRate* mCurrentRefreshRate GUARDED_BY(mLock); |
| 145 | |
| 146 | // The current config group. This will change at runtime. This is set by SurfaceFlinger on |
| 147 | // the main thread, and read by the Scheduler (and other objects) on other threads. |
| 148 | HwcConfigGroupType mCurrentGroupId GUARDED_BY(mLock); |
| 149 | |
| 150 | // The min and max FPS allowed by the policy. This will change at runtime and set by |
| 151 | // SurfaceFlinger on the main thread. |
| 152 | float mMinRefreshRateFps GUARDED_BY(mLock) = 0; |
| 153 | float mMaxRefreshRateFps GUARDED_BY(mLock) = std::numeric_limits<float>::max(); |
| 154 | |
| 155 | // The min and max refresh rates supported by the device. |
| 156 | // This will not change at runtime. |
| 157 | const RefreshRate* mMinSupportedRefreshRate; |
| 158 | const RefreshRate* mMaxSupportedRefreshRate; |
| 159 | |
| 160 | const bool mRefreshRateSwitching; |
| 161 | |
| 162 | mutable std::mutex mLock; |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 163 | }; |
| 164 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 165 | } // namespace android::scheduler |