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