Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 17 | #undef LOG_TAG |
| 18 | #define LOG_TAG "Scheduler" |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 19 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 20 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 21 | #include "Scheduler.h" |
| 22 | |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 23 | #include <android-base/properties.h> |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 24 | #include <android-base/stringprintf.h> |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 25 | #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h> |
| 26 | #include <android/hardware/configstore/1.1/ISurfaceFlingerConfigs.h> |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 27 | #include <configstore/Utils.h> |
Ady Abraham | 8f1ee7f | 2019-04-05 10:32:50 -0700 | [diff] [blame] | 28 | #include <input/InputWindow.h> |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 29 | #include <system/window.h> |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 30 | #include <ui/DisplayStatInfo.h> |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 31 | #include <utils/Timers.h> |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 32 | #include <utils/Trace.h> |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 33 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 34 | #include <algorithm> |
| 35 | #include <cinttypes> |
| 36 | #include <cstdint> |
| 37 | #include <functional> |
| 38 | #include <memory> |
| 39 | #include <numeric> |
| 40 | |
| 41 | #include "../Layer.h" |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 42 | #include "DispSync.h" |
| 43 | #include "DispSyncSource.h" |
| 44 | #include "EventThread.h" |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 45 | #include "InjectVSyncSource.h" |
Ana Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 46 | #include "OneShotTimer.h" |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 47 | #include "SchedulerUtils.h" |
Sundong Ahn | d5e08f6 | 2018-12-12 20:27:28 +0900 | [diff] [blame] | 48 | #include "SurfaceFlingerProperties.h" |
Kevin DuBois | 0028738 | 2019-11-19 15:11:55 -0800 | [diff] [blame] | 49 | #include "Timer.h" |
| 50 | #include "VSyncDispatchTimerQueue.h" |
| 51 | #include "VSyncPredictor.h" |
| 52 | #include "VSyncReactor.h" |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 53 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 54 | #define RETURN_IF_INVALID_HANDLE(handle, ...) \ |
| 55 | do { \ |
| 56 | if (mConnections.count(handle) == 0) { \ |
| 57 | ALOGE("Invalid connection handle %" PRIuPTR, handle.id); \ |
| 58 | return __VA_ARGS__; \ |
| 59 | } \ |
| 60 | } while (false) |
| 61 | |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 62 | using namespace std::string_literals; |
| 63 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 64 | namespace android { |
Ady Abraham | 5a85855 | 2020-03-31 17:54:56 -0700 | [diff] [blame] | 65 | |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 66 | namespace { |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 67 | |
Ady Abraham | 5a85855 | 2020-03-31 17:54:56 -0700 | [diff] [blame] | 68 | std::unique_ptr<scheduler::VSyncTracker> createVSyncTracker() { |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 69 | // TODO(b/144707443): Tune constants. |
| 70 | constexpr int kDefaultRate = 60; |
| 71 | constexpr auto initialPeriod = std::chrono::duration<nsecs_t, std::ratio<1, kDefaultRate>>(1); |
| 72 | constexpr nsecs_t idealPeriod = |
| 73 | std::chrono::duration_cast<std::chrono::nanoseconds>(initialPeriod).count(); |
| 74 | constexpr size_t vsyncTimestampHistorySize = 20; |
| 75 | constexpr size_t minimumSamplesForPrediction = 6; |
| 76 | constexpr uint32_t discardOutlierPercent = 20; |
| 77 | return std::make_unique<scheduler::VSyncPredictor>(idealPeriod, vsyncTimestampHistorySize, |
| 78 | minimumSamplesForPrediction, |
| 79 | discardOutlierPercent); |
Ady Abraham | 5a85855 | 2020-03-31 17:54:56 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 82 | std::unique_ptr<scheduler::VSyncDispatch> createVSyncDispatch(scheduler::VSyncTracker& tracker) { |
| 83 | // TODO(b/144707443): Tune constants. |
| 84 | constexpr std::chrono::nanoseconds vsyncMoveThreshold = 3ms; |
| 85 | constexpr std::chrono::nanoseconds timerSlack = 500us; |
Ady Abraham | 5a85855 | 2020-03-31 17:54:56 -0700 | [diff] [blame] | 86 | return std::make_unique< |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 87 | scheduler::VSyncDispatchTimerQueue>(std::make_unique<scheduler::Timer>(), tracker, |
Ady Abraham | 5a85855 | 2020-03-31 17:54:56 -0700 | [diff] [blame] | 88 | timerSlack.count(), vsyncMoveThreshold.count()); |
| 89 | } |
| 90 | |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 91 | const char* toContentDetectionString(bool useContentDetection, bool useContentDetectionV2) { |
| 92 | if (!useContentDetection) return "off"; |
| 93 | return useContentDetectionV2 ? "V2" : "V1"; |
| 94 | } |
| 95 | |
| 96 | } // namespace |
| 97 | |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 98 | Scheduler::Scheduler(const scheduler::RefreshRateConfigs& configs, ISchedulerCallback& callback) |
| 99 | : Scheduler(configs, callback, |
| 100 | {.supportKernelTimer = sysprop::support_kernel_idle_timer(false), |
| 101 | .useContentDetection = sysprop::use_content_detection_for_refresh_rate(false), |
| 102 | .useContentDetectionV2 = |
| 103 | base::GetBoolProperty("debug.sf.use_content_detection_v2"s, true), |
| 104 | // TODO(b/140302863): Remove this and use the vsync_reactor system. |
| 105 | .useVsyncPredictor = base::GetBoolProperty("debug.sf.vsync_reactor"s, true)}) {} |
| 106 | |
| 107 | Scheduler::Scheduler(const scheduler::RefreshRateConfigs& configs, ISchedulerCallback& callback, |
| 108 | Options options) |
| 109 | : Scheduler(createVsyncSchedule(options), configs, callback, |
| 110 | createLayerHistory(configs, options.useContentDetectionV2), options) { |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 111 | using namespace sysprop; |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 112 | |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 113 | const int setIdleTimerMs = base::GetIntProperty("debug.sf.set_idle_timer_ms"s, 0); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 114 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 115 | if (const auto millis = setIdleTimerMs ? setIdleTimerMs : set_idle_timer_ms(0); millis > 0) { |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 116 | const auto callback = mOptions.supportKernelTimer ? &Scheduler::kernelIdleTimerCallback |
| 117 | : &Scheduler::idleTimerCallback; |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 118 | mIdleTimer.emplace( |
| 119 | std::chrono::milliseconds(millis), |
| 120 | [this, callback] { std::invoke(callback, this, TimerState::Reset); }, |
| 121 | [this, callback] { std::invoke(callback, this, TimerState::Expired); }); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 122 | mIdleTimer->start(); |
| 123 | } |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 124 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 125 | if (const int64_t millis = set_touch_timer_ms(0); millis > 0) { |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 126 | // Touch events are coming to SF every 100ms, so the timer needs to be higher than that |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 127 | mTouchTimer.emplace( |
| 128 | std::chrono::milliseconds(millis), |
Dominik Laskowski | dd252cd | 2019-07-26 09:10:16 -0700 | [diff] [blame] | 129 | [this] { touchTimerCallback(TimerState::Reset); }, |
| 130 | [this] { touchTimerCallback(TimerState::Expired); }); |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 131 | mTouchTimer->start(); |
| 132 | } |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 133 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 134 | if (const int64_t millis = set_display_power_timer_ms(0); millis > 0) { |
| 135 | mDisplayPowerTimer.emplace( |
| 136 | std::chrono::milliseconds(millis), |
Dominik Laskowski | dd252cd | 2019-07-26 09:10:16 -0700 | [diff] [blame] | 137 | [this] { displayPowerTimerCallback(TimerState::Reset); }, |
| 138 | [this] { displayPowerTimerCallback(TimerState::Expired); }); |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 139 | mDisplayPowerTimer->start(); |
| 140 | } |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 143 | Scheduler::Scheduler(VsyncSchedule schedule, const scheduler::RefreshRateConfigs& configs, |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 144 | ISchedulerCallback& schedulerCallback, |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 145 | std::unique_ptr<LayerHistory> layerHistory, Options options) |
| 146 | : mOptions(options), |
| 147 | mVsyncSchedule(std::move(schedule)), |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 148 | mLayerHistory(std::move(layerHistory)), |
Ady Abraham | 3a77a7b | 2019-12-02 18:46:59 -0800 | [diff] [blame] | 149 | mSchedulerCallback(schedulerCallback), |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 150 | mRefreshRateConfigs(configs) { |
| 151 | mSchedulerCallback.setVsyncEnabled(false); |
| 152 | } |
Dominik Laskowski | 7c9dbf9 | 2019-08-01 17:57:31 -0700 | [diff] [blame] | 153 | |
Lloyd Pique | 1f9f1a4 | 2019-01-31 13:04:00 -0800 | [diff] [blame] | 154 | Scheduler::~Scheduler() { |
Ana Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 155 | // Ensure the OneShotTimer threads are joined before we start destroying state. |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 156 | mDisplayPowerTimer.reset(); |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 157 | mTouchTimer.reset(); |
Lloyd Pique | 1f9f1a4 | 2019-01-31 13:04:00 -0800 | [diff] [blame] | 158 | mIdleTimer.reset(); |
| 159 | } |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 160 | |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 161 | Scheduler::VsyncSchedule Scheduler::createVsyncSchedule(Options options) { |
| 162 | if (!options.useVsyncPredictor) { |
| 163 | auto sync = std::make_unique<impl::DispSync>("SchedulerDispSync", |
| 164 | sysprop::running_without_sync_framework(true)); |
| 165 | return {std::move(sync), nullptr, nullptr}; |
| 166 | } |
| 167 | |
| 168 | auto clock = std::make_unique<scheduler::SystemClock>(); |
| 169 | auto tracker = createVSyncTracker(); |
| 170 | auto dispatch = createVSyncDispatch(*tracker); |
| 171 | |
| 172 | // TODO(b/144707443): Tune constants. |
| 173 | constexpr size_t pendingFenceLimit = 20; |
| 174 | auto sync = std::make_unique<scheduler::VSyncReactor>(std::move(clock), *dispatch, *tracker, |
| 175 | pendingFenceLimit, |
| 176 | options.supportKernelTimer); |
| 177 | return {std::move(sync), std::move(tracker), std::move(dispatch)}; |
| 178 | } |
| 179 | |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 180 | std::unique_ptr<LayerHistory> Scheduler::createLayerHistory( |
| 181 | const scheduler::RefreshRateConfigs& configs, bool useContentDetectionV2) { |
| 182 | if (!configs.canSwitch()) return nullptr; |
| 183 | |
| 184 | if (useContentDetectionV2) { |
| 185 | return std::make_unique<scheduler::impl::LayerHistoryV2>(configs); |
| 186 | } |
| 187 | |
| 188 | return std::make_unique<scheduler::impl::LayerHistory>(); |
| 189 | } |
| 190 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 191 | DispSync& Scheduler::getPrimaryDispSync() { |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 192 | return *mVsyncSchedule.sync; |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 195 | std::unique_ptr<VSyncSource> Scheduler::makePrimaryDispSyncSource(const char* name, |
| 196 | nsecs_t phaseOffsetNs) { |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 197 | return std::make_unique<DispSyncSource>(&getPrimaryDispSync(), phaseOffsetNs, |
Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 198 | true /* traceVsync */, name); |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 201 | Scheduler::ConnectionHandle Scheduler::createConnection( |
Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 202 | const char* connectionName, nsecs_t phaseOffsetNs, |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 203 | impl::EventThread::InterceptVSyncsCallback interceptCallback) { |
Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 204 | auto vsyncSource = makePrimaryDispSyncSource(connectionName, phaseOffsetNs); |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 205 | auto eventThread = std::make_unique<impl::EventThread>(std::move(vsyncSource), |
| 206 | std::move(interceptCallback)); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 207 | return createConnection(std::move(eventThread)); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 208 | } |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 209 | |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 210 | Scheduler::ConnectionHandle Scheduler::createConnection(std::unique_ptr<EventThread> eventThread) { |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 211 | const ConnectionHandle handle = ConnectionHandle{mNextConnectionHandleId++}; |
| 212 | ALOGV("Creating a connection handle with ID %" PRIuPTR, handle.id); |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 213 | |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 214 | auto connection = |
| 215 | createConnectionInternal(eventThread.get(), ISurfaceComposer::eConfigChangedSuppress); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 216 | |
| 217 | mConnections.emplace(handle, Connection{connection, std::move(eventThread)}); |
| 218 | return handle; |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 221 | sp<EventThreadConnection> Scheduler::createConnectionInternal( |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 222 | EventThread* eventThread, ISurfaceComposer::ConfigChanged configChanged) { |
| 223 | return eventThread->createEventConnection([&] { resync(); }, configChanged); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 226 | sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection( |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 227 | ConnectionHandle handle, ISurfaceComposer::ConfigChanged configChanged) { |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 228 | RETURN_IF_INVALID_HANDLE(handle, nullptr); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 229 | return createConnectionInternal(mConnections[handle].thread.get(), configChanged); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 232 | sp<EventThreadConnection> Scheduler::getEventConnection(ConnectionHandle handle) { |
| 233 | RETURN_IF_INVALID_HANDLE(handle, nullptr); |
| 234 | return mConnections[handle].connection; |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 237 | void Scheduler::onHotplugReceived(ConnectionHandle handle, PhysicalDisplayId displayId, |
| 238 | bool connected) { |
| 239 | RETURN_IF_INVALID_HANDLE(handle); |
| 240 | mConnections[handle].thread->onHotplugReceived(displayId, connected); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 243 | void Scheduler::onScreenAcquired(ConnectionHandle handle) { |
| 244 | RETURN_IF_INVALID_HANDLE(handle); |
| 245 | mConnections[handle].thread->onScreenAcquired(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 248 | void Scheduler::onScreenReleased(ConnectionHandle handle) { |
| 249 | RETURN_IF_INVALID_HANDLE(handle); |
| 250 | mConnections[handle].thread->onScreenReleased(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 253 | void Scheduler::onPrimaryDisplayConfigChanged(ConnectionHandle handle, PhysicalDisplayId displayId, |
| 254 | HwcConfigIndexType configId, nsecs_t vsyncPeriod) { |
| 255 | std::lock_guard<std::mutex> lock(mFeatureStateLock); |
| 256 | // Cache the last reported config for primary display. |
| 257 | mFeatures.cachedConfigChangedParams = {handle, displayId, configId, vsyncPeriod}; |
| 258 | onNonPrimaryDisplayConfigChanged(handle, displayId, configId, vsyncPeriod); |
| 259 | } |
| 260 | |
| 261 | void Scheduler::dispatchCachedReportedConfig() { |
| 262 | const auto configId = *mFeatures.configId; |
| 263 | const auto vsyncPeriod = |
| 264 | mRefreshRateConfigs.getRefreshRateFromConfigId(configId).getVsyncPeriod(); |
| 265 | |
| 266 | // If there is no change from cached config, there is no need to dispatch an event |
| 267 | if (configId == mFeatures.cachedConfigChangedParams->configId && |
| 268 | vsyncPeriod == mFeatures.cachedConfigChangedParams->vsyncPeriod) { |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | mFeatures.cachedConfigChangedParams->configId = configId; |
| 273 | mFeatures.cachedConfigChangedParams->vsyncPeriod = vsyncPeriod; |
| 274 | onNonPrimaryDisplayConfigChanged(mFeatures.cachedConfigChangedParams->handle, |
| 275 | mFeatures.cachedConfigChangedParams->displayId, |
| 276 | mFeatures.cachedConfigChangedParams->configId, |
| 277 | mFeatures.cachedConfigChangedParams->vsyncPeriod); |
| 278 | } |
| 279 | |
| 280 | void Scheduler::onNonPrimaryDisplayConfigChanged(ConnectionHandle handle, |
| 281 | PhysicalDisplayId displayId, |
| 282 | HwcConfigIndexType configId, nsecs_t vsyncPeriod) { |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 283 | RETURN_IF_INVALID_HANDLE(handle); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 284 | mConnections[handle].thread->onConfigChanged(displayId, configId, vsyncPeriod); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 287 | size_t Scheduler::getEventThreadConnectionCount(ConnectionHandle handle) { |
| 288 | RETURN_IF_INVALID_HANDLE(handle, 0); |
| 289 | return mConnections[handle].thread->getEventThreadConnectionCount(); |
| 290 | } |
| 291 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 292 | void Scheduler::dump(ConnectionHandle handle, std::string& result) const { |
| 293 | RETURN_IF_INVALID_HANDLE(handle); |
| 294 | mConnections.at(handle).thread->dump(result); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 297 | void Scheduler::setPhaseOffset(ConnectionHandle handle, nsecs_t phaseOffset) { |
| 298 | RETURN_IF_INVALID_HANDLE(handle); |
| 299 | mConnections[handle].thread->setPhaseOffset(phaseOffset); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 300 | } |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 301 | |
| 302 | void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) { |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 303 | stats->vsyncTime = getPrimaryDispSync().computeNextRefresh(0, systemTime()); |
| 304 | stats->vsyncPeriod = getPrimaryDispSync().getPeriod(); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 307 | Scheduler::ConnectionHandle Scheduler::enableVSyncInjection(bool enable) { |
| 308 | if (mInjectVSyncs == enable) { |
| 309 | return {}; |
| 310 | } |
| 311 | |
| 312 | ALOGV("%s VSYNC injection", enable ? "Enabling" : "Disabling"); |
| 313 | |
| 314 | if (!mInjectorConnectionHandle) { |
| 315 | auto vsyncSource = std::make_unique<InjectVSyncSource>(); |
| 316 | mVSyncInjector = vsyncSource.get(); |
| 317 | |
| 318 | auto eventThread = |
| 319 | std::make_unique<impl::EventThread>(std::move(vsyncSource), |
| 320 | impl::EventThread::InterceptVSyncsCallback()); |
| 321 | |
| 322 | mInjectorConnectionHandle = createConnection(std::move(eventThread)); |
| 323 | } |
| 324 | |
| 325 | mInjectVSyncs = enable; |
| 326 | return mInjectorConnectionHandle; |
| 327 | } |
| 328 | |
Ady Abraham | 5facfb1 | 2020-04-22 15:18:31 -0700 | [diff] [blame] | 329 | bool Scheduler::injectVSync(nsecs_t when, nsecs_t expectedVSyncTime) { |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 330 | if (!mInjectVSyncs || !mVSyncInjector) { |
| 331 | return false; |
| 332 | } |
| 333 | |
Ady Abraham | 5facfb1 | 2020-04-22 15:18:31 -0700 | [diff] [blame] | 334 | mVSyncInjector->onInjectSyncEvent(when, expectedVSyncTime); |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 335 | return true; |
| 336 | } |
| 337 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 338 | void Scheduler::enableHardwareVsync() { |
| 339 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 340 | if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) { |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 341 | getPrimaryDispSync().beginResync(); |
| 342 | mSchedulerCallback.setVsyncEnabled(true); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 343 | mPrimaryHWVsyncEnabled = true; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | void Scheduler::disableHardwareVsync(bool makeUnavailable) { |
| 348 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 349 | if (mPrimaryHWVsyncEnabled) { |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 350 | mSchedulerCallback.setVsyncEnabled(false); |
| 351 | getPrimaryDispSync().endResync(); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 352 | mPrimaryHWVsyncEnabled = false; |
| 353 | } |
| 354 | if (makeUnavailable) { |
| 355 | mHWVsyncAvailable = false; |
| 356 | } |
| 357 | } |
| 358 | |
Ana Krulec | c287042 | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 359 | void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) { |
| 360 | { |
| 361 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 362 | if (makeAvailable) { |
| 363 | mHWVsyncAvailable = makeAvailable; |
| 364 | } else if (!mHWVsyncAvailable) { |
| 365 | // Hardware vsync is not currently available, so abort the resync |
| 366 | // attempt for now |
| 367 | return; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | if (period <= 0) { |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | setVsyncPeriod(period); |
| 376 | } |
| 377 | |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 378 | void Scheduler::resync() { |
Long Ling | 457bef9 | 2019-09-11 14:43:11 -0700 | [diff] [blame] | 379 | static constexpr nsecs_t kIgnoreDelay = ms2ns(750); |
Ana Krulec | c287042 | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 380 | |
| 381 | const nsecs_t now = systemTime(); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 382 | const nsecs_t last = mLastResyncTime.exchange(now); |
Ana Krulec | c287042 | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 383 | |
| 384 | if (now - last > kIgnoreDelay) { |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 385 | resyncToHardwareVsync(false, mRefreshRateConfigs.getCurrentRefreshRate().getVsyncPeriod()); |
Ana Krulec | c287042 | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 389 | void Scheduler::setVsyncPeriod(nsecs_t period) { |
Ady Abraham | 3aff917 | 2019-02-07 19:10:26 -0800 | [diff] [blame] | 390 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 391 | getPrimaryDispSync().setPeriod(period); |
Ady Abraham | 3aff917 | 2019-02-07 19:10:26 -0800 | [diff] [blame] | 392 | |
| 393 | if (!mPrimaryHWVsyncEnabled) { |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 394 | getPrimaryDispSync().beginResync(); |
| 395 | mSchedulerCallback.setVsyncEnabled(true); |
Ady Abraham | 3aff917 | 2019-02-07 19:10:26 -0800 | [diff] [blame] | 396 | mPrimaryHWVsyncEnabled = true; |
| 397 | } |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Ady Abraham | 5dee2f1 | 2020-02-05 17:49:47 -0800 | [diff] [blame] | 400 | void Scheduler::addResyncSample(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod, |
| 401 | bool* periodFlushed) { |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 402 | bool needsHwVsync = false; |
Alec Mouri | f8e689c | 2019-05-20 18:32:22 -0700 | [diff] [blame] | 403 | *periodFlushed = false; |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 404 | { // Scope for the lock |
| 405 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 406 | if (mPrimaryHWVsyncEnabled) { |
Ady Abraham | 5dee2f1 | 2020-02-05 17:49:47 -0800 | [diff] [blame] | 407 | needsHwVsync = |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 408 | getPrimaryDispSync().addResyncSample(timestamp, hwcVsyncPeriod, periodFlushed); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
| 412 | if (needsHwVsync) { |
| 413 | enableHardwareVsync(); |
| 414 | } else { |
| 415 | disableHardwareVsync(false); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) { |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 420 | if (getPrimaryDispSync().addPresentFence(fenceTime)) { |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 421 | enableHardwareVsync(); |
| 422 | } else { |
| 423 | disableHardwareVsync(false); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | void Scheduler::setIgnorePresentFences(bool ignore) { |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 428 | getPrimaryDispSync().setIgnorePresentFences(ignore); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Ady Abraham | 0ed31c9 | 2020-04-16 11:48:45 -0700 | [diff] [blame] | 431 | nsecs_t Scheduler::getDispSyncExpectedPresentTime(nsecs_t now) { |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 432 | return getPrimaryDispSync().expectedPresentTime(now); |
Ady Abraham | c3e2131 | 2019-02-07 14:30:23 -0800 | [diff] [blame] | 433 | } |
| 434 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 435 | void Scheduler::registerLayer(Layer* layer) { |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 436 | if (!mLayerHistory) return; |
| 437 | |
Steven Thomas | debafed | 2020-05-18 17:30:35 -0700 | [diff] [blame] | 438 | const auto minFps = mRefreshRateConfigs.getMinRefreshRate().getFps(); |
| 439 | const auto maxFps = mRefreshRateConfigs.getMaxRefreshRate().getFps(); |
| 440 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 441 | if (layer->getWindowType() == InputWindowInfo::Type::STATUS_BAR) { |
Steven Thomas | debafed | 2020-05-18 17:30:35 -0700 | [diff] [blame] | 442 | mLayerHistory->registerLayer(layer, minFps, maxFps, |
Ana Krulec | 3d367c8 | 2020-02-25 15:02:01 -0800 | [diff] [blame] | 443 | scheduler::LayerHistory::LayerVoteType::NoVote); |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 444 | } else if (!mOptions.useContentDetection) { |
Steven Thomas | debafed | 2020-05-18 17:30:35 -0700 | [diff] [blame] | 445 | // If the content detection feature is off, all layers are registered at Max. We still keep |
| 446 | // the layer history, since we use it for other features (like Frame Rate API), so layers |
| 447 | // still need to be registered. |
| 448 | mLayerHistory->registerLayer(layer, minFps, maxFps, |
| 449 | scheduler::LayerHistory::LayerVoteType::Max); |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 450 | } else if (!mOptions.useContentDetectionV2) { |
Steven Thomas | debafed | 2020-05-18 17:30:35 -0700 | [diff] [blame] | 451 | // In V1 of content detection, all layers are registered as Heuristic (unless it's |
| 452 | // wallpaper). |
| 453 | const auto highFps = |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 454 | layer->getWindowType() == InputWindowInfo::Type::WALLPAPER ? minFps : maxFps; |
Ana Krulec | 3d367c8 | 2020-02-25 15:02:01 -0800 | [diff] [blame] | 455 | |
Steven Thomas | debafed | 2020-05-18 17:30:35 -0700 | [diff] [blame] | 456 | mLayerHistory->registerLayer(layer, minFps, highFps, |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 457 | scheduler::LayerHistory::LayerVoteType::Heuristic); |
| 458 | } else { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 459 | if (layer->getWindowType() == InputWindowInfo::Type::WALLPAPER) { |
Ana Krulec | 3d367c8 | 2020-02-25 15:02:01 -0800 | [diff] [blame] | 460 | // Running Wallpaper at Min is considered as part of content detection. |
Steven Thomas | debafed | 2020-05-18 17:30:35 -0700 | [diff] [blame] | 461 | mLayerHistory->registerLayer(layer, minFps, maxFps, |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 462 | scheduler::LayerHistory::LayerVoteType::Min); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 463 | } else { |
Steven Thomas | debafed | 2020-05-18 17:30:35 -0700 | [diff] [blame] | 464 | mLayerHistory->registerLayer(layer, minFps, maxFps, |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 465 | scheduler::LayerHistory::LayerVoteType::Heuristic); |
| 466 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 467 | } |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 470 | void Scheduler::recordLayerHistory(Layer* layer, nsecs_t presentTime, |
| 471 | LayerHistory::LayerUpdateType updateType) { |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 472 | if (mLayerHistory) { |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 473 | mLayerHistory->record(layer, presentTime, systemTime(), updateType); |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 474 | } |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 475 | } |
| 476 | |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame] | 477 | void Scheduler::setConfigChangePending(bool pending) { |
| 478 | if (mLayerHistory) { |
| 479 | mLayerHistory->setConfigChangePending(pending); |
| 480 | } |
| 481 | } |
| 482 | |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 483 | void Scheduler::chooseRefreshRateForContent() { |
| 484 | if (!mLayerHistory) return; |
| 485 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 486 | ATRACE_CALL(); |
| 487 | |
| 488 | scheduler::LayerHistory::Summary summary = mLayerHistory->summarize(systemTime()); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 489 | HwcConfigIndexType newConfigId; |
Ady Abraham | 6398a0a | 2019-04-18 19:30:44 -0700 | [diff] [blame] | 490 | { |
| 491 | std::lock_guard<std::mutex> lock(mFeatureStateLock); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 492 | if (mFeatures.contentRequirements == summary) { |
Ady Abraham | 6398a0a | 2019-04-18 19:30:44 -0700 | [diff] [blame] | 493 | return; |
| 494 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 495 | mFeatures.contentRequirements = summary; |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 496 | mFeatures.contentDetectionV1 = |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 497 | !summary.empty() ? ContentDetectionState::On : ContentDetectionState::Off; |
| 498 | |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 499 | scheduler::RefreshRateConfigs::GlobalSignals consideredSignals; |
| 500 | newConfigId = calculateRefreshRateConfigIndexType(&consideredSignals); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 501 | if (mFeatures.configId == newConfigId) { |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 502 | // We don't need to change the config, but we might need to send an event |
| 503 | // about a config change, since it was suppressed due to a previous idleConsidered |
| 504 | if (!consideredSignals.idle) { |
| 505 | dispatchCachedReportedConfig(); |
| 506 | } |
Ady Abraham | 6398a0a | 2019-04-18 19:30:44 -0700 | [diff] [blame] | 507 | return; |
| 508 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 509 | mFeatures.configId = newConfigId; |
Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 510 | auto& newRefreshRate = mRefreshRateConfigs.getRefreshRateFromConfigId(newConfigId); |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 511 | mSchedulerCallback.changeRefreshRate(newRefreshRate, |
| 512 | consideredSignals.idle ? ConfigEvent::None |
| 513 | : ConfigEvent::Changed); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 514 | } |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 515 | } |
| 516 | |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 517 | void Scheduler::resetIdleTimer() { |
| 518 | if (mIdleTimer) { |
| 519 | mIdleTimer->reset(); |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 523 | void Scheduler::notifyTouchEvent() { |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 524 | if (mTouchTimer) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 525 | mTouchTimer->reset(); |
Steven Thomas | 540730a | 2020-01-08 20:12:42 -0800 | [diff] [blame] | 526 | |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 527 | if (mOptions.supportKernelTimer && mIdleTimer) { |
Steven Thomas | 540730a | 2020-01-08 20:12:42 -0800 | [diff] [blame] | 528 | mIdleTimer->reset(); |
| 529 | } |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 530 | } |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 533 | void Scheduler::setDisplayPowerState(bool normal) { |
| 534 | { |
| 535 | std::lock_guard<std::mutex> lock(mFeatureStateLock); |
Dominik Laskowski | dd252cd | 2019-07-26 09:10:16 -0700 | [diff] [blame] | 536 | mFeatures.isDisplayPowerStateNormal = normal; |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | if (mDisplayPowerTimer) { |
| 540 | mDisplayPowerTimer->reset(); |
| 541 | } |
| 542 | |
| 543 | // Display Power event will boost the refresh rate to performance. |
| 544 | // Clear Layer History to get fresh FPS detection |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 545 | if (mLayerHistory) { |
| 546 | mLayerHistory->clear(); |
| 547 | } |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 550 | void Scheduler::kernelIdleTimerCallback(TimerState state) { |
| 551 | ATRACE_INT("ExpiredKernelIdleTimer", static_cast<int>(state)); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 552 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 553 | // TODO(145561154): cleanup the kernel idle timer implementation and the refresh rate |
| 554 | // magic number |
Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 555 | const auto& refreshRate = mRefreshRateConfigs.getCurrentRefreshRate(); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 556 | constexpr float FPS_THRESHOLD_FOR_KERNEL_TIMER = 65.0f; |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 557 | if (state == TimerState::Reset && refreshRate.getFps() > FPS_THRESHOLD_FOR_KERNEL_TIMER) { |
Alec Mouri | 7f01518 | 2019-07-11 13:56:22 -0700 | [diff] [blame] | 558 | // If we're not in performance mode then the kernel timer shouldn't do |
| 559 | // anything, as the refresh rate during DPU power collapse will be the |
| 560 | // same. |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 561 | resyncToHardwareVsync(true /* makeAvailable */, refreshRate.getVsyncPeriod()); |
| 562 | } else if (state == TimerState::Expired && |
| 563 | refreshRate.getFps() <= FPS_THRESHOLD_FOR_KERNEL_TIMER) { |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 564 | // Disable HW VSYNC if the timer expired, as we don't need it enabled if |
| 565 | // we're not pushing frames, and if we're in PERFORMANCE mode then we'll |
| 566 | // need to update the DispSync model anyway. |
| 567 | disableHardwareVsync(false /* makeUnavailable */); |
Alec Mouri | dc28b37 | 2019-04-18 21:17:13 -0700 | [diff] [blame] | 568 | } |
Ady Abraham | a09852a | 2020-02-20 14:23:42 -0800 | [diff] [blame] | 569 | |
| 570 | mSchedulerCallback.kernelTimerChanged(state == TimerState::Expired); |
Alec Mouri | dc28b37 | 2019-04-18 21:17:13 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 573 | void Scheduler::idleTimerCallback(TimerState state) { |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 574 | handleTimerStateChanged(&mFeatures.idleTimer, state); |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 575 | ATRACE_INT("ExpiredIdleTimer", static_cast<int>(state)); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 576 | } |
| 577 | |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 578 | void Scheduler::touchTimerCallback(TimerState state) { |
Dominik Laskowski | dd252cd | 2019-07-26 09:10:16 -0700 | [diff] [blame] | 579 | const TouchState touch = state == TimerState::Reset ? TouchState::Active : TouchState::Inactive; |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 580 | // Touch event will boost the refresh rate to performance. |
| 581 | // Clear layer history to get fresh FPS detection. |
| 582 | // NOTE: Instead of checking all the layers, we should be checking the layer |
| 583 | // that is currently on top. b/142507166 will give us this capability. |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 584 | if (handleTimerStateChanged(&mFeatures.touch, touch)) { |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 585 | if (mLayerHistory) { |
| 586 | mLayerHistory->clear(); |
| 587 | } |
Ady Abraham | 1adbb72 | 2020-05-15 11:51:48 -0700 | [diff] [blame] | 588 | } |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 589 | ATRACE_INT("TouchState", static_cast<int>(touch)); |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 592 | void Scheduler::displayPowerTimerCallback(TimerState state) { |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 593 | handleTimerStateChanged(&mFeatures.displayPowerTimer, state); |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 594 | ATRACE_INT("ExpiredDisplayPowerTimer", static_cast<int>(state)); |
Alec Mouri | dc28b37 | 2019-04-18 21:17:13 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 597 | void Scheduler::dump(std::string& result) const { |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 598 | using base::StringAppendF; |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 599 | |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 600 | StringAppendF(&result, "+ Idle timer: %s\n", mIdleTimer ? mIdleTimer->dump().c_str() : "off"); |
Ana Krulec | 3d367c8 | 2020-02-25 15:02:01 -0800 | [diff] [blame] | 601 | StringAppendF(&result, "+ Touch timer: %s\n", |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 602 | mTouchTimer ? mTouchTimer->dump().c_str() : "off"); |
| 603 | StringAppendF(&result, "+ Content detection: %s %s\n\n", |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 604 | toContentDetectionString(mOptions.useContentDetection, |
| 605 | mOptions.useContentDetectionV2), |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 606 | mLayerHistory ? mLayerHistory->dump().c_str() : "(no layer history)"); |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 607 | } |
| 608 | |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 609 | template <class T> |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 610 | bool Scheduler::handleTimerStateChanged(T* currentState, T newState) { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 611 | HwcConfigIndexType newConfigId; |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 612 | scheduler::RefreshRateConfigs::GlobalSignals consideredSignals; |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 613 | { |
| 614 | std::lock_guard<std::mutex> lock(mFeatureStateLock); |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 615 | if (*currentState == newState) { |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 616 | return false; |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 617 | } |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 618 | *currentState = newState; |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 619 | newConfigId = calculateRefreshRateConfigIndexType(&consideredSignals); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 620 | if (mFeatures.configId == newConfigId) { |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 621 | // We don't need to change the config, but we might need to send an event |
| 622 | // about a config change, since it was suppressed due to a previous idleConsidered |
| 623 | if (!consideredSignals.idle) { |
| 624 | dispatchCachedReportedConfig(); |
| 625 | } |
| 626 | return consideredSignals.touch; |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 627 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 628 | mFeatures.configId = newConfigId; |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 629 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 630 | const RefreshRate& newRefreshRate = mRefreshRateConfigs.getRefreshRateFromConfigId(newConfigId); |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 631 | mSchedulerCallback.changeRefreshRate(newRefreshRate, |
| 632 | consideredSignals.idle ? ConfigEvent::None |
| 633 | : ConfigEvent::Changed); |
| 634 | return consideredSignals.touch; |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 635 | } |
| 636 | |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 637 | HwcConfigIndexType Scheduler::calculateRefreshRateConfigIndexType( |
| 638 | scheduler::RefreshRateConfigs::GlobalSignals* consideredSignals) { |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 639 | ATRACE_CALL(); |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 640 | if (consideredSignals) *consideredSignals = {}; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 641 | |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 642 | // If Display Power is not in normal operation we want to be in performance mode. When coming |
| 643 | // back to normal mode, a grace period is given with DisplayPowerTimer. |
Ana Krulec | 3f6a206 | 2020-01-23 15:48:01 -0800 | [diff] [blame] | 644 | if (mDisplayPowerTimer && |
| 645 | (!mFeatures.isDisplayPowerStateNormal || |
| 646 | mFeatures.displayPowerTimer == TimerState::Reset)) { |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 647 | return mRefreshRateConfigs.getMaxRefreshRateByPolicy().getConfigId(); |
Ana Krulec | 3f6a206 | 2020-01-23 15:48:01 -0800 | [diff] [blame] | 648 | } |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 649 | |
Steven Thomas | bb37432 | 2020-04-28 22:47:16 -0700 | [diff] [blame] | 650 | const bool touchActive = mTouchTimer && mFeatures.touch == TouchState::Active; |
| 651 | const bool idle = mIdleTimer && mFeatures.idleTimer == TimerState::Expired; |
| 652 | |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame^] | 653 | if (!mOptions.useContentDetectionV2) { |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 654 | // As long as touch is active we want to be in performance mode. |
Steven Thomas | bb37432 | 2020-04-28 22:47:16 -0700 | [diff] [blame] | 655 | if (touchActive) { |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 656 | return mRefreshRateConfigs.getMaxRefreshRateByPolicy().getConfigId(); |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 657 | } |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 658 | |
Steven Thomas | bb37432 | 2020-04-28 22:47:16 -0700 | [diff] [blame] | 659 | // If timer has expired as it means there is no new content on the screen. |
| 660 | if (idle) { |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 661 | if (consideredSignals) consideredSignals->idle = true; |
Steven Thomas | bb37432 | 2020-04-28 22:47:16 -0700 | [diff] [blame] | 662 | return mRefreshRateConfigs.getMinRefreshRateByPolicy().getConfigId(); |
| 663 | } |
Ady Abraham | a315ce7 | 2019-04-24 14:35:20 -0700 | [diff] [blame] | 664 | |
Ana Krulec | 3f6a206 | 2020-01-23 15:48:01 -0800 | [diff] [blame] | 665 | // If content detection is off we choose performance as we don't know the content fps. |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 666 | if (mFeatures.contentDetectionV1 == ContentDetectionState::Off) { |
Ana Krulec | 3803b8d | 2020-02-03 16:35:46 -0800 | [diff] [blame] | 667 | // NOTE: V1 always calls this, but this is not a default behavior for V2. |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 668 | return mRefreshRateConfigs.getMaxRefreshRateByPolicy().getConfigId(); |
Steven Thomas | 540730a | 2020-01-08 20:12:42 -0800 | [diff] [blame] | 669 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 670 | |
| 671 | // Content detection is on, find the appropriate refresh rate with minimal error |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 672 | return mRefreshRateConfigs.getRefreshRateForContent(mFeatures.contentRequirements) |
| 673 | .getConfigId(); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Ady Abraham | 1adbb72 | 2020-05-15 11:51:48 -0700 | [diff] [blame] | 676 | return mRefreshRateConfigs |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 677 | .getBestRefreshRate(mFeatures.contentRequirements, {.touch = touchActive, .idle = idle}, |
| 678 | consideredSignals) |
Ady Abraham | 1adbb72 | 2020-05-15 11:51:48 -0700 | [diff] [blame] | 679 | .getConfigId(); |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 680 | } |
| 681 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 682 | std::optional<HwcConfigIndexType> Scheduler::getPreferredConfigId() { |
Daniel Solomon | 0f0ddc1 | 2019-08-19 19:31:09 -0700 | [diff] [blame] | 683 | std::lock_guard<std::mutex> lock(mFeatureStateLock); |
Ana Krulec | 3f6a206 | 2020-01-23 15:48:01 -0800 | [diff] [blame] | 684 | // Make sure that the default config ID is first updated, before returned. |
| 685 | if (mFeatures.configId.has_value()) { |
Ana Krulec | 3803b8d | 2020-02-03 16:35:46 -0800 | [diff] [blame] | 686 | mFeatures.configId = calculateRefreshRateConfigIndexType(); |
Ana Krulec | 3f6a206 | 2020-01-23 15:48:01 -0800 | [diff] [blame] | 687 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 688 | return mFeatures.configId; |
Daniel Solomon | 0f0ddc1 | 2019-08-19 19:31:09 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 691 | void Scheduler::onNewVsyncPeriodChangeTimeline(const hal::VsyncPeriodChangeTimeline& timeline) { |
Ady Abraham | 3a77a7b | 2019-12-02 18:46:59 -0800 | [diff] [blame] | 692 | if (timeline.refreshRequired) { |
| 693 | mSchedulerCallback.repaintEverythingForHWC(); |
| 694 | } |
| 695 | |
| 696 | std::lock_guard<std::mutex> lock(mVsyncTimelineLock); |
| 697 | mLastVsyncPeriodChangeTimeline = std::make_optional(timeline); |
| 698 | |
| 699 | const auto maxAppliedTime = systemTime() + MAX_VSYNC_APPLIED_TIME.count(); |
| 700 | if (timeline.newVsyncAppliedTimeNanos > maxAppliedTime) { |
| 701 | mLastVsyncPeriodChangeTimeline->newVsyncAppliedTimeNanos = maxAppliedTime; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | void Scheduler::onDisplayRefreshed(nsecs_t timestamp) { |
| 706 | bool callRepaint = false; |
| 707 | { |
| 708 | std::lock_guard<std::mutex> lock(mVsyncTimelineLock); |
| 709 | if (mLastVsyncPeriodChangeTimeline && mLastVsyncPeriodChangeTimeline->refreshRequired) { |
| 710 | if (mLastVsyncPeriodChangeTimeline->refreshTimeNanos < timestamp) { |
| 711 | mLastVsyncPeriodChangeTimeline->refreshRequired = false; |
| 712 | } else { |
| 713 | // We need to send another refresh as refreshTimeNanos is still in the future |
| 714 | callRepaint = true; |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | if (callRepaint) { |
| 720 | mSchedulerCallback.repaintEverythingForHWC(); |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 724 | void Scheduler::onPrimaryDisplayAreaChanged(uint32_t displayArea) { |
| 725 | if (mLayerHistory) { |
| 726 | mLayerHistory->setDisplayArea(displayArea); |
| 727 | } |
| 728 | } |
| 729 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 730 | } // namespace android |