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