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: |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 44 | // Enum to indicate which vsync rate to run at. Default is the old 60Hz, and performance |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 45 | // is the new 90Hz. Eventually we want to have a way for vendors to map these in the configs. |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 46 | enum class RefreshRateType { DEFAULT, PERFORMANCE }; |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 47 | |
| 48 | struct RefreshRate { |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 49 | // This config ID corresponds to the position of the config in the vector that is stored |
| 50 | // on the device. |
| 51 | int configId; |
| 52 | // Human readable name of the refresh rate. |
| 53 | std::string name; |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 54 | // Refresh rate in frames per second, rounded to the nearest integer. |
| 55 | uint32_t fps = 0; |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 56 | // Vsync period in nanoseconds. |
| 57 | nsecs_t vsyncPeriod; |
| 58 | // Hwc config Id (returned from HWC2::Display::Config::getId()) |
| 59 | hwc2_config_t hwcId; |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 60 | }; |
| 61 | |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 62 | // Returns true if this device is doing refresh rate switching. This won't change at runtime. |
| 63 | bool refreshRateSwitchingSupported() const { return mRefreshRateSwitchingSupported; } |
| 64 | |
| 65 | // Returns the refresh rate map. This map won't be modified at runtime, so it's safe to access |
| 66 | // from multiple threads. This can only be called if refreshRateSwitching() returns true. |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 67 | // TODO(b/122916473): Get this information from configs prepared by vendors, instead of |
| 68 | // baking them in. |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 69 | const std::map<RefreshRateType, RefreshRate>& getRefreshRateMap() const; |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 70 | |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 71 | const RefreshRate& getRefreshRateFromType(RefreshRateType type) const; |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 72 | |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 73 | std::pair<RefreshRateType, const RefreshRate&> getCurrentRefreshRate() const; |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 74 | |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 75 | const RefreshRate& getRefreshRateFromConfigId(int configId) const; |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 76 | |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 77 | RefreshRateType getRefreshRateTypeFromHwcConfigId(hwc2_config_t hwcId) const; |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 78 | |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 79 | void setCurrentConfig(int config); |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 80 | |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 81 | struct InputConfig { |
| 82 | hwc2_config_t hwcId = 0; |
| 83 | nsecs_t vsyncPeriod = 0; |
| 84 | }; |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 85 | |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 86 | RefreshRateConfigs(bool refreshRateSwitching, const std::vector<InputConfig>& configs, |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 87 | int currentConfig); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 88 | |
| 89 | RefreshRateConfigs(bool refreshRateSwitching, |
| 90 | const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs, |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 91 | int currentConfig); |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 92 | |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 93 | private: |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 94 | void init(bool refreshRateSwitching, const std::vector<InputConfig>& configs, |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 95 | int currentConfig); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 96 | // Whether this device is doing refresh rate switching or not. This must not change after this |
| 97 | // object is initialized. |
| 98 | bool mRefreshRateSwitchingSupported; |
| 99 | // The list of refresh rates, indexed by display config ID. This must not change after this |
| 100 | // object is initialized. |
| 101 | std::vector<RefreshRate> mRefreshRates; |
| 102 | // The mapping of refresh rate type to RefreshRate. This must not change after this object is |
| 103 | // initialized. |
| 104 | std::map<RefreshRateType, RefreshRate> mRefreshRateMap; |
| 105 | // The ID of the current config. This will change at runtime. This is set by SurfaceFlinger on |
| 106 | // the main thread, and read by the Scheduler (and other objects) on other threads, so it's |
| 107 | // atomic. |
| 108 | std::atomic<int> mCurrentConfig; |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 109 | }; |
| 110 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 111 | } // namespace android::scheduler |