blob: fb14dc7a9a19b95e072bd374dfc6cd29a012b9fe [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
76 // Sets the current policy to choose refresh rates.
77 void setPolicy(HwcConfigIndexType defaultConfigId, float minRefreshRate, float maxRefreshRate)
78 EXCLUDES(mLock);
79
Steven Thomas2bbaabe2019-08-28 16:08:35 -070080 // Returns true if this device is doing refresh rate switching. This won't change at runtime.
Ady Abraham2139f732019-11-13 18:56:40 -080081 bool refreshRateSwitchingSupported() const { return mRefreshRateSwitching; }
Steven Thomas2bbaabe2019-08-28 16:08:35 -070082
Ady Abraham2139f732019-11-13 18:56:40 -080083 // Returns all available refresh rates according to the current policy.
84 const RefreshRate& getRefreshRateForContent(float contentFramerate) const EXCLUDES(mLock);
Ana Krulecb43429d2019-01-09 14:28:51 -080085
Ady Abraham2139f732019-11-13 18:56:40 -080086 // Returns all the refresh rates supported by the device. This won't change at runtime.
87 const AllRefreshRatesMapType& getAllRefreshRates() const EXCLUDES(mLock);
Steven Thomas2bbaabe2019-08-28 16:08:35 -070088
Ady Abraham2139f732019-11-13 18:56:40 -080089 // Returns the lowest refresh rate supported by the device. This won't change at runtime.
90 const RefreshRate& getMinRefreshRate() const { return *mMinSupportedRefreshRate; }
Steven Thomas2bbaabe2019-08-28 16:08:35 -070091
Ady Abraham2139f732019-11-13 18:56:40 -080092 // Returns the lowest refresh rate according to the current policy. May change in runtime.
93 const RefreshRate& getMinRefreshRateByPolicy() const EXCLUDES(mLock);
Steven Thomas2bbaabe2019-08-28 16:08:35 -070094
Ady Abraham2139f732019-11-13 18:56:40 -080095 // Returns the highest refresh rate supported by the device. This won't change at runtime.
96 const RefreshRate& getMaxRefreshRate() const { return *mMaxSupportedRefreshRate; }
Steven Thomas2bbaabe2019-08-28 16:08:35 -070097
Ady Abraham2139f732019-11-13 18:56:40 -080098 // 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 Laskowski22488f62019-03-28 09:53:04 -0700112
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700113 struct InputConfig {
Ady Abraham2139f732019-11-13 18:56:40 -0800114 HwcConfigIndexType configId = HwcConfigIndexType(0);
115 HwcConfigGroupType configGroup = HwcConfigGroupType(0);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700116 nsecs_t vsyncPeriod = 0;
117 };
Ana Krulec4593b692019-01-11 22:07:25 -0800118
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700119 RefreshRateConfigs(bool refreshRateSwitching, const std::vector<InputConfig>& configs,
Ady Abraham2139f732019-11-13 18:56:40 -0800120 HwcConfigIndexType currentHwcConfig);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700121 RefreshRateConfigs(bool refreshRateSwitching,
122 const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs,
Ady Abraham2139f732019-11-13 18:56:40 -0800123 HwcConfigIndexType currentConfigId);
Ana Krulec4593b692019-01-11 22:07:25 -0800124
Dominik Laskowski22488f62019-03-28 09:53:04 -0700125private:
Ady Abraham2139f732019-11-13 18:56:40 -0800126 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 Thomas2bbaabe2019-08-28 16:08:35 -0700134 // The list of refresh rates, indexed by display config ID. This must not change after this
135 // object is initialized.
Ady Abraham2139f732019-11-13 18:56:40 -0800136 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 Krulecb43429d2019-01-09 14:28:51 -0800163};
164
Dominik Laskowski98041832019-08-01 18:35:59 -0700165} // namespace android::scheduler