| 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" | 
|  | 26 | #include "Scheduler/SchedulerUtils.h" | 
|  | 27 |  | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 28 | namespace android::scheduler { | 
|  | 29 |  | 
|  | 30 | enum class RefreshRateConfigEvent : unsigned { None = 0b0, Changed = 0b1 }; | 
|  | 31 |  | 
|  | 32 | inline 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 Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 36 |  | 
|  | 37 | /** | 
| Ady Abraham | 1902d07 | 2019-03-01 17:18:59 -0800 | [diff] [blame] | 38 | * 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] | 39 | * about available refresh rates on the device, and the mapping between the numbers and human | 
|  | 40 | * readable names. | 
|  | 41 | */ | 
|  | 42 | class RefreshRateConfigs { | 
|  | 43 | public: | 
|  | 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 Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 50 | // 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 Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 55 | // Refresh rate in frames per second, rounded to the nearest integer. | 
|  | 56 | uint32_t fps = 0; | 
| Ady Abraham | 796beb0 | 2019-04-11 15:23:07 -0700 | [diff] [blame] | 57 | // config Id (returned from HWC2::Display::Config::getId()) | 
|  | 58 | hwc2_config_t id; | 
| Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 59 | }; | 
|  | 60 |  | 
|  | 61 | // TODO(b/122916473): Get this information from configs prepared by vendors, instead of | 
|  | 62 | // baking them in. | 
| Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 63 | const std::map<RefreshRateType, std::shared_ptr<RefreshRate>>& getRefreshRates() const { | 
| Ady Abraham | 1902d07 | 2019-03-01 17:18:59 -0800 | [diff] [blame] | 64 | return mRefreshRates; | 
|  | 65 | } | 
| Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 66 | std::shared_ptr<RefreshRate> getRefreshRate(RefreshRateType type) const { | 
| Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 67 | const auto& refreshRate = mRefreshRates.find(type); | 
|  | 68 | if (refreshRate != mRefreshRates.end()) { | 
|  | 69 | return refreshRate->second; | 
|  | 70 | } | 
|  | 71 | return nullptr; | 
|  | 72 | } | 
| Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 73 |  | 
| Ady Abraham | 796beb0 | 2019-04-11 15:23:07 -0700 | [diff] [blame] | 74 | 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 Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 84 | void populate(const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs) { | 
|  | 85 | mRefreshRates.clear(); | 
|  | 86 |  | 
| Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 87 | // This is the rate that HWC encapsulates right now when the device is in DOZE mode. | 
| Ady Abraham | 1902d07 | 2019-03-01 17:18:59 -0800 | [diff] [blame] | 88 | mRefreshRates.emplace(RefreshRateType::POWER_SAVING, | 
| Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 89 | std::make_shared<RefreshRate>( | 
| Ady Abraham | 796beb0 | 2019-04-11 15:23:07 -0700 | [diff] [blame] | 90 | RefreshRate{SCREEN_OFF_CONFIG_ID, "ScreenOff", 0, | 
|  | 91 | HWC2_SCREEN_OFF_CONFIG_ID})); | 
| Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 92 |  | 
|  | 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 Abraham | 796beb0 | 2019-04-11 15:23:07 -0700 | [diff] [blame] | 114 | const int configId = configIdToVsyncPeriod[0].first; | 
| Ady Abraham | 1902d07 | 2019-03-01 17:18:59 -0800 | [diff] [blame] | 115 | mRefreshRates.emplace(RefreshRateType::DEFAULT, | 
| Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 116 | std::make_shared<RefreshRate>( | 
| Ady Abraham | 796beb0 | 2019-04-11 15:23:07 -0700 | [diff] [blame] | 117 | RefreshRate{configId, base::StringPrintf("%2.ffps", fps), | 
|  | 118 | static_cast<uint32_t>(fps), | 
|  | 119 | configs.at(configId)->getId()})); | 
| Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 120 | } | 
| Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 121 |  | 
| Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 122 | 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 Abraham | 796beb0 | 2019-04-11 15:23:07 -0700 | [diff] [blame] | 131 | const int configId = configIdToVsyncPeriod[1].first; | 
| Ady Abraham | 1902d07 | 2019-03-01 17:18:59 -0800 | [diff] [blame] | 132 | mRefreshRates.emplace(RefreshRateType::PERFORMANCE, | 
| Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 133 | std::make_shared<RefreshRate>( | 
| Ady Abraham | 796beb0 | 2019-04-11 15:23:07 -0700 | [diff] [blame] | 134 | RefreshRate{configId, base::StringPrintf("%2.ffps", fps), | 
|  | 135 | static_cast<uint32_t>(fps), | 
|  | 136 | configs.at(configId)->getId()})); | 
| Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 137 | } | 
|  | 138 | } | 
|  | 139 |  | 
| Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 140 | private: | 
| Ady Abraham | de3b67b | 2019-03-25 16:38:10 -0700 | [diff] [blame] | 141 | std::map<RefreshRateType, std::shared_ptr<RefreshRate>> mRefreshRates; | 
| Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 142 | }; | 
|  | 143 |  | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 144 | } // namespace android::scheduler |