blob: 90bba24332381811eb6c7dffd7958b0e710c0e56 [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
Dominik Laskowski98041832019-08-01 18:35:59 -070019#include <android-base/stringprintf.h>
20
Ana Krulecb43429d2019-01-09 14:28:51 -080021#include <algorithm>
22#include <numeric>
Dominik Laskowski98041832019-08-01 18:35:59 -070023#include <type_traits>
Ana Krulecb43429d2019-01-09 14:28:51 -080024
Ana Krulec4593b692019-01-11 22:07:25 -080025#include "DisplayHardware/HWComposer.h"
26#include "Scheduler/SchedulerUtils.h"
27
Dominik Laskowski98041832019-08-01 18:35:59 -070028namespace android::scheduler {
29
30enum class RefreshRateConfigEvent : unsigned { None = 0b0, Changed = 0b1 };
31
32inline 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 Krulecb43429d2019-01-09 14:28:51 -080036
37/**
Ady Abraham1902d072019-03-01 17:18:59 -080038 * This class is used to encapsulate configuration for refresh rates. It holds information
Ana Krulecb43429d2019-01-09 14:28:51 -080039 * about available refresh rates on the device, and the mapping between the numbers and human
40 * readable names.
41 */
42class RefreshRateConfigs {
43public:
Steven Thomas2bbaabe2019-08-28 16:08:35 -070044 // Enum to indicate which vsync rate to run at. Default is the old 60Hz, and performance
Ana Krulecb43429d2019-01-09 14:28:51 -080045 // is the new 90Hz. Eventually we want to have a way for vendors to map these in the configs.
Steven Thomas2bbaabe2019-08-28 16:08:35 -070046 enum class RefreshRateType { DEFAULT, PERFORMANCE };
Ana Krulecb43429d2019-01-09 14:28:51 -080047
48 struct RefreshRate {
Ana Krulecb43429d2019-01-09 14:28:51 -080049 // 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 Mourifb571ea2019-01-24 18:42:10 -080054 // Refresh rate in frames per second, rounded to the nearest integer.
55 uint32_t fps = 0;
Steven Thomas2bbaabe2019-08-28 16:08:35 -070056 // Vsync period in nanoseconds.
57 nsecs_t vsyncPeriod;
58 // Hwc config Id (returned from HWC2::Display::Config::getId())
59 hwc2_config_t hwcId;
Ana Krulecb43429d2019-01-09 14:28:51 -080060 };
61
Steven Thomas2bbaabe2019-08-28 16:08:35 -070062 // 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 Krulecb43429d2019-01-09 14:28:51 -080067 // TODO(b/122916473): Get this information from configs prepared by vendors, instead of
68 // baking them in.
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080069 const std::map<RefreshRateType, RefreshRate>& getRefreshRateMap() const;
Ana Krulecb43429d2019-01-09 14:28:51 -080070
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080071 const RefreshRate& getRefreshRateFromType(RefreshRateType type) const;
Steven Thomas2bbaabe2019-08-28 16:08:35 -070072
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080073 std::pair<RefreshRateType, const RefreshRate&> getCurrentRefreshRate() const;
Steven Thomas2bbaabe2019-08-28 16:08:35 -070074
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080075 const RefreshRate& getRefreshRateFromConfigId(int configId) const;
Steven Thomas2bbaabe2019-08-28 16:08:35 -070076
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080077 RefreshRateType getRefreshRateTypeFromHwcConfigId(hwc2_config_t hwcId) const;
Steven Thomas2bbaabe2019-08-28 16:08:35 -070078
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080079 void setCurrentConfig(int config);
Dominik Laskowski22488f62019-03-28 09:53:04 -070080
Steven Thomas2bbaabe2019-08-28 16:08:35 -070081 struct InputConfig {
82 hwc2_config_t hwcId = 0;
83 nsecs_t vsyncPeriod = 0;
84 };
Ana Krulec4593b692019-01-11 22:07:25 -080085
Steven Thomas2bbaabe2019-08-28 16:08:35 -070086 RefreshRateConfigs(bool refreshRateSwitching, const std::vector<InputConfig>& configs,
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080087 int currentConfig);
Steven Thomas2bbaabe2019-08-28 16:08:35 -070088
89 RefreshRateConfigs(bool refreshRateSwitching,
90 const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs,
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080091 int currentConfig);
Ana Krulec4593b692019-01-11 22:07:25 -080092
Dominik Laskowski22488f62019-03-28 09:53:04 -070093private:
Steven Thomas2bbaabe2019-08-28 16:08:35 -070094 void init(bool refreshRateSwitching, const std::vector<InputConfig>& configs,
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080095 int currentConfig);
Steven Thomas2bbaabe2019-08-28 16:08:35 -070096 // 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 Krulecb43429d2019-01-09 14:28:51 -0800109};
110
Dominik Laskowski98041832019-08-01 18:35:59 -0700111} // namespace android::scheduler