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