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