blob: eb9ae35cecd2aa80a45189eb87e456c60d2e605e [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
19#include <algorithm>
20#include <numeric>
21
Ana Krulecb43429d2019-01-09 14:28:51 -080022#include "android-base/stringprintf.h"
23
Ana Krulec4593b692019-01-11 22:07:25 -080024#include "DisplayHardware/HWComposer.h"
25#include "Scheduler/SchedulerUtils.h"
26
Ana Krulecb43429d2019-01-09 14:28:51 -080027namespace android {
28namespace scheduler {
29
30/**
Ady Abraham1902d072019-03-01 17:18:59 -080031 * This class is used to encapsulate configuration for refresh rates. It holds information
Ana Krulecb43429d2019-01-09 14:28:51 -080032 * about available refresh rates on the device, and the mapping between the numbers and human
33 * readable names.
34 */
35class RefreshRateConfigs {
36public:
37 // Enum to indicate which vsync rate to run at. Power saving is intended to be the lowest
38 // (eg. when the screen is in AOD mode or off), default is the old 60Hz, and performance
39 // is the new 90Hz. Eventually we want to have a way for vendors to map these in the configs.
40 enum class RefreshRateType { POWER_SAVING, DEFAULT, PERFORMANCE };
41
42 struct RefreshRate {
Ana Krulecb43429d2019-01-09 14:28:51 -080043 // This config ID corresponds to the position of the config in the vector that is stored
44 // on the device.
45 int configId;
46 // Human readable name of the refresh rate.
47 std::string name;
Alec Mourifb571ea2019-01-24 18:42:10 -080048 // Refresh rate in frames per second, rounded to the nearest integer.
49 uint32_t fps = 0;
Ana Krulecb43429d2019-01-09 14:28:51 -080050 };
51
52 // TODO(b/122916473): Get this information from configs prepared by vendors, instead of
53 // baking them in.
Dominik Laskowski22488f62019-03-28 09:53:04 -070054 const std::map<RefreshRateType, std::shared_ptr<RefreshRate>>& getRefreshRates() const {
Ady Abraham1902d072019-03-01 17:18:59 -080055 return mRefreshRates;
56 }
Ady Abraham09bd3922019-04-08 10:44:56 -070057 std::shared_ptr<RefreshRate> getRefreshRate(RefreshRateType type) const {
Alec Mouri0a1cc962019-03-14 12:33:02 -070058 const auto& refreshRate = mRefreshRates.find(type);
59 if (refreshRate != mRefreshRates.end()) {
60 return refreshRate->second;
61 }
62 return nullptr;
63 }
Ana Krulecb43429d2019-01-09 14:28:51 -080064
Dominik Laskowski22488f62019-03-28 09:53:04 -070065 void populate(const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs) {
66 mRefreshRates.clear();
67
Ana Krulec4593b692019-01-11 22:07:25 -080068 // This is the rate that HWC encapsulates right now when the device is in DOZE mode.
Ady Abraham1902d072019-03-01 17:18:59 -080069 mRefreshRates.emplace(RefreshRateType::POWER_SAVING,
Alec Mouri0a1cc962019-03-14 12:33:02 -070070 std::make_shared<RefreshRate>(
71 RefreshRate{SCREEN_OFF_CONFIG_ID, "ScreenOff", 0}));
Ana Krulec4593b692019-01-11 22:07:25 -080072
73 if (configs.size() < 1) {
74 ALOGE("Device does not have valid configs. Config size is 0.");
75 return;
76 }
77
78 // Create a map between config index and vsync period. This is all the info we need
79 // from the configs.
80 std::vector<std::pair<int, nsecs_t>> configIdToVsyncPeriod;
81 for (int i = 0; i < configs.size(); ++i) {
82 configIdToVsyncPeriod.emplace_back(i, configs.at(i)->getVsyncPeriod());
83 }
84
85 std::sort(configIdToVsyncPeriod.begin(), configIdToVsyncPeriod.end(),
86 [](const std::pair<int, nsecs_t>& a, const std::pair<int, nsecs_t>& b) {
87 return a.second > b.second;
88 });
89
90 // When the configs are ordered by the resync rate. We assume that the first one is DEFAULT.
91 nsecs_t vsyncPeriod = configIdToVsyncPeriod[0].second;
92 if (vsyncPeriod != 0) {
93 const float fps = 1e9 / vsyncPeriod;
Ady Abraham1902d072019-03-01 17:18:59 -080094 mRefreshRates.emplace(RefreshRateType::DEFAULT,
Alec Mouri0a1cc962019-03-14 12:33:02 -070095 std::make_shared<RefreshRate>(
96 RefreshRate{configIdToVsyncPeriod[0].first,
97 base::StringPrintf("%2.ffps", fps),
98 static_cast<uint32_t>(fps)}));
Ana Krulec4593b692019-01-11 22:07:25 -080099 }
Alec Mourifb571ea2019-01-24 18:42:10 -0800100
Ana Krulec4593b692019-01-11 22:07:25 -0800101 if (configs.size() < 2) {
102 return;
103 }
104
105 // When the configs are ordered by the resync rate. We assume that the second one is
106 // PERFORMANCE, eg. the higher rate.
107 vsyncPeriod = configIdToVsyncPeriod[1].second;
108 if (vsyncPeriod != 0) {
109 const float fps = 1e9 / vsyncPeriod;
Ady Abraham1902d072019-03-01 17:18:59 -0800110 mRefreshRates.emplace(RefreshRateType::PERFORMANCE,
Alec Mouri0a1cc962019-03-14 12:33:02 -0700111 std::make_shared<RefreshRate>(
112 RefreshRate{configIdToVsyncPeriod[1].first,
113 base::StringPrintf("%2.ffps", fps),
114 static_cast<uint32_t>(fps)}));
Ana Krulec4593b692019-01-11 22:07:25 -0800115 }
116 }
117
Dominik Laskowski22488f62019-03-28 09:53:04 -0700118private:
Ady Abrahamde3b67b2019-03-25 16:38:10 -0700119 std::map<RefreshRateType, std::shared_ptr<RefreshRate>> mRefreshRates;
Ana Krulecb43429d2019-01-09 14:28:51 -0800120};
121
122} // namespace scheduler
Alec Mourifb571ea2019-01-24 18:42:10 -0800123} // namespace android