blob: 1e740ca1d66518dbc72d2c18e2d38eb5abcae08f [file] [log] [blame]
Ana Krulecb43429d2019-01-09 14:28:51 -08001/*
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 Laskowski98041832019-08-01 18:35:59 -070019#include <android-base/stringprintf.h>
20
Ana Krulecb43429d2019-01-09 14:28:51 -080021#include <algorithm>
22#include <numeric>
Dominik Laskowski98041832019-08-01 18:35:59 -070023#include <type_traits>
Ana Krulecb43429d2019-01-09 14:28:51 -080024
Ana Krulec4593b692019-01-11 22:07:25 -080025#include "DisplayHardware/HWComposer.h"
Ady Abraham2139f732019-11-13 18:56:40 -080026#include "HwcStrongTypes.h"
Ana Krulec4593b692019-01-11 22:07:25 -080027#include "Scheduler/SchedulerUtils.h"
Ady Abraham2139f732019-11-13 18:56:40 -080028#include "Scheduler/StrongTyping.h"
Ana Krulec4593b692019-01-11 22:07:25 -080029
Dominik Laskowski98041832019-08-01 18:35:59 -070030namespace android::scheduler {
31
32enum class RefreshRateConfigEvent : unsigned { None = 0b0, Changed = 0b1 };
33
34inline 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 Krulecb43429d2019-01-09 14:28:51 -080038
39/**
Ady Abraham1902d072019-03-01 17:18:59 -080040 * This class is used to encapsulate configuration for refresh rates. It holds information
Ana Krulecb43429d2019-01-09 14:28:51 -080041 * about available refresh rates on the device, and the mapping between the numbers and human
42 * readable names.
43 */
44class RefreshRateConfigs {
45public:
Ana Krulecb43429d2019-01-09 14:28:51 -080046 struct RefreshRate {
Ady Abraham2139f732019-11-13 18:56:40 -080047 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 Krulecb43429d2019-01-09 14:28:51 -080054 // This config ID corresponds to the position of the config in the vector that is stored
55 // on the device.
Ady Abraham2139f732019-11-13 18:56:40 -080056 const HwcConfigIndexType configId;
Steven Thomas2bbaabe2019-08-28 16:08:35 -070057 // Vsync period in nanoseconds.
Ady Abraham2139f732019-11-13 18:56:40 -080058 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 Krulecb43429d2019-01-09 14:28:51 -080072 };
73
Ady Abraham2139f732019-11-13 18:56:40 -080074 using AllRefreshRatesMapType = std::unordered_map<HwcConfigIndexType, const RefreshRate>;
75
Ana Kruleced3a8cc2019-11-14 00:55:07 +010076 // Sets the current policy to choose refresh rates. Returns NO_ERROR if the requested policy is
77 // valid, or a negative error value otherwise. policyChanged, if non-null, will be set to true
78 // if the new policy is different from the old policy.
79 status_t setPolicy(HwcConfigIndexType defaultConfigId, float minRefreshRate,
80 float maxRefreshRate, bool* policyChanged) EXCLUDES(mLock);
81 // Gets the current policy.
82 void getPolicy(HwcConfigIndexType* defaultConfigId, float* minRefreshRate,
83 float* maxRefreshRate) const EXCLUDES(mLock);
84
85 // Returns true if config is allowed by the current policy.
86 bool isConfigAllowed(HwcConfigIndexType config) const EXCLUDES(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -080087
Steven Thomas2bbaabe2019-08-28 16:08:35 -070088 // Returns true if this device is doing refresh rate switching. This won't change at runtime.
Ady Abraham2139f732019-11-13 18:56:40 -080089 bool refreshRateSwitchingSupported() const { return mRefreshRateSwitching; }
Steven Thomas2bbaabe2019-08-28 16:08:35 -070090
Ady Abraham2139f732019-11-13 18:56:40 -080091 // Returns all available refresh rates according to the current policy.
92 const RefreshRate& getRefreshRateForContent(float contentFramerate) const EXCLUDES(mLock);
Ana Krulecb43429d2019-01-09 14:28:51 -080093
Ady Abraham2139f732019-11-13 18:56:40 -080094 // Returns all the refresh rates supported by the device. This won't change at runtime.
95 const AllRefreshRatesMapType& getAllRefreshRates() const EXCLUDES(mLock);
Steven Thomas2bbaabe2019-08-28 16:08:35 -070096
Ady Abraham2139f732019-11-13 18:56:40 -080097 // Returns the lowest refresh rate supported by the device. This won't change at runtime.
98 const RefreshRate& getMinRefreshRate() const { return *mMinSupportedRefreshRate; }
Steven Thomas2bbaabe2019-08-28 16:08:35 -070099
Ady Abraham2139f732019-11-13 18:56:40 -0800100 // Returns the lowest refresh rate according to the current policy. May change in runtime.
101 const RefreshRate& getMinRefreshRateByPolicy() const EXCLUDES(mLock);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700102
Ady Abraham2139f732019-11-13 18:56:40 -0800103 // Returns the highest refresh rate supported by the device. This won't change at runtime.
104 const RefreshRate& getMaxRefreshRate() const { return *mMaxSupportedRefreshRate; }
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700105
Ady Abraham2139f732019-11-13 18:56:40 -0800106 // Returns the highest refresh rate according to the current policy. May change in runtime.
107 const RefreshRate& getMaxRefreshRateByPolicy() const EXCLUDES(mLock);
108
109 // Returns the current refresh rate
110 const RefreshRate& getCurrentRefreshRate() const EXCLUDES(mLock);
111
112 // Returns the refresh rate that corresponds to a HwcConfigIndexType. This won't change at
113 // runtime.
114 const RefreshRate& getRefreshRateFromConfigId(HwcConfigIndexType configId) const {
115 return mRefreshRates.at(configId);
116 };
117
118 // Stores the current configId the device operates at
119 void setCurrentConfigId(HwcConfigIndexType configId) EXCLUDES(mLock);
Dominik Laskowski22488f62019-03-28 09:53:04 -0700120
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700121 struct InputConfig {
Ady Abraham2139f732019-11-13 18:56:40 -0800122 HwcConfigIndexType configId = HwcConfigIndexType(0);
123 HwcConfigGroupType configGroup = HwcConfigGroupType(0);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700124 nsecs_t vsyncPeriod = 0;
125 };
Ana Krulec4593b692019-01-11 22:07:25 -0800126
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700127 RefreshRateConfigs(bool refreshRateSwitching, const std::vector<InputConfig>& configs,
Ady Abraham2139f732019-11-13 18:56:40 -0800128 HwcConfigIndexType currentHwcConfig);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700129 RefreshRateConfigs(bool refreshRateSwitching,
130 const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs,
Ady Abraham2139f732019-11-13 18:56:40 -0800131 HwcConfigIndexType currentConfigId);
Ana Krulec4593b692019-01-11 22:07:25 -0800132
Dominik Laskowski22488f62019-03-28 09:53:04 -0700133private:
Ady Abraham2139f732019-11-13 18:56:40 -0800134 void init(const std::vector<InputConfig>& configs, HwcConfigIndexType currentHwcConfig);
135
136 void constructAvailableRefreshRates() REQUIRES(mLock);
137
138 void getSortedRefreshRateList(
139 const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate,
140 std::vector<const RefreshRate*>* outRefreshRates);
141
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700142 // The list of refresh rates, indexed by display config ID. This must not change after this
143 // object is initialized.
Ady Abraham2139f732019-11-13 18:56:40 -0800144 AllRefreshRatesMapType mRefreshRates;
145
146 // The list of refresh rates which are available in the current policy, ordered by vsyncPeriod
147 // (the first element is the lowest refresh rate)
148 std::vector<const RefreshRate*> mAvailableRefreshRates GUARDED_BY(mLock);
149
150 // The current config. This will change at runtime. This is set by SurfaceFlinger on
151 // the main thread, and read by the Scheduler (and other objects) on other threads.
152 const RefreshRate* mCurrentRefreshRate GUARDED_BY(mLock);
153
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100154 // The default config. This will change at runtime. This is set by SurfaceFlinger on
Ady Abraham2139f732019-11-13 18:56:40 -0800155 // the main thread, and read by the Scheduler (and other objects) on other threads.
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100156 HwcConfigIndexType mDefaultConfig GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800157
158 // The min and max FPS allowed by the policy. This will change at runtime and set by
159 // SurfaceFlinger on the main thread.
160 float mMinRefreshRateFps GUARDED_BY(mLock) = 0;
161 float mMaxRefreshRateFps GUARDED_BY(mLock) = std::numeric_limits<float>::max();
162
163 // The min and max refresh rates supported by the device.
164 // This will not change at runtime.
165 const RefreshRate* mMinSupportedRefreshRate;
166 const RefreshRate* mMaxSupportedRefreshRate;
167
168 const bool mRefreshRateSwitching;
169
170 mutable std::mutex mLock;
Ana Krulecb43429d2019-01-09 14:28:51 -0800171};
172
Dominik Laskowski98041832019-08-01 18:35:59 -0700173} // namespace android::scheduler