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 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 215 | bool Scheduler::isVsyncValid(nsecs_t expectedVsyncTimestamp, uid_t uid) const { |
| 216 | const auto divider = mRefreshRateConfigs.getRefreshRateDividerForUid(uid); |
| 217 | if (divider <= 1) { |
| 218 | return true; |
| 219 | } |
| 220 | |
| 221 | return mVsyncSchedule.tracker->isVSyncInPhase(expectedVsyncTimestamp, divider); |
| 222 | } |
| 223 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 224 | Scheduler::ConnectionHandle Scheduler::createConnection( |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 225 | const char* connectionName, frametimeline::TokenManager* tokenManager, |
| 226 | std::chrono::nanoseconds workDuration, std::chrono::nanoseconds readyDuration, |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 227 | impl::EventThread::InterceptVSyncsCallback interceptCallback) { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 228 | auto vsyncSource = makePrimaryDispSyncSource(connectionName, workDuration, readyDuration); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 229 | auto throttleVsync = [this](nsecs_t expectedVsyncTimestamp, uid_t uid) { |
| 230 | return !isVsyncValid(expectedVsyncTimestamp, uid); |
| 231 | }; |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 232 | auto eventThread = std::make_unique<impl::EventThread>(std::move(vsyncSource), tokenManager, |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 233 | std::move(interceptCallback), |
| 234 | std::move(throttleVsync)); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 235 | return createConnection(std::move(eventThread)); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 236 | } |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 237 | |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 238 | Scheduler::ConnectionHandle Scheduler::createConnection(std::unique_ptr<EventThread> eventThread) { |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 239 | const ConnectionHandle handle = ConnectionHandle{mNextConnectionHandleId++}; |
| 240 | ALOGV("Creating a connection handle with ID %" PRIuPTR, handle.id); |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 241 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame^] | 242 | auto connection = createConnectionInternal(eventThread.get()); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 243 | |
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 | mConnections.emplace(handle, Connection{connection, std::move(eventThread)}); |
| 246 | return handle; |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 249 | sp<EventThreadConnection> Scheduler::createConnectionInternal( |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame^] | 250 | EventThread* eventThread, ISurfaceComposer::EventRegistrationFlags eventRegistration) { |
| 251 | return eventThread->createEventConnection([&] { resync(); }, eventRegistration); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 254 | sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection( |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame^] | 255 | ConnectionHandle handle, ISurfaceComposer::EventRegistrationFlags eventRegistration) { |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 256 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 257 | RETURN_IF_INVALID_HANDLE(handle, nullptr); |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame^] | 258 | return createConnectionInternal(mConnections[handle].thread.get(), eventRegistration); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 261 | sp<EventThreadConnection> Scheduler::getEventConnection(ConnectionHandle handle) { |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 262 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 263 | RETURN_IF_INVALID_HANDLE(handle, nullptr); |
| 264 | return mConnections[handle].connection; |
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::onHotplugReceived(ConnectionHandle handle, PhysicalDisplayId displayId, |
| 268 | bool connected) { |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 269 | android::EventThread* thread; |
| 270 | { |
| 271 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
| 272 | RETURN_IF_INVALID_HANDLE(handle); |
| 273 | thread = mConnections[handle].thread.get(); |
| 274 | } |
| 275 | |
| 276 | thread->onHotplugReceived(displayId, connected); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 279 | void Scheduler::onScreenAcquired(ConnectionHandle handle) { |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 280 | android::EventThread* thread; |
| 281 | { |
| 282 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
| 283 | RETURN_IF_INVALID_HANDLE(handle); |
| 284 | thread = mConnections[handle].thread.get(); |
| 285 | } |
| 286 | thread->onScreenAcquired(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 289 | void Scheduler::onScreenReleased(ConnectionHandle handle) { |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 290 | android::EventThread* thread; |
| 291 | { |
| 292 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
| 293 | RETURN_IF_INVALID_HANDLE(handle); |
| 294 | thread = mConnections[handle].thread.get(); |
| 295 | } |
| 296 | thread->onScreenReleased(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame^] | 299 | void Scheduler::onFrameRateOverridesChanged(ConnectionHandle handle, PhysicalDisplayId displayId, |
| 300 | std::vector<FrameRateOverride> overrides) { |
| 301 | android::EventThread* thread; |
| 302 | { |
| 303 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
| 304 | RETURN_IF_INVALID_HANDLE(handle); |
| 305 | thread = mConnections[handle].thread.get(); |
| 306 | } |
| 307 | thread->onFrameRateOverridesChanged(displayId, std::move(overrides)); |
| 308 | } |
| 309 | |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 310 | void Scheduler::onPrimaryDisplayConfigChanged(ConnectionHandle handle, PhysicalDisplayId displayId, |
| 311 | HwcConfigIndexType configId, nsecs_t vsyncPeriod) { |
| 312 | std::lock_guard<std::mutex> lock(mFeatureStateLock); |
| 313 | // Cache the last reported config for primary display. |
| 314 | mFeatures.cachedConfigChangedParams = {handle, displayId, configId, vsyncPeriod}; |
| 315 | onNonPrimaryDisplayConfigChanged(handle, displayId, configId, vsyncPeriod); |
| 316 | } |
| 317 | |
| 318 | void Scheduler::dispatchCachedReportedConfig() { |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 319 | // Check optional fields first. |
| 320 | if (!mFeatures.configId.has_value()) { |
| 321 | ALOGW("No config ID found, not dispatching cached config."); |
| 322 | return; |
| 323 | } |
| 324 | if (!mFeatures.cachedConfigChangedParams.has_value()) { |
| 325 | ALOGW("No config changed params found, not dispatching cached config."); |
| 326 | return; |
| 327 | } |
| 328 | |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 329 | const auto configId = *mFeatures.configId; |
| 330 | const auto vsyncPeriod = |
| 331 | mRefreshRateConfigs.getRefreshRateFromConfigId(configId).getVsyncPeriod(); |
| 332 | |
| 333 | // If there is no change from cached config, there is no need to dispatch an event |
| 334 | if (configId == mFeatures.cachedConfigChangedParams->configId && |
| 335 | vsyncPeriod == mFeatures.cachedConfigChangedParams->vsyncPeriod) { |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | mFeatures.cachedConfigChangedParams->configId = configId; |
| 340 | mFeatures.cachedConfigChangedParams->vsyncPeriod = vsyncPeriod; |
| 341 | onNonPrimaryDisplayConfigChanged(mFeatures.cachedConfigChangedParams->handle, |
| 342 | mFeatures.cachedConfigChangedParams->displayId, |
| 343 | mFeatures.cachedConfigChangedParams->configId, |
| 344 | mFeatures.cachedConfigChangedParams->vsyncPeriod); |
| 345 | } |
| 346 | |
| 347 | void Scheduler::onNonPrimaryDisplayConfigChanged(ConnectionHandle handle, |
| 348 | PhysicalDisplayId displayId, |
| 349 | HwcConfigIndexType configId, nsecs_t vsyncPeriod) { |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 350 | android::EventThread* thread; |
| 351 | { |
| 352 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
| 353 | RETURN_IF_INVALID_HANDLE(handle); |
| 354 | thread = mConnections[handle].thread.get(); |
| 355 | } |
| 356 | thread->onConfigChanged(displayId, configId, vsyncPeriod); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 359 | size_t Scheduler::getEventThreadConnectionCount(ConnectionHandle handle) { |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 360 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 361 | RETURN_IF_INVALID_HANDLE(handle, 0); |
| 362 | return mConnections[handle].thread->getEventThreadConnectionCount(); |
| 363 | } |
| 364 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 365 | void Scheduler::dump(ConnectionHandle handle, std::string& result) const { |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 366 | android::EventThread* thread; |
| 367 | { |
| 368 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
| 369 | RETURN_IF_INVALID_HANDLE(handle); |
| 370 | thread = mConnections.at(handle).thread.get(); |
| 371 | } |
| 372 | thread->dump(result); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 375 | void Scheduler::setDuration(ConnectionHandle handle, std::chrono::nanoseconds workDuration, |
| 376 | std::chrono::nanoseconds readyDuration) { |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 377 | android::EventThread* thread; |
| 378 | { |
| 379 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
| 380 | RETURN_IF_INVALID_HANDLE(handle); |
| 381 | thread = mConnections[handle].thread.get(); |
| 382 | } |
| 383 | thread->setDuration(workDuration, readyDuration); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 384 | } |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 385 | |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 386 | void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats, nsecs_t now) { |
| 387 | stats->vsyncTime = mVsyncSchedule.tracker->nextAnticipatedVSyncTimeFrom(now); |
| 388 | stats->vsyncPeriod = mVsyncSchedule.tracker->currentPeriod(); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 391 | Scheduler::ConnectionHandle Scheduler::enableVSyncInjection(bool enable) { |
| 392 | if (mInjectVSyncs == enable) { |
| 393 | return {}; |
| 394 | } |
| 395 | |
| 396 | ALOGV("%s VSYNC injection", enable ? "Enabling" : "Disabling"); |
| 397 | |
| 398 | if (!mInjectorConnectionHandle) { |
| 399 | auto vsyncSource = std::make_unique<InjectVSyncSource>(); |
| 400 | mVSyncInjector = vsyncSource.get(); |
| 401 | |
| 402 | auto eventThread = |
| 403 | std::make_unique<impl::EventThread>(std::move(vsyncSource), |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 404 | /*tokenManager=*/nullptr, |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 405 | impl::EventThread::InterceptVSyncsCallback(), |
| 406 | impl::EventThread::ThrottleVsyncCallback()); |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 407 | |
| 408 | mInjectorConnectionHandle = createConnection(std::move(eventThread)); |
| 409 | } |
| 410 | |
| 411 | mInjectVSyncs = enable; |
| 412 | return mInjectorConnectionHandle; |
| 413 | } |
| 414 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 415 | bool Scheduler::injectVSync(nsecs_t when, nsecs_t expectedVSyncTime, nsecs_t deadlineTimestamp) { |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 416 | if (!mInjectVSyncs || !mVSyncInjector) { |
| 417 | return false; |
| 418 | } |
| 419 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 420 | mVSyncInjector->onInjectSyncEvent(when, expectedVSyncTime, deadlineTimestamp); |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 421 | return true; |
| 422 | } |
| 423 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 424 | void Scheduler::enableHardwareVsync() { |
| 425 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 426 | if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 427 | mVsyncSchedule.tracker->resetModel(); |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 428 | mSchedulerCallback.setVsyncEnabled(true); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 429 | mPrimaryHWVsyncEnabled = true; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | void Scheduler::disableHardwareVsync(bool makeUnavailable) { |
| 434 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 435 | if (mPrimaryHWVsyncEnabled) { |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 436 | mSchedulerCallback.setVsyncEnabled(false); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 437 | mPrimaryHWVsyncEnabled = false; |
| 438 | } |
| 439 | if (makeUnavailable) { |
| 440 | mHWVsyncAvailable = false; |
| 441 | } |
| 442 | } |
| 443 | |
Ana Krulec | c287042 | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 444 | void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) { |
| 445 | { |
| 446 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 447 | if (makeAvailable) { |
| 448 | mHWVsyncAvailable = makeAvailable; |
| 449 | } else if (!mHWVsyncAvailable) { |
| 450 | // Hardware vsync is not currently available, so abort the resync |
| 451 | // attempt for now |
| 452 | return; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | if (period <= 0) { |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | setVsyncPeriod(period); |
| 461 | } |
| 462 | |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 463 | void Scheduler::resync() { |
Long Ling | 457bef9 | 2019-09-11 14:43:11 -0700 | [diff] [blame] | 464 | static constexpr nsecs_t kIgnoreDelay = ms2ns(750); |
Ana Krulec | c287042 | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 465 | |
| 466 | const nsecs_t now = systemTime(); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 467 | const nsecs_t last = mLastResyncTime.exchange(now); |
Ana Krulec | c287042 | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 468 | |
| 469 | if (now - last > kIgnoreDelay) { |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 470 | resyncToHardwareVsync(false, mRefreshRateConfigs.getCurrentRefreshRate().getVsyncPeriod()); |
Ana Krulec | c287042 | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 474 | void Scheduler::setVsyncPeriod(nsecs_t period) { |
Ady Abraham | 3aff917 | 2019-02-07 19:10:26 -0800 | [diff] [blame] | 475 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 476 | mVsyncSchedule.controller->startPeriodTransition(period); |
Ady Abraham | 3aff917 | 2019-02-07 19:10:26 -0800 | [diff] [blame] | 477 | |
| 478 | if (!mPrimaryHWVsyncEnabled) { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 479 | mVsyncSchedule.tracker->resetModel(); |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 480 | mSchedulerCallback.setVsyncEnabled(true); |
Ady Abraham | 3aff917 | 2019-02-07 19:10:26 -0800 | [diff] [blame] | 481 | mPrimaryHWVsyncEnabled = true; |
| 482 | } |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Ady Abraham | 5dee2f1 | 2020-02-05 17:49:47 -0800 | [diff] [blame] | 485 | void Scheduler::addResyncSample(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod, |
| 486 | bool* periodFlushed) { |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 487 | bool needsHwVsync = false; |
Alec Mouri | f8e689c | 2019-05-20 18:32:22 -0700 | [diff] [blame] | 488 | *periodFlushed = false; |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 489 | { // Scope for the lock |
| 490 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 491 | if (mPrimaryHWVsyncEnabled) { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 492 | needsHwVsync = mVsyncSchedule.controller->addHwVsyncTimestamp(timestamp, hwcVsyncPeriod, |
| 493 | periodFlushed); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
| 497 | if (needsHwVsync) { |
| 498 | enableHardwareVsync(); |
| 499 | } else { |
| 500 | disableHardwareVsync(false); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 505 | if (mVsyncSchedule.controller->addPresentFence(fenceTime)) { |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 506 | enableHardwareVsync(); |
| 507 | } else { |
| 508 | disableHardwareVsync(false); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | void Scheduler::setIgnorePresentFences(bool ignore) { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 513 | mVsyncSchedule.controller->setIgnorePresentFences(ignore); |
Ady Abraham | c3e2131 | 2019-02-07 14:30:23 -0800 | [diff] [blame] | 514 | } |
| 515 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 516 | void Scheduler::registerLayer(Layer* layer) { |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 517 | if (!mLayerHistory) return; |
| 518 | |
Steven Thomas | debafed | 2020-05-18 17:30:35 -0700 | [diff] [blame] | 519 | const auto minFps = mRefreshRateConfigs.getMinRefreshRate().getFps(); |
| 520 | const auto maxFps = mRefreshRateConfigs.getMaxRefreshRate().getFps(); |
| 521 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 522 | if (layer->getWindowType() == InputWindowInfo::Type::STATUS_BAR) { |
Steven Thomas | debafed | 2020-05-18 17:30:35 -0700 | [diff] [blame] | 523 | mLayerHistory->registerLayer(layer, minFps, maxFps, |
Ana Krulec | 3d367c8 | 2020-02-25 15:02:01 -0800 | [diff] [blame] | 524 | scheduler::LayerHistory::LayerVoteType::NoVote); |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 525 | } else if (!mOptions.useContentDetection) { |
Steven Thomas | debafed | 2020-05-18 17:30:35 -0700 | [diff] [blame] | 526 | // If the content detection feature is off, all layers are registered at Max. We still keep |
| 527 | // the layer history, since we use it for other features (like Frame Rate API), so layers |
| 528 | // still need to be registered. |
| 529 | mLayerHistory->registerLayer(layer, minFps, maxFps, |
| 530 | scheduler::LayerHistory::LayerVoteType::Max); |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 531 | } else if (!mOptions.useContentDetectionV2) { |
Steven Thomas | debafed | 2020-05-18 17:30:35 -0700 | [diff] [blame] | 532 | // In V1 of content detection, all layers are registered as Heuristic (unless it's |
| 533 | // wallpaper). |
| 534 | const auto highFps = |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 535 | layer->getWindowType() == InputWindowInfo::Type::WALLPAPER ? minFps : maxFps; |
Ana Krulec | 3d367c8 | 2020-02-25 15:02:01 -0800 | [diff] [blame] | 536 | |
Steven Thomas | debafed | 2020-05-18 17:30:35 -0700 | [diff] [blame] | 537 | mLayerHistory->registerLayer(layer, minFps, highFps, |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 538 | scheduler::LayerHistory::LayerVoteType::Heuristic); |
| 539 | } else { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 540 | if (layer->getWindowType() == InputWindowInfo::Type::WALLPAPER) { |
Ana Krulec | 3d367c8 | 2020-02-25 15:02:01 -0800 | [diff] [blame] | 541 | // Running Wallpaper at Min is considered as part of content detection. |
Steven Thomas | debafed | 2020-05-18 17:30:35 -0700 | [diff] [blame] | 542 | mLayerHistory->registerLayer(layer, minFps, maxFps, |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 543 | scheduler::LayerHistory::LayerVoteType::Min); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 544 | } else { |
Steven Thomas | debafed | 2020-05-18 17:30:35 -0700 | [diff] [blame] | 545 | mLayerHistory->registerLayer(layer, minFps, maxFps, |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 546 | scheduler::LayerHistory::LayerVoteType::Heuristic); |
| 547 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 548 | } |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 551 | void Scheduler::recordLayerHistory(Layer* layer, nsecs_t presentTime, |
| 552 | LayerHistory::LayerUpdateType updateType) { |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 553 | if (mLayerHistory) { |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 554 | mLayerHistory->record(layer, presentTime, systemTime(), updateType); |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 555 | } |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 556 | } |
| 557 | |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame] | 558 | void Scheduler::setConfigChangePending(bool pending) { |
| 559 | if (mLayerHistory) { |
| 560 | mLayerHistory->setConfigChangePending(pending); |
| 561 | } |
| 562 | } |
| 563 | |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 564 | void Scheduler::chooseRefreshRateForContent() { |
| 565 | if (!mLayerHistory) return; |
| 566 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 567 | ATRACE_CALL(); |
| 568 | |
| 569 | scheduler::LayerHistory::Summary summary = mLayerHistory->summarize(systemTime()); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 570 | HwcConfigIndexType newConfigId; |
Ady Abraham | 6398a0a | 2019-04-18 19:30:44 -0700 | [diff] [blame] | 571 | { |
| 572 | std::lock_guard<std::mutex> lock(mFeatureStateLock); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 573 | if (mFeatures.contentRequirements == summary) { |
Ady Abraham | 6398a0a | 2019-04-18 19:30:44 -0700 | [diff] [blame] | 574 | return; |
| 575 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 576 | mFeatures.contentRequirements = summary; |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 577 | mFeatures.contentDetectionV1 = |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 578 | !summary.empty() ? ContentDetectionState::On : ContentDetectionState::Off; |
| 579 | |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 580 | scheduler::RefreshRateConfigs::GlobalSignals consideredSignals; |
| 581 | newConfigId = calculateRefreshRateConfigIndexType(&consideredSignals); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 582 | if (mFeatures.configId == newConfigId) { |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 583 | // We don't need to change the config, but we might need to send an event |
| 584 | // about a config change, since it was suppressed due to a previous idleConsidered |
| 585 | if (!consideredSignals.idle) { |
| 586 | dispatchCachedReportedConfig(); |
| 587 | } |
Ady Abraham | 6398a0a | 2019-04-18 19:30:44 -0700 | [diff] [blame] | 588 | return; |
| 589 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 590 | mFeatures.configId = newConfigId; |
Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 591 | auto& newRefreshRate = mRefreshRateConfigs.getRefreshRateFromConfigId(newConfigId); |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 592 | mSchedulerCallback.changeRefreshRate(newRefreshRate, |
| 593 | consideredSignals.idle ? ConfigEvent::None |
| 594 | : ConfigEvent::Changed); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 595 | } |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 596 | } |
| 597 | |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 598 | void Scheduler::resetIdleTimer() { |
| 599 | if (mIdleTimer) { |
| 600 | mIdleTimer->reset(); |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 604 | void Scheduler::notifyTouchEvent() { |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 605 | if (mTouchTimer) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 606 | mTouchTimer->reset(); |
Steven Thomas | 540730a | 2020-01-08 20:12:42 -0800 | [diff] [blame] | 607 | |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 608 | if (mOptions.supportKernelTimer && mIdleTimer) { |
Steven Thomas | 540730a | 2020-01-08 20:12:42 -0800 | [diff] [blame] | 609 | mIdleTimer->reset(); |
| 610 | } |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 611 | } |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 614 | void Scheduler::setDisplayPowerState(bool normal) { |
| 615 | { |
| 616 | std::lock_guard<std::mutex> lock(mFeatureStateLock); |
Dominik Laskowski | dd252cd | 2019-07-26 09:10:16 -0700 | [diff] [blame] | 617 | mFeatures.isDisplayPowerStateNormal = normal; |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | if (mDisplayPowerTimer) { |
| 621 | mDisplayPowerTimer->reset(); |
| 622 | } |
| 623 | |
| 624 | // Display Power event will boost the refresh rate to performance. |
| 625 | // Clear Layer History to get fresh FPS detection |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 626 | if (mLayerHistory) { |
| 627 | mLayerHistory->clear(); |
| 628 | } |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 631 | void Scheduler::kernelIdleTimerCallback(TimerState state) { |
| 632 | ATRACE_INT("ExpiredKernelIdleTimer", static_cast<int>(state)); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 633 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 634 | // TODO(145561154): cleanup the kernel idle timer implementation and the refresh rate |
| 635 | // magic number |
Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 636 | const auto& refreshRate = mRefreshRateConfigs.getCurrentRefreshRate(); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 637 | constexpr float FPS_THRESHOLD_FOR_KERNEL_TIMER = 65.0f; |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 638 | if (state == TimerState::Reset && refreshRate.getFps() > FPS_THRESHOLD_FOR_KERNEL_TIMER) { |
Alec Mouri | 7f01518 | 2019-07-11 13:56:22 -0700 | [diff] [blame] | 639 | // If we're not in performance mode then the kernel timer shouldn't do |
| 640 | // anything, as the refresh rate during DPU power collapse will be the |
| 641 | // same. |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 642 | resyncToHardwareVsync(true /* makeAvailable */, refreshRate.getVsyncPeriod()); |
| 643 | } else if (state == TimerState::Expired && |
| 644 | refreshRate.getFps() <= FPS_THRESHOLD_FOR_KERNEL_TIMER) { |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 645 | // Disable HW VSYNC if the timer expired, as we don't need it enabled if |
| 646 | // 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] | 647 | // need to update the VsyncController model anyway. |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 648 | disableHardwareVsync(false /* makeUnavailable */); |
Alec Mouri | dc28b37 | 2019-04-18 21:17:13 -0700 | [diff] [blame] | 649 | } |
Ady Abraham | a09852a | 2020-02-20 14:23:42 -0800 | [diff] [blame] | 650 | |
| 651 | mSchedulerCallback.kernelTimerChanged(state == TimerState::Expired); |
Alec Mouri | dc28b37 | 2019-04-18 21:17:13 -0700 | [diff] [blame] | 652 | } |
| 653 | |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 654 | void Scheduler::idleTimerCallback(TimerState state) { |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 655 | handleTimerStateChanged(&mFeatures.idleTimer, state); |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 656 | ATRACE_INT("ExpiredIdleTimer", static_cast<int>(state)); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 657 | } |
| 658 | |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 659 | void Scheduler::touchTimerCallback(TimerState state) { |
Dominik Laskowski | dd252cd | 2019-07-26 09:10:16 -0700 | [diff] [blame] | 660 | const TouchState touch = state == TimerState::Reset ? TouchState::Active : TouchState::Inactive; |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 661 | // Touch event will boost the refresh rate to performance. |
| 662 | // Clear layer history to get fresh FPS detection. |
| 663 | // NOTE: Instead of checking all the layers, we should be checking the layer |
| 664 | // that is currently on top. b/142507166 will give us this capability. |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 665 | if (handleTimerStateChanged(&mFeatures.touch, touch)) { |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 666 | if (mLayerHistory) { |
| 667 | mLayerHistory->clear(); |
| 668 | } |
Ady Abraham | 1adbb72 | 2020-05-15 11:51:48 -0700 | [diff] [blame] | 669 | } |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 670 | ATRACE_INT("TouchState", static_cast<int>(touch)); |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 673 | void Scheduler::displayPowerTimerCallback(TimerState state) { |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 674 | handleTimerStateChanged(&mFeatures.displayPowerTimer, state); |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 675 | ATRACE_INT("ExpiredDisplayPowerTimer", static_cast<int>(state)); |
Alec Mouri | dc28b37 | 2019-04-18 21:17:13 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 678 | void Scheduler::dump(std::string& result) const { |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 679 | using base::StringAppendF; |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 680 | |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 681 | StringAppendF(&result, "+ Idle timer: %s\n", mIdleTimer ? mIdleTimer->dump().c_str() : "off"); |
Ana Krulec | 3d367c8 | 2020-02-25 15:02:01 -0800 | [diff] [blame] | 682 | StringAppendF(&result, "+ Touch timer: %s\n", |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 683 | mTouchTimer ? mTouchTimer->dump().c_str() : "off"); |
| 684 | StringAppendF(&result, "+ Content detection: %s %s\n\n", |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 685 | toContentDetectionString(mOptions.useContentDetection, |
| 686 | mOptions.useContentDetectionV2), |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 687 | mLayerHistory ? mLayerHistory->dump().c_str() : "(no layer history)"); |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 688 | } |
| 689 | |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 690 | void Scheduler::dumpVsync(std::string& s) const { |
Ady Abraham | 8735eac | 2020-08-12 16:35:04 -0700 | [diff] [blame] | 691 | using base::StringAppendF; |
| 692 | |
| 693 | StringAppendF(&s, "VSyncReactor:\n"); |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 694 | mVsyncSchedule.controller->dump(s); |
Ady Abraham | 8735eac | 2020-08-12 16:35:04 -0700 | [diff] [blame] | 695 | StringAppendF(&s, "VSyncDispatch:\n"); |
| 696 | mVsyncSchedule.dispatch->dump(s); |
| 697 | } |
| 698 | |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 699 | template <class T> |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 700 | bool Scheduler::handleTimerStateChanged(T* currentState, T newState) { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 701 | HwcConfigIndexType newConfigId; |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 702 | scheduler::RefreshRateConfigs::GlobalSignals consideredSignals; |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 703 | { |
| 704 | std::lock_guard<std::mutex> lock(mFeatureStateLock); |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 705 | if (*currentState == newState) { |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 706 | return false; |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 707 | } |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 708 | *currentState = newState; |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 709 | newConfigId = calculateRefreshRateConfigIndexType(&consideredSignals); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 710 | if (mFeatures.configId == newConfigId) { |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 711 | // We don't need to change the config, but we might need to send an event |
| 712 | // about a config change, since it was suppressed due to a previous idleConsidered |
| 713 | if (!consideredSignals.idle) { |
| 714 | dispatchCachedReportedConfig(); |
| 715 | } |
| 716 | return consideredSignals.touch; |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 717 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 718 | mFeatures.configId = newConfigId; |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 719 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 720 | const RefreshRate& newRefreshRate = mRefreshRateConfigs.getRefreshRateFromConfigId(newConfigId); |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 721 | mSchedulerCallback.changeRefreshRate(newRefreshRate, |
| 722 | consideredSignals.idle ? ConfigEvent::None |
| 723 | : ConfigEvent::Changed); |
| 724 | return consideredSignals.touch; |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 725 | } |
| 726 | |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 727 | HwcConfigIndexType Scheduler::calculateRefreshRateConfigIndexType( |
| 728 | scheduler::RefreshRateConfigs::GlobalSignals* consideredSignals) { |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 729 | ATRACE_CALL(); |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 730 | if (consideredSignals) *consideredSignals = {}; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 731 | |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 732 | // If Display Power is not in normal operation we want to be in performance mode. When coming |
| 733 | // back to normal mode, a grace period is given with DisplayPowerTimer. |
Ana Krulec | 3f6a206 | 2020-01-23 15:48:01 -0800 | [diff] [blame] | 734 | if (mDisplayPowerTimer && |
| 735 | (!mFeatures.isDisplayPowerStateNormal || |
| 736 | mFeatures.displayPowerTimer == TimerState::Reset)) { |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 737 | return mRefreshRateConfigs.getMaxRefreshRateByPolicy().getConfigId(); |
Ana Krulec | 3f6a206 | 2020-01-23 15:48:01 -0800 | [diff] [blame] | 738 | } |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 739 | |
Steven Thomas | bb37432 | 2020-04-28 22:47:16 -0700 | [diff] [blame] | 740 | const bool touchActive = mTouchTimer && mFeatures.touch == TouchState::Active; |
| 741 | const bool idle = mIdleTimer && mFeatures.idleTimer == TimerState::Expired; |
| 742 | |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 743 | if (!mOptions.useContentDetectionV2) { |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 744 | // 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] | 745 | if (touchActive) { |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 746 | return mRefreshRateConfigs.getMaxRefreshRateByPolicy().getConfigId(); |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 747 | } |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 748 | |
Steven Thomas | bb37432 | 2020-04-28 22:47:16 -0700 | [diff] [blame] | 749 | // If timer has expired as it means there is no new content on the screen. |
| 750 | if (idle) { |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 751 | if (consideredSignals) consideredSignals->idle = true; |
Steven Thomas | bb37432 | 2020-04-28 22:47:16 -0700 | [diff] [blame] | 752 | return mRefreshRateConfigs.getMinRefreshRateByPolicy().getConfigId(); |
| 753 | } |
Ady Abraham | a315ce7 | 2019-04-24 14:35:20 -0700 | [diff] [blame] | 754 | |
Ana Krulec | 3f6a206 | 2020-01-23 15:48:01 -0800 | [diff] [blame] | 755 | // 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] | 756 | if (mFeatures.contentDetectionV1 == ContentDetectionState::Off) { |
Ana Krulec | 3803b8d | 2020-02-03 16:35:46 -0800 | [diff] [blame] | 757 | // 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] | 758 | return mRefreshRateConfigs.getMaxRefreshRateByPolicy().getConfigId(); |
Steven Thomas | 540730a | 2020-01-08 20:12:42 -0800 | [diff] [blame] | 759 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 760 | |
| 761 | // Content detection is on, find the appropriate refresh rate with minimal error |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 762 | return mRefreshRateConfigs.getRefreshRateForContent(mFeatures.contentRequirements) |
| 763 | .getConfigId(); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 764 | } |
| 765 | |
Ady Abraham | 1adbb72 | 2020-05-15 11:51:48 -0700 | [diff] [blame] | 766 | return mRefreshRateConfigs |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 767 | .getBestRefreshRate(mFeatures.contentRequirements, {.touch = touchActive, .idle = idle}, |
| 768 | consideredSignals) |
Ady Abraham | 1adbb72 | 2020-05-15 11:51:48 -0700 | [diff] [blame] | 769 | .getConfigId(); |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 770 | } |
| 771 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 772 | std::optional<HwcConfigIndexType> Scheduler::getPreferredConfigId() { |
Daniel Solomon | 0f0ddc1 | 2019-08-19 19:31:09 -0700 | [diff] [blame] | 773 | std::lock_guard<std::mutex> lock(mFeatureStateLock); |
Ana Krulec | 3f6a206 | 2020-01-23 15:48:01 -0800 | [diff] [blame] | 774 | // Make sure that the default config ID is first updated, before returned. |
| 775 | if (mFeatures.configId.has_value()) { |
Ana Krulec | 3803b8d | 2020-02-03 16:35:46 -0800 | [diff] [blame] | 776 | mFeatures.configId = calculateRefreshRateConfigIndexType(); |
Ana Krulec | 3f6a206 | 2020-01-23 15:48:01 -0800 | [diff] [blame] | 777 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 778 | return mFeatures.configId; |
Daniel Solomon | 0f0ddc1 | 2019-08-19 19:31:09 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 781 | void Scheduler::onNewVsyncPeriodChangeTimeline(const hal::VsyncPeriodChangeTimeline& timeline) { |
Ady Abraham | 3a77a7b | 2019-12-02 18:46:59 -0800 | [diff] [blame] | 782 | if (timeline.refreshRequired) { |
| 783 | mSchedulerCallback.repaintEverythingForHWC(); |
| 784 | } |
| 785 | |
| 786 | std::lock_guard<std::mutex> lock(mVsyncTimelineLock); |
| 787 | mLastVsyncPeriodChangeTimeline = std::make_optional(timeline); |
| 788 | |
| 789 | const auto maxAppliedTime = systemTime() + MAX_VSYNC_APPLIED_TIME.count(); |
| 790 | if (timeline.newVsyncAppliedTimeNanos > maxAppliedTime) { |
| 791 | mLastVsyncPeriodChangeTimeline->newVsyncAppliedTimeNanos = maxAppliedTime; |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | void Scheduler::onDisplayRefreshed(nsecs_t timestamp) { |
| 796 | bool callRepaint = false; |
| 797 | { |
| 798 | std::lock_guard<std::mutex> lock(mVsyncTimelineLock); |
| 799 | if (mLastVsyncPeriodChangeTimeline && mLastVsyncPeriodChangeTimeline->refreshRequired) { |
| 800 | if (mLastVsyncPeriodChangeTimeline->refreshTimeNanos < timestamp) { |
| 801 | mLastVsyncPeriodChangeTimeline->refreshRequired = false; |
| 802 | } else { |
| 803 | // We need to send another refresh as refreshTimeNanos is still in the future |
| 804 | callRepaint = true; |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | if (callRepaint) { |
| 810 | mSchedulerCallback.repaintEverythingForHWC(); |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 814 | void Scheduler::onPrimaryDisplayAreaChanged(uint32_t displayArea) { |
| 815 | if (mLayerHistory) { |
| 816 | mLayerHistory->setDisplayArea(displayArea); |
| 817 | } |
| 818 | } |
| 819 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 820 | } // namespace android |