blob: 281f6b70698fa4dc71b1f6b72141343588e3a96b [file] [log] [blame]
Mathias Agopiand0566bc2011-11-17 17:49:17 -08001/*
2 * Copyright (C) 2011 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
Mathias Agopian841cde52012-03-01 15:44:37 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Lloyd Pique46a46b32018-01-31 19:01:18 -080019#include <pthread.h>
20#include <sched.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080021#include <sys/types.h>
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080022
Lloyd Pique46a46b32018-01-31 19:01:18 -080023#include <chrono>
24#include <cstdint>
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080025#include <optional>
26#include <type_traits>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080027
Yiwei Zhang5434a782018-12-05 18:06:32 -080028#include <android-base/stringprintf.h>
29
Mathias Agopiana4cb35a2012-08-20 20:07:34 -070030#include <cutils/compiler.h>
Lloyd Pique46a46b32018-01-31 19:01:18 -080031#include <cutils/sched_policy.h>
Mathias Agopiana4cb35a2012-08-20 20:07:34 -070032
Mathias Agopiand0566bc2011-11-17 17:49:17 -080033#include <gui/DisplayEventReceiver.h>
34
35#include <utils/Errors.h>
Mathias Agopian841cde52012-03-01 15:44:37 -080036#include <utils/Trace.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080037
Mathias Agopiand0566bc2011-11-17 17:49:17 -080038#include "EventThread.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080039
Lloyd Pique46a46b32018-01-31 19:01:18 -080040using namespace std::chrono_literals;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080041
Lloyd Pique46a46b32018-01-31 19:01:18 -080042namespace android {
43
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080044using base::StringAppendF;
45using base::StringPrintf;
46
47namespace {
48
49auto vsyncPeriod(VSyncRequest request) {
50 return static_cast<std::underlying_type_t<VSyncRequest>>(request);
51}
52
53std::string toString(VSyncRequest request) {
54 switch (request) {
55 case VSyncRequest::None:
56 return "VSyncRequest::None";
57 case VSyncRequest::Single:
58 return "VSyncRequest::Single";
59 default:
60 return StringPrintf("VSyncRequest::Periodic{period=%d}", vsyncPeriod(request));
61 }
62}
63
64std::string toString(const EventThreadConnection& connection) {
65 return StringPrintf("Connection{%p, %s}", &connection,
66 toString(connection.vsyncRequest).c_str());
67}
68
69std::string toString(const DisplayEventReceiver::Event& event) {
70 switch (event.header.type) {
71 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
72 return StringPrintf("Hotplug{displayId=%u, %s}", event.header.id,
73 event.hotplug.connected ? "connected" : "disconnected");
74 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
75 return StringPrintf("VSync{displayId=%u, count=%u}", event.header.id,
76 event.vsync.count);
77 default:
78 return "Event{}";
79 }
80}
81
Dominik Laskowski23b867a2019-01-22 12:44:53 -080082DisplayEventReceiver::Event makeHotplug(uint32_t displayId, nsecs_t timestamp, bool connected) {
83 DisplayEventReceiver::Event event;
84 event.header = {DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, displayId, timestamp};
85 event.hotplug.connected = connected;
86 return event;
87}
88
89DisplayEventReceiver::Event makeVSync(uint32_t displayId, nsecs_t timestamp, uint32_t count) {
90 DisplayEventReceiver::Event event;
91 event.header = {DisplayEventReceiver::DISPLAY_EVENT_VSYNC, displayId, timestamp};
92 event.vsync.count = count;
93 return event;
94}
95
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080096} // namespace
Lloyd Pique46a46b32018-01-31 19:01:18 -080097
Dominik Laskowskif654d572018-12-20 11:03:06 -080098EventThreadConnection::EventThreadConnection(EventThread* eventThread,
99 ResyncCallback resyncCallback)
100 : resyncCallback(std::move(resyncCallback)),
Dominik Laskowskif654d572018-12-20 11:03:06 -0800101 mEventThread(eventThread),
102 mChannel(gui::BitTube::DefaultSize) {}
Ana Krulec85c39af2018-12-26 17:29:57 -0800103
104EventThreadConnection::~EventThreadConnection() {
105 // do nothing here -- clean-up will happen automatically
106 // when the main thread wakes up
107}
108
109void EventThreadConnection::onFirstRef() {
110 // NOTE: mEventThread doesn't hold a strong reference on us
111 mEventThread->registerDisplayEventConnection(this);
112}
113
114status_t EventThreadConnection::stealReceiveChannel(gui::BitTube* outChannel) {
115 outChannel->setReceiveFd(mChannel.moveReceiveFd());
116 return NO_ERROR;
117}
118
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800119status_t EventThreadConnection::setVsyncRate(uint32_t rate) {
120 mEventThread->setVsyncRate(rate, this);
Ana Krulec85c39af2018-12-26 17:29:57 -0800121 return NO_ERROR;
122}
123
124void EventThreadConnection::requestNextVsync() {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800125 ATRACE_NAME("requestNextVsync");
126 mEventThread->requestNextVsync(this, true);
127}
128
129void EventThreadConnection::requestNextVsyncForHWC() {
130 ATRACE_NAME("requestNextVsyncForHWC");
131 mEventThread->requestNextVsync(this, false);
Ana Krulec85c39af2018-12-26 17:29:57 -0800132}
133
134status_t EventThreadConnection::postEvent(const DisplayEventReceiver::Event& event) {
135 ssize_t size = DisplayEventReceiver::sendEvents(&mChannel, &event, 1);
136 return size < 0 ? status_t(size) : status_t(NO_ERROR);
137}
138
139// ---------------------------------------------------------------------------
140
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800141EventThread::~EventThread() = default;
142
143namespace impl {
144
Ana Krulec98b5b242018-08-10 15:03:23 -0700145EventThread::EventThread(std::unique_ptr<VSyncSource> src,
Ana Krulecfb772822018-11-30 10:44:07 +0100146 const InterceptVSyncsCallback& interceptVSyncsCallback,
147 const ResetIdleTimerCallback& resetIdleTimerCallback,
148 const char* threadName)
Dominik Laskowskif654d572018-12-20 11:03:06 -0800149 : EventThread(nullptr, std::move(src), interceptVSyncsCallback, threadName) {
Ana Krulecfb772822018-11-30 10:44:07 +0100150 mResetIdleTimer = resetIdleTimerCallback;
151}
Ana Krulec98b5b242018-08-10 15:03:23 -0700152
Dominik Laskowskif654d572018-12-20 11:03:06 -0800153EventThread::EventThread(VSyncSource* src, InterceptVSyncsCallback interceptVSyncsCallback,
154 const char* threadName)
155 : EventThread(src, nullptr, interceptVSyncsCallback, threadName) {}
Ana Krulec98b5b242018-08-10 15:03:23 -0700156
157EventThread::EventThread(VSyncSource* src, std::unique_ptr<VSyncSource> uniqueSrc,
Ana Krulec98b5b242018-08-10 15:03:23 -0700158 InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName)
Lloyd Pique24b0a482018-03-09 18:52:26 -0800159 : mVSyncSource(src),
Ana Krulec98b5b242018-08-10 15:03:23 -0700160 mVSyncSourceUnique(std::move(uniqueSrc)),
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800161 mInterceptVSyncsCallback(interceptVSyncsCallback),
162 mThreadName(threadName) {
Ana Krulec98b5b242018-08-10 15:03:23 -0700163 if (src == nullptr) {
164 mVSyncSource = mVSyncSourceUnique.get();
165 }
Lloyd Pique46a46b32018-01-31 19:01:18 -0800166
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800167 mThread = std::thread([this]() NO_THREAD_SAFETY_ANALYSIS {
168 std::unique_lock<std::mutex> lock(mMutex);
169 threadMain(lock);
170 });
Lloyd Pique46a46b32018-01-31 19:01:18 -0800171
172 pthread_setname_np(mThread.native_handle(), threadName);
173
174 pid_t tid = pthread_gettid_np(mThread.native_handle());
175
176 // Use SCHED_FIFO to minimize jitter
177 constexpr int EVENT_THREAD_PRIORITY = 2;
178 struct sched_param param = {0};
179 param.sched_priority = EVENT_THREAD_PRIORITY;
180 if (pthread_setschedparam(mThread.native_handle(), SCHED_FIFO, &param) != 0) {
181 ALOGE("Couldn't set SCHED_FIFO for EventThread");
182 }
183
184 set_sched_policy(tid, SP_FOREGROUND);
185}
186
187EventThread::~EventThread() {
188 {
189 std::lock_guard<std::mutex> lock(mMutex);
190 mKeepRunning = false;
191 mCondition.notify_all();
192 }
193 mThread.join();
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700194}
195
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700196void EventThread::setPhaseOffset(nsecs_t phaseOffset) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800197 std::lock_guard<std::mutex> lock(mMutex);
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700198 mVSyncSource->setPhaseOffset(phaseOffset);
199}
200
Dominik Laskowskif654d572018-12-20 11:03:06 -0800201sp<EventThreadConnection> EventThread::createEventConnection(ResyncCallback resyncCallback) const {
202 return new EventThreadConnection(const_cast<EventThread*>(this), std::move(resyncCallback));
Mathias Agopian8aedd472012-01-24 16:39:14 -0800203}
204
Ana Krulec85c39af2018-12-26 17:29:57 -0800205status_t EventThread::registerDisplayEventConnection(const sp<EventThreadConnection>& connection) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800206 std::lock_guard<std::mutex> lock(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700207
208 // this should never happen
209 auto it = std::find(mDisplayEventConnections.cbegin(),
210 mDisplayEventConnections.cend(), connection);
211 if (it != mDisplayEventConnections.cend()) {
212 ALOGW("DisplayEventConnection %p already exists", connection.get());
213 mCondition.notify_all();
214 return ALREADY_EXISTS;
215 }
216
217 mDisplayEventConnections.push_back(connection);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800218 mCondition.notify_all();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800219 return NO_ERROR;
220}
221
Ana Krulec85c39af2018-12-26 17:29:57 -0800222void EventThread::removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection) {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700223 auto it = std::find(mDisplayEventConnections.cbegin(),
224 mDisplayEventConnections.cend(), connection);
225 if (it != mDisplayEventConnections.cend()) {
226 mDisplayEventConnections.erase(it);
227 }
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800228}
229
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800230void EventThread::setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) {
231 if (static_cast<std::underlying_type_t<VSyncRequest>>(rate) < 0) {
232 return;
233 }
234
235 std::lock_guard<std::mutex> lock(mMutex);
236
237 const auto request = rate == 0 ? VSyncRequest::None : static_cast<VSyncRequest>(rate);
238 if (connection->vsyncRequest != request) {
239 connection->vsyncRequest = request;
240 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800241 }
242}
243
Ana Krulec7d1d6832018-12-27 11:10:09 -0800244void EventThread::requestNextVsync(const sp<EventThreadConnection>& connection, bool reset) {
245 if (mResetIdleTimer && reset) {
246 ATRACE_NAME("resetIdleTimer");
Ana Krulecfb772822018-11-30 10:44:07 +0100247 mResetIdleTimer();
248 }
Dominik Laskowskif654d572018-12-20 11:03:06 -0800249
250 if (connection->resyncCallback) {
251 connection->resyncCallback();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800252 }
Tim Murray4a4e4a22016-04-19 16:29:23 +0000253
Ana Krulec7d1d6832018-12-27 11:10:09 -0800254 std::lock_guard<std::mutex> lock(mMutex);
255
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800256 if (connection->vsyncRequest == VSyncRequest::None) {
257 connection->vsyncRequest = VSyncRequest::Single;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800258 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800259 }
Mathias Agopian23748662011-12-05 14:33:34 -0800260}
261
Mathias Agopian22ffb112012-04-10 21:04:02 -0700262void EventThread::onScreenReleased() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800263 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800264 if (!mVSyncState.synthetic) {
265 mVSyncState.synthetic = true;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800266 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700267 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700268}
269
270void EventThread::onScreenAcquired() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800271 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800272 if (mVSyncState.synthetic) {
273 mVSyncState.synthetic = false;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800274 mCondition.notify_all();
Mathias Agopian7d886472012-06-14 23:39:35 -0700275 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700276}
277
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700278void EventThread::onVSyncEvent(nsecs_t timestamp) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800279 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800280
281 mPendingEvents.push_back(makeVSync(mVSyncState.displayId, timestamp, ++mVSyncState.count));
Lloyd Pique46a46b32018-01-31 19:01:18 -0800282 mCondition.notify_all();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700283}
284
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700285void EventThread::onHotplugReceived(DisplayType displayType, bool connected) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800286 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700287
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800288 const uint32_t displayId = displayType == DisplayType::Primary ? 0 : 1;
289 mPendingEvents.push_back(makeHotplug(displayId, systemTime(), connected));
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700290 mCondition.notify_all();
Mathias Agopian148994e2012-09-19 17:31:36 -0700291}
292
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800293void EventThread::threadMain(std::unique_lock<std::mutex>& lock) {
294 DisplayEventConsumers consumers;
295
Lloyd Pique46a46b32018-01-31 19:01:18 -0800296 while (mKeepRunning) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800297 std::optional<DisplayEventReceiver::Event> event;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700298
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800299 // Determine next event to dispatch.
300 if (!mPendingEvents.empty()) {
301 event = mPendingEvents.front();
302 mPendingEvents.pop_front();
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800303 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800304
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800305 const bool vsyncPending =
306 event && event->header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
307
308 if (mInterceptVSyncsCallback && vsyncPending) {
309 mInterceptVSyncsCallback(event->header.timestamp);
Mathias Agopianff28e202012-09-20 23:24:19 -0700310 }
311
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800312 bool vsyncRequested = false;
Mathias Agopian148994e2012-09-19 17:31:36 -0700313
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800314 // Find connections that should consume this event.
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700315 auto it = mDisplayEventConnections.begin();
316 while (it != mDisplayEventConnections.end()) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800317 if (const auto connection = it->promote()) {
318 vsyncRequested |= connection->vsyncRequest != VSyncRequest::None;
319
320 if (event && shouldConsumeEvent(*event, connection)) {
321 consumers.push_back(connection);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700322 }
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700323
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700324 ++it;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700325 } else {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700326 it = mDisplayEventConnections.erase(it);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700327 }
328 }
329
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800330 if (!consumers.empty()) {
331 dispatchEvent(*event, consumers);
332 consumers.clear();
333 }
334
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700335 // Here we figure out if we need to enable or disable vsyncs
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800336 if (vsyncPending && !vsyncRequested) {
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700337 // we received a VSYNC but we have no clients
338 // don't report it, and disable VSYNC events
339 disableVSyncLocked();
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800340 } else if (!vsyncPending && vsyncRequested) {
Andy McFadden6bf552e2012-08-30 16:34:41 -0700341 // we have at least one client, so we want vsync enabled
342 // (TODO: this function is called right after we finish
343 // notifying clients of a vsync, so this call will be made
344 // at the vsync rate, e.g. 60fps. If we can accurately
345 // track the current state we could avoid making this call
346 // so often.)
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700347 enableVSyncLocked();
348 }
349
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800350 if (event) {
351 continue;
352 }
353
354 // Wait for event or client registration/request.
355 if (vsyncRequested) {
356 // Generate a fake VSYNC after a long timeout in case the driver stalls. When the
357 // display is off, keep feeding clients at 60 Hz.
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800358 const auto timeout = mVSyncState.synthetic ? 16ms : 1000ms;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800359 if (mCondition.wait_for(lock, timeout) == std::cv_status::timeout) {
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800360 ALOGW_IF(!mVSyncState.synthetic, "Faking VSYNC due to driver stall");
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800361
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800362 mPendingEvents.push_back(makeVSync(mVSyncState.displayId,
363 systemTime(SYSTEM_TIME_MONOTONIC),
364 ++mVSyncState.count));
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700365 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800366 } else {
367 mCondition.wait(lock);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700368 }
Lloyd Pique46a46b32018-01-31 19:01:18 -0800369 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800370}
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700371
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800372bool EventThread::shouldConsumeEvent(const DisplayEventReceiver::Event& event,
373 const sp<EventThreadConnection>& connection) const {
374 switch (event.header.type) {
375 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
376 return true;
377
378 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
379 switch (connection->vsyncRequest) {
380 case VSyncRequest::None:
381 return false;
382 case VSyncRequest::Single:
383 connection->vsyncRequest = VSyncRequest::None;
384 return true;
385 case VSyncRequest::Periodic:
386 return true;
387 default:
388 return event.vsync.count % vsyncPeriod(connection->vsyncRequest) == 0;
389 }
390
391 default:
392 return false;
393 }
394}
395
396void EventThread::dispatchEvent(const DisplayEventReceiver::Event& event,
397 const DisplayEventConsumers& consumers) {
398 for (const auto& consumer : consumers) {
399 switch (consumer->postEvent(event)) {
400 case NO_ERROR:
401 break;
402
403 case -EAGAIN:
404 // TODO: Try again if pipe is full.
405 ALOGW("Failed dispatching %s for %s", toString(event).c_str(),
406 toString(*consumer).c_str());
407 break;
408
409 default:
410 // Treat EPIPE and other errors as fatal.
411 removeDisplayEventConnectionLocked(consumer);
412 }
413 }
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700414}
415
Mathias Agopian22ffb112012-04-10 21:04:02 -0700416void EventThread::enableVSyncLocked() {
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800417 if (!mVSyncState.synthetic) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700418 if (!mVsyncEnabled) {
419 mVsyncEnabled = true;
Lloyd Piquee83f9312018-02-01 12:53:17 -0800420 mVSyncSource->setCallback(this);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700421 mVSyncSource->setVSyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700422 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700423 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700424 mDebugVsyncEnabled = true;
425}
426
Mathias Agopian22ffb112012-04-10 21:04:02 -0700427void EventThread::disableVSyncLocked() {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700428 if (mVsyncEnabled) {
429 mVsyncEnabled = false;
430 mVSyncSource->setVSyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700431 mDebugVsyncEnabled = false;
432 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700433}
434
Yiwei Zhang5434a782018-12-05 18:06:32 -0800435void EventThread::dump(std::string& result) const {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800436 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800437
438 StringAppendF(&result, "%s: VSYNC %s\n", mThreadName, mDebugVsyncEnabled ? "on" : "off");
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800439 StringAppendF(&result, " VSyncState{displayId=%u, count=%u%s}\n", mVSyncState.displayId,
440 mVSyncState.count, mVSyncState.synthetic ? ", synthetic" : "");
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800441
442 StringAppendF(&result, " pending events (count=%zu):\n", mPendingEvents.size());
443 for (const auto& event : mPendingEvents) {
444 StringAppendF(&result, " %s\n", toString(event).c_str());
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700445 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800446
447 StringAppendF(&result, " connections (count=%zu):\n", mDisplayEventConnections.size());
448 for (const auto& ptr : mDisplayEventConnections) {
449 if (const auto connection = ptr.promote()) {
450 StringAppendF(&result, " %s\n", toString(*connection).c_str());
451 }
452 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800453}
454
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800455} // namespace impl
Lloyd Pique46a46b32018-01-31 19:01:18 -0800456} // namespace android