blob: f647742e2c1d66e9e7a0c78109244ad47c7b94b8 [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
Mathias Agopiand0566bc2011-11-17 17:49:17 -080019#include <stdint.h>
20#include <sys/types.h>
21
Mathias Agopiana4cb35a2012-08-20 20:07:34 -070022#include <cutils/compiler.h>
23
Mathias Agopiand0566bc2011-11-17 17:49:17 -080024#include <gui/IDisplayEventConnection.h>
25#include <gui/DisplayEventReceiver.h>
26
27#include <utils/Errors.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070028#include <utils/String8.h>
Mathias Agopian841cde52012-03-01 15:44:37 -080029#include <utils/Trace.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080030
Mathias Agopiand0566bc2011-11-17 17:49:17 -080031#include "EventThread.h"
32#include "SurfaceFlinger.h"
33
34// ---------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -080035namespace android {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080036// ---------------------------------------------------------------------------
Ruchi Kandoief472ec2014-04-02 12:50:06 -070037// time to wait between VSYNC requests before sending a VSYNC OFF power hint: 40msec.
38const long vsyncHintOffDelay = 40000000;
39
40static void vsyncOffCallback(union sigval val) {
41 EventThread *ev = (EventThread *)val.sival_ptr;
42 ev->sendVsyncHintOff();
43 return;
44}
Mathias Agopiand0566bc2011-11-17 17:49:17 -080045
Irvelab046852016-07-28 11:23:08 -070046EventThread::EventThread(const sp<VSyncSource>& src, SurfaceFlinger& flinger, bool interceptVSyncs)
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070047 : mVSyncSource(src),
Tim Murray4a4e4a22016-04-19 16:29:23 +000048 mFlinger(flinger),
Mathias Agopian22ffb112012-04-10 21:04:02 -070049 mUseSoftwareVSync(false),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070050 mVsyncEnabled(false),
Ruchi Kandoief472ec2014-04-02 12:50:06 -070051 mDebugVsyncEnabled(false),
Irvelab046852016-07-28 11:23:08 -070052 mVsyncHintSent(false),
53 mInterceptVSyncs(interceptVSyncs) {
Mathias Agopianff28e202012-09-20 23:24:19 -070054
Jesse Hall9e663de2013-08-16 14:28:37 -070055 for (int32_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianff28e202012-09-20 23:24:19 -070056 mVSyncEvent[i].header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
57 mVSyncEvent[i].header.id = 0;
58 mVSyncEvent[i].header.timestamp = 0;
59 mVSyncEvent[i].vsync.count = 0;
60 }
Ruchi Kandoief472ec2014-04-02 12:50:06 -070061 struct sigevent se;
62 se.sigev_notify = SIGEV_THREAD;
63 se.sigev_value.sival_ptr = this;
64 se.sigev_notify_function = vsyncOffCallback;
65 se.sigev_notify_attributes = NULL;
66 timer_create(CLOCK_MONOTONIC, &se, &mTimerId);
67}
68
69void EventThread::sendVsyncHintOff() {
70 Mutex::Autolock _l(mLock);
71 mPowerHAL.vsyncHint(false);
72 mVsyncHintSent = false;
73}
74
Dan Stozadb4ac3c2015-04-14 11:34:01 -070075void EventThread::setPhaseOffset(nsecs_t phaseOffset) {
76 Mutex::Autolock _l(mLock);
77 mVSyncSource->setPhaseOffset(phaseOffset);
78}
79
Ruchi Kandoief472ec2014-04-02 12:50:06 -070080void EventThread::sendVsyncHintOnLocked() {
81 struct itimerspec ts;
82 if(!mVsyncHintSent) {
83 mPowerHAL.vsyncHint(true);
84 mVsyncHintSent = true;
85 }
86 ts.it_value.tv_sec = 0;
87 ts.it_value.tv_nsec = vsyncHintOffDelay;
88 ts.it_interval.tv_sec = 0;
89 ts.it_interval.tv_nsec = 0;
90 timer_settime(mTimerId, 0, &ts, NULL);
Mathias Agopiand0566bc2011-11-17 17:49:17 -080091}
92
93void EventThread::onFirstRef() {
94 run("EventThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
95}
96
Mathias Agopiancb9732a2012-04-03 17:48:03 -070097sp<EventThread::Connection> EventThread::createEventConnection() const {
98 return new Connection(const_cast<EventThread*>(this));
Mathias Agopian8aedd472012-01-24 16:39:14 -080099}
100
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800101status_t EventThread::registerDisplayEventConnection(
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700102 const sp<EventThread::Connection>& connection) {
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800103 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700104 mDisplayEventConnections.add(connection);
Mathias Agopian7d886472012-06-14 23:39:35 -0700105 mCondition.broadcast();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800106 return NO_ERROR;
107}
108
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800109void EventThread::removeDisplayEventConnection(
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700110 const wp<EventThread::Connection>& connection) {
Mathias Agopian23748662011-12-05 14:33:34 -0800111 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700112 mDisplayEventConnections.remove(connection);
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800113}
114
115void EventThread::setVsyncRate(uint32_t count,
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700116 const sp<EventThread::Connection>& connection) {
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800117 if (int32_t(count) >= 0) { // server must protect against bad params
118 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700119 const int32_t new_count = (count == 0) ? -1 : count;
120 if (connection->count != new_count) {
121 connection->count = new_count;
Mathias Agopian7d886472012-06-14 23:39:35 -0700122 mCondition.broadcast();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800123 }
124 }
125}
126
127void EventThread::requestNextVsync(
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700128 const sp<EventThread::Connection>& connection) {
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800129 Mutex::Autolock _l(mLock);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000130
131 mFlinger.resyncWithRateLimit();
132
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700133 if (connection->count < 0) {
134 connection->count = 0;
Mathias Agopian7d886472012-06-14 23:39:35 -0700135 mCondition.broadcast();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800136 }
Mathias Agopian23748662011-12-05 14:33:34 -0800137}
138
Mathias Agopian22ffb112012-04-10 21:04:02 -0700139void EventThread::onScreenReleased() {
140 Mutex::Autolock _l(mLock);
Mathias Agopian22ffb112012-04-10 21:04:02 -0700141 if (!mUseSoftwareVSync) {
Mathias Agopian7d886472012-06-14 23:39:35 -0700142 // disable reliance on h/w vsync
143 mUseSoftwareVSync = true;
144 mCondition.broadcast();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700145 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700146}
147
148void EventThread::onScreenAcquired() {
149 Mutex::Autolock _l(mLock);
Mathias Agopian7d886472012-06-14 23:39:35 -0700150 if (mUseSoftwareVSync) {
151 // resume use of h/w vsync
152 mUseSoftwareVSync = false;
153 mCondition.broadcast();
154 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700155}
156
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700157void EventThread::onVSyncEvent(nsecs_t timestamp) {
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700158 Mutex::Autolock _l(mLock);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700159 mVSyncEvent[0].header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
160 mVSyncEvent[0].header.id = 0;
161 mVSyncEvent[0].header.timestamp = timestamp;
162 mVSyncEvent[0].vsync.count++;
163 mCondition.broadcast();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700164}
165
Mathias Agopian148994e2012-09-19 17:31:36 -0700166void EventThread::onHotplugReceived(int type, bool connected) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700167 ALOGE_IF(type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES,
Jesse Hall7adb0f82013-03-06 16:13:49 -0800168 "received hotplug event for an invalid display (id=%d)", type);
Mathias Agopianff28e202012-09-20 23:24:19 -0700169
Mathias Agopian148994e2012-09-19 17:31:36 -0700170 Mutex::Autolock _l(mLock);
Jesse Hall9e663de2013-08-16 14:28:37 -0700171 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopianff28e202012-09-20 23:24:19 -0700172 DisplayEventReceiver::Event event;
173 event.header.type = DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG;
174 event.header.id = type;
175 event.header.timestamp = systemTime();
176 event.hotplug.connected = connected;
177 mPendingEvents.add(event);
178 mCondition.broadcast();
179 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700180}
181
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800182bool EventThread::threadLoop() {
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700183 DisplayEventReceiver::Event event;
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700184 Vector< sp<EventThread::Connection> > signalConnections;
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700185 signalConnections = waitForEvent(&event);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700186
Mathias Agopian148994e2012-09-19 17:31:36 -0700187 // dispatch events to listeners...
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700188 const size_t count = signalConnections.size();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800189 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700190 const sp<Connection>& conn(signalConnections[i]);
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700191 // now see if we still need to report this event
192 status_t err = conn->postEvent(event);
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700193 if (err == -EAGAIN || err == -EWOULDBLOCK) {
194 // The destination doesn't accept events anymore, it's probably
195 // full. For now, we just drop the events on the floor.
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700196 // FIXME: Note that some events cannot be dropped and would have
197 // to be re-sent later.
198 // Right-now we don't have the ability to do this.
199 ALOGW("EventThread: dropping event (%08x) for connection %p",
200 event.header.type, conn.get());
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700201 } else if (err < 0) {
202 // handle any other error on the pipe as fatal. the only
203 // reasonable thing to do is to clean-up this connection.
204 // The most common error we'll get here is -EPIPE.
205 removeDisplayEventConnection(signalConnections[i]);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800206 }
207 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800208 return true;
209}
210
Andy McFadden6bf552e2012-08-30 16:34:41 -0700211// This will return when (1) a vsync event has been received, and (2) there was
212// at least one connection interested in receiving it when we started waiting.
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700213Vector< sp<EventThread::Connection> > EventThread::waitForEvent(
214 DisplayEventReceiver::Event* event)
215{
216 Mutex::Autolock _l(mLock);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700217 Vector< sp<EventThread::Connection> > signalConnections;
218
219 do {
Mathias Agopian148994e2012-09-19 17:31:36 -0700220 bool eventPending = false;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700221 bool waitForVSync = false;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700222
Mathias Agopianff28e202012-09-20 23:24:19 -0700223 size_t vsyncCount = 0;
224 nsecs_t timestamp = 0;
Jesse Hall9e663de2013-08-16 14:28:37 -0700225 for (int32_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianff28e202012-09-20 23:24:19 -0700226 timestamp = mVSyncEvent[i].header.timestamp;
227 if (timestamp) {
228 // we have a vsync event to dispatch
Irvelab046852016-07-28 11:23:08 -0700229 if (mInterceptVSyncs) {
230 mFlinger.mInterceptor.saveVSyncEvent(timestamp);
231 }
Mathias Agopianff28e202012-09-20 23:24:19 -0700232 *event = mVSyncEvent[i];
233 mVSyncEvent[i].header.timestamp = 0;
234 vsyncCount = mVSyncEvent[i].vsync.count;
235 break;
236 }
237 }
238
239 if (!timestamp) {
240 // no vsync event, see if there are some other event
Mathias Agopian148994e2012-09-19 17:31:36 -0700241 eventPending = !mPendingEvents.isEmpty();
242 if (eventPending) {
243 // we have some other event to dispatch
244 *event = mPendingEvents[0];
245 mPendingEvents.removeAt(0);
246 }
247 }
248
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700249 // find out connections waiting for events
250 size_t count = mDisplayEventConnections.size();
Ivan Lozano01be49f2017-11-09 10:03:38 -0800251 for (size_t i=0 ; i<count ; ) {
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700252 sp<Connection> connection(mDisplayEventConnections[i].promote());
253 if (connection != NULL) {
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700254 bool added = false;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700255 if (connection->count >= 0) {
256 // we need vsync events because at least
257 // one connection is waiting for it
258 waitForVSync = true;
259 if (timestamp) {
260 // we consume the event only if it's time
261 // (ie: we received a vsync event)
262 if (connection->count == 0) {
263 // fired this time around
264 connection->count = -1;
265 signalConnections.add(connection);
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700266 added = true;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700267 } else if (connection->count == 1 ||
268 (vsyncCount % connection->count) == 0) {
269 // continuous event, and time to report it
270 signalConnections.add(connection);
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700271 added = true;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700272 }
273 }
274 }
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700275
276 if (eventPending && !timestamp && !added) {
277 // we don't have a vsync event to process
278 // (timestamp==0), but we have some pending
279 // messages.
280 signalConnections.add(connection);
281 }
Ivan Lozano01be49f2017-11-09 10:03:38 -0800282 ++i;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700283 } else {
284 // we couldn't promote this reference, the connection has
285 // died, so clean-up!
286 mDisplayEventConnections.removeAt(i);
Ivan Lozano01be49f2017-11-09 10:03:38 -0800287 --count;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700288 }
289 }
290
291 // Here we figure out if we need to enable or disable vsyncs
292 if (timestamp && !waitForVSync) {
293 // we received a VSYNC but we have no clients
294 // don't report it, and disable VSYNC events
295 disableVSyncLocked();
296 } else if (!timestamp && waitForVSync) {
Andy McFadden6bf552e2012-08-30 16:34:41 -0700297 // we have at least one client, so we want vsync enabled
298 // (TODO: this function is called right after we finish
299 // notifying clients of a vsync, so this call will be made
300 // at the vsync rate, e.g. 60fps. If we can accurately
301 // track the current state we could avoid making this call
302 // so often.)
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700303 enableVSyncLocked();
304 }
305
Andy McFadden6bf552e2012-08-30 16:34:41 -0700306 // note: !timestamp implies signalConnections.isEmpty(), because we
307 // don't populate signalConnections if there's no vsync pending
Mathias Agopian148994e2012-09-19 17:31:36 -0700308 if (!timestamp && !eventPending) {
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700309 // wait for something to happen
Andy McFadden6bf552e2012-08-30 16:34:41 -0700310 if (waitForVSync) {
311 // This is where we spend most of our time, waiting
312 // for vsync events and new client registrations.
313 //
314 // If the screen is off, we can't use h/w vsync, so we
315 // use a 16ms timeout instead. It doesn't need to be
316 // precise, we just need to keep feeding our clients.
317 //
318 // We don't want to stall if there's a driver bug, so we
319 // use a (long) timeout when waiting for h/w vsync, and
320 // generate fake events when necessary.
321 bool softwareSync = mUseSoftwareVSync;
322 nsecs_t timeout = softwareSync ? ms2ns(16) : ms2ns(1000);
323 if (mCondition.waitRelative(mLock, timeout) == TIMED_OUT) {
324 if (!softwareSync) {
325 ALOGW("Timed out waiting for hw vsync; faking it");
326 }
Mathias Agopianff28e202012-09-20 23:24:19 -0700327 // FIXME: how do we decide which display id the fake
328 // vsync came from ?
329 mVSyncEvent[0].header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
Jesse Hall9e663de2013-08-16 14:28:37 -0700330 mVSyncEvent[0].header.id = DisplayDevice::DISPLAY_PRIMARY;
Mathias Agopianff28e202012-09-20 23:24:19 -0700331 mVSyncEvent[0].header.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
332 mVSyncEvent[0].vsync.count++;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700333 }
334 } else {
Andy McFadden6bf552e2012-08-30 16:34:41 -0700335 // Nobody is interested in vsync, so we just want to sleep.
336 // h/w vsync should be disabled, so this will wait until we
337 // get a new connection, or an existing connection becomes
338 // interested in receiving vsync again.
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700339 mCondition.wait(mLock);
340 }
341 }
342 } while (signalConnections.isEmpty());
343
344 // here we're guaranteed to have a timestamp and some connections to signal
Andy McFadden6bf552e2012-08-30 16:34:41 -0700345 // (The connections might have dropped out of mDisplayEventConnections
346 // while we were asleep, but we'll still have strong references to them.)
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700347 return signalConnections;
348}
349
Mathias Agopian22ffb112012-04-10 21:04:02 -0700350void EventThread::enableVSyncLocked() {
351 if (!mUseSoftwareVSync) {
352 // never enable h/w VSYNC when screen is off
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700353 if (!mVsyncEnabled) {
354 mVsyncEnabled = true;
355 mVSyncSource->setCallback(static_cast<VSyncSource::Callback*>(this));
356 mVSyncSource->setVSyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700357 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700358 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700359 mDebugVsyncEnabled = true;
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700360 sendVsyncHintOnLocked();
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700361}
362
Mathias Agopian22ffb112012-04-10 21:04:02 -0700363void EventThread::disableVSyncLocked() {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700364 if (mVsyncEnabled) {
365 mVsyncEnabled = false;
366 mVSyncSource->setVSyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700367 mDebugVsyncEnabled = false;
368 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700369}
370
Mathias Agopian74d211a2013-04-22 16:55:35 +0200371void EventThread::dump(String8& result) const {
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800372 Mutex::Autolock _l(mLock);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700373 result.appendFormat("VSYNC state: %s\n",
374 mDebugVsyncEnabled?"enabled":"disabled");
Mathias Agopian22ffb112012-04-10 21:04:02 -0700375 result.appendFormat(" soft-vsync: %s\n",
376 mUseSoftwareVSync?"enabled":"disabled");
Greg Hackmann86efcc02014-03-07 12:44:02 -0800377 result.appendFormat(" numListeners=%zu,\n events-delivered: %u\n",
Mathias Agopianff28e202012-09-20 23:24:19 -0700378 mDisplayEventConnections.size(),
Jesse Hall9e663de2013-08-16 14:28:37 -0700379 mVSyncEvent[DisplayDevice::DISPLAY_PRIMARY].vsync.count);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700380 for (size_t i=0 ; i<mDisplayEventConnections.size() ; i++) {
381 sp<Connection> connection =
382 mDisplayEventConnections.itemAt(i).promote();
383 result.appendFormat(" %p: count=%d\n",
384 connection.get(), connection!=NULL ? connection->count : 0);
385 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800386}
387
388// ---------------------------------------------------------------------------
389
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700390EventThread::Connection::Connection(
391 const sp<EventThread>& eventThread)
Dan Stoza6b698e42017-04-03 13:09:08 -0700392 : count(-1), mEventThread(eventThread), mChannel(gui::BitTube::DefaultSize)
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700393{
394}
395
396EventThread::Connection::~Connection() {
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700397 // do nothing here -- clean-up will happen automatically
398 // when the main thread wakes up
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700399}
400
401void EventThread::Connection::onFirstRef() {
402 // NOTE: mEventThread doesn't hold a strong reference on us
403 mEventThread->registerDisplayEventConnection(this);
404}
405
Dan Stoza6b698e42017-04-03 13:09:08 -0700406status_t EventThread::Connection::stealReceiveChannel(gui::BitTube* outChannel) {
407 outChannel->setReceiveFd(mChannel.moveReceiveFd());
Dan Stozae1c599b2017-03-30 16:37:19 -0700408 return NO_ERROR;
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700409}
410
Dan Stozae1c599b2017-03-30 16:37:19 -0700411status_t EventThread::Connection::setVsyncRate(uint32_t count) {
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700412 mEventThread->setVsyncRate(count, this);
Dan Stozae1c599b2017-03-30 16:37:19 -0700413 return NO_ERROR;
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700414}
415
416void EventThread::Connection::requestNextVsync() {
417 mEventThread->requestNextVsync(this);
418}
419
420status_t EventThread::Connection::postEvent(
421 const DisplayEventReceiver::Event& event) {
Dan Stoza6b698e42017-04-03 13:09:08 -0700422 ssize_t size = DisplayEventReceiver::sendEvents(&mChannel, &event, 1);
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700423 return size < 0 ? status_t(size) : status_t(NO_ERROR);
424}
425
426// ---------------------------------------------------------------------------
427
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800428}; // namespace android