blob: 1e005770766094a14260be7c34cad11be5ab3a05 [file] [log] [blame]
Ana Krulec98b5b242018-08-10 15:03:23 -07001/*
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 Laskowski98041832019-08-01 18:35:59 -070017#undef LOG_TAG
18#define LOG_TAG "Scheduler"
Ana Krulec7ab56032018-11-02 20:51:06 +010019#define ATRACE_TAG ATRACE_TAG_GRAPHICS
20
Ana Krulec98b5b242018-08-10 15:03:23 -070021#include "Scheduler.h"
22
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070023#include <android-base/properties.h>
Dominik Laskowski49cea512019-11-12 14:13:23 -080024#include <android-base/stringprintf.h>
Ana Krulece588e312018-09-18 12:32:24 -070025#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
26#include <android/hardware/configstore/1.1/ISurfaceFlingerConfigs.h>
Ana Krulece588e312018-09-18 12:32:24 -070027#include <configstore/Utils.h>
Ady Abraham8f1ee7f2019-04-05 10:32:50 -070028#include <input/InputWindow.h>
Ana Krulecfefd6ae2019-02-13 17:53:08 -080029#include <system/window.h>
Ana Krulece588e312018-09-18 12:32:24 -070030#include <ui/DisplayStatInfo.h>
Ana Krulec3084c052018-11-21 20:27:17 +010031#include <utils/Timers.h>
Ana Krulec7ab56032018-11-02 20:51:06 +010032#include <utils/Trace.h>
Ana Krulec98b5b242018-08-10 15:03:23 -070033
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070034#include <FrameTimeline/FrameTimeline.h>
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070035#include <algorithm>
36#include <cinttypes>
37#include <cstdint>
38#include <functional>
39#include <memory>
40#include <numeric>
41
42#include "../Layer.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070043#include "DispSyncSource.h"
44#include "EventThread.h"
Dominik Laskowski6505f792019-09-18 11:10:05 -070045#include "InjectVSyncSource.h"
Ana Krulecf2c006d2019-06-21 15:37:07 -070046#include "OneShotTimer.h"
Ana Krulec434c22d2018-11-28 13:48:36 +010047#include "SchedulerUtils.h"
Sundong Ahnd5e08f62018-12-12 20:27:28 +090048#include "SurfaceFlingerProperties.h"
Kevin DuBois00287382019-11-19 15:11:55 -080049#include "Timer.h"
50#include "VSyncDispatchTimerQueue.h"
51#include "VSyncPredictor.h"
52#include "VSyncReactor.h"
Ady Abraham8cb21882020-08-26 18:22:05 -070053#include "VsyncController.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070054
Dominik Laskowski98041832019-08-01 18:35:59 -070055#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 Laskowski8b01cc02020-07-14 19:02:41 -070063using namespace std::string_literals;
64
Ana Krulec98b5b242018-08-10 15:03:23 -070065namespace android {
Ady Abraham5a858552020-03-31 17:54:56 -070066
Dominik Laskowski983f2b52020-06-25 16:54:06 -070067namespace {
Ana Krulec98b5b242018-08-10 15:03:23 -070068
Ady Abraham5a858552020-03-31 17:54:56 -070069std::unique_ptr<scheduler::VSyncTracker> createVSyncTracker() {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070070 // 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 Abraham5a858552020-03-31 17:54:56 -070081}
82
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070083std::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 Abraham5a858552020-03-31 17:54:56 -070087 return std::make_unique<
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070088 scheduler::VSyncDispatchTimerQueue>(std::make_unique<scheduler::Timer>(), tracker,
Ady Abraham5a858552020-03-31 17:54:56 -070089 timerSlack.count(), vsyncMoveThreshold.count());
90}
91
Marin Shalamanov27fa3de2020-11-20 16:22:48 +010092const char* toContentDetectionString(bool useContentDetection) {
93 return useContentDetection ? "on" : "off";
Dominik Laskowski983f2b52020-06-25 16:54:06 -070094}
95
96} // namespace
97
Ady Abraham8735eac2020-08-12 16:35:04 -070098class PredictedVsyncTracer {
99public:
100 PredictedVsyncTracer(scheduler::VSyncDispatch& dispatch)
101 : mRegistration(dispatch, std::bind(&PredictedVsyncTracer::callback, this),
102 "PredictedVsyncTracer") {
103 scheduleRegistration();
104 }
105
106private:
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 Laskowski8b01cc02020-07-14 19:02:41 -0700118Scheduler::Scheduler(const scheduler::RefreshRateConfigs& configs, ISchedulerCallback& callback)
119 : Scheduler(configs, callback,
120 {.supportKernelTimer = sysprop::support_kernel_idle_timer(false),
Marin Shalamanov27fa3de2020-11-20 16:22:48 +0100121 .useContentDetection = sysprop::use_content_detection_for_refresh_rate(false)}) {
122}
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700123
124Scheduler::Scheduler(const scheduler::RefreshRateConfigs& configs, ISchedulerCallback& callback,
125 Options options)
Ady Abraham8cb21882020-08-26 18:22:05 -0700126 : Scheduler(createVsyncSchedule(options.supportKernelTimer), configs, callback,
Marin Shalamanov27fa3de2020-11-20 16:22:48 +0100127 createLayerHistory(configs), options) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700128 using namespace sysprop;
Ady Abraham8532d012019-05-08 14:50:56 -0700129
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700130 const int setIdleTimerMs = base::GetIntProperty("debug.sf.set_idle_timer_ms"s, 0);
Ana Krulecfb772822018-11-30 10:44:07 +0100131
Dominik Laskowski98041832019-08-01 18:35:59 -0700132 if (const auto millis = setIdleTimerMs ? setIdleTimerMs : set_idle_timer_ms(0); millis > 0) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700133 const auto callback = mOptions.supportKernelTimer ? &Scheduler::kernelIdleTimerCallback
134 : &Scheduler::idleTimerCallback;
Dominik Laskowski98041832019-08-01 18:35:59 -0700135 mIdleTimer.emplace(
Ady Abrahamdb3dfee2020-11-17 17:07:12 -0800136 "IdleTimer", std::chrono::milliseconds(millis),
Dominik Laskowski98041832019-08-01 18:35:59 -0700137 [this, callback] { std::invoke(callback, this, TimerState::Reset); },
138 [this, callback] { std::invoke(callback, this, TimerState::Expired); });
Ana Krulecfb772822018-11-30 10:44:07 +0100139 mIdleTimer->start();
140 }
Ady Abraham8532d012019-05-08 14:50:56 -0700141
Dominik Laskowski98041832019-08-01 18:35:59 -0700142 if (const int64_t millis = set_touch_timer_ms(0); millis > 0) {
Ady Abraham8532d012019-05-08 14:50:56 -0700143 // Touch events are coming to SF every 100ms, so the timer needs to be higher than that
Dominik Laskowski98041832019-08-01 18:35:59 -0700144 mTouchTimer.emplace(
Ady Abrahamdb3dfee2020-11-17 17:07:12 -0800145 "TouchTimer", std::chrono::milliseconds(millis),
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700146 [this] { touchTimerCallback(TimerState::Reset); },
147 [this] { touchTimerCallback(TimerState::Expired); });
Ady Abraham8532d012019-05-08 14:50:56 -0700148 mTouchTimer->start();
149 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700150
Dominik Laskowski98041832019-08-01 18:35:59 -0700151 if (const int64_t millis = set_display_power_timer_ms(0); millis > 0) {
152 mDisplayPowerTimer.emplace(
Ady Abrahamdb3dfee2020-11-17 17:07:12 -0800153 "DisplayPowerTimer", std::chrono::milliseconds(millis),
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700154 [this] { displayPowerTimerCallback(TimerState::Reset); },
155 [this] { displayPowerTimerCallback(TimerState::Expired); });
Ady Abraham6fe2c172019-07-12 12:37:57 -0700156 mDisplayPowerTimer->start();
157 }
Ana Krulece588e312018-09-18 12:32:24 -0700158}
159
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700160Scheduler::Scheduler(VsyncSchedule schedule, const scheduler::RefreshRateConfigs& configs,
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700161 ISchedulerCallback& schedulerCallback,
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700162 std::unique_ptr<LayerHistory> layerHistory, Options options)
163 : mOptions(options),
164 mVsyncSchedule(std::move(schedule)),
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700165 mLayerHistory(std::move(layerHistory)),
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800166 mSchedulerCallback(schedulerCallback),
Ady Abraham8735eac2020-08-12 16:35:04 -0700167 mRefreshRateConfigs(configs),
168 mPredictedVsyncTracer(
169 base::GetBoolProperty("debug.sf.show_predicted_vsync", false)
170 ? std::make_unique<PredictedVsyncTracer>(*mVsyncSchedule.dispatch)
171 : nullptr) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700172 mSchedulerCallback.setVsyncEnabled(false);
173}
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700174
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800175Scheduler::~Scheduler() {
Ana Krulecf2c006d2019-06-21 15:37:07 -0700176 // Ensure the OneShotTimer threads are joined before we start destroying state.
Ady Abraham6fe2c172019-07-12 12:37:57 -0700177 mDisplayPowerTimer.reset();
Ady Abraham8532d012019-05-08 14:50:56 -0700178 mTouchTimer.reset();
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800179 mIdleTimer.reset();
180}
Ana Krulec0c8cd522018-08-31 12:27:28 -0700181
Ady Abraham8cb21882020-08-26 18:22:05 -0700182Scheduler::VsyncSchedule Scheduler::createVsyncSchedule(bool supportKernelTimer) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700183 auto clock = std::make_unique<scheduler::SystemClock>();
184 auto tracker = createVSyncTracker();
185 auto dispatch = createVSyncDispatch(*tracker);
186
187 // TODO(b/144707443): Tune constants.
188 constexpr size_t pendingFenceLimit = 20;
Ady Abraham8cb21882020-08-26 18:22:05 -0700189 auto controller =
Ady Abraham8735eac2020-08-12 16:35:04 -0700190 std::make_unique<scheduler::VSyncReactor>(std::move(clock), *tracker, pendingFenceLimit,
Ady Abraham8cb21882020-08-26 18:22:05 -0700191 supportKernelTimer);
192 return {std::move(controller), std::move(tracker), std::move(dispatch)};
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700193}
194
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700195std::unique_ptr<LayerHistory> Scheduler::createLayerHistory(
Marin Shalamanov27fa3de2020-11-20 16:22:48 +0100196 const scheduler::RefreshRateConfigs& configs) {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700197 if (!configs.canSwitch()) return nullptr;
198
Marin Shalamanov27fa3de2020-11-20 16:22:48 +0100199 return std::make_unique<scheduler::impl::LayerHistoryV2>(configs);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700200}
201
Ady Abraham9c53ee72020-07-22 21:16:18 -0700202std::unique_ptr<VSyncSource> Scheduler::makePrimaryDispSyncSource(
203 const char* name, std::chrono::nanoseconds workDuration,
204 std::chrono::nanoseconds readyDuration, bool traceVsync) {
205 return std::make_unique<scheduler::DispSyncSource>(*mVsyncSchedule.dispatch, workDuration,
206 readyDuration, traceVsync, name);
Dominik Laskowski6505f792019-09-18 11:10:05 -0700207}
208
Ady Abraham0bb6a472020-10-12 10:22:13 -0700209bool Scheduler::isVsyncValid(nsecs_t expectedVsyncTimestamp, uid_t uid) const {
210 const auto divider = mRefreshRateConfigs.getRefreshRateDividerForUid(uid);
211 if (divider <= 1) {
212 return true;
213 }
214
215 return mVsyncSchedule.tracker->isVSyncInPhase(expectedVsyncTimestamp, divider);
216}
217
Dominik Laskowski98041832019-08-01 18:35:59 -0700218Scheduler::ConnectionHandle Scheduler::createConnection(
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700219 const char* connectionName, frametimeline::TokenManager* tokenManager,
220 std::chrono::nanoseconds workDuration, std::chrono::nanoseconds readyDuration,
Ana Krulec98b5b242018-08-10 15:03:23 -0700221 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700222 auto vsyncSource = makePrimaryDispSyncSource(connectionName, workDuration, readyDuration);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700223 auto throttleVsync = [this](nsecs_t expectedVsyncTimestamp, uid_t uid) {
224 return !isVsyncValid(expectedVsyncTimestamp, uid);
225 };
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700226 auto eventThread = std::make_unique<impl::EventThread>(std::move(vsyncSource), tokenManager,
Ady Abraham0bb6a472020-10-12 10:22:13 -0700227 std::move(interceptCallback),
228 std::move(throttleVsync));
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700229 return createConnection(std::move(eventThread));
Dominik Laskowski98041832019-08-01 18:35:59 -0700230}
Ana Krulec98b5b242018-08-10 15:03:23 -0700231
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700232Scheduler::ConnectionHandle Scheduler::createConnection(std::unique_ptr<EventThread> eventThread) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700233 const ConnectionHandle handle = ConnectionHandle{mNextConnectionHandleId++};
234 ALOGV("Creating a connection handle with ID %" PRIuPTR, handle.id);
Dominik Laskowskif654d572018-12-20 11:03:06 -0800235
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700236 auto connection =
237 createConnectionInternal(eventThread.get(), ISurfaceComposer::eConfigChangedSuppress);
Dominik Laskowski98041832019-08-01 18:35:59 -0700238
Ana Krulec6ddd2612020-09-24 13:06:33 -0700239 std::lock_guard<std::mutex> lock(mConnectionsLock);
Dominik Laskowski98041832019-08-01 18:35:59 -0700240 mConnections.emplace(handle, Connection{connection, std::move(eventThread)});
241 return handle;
Ana Krulec98b5b242018-08-10 15:03:23 -0700242}
243
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700244sp<EventThreadConnection> Scheduler::createConnectionInternal(
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700245 EventThread* eventThread, ISurfaceComposer::ConfigChanged configChanged) {
246 return eventThread->createEventConnection([&] { resync(); }, configChanged);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700247}
248
Ana Krulec98b5b242018-08-10 15:03:23 -0700249sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700250 ConnectionHandle handle, ISurfaceComposer::ConfigChanged configChanged) {
Ana Krulec6ddd2612020-09-24 13:06:33 -0700251 std::lock_guard<std::mutex> lock(mConnectionsLock);
Dominik Laskowski98041832019-08-01 18:35:59 -0700252 RETURN_IF_INVALID_HANDLE(handle, nullptr);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700253 return createConnectionInternal(mConnections[handle].thread.get(), configChanged);
Ana Krulec98b5b242018-08-10 15:03:23 -0700254}
255
Dominik Laskowski98041832019-08-01 18:35:59 -0700256sp<EventThreadConnection> Scheduler::getEventConnection(ConnectionHandle handle) {
Ana Krulec6ddd2612020-09-24 13:06:33 -0700257 std::lock_guard<std::mutex> lock(mConnectionsLock);
Dominik Laskowski98041832019-08-01 18:35:59 -0700258 RETURN_IF_INVALID_HANDLE(handle, nullptr);
259 return mConnections[handle].connection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700260}
261
Dominik Laskowski98041832019-08-01 18:35:59 -0700262void Scheduler::onHotplugReceived(ConnectionHandle handle, PhysicalDisplayId displayId,
263 bool connected) {
Ana Krulec6ddd2612020-09-24 13:06:33 -0700264 android::EventThread* thread;
265 {
266 std::lock_guard<std::mutex> lock(mConnectionsLock);
267 RETURN_IF_INVALID_HANDLE(handle);
268 thread = mConnections[handle].thread.get();
269 }
270
271 thread->onHotplugReceived(displayId, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700272}
273
Dominik Laskowski98041832019-08-01 18:35:59 -0700274void Scheduler::onScreenAcquired(ConnectionHandle handle) {
Ana Krulec6ddd2612020-09-24 13:06:33 -0700275 android::EventThread* thread;
276 {
277 std::lock_guard<std::mutex> lock(mConnectionsLock);
278 RETURN_IF_INVALID_HANDLE(handle);
279 thread = mConnections[handle].thread.get();
280 }
281 thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700282}
283
Dominik Laskowski98041832019-08-01 18:35:59 -0700284void Scheduler::onScreenReleased(ConnectionHandle handle) {
Ana Krulec6ddd2612020-09-24 13:06:33 -0700285 android::EventThread* thread;
286 {
287 std::lock_guard<std::mutex> lock(mConnectionsLock);
288 RETURN_IF_INVALID_HANDLE(handle);
289 thread = mConnections[handle].thread.get();
290 }
291 thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700292}
293
Ady Abrahamdfd62162020-06-10 16:11:56 -0700294void Scheduler::onPrimaryDisplayConfigChanged(ConnectionHandle handle, PhysicalDisplayId displayId,
295 HwcConfigIndexType configId, nsecs_t vsyncPeriod) {
296 std::lock_guard<std::mutex> lock(mFeatureStateLock);
297 // Cache the last reported config for primary display.
298 mFeatures.cachedConfigChangedParams = {handle, displayId, configId, vsyncPeriod};
299 onNonPrimaryDisplayConfigChanged(handle, displayId, configId, vsyncPeriod);
300}
301
302void Scheduler::dispatchCachedReportedConfig() {
Ana Krulec6ddd2612020-09-24 13:06:33 -0700303 // Check optional fields first.
304 if (!mFeatures.configId.has_value()) {
305 ALOGW("No config ID found, not dispatching cached config.");
306 return;
307 }
308 if (!mFeatures.cachedConfigChangedParams.has_value()) {
309 ALOGW("No config changed params found, not dispatching cached config.");
310 return;
311 }
312
Ady Abrahamdfd62162020-06-10 16:11:56 -0700313 const auto configId = *mFeatures.configId;
314 const auto vsyncPeriod =
315 mRefreshRateConfigs.getRefreshRateFromConfigId(configId).getVsyncPeriod();
316
317 // If there is no change from cached config, there is no need to dispatch an event
318 if (configId == mFeatures.cachedConfigChangedParams->configId &&
319 vsyncPeriod == mFeatures.cachedConfigChangedParams->vsyncPeriod) {
320 return;
321 }
322
323 mFeatures.cachedConfigChangedParams->configId = configId;
324 mFeatures.cachedConfigChangedParams->vsyncPeriod = vsyncPeriod;
325 onNonPrimaryDisplayConfigChanged(mFeatures.cachedConfigChangedParams->handle,
326 mFeatures.cachedConfigChangedParams->displayId,
327 mFeatures.cachedConfigChangedParams->configId,
328 mFeatures.cachedConfigChangedParams->vsyncPeriod);
329}
330
331void Scheduler::onNonPrimaryDisplayConfigChanged(ConnectionHandle handle,
332 PhysicalDisplayId displayId,
333 HwcConfigIndexType configId, nsecs_t vsyncPeriod) {
Ana Krulec6ddd2612020-09-24 13:06:33 -0700334 android::EventThread* thread;
335 {
336 std::lock_guard<std::mutex> lock(mConnectionsLock);
337 RETURN_IF_INVALID_HANDLE(handle);
338 thread = mConnections[handle].thread.get();
339 }
340 thread->onConfigChanged(displayId, configId, vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800341}
342
Alec Mouri717bcb62020-02-10 17:07:19 -0800343size_t Scheduler::getEventThreadConnectionCount(ConnectionHandle handle) {
Ana Krulec6ddd2612020-09-24 13:06:33 -0700344 std::lock_guard<std::mutex> lock(mConnectionsLock);
Alec Mouri717bcb62020-02-10 17:07:19 -0800345 RETURN_IF_INVALID_HANDLE(handle, 0);
346 return mConnections[handle].thread->getEventThreadConnectionCount();
347}
348
Dominik Laskowski98041832019-08-01 18:35:59 -0700349void Scheduler::dump(ConnectionHandle handle, std::string& result) const {
Ana Krulec6ddd2612020-09-24 13:06:33 -0700350 android::EventThread* thread;
351 {
352 std::lock_guard<std::mutex> lock(mConnectionsLock);
353 RETURN_IF_INVALID_HANDLE(handle);
354 thread = mConnections.at(handle).thread.get();
355 }
356 thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700357}
358
Ady Abraham9c53ee72020-07-22 21:16:18 -0700359void Scheduler::setDuration(ConnectionHandle handle, std::chrono::nanoseconds workDuration,
360 std::chrono::nanoseconds readyDuration) {
Ana Krulec6ddd2612020-09-24 13:06:33 -0700361 android::EventThread* thread;
362 {
363 std::lock_guard<std::mutex> lock(mConnectionsLock);
364 RETURN_IF_INVALID_HANDLE(handle);
365 thread = mConnections[handle].thread.get();
366 }
367 thread->setDuration(workDuration, readyDuration);
Ana Krulec98b5b242018-08-10 15:03:23 -0700368}
Ana Krulece588e312018-09-18 12:32:24 -0700369
Ady Abraham8cb21882020-08-26 18:22:05 -0700370void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats, nsecs_t now) {
371 stats->vsyncTime = mVsyncSchedule.tracker->nextAnticipatedVSyncTimeFrom(now);
372 stats->vsyncPeriod = mVsyncSchedule.tracker->currentPeriod();
Ana Krulece588e312018-09-18 12:32:24 -0700373}
374
Dominik Laskowski6505f792019-09-18 11:10:05 -0700375Scheduler::ConnectionHandle Scheduler::enableVSyncInjection(bool enable) {
376 if (mInjectVSyncs == enable) {
377 return {};
378 }
379
380 ALOGV("%s VSYNC injection", enable ? "Enabling" : "Disabling");
381
382 if (!mInjectorConnectionHandle) {
383 auto vsyncSource = std::make_unique<InjectVSyncSource>();
384 mVSyncInjector = vsyncSource.get();
385
386 auto eventThread =
387 std::make_unique<impl::EventThread>(std::move(vsyncSource),
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700388 /*tokenManager=*/nullptr,
Ady Abraham0bb6a472020-10-12 10:22:13 -0700389 impl::EventThread::InterceptVSyncsCallback(),
390 impl::EventThread::ThrottleVsyncCallback());
Dominik Laskowski6505f792019-09-18 11:10:05 -0700391
392 mInjectorConnectionHandle = createConnection(std::move(eventThread));
393 }
394
395 mInjectVSyncs = enable;
396 return mInjectorConnectionHandle;
397}
398
Ady Abraham9c53ee72020-07-22 21:16:18 -0700399bool Scheduler::injectVSync(nsecs_t when, nsecs_t expectedVSyncTime, nsecs_t deadlineTimestamp) {
Dominik Laskowski6505f792019-09-18 11:10:05 -0700400 if (!mInjectVSyncs || !mVSyncInjector) {
401 return false;
402 }
403
Ady Abraham9c53ee72020-07-22 21:16:18 -0700404 mVSyncInjector->onInjectSyncEvent(when, expectedVSyncTime, deadlineTimestamp);
Dominik Laskowski6505f792019-09-18 11:10:05 -0700405 return true;
406}
407
Ana Krulece588e312018-09-18 12:32:24 -0700408void Scheduler::enableHardwareVsync() {
409 std::lock_guard<std::mutex> lock(mHWVsyncLock);
410 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700411 mVsyncSchedule.tracker->resetModel();
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700412 mSchedulerCallback.setVsyncEnabled(true);
Ana Krulece588e312018-09-18 12:32:24 -0700413 mPrimaryHWVsyncEnabled = true;
414 }
415}
416
417void Scheduler::disableHardwareVsync(bool makeUnavailable) {
418 std::lock_guard<std::mutex> lock(mHWVsyncLock);
419 if (mPrimaryHWVsyncEnabled) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700420 mSchedulerCallback.setVsyncEnabled(false);
Ana Krulece588e312018-09-18 12:32:24 -0700421 mPrimaryHWVsyncEnabled = false;
422 }
423 if (makeUnavailable) {
424 mHWVsyncAvailable = false;
425 }
426}
427
Ana Krulecc2870422019-01-29 19:00:58 -0800428void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) {
429 {
430 std::lock_guard<std::mutex> lock(mHWVsyncLock);
431 if (makeAvailable) {
432 mHWVsyncAvailable = makeAvailable;
433 } else if (!mHWVsyncAvailable) {
434 // Hardware vsync is not currently available, so abort the resync
435 // attempt for now
436 return;
437 }
438 }
439
440 if (period <= 0) {
441 return;
442 }
443
444 setVsyncPeriod(period);
445}
446
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700447void Scheduler::resync() {
Long Ling457bef92019-09-11 14:43:11 -0700448 static constexpr nsecs_t kIgnoreDelay = ms2ns(750);
Ana Krulecc2870422019-01-29 19:00:58 -0800449
450 const nsecs_t now = systemTime();
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700451 const nsecs_t last = mLastResyncTime.exchange(now);
Ana Krulecc2870422019-01-29 19:00:58 -0800452
453 if (now - last > kIgnoreDelay) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700454 resyncToHardwareVsync(false, mRefreshRateConfigs.getCurrentRefreshRate().getVsyncPeriod());
Ana Krulecc2870422019-01-29 19:00:58 -0800455 }
456}
457
Dominik Laskowski98041832019-08-01 18:35:59 -0700458void Scheduler::setVsyncPeriod(nsecs_t period) {
Ady Abraham3aff9172019-02-07 19:10:26 -0800459 std::lock_guard<std::mutex> lock(mHWVsyncLock);
Ady Abraham8cb21882020-08-26 18:22:05 -0700460 mVsyncSchedule.controller->startPeriodTransition(period);
Ady Abraham3aff9172019-02-07 19:10:26 -0800461
462 if (!mPrimaryHWVsyncEnabled) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700463 mVsyncSchedule.tracker->resetModel();
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700464 mSchedulerCallback.setVsyncEnabled(true);
Ady Abraham3aff9172019-02-07 19:10:26 -0800465 mPrimaryHWVsyncEnabled = true;
466 }
Ana Krulece588e312018-09-18 12:32:24 -0700467}
468
Ady Abraham5dee2f12020-02-05 17:49:47 -0800469void Scheduler::addResyncSample(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
470 bool* periodFlushed) {
Ana Krulece588e312018-09-18 12:32:24 -0700471 bool needsHwVsync = false;
Alec Mourif8e689c2019-05-20 18:32:22 -0700472 *periodFlushed = false;
Ana Krulece588e312018-09-18 12:32:24 -0700473 { // Scope for the lock
474 std::lock_guard<std::mutex> lock(mHWVsyncLock);
475 if (mPrimaryHWVsyncEnabled) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700476 needsHwVsync = mVsyncSchedule.controller->addHwVsyncTimestamp(timestamp, hwcVsyncPeriod,
477 periodFlushed);
Ana Krulece588e312018-09-18 12:32:24 -0700478 }
479 }
480
481 if (needsHwVsync) {
482 enableHardwareVsync();
483 } else {
484 disableHardwareVsync(false);
485 }
486}
487
488void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700489 if (mVsyncSchedule.controller->addPresentFence(fenceTime)) {
Ana Krulece588e312018-09-18 12:32:24 -0700490 enableHardwareVsync();
491 } else {
492 disableHardwareVsync(false);
493 }
494}
495
496void Scheduler::setIgnorePresentFences(bool ignore) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700497 mVsyncSchedule.controller->setIgnorePresentFences(ignore);
Ady Abrahamc3e21312019-02-07 14:30:23 -0800498}
499
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700500void Scheduler::registerLayer(Layer* layer) {
Dominik Laskowski49cea512019-11-12 14:13:23 -0800501 if (!mLayerHistory) return;
502
Steven Thomasdebafed2020-05-18 17:30:35 -0700503 const auto minFps = mRefreshRateConfigs.getMinRefreshRate().getFps();
504 const auto maxFps = mRefreshRateConfigs.getMaxRefreshRate().getFps();
505
Michael Wright44753b12020-07-08 13:48:11 +0100506 if (layer->getWindowType() == InputWindowInfo::Type::STATUS_BAR) {
Steven Thomasdebafed2020-05-18 17:30:35 -0700507 mLayerHistory->registerLayer(layer, minFps, maxFps,
Ana Krulec3d367c82020-02-25 15:02:01 -0800508 scheduler::LayerHistory::LayerVoteType::NoVote);
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700509 } else if (!mOptions.useContentDetection) {
Steven Thomasdebafed2020-05-18 17:30:35 -0700510 // If the content detection feature is off, all layers are registered at Max. We still keep
511 // the layer history, since we use it for other features (like Frame Rate API), so layers
512 // still need to be registered.
513 mLayerHistory->registerLayer(layer, minFps, maxFps,
514 scheduler::LayerHistory::LayerVoteType::Max);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800515 } else {
Michael Wright44753b12020-07-08 13:48:11 +0100516 if (layer->getWindowType() == InputWindowInfo::Type::WALLPAPER) {
Ana Krulec3d367c82020-02-25 15:02:01 -0800517 // Running Wallpaper at Min is considered as part of content detection.
Steven Thomasdebafed2020-05-18 17:30:35 -0700518 mLayerHistory->registerLayer(layer, minFps, maxFps,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800519 scheduler::LayerHistory::LayerVoteType::Min);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800520 } else {
Steven Thomasdebafed2020-05-18 17:30:35 -0700521 mLayerHistory->registerLayer(layer, minFps, maxFps,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800522 scheduler::LayerHistory::LayerVoteType::Heuristic);
523 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800524 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700525}
526
Ady Abraham5def7332020-05-29 16:13:47 -0700527void Scheduler::recordLayerHistory(Layer* layer, nsecs_t presentTime,
528 LayerHistory::LayerUpdateType updateType) {
Dominik Laskowski49cea512019-11-12 14:13:23 -0800529 if (mLayerHistory) {
Ady Abraham5def7332020-05-29 16:13:47 -0700530 mLayerHistory->record(layer, presentTime, systemTime(), updateType);
Dominik Laskowski49cea512019-11-12 14:13:23 -0800531 }
Ana Krulec3084c052018-11-21 20:27:17 +0100532}
533
Ady Abraham32efd542020-05-19 17:49:26 -0700534void Scheduler::setConfigChangePending(bool pending) {
535 if (mLayerHistory) {
536 mLayerHistory->setConfigChangePending(pending);
537 }
538}
539
Dominik Laskowski49cea512019-11-12 14:13:23 -0800540void Scheduler::chooseRefreshRateForContent() {
541 if (!mLayerHistory) return;
542
Ady Abraham8a82ba62020-01-17 12:43:17 -0800543 ATRACE_CALL();
544
545 scheduler::LayerHistory::Summary summary = mLayerHistory->summarize(systemTime());
Ady Abraham2139f732019-11-13 18:56:40 -0800546 HwcConfigIndexType newConfigId;
Ady Abraham6398a0a2019-04-18 19:30:44 -0700547 {
548 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800549 if (mFeatures.contentRequirements == summary) {
Ady Abraham6398a0a2019-04-18 19:30:44 -0700550 return;
551 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800552 mFeatures.contentRequirements = summary;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800553
Ady Abrahamdfd62162020-06-10 16:11:56 -0700554 scheduler::RefreshRateConfigs::GlobalSignals consideredSignals;
555 newConfigId = calculateRefreshRateConfigIndexType(&consideredSignals);
Ady Abraham2139f732019-11-13 18:56:40 -0800556 if (mFeatures.configId == newConfigId) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700557 // We don't need to change the config, but we might need to send an event
558 // about a config change, since it was suppressed due to a previous idleConsidered
559 if (!consideredSignals.idle) {
560 dispatchCachedReportedConfig();
561 }
Ady Abraham6398a0a2019-04-18 19:30:44 -0700562 return;
563 }
Ady Abraham2139f732019-11-13 18:56:40 -0800564 mFeatures.configId = newConfigId;
Ady Abraham2e1dd892020-03-05 13:48:36 -0800565 auto& newRefreshRate = mRefreshRateConfigs.getRefreshRateFromConfigId(newConfigId);
Ady Abrahamdfd62162020-06-10 16:11:56 -0700566 mSchedulerCallback.changeRefreshRate(newRefreshRate,
567 consideredSignals.idle ? ConfigEvent::None
568 : ConfigEvent::Changed);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800569 }
Ady Abrahama1a49af2019-02-07 14:36:55 -0800570}
571
Ana Krulecfb772822018-11-30 10:44:07 +0100572void Scheduler::resetIdleTimer() {
573 if (mIdleTimer) {
574 mIdleTimer->reset();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800575 }
576}
577
Ady Abraham8532d012019-05-08 14:50:56 -0700578void Scheduler::notifyTouchEvent() {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700579 if (mTouchTimer) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800580 mTouchTimer->reset();
Steven Thomas540730a2020-01-08 20:12:42 -0800581
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700582 if (mOptions.supportKernelTimer && mIdleTimer) {
Steven Thomas540730a2020-01-08 20:12:42 -0800583 mIdleTimer->reset();
584 }
Dominik Laskowski49cea512019-11-12 14:13:23 -0800585 }
Ady Abraham8532d012019-05-08 14:50:56 -0700586}
587
Ady Abraham6fe2c172019-07-12 12:37:57 -0700588void Scheduler::setDisplayPowerState(bool normal) {
589 {
590 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700591 mFeatures.isDisplayPowerStateNormal = normal;
Ady Abraham6fe2c172019-07-12 12:37:57 -0700592 }
593
594 if (mDisplayPowerTimer) {
595 mDisplayPowerTimer->reset();
596 }
597
598 // Display Power event will boost the refresh rate to performance.
599 // Clear Layer History to get fresh FPS detection
Dominik Laskowski49cea512019-11-12 14:13:23 -0800600 if (mLayerHistory) {
601 mLayerHistory->clear();
602 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700603}
604
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700605void Scheduler::kernelIdleTimerCallback(TimerState state) {
606 ATRACE_INT("ExpiredKernelIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100607
Ady Abraham2139f732019-11-13 18:56:40 -0800608 // TODO(145561154): cleanup the kernel idle timer implementation and the refresh rate
609 // magic number
Ady Abraham2e1dd892020-03-05 13:48:36 -0800610 const auto& refreshRate = mRefreshRateConfigs.getCurrentRefreshRate();
Ady Abraham2139f732019-11-13 18:56:40 -0800611 constexpr float FPS_THRESHOLD_FOR_KERNEL_TIMER = 65.0f;
Ady Abrahamabc27602020-04-08 17:20:29 -0700612 if (state == TimerState::Reset && refreshRate.getFps() > FPS_THRESHOLD_FOR_KERNEL_TIMER) {
Alec Mouri7f015182019-07-11 13:56:22 -0700613 // If we're not in performance mode then the kernel timer shouldn't do
614 // anything, as the refresh rate during DPU power collapse will be the
615 // same.
Ady Abrahamabc27602020-04-08 17:20:29 -0700616 resyncToHardwareVsync(true /* makeAvailable */, refreshRate.getVsyncPeriod());
617 } else if (state == TimerState::Expired &&
618 refreshRate.getFps() <= FPS_THRESHOLD_FOR_KERNEL_TIMER) {
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700619 // Disable HW VSYNC if the timer expired, as we don't need it enabled if
620 // we're not pushing frames, and if we're in PERFORMANCE mode then we'll
Ady Abraham8cb21882020-08-26 18:22:05 -0700621 // need to update the VsyncController model anyway.
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700622 disableHardwareVsync(false /* makeUnavailable */);
Alec Mouridc28b372019-04-18 21:17:13 -0700623 }
Ady Abrahama09852a2020-02-20 14:23:42 -0800624
625 mSchedulerCallback.kernelTimerChanged(state == TimerState::Expired);
Alec Mouridc28b372019-04-18 21:17:13 -0700626}
627
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700628void Scheduler::idleTimerCallback(TimerState state) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700629 handleTimerStateChanged(&mFeatures.idleTimer, state);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700630 ATRACE_INT("ExpiredIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100631}
632
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700633void Scheduler::touchTimerCallback(TimerState state) {
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700634 const TouchState touch = state == TimerState::Reset ? TouchState::Active : TouchState::Inactive;
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700635 // Touch event will boost the refresh rate to performance.
636 // Clear layer history to get fresh FPS detection.
637 // NOTE: Instead of checking all the layers, we should be checking the layer
638 // that is currently on top. b/142507166 will give us this capability.
Ady Abrahamdfd62162020-06-10 16:11:56 -0700639 if (handleTimerStateChanged(&mFeatures.touch, touch)) {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700640 if (mLayerHistory) {
641 mLayerHistory->clear();
642 }
Ady Abraham1adbb722020-05-15 11:51:48 -0700643 }
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700644 ATRACE_INT("TouchState", static_cast<int>(touch));
Ady Abraham8532d012019-05-08 14:50:56 -0700645}
646
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700647void Scheduler::displayPowerTimerCallback(TimerState state) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700648 handleTimerStateChanged(&mFeatures.displayPowerTimer, state);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700649 ATRACE_INT("ExpiredDisplayPowerTimer", static_cast<int>(state));
Alec Mouridc28b372019-04-18 21:17:13 -0700650}
651
Dominik Laskowski98041832019-08-01 18:35:59 -0700652void Scheduler::dump(std::string& result) const {
Dominik Laskowski49cea512019-11-12 14:13:23 -0800653 using base::StringAppendF;
Dominik Laskowski98041832019-08-01 18:35:59 -0700654
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700655 StringAppendF(&result, "+ Idle timer: %s\n", mIdleTimer ? mIdleTimer->dump().c_str() : "off");
Ana Krulec3d367c82020-02-25 15:02:01 -0800656 StringAppendF(&result, "+ Touch timer: %s\n",
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700657 mTouchTimer ? mTouchTimer->dump().c_str() : "off");
658 StringAppendF(&result, "+ Content detection: %s %s\n\n",
Marin Shalamanov27fa3de2020-11-20 16:22:48 +0100659 toContentDetectionString(mOptions.useContentDetection),
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700660 mLayerHistory ? mLayerHistory->dump().c_str() : "(no layer history)");
Ana Krulecb43429d2019-01-09 14:28:51 -0800661}
662
Ady Abraham8cb21882020-08-26 18:22:05 -0700663void Scheduler::dumpVsync(std::string& s) const {
Ady Abraham8735eac2020-08-12 16:35:04 -0700664 using base::StringAppendF;
665
666 StringAppendF(&s, "VSyncReactor:\n");
Ady Abraham8cb21882020-08-26 18:22:05 -0700667 mVsyncSchedule.controller->dump(s);
Ady Abraham8735eac2020-08-12 16:35:04 -0700668 StringAppendF(&s, "VSyncDispatch:\n");
669 mVsyncSchedule.dispatch->dump(s);
670}
671
Ady Abraham6fe2c172019-07-12 12:37:57 -0700672template <class T>
Ady Abrahamdfd62162020-06-10 16:11:56 -0700673bool Scheduler::handleTimerStateChanged(T* currentState, T newState) {
Ady Abraham2139f732019-11-13 18:56:40 -0800674 HwcConfigIndexType newConfigId;
Ady Abrahamdfd62162020-06-10 16:11:56 -0700675 scheduler::RefreshRateConfigs::GlobalSignals consideredSignals;
Ady Abraham8532d012019-05-08 14:50:56 -0700676 {
677 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham6fe2c172019-07-12 12:37:57 -0700678 if (*currentState == newState) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700679 return false;
Ady Abraham8532d012019-05-08 14:50:56 -0700680 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700681 *currentState = newState;
Ady Abrahamdfd62162020-06-10 16:11:56 -0700682 newConfigId = calculateRefreshRateConfigIndexType(&consideredSignals);
Ady Abraham2139f732019-11-13 18:56:40 -0800683 if (mFeatures.configId == newConfigId) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700684 // We don't need to change the config, but we might need to send an event
685 // about a config change, since it was suppressed due to a previous idleConsidered
686 if (!consideredSignals.idle) {
687 dispatchCachedReportedConfig();
688 }
689 return consideredSignals.touch;
Ady Abraham8532d012019-05-08 14:50:56 -0700690 }
Ady Abraham2139f732019-11-13 18:56:40 -0800691 mFeatures.configId = newConfigId;
Ady Abraham8532d012019-05-08 14:50:56 -0700692 }
Ady Abraham2139f732019-11-13 18:56:40 -0800693 const RefreshRate& newRefreshRate = mRefreshRateConfigs.getRefreshRateFromConfigId(newConfigId);
Ady Abrahamdfd62162020-06-10 16:11:56 -0700694 mSchedulerCallback.changeRefreshRate(newRefreshRate,
695 consideredSignals.idle ? ConfigEvent::None
696 : ConfigEvent::Changed);
697 return consideredSignals.touch;
Ady Abraham8532d012019-05-08 14:50:56 -0700698}
699
Ady Abrahamdfd62162020-06-10 16:11:56 -0700700HwcConfigIndexType Scheduler::calculateRefreshRateConfigIndexType(
701 scheduler::RefreshRateConfigs::GlobalSignals* consideredSignals) {
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800702 ATRACE_CALL();
Ady Abrahamdfd62162020-06-10 16:11:56 -0700703 if (consideredSignals) *consideredSignals = {};
Ady Abraham09bd3922019-04-08 10:44:56 -0700704
Steven Thomasf734df42020-04-13 21:09:28 -0700705 // If Display Power is not in normal operation we want to be in performance mode. When coming
706 // back to normal mode, a grace period is given with DisplayPowerTimer.
Ana Krulec3f6a2062020-01-23 15:48:01 -0800707 if (mDisplayPowerTimer &&
708 (!mFeatures.isDisplayPowerStateNormal ||
709 mFeatures.displayPowerTimer == TimerState::Reset)) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700710 return mRefreshRateConfigs.getMaxRefreshRateByPolicy().getConfigId();
Ana Krulec3f6a2062020-01-23 15:48:01 -0800711 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700712
Steven Thomasbb374322020-04-28 22:47:16 -0700713 const bool touchActive = mTouchTimer && mFeatures.touch == TouchState::Active;
714 const bool idle = mIdleTimer && mFeatures.idleTimer == TimerState::Expired;
715
Ady Abraham1adbb722020-05-15 11:51:48 -0700716 return mRefreshRateConfigs
Ady Abrahamdfd62162020-06-10 16:11:56 -0700717 .getBestRefreshRate(mFeatures.contentRequirements, {.touch = touchActive, .idle = idle},
718 consideredSignals)
Ady Abraham1adbb722020-05-15 11:51:48 -0700719 .getConfigId();
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800720}
721
Ady Abraham2139f732019-11-13 18:56:40 -0800722std::optional<HwcConfigIndexType> Scheduler::getPreferredConfigId() {
Daniel Solomon0f0ddc12019-08-19 19:31:09 -0700723 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ana Krulec3f6a2062020-01-23 15:48:01 -0800724 // Make sure that the default config ID is first updated, before returned.
725 if (mFeatures.configId.has_value()) {
Ana Krulec3803b8d2020-02-03 16:35:46 -0800726 mFeatures.configId = calculateRefreshRateConfigIndexType();
Ana Krulec3f6a2062020-01-23 15:48:01 -0800727 }
Ady Abraham2139f732019-11-13 18:56:40 -0800728 return mFeatures.configId;
Daniel Solomon0f0ddc12019-08-19 19:31:09 -0700729}
730
Peiyong Line9d809e2020-04-14 13:10:48 -0700731void Scheduler::onNewVsyncPeriodChangeTimeline(const hal::VsyncPeriodChangeTimeline& timeline) {
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800732 if (timeline.refreshRequired) {
733 mSchedulerCallback.repaintEverythingForHWC();
734 }
735
736 std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
737 mLastVsyncPeriodChangeTimeline = std::make_optional(timeline);
738
739 const auto maxAppliedTime = systemTime() + MAX_VSYNC_APPLIED_TIME.count();
740 if (timeline.newVsyncAppliedTimeNanos > maxAppliedTime) {
741 mLastVsyncPeriodChangeTimeline->newVsyncAppliedTimeNanos = maxAppliedTime;
742 }
743}
744
745void Scheduler::onDisplayRefreshed(nsecs_t timestamp) {
746 bool callRepaint = false;
747 {
748 std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
749 if (mLastVsyncPeriodChangeTimeline && mLastVsyncPeriodChangeTimeline->refreshRequired) {
750 if (mLastVsyncPeriodChangeTimeline->refreshTimeNanos < timestamp) {
751 mLastVsyncPeriodChangeTimeline->refreshRequired = false;
752 } else {
753 // We need to send another refresh as refreshTimeNanos is still in the future
754 callRepaint = true;
755 }
756 }
757 }
758
759 if (callRepaint) {
760 mSchedulerCallback.repaintEverythingForHWC();
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800761 }
762}
763
Ady Abraham8a82ba62020-01-17 12:43:17 -0800764void Scheduler::onPrimaryDisplayAreaChanged(uint32_t displayArea) {
765 if (mLayerHistory) {
766 mLayerHistory->setDisplayArea(displayArea);
767 }
768}
769
Ana Krulec98b5b242018-08-10 15:03:23 -0700770} // namespace android