blob: 9d4774962a63daaa7f03af857634217799602d2c [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"
26#include "Scheduler/SchedulerUtils.h"
27
Dominik Laskowski98041832019-08-01 18:35:59 -070028namespace android::scheduler {
29
30enum class RefreshRateConfigEvent : unsigned { None = 0b0, Changed = 0b1 };
31
32inline RefreshRateConfigEvent operator|(RefreshRateConfigEvent lhs, RefreshRateConfigEvent rhs) {
33 using T = std::underlying_type_t<RefreshRateConfigEvent>;
34 return static_cast<RefreshRateConfigEvent>(static_cast<T>(lhs) | static_cast<T>(rhs));
35}
Ana Krulecb43429d2019-01-09 14:28:51 -080036
37/**
Ady Abraham1902d072019-03-01 17:18:59 -080038 * This class is used to encapsulate configuration for refresh rates. It holds information
Ana Krulecb43429d2019-01-09 14:28:51 -080039 * about available refresh rates on the device, and the mapping between the numbers and human
40 * readable names.
41 */
42class RefreshRateConfigs {
43public:
44 // Enum to indicate which vsync rate to run at. Power saving is intended to be the lowest
45 // (eg. when the screen is in AOD mode or off), default is the old 60Hz, and performance
46 // is the new 90Hz. Eventually we want to have a way for vendors to map these in the configs.
47 enum class RefreshRateType { POWER_SAVING, DEFAULT, PERFORMANCE };
48
49 struct RefreshRate {
Ana Krulecb43429d2019-01-09 14:28:51 -080050 // This config ID corresponds to the position of the config in the vector that is stored
51 // on the device.
52 int configId;
53 // Human readable name of the refresh rate.
54 std::string name;
Alec Mourifb571ea2019-01-24 18:42:10 -080055 // Refresh rate in frames per second, rounded to the nearest integer.
56 uint32_t fps = 0;
Ady Abraham796beb02019-04-11 15:23:07 -070057 // config Id (returned from HWC2::Display::Config::getId())
58 hwc2_config_t id;
Ana Krulecb43429d2019-01-09 14:28:51 -080059 };
60
61 // TODO(b/122916473): Get this information from configs prepared by vendors, instead of
62 // baking them in.
Dominik Laskowski22488f62019-03-28 09:53:04 -070063 const std::map<RefreshRateType, std::shared_ptr<RefreshRate>>& getRefreshRates() const {
Ady Abraham1902d072019-03-01 17:18:59 -080064 return mRefreshRates;
65 }
Ady Abraham09bd3922019-04-08 10:44:56 -070066 std::shared_ptr<RefreshRate> getRefreshRate(RefreshRateType type) const {
Alec Mouri0a1cc962019-03-14 12:33:02 -070067 const auto& refreshRate = mRefreshRates.find(type);
68 if (refreshRate != mRefreshRates.end()) {
69 return refreshRate->second;
70 }
71 return nullptr;
72 }
Ana Krulecb43429d2019-01-09 14:28:51 -080073
Ady Abraham796beb02019-04-11 15:23:07 -070074 RefreshRateType getRefreshRateType(hwc2_config_t id) const {
75 for (const auto& [type, refreshRate] : mRefreshRates) {
76 if (refreshRate->id == id) {
77 return type;
78 }
79 }
80
81 return RefreshRateType::DEFAULT;
82 }
83
Dominik Laskowski22488f62019-03-28 09:53:04 -070084 void populate(const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs) {
85 mRefreshRates.clear();
86
Ana Krulec4593b692019-01-11 22:07:25 -080087 // This is the rate that HWC encapsulates right now when the device is in DOZE mode.
Ady Abraham1902d072019-03-01 17:18:59 -080088 mRefreshRates.emplace(RefreshRateType::POWER_SAVING,
Alec Mouri0a1cc962019-03-14 12:33:02 -070089 std::make_shared<RefreshRate>(
Ady Abraham796beb02019-04-11 15:23:07 -070090 RefreshRate{SCREEN_OFF_CONFIG_ID, "ScreenOff", 0,
91 HWC2_SCREEN_OFF_CONFIG_ID}));
Ana Krulec4593b692019-01-11 22:07:25 -080092
93 if (configs.size() < 1) {
94 ALOGE("Device does not have valid configs. Config size is 0.");
95 return;
96 }
97
98 // Create a map between config index and vsync period. This is all the info we need
99 // from the configs.
100 std::vector<std::pair<int, nsecs_t>> configIdToVsyncPeriod;
101 for (int i = 0; i < configs.size(); ++i) {
102 configIdToVsyncPeriod.emplace_back(i, configs.at(i)->getVsyncPeriod());
103 }
104
105 std::sort(configIdToVsyncPeriod.begin(), configIdToVsyncPeriod.end(),
106 [](const std::pair<int, nsecs_t>& a, const std::pair<int, nsecs_t>& b) {
107 return a.second > b.second;
108 });
109
110 // When the configs are ordered by the resync rate. We assume that the first one is DEFAULT.
111 nsecs_t vsyncPeriod = configIdToVsyncPeriod[0].second;
112 if (vsyncPeriod != 0) {
113 const float fps = 1e9 / vsyncPeriod;
Ady Abraham796beb02019-04-11 15:23:07 -0700114 const int configId = configIdToVsyncPeriod[0].first;
Ady Abraham1902d072019-03-01 17:18:59 -0800115 mRefreshRates.emplace(RefreshRateType::DEFAULT,
Alec Mouri0a1cc962019-03-14 12:33:02 -0700116 std::make_shared<RefreshRate>(
Ady Abraham796beb02019-04-11 15:23:07 -0700117 RefreshRate{configId, base::StringPrintf("%2.ffps", fps),
118 static_cast<uint32_t>(fps),
119 configs.at(configId)->getId()}));
Ana Krulec4593b692019-01-11 22:07:25 -0800120 }
Alec Mourifb571ea2019-01-24 18:42:10 -0800121
Ana Krulec4593b692019-01-11 22:07:25 -0800122 if (configs.size() < 2) {
123 return;
124 }
125
126 // When the configs are ordered by the resync rate. We assume that the second one is
127 // PERFORMANCE, eg. the higher rate.
128 vsyncPeriod = configIdToVsyncPeriod[1].second;
129 if (vsyncPeriod != 0) {
130 const float fps = 1e9 / vsyncPeriod;
Ady Abraham796beb02019-04-11 15:23:07 -0700131 const int configId = configIdToVsyncPeriod[1].first;
Ady Abraham1902d072019-03-01 17:18:59 -0800132 mRefreshRates.emplace(RefreshRateType::PERFORMANCE,
Alec Mouri0a1cc962019-03-14 12:33:02 -0700133 std::make_shared<RefreshRate>(
Ady Abraham796beb02019-04-11 15:23:07 -0700134 RefreshRate{configId, base::StringPrintf("%2.ffps", fps),
135 static_cast<uint32_t>(fps),
136 configs.at(configId)->getId()}));
Ana Krulec4593b692019-01-11 22:07:25 -0800137 }
138 }
139
Dominik Laskowski22488f62019-03-28 09:53:04 -0700140private:
Ady Abrahamde3b67b2019-03-25 16:38:10 -0700141 std::map<RefreshRateType, std::shared_ptr<RefreshRate>> mRefreshRates;
Ana Krulecb43429d2019-01-09 14:28:51 -0800142};
143
Dominik Laskowski98041832019-08-01 18:35:59 -0700144} // namespace android::scheduler