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