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 | |
| 19 | #include <algorithm> |
| 20 | #include <numeric> |
| 21 | |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 22 | #include "android-base/stringprintf.h" |
| 23 | |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 24 | #include "DisplayHardware/HWComposer.h" |
| 25 | #include "Scheduler/SchedulerUtils.h" |
| 26 | |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 27 | namespace android { |
| 28 | namespace scheduler { |
| 29 | |
| 30 | /** |
| 31 | * This class is used to encapsulate configuration for refresh rates. It holds infomation |
| 32 | * about available refresh rates on the device, and the mapping between the numbers and human |
| 33 | * readable names. |
| 34 | */ |
| 35 | class RefreshRateConfigs { |
| 36 | public: |
| 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 { |
| 43 | // Type of the refresh rate. |
| 44 | RefreshRateType type; |
| 45 | // This config ID corresponds to the position of the config in the vector that is stored |
| 46 | // on the device. |
| 47 | int configId; |
| 48 | // Human readable name of the refresh rate. |
| 49 | std::string name; |
| 50 | }; |
| 51 | |
| 52 | // TODO(b/122916473): Get this information from configs prepared by vendors, instead of |
| 53 | // baking them in. |
| 54 | explicit RefreshRateConfigs( |
| 55 | const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs) { |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 56 | init(configs); |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 57 | } |
| 58 | ~RefreshRateConfigs() = default; |
| 59 | |
| 60 | const std::vector<RefreshRate>& getRefreshRates() { return mRefreshRates; } |
| 61 | |
| 62 | private: |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 63 | void init(const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs) { |
| 64 | // This is the rate that HWC encapsulates right now when the device is in DOZE mode. |
| 65 | mRefreshRates.push_back( |
| 66 | RefreshRate{RefreshRateType::POWER_SAVING, SCREEN_OFF_CONFIG_ID, "ScreenOff"}); |
| 67 | |
| 68 | if (configs.size() < 1) { |
| 69 | ALOGE("Device does not have valid configs. Config size is 0."); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | // Create a map between config index and vsync period. This is all the info we need |
| 74 | // from the configs. |
| 75 | std::vector<std::pair<int, nsecs_t>> configIdToVsyncPeriod; |
| 76 | for (int i = 0; i < configs.size(); ++i) { |
| 77 | configIdToVsyncPeriod.emplace_back(i, configs.at(i)->getVsyncPeriod()); |
| 78 | } |
| 79 | |
| 80 | std::sort(configIdToVsyncPeriod.begin(), configIdToVsyncPeriod.end(), |
| 81 | [](const std::pair<int, nsecs_t>& a, const std::pair<int, nsecs_t>& b) { |
| 82 | return a.second > b.second; |
| 83 | }); |
| 84 | |
| 85 | // When the configs are ordered by the resync rate. We assume that the first one is DEFAULT. |
| 86 | nsecs_t vsyncPeriod = configIdToVsyncPeriod[0].second; |
| 87 | if (vsyncPeriod != 0) { |
| 88 | const float fps = 1e9 / vsyncPeriod; |
| 89 | mRefreshRates.push_back(RefreshRate{RefreshRateType::DEFAULT, |
| 90 | configIdToVsyncPeriod[0].first, |
| 91 | base::StringPrintf("%2.ffps", fps)}); |
| 92 | } |
| 93 | if (configs.size() < 2) { |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | // When the configs are ordered by the resync rate. We assume that the second one is |
| 98 | // PERFORMANCE, eg. the higher rate. |
| 99 | vsyncPeriod = configIdToVsyncPeriod[1].second; |
| 100 | if (vsyncPeriod != 0) { |
| 101 | const float fps = 1e9 / vsyncPeriod; |
| 102 | mRefreshRates.push_back(RefreshRate{RefreshRateType::PERFORMANCE, |
| 103 | configIdToVsyncPeriod[1].first, |
| 104 | base::StringPrintf("%2.ffps", fps)}); |
| 105 | } |
| 106 | } |
| 107 | |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 108 | std::vector<RefreshRate> mRefreshRates; |
| 109 | }; |
| 110 | |
| 111 | } // namespace scheduler |
| 112 | } // namespace android |