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 | |
| 22 | #include "Scheduler/SchedulerUtils.h" |
| 23 | #include "android-base/stringprintf.h" |
| 24 | |
| 25 | namespace android { |
| 26 | namespace scheduler { |
| 27 | |
| 28 | /** |
| 29 | * This class is used to encapsulate configuration for refresh rates. It holds infomation |
| 30 | * about available refresh rates on the device, and the mapping between the numbers and human |
| 31 | * readable names. |
| 32 | */ |
| 33 | class RefreshRateConfigs { |
| 34 | public: |
| 35 | // Enum to indicate which vsync rate to run at. Power saving is intended to be the lowest |
| 36 | // (eg. when the screen is in AOD mode or off), default is the old 60Hz, and performance |
| 37 | // is the new 90Hz. Eventually we want to have a way for vendors to map these in the configs. |
| 38 | enum class RefreshRateType { POWER_SAVING, DEFAULT, PERFORMANCE }; |
| 39 | |
| 40 | struct RefreshRate { |
| 41 | // Type of the refresh rate. |
| 42 | RefreshRateType type; |
| 43 | // 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; |
| 48 | }; |
| 49 | |
| 50 | // TODO(b/122916473): Get this information from configs prepared by vendors, instead of |
| 51 | // baking them in. |
| 52 | explicit RefreshRateConfigs( |
| 53 | const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs) { |
| 54 | // This is the rate that HWC encapsulates right now when the screen is off. |
| 55 | RefreshRate rate; |
| 56 | rate.type = RefreshRateType::POWER_SAVING; |
| 57 | rate.configId = SCREEN_OFF_CONFIG_ID; |
| 58 | rate.name = "ScreenOff"; |
| 59 | mRefreshRates.push_back(rate); |
| 60 | |
| 61 | if (configs.size() < 1) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | std::vector<std::pair<int, nsecs_t>> configIdToVsyncPeriod; |
| 66 | for (int i = 0; i < configs.size(); ++i) { |
| 67 | configIdToVsyncPeriod.push_back(std::make_pair(i, configs.at(i)->getVsyncPeriod())); |
| 68 | } |
| 69 | std::sort(configIdToVsyncPeriod.begin(), configIdToVsyncPeriod.end(), |
| 70 | [](const std::pair<int, nsecs_t>& a, const std::pair<int, nsecs_t>& b) { |
| 71 | return a.second > b.second; |
| 72 | }); |
| 73 | |
| 74 | nsecs_t vsyncPeriod = configIdToVsyncPeriod.at(0).second; |
| 75 | if (vsyncPeriod != 0) { |
| 76 | const float fps = std::chrono::nanoseconds(1).count() / vsyncPeriod; |
| 77 | rate.type = RefreshRateType::DEFAULT; |
| 78 | rate.configId = configIdToVsyncPeriod.at(0).first; |
| 79 | rate.name = base::StringPrintf("%2.ffps", fps); |
| 80 | mRefreshRates.push_back(rate); |
| 81 | } |
| 82 | if (configs.size() < 2) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | vsyncPeriod = configIdToVsyncPeriod.at(1).second; |
| 87 | if (vsyncPeriod != 0) { |
| 88 | const float fps = std::chrono::nanoseconds(1).count() / vsyncPeriod; |
| 89 | rate.type = RefreshRateType::PERFORMANCE; |
| 90 | rate.configId = configIdToVsyncPeriod.at(1).first; |
| 91 | rate.name = base::StringPrintf("%2.ffps", fps); |
| 92 | mRefreshRates.push_back(rate); |
| 93 | } |
| 94 | |
| 95 | for (auto refreshRate : mRefreshRates) { |
| 96 | ALOGV("type: %d, id: %d, name: %s", refreshRate.type, refreshRate.configId, |
| 97 | refreshRate.name.c_str()); |
| 98 | } |
| 99 | } |
| 100 | ~RefreshRateConfigs() = default; |
| 101 | |
| 102 | const std::vector<RefreshRate>& getRefreshRates() { return mRefreshRates; } |
| 103 | |
| 104 | private: |
| 105 | std::vector<RefreshRate> mRefreshRates; |
| 106 | }; |
| 107 | |
| 108 | } // namespace scheduler |
| 109 | } // namespace android |