blob: 0ba6cf97e355beab399b67fdf9e05ada4e32bffc [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
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800307void Scheduler::updateFpsBasedOnNativeWindowApi() {
308 int mode;
309 {
310 std::lock_guard<std::mutex> lock(mWindowApiHistoryLock);
311 mode = scheduler::calculate_mode(mWindowApiHistory);
312 }
313 ATRACE_INT("NativeWindowApiMode", mode);
314
315 if (mode == NATIVE_WINDOW_API_MEDIA) {
316 // TODO(b/127365162): These callback names are not accurate anymore. Update.
317 mediaChangeRefreshRate(MediaFeatureState::MEDIA_PLAYING);
318 ATRACE_INT("DetectedVideo", 1);
319 } else {
320 mediaChangeRefreshRate(MediaFeatureState::MEDIA_OFF);
321 ATRACE_INT("DetectedVideo", 0);
322 }
Ana Krulec3084c052018-11-21 20:27:17 +0100323}
324
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800325void Scheduler::setChangeRefreshRateCallback(
326 const ChangeRefreshRateCallback& changeRefreshRateCallback) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800327 std::lock_guard<std::mutex> lock(mCallbackLock);
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800328 mChangeRefreshRateCallback = changeRefreshRateCallback;
Ady Abrahama1a49af2019-02-07 14:36:55 -0800329}
330
Ana Krulec3084c052018-11-21 20:27:17 +0100331void Scheduler::updateFrameSkipping(const int64_t skipCount) {
332 ATRACE_INT("FrameSkipCount", skipCount);
333 if (mSkipCount != skipCount) {
334 // Only update DispSync if it hasn't been updated yet.
335 mPrimaryDispSync->setRefreshSkipCount(skipCount);
336 mSkipCount = skipCount;
337 }
338}
339
Ana Krulecfb772822018-11-30 10:44:07 +0100340void Scheduler::resetIdleTimer() {
341 if (mIdleTimer) {
342 mIdleTimer->reset();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800343 }
344}
345
346void Scheduler::resetTimerCallback() {
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800347 // We do not notify the applications about config changes when idle timer is reset.
348 timerChangeRefreshRate(IdleTimerState::RESET);
349 ATRACE_INT("ExpiredIdleTimer", 0);
Ana Krulecfb772822018-11-30 10:44:07 +0100350}
351
352void Scheduler::expiredTimerCallback() {
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800353 // We do not notify the applications about config changes when idle timer expires.
354 timerChangeRefreshRate(IdleTimerState::EXPIRED);
355 ATRACE_INT("ExpiredIdleTimer", 1);
Ana Krulecfb772822018-11-30 10:44:07 +0100356}
357
Ana Krulecb43429d2019-01-09 14:28:51 -0800358std::string Scheduler::doDump() {
359 std::ostringstream stream;
360 stream << "+ Idle timer interval: " << mSetIdleTimerMs << " ms" << std::endl;
361 return stream.str();
362}
363
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800364void Scheduler::mediaChangeRefreshRate(MediaFeatureState mediaFeatureState) {
365 // Default playback for media is DEFAULT when media is on.
366 RefreshRateType refreshRateType = RefreshRateType::DEFAULT;
367 ConfigEvent configEvent = ConfigEvent::None;
368
369 {
370 std::lock_guard<std::mutex> lock(mFeatureStateLock);
371 mCurrentMediaFeatureState = mediaFeatureState;
372 // Only switch to PERFORMANCE if idle timer was reset, when turning
373 // media off. If the timer is IDLE, stay at DEFAULT.
374 if (mediaFeatureState == MediaFeatureState::MEDIA_OFF &&
375 mCurrentIdleTimerState == IdleTimerState::RESET) {
376 refreshRateType = RefreshRateType::PERFORMANCE;
377 }
378 }
379 changeRefreshRate(refreshRateType, configEvent);
380}
381
382void Scheduler::timerChangeRefreshRate(IdleTimerState idleTimerState) {
383 RefreshRateType refreshRateType = RefreshRateType::DEFAULT;
384 ConfigEvent configEvent = ConfigEvent::None;
385
386 {
387 std::lock_guard<std::mutex> lock(mFeatureStateLock);
388 mCurrentIdleTimerState = idleTimerState;
389 // Only switch to PERFOMANCE if the idle timer was reset, and media is
390 // not playing. Otherwise, stay at DEFAULT.
391 if (idleTimerState == IdleTimerState::RESET &&
392 mCurrentMediaFeatureState == MediaFeatureState::MEDIA_OFF) {
393 refreshRateType = RefreshRateType::PERFORMANCE;
394 }
395 }
396 changeRefreshRate(refreshRateType, configEvent);
397}
398
399void Scheduler::changeRefreshRate(RefreshRateType refreshRateType, ConfigEvent configEvent) {
400 std::lock_guard<std::mutex> lock(mCallbackLock);
401 if (mChangeRefreshRateCallback) {
402 mChangeRefreshRateCallback(refreshRateType, configEvent);
403 }
404}
405
Ana Krulec98b5b242018-08-10 15:03:23 -0700406} // namespace android