blob: 01aedf45d75c3e660c699d086d6febfb7ba64a98 [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
82} // namespace
Lloyd Pique46a46b32018-01-31 19:01:18 -080083
Dominik Laskowskif654d572018-12-20 11:03:06 -080084EventThreadConnection::EventThreadConnection(EventThread* eventThread,
85 ResyncCallback resyncCallback)
86 : resyncCallback(std::move(resyncCallback)),
Dominik Laskowskif654d572018-12-20 11:03:06 -080087 mEventThread(eventThread),
88 mChannel(gui::BitTube::DefaultSize) {}
Ana Krulec85c39af2018-12-26 17:29:57 -080089
90EventThreadConnection::~EventThreadConnection() {
91 // do nothing here -- clean-up will happen automatically
92 // when the main thread wakes up
93}
94
95void EventThreadConnection::onFirstRef() {
96 // NOTE: mEventThread doesn't hold a strong reference on us
97 mEventThread->registerDisplayEventConnection(this);
98}
99
100status_t EventThreadConnection::stealReceiveChannel(gui::BitTube* outChannel) {
101 outChannel->setReceiveFd(mChannel.moveReceiveFd());
102 return NO_ERROR;
103}
104
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800105status_t EventThreadConnection::setVsyncRate(uint32_t rate) {
106 mEventThread->setVsyncRate(rate, this);
Ana Krulec85c39af2018-12-26 17:29:57 -0800107 return NO_ERROR;
108}
109
110void EventThreadConnection::requestNextVsync() {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800111 ATRACE_NAME("requestNextVsync");
112 mEventThread->requestNextVsync(this, true);
113}
114
115void EventThreadConnection::requestNextVsyncForHWC() {
116 ATRACE_NAME("requestNextVsyncForHWC");
117 mEventThread->requestNextVsync(this, false);
Ana Krulec85c39af2018-12-26 17:29:57 -0800118}
119
120status_t EventThreadConnection::postEvent(const DisplayEventReceiver::Event& event) {
121 ssize_t size = DisplayEventReceiver::sendEvents(&mChannel, &event, 1);
122 return size < 0 ? status_t(size) : status_t(NO_ERROR);
123}
124
125// ---------------------------------------------------------------------------
126
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800127EventThread::~EventThread() = default;
128
129namespace impl {
130
Ana Krulec98b5b242018-08-10 15:03:23 -0700131EventThread::EventThread(std::unique_ptr<VSyncSource> src,
Ana Krulecfb772822018-11-30 10:44:07 +0100132 const InterceptVSyncsCallback& interceptVSyncsCallback,
133 const ResetIdleTimerCallback& resetIdleTimerCallback,
134 const char* threadName)
Dominik Laskowskif654d572018-12-20 11:03:06 -0800135 : EventThread(nullptr, std::move(src), interceptVSyncsCallback, threadName) {
Ana Krulecfb772822018-11-30 10:44:07 +0100136 mResetIdleTimer = resetIdleTimerCallback;
137}
Ana Krulec98b5b242018-08-10 15:03:23 -0700138
Dominik Laskowskif654d572018-12-20 11:03:06 -0800139EventThread::EventThread(VSyncSource* src, InterceptVSyncsCallback interceptVSyncsCallback,
140 const char* threadName)
141 : EventThread(src, nullptr, interceptVSyncsCallback, threadName) {}
Ana Krulec98b5b242018-08-10 15:03:23 -0700142
143EventThread::EventThread(VSyncSource* src, std::unique_ptr<VSyncSource> uniqueSrc,
Ana Krulec98b5b242018-08-10 15:03:23 -0700144 InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName)
Lloyd Pique24b0a482018-03-09 18:52:26 -0800145 : mVSyncSource(src),
Ana Krulec98b5b242018-08-10 15:03:23 -0700146 mVSyncSourceUnique(std::move(uniqueSrc)),
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800147 mInterceptVSyncsCallback(interceptVSyncsCallback),
148 mThreadName(threadName) {
Ana Krulec98b5b242018-08-10 15:03:23 -0700149 if (src == nullptr) {
150 mVSyncSource = mVSyncSourceUnique.get();
151 }
Lloyd Pique46a46b32018-01-31 19:01:18 -0800152 for (auto& event : mVSyncEvent) {
153 event.header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
154 event.header.id = 0;
155 event.header.timestamp = 0;
156 event.vsync.count = 0;
Mathias Agopianff28e202012-09-20 23:24:19 -0700157 }
Lloyd Pique46a46b32018-01-31 19:01:18 -0800158
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800159 mThread = std::thread([this]() NO_THREAD_SAFETY_ANALYSIS {
160 std::unique_lock<std::mutex> lock(mMutex);
161 threadMain(lock);
162 });
Lloyd Pique46a46b32018-01-31 19:01:18 -0800163
164 pthread_setname_np(mThread.native_handle(), threadName);
165
166 pid_t tid = pthread_gettid_np(mThread.native_handle());
167
168 // Use SCHED_FIFO to minimize jitter
169 constexpr int EVENT_THREAD_PRIORITY = 2;
170 struct sched_param param = {0};
171 param.sched_priority = EVENT_THREAD_PRIORITY;
172 if (pthread_setschedparam(mThread.native_handle(), SCHED_FIFO, &param) != 0) {
173 ALOGE("Couldn't set SCHED_FIFO for EventThread");
174 }
175
176 set_sched_policy(tid, SP_FOREGROUND);
177}
178
179EventThread::~EventThread() {
180 {
181 std::lock_guard<std::mutex> lock(mMutex);
182 mKeepRunning = false;
183 mCondition.notify_all();
184 }
185 mThread.join();
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700186}
187
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700188void EventThread::setPhaseOffset(nsecs_t phaseOffset) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800189 std::lock_guard<std::mutex> lock(mMutex);
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700190 mVSyncSource->setPhaseOffset(phaseOffset);
191}
192
Dominik Laskowskif654d572018-12-20 11:03:06 -0800193sp<EventThreadConnection> EventThread::createEventConnection(ResyncCallback resyncCallback) const {
194 return new EventThreadConnection(const_cast<EventThread*>(this), std::move(resyncCallback));
Mathias Agopian8aedd472012-01-24 16:39:14 -0800195}
196
Ana Krulec85c39af2018-12-26 17:29:57 -0800197status_t EventThread::registerDisplayEventConnection(const sp<EventThreadConnection>& connection) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800198 std::lock_guard<std::mutex> lock(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700199
200 // this should never happen
201 auto it = std::find(mDisplayEventConnections.cbegin(),
202 mDisplayEventConnections.cend(), connection);
203 if (it != mDisplayEventConnections.cend()) {
204 ALOGW("DisplayEventConnection %p already exists", connection.get());
205 mCondition.notify_all();
206 return ALREADY_EXISTS;
207 }
208
209 mDisplayEventConnections.push_back(connection);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800210 mCondition.notify_all();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800211 return NO_ERROR;
212}
213
Ana Krulec85c39af2018-12-26 17:29:57 -0800214void EventThread::removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection) {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700215 auto it = std::find(mDisplayEventConnections.cbegin(),
216 mDisplayEventConnections.cend(), connection);
217 if (it != mDisplayEventConnections.cend()) {
218 mDisplayEventConnections.erase(it);
219 }
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800220}
221
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800222void EventThread::setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) {
223 if (static_cast<std::underlying_type_t<VSyncRequest>>(rate) < 0) {
224 return;
225 }
226
227 std::lock_guard<std::mutex> lock(mMutex);
228
229 const auto request = rate == 0 ? VSyncRequest::None : static_cast<VSyncRequest>(rate);
230 if (connection->vsyncRequest != request) {
231 connection->vsyncRequest = request;
232 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800233 }
234}
235
Ana Krulec7d1d6832018-12-27 11:10:09 -0800236void EventThread::requestNextVsync(const sp<EventThreadConnection>& connection, bool reset) {
237 if (mResetIdleTimer && reset) {
238 ATRACE_NAME("resetIdleTimer");
Ana Krulecfb772822018-11-30 10:44:07 +0100239 mResetIdleTimer();
240 }
Dominik Laskowskif654d572018-12-20 11:03:06 -0800241
242 if (connection->resyncCallback) {
243 connection->resyncCallback();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800244 }
Tim Murray4a4e4a22016-04-19 16:29:23 +0000245
Ana Krulec7d1d6832018-12-27 11:10:09 -0800246 std::lock_guard<std::mutex> lock(mMutex);
247
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800248 if (connection->vsyncRequest == VSyncRequest::None) {
249 connection->vsyncRequest = VSyncRequest::Single;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800250 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800251 }
Mathias Agopian23748662011-12-05 14:33:34 -0800252}
253
Mathias Agopian22ffb112012-04-10 21:04:02 -0700254void EventThread::onScreenReleased() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800255 std::lock_guard<std::mutex> lock(mMutex);
Mathias Agopian22ffb112012-04-10 21:04:02 -0700256 if (!mUseSoftwareVSync) {
Mathias Agopian7d886472012-06-14 23:39:35 -0700257 // disable reliance on h/w vsync
258 mUseSoftwareVSync = true;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800259 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700260 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700261}
262
263void EventThread::onScreenAcquired() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800264 std::lock_guard<std::mutex> lock(mMutex);
Mathias Agopian7d886472012-06-14 23:39:35 -0700265 if (mUseSoftwareVSync) {
266 // resume use of h/w vsync
267 mUseSoftwareVSync = false;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800268 mCondition.notify_all();
Mathias Agopian7d886472012-06-14 23:39:35 -0700269 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700270}
271
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700272void EventThread::onVSyncEvent(nsecs_t timestamp) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800273 std::lock_guard<std::mutex> lock(mMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700274 mVSyncEvent[0].header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
275 mVSyncEvent[0].header.id = 0;
276 mVSyncEvent[0].header.timestamp = timestamp;
277 mVSyncEvent[0].vsync.count++;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800278 mCondition.notify_all();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700279}
280
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700281void EventThread::onHotplugReceived(DisplayType displayType, bool connected) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800282 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700283
284 DisplayEventReceiver::Event event;
285 event.header.type = DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG;
286 event.header.id = displayType == DisplayType::Primary ? 0 : 1;
287 event.header.timestamp = systemTime();
288 event.hotplug.connected = connected;
289
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800290 mPendingEvents.push_back(event);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700291 mCondition.notify_all();
Mathias Agopian148994e2012-09-19 17:31:36 -0700292}
293
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800294void EventThread::threadMain(std::unique_lock<std::mutex>& lock) {
295 DisplayEventConsumers consumers;
296
Lloyd Pique46a46b32018-01-31 19:01:18 -0800297 while (mKeepRunning) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800298 std::optional<DisplayEventReceiver::Event> event;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700299
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800300 // Determine next event to dispatch.
301 if (!mPendingEvents.empty()) {
302 event = mPendingEvents.front();
303 mPendingEvents.pop_front();
304 } else {
305 for (auto& vsyncEvent : mVSyncEvent) {
306 if (vsyncEvent.header.timestamp) {
307 event = vsyncEvent;
308 vsyncEvent.header.timestamp = 0;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800309
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800310 if (mInterceptVSyncsCallback) {
311 mInterceptVSyncsCallback(event->header.timestamp);
312 }
313 break;
Irvelab046852016-07-28 11:23:08 -0700314 }
Mathias Agopianff28e202012-09-20 23:24:19 -0700315 }
316 }
317
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800318 bool vsyncRequested = false;
Mathias Agopian148994e2012-09-19 17:31:36 -0700319
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800320 // Find connections that should consume this event.
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700321 auto it = mDisplayEventConnections.begin();
322 while (it != mDisplayEventConnections.end()) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800323 if (const auto connection = it->promote()) {
324 vsyncRequested |= connection->vsyncRequest != VSyncRequest::None;
325
326 if (event && shouldConsumeEvent(*event, connection)) {
327 consumers.push_back(connection);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700328 }
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700329
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700330 ++it;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700331 } else {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700332 it = mDisplayEventConnections.erase(it);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700333 }
334 }
335
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800336 if (!consumers.empty()) {
337 dispatchEvent(*event, consumers);
338 consumers.clear();
339 }
340
341 const bool vsyncPending =
342 event && event->header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
343
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700344 // Here we figure out if we need to enable or disable vsyncs
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800345 if (vsyncPending && !vsyncRequested) {
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700346 // we received a VSYNC but we have no clients
347 // don't report it, and disable VSYNC events
348 disableVSyncLocked();
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800349 } else if (!vsyncPending && vsyncRequested) {
Andy McFadden6bf552e2012-08-30 16:34:41 -0700350 // we have at least one client, so we want vsync enabled
351 // (TODO: this function is called right after we finish
352 // notifying clients of a vsync, so this call will be made
353 // at the vsync rate, e.g. 60fps. If we can accurately
354 // track the current state we could avoid making this call
355 // so often.)
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700356 enableVSyncLocked();
357 }
358
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800359 if (event) {
360 continue;
361 }
362
363 // Wait for event or client registration/request.
364 if (vsyncRequested) {
365 // Generate a fake VSYNC after a long timeout in case the driver stalls. When the
366 // display is off, keep feeding clients at 60 Hz.
367 const bool softwareSync = mUseSoftwareVSync;
368 const auto timeout = softwareSync ? 16ms : 1000ms;
369 if (mCondition.wait_for(lock, timeout) == std::cv_status::timeout) {
370 ALOGW_IF(!softwareSync, "Faking VSYNC due to driver stall");
371
372 mVSyncEvent[0].header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
373 mVSyncEvent[0].header.id = 0;
374 mVSyncEvent[0].header.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
375 mVSyncEvent[0].vsync.count++;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700376 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800377 } else {
378 mCondition.wait(lock);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700379 }
Lloyd Pique46a46b32018-01-31 19:01:18 -0800380 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800381}
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700382
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800383bool EventThread::shouldConsumeEvent(const DisplayEventReceiver::Event& event,
384 const sp<EventThreadConnection>& connection) const {
385 switch (event.header.type) {
386 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
387 return true;
388
389 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
390 switch (connection->vsyncRequest) {
391 case VSyncRequest::None:
392 return false;
393 case VSyncRequest::Single:
394 connection->vsyncRequest = VSyncRequest::None;
395 return true;
396 case VSyncRequest::Periodic:
397 return true;
398 default:
399 return event.vsync.count % vsyncPeriod(connection->vsyncRequest) == 0;
400 }
401
402 default:
403 return false;
404 }
405}
406
407void EventThread::dispatchEvent(const DisplayEventReceiver::Event& event,
408 const DisplayEventConsumers& consumers) {
409 for (const auto& consumer : consumers) {
410 switch (consumer->postEvent(event)) {
411 case NO_ERROR:
412 break;
413
414 case -EAGAIN:
415 // TODO: Try again if pipe is full.
416 ALOGW("Failed dispatching %s for %s", toString(event).c_str(),
417 toString(*consumer).c_str());
418 break;
419
420 default:
421 // Treat EPIPE and other errors as fatal.
422 removeDisplayEventConnectionLocked(consumer);
423 }
424 }
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700425}
426
Mathias Agopian22ffb112012-04-10 21:04:02 -0700427void EventThread::enableVSyncLocked() {
428 if (!mUseSoftwareVSync) {
429 // never enable h/w VSYNC when screen is off
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700430 if (!mVsyncEnabled) {
431 mVsyncEnabled = true;
Lloyd Piquee83f9312018-02-01 12:53:17 -0800432 mVSyncSource->setCallback(this);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700433 mVSyncSource->setVSyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700434 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700435 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700436 mDebugVsyncEnabled = true;
437}
438
Mathias Agopian22ffb112012-04-10 21:04:02 -0700439void EventThread::disableVSyncLocked() {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700440 if (mVsyncEnabled) {
441 mVsyncEnabled = false;
442 mVSyncSource->setVSyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700443 mDebugVsyncEnabled = false;
444 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700445}
446
Yiwei Zhang5434a782018-12-05 18:06:32 -0800447void EventThread::dump(std::string& result) const {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800448 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800449
450 StringAppendF(&result, "%s: VSYNC %s\n", mThreadName, mDebugVsyncEnabled ? "on" : "off");
451 StringAppendF(&result, " software VSYNC: %s\n", mUseSoftwareVSync ? "on" : "off");
452 StringAppendF(&result, " VSYNC count: %u\n", mVSyncEvent[0].vsync.count);
453
454 StringAppendF(&result, " pending events (count=%zu):\n", mPendingEvents.size());
455 for (const auto& event : mPendingEvents) {
456 StringAppendF(&result, " %s\n", toString(event).c_str());
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700457 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800458
459 StringAppendF(&result, " connections (count=%zu):\n", mDisplayEventConnections.size());
460 for (const auto& ptr : mDisplayEventConnections) {
461 if (const auto connection = ptr.promote()) {
462 StringAppendF(&result, " %s\n", toString(*connection).c_str());
463 }
464 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800465}
466
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800467} // namespace impl
Lloyd Pique46a46b32018-01-31 19:01:18 -0800468} // namespace android