blob: 7b25adb73f4ddf70a88ae3d29dffff3aa9feb793 [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>
Lloyd Pique46a46b32018-01-31 19:01:18 -080022#include <chrono>
23#include <cstdint>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080024
Yiwei Zhang5434a782018-12-05 18:06:32 -080025#include <android-base/stringprintf.h>
26
Mathias Agopiana4cb35a2012-08-20 20:07:34 -070027#include <cutils/compiler.h>
Lloyd Pique46a46b32018-01-31 19:01:18 -080028#include <cutils/sched_policy.h>
Mathias Agopiana4cb35a2012-08-20 20:07:34 -070029
Mathias Agopiand0566bc2011-11-17 17:49:17 -080030#include <gui/DisplayEventReceiver.h>
31
32#include <utils/Errors.h>
Mathias Agopian841cde52012-03-01 15:44:37 -080033#include <utils/Trace.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080034
Mathias Agopiand0566bc2011-11-17 17:49:17 -080035#include "EventThread.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080036
Lloyd Pique46a46b32018-01-31 19:01:18 -080037using namespace std::chrono_literals;
Yiwei Zhang5434a782018-12-05 18:06:32 -080038using android::base::StringAppendF;
Lloyd Pique46a46b32018-01-31 19:01:18 -080039
Mathias Agopiand0566bc2011-11-17 17:49:17 -080040// ---------------------------------------------------------------------------
41
Lloyd Pique46a46b32018-01-31 19:01:18 -080042namespace android {
43
44// ---------------------------------------------------------------------------
45
Dominik Laskowskif654d572018-12-20 11:03:06 -080046EventThreadConnection::EventThreadConnection(EventThread* eventThread,
47 ResyncCallback resyncCallback)
48 : resyncCallback(std::move(resyncCallback)),
49 count(-1),
50 mEventThread(eventThread),
51 mChannel(gui::BitTube::DefaultSize) {}
Ana Krulec85c39af2018-12-26 17:29:57 -080052
53EventThreadConnection::~EventThreadConnection() {
54 // do nothing here -- clean-up will happen automatically
55 // when the main thread wakes up
56}
57
58void EventThreadConnection::onFirstRef() {
59 // NOTE: mEventThread doesn't hold a strong reference on us
60 mEventThread->registerDisplayEventConnection(this);
61}
62
63status_t EventThreadConnection::stealReceiveChannel(gui::BitTube* outChannel) {
64 outChannel->setReceiveFd(mChannel.moveReceiveFd());
65 return NO_ERROR;
66}
67
68status_t EventThreadConnection::setVsyncRate(uint32_t count) {
69 mEventThread->setVsyncRate(count, this);
70 return NO_ERROR;
71}
72
73void EventThreadConnection::requestNextVsync() {
Ana Krulec7d1d6832018-12-27 11:10:09 -080074 ATRACE_NAME("requestNextVsync");
75 mEventThread->requestNextVsync(this, true);
76}
77
78void EventThreadConnection::requestNextVsyncForHWC() {
79 ATRACE_NAME("requestNextVsyncForHWC");
80 mEventThread->requestNextVsync(this, false);
Ana Krulec85c39af2018-12-26 17:29:57 -080081}
82
83status_t EventThreadConnection::postEvent(const DisplayEventReceiver::Event& event) {
84 ssize_t size = DisplayEventReceiver::sendEvents(&mChannel, &event, 1);
85 return size < 0 ? status_t(size) : status_t(NO_ERROR);
86}
87
88// ---------------------------------------------------------------------------
89
Lloyd Pique0fcde1b2017-12-20 16:50:21 -080090EventThread::~EventThread() = default;
91
92namespace impl {
93
Ana Krulec98b5b242018-08-10 15:03:23 -070094EventThread::EventThread(std::unique_ptr<VSyncSource> src,
Ana Krulecfb772822018-11-30 10:44:07 +010095 const InterceptVSyncsCallback& interceptVSyncsCallback,
96 const ResetIdleTimerCallback& resetIdleTimerCallback,
97 const char* threadName)
Dominik Laskowskif654d572018-12-20 11:03:06 -080098 : EventThread(nullptr, std::move(src), interceptVSyncsCallback, threadName) {
Ana Krulecfb772822018-11-30 10:44:07 +010099 mResetIdleTimer = resetIdleTimerCallback;
100}
Ana Krulec98b5b242018-08-10 15:03:23 -0700101
Dominik Laskowskif654d572018-12-20 11:03:06 -0800102EventThread::EventThread(VSyncSource* src, InterceptVSyncsCallback interceptVSyncsCallback,
103 const char* threadName)
104 : EventThread(src, nullptr, interceptVSyncsCallback, threadName) {}
Ana Krulec98b5b242018-08-10 15:03:23 -0700105
106EventThread::EventThread(VSyncSource* src, std::unique_ptr<VSyncSource> uniqueSrc,
Ana Krulec98b5b242018-08-10 15:03:23 -0700107 InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName)
Lloyd Pique24b0a482018-03-09 18:52:26 -0800108 : mVSyncSource(src),
Ana Krulec98b5b242018-08-10 15:03:23 -0700109 mVSyncSourceUnique(std::move(uniqueSrc)),
Lloyd Pique24b0a482018-03-09 18:52:26 -0800110 mInterceptVSyncsCallback(interceptVSyncsCallback) {
Ana Krulec98b5b242018-08-10 15:03:23 -0700111 if (src == nullptr) {
112 mVSyncSource = mVSyncSourceUnique.get();
113 }
Lloyd Pique46a46b32018-01-31 19:01:18 -0800114 for (auto& event : mVSyncEvent) {
115 event.header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
116 event.header.id = 0;
117 event.header.timestamp = 0;
118 event.vsync.count = 0;
Mathias Agopianff28e202012-09-20 23:24:19 -0700119 }
Lloyd Pique46a46b32018-01-31 19:01:18 -0800120
121 mThread = std::thread(&EventThread::threadMain, this);
122
123 pthread_setname_np(mThread.native_handle(), threadName);
124
125 pid_t tid = pthread_gettid_np(mThread.native_handle());
126
127 // Use SCHED_FIFO to minimize jitter
128 constexpr int EVENT_THREAD_PRIORITY = 2;
129 struct sched_param param = {0};
130 param.sched_priority = EVENT_THREAD_PRIORITY;
131 if (pthread_setschedparam(mThread.native_handle(), SCHED_FIFO, &param) != 0) {
132 ALOGE("Couldn't set SCHED_FIFO for EventThread");
133 }
134
135 set_sched_policy(tid, SP_FOREGROUND);
136}
137
138EventThread::~EventThread() {
139 {
140 std::lock_guard<std::mutex> lock(mMutex);
141 mKeepRunning = false;
142 mCondition.notify_all();
143 }
144 mThread.join();
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700145}
146
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700147void EventThread::setPhaseOffset(nsecs_t phaseOffset) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800148 std::lock_guard<std::mutex> lock(mMutex);
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700149 mVSyncSource->setPhaseOffset(phaseOffset);
150}
151
Dominik Laskowskif654d572018-12-20 11:03:06 -0800152sp<EventThreadConnection> EventThread::createEventConnection(ResyncCallback resyncCallback) const {
153 return new EventThreadConnection(const_cast<EventThread*>(this), std::move(resyncCallback));
Mathias Agopian8aedd472012-01-24 16:39:14 -0800154}
155
Ana Krulec85c39af2018-12-26 17:29:57 -0800156status_t EventThread::registerDisplayEventConnection(const sp<EventThreadConnection>& connection) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800157 std::lock_guard<std::mutex> lock(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700158
159 // this should never happen
160 auto it = std::find(mDisplayEventConnections.cbegin(),
161 mDisplayEventConnections.cend(), connection);
162 if (it != mDisplayEventConnections.cend()) {
163 ALOGW("DisplayEventConnection %p already exists", connection.get());
164 mCondition.notify_all();
165 return ALREADY_EXISTS;
166 }
167
168 mDisplayEventConnections.push_back(connection);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800169 mCondition.notify_all();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800170 return NO_ERROR;
171}
172
Ana Krulec85c39af2018-12-26 17:29:57 -0800173void EventThread::removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection) {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700174 auto it = std::find(mDisplayEventConnections.cbegin(),
175 mDisplayEventConnections.cend(), connection);
176 if (it != mDisplayEventConnections.cend()) {
177 mDisplayEventConnections.erase(it);
178 }
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800179}
180
Ana Krulec85c39af2018-12-26 17:29:57 -0800181void EventThread::setVsyncRate(uint32_t count, const sp<EventThreadConnection>& connection) {
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800182 if (int32_t(count) >= 0) { // server must protect against bad params
Lloyd Pique46a46b32018-01-31 19:01:18 -0800183 std::lock_guard<std::mutex> lock(mMutex);
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700184 const int32_t new_count = (count == 0) ? -1 : count;
185 if (connection->count != new_count) {
186 connection->count = new_count;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800187 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800188 }
189 }
190}
191
Ana Krulec7d1d6832018-12-27 11:10:09 -0800192void EventThread::requestNextVsync(const sp<EventThreadConnection>& connection, bool reset) {
193 if (mResetIdleTimer && reset) {
194 ATRACE_NAME("resetIdleTimer");
Ana Krulecfb772822018-11-30 10:44:07 +0100195 mResetIdleTimer();
196 }
Dominik Laskowskif654d572018-12-20 11:03:06 -0800197
198 if (connection->resyncCallback) {
199 connection->resyncCallback();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800200 }
Tim Murray4a4e4a22016-04-19 16:29:23 +0000201
Ana Krulec7d1d6832018-12-27 11:10:09 -0800202 std::lock_guard<std::mutex> lock(mMutex);
203
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700204 if (connection->count < 0) {
205 connection->count = 0;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800206 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800207 }
Mathias Agopian23748662011-12-05 14:33:34 -0800208}
209
Mathias Agopian22ffb112012-04-10 21:04:02 -0700210void EventThread::onScreenReleased() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800211 std::lock_guard<std::mutex> lock(mMutex);
Mathias Agopian22ffb112012-04-10 21:04:02 -0700212 if (!mUseSoftwareVSync) {
Mathias Agopian7d886472012-06-14 23:39:35 -0700213 // disable reliance on h/w vsync
214 mUseSoftwareVSync = true;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800215 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700216 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700217}
218
219void EventThread::onScreenAcquired() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800220 std::lock_guard<std::mutex> lock(mMutex);
Mathias Agopian7d886472012-06-14 23:39:35 -0700221 if (mUseSoftwareVSync) {
222 // resume use of h/w vsync
223 mUseSoftwareVSync = false;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800224 mCondition.notify_all();
Mathias Agopian7d886472012-06-14 23:39:35 -0700225 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700226}
227
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700228void EventThread::onVSyncEvent(nsecs_t timestamp) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800229 std::lock_guard<std::mutex> lock(mMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700230 mVSyncEvent[0].header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
231 mVSyncEvent[0].header.id = 0;
232 mVSyncEvent[0].header.timestamp = timestamp;
233 mVSyncEvent[0].vsync.count++;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800234 mCondition.notify_all();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700235}
236
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700237void EventThread::onHotplugReceived(DisplayType displayType, bool connected) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800238 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700239
240 DisplayEventReceiver::Event event;
241 event.header.type = DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG;
242 event.header.id = displayType == DisplayType::Primary ? 0 : 1;
243 event.header.timestamp = systemTime();
244 event.hotplug.connected = connected;
245
Chia-I Wueac3db22018-09-14 11:28:03 -0700246 mPendingEvents.push(event);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700247 mCondition.notify_all();
Mathias Agopian148994e2012-09-19 17:31:36 -0700248}
249
Lloyd Pique46a46b32018-01-31 19:01:18 -0800250void EventThread::threadMain() NO_THREAD_SAFETY_ANALYSIS {
251 std::unique_lock<std::mutex> lock(mMutex);
252 while (mKeepRunning) {
253 DisplayEventReceiver::Event event;
Ana Krulec85c39af2018-12-26 17:29:57 -0800254 std::vector<sp<EventThreadConnection>> signalConnections;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800255 signalConnections = waitForEventLocked(&lock, &event);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700256
Lloyd Pique46a46b32018-01-31 19:01:18 -0800257 // dispatch events to listeners...
Ana Krulec85c39af2018-12-26 17:29:57 -0800258 for (const sp<EventThreadConnection>& conn : signalConnections) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800259 // now see if we still need to report this event
260 status_t err = conn->postEvent(event);
261 if (err == -EAGAIN || err == -EWOULDBLOCK) {
262 // The destination doesn't accept events anymore, it's probably
263 // full. For now, we just drop the events on the floor.
264 // FIXME: Note that some events cannot be dropped and would have
265 // to be re-sent later.
266 // Right-now we don't have the ability to do this.
267 ALOGW("EventThread: dropping event (%08x) for connection %p", event.header.type,
268 conn.get());
269 } else if (err < 0) {
270 // handle any other error on the pipe as fatal. the only
271 // reasonable thing to do is to clean-up this connection.
272 // The most common error we'll get here is -EPIPE.
Chia-I Wub02d51c2018-09-14 11:20:48 -0700273 removeDisplayEventConnectionLocked(conn);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800274 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800275 }
276 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800277}
278
Andy McFadden6bf552e2012-08-30 16:34:41 -0700279// This will return when (1) a vsync event has been received, and (2) there was
280// at least one connection interested in receiving it when we started waiting.
Ana Krulec85c39af2018-12-26 17:29:57 -0800281std::vector<sp<EventThreadConnection>> EventThread::waitForEventLocked(
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700282 std::unique_lock<std::mutex>* lock, DisplayEventReceiver::Event* outEvent) {
Ana Krulec85c39af2018-12-26 17:29:57 -0800283 std::vector<sp<EventThreadConnection>> signalConnections;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700284
Chia-I Wub02d51c2018-09-14 11:20:48 -0700285 while (signalConnections.empty() && mKeepRunning) {
Mathias Agopian148994e2012-09-19 17:31:36 -0700286 bool eventPending = false;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700287 bool waitForVSync = false;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700288
Mathias Agopianff28e202012-09-20 23:24:19 -0700289 size_t vsyncCount = 0;
290 nsecs_t timestamp = 0;
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700291 for (auto& event : mVSyncEvent) {
292 timestamp = event.header.timestamp;
Mathias Agopianff28e202012-09-20 23:24:19 -0700293 if (timestamp) {
294 // we have a vsync event to dispatch
Lloyd Pique24b0a482018-03-09 18:52:26 -0800295 if (mInterceptVSyncsCallback) {
296 mInterceptVSyncsCallback(timestamp);
Irvelab046852016-07-28 11:23:08 -0700297 }
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700298 *outEvent = event;
299 event.header.timestamp = 0;
300 vsyncCount = event.vsync.count;
Mathias Agopianff28e202012-09-20 23:24:19 -0700301 break;
302 }
303 }
304
305 if (!timestamp) {
306 // no vsync event, see if there are some other event
Chia-I Wueac3db22018-09-14 11:28:03 -0700307 eventPending = !mPendingEvents.empty();
Mathias Agopian148994e2012-09-19 17:31:36 -0700308 if (eventPending) {
309 // we have some other event to dispatch
Chia-I Wueac3db22018-09-14 11:28:03 -0700310 *outEvent = mPendingEvents.front();
311 mPendingEvents.pop();
Mathias Agopian148994e2012-09-19 17:31:36 -0700312 }
313 }
314
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700315 // find out connections waiting for events
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700316 auto it = mDisplayEventConnections.begin();
317 while (it != mDisplayEventConnections.end()) {
Ana Krulec85c39af2018-12-26 17:29:57 -0800318 sp<EventThreadConnection> connection(it->promote());
Peiyong Lin566a3b42018-01-09 18:22:43 -0800319 if (connection != nullptr) {
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700320 bool added = false;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700321 if (connection->count >= 0) {
322 // we need vsync events because at least
323 // one connection is waiting for it
324 waitForVSync = true;
325 if (timestamp) {
326 // we consume the event only if it's time
327 // (ie: we received a vsync event)
328 if (connection->count == 0) {
329 // fired this time around
330 connection->count = -1;
Chia-I Wub02d51c2018-09-14 11:20:48 -0700331 signalConnections.push_back(connection);
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700332 added = true;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700333 } else if (connection->count == 1 ||
Lloyd Pique78ce4182018-01-31 16:39:51 -0800334 (vsyncCount % connection->count) == 0) {
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700335 // continuous event, and time to report it
Chia-I Wub02d51c2018-09-14 11:20:48 -0700336 signalConnections.push_back(connection);
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700337 added = true;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700338 }
339 }
340 }
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700341
342 if (eventPending && !timestamp && !added) {
343 // we don't have a vsync event to process
344 // (timestamp==0), but we have some pending
345 // messages.
Chia-I Wub02d51c2018-09-14 11:20:48 -0700346 signalConnections.push_back(connection);
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700347 }
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700348 ++it;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700349 } else {
350 // we couldn't promote this reference, the connection has
351 // died, so clean-up!
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700352 it = mDisplayEventConnections.erase(it);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700353 }
354 }
355
356 // Here we figure out if we need to enable or disable vsyncs
357 if (timestamp && !waitForVSync) {
358 // we received a VSYNC but we have no clients
359 // don't report it, and disable VSYNC events
360 disableVSyncLocked();
361 } else if (!timestamp && waitForVSync) {
Andy McFadden6bf552e2012-08-30 16:34:41 -0700362 // we have at least one client, so we want vsync enabled
363 // (TODO: this function is called right after we finish
364 // notifying clients of a vsync, so this call will be made
365 // at the vsync rate, e.g. 60fps. If we can accurately
366 // track the current state we could avoid making this call
367 // so often.)
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700368 enableVSyncLocked();
369 }
370
Andy McFadden6bf552e2012-08-30 16:34:41 -0700371 // note: !timestamp implies signalConnections.isEmpty(), because we
372 // don't populate signalConnections if there's no vsync pending
Mathias Agopian148994e2012-09-19 17:31:36 -0700373 if (!timestamp && !eventPending) {
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700374 // wait for something to happen
Andy McFadden6bf552e2012-08-30 16:34:41 -0700375 if (waitForVSync) {
376 // This is where we spend most of our time, waiting
377 // for vsync events and new client registrations.
378 //
379 // If the screen is off, we can't use h/w vsync, so we
380 // use a 16ms timeout instead. It doesn't need to be
381 // precise, we just need to keep feeding our clients.
382 //
383 // We don't want to stall if there's a driver bug, so we
384 // use a (long) timeout when waiting for h/w vsync, and
385 // generate fake events when necessary.
386 bool softwareSync = mUseSoftwareVSync;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800387 auto timeout = softwareSync ? 16ms : 1000ms;
388 if (mCondition.wait_for(*lock, timeout) == std::cv_status::timeout) {
Andy McFadden6bf552e2012-08-30 16:34:41 -0700389 if (!softwareSync) {
390 ALOGW("Timed out waiting for hw vsync; faking it");
391 }
Mathias Agopianff28e202012-09-20 23:24:19 -0700392 // FIXME: how do we decide which display id the fake
393 // vsync came from ?
394 mVSyncEvent[0].header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700395 mVSyncEvent[0].header.id = 0;
Mathias Agopianff28e202012-09-20 23:24:19 -0700396 mVSyncEvent[0].header.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
397 mVSyncEvent[0].vsync.count++;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700398 }
399 } else {
Andy McFadden6bf552e2012-08-30 16:34:41 -0700400 // Nobody is interested in vsync, so we just want to sleep.
401 // h/w vsync should be disabled, so this will wait until we
402 // get a new connection, or an existing connection becomes
403 // interested in receiving vsync again.
Lloyd Pique46a46b32018-01-31 19:01:18 -0800404 mCondition.wait(*lock);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700405 }
406 }
Lloyd Pique46a46b32018-01-31 19:01:18 -0800407 }
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700408
409 // here we're guaranteed to have a timestamp and some connections to signal
Andy McFadden6bf552e2012-08-30 16:34:41 -0700410 // (The connections might have dropped out of mDisplayEventConnections
411 // while we were asleep, but we'll still have strong references to them.)
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700412 return signalConnections;
413}
414
Mathias Agopian22ffb112012-04-10 21:04:02 -0700415void EventThread::enableVSyncLocked() {
416 if (!mUseSoftwareVSync) {
417 // never enable h/w VSYNC when screen is off
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);
Yiwei Zhang5434a782018-12-05 18:06:32 -0800437 StringAppendF(&result, "VSYNC state: %s\n", mDebugVsyncEnabled ? "enabled" : "disabled");
438 StringAppendF(&result, " soft-vsync: %s\n", mUseSoftwareVSync ? "enabled" : "disabled");
439 StringAppendF(&result, " numListeners=%zu,\n events-delivered: %u\n",
440 mDisplayEventConnections.size(), mVSyncEvent[0].vsync.count);
Ana Krulec85c39af2018-12-26 17:29:57 -0800441 for (const wp<EventThreadConnection>& weak : mDisplayEventConnections) {
442 sp<EventThreadConnection> connection = weak.promote();
Yiwei Zhang5434a782018-12-05 18:06:32 -0800443 StringAppendF(&result, " %p: count=%d\n", connection.get(),
444 connection != nullptr ? connection->count : 0);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700445 }
Yiwei Zhang5434a782018-12-05 18:06:32 -0800446 StringAppendF(&result, " other-events-pending: %zu\n", mPendingEvents.size());
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800447}
448
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800449} // namespace impl
Lloyd Pique46a46b32018-01-31 19:01:18 -0800450} // namespace android