blob: d868f3235bccfb451b5d02554700bb551af80d4a [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 Agopiancb9732a2012-04-03 17:48:03 -070024#include <gui/BitTube.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080025#include <gui/IDisplayEventConnection.h>
26#include <gui/DisplayEventReceiver.h>
27
28#include <utils/Errors.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070029#include <utils/String8.h>
Mathias Agopian841cde52012-03-01 15:44:37 -080030#include <utils/Trace.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080031
Mathias Agopiand0566bc2011-11-17 17:49:17 -080032#include "EventThread.h"
33#include "SurfaceFlinger.h"
34
35// ---------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -080036namespace android {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080037// ---------------------------------------------------------------------------
38
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070039EventThread::EventThread(const sp<VSyncSource>& src)
40 : mVSyncSource(src),
Mathias Agopian22ffb112012-04-10 21:04:02 -070041 mUseSoftwareVSync(false),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070042 mVsyncEnabled(false),
Ruchi Kandoi4098f032014-04-04 18:09:30 -070043 mDebugVsyncEnabled(false) {
Mathias Agopianff28e202012-09-20 23:24:19 -070044
Jesse Hall9e663de2013-08-16 14:28:37 -070045 for (int32_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianff28e202012-09-20 23:24:19 -070046 mVSyncEvent[i].header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
47 mVSyncEvent[i].header.id = 0;
48 mVSyncEvent[i].header.timestamp = 0;
49 mVSyncEvent[i].vsync.count = 0;
50 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -080051}
52
53void EventThread::onFirstRef() {
54 run("EventThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
55}
56
Mathias Agopiancb9732a2012-04-03 17:48:03 -070057sp<EventThread::Connection> EventThread::createEventConnection() const {
58 return new Connection(const_cast<EventThread*>(this));
Mathias Agopian8aedd472012-01-24 16:39:14 -080059}
60
Mathias Agopiand0566bc2011-11-17 17:49:17 -080061status_t EventThread::registerDisplayEventConnection(
Mathias Agopiancb9732a2012-04-03 17:48:03 -070062 const sp<EventThread::Connection>& connection) {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080063 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070064 mDisplayEventConnections.add(connection);
Mathias Agopian7d886472012-06-14 23:39:35 -070065 mCondition.broadcast();
Mathias Agopiand0566bc2011-11-17 17:49:17 -080066 return NO_ERROR;
67}
68
Mathias Agopian478ae5e2011-12-06 17:22:19 -080069void EventThread::removeDisplayEventConnection(
Mathias Agopiancb9732a2012-04-03 17:48:03 -070070 const wp<EventThread::Connection>& connection) {
Mathias Agopian23748662011-12-05 14:33:34 -080071 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070072 mDisplayEventConnections.remove(connection);
Mathias Agopian478ae5e2011-12-06 17:22:19 -080073}
74
75void EventThread::setVsyncRate(uint32_t count,
Mathias Agopiancb9732a2012-04-03 17:48:03 -070076 const sp<EventThread::Connection>& connection) {
Mathias Agopian478ae5e2011-12-06 17:22:19 -080077 if (int32_t(count) >= 0) { // server must protect against bad params
78 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070079 const int32_t new_count = (count == 0) ? -1 : count;
80 if (connection->count != new_count) {
81 connection->count = new_count;
Mathias Agopian7d886472012-06-14 23:39:35 -070082 mCondition.broadcast();
Mathias Agopian478ae5e2011-12-06 17:22:19 -080083 }
84 }
85}
86
87void EventThread::requestNextVsync(
Mathias Agopiancb9732a2012-04-03 17:48:03 -070088 const sp<EventThread::Connection>& connection) {
Mathias Agopian478ae5e2011-12-06 17:22:19 -080089 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070090 if (connection->count < 0) {
91 connection->count = 0;
Mathias Agopian7d886472012-06-14 23:39:35 -070092 mCondition.broadcast();
Mathias Agopian478ae5e2011-12-06 17:22:19 -080093 }
Mathias Agopian23748662011-12-05 14:33:34 -080094}
95
Mathias Agopian22ffb112012-04-10 21:04:02 -070096void EventThread::onScreenReleased() {
97 Mutex::Autolock _l(mLock);
Mathias Agopian22ffb112012-04-10 21:04:02 -070098 if (!mUseSoftwareVSync) {
Mathias Agopian7d886472012-06-14 23:39:35 -070099 // disable reliance on h/w vsync
100 mUseSoftwareVSync = true;
101 mCondition.broadcast();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700102 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700103}
104
105void EventThread::onScreenAcquired() {
106 Mutex::Autolock _l(mLock);
Mathias Agopian7d886472012-06-14 23:39:35 -0700107 if (mUseSoftwareVSync) {
108 // resume use of h/w vsync
109 mUseSoftwareVSync = false;
110 mCondition.broadcast();
111 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700112}
113
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700114void EventThread::onVSyncEvent(nsecs_t timestamp) {
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700115 Mutex::Autolock _l(mLock);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700116 mVSyncEvent[0].header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
117 mVSyncEvent[0].header.id = 0;
118 mVSyncEvent[0].header.timestamp = timestamp;
119 mVSyncEvent[0].vsync.count++;
120 mCondition.broadcast();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700121}
122
Mathias Agopian148994e2012-09-19 17:31:36 -0700123void EventThread::onHotplugReceived(int type, bool connected) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700124 ALOGE_IF(type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES,
Jesse Hall7adb0f82013-03-06 16:13:49 -0800125 "received hotplug event for an invalid display (id=%d)", type);
Mathias Agopianff28e202012-09-20 23:24:19 -0700126
Mathias Agopian148994e2012-09-19 17:31:36 -0700127 Mutex::Autolock _l(mLock);
Jesse Hall9e663de2013-08-16 14:28:37 -0700128 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopianff28e202012-09-20 23:24:19 -0700129 DisplayEventReceiver::Event event;
130 event.header.type = DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG;
131 event.header.id = type;
132 event.header.timestamp = systemTime();
133 event.hotplug.connected = connected;
134 mPendingEvents.add(event);
135 mCondition.broadcast();
136 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700137}
138
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800139bool EventThread::threadLoop() {
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700140 DisplayEventReceiver::Event event;
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700141 Vector< sp<EventThread::Connection> > signalConnections;
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700142 signalConnections = waitForEvent(&event);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700143
Mathias Agopian148994e2012-09-19 17:31:36 -0700144 // dispatch events to listeners...
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700145 const size_t count = signalConnections.size();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800146 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700147 const sp<Connection>& conn(signalConnections[i]);
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700148 // now see if we still need to report this event
149 status_t err = conn->postEvent(event);
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700150 if (err == -EAGAIN || err == -EWOULDBLOCK) {
151 // The destination doesn't accept events anymore, it's probably
152 // full. For now, we just drop the events on the floor.
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700153 // FIXME: Note that some events cannot be dropped and would have
154 // to be re-sent later.
155 // Right-now we don't have the ability to do this.
156 ALOGW("EventThread: dropping event (%08x) for connection %p",
157 event.header.type, conn.get());
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700158 } else if (err < 0) {
159 // handle any other error on the pipe as fatal. the only
160 // reasonable thing to do is to clean-up this connection.
161 // The most common error we'll get here is -EPIPE.
162 removeDisplayEventConnection(signalConnections[i]);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800163 }
164 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800165 return true;
166}
167
Andy McFadden6bf552e2012-08-30 16:34:41 -0700168// This will return when (1) a vsync event has been received, and (2) there was
169// at least one connection interested in receiving it when we started waiting.
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700170Vector< sp<EventThread::Connection> > EventThread::waitForEvent(
171 DisplayEventReceiver::Event* event)
172{
173 Mutex::Autolock _l(mLock);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700174 Vector< sp<EventThread::Connection> > signalConnections;
175
176 do {
Mathias Agopian148994e2012-09-19 17:31:36 -0700177 bool eventPending = false;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700178 bool waitForVSync = false;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700179
Mathias Agopianff28e202012-09-20 23:24:19 -0700180 size_t vsyncCount = 0;
181 nsecs_t timestamp = 0;
Jesse Hall9e663de2013-08-16 14:28:37 -0700182 for (int32_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianff28e202012-09-20 23:24:19 -0700183 timestamp = mVSyncEvent[i].header.timestamp;
184 if (timestamp) {
185 // we have a vsync event to dispatch
186 *event = mVSyncEvent[i];
187 mVSyncEvent[i].header.timestamp = 0;
188 vsyncCount = mVSyncEvent[i].vsync.count;
189 break;
190 }
191 }
192
193 if (!timestamp) {
194 // no vsync event, see if there are some other event
Mathias Agopian148994e2012-09-19 17:31:36 -0700195 eventPending = !mPendingEvents.isEmpty();
196 if (eventPending) {
197 // we have some other event to dispatch
198 *event = mPendingEvents[0];
199 mPendingEvents.removeAt(0);
200 }
201 }
202
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700203 // find out connections waiting for events
204 size_t count = mDisplayEventConnections.size();
205 for (size_t i=0 ; i<count ; i++) {
206 sp<Connection> connection(mDisplayEventConnections[i].promote());
207 if (connection != NULL) {
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700208 bool added = false;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700209 if (connection->count >= 0) {
210 // we need vsync events because at least
211 // one connection is waiting for it
212 waitForVSync = true;
213 if (timestamp) {
214 // we consume the event only if it's time
215 // (ie: we received a vsync event)
216 if (connection->count == 0) {
217 // fired this time around
218 connection->count = -1;
219 signalConnections.add(connection);
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700220 added = true;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700221 } else if (connection->count == 1 ||
222 (vsyncCount % connection->count) == 0) {
223 // continuous event, and time to report it
224 signalConnections.add(connection);
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700225 added = true;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700226 }
227 }
228 }
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700229
230 if (eventPending && !timestamp && !added) {
231 // we don't have a vsync event to process
232 // (timestamp==0), but we have some pending
233 // messages.
234 signalConnections.add(connection);
235 }
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700236 } else {
237 // we couldn't promote this reference, the connection has
238 // died, so clean-up!
239 mDisplayEventConnections.removeAt(i);
240 --i; --count;
241 }
242 }
243
244 // Here we figure out if we need to enable or disable vsyncs
245 if (timestamp && !waitForVSync) {
246 // we received a VSYNC but we have no clients
247 // don't report it, and disable VSYNC events
248 disableVSyncLocked();
249 } else if (!timestamp && waitForVSync) {
Andy McFadden6bf552e2012-08-30 16:34:41 -0700250 // we have at least one client, so we want vsync enabled
251 // (TODO: this function is called right after we finish
252 // notifying clients of a vsync, so this call will be made
253 // at the vsync rate, e.g. 60fps. If we can accurately
254 // track the current state we could avoid making this call
255 // so often.)
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700256 enableVSyncLocked();
257 }
258
Andy McFadden6bf552e2012-08-30 16:34:41 -0700259 // note: !timestamp implies signalConnections.isEmpty(), because we
260 // don't populate signalConnections if there's no vsync pending
Mathias Agopian148994e2012-09-19 17:31:36 -0700261 if (!timestamp && !eventPending) {
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700262 // wait for something to happen
Andy McFadden6bf552e2012-08-30 16:34:41 -0700263 if (waitForVSync) {
264 // This is where we spend most of our time, waiting
265 // for vsync events and new client registrations.
266 //
267 // If the screen is off, we can't use h/w vsync, so we
268 // use a 16ms timeout instead. It doesn't need to be
269 // precise, we just need to keep feeding our clients.
270 //
271 // We don't want to stall if there's a driver bug, so we
272 // use a (long) timeout when waiting for h/w vsync, and
273 // generate fake events when necessary.
274 bool softwareSync = mUseSoftwareVSync;
275 nsecs_t timeout = softwareSync ? ms2ns(16) : ms2ns(1000);
276 if (mCondition.waitRelative(mLock, timeout) == TIMED_OUT) {
277 if (!softwareSync) {
278 ALOGW("Timed out waiting for hw vsync; faking it");
279 }
Mathias Agopianff28e202012-09-20 23:24:19 -0700280 // FIXME: how do we decide which display id the fake
281 // vsync came from ?
282 mVSyncEvent[0].header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
Jesse Hall9e663de2013-08-16 14:28:37 -0700283 mVSyncEvent[0].header.id = DisplayDevice::DISPLAY_PRIMARY;
Mathias Agopianff28e202012-09-20 23:24:19 -0700284 mVSyncEvent[0].header.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
285 mVSyncEvent[0].vsync.count++;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700286 }
287 } else {
Andy McFadden6bf552e2012-08-30 16:34:41 -0700288 // Nobody is interested in vsync, so we just want to sleep.
289 // h/w vsync should be disabled, so this will wait until we
290 // get a new connection, or an existing connection becomes
291 // interested in receiving vsync again.
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700292 mCondition.wait(mLock);
293 }
294 }
295 } while (signalConnections.isEmpty());
296
297 // here we're guaranteed to have a timestamp and some connections to signal
Andy McFadden6bf552e2012-08-30 16:34:41 -0700298 // (The connections might have dropped out of mDisplayEventConnections
299 // while we were asleep, but we'll still have strong references to them.)
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700300 return signalConnections;
301}
302
Mathias Agopian22ffb112012-04-10 21:04:02 -0700303void EventThread::enableVSyncLocked() {
304 if (!mUseSoftwareVSync) {
305 // never enable h/w VSYNC when screen is off
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700306 if (!mVsyncEnabled) {
307 mVsyncEnabled = true;
308 mVSyncSource->setCallback(static_cast<VSyncSource::Callback*>(this));
309 mVSyncSource->setVSyncEnabled(true);
Ruchi Kandoi4098f032014-04-04 18:09:30 -0700310 mPowerHAL.vsyncHint(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700311 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700312 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700313 mDebugVsyncEnabled = true;
314}
315
Mathias Agopian22ffb112012-04-10 21:04:02 -0700316void EventThread::disableVSyncLocked() {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700317 if (mVsyncEnabled) {
318 mVsyncEnabled = false;
319 mVSyncSource->setVSyncEnabled(false);
Ruchi Kandoi4098f032014-04-04 18:09:30 -0700320 mPowerHAL.vsyncHint(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700321 mDebugVsyncEnabled = false;
322 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700323}
324
Mathias Agopian74d211a2013-04-22 16:55:35 +0200325void EventThread::dump(String8& result) const {
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800326 Mutex::Autolock _l(mLock);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700327 result.appendFormat("VSYNC state: %s\n",
328 mDebugVsyncEnabled?"enabled":"disabled");
Mathias Agopian22ffb112012-04-10 21:04:02 -0700329 result.appendFormat(" soft-vsync: %s\n",
330 mUseSoftwareVSync?"enabled":"disabled");
Greg Hackmann86efcc02014-03-07 12:44:02 -0800331 result.appendFormat(" numListeners=%zu,\n events-delivered: %u\n",
Mathias Agopianff28e202012-09-20 23:24:19 -0700332 mDisplayEventConnections.size(),
Jesse Hall9e663de2013-08-16 14:28:37 -0700333 mVSyncEvent[DisplayDevice::DISPLAY_PRIMARY].vsync.count);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700334 for (size_t i=0 ; i<mDisplayEventConnections.size() ; i++) {
335 sp<Connection> connection =
336 mDisplayEventConnections.itemAt(i).promote();
337 result.appendFormat(" %p: count=%d\n",
338 connection.get(), connection!=NULL ? connection->count : 0);
339 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800340}
341
342// ---------------------------------------------------------------------------
343
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700344EventThread::Connection::Connection(
345 const sp<EventThread>& eventThread)
346 : count(-1), mEventThread(eventThread), mChannel(new BitTube())
347{
348}
349
350EventThread::Connection::~Connection() {
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700351 // do nothing here -- clean-up will happen automatically
352 // when the main thread wakes up
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700353}
354
355void EventThread::Connection::onFirstRef() {
356 // NOTE: mEventThread doesn't hold a strong reference on us
357 mEventThread->registerDisplayEventConnection(this);
358}
359
360sp<BitTube> EventThread::Connection::getDataChannel() const {
361 return mChannel;
362}
363
364void EventThread::Connection::setVsyncRate(uint32_t count) {
365 mEventThread->setVsyncRate(count, this);
366}
367
368void EventThread::Connection::requestNextVsync() {
369 mEventThread->requestNextVsync(this);
370}
371
372status_t EventThread::Connection::postEvent(
373 const DisplayEventReceiver::Event& event) {
374 ssize_t size = DisplayEventReceiver::sendEvents(mChannel, &event, 1);
375 return size < 0 ? status_t(size) : status_t(NO_ERROR);
376}
377
378// ---------------------------------------------------------------------------
379
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800380}; // namespace android