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