blob: 343162cda88d15ee5755d58fa3b5553985382512 [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
Mathias Agopiana4cb35a2012-08-20 20:07:34 -070025#include <cutils/compiler.h>
Lloyd Pique46a46b32018-01-31 19:01:18 -080026#include <cutils/sched_policy.h>
Mathias Agopiana4cb35a2012-08-20 20:07:34 -070027
Mathias Agopiand0566bc2011-11-17 17:49:17 -080028#include <gui/DisplayEventReceiver.h>
29
30#include <utils/Errors.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070031#include <utils/String8.h>
Mathias Agopian841cde52012-03-01 15:44:37 -080032#include <utils/Trace.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080033
Mathias Agopiand0566bc2011-11-17 17:49:17 -080034#include "EventThread.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080035
Lloyd Pique46a46b32018-01-31 19:01:18 -080036using namespace std::chrono_literals;
37
Mathias Agopiand0566bc2011-11-17 17:49:17 -080038// ---------------------------------------------------------------------------
39
Lloyd Pique46a46b32018-01-31 19:01:18 -080040namespace android {
41
42// ---------------------------------------------------------------------------
43
Lloyd Pique0fcde1b2017-12-20 16:50:21 -080044EventThread::~EventThread() = default;
45
46namespace impl {
47
Ana Krulec98b5b242018-08-10 15:03:23 -070048EventThread::EventThread(std::unique_ptr<VSyncSource> src,
49 ResyncWithRateLimitCallback resyncWithRateLimitCallback,
50 InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName)
51 : EventThread(nullptr, std::move(src), resyncWithRateLimitCallback, interceptVSyncsCallback,
52 threadName) {}
53
Lloyd Pique24b0a482018-03-09 18:52:26 -080054EventThread::EventThread(VSyncSource* src, ResyncWithRateLimitCallback resyncWithRateLimitCallback,
55 InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName)
Ana Krulec98b5b242018-08-10 15:03:23 -070056 : EventThread(src, nullptr, resyncWithRateLimitCallback, interceptVSyncsCallback,
57 threadName) {}
58
59EventThread::EventThread(VSyncSource* src, std::unique_ptr<VSyncSource> uniqueSrc,
60 ResyncWithRateLimitCallback resyncWithRateLimitCallback,
61 InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName)
Lloyd Pique24b0a482018-03-09 18:52:26 -080062 : mVSyncSource(src),
Ana Krulec98b5b242018-08-10 15:03:23 -070063 mVSyncSourceUnique(std::move(uniqueSrc)),
Lloyd Pique24b0a482018-03-09 18:52:26 -080064 mResyncWithRateLimitCallback(resyncWithRateLimitCallback),
65 mInterceptVSyncsCallback(interceptVSyncsCallback) {
Ana Krulec98b5b242018-08-10 15:03:23 -070066 if (src == nullptr) {
67 mVSyncSource = mVSyncSourceUnique.get();
68 }
Lloyd Pique46a46b32018-01-31 19:01:18 -080069 for (auto& event : mVSyncEvent) {
70 event.header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
71 event.header.id = 0;
72 event.header.timestamp = 0;
73 event.vsync.count = 0;
Mathias Agopianff28e202012-09-20 23:24:19 -070074 }
Lloyd Pique46a46b32018-01-31 19:01:18 -080075
76 mThread = std::thread(&EventThread::threadMain, this);
77
78 pthread_setname_np(mThread.native_handle(), threadName);
79
80 pid_t tid = pthread_gettid_np(mThread.native_handle());
81
82 // Use SCHED_FIFO to minimize jitter
83 constexpr int EVENT_THREAD_PRIORITY = 2;
84 struct sched_param param = {0};
85 param.sched_priority = EVENT_THREAD_PRIORITY;
86 if (pthread_setschedparam(mThread.native_handle(), SCHED_FIFO, &param) != 0) {
87 ALOGE("Couldn't set SCHED_FIFO for EventThread");
88 }
89
90 set_sched_policy(tid, SP_FOREGROUND);
91}
92
93EventThread::~EventThread() {
94 {
95 std::lock_guard<std::mutex> lock(mMutex);
96 mKeepRunning = false;
97 mCondition.notify_all();
98 }
99 mThread.join();
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700100}
101
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700102void EventThread::setPhaseOffset(nsecs_t phaseOffset) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800103 std::lock_guard<std::mutex> lock(mMutex);
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700104 mVSyncSource->setPhaseOffset(phaseOffset);
105}
106
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800107sp<BnDisplayEventConnection> EventThread::createEventConnection() const {
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700108 return new Connection(const_cast<EventThread*>(this));
Mathias Agopian8aedd472012-01-24 16:39:14 -0800109}
110
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800111status_t EventThread::registerDisplayEventConnection(
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700112 const sp<EventThread::Connection>& connection) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800113 std::lock_guard<std::mutex> lock(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700114
115 // this should never happen
116 auto it = std::find(mDisplayEventConnections.cbegin(),
117 mDisplayEventConnections.cend(), connection);
118 if (it != mDisplayEventConnections.cend()) {
119 ALOGW("DisplayEventConnection %p already exists", connection.get());
120 mCondition.notify_all();
121 return ALREADY_EXISTS;
122 }
123
124 mDisplayEventConnections.push_back(connection);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800125 mCondition.notify_all();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800126 return NO_ERROR;
127}
128
Ana Krulecfefcb582018-08-07 14:22:37 -0700129void EventThread::removeDisplayEventConnectionLocked(
130 const wp<EventThread::Connection>& connection) {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700131 auto it = std::find(mDisplayEventConnections.cbegin(),
132 mDisplayEventConnections.cend(), connection);
133 if (it != mDisplayEventConnections.cend()) {
134 mDisplayEventConnections.erase(it);
135 }
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800136}
137
Lloyd Pique78ce4182018-01-31 16:39:51 -0800138void EventThread::setVsyncRate(uint32_t count, const sp<EventThread::Connection>& connection) {
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800139 if (int32_t(count) >= 0) { // server must protect against bad params
Lloyd Pique46a46b32018-01-31 19:01:18 -0800140 std::lock_guard<std::mutex> lock(mMutex);
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700141 const int32_t new_count = (count == 0) ? -1 : count;
142 if (connection->count != new_count) {
143 connection->count = new_count;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800144 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800145 }
146 }
147}
148
Lloyd Pique78ce4182018-01-31 16:39:51 -0800149void EventThread::requestNextVsync(const sp<EventThread::Connection>& connection) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800150 std::lock_guard<std::mutex> lock(mMutex);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000151
Lloyd Pique24b0a482018-03-09 18:52:26 -0800152 if (mResyncWithRateLimitCallback) {
153 mResyncWithRateLimitCallback();
154 }
Tim Murray4a4e4a22016-04-19 16:29:23 +0000155
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700156 if (connection->count < 0) {
157 connection->count = 0;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800158 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800159 }
Mathias Agopian23748662011-12-05 14:33:34 -0800160}
161
Mathias Agopian22ffb112012-04-10 21:04:02 -0700162void EventThread::onScreenReleased() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800163 std::lock_guard<std::mutex> lock(mMutex);
Mathias Agopian22ffb112012-04-10 21:04:02 -0700164 if (!mUseSoftwareVSync) {
Mathias Agopian7d886472012-06-14 23:39:35 -0700165 // disable reliance on h/w vsync
166 mUseSoftwareVSync = true;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800167 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700168 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700169}
170
171void EventThread::onScreenAcquired() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800172 std::lock_guard<std::mutex> lock(mMutex);
Mathias Agopian7d886472012-06-14 23:39:35 -0700173 if (mUseSoftwareVSync) {
174 // resume use of h/w vsync
175 mUseSoftwareVSync = false;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800176 mCondition.notify_all();
Mathias Agopian7d886472012-06-14 23:39:35 -0700177 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700178}
179
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700180void EventThread::onVSyncEvent(nsecs_t timestamp) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800181 std::lock_guard<std::mutex> lock(mMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700182 mVSyncEvent[0].header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
183 mVSyncEvent[0].header.id = 0;
184 mVSyncEvent[0].header.timestamp = timestamp;
185 mVSyncEvent[0].vsync.count++;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800186 mCondition.notify_all();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700187}
188
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700189void EventThread::onHotplugReceived(DisplayType displayType, bool connected) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800190 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700191
192 DisplayEventReceiver::Event event;
193 event.header.type = DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG;
194 event.header.id = displayType == DisplayType::Primary ? 0 : 1;
195 event.header.timestamp = systemTime();
196 event.hotplug.connected = connected;
197
Chia-I Wueac3db22018-09-14 11:28:03 -0700198 mPendingEvents.push(event);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700199 mCondition.notify_all();
Mathias Agopian148994e2012-09-19 17:31:36 -0700200}
201
Lloyd Pique46a46b32018-01-31 19:01:18 -0800202void EventThread::threadMain() NO_THREAD_SAFETY_ANALYSIS {
203 std::unique_lock<std::mutex> lock(mMutex);
204 while (mKeepRunning) {
205 DisplayEventReceiver::Event event;
206 Vector<sp<EventThread::Connection> > signalConnections;
207 signalConnections = waitForEventLocked(&lock, &event);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700208
Lloyd Pique46a46b32018-01-31 19:01:18 -0800209 // dispatch events to listeners...
210 const size_t count = signalConnections.size();
211 for (size_t i = 0; i < count; i++) {
212 const sp<Connection>& conn(signalConnections[i]);
213 // now see if we still need to report this event
214 status_t err = conn->postEvent(event);
215 if (err == -EAGAIN || err == -EWOULDBLOCK) {
216 // The destination doesn't accept events anymore, it's probably
217 // full. For now, we just drop the events on the floor.
218 // FIXME: Note that some events cannot be dropped and would have
219 // to be re-sent later.
220 // Right-now we don't have the ability to do this.
221 ALOGW("EventThread: dropping event (%08x) for connection %p", event.header.type,
222 conn.get());
223 } else if (err < 0) {
224 // handle any other error on the pipe as fatal. the only
225 // reasonable thing to do is to clean-up this connection.
226 // The most common error we'll get here is -EPIPE.
Lloyd Pique9cbe4da2018-02-13 18:51:55 -0800227 removeDisplayEventConnectionLocked(signalConnections[i]);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800228 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800229 }
230 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800231}
232
Andy McFadden6bf552e2012-08-30 16:34:41 -0700233// This will return when (1) a vsync event has been received, and (2) there was
234// at least one connection interested in receiving it when we started waiting.
Lloyd Pique46a46b32018-01-31 19:01:18 -0800235Vector<sp<EventThread::Connection> > EventThread::waitForEventLocked(
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700236 std::unique_lock<std::mutex>* lock, DisplayEventReceiver::Event* outEvent) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800237 Vector<sp<EventThread::Connection> > signalConnections;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700238
Lloyd Pique46a46b32018-01-31 19:01:18 -0800239 while (signalConnections.isEmpty() && mKeepRunning) {
Mathias Agopian148994e2012-09-19 17:31:36 -0700240 bool eventPending = false;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700241 bool waitForVSync = false;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700242
Mathias Agopianff28e202012-09-20 23:24:19 -0700243 size_t vsyncCount = 0;
244 nsecs_t timestamp = 0;
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700245 for (auto& event : mVSyncEvent) {
246 timestamp = event.header.timestamp;
Mathias Agopianff28e202012-09-20 23:24:19 -0700247 if (timestamp) {
248 // we have a vsync event to dispatch
Lloyd Pique24b0a482018-03-09 18:52:26 -0800249 if (mInterceptVSyncsCallback) {
250 mInterceptVSyncsCallback(timestamp);
Irvelab046852016-07-28 11:23:08 -0700251 }
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700252 *outEvent = event;
253 event.header.timestamp = 0;
254 vsyncCount = event.vsync.count;
Mathias Agopianff28e202012-09-20 23:24:19 -0700255 break;
256 }
257 }
258
259 if (!timestamp) {
260 // no vsync event, see if there are some other event
Chia-I Wueac3db22018-09-14 11:28:03 -0700261 eventPending = !mPendingEvents.empty();
Mathias Agopian148994e2012-09-19 17:31:36 -0700262 if (eventPending) {
263 // we have some other event to dispatch
Chia-I Wueac3db22018-09-14 11:28:03 -0700264 *outEvent = mPendingEvents.front();
265 mPendingEvents.pop();
Mathias Agopian148994e2012-09-19 17:31:36 -0700266 }
267 }
268
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700269 // find out connections waiting for events
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700270 auto it = mDisplayEventConnections.begin();
271 while (it != mDisplayEventConnections.end()) {
272 sp<Connection> connection(it->promote());
Peiyong Lin566a3b42018-01-09 18:22:43 -0800273 if (connection != nullptr) {
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700274 bool added = false;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700275 if (connection->count >= 0) {
276 // we need vsync events because at least
277 // one connection is waiting for it
278 waitForVSync = true;
279 if (timestamp) {
280 // we consume the event only if it's time
281 // (ie: we received a vsync event)
282 if (connection->count == 0) {
283 // fired this time around
284 connection->count = -1;
285 signalConnections.add(connection);
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700286 added = true;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700287 } else if (connection->count == 1 ||
Lloyd Pique78ce4182018-01-31 16:39:51 -0800288 (vsyncCount % connection->count) == 0) {
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700289 // continuous event, and time to report it
290 signalConnections.add(connection);
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700291 added = true;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700292 }
293 }
294 }
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700295
296 if (eventPending && !timestamp && !added) {
297 // we don't have a vsync event to process
298 // (timestamp==0), but we have some pending
299 // messages.
300 signalConnections.add(connection);
301 }
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700302 ++it;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700303 } else {
304 // we couldn't promote this reference, the connection has
305 // died, so clean-up!
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700306 it = mDisplayEventConnections.erase(it);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700307 }
308 }
309
310 // Here we figure out if we need to enable or disable vsyncs
311 if (timestamp && !waitForVSync) {
312 // we received a VSYNC but we have no clients
313 // don't report it, and disable VSYNC events
314 disableVSyncLocked();
315 } else if (!timestamp && waitForVSync) {
Andy McFadden6bf552e2012-08-30 16:34:41 -0700316 // we have at least one client, so we want vsync enabled
317 // (TODO: this function is called right after we finish
318 // notifying clients of a vsync, so this call will be made
319 // at the vsync rate, e.g. 60fps. If we can accurately
320 // track the current state we could avoid making this call
321 // so often.)
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700322 enableVSyncLocked();
323 }
324
Andy McFadden6bf552e2012-08-30 16:34:41 -0700325 // note: !timestamp implies signalConnections.isEmpty(), because we
326 // don't populate signalConnections if there's no vsync pending
Mathias Agopian148994e2012-09-19 17:31:36 -0700327 if (!timestamp && !eventPending) {
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700328 // wait for something to happen
Andy McFadden6bf552e2012-08-30 16:34:41 -0700329 if (waitForVSync) {
330 // This is where we spend most of our time, waiting
331 // for vsync events and new client registrations.
332 //
333 // If the screen is off, we can't use h/w vsync, so we
334 // use a 16ms timeout instead. It doesn't need to be
335 // precise, we just need to keep feeding our clients.
336 //
337 // We don't want to stall if there's a driver bug, so we
338 // use a (long) timeout when waiting for h/w vsync, and
339 // generate fake events when necessary.
340 bool softwareSync = mUseSoftwareVSync;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800341 auto timeout = softwareSync ? 16ms : 1000ms;
342 if (mCondition.wait_for(*lock, timeout) == std::cv_status::timeout) {
Andy McFadden6bf552e2012-08-30 16:34:41 -0700343 if (!softwareSync) {
344 ALOGW("Timed out waiting for hw vsync; faking it");
345 }
Mathias Agopianff28e202012-09-20 23:24:19 -0700346 // FIXME: how do we decide which display id the fake
347 // vsync came from ?
348 mVSyncEvent[0].header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700349 mVSyncEvent[0].header.id = 0;
Mathias Agopianff28e202012-09-20 23:24:19 -0700350 mVSyncEvent[0].header.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
351 mVSyncEvent[0].vsync.count++;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700352 }
353 } else {
Andy McFadden6bf552e2012-08-30 16:34:41 -0700354 // Nobody is interested in vsync, so we just want to sleep.
355 // h/w vsync should be disabled, so this will wait until we
356 // get a new connection, or an existing connection becomes
357 // interested in receiving vsync again.
Lloyd Pique46a46b32018-01-31 19:01:18 -0800358 mCondition.wait(*lock);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700359 }
360 }
Lloyd Pique46a46b32018-01-31 19:01:18 -0800361 }
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700362
363 // here we're guaranteed to have a timestamp and some connections to signal
Andy McFadden6bf552e2012-08-30 16:34:41 -0700364 // (The connections might have dropped out of mDisplayEventConnections
365 // while we were asleep, but we'll still have strong references to them.)
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700366 return signalConnections;
367}
368
Mathias Agopian22ffb112012-04-10 21:04:02 -0700369void EventThread::enableVSyncLocked() {
370 if (!mUseSoftwareVSync) {
371 // never enable h/w VSYNC when screen is off
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700372 if (!mVsyncEnabled) {
373 mVsyncEnabled = true;
Lloyd Piquee83f9312018-02-01 12:53:17 -0800374 mVSyncSource->setCallback(this);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700375 mVSyncSource->setVSyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700376 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700377 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700378 mDebugVsyncEnabled = true;
379}
380
Mathias Agopian22ffb112012-04-10 21:04:02 -0700381void EventThread::disableVSyncLocked() {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700382 if (mVsyncEnabled) {
383 mVsyncEnabled = false;
384 mVSyncSource->setVSyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700385 mDebugVsyncEnabled = false;
386 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700387}
388
Mathias Agopian74d211a2013-04-22 16:55:35 +0200389void EventThread::dump(String8& result) const {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800390 std::lock_guard<std::mutex> lock(mMutex);
Lloyd Pique78ce4182018-01-31 16:39:51 -0800391 result.appendFormat("VSYNC state: %s\n", mDebugVsyncEnabled ? "enabled" : "disabled");
392 result.appendFormat(" soft-vsync: %s\n", mUseSoftwareVSync ? "enabled" : "disabled");
Greg Hackmann86efcc02014-03-07 12:44:02 -0800393 result.appendFormat(" numListeners=%zu,\n events-delivered: %u\n",
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700394 mDisplayEventConnections.size(), mVSyncEvent[0].vsync.count);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700395 for (const wp<Connection>& weak : mDisplayEventConnections) {
396 sp<Connection> connection = weak.promote();
Lloyd Pique78ce4182018-01-31 16:39:51 -0800397 result.appendFormat(" %p: count=%d\n", connection.get(),
398 connection != nullptr ? connection->count : 0);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700399 }
Chia-I Wueac3db22018-09-14 11:28:03 -0700400 result.appendFormat(" other-events-pending: %zu\n", mPendingEvents.size());
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800401}
402
403// ---------------------------------------------------------------------------
404
Lloyd Piquee83f9312018-02-01 12:53:17 -0800405EventThread::Connection::Connection(EventThread* eventThread)
Lloyd Pique78ce4182018-01-31 16:39:51 -0800406 : count(-1), mEventThread(eventThread), mChannel(gui::BitTube::DefaultSize) {}
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700407
408EventThread::Connection::~Connection() {
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700409 // do nothing here -- clean-up will happen automatically
410 // when the main thread wakes up
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700411}
412
413void EventThread::Connection::onFirstRef() {
414 // NOTE: mEventThread doesn't hold a strong reference on us
415 mEventThread->registerDisplayEventConnection(this);
416}
417
Dan Stoza6b698e42017-04-03 13:09:08 -0700418status_t EventThread::Connection::stealReceiveChannel(gui::BitTube* outChannel) {
419 outChannel->setReceiveFd(mChannel.moveReceiveFd());
Dan Stozae1c599b2017-03-30 16:37:19 -0700420 return NO_ERROR;
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700421}
422
Dan Stozae1c599b2017-03-30 16:37:19 -0700423status_t EventThread::Connection::setVsyncRate(uint32_t count) {
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700424 mEventThread->setVsyncRate(count, this);
Dan Stozae1c599b2017-03-30 16:37:19 -0700425 return NO_ERROR;
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700426}
427
428void EventThread::Connection::requestNextVsync() {
429 mEventThread->requestNextVsync(this);
430}
431
Lloyd Pique78ce4182018-01-31 16:39:51 -0800432status_t EventThread::Connection::postEvent(const DisplayEventReceiver::Event& event) {
Dan Stoza6b698e42017-04-03 13:09:08 -0700433 ssize_t size = DisplayEventReceiver::sendEvents(&mChannel, &event, 1);
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700434 return size < 0 ? status_t(size) : status_t(NO_ERROR);
435}
436
437// ---------------------------------------------------------------------------
438
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800439} // namespace impl
Lloyd Pique46a46b32018-01-31 19:01:18 -0800440} // namespace android