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