blob: 0063c8a202e11f462a50895f0090d7902b1f2b46 [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
Ana Krulec7ab56032018-11-02 20:51:06 +010017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Ana Krulec98b5b242018-08-10 15:03:23 -070019#include "Scheduler.h"
20
Ana Krulec434c22d2018-11-28 13:48:36 +010021#include <algorithm>
Ana Krulec98b5b242018-08-10 15:03:23 -070022#include <cinttypes>
23#include <cstdint>
24#include <memory>
Ana Krulec7ab56032018-11-02 20:51:06 +010025#include <numeric>
Ana Krulec98b5b242018-08-10 15:03:23 -070026
Ana Krulece588e312018-09-18 12:32:24 -070027#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
28#include <android/hardware/configstore/1.1/ISurfaceFlingerConfigs.h>
Ana Krulece588e312018-09-18 12:32:24 -070029#include <configstore/Utils.h>
Ana Krulecfb772822018-11-30 10:44:07 +010030#include <cutils/properties.h>
Ana Krulecfefd6ae2019-02-13 17:53:08 -080031#include <system/window.h>
Ana Krulece588e312018-09-18 12:32:24 -070032#include <ui/DisplayStatInfo.h>
Ana Krulec3084c052018-11-21 20:27:17 +010033#include <utils/Timers.h>
Ana Krulec7ab56032018-11-02 20:51:06 +010034#include <utils/Trace.h>
Ana Krulec98b5b242018-08-10 15:03:23 -070035
36#include "DispSync.h"
37#include "DispSyncSource.h"
Ana Krulece588e312018-09-18 12:32:24 -070038#include "EventControlThread.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070039#include "EventThread.h"
Ana Krulecfb772822018-11-30 10:44:07 +010040#include "IdleTimer.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070041#include "InjectVSyncSource.h"
Ana Krulec434c22d2018-11-28 13:48:36 +010042#include "SchedulerUtils.h"
Sundong Ahnd5e08f62018-12-12 20:27:28 +090043#include "SurfaceFlingerProperties.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070044
45namespace android {
46
Ana Krulece588e312018-09-18 12:32:24 -070047using namespace android::hardware::configstore;
48using namespace android::hardware::configstore::V1_0;
Sundong Ahnd5e08f62018-12-12 20:27:28 +090049using namespace android::sysprop;
Ana Krulece588e312018-09-18 12:32:24 -070050
Ana Krulec0c8cd522018-08-31 12:27:28 -070051#define RETURN_VALUE_IF_INVALID(value) \
52 if (handle == nullptr || mConnections.count(handle->id) == 0) return value
53#define RETURN_IF_INVALID() \
54 if (handle == nullptr || mConnections.count(handle->id) == 0) return
55
Ana Krulec98b5b242018-08-10 15:03:23 -070056std::atomic<int64_t> Scheduler::sNextId = 0;
57
Ana Krulece588e312018-09-18 12:32:24 -070058Scheduler::Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function)
Sundong Ahnd5e08f62018-12-12 20:27:28 +090059 : mHasSyncFramework(running_without_sync_framework(true)),
60 mDispSyncPresentTimeOffset(present_time_offset_from_vsync_ns(0)),
Ana Krulece588e312018-09-18 12:32:24 -070061 mPrimaryHWVsyncEnabled(false),
62 mHWVsyncAvailable(false) {
63 // Note: We create a local temporary with the real DispSync implementation
64 // type temporarily so we can initialize it with the configured values,
65 // before storing it for more generic use using the interface type.
66 auto primaryDispSync = std::make_unique<impl::DispSync>("SchedulerDispSync");
67 primaryDispSync->init(mHasSyncFramework, mDispSyncPresentTimeOffset);
68 mPrimaryDispSync = std::move(primaryDispSync);
69 mEventControlThread = std::make_unique<impl::EventControlThread>(function);
Ana Krulecfb772822018-11-30 10:44:07 +010070
Ady Abrahambe59c0d2019-03-05 13:01:13 -080071 mSetIdleTimerMs = set_idle_timer_ms(0);
72
Ana Krulecfb772822018-11-30 10:44:07 +010073 char value[PROPERTY_VALUE_MAX];
Ana Kruleca5bdd9d2019-01-29 19:00:58 -080074 property_get("debug.sf.set_idle_timer_ms", value, "0");
Ady Abrahambe59c0d2019-03-05 13:01:13 -080075 int int_value = atoi(value);
76 if (int_value) {
77 mSetIdleTimerMs = atoi(value);
78 }
Ana Krulecfb772822018-11-30 10:44:07 +010079
80 if (mSetIdleTimerMs > 0) {
81 mIdleTimer =
82 std::make_unique<scheduler::IdleTimer>(std::chrono::milliseconds(mSetIdleTimerMs),
Ady Abrahama1a49af2019-02-07 14:36:55 -080083 [this] { resetTimerCallback(); },
Ana Krulecfb772822018-11-30 10:44:07 +010084 [this] { expiredTimerCallback(); });
85 mIdleTimer->start();
86 }
Ana Krulece588e312018-09-18 12:32:24 -070087}
88
Lloyd Pique1f9f1a42019-01-31 13:04:00 -080089Scheduler::~Scheduler() {
90 // Ensure the IdleTimer thread is joined before we start destroying state.
91 mIdleTimer.reset();
92}
Ana Krulec0c8cd522018-08-31 12:27:28 -070093
Ana Krulec98b5b242018-08-10 15:03:23 -070094sp<Scheduler::ConnectionHandle> Scheduler::createConnection(
Dominik Laskowskibd52c842019-01-28 18:11:23 -080095 const char* connectionName, int64_t phaseOffsetNs, ResyncCallback resyncCallback,
Ana Krulec98b5b242018-08-10 15:03:23 -070096 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
97 const int64_t id = sNextId++;
98 ALOGV("Creating a connection handle with ID: %" PRId64 "\n", id);
99
Ana Krulec98b5b242018-08-10 15:03:23 -0700100 std::unique_ptr<EventThread> eventThread =
Dominik Laskowskif654d572018-12-20 11:03:06 -0800101 makeEventThread(connectionName, mPrimaryDispSync.get(), phaseOffsetNs,
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800102 std::move(interceptCallback));
Dominik Laskowskif654d572018-12-20 11:03:06 -0800103
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800104 auto eventThreadConnection =
Ady Abrahama1a49af2019-02-07 14:36:55 -0800105 createConnectionInternal(eventThread.get(), std::move(resyncCallback));
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800106 mConnections.emplace(id,
107 std::make_unique<Connection>(new ConnectionHandle(id),
108 eventThreadConnection,
109 std::move(eventThread)));
Ana Krulec98b5b242018-08-10 15:03:23 -0700110 return mConnections[id]->handle;
111}
112
Ana Krulec0c8cd522018-08-31 12:27:28 -0700113std::unique_ptr<EventThread> Scheduler::makeEventThread(
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800114 const char* connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
Ana Krulec0c8cd522018-08-31 12:27:28 -0700115 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
116 std::unique_ptr<VSyncSource> eventThreadSource =
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800117 std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, true, connectionName);
118 return std::make_unique<impl::EventThread>(std::move(eventThreadSource),
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800119 std::move(interceptCallback), connectionName);
120}
121
Ady Abrahama1a49af2019-02-07 14:36:55 -0800122sp<EventThreadConnection> Scheduler::createConnectionInternal(EventThread* eventThread,
123 ResyncCallback&& resyncCallback) {
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800124 return eventThread->createEventConnection(std::move(resyncCallback),
Ady Abrahama1a49af2019-02-07 14:36:55 -0800125 [this] { resetIdleTimer(); });
Ana Krulec0c8cd522018-08-31 12:27:28 -0700126}
127
Ana Krulec98b5b242018-08-10 15:03:23 -0700128sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Ady Abrahama1a49af2019-02-07 14:36:55 -0800129 const sp<Scheduler::ConnectionHandle>& handle, ResyncCallback resyncCallback) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700130 RETURN_VALUE_IF_INVALID(nullptr);
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800131 return createConnectionInternal(mConnections[handle->id]->thread.get(),
Ady Abrahama1a49af2019-02-07 14:36:55 -0800132 std::move(resyncCallback));
Ana Krulec98b5b242018-08-10 15:03:23 -0700133}
134
135EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700136 RETURN_VALUE_IF_INVALID(nullptr);
137 return mConnections[handle->id]->thread.get();
Ana Krulec98b5b242018-08-10 15:03:23 -0700138}
139
Ana Krulec85c39af2018-12-26 17:29:57 -0800140sp<EventThreadConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700141 RETURN_VALUE_IF_INVALID(nullptr);
142 return mConnections[handle->id]->eventConnection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700143}
144
145void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle,
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800146 PhysicalDisplayId displayId, bool connected) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700147 RETURN_IF_INVALID();
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800148 mConnections[handle->id]->thread->onHotplugReceived(displayId, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700149}
150
151void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700152 RETURN_IF_INVALID();
153 mConnections[handle->id]->thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700154}
155
156void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700157 RETURN_IF_INVALID();
158 mConnections[handle->id]->thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700159}
160
Ady Abraham447052e2019-02-13 16:07:27 -0800161void Scheduler::onConfigChanged(const sp<ConnectionHandle>& handle, PhysicalDisplayId displayId,
162 int32_t configId) {
163 RETURN_IF_INVALID();
164 mConnections[handle->id]->thread->onConfigChanged(displayId, configId);
165}
166
Yiwei Zhang5434a782018-12-05 18:06:32 -0800167void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, std::string& result) const {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700168 RETURN_IF_INVALID();
169 mConnections.at(handle->id)->thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700170}
171
172void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700173 RETURN_IF_INVALID();
174 mConnections[handle->id]->thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700175}
Ana Krulece588e312018-09-18 12:32:24 -0700176
Ady Abrahamb838aed2019-02-12 15:30:16 -0800177void Scheduler::pauseVsyncCallback(const android::sp<android::Scheduler::ConnectionHandle>& handle,
178 bool pause) {
179 RETURN_IF_INVALID();
180 mConnections[handle->id]->thread->pauseVsyncCallback(pause);
181}
182
Ana Krulece588e312018-09-18 12:32:24 -0700183void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
184 stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0);
185 stats->vsyncPeriod = mPrimaryDispSync->getPeriod();
186}
187
188void Scheduler::enableHardwareVsync() {
189 std::lock_guard<std::mutex> lock(mHWVsyncLock);
190 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
191 mPrimaryDispSync->beginResync();
192 mEventControlThread->setVsyncEnabled(true);
193 mPrimaryHWVsyncEnabled = true;
194 }
195}
196
197void Scheduler::disableHardwareVsync(bool makeUnavailable) {
198 std::lock_guard<std::mutex> lock(mHWVsyncLock);
199 if (mPrimaryHWVsyncEnabled) {
200 mEventControlThread->setVsyncEnabled(false);
201 mPrimaryDispSync->endResync();
202 mPrimaryHWVsyncEnabled = false;
203 }
204 if (makeUnavailable) {
205 mHWVsyncAvailable = false;
206 }
207}
208
Ana Krulecc2870422019-01-29 19:00:58 -0800209void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) {
210 {
211 std::lock_guard<std::mutex> lock(mHWVsyncLock);
212 if (makeAvailable) {
213 mHWVsyncAvailable = makeAvailable;
214 } else if (!mHWVsyncAvailable) {
215 // Hardware vsync is not currently available, so abort the resync
216 // attempt for now
217 return;
218 }
219 }
220
221 if (period <= 0) {
222 return;
223 }
224
225 setVsyncPeriod(period);
226}
227
228ResyncCallback Scheduler::makeResyncCallback(GetVsyncPeriod&& getVsyncPeriod) {
229 std::weak_ptr<VsyncState> ptr = mPrimaryVsyncState;
230 return [ptr, getVsyncPeriod = std::move(getVsyncPeriod)]() {
231 if (const auto vsync = ptr.lock()) {
232 vsync->resync(getVsyncPeriod);
233 }
234 };
235}
236
237void Scheduler::VsyncState::resync(const GetVsyncPeriod& getVsyncPeriod) {
238 static constexpr nsecs_t kIgnoreDelay = ms2ns(500);
239
240 const nsecs_t now = systemTime();
241 const nsecs_t last = lastResyncTime.exchange(now);
242
243 if (now - last > kIgnoreDelay) {
244 scheduler.resyncToHardwareVsync(false, getVsyncPeriod());
245 }
246}
247
248void Scheduler::setRefreshSkipCount(int count) {
249 mPrimaryDispSync->setRefreshSkipCount(count);
250}
251
Ana Krulece588e312018-09-18 12:32:24 -0700252void Scheduler::setVsyncPeriod(const nsecs_t period) {
Ady Abraham3aff9172019-02-07 19:10:26 -0800253 std::lock_guard<std::mutex> lock(mHWVsyncLock);
Ana Krulece588e312018-09-18 12:32:24 -0700254 mPrimaryDispSync->reset();
255 mPrimaryDispSync->setPeriod(period);
Ady Abraham3aff9172019-02-07 19:10:26 -0800256
257 if (!mPrimaryHWVsyncEnabled) {
258 mPrimaryDispSync->beginResync();
259 mEventControlThread->setVsyncEnabled(true);
260 mPrimaryHWVsyncEnabled = true;
261 }
Ana Krulece588e312018-09-18 12:32:24 -0700262}
263
264void Scheduler::addResyncSample(const nsecs_t timestamp) {
265 bool needsHwVsync = false;
266 { // Scope for the lock
267 std::lock_guard<std::mutex> lock(mHWVsyncLock);
268 if (mPrimaryHWVsyncEnabled) {
269 needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp);
270 }
271 }
272
273 if (needsHwVsync) {
274 enableHardwareVsync();
275 } else {
276 disableHardwareVsync(false);
277 }
278}
279
280void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
281 if (mPrimaryDispSync->addPresentFence(fenceTime)) {
282 enableHardwareVsync();
283 } else {
284 disableHardwareVsync(false);
285 }
286}
287
288void Scheduler::setIgnorePresentFences(bool ignore) {
289 mPrimaryDispSync->setIgnorePresentFences(ignore);
290}
291
Ady Abrahamc3e21312019-02-07 14:30:23 -0800292nsecs_t Scheduler::expectedPresentTime() {
293 return mPrimaryDispSync->expectedPresentTime();
294}
295
Ady Abraham3aff9172019-02-07 19:10:26 -0800296void Scheduler::dumpPrimaryDispSync(std::string& result) const {
297 mPrimaryDispSync->dump(result);
298}
299
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800300void Scheduler::addNativeWindowApi(int apiId) {
301 std::lock_guard<std::mutex> lock(mWindowApiHistoryLock);
302 mWindowApiHistory[mApiHistoryCounter] = apiId;
303 mApiHistoryCounter++;
304 mApiHistoryCounter = mApiHistoryCounter % scheduler::ARRAY_SIZE;
Ana Krulec3084c052018-11-21 20:27:17 +0100305}
306
Kevin DuBois413287f2019-02-25 08:46:47 -0800307void Scheduler::withPrimaryDispSync(std::function<void(DispSync&)> const& fn) {
308 fn(*mPrimaryDispSync);
309}
310
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800311void Scheduler::updateFpsBasedOnNativeWindowApi() {
312 int mode;
313 {
314 std::lock_guard<std::mutex> lock(mWindowApiHistoryLock);
315 mode = scheduler::calculate_mode(mWindowApiHistory);
316 }
317 ATRACE_INT("NativeWindowApiMode", mode);
318
319 if (mode == NATIVE_WINDOW_API_MEDIA) {
320 // TODO(b/127365162): These callback names are not accurate anymore. Update.
321 mediaChangeRefreshRate(MediaFeatureState::MEDIA_PLAYING);
322 ATRACE_INT("DetectedVideo", 1);
323 } else {
324 mediaChangeRefreshRate(MediaFeatureState::MEDIA_OFF);
325 ATRACE_INT("DetectedVideo", 0);
326 }
Ana Krulec3084c052018-11-21 20:27:17 +0100327}
328
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800329void Scheduler::setChangeRefreshRateCallback(
330 const ChangeRefreshRateCallback& changeRefreshRateCallback) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800331 std::lock_guard<std::mutex> lock(mCallbackLock);
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800332 mChangeRefreshRateCallback = changeRefreshRateCallback;
Ady Abrahama1a49af2019-02-07 14:36:55 -0800333}
334
Ana Krulec3084c052018-11-21 20:27:17 +0100335void Scheduler::updateFrameSkipping(const int64_t skipCount) {
336 ATRACE_INT("FrameSkipCount", skipCount);
337 if (mSkipCount != skipCount) {
338 // Only update DispSync if it hasn't been updated yet.
339 mPrimaryDispSync->setRefreshSkipCount(skipCount);
340 mSkipCount = skipCount;
341 }
342}
343
Ana Krulecfb772822018-11-30 10:44:07 +0100344void Scheduler::resetIdleTimer() {
345 if (mIdleTimer) {
346 mIdleTimer->reset();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800347 }
348}
349
350void Scheduler::resetTimerCallback() {
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800351 // We do not notify the applications about config changes when idle timer is reset.
352 timerChangeRefreshRate(IdleTimerState::RESET);
353 ATRACE_INT("ExpiredIdleTimer", 0);
Ana Krulecfb772822018-11-30 10:44:07 +0100354}
355
356void Scheduler::expiredTimerCallback() {
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800357 // We do not notify the applications about config changes when idle timer expires.
358 timerChangeRefreshRate(IdleTimerState::EXPIRED);
359 ATRACE_INT("ExpiredIdleTimer", 1);
Ana Krulecfb772822018-11-30 10:44:07 +0100360}
361
Ana Krulecb43429d2019-01-09 14:28:51 -0800362std::string Scheduler::doDump() {
363 std::ostringstream stream;
364 stream << "+ Idle timer interval: " << mSetIdleTimerMs << " ms" << std::endl;
365 return stream.str();
366}
367
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800368void Scheduler::mediaChangeRefreshRate(MediaFeatureState mediaFeatureState) {
369 // Default playback for media is DEFAULT when media is on.
370 RefreshRateType refreshRateType = RefreshRateType::DEFAULT;
371 ConfigEvent configEvent = ConfigEvent::None;
372
373 {
374 std::lock_guard<std::mutex> lock(mFeatureStateLock);
375 mCurrentMediaFeatureState = mediaFeatureState;
376 // Only switch to PERFORMANCE if idle timer was reset, when turning
377 // media off. If the timer is IDLE, stay at DEFAULT.
378 if (mediaFeatureState == MediaFeatureState::MEDIA_OFF &&
379 mCurrentIdleTimerState == IdleTimerState::RESET) {
380 refreshRateType = RefreshRateType::PERFORMANCE;
381 }
382 }
383 changeRefreshRate(refreshRateType, configEvent);
384}
385
386void Scheduler::timerChangeRefreshRate(IdleTimerState idleTimerState) {
387 RefreshRateType refreshRateType = RefreshRateType::DEFAULT;
388 ConfigEvent configEvent = ConfigEvent::None;
389
390 {
391 std::lock_guard<std::mutex> lock(mFeatureStateLock);
392 mCurrentIdleTimerState = idleTimerState;
393 // Only switch to PERFOMANCE if the idle timer was reset, and media is
394 // not playing. Otherwise, stay at DEFAULT.
395 if (idleTimerState == IdleTimerState::RESET &&
396 mCurrentMediaFeatureState == MediaFeatureState::MEDIA_OFF) {
397 refreshRateType = RefreshRateType::PERFORMANCE;
398 }
399 }
400 changeRefreshRate(refreshRateType, configEvent);
401}
402
403void Scheduler::changeRefreshRate(RefreshRateType refreshRateType, ConfigEvent configEvent) {
404 std::lock_guard<std::mutex> lock(mCallbackLock);
405 if (mChangeRefreshRateCallback) {
406 mChangeRefreshRateCallback(refreshRateType, configEvent);
407 }
408}
409
Ana Krulec98b5b242018-08-10 15:03:23 -0700410} // namespace android