blob: 6e7424efa3399bc575341e5b378fcbe8808aef76 [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
39EventThread::EventThread(const sp<SurfaceFlinger>& flinger)
Mathias Agopian86303202012-07-24 22:46:10 -070040 : mFlinger(flinger),
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070041 mVSyncTimestamp(0),
Mathias Agopian22ffb112012-04-10 21:04:02 -070042 mUseSoftwareVSync(false),
Mathias Agopian10125f02012-08-17 15:06:02 -070043 mVSyncCount(0),
Mathias Agopian921e6ac2012-07-23 23:11:29 -070044 mDebugVsyncEnabled(false) {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080045}
46
47void EventThread::onFirstRef() {
48 run("EventThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
49}
50
Mathias Agopiancb9732a2012-04-03 17:48:03 -070051sp<EventThread::Connection> EventThread::createEventConnection() const {
52 return new Connection(const_cast<EventThread*>(this));
Mathias Agopian8aedd472012-01-24 16:39:14 -080053}
54
Mathias Agopiand0566bc2011-11-17 17:49:17 -080055status_t EventThread::registerDisplayEventConnection(
Mathias Agopiancb9732a2012-04-03 17:48:03 -070056 const sp<EventThread::Connection>& connection) {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080057 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070058 mDisplayEventConnections.add(connection);
Mathias Agopian7d886472012-06-14 23:39:35 -070059 mCondition.broadcast();
Mathias Agopiand0566bc2011-11-17 17:49:17 -080060 return NO_ERROR;
61}
62
Mathias Agopian478ae5e2011-12-06 17:22:19 -080063void EventThread::removeDisplayEventConnection(
Mathias Agopiancb9732a2012-04-03 17:48:03 -070064 const wp<EventThread::Connection>& connection) {
Mathias Agopian23748662011-12-05 14:33:34 -080065 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070066 mDisplayEventConnections.remove(connection);
Mathias Agopian478ae5e2011-12-06 17:22:19 -080067}
68
69void EventThread::setVsyncRate(uint32_t count,
Mathias Agopiancb9732a2012-04-03 17:48:03 -070070 const sp<EventThread::Connection>& connection) {
Mathias Agopian478ae5e2011-12-06 17:22:19 -080071 if (int32_t(count) >= 0) { // server must protect against bad params
72 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070073 const int32_t new_count = (count == 0) ? -1 : count;
74 if (connection->count != new_count) {
75 connection->count = new_count;
Mathias Agopian7d886472012-06-14 23:39:35 -070076 mCondition.broadcast();
Mathias Agopian478ae5e2011-12-06 17:22:19 -080077 }
78 }
79}
80
81void EventThread::requestNextVsync(
Mathias Agopiancb9732a2012-04-03 17:48:03 -070082 const sp<EventThread::Connection>& connection) {
Mathias Agopian478ae5e2011-12-06 17:22:19 -080083 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070084 if (connection->count < 0) {
85 connection->count = 0;
Mathias Agopian7d886472012-06-14 23:39:35 -070086 mCondition.broadcast();
Mathias Agopian478ae5e2011-12-06 17:22:19 -080087 }
Mathias Agopian23748662011-12-05 14:33:34 -080088}
89
Mathias Agopian22ffb112012-04-10 21:04:02 -070090void EventThread::onScreenReleased() {
91 Mutex::Autolock _l(mLock);
Mathias Agopian22ffb112012-04-10 21:04:02 -070092 if (!mUseSoftwareVSync) {
Mathias Agopian7d886472012-06-14 23:39:35 -070093 // disable reliance on h/w vsync
94 mUseSoftwareVSync = true;
95 mCondition.broadcast();
Mathias Agopian22ffb112012-04-10 21:04:02 -070096 }
Mathias Agopian22ffb112012-04-10 21:04:02 -070097}
98
99void EventThread::onScreenAcquired() {
100 Mutex::Autolock _l(mLock);
Mathias Agopian7d886472012-06-14 23:39:35 -0700101 if (mUseSoftwareVSync) {
102 // resume use of h/w vsync
103 mUseSoftwareVSync = false;
104 mCondition.broadcast();
105 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700106}
107
108
Mathias Agopian148994e2012-09-19 17:31:36 -0700109void EventThread::onVSyncReceived(int type, nsecs_t timestamp) {
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700110 Mutex::Autolock _l(mLock);
111 mVSyncTimestamp = timestamp;
Mathias Agopian10125f02012-08-17 15:06:02 -0700112 mVSyncCount++;
Mathias Agopian7d886472012-06-14 23:39:35 -0700113 mCondition.broadcast();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700114}
115
Mathias Agopian148994e2012-09-19 17:31:36 -0700116void EventThread::onHotplugReceived(int type, bool connected) {
117 Mutex::Autolock _l(mLock);
118 DisplayEventReceiver::Event event;
119 event.header.type = DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG;
120 event.header.timestamp = systemTime();
121 event.hotplug.id = type;
122 event.hotplug.connected = connected;
123 mPendingEvents.add(event);
124 mCondition.broadcast();
125}
126
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800127bool EventThread::threadLoop() {
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800128 DisplayEventReceiver::Event vsync;
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700129 Vector< sp<EventThread::Connection> > signalConnections;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700130 signalConnections = waitForEvent(&vsync);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700131
Mathias Agopian148994e2012-09-19 17:31:36 -0700132 // dispatch events to listeners...
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700133 const size_t count = signalConnections.size();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800134 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700135 const sp<Connection>& conn(signalConnections[i]);
Mathias Agopian10125f02012-08-17 15:06:02 -0700136 // now see if we still need to report this VSYNC event
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700137 status_t err = conn->postEvent(vsync);
138 if (err == -EAGAIN || err == -EWOULDBLOCK) {
139 // The destination doesn't accept events anymore, it's probably
140 // full. For now, we just drop the events on the floor.
141 // Note that some events cannot be dropped and would have to be
142 // re-sent later. Right-now we don't have the ability to do
143 // this, but it doesn't matter for VSYNC.
144 } else if (err < 0) {
145 // handle any other error on the pipe as fatal. the only
146 // reasonable thing to do is to clean-up this connection.
147 // The most common error we'll get here is -EPIPE.
148 removeDisplayEventConnection(signalConnections[i]);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800149 }
150 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800151 return true;
152}
153
Andy McFadden6bf552e2012-08-30 16:34:41 -0700154// This will return when (1) a vsync event has been received, and (2) there was
155// at least one connection interested in receiving it when we started waiting.
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700156Vector< sp<EventThread::Connection> > EventThread::waitForEvent(
157 DisplayEventReceiver::Event* event)
158{
159 Mutex::Autolock _l(mLock);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700160 Vector< sp<EventThread::Connection> > signalConnections;
161
162 do {
Mathias Agopian148994e2012-09-19 17:31:36 -0700163 bool eventPending = false;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700164 bool waitForVSync = false;
Mathias Agopian148994e2012-09-19 17:31:36 -0700165 size_t vsyncCount = mVSyncCount;
166 nsecs_t timestamp = mVSyncTimestamp;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700167 mVSyncTimestamp = 0;
168
Mathias Agopian148994e2012-09-19 17:31:36 -0700169 if (timestamp) {
170 // we have a vsync event to dispatch
171 event->header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
172 event->header.timestamp = timestamp;
173 event->vsync.count = vsyncCount;
174 } else {
175 eventPending = !mPendingEvents.isEmpty();
176 if (eventPending) {
177 // we have some other event to dispatch
178 *event = mPendingEvents[0];
179 mPendingEvents.removeAt(0);
180 }
181 }
182
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700183 // find out connections waiting for events
184 size_t count = mDisplayEventConnections.size();
185 for (size_t i=0 ; i<count ; i++) {
186 sp<Connection> connection(mDisplayEventConnections[i].promote());
187 if (connection != NULL) {
188 if (connection->count >= 0) {
189 // we need vsync events because at least
190 // one connection is waiting for it
191 waitForVSync = true;
192 if (timestamp) {
193 // we consume the event only if it's time
194 // (ie: we received a vsync event)
195 if (connection->count == 0) {
196 // fired this time around
197 connection->count = -1;
198 signalConnections.add(connection);
199 } else if (connection->count == 1 ||
200 (vsyncCount % connection->count) == 0) {
201 // continuous event, and time to report it
202 signalConnections.add(connection);
203 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700204 } else if (eventPending) {
205 // we don't have a vsync event to process
206 // (timestamp==0), but we have some pending
207 // messages.
208 signalConnections.add(connection);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700209 }
210 }
211 } else {
212 // we couldn't promote this reference, the connection has
213 // died, so clean-up!
214 mDisplayEventConnections.removeAt(i);
215 --i; --count;
216 }
217 }
218
219 // Here we figure out if we need to enable or disable vsyncs
220 if (timestamp && !waitForVSync) {
221 // we received a VSYNC but we have no clients
222 // don't report it, and disable VSYNC events
223 disableVSyncLocked();
224 } else if (!timestamp && waitForVSync) {
Andy McFadden6bf552e2012-08-30 16:34:41 -0700225 // we have at least one client, so we want vsync enabled
226 // (TODO: this function is called right after we finish
227 // notifying clients of a vsync, so this call will be made
228 // at the vsync rate, e.g. 60fps. If we can accurately
229 // track the current state we could avoid making this call
230 // so often.)
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700231 enableVSyncLocked();
232 }
233
Andy McFadden6bf552e2012-08-30 16:34:41 -0700234 // note: !timestamp implies signalConnections.isEmpty(), because we
235 // don't populate signalConnections if there's no vsync pending
Mathias Agopian148994e2012-09-19 17:31:36 -0700236 if (!timestamp && !eventPending) {
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700237 // wait for something to happen
Andy McFadden6bf552e2012-08-30 16:34:41 -0700238 if (waitForVSync) {
239 // This is where we spend most of our time, waiting
240 // for vsync events and new client registrations.
241 //
242 // If the screen is off, we can't use h/w vsync, so we
243 // use a 16ms timeout instead. It doesn't need to be
244 // precise, we just need to keep feeding our clients.
245 //
246 // We don't want to stall if there's a driver bug, so we
247 // use a (long) timeout when waiting for h/w vsync, and
248 // generate fake events when necessary.
249 bool softwareSync = mUseSoftwareVSync;
250 nsecs_t timeout = softwareSync ? ms2ns(16) : ms2ns(1000);
251 if (mCondition.waitRelative(mLock, timeout) == TIMED_OUT) {
252 if (!softwareSync) {
253 ALOGW("Timed out waiting for hw vsync; faking it");
254 }
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700255 mVSyncTimestamp = systemTime(SYSTEM_TIME_MONOTONIC);
256 mVSyncCount++;
257 }
258 } else {
Andy McFadden6bf552e2012-08-30 16:34:41 -0700259 // Nobody is interested in vsync, so we just want to sleep.
260 // h/w vsync should be disabled, so this will wait until we
261 // get a new connection, or an existing connection becomes
262 // interested in receiving vsync again.
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700263 mCondition.wait(mLock);
264 }
265 }
266 } while (signalConnections.isEmpty());
267
268 // here we're guaranteed to have a timestamp and some connections to signal
Andy McFadden6bf552e2012-08-30 16:34:41 -0700269 // (The connections might have dropped out of mDisplayEventConnections
270 // while we were asleep, but we'll still have strong references to them.)
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700271 return signalConnections;
272}
273
Mathias Agopian22ffb112012-04-10 21:04:02 -0700274void EventThread::enableVSyncLocked() {
275 if (!mUseSoftwareVSync) {
276 // never enable h/w VSYNC when screen is off
Mathias Agopian86303202012-07-24 22:46:10 -0700277 mFlinger->eventControl(SurfaceFlinger::EVENT_VSYNC, true);
278 mPowerHAL.vsyncHint(true);
Mathias Agopian22ffb112012-04-10 21:04:02 -0700279 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700280 mDebugVsyncEnabled = true;
281}
282
Mathias Agopian22ffb112012-04-10 21:04:02 -0700283void EventThread::disableVSyncLocked() {
Mathias Agopian86303202012-07-24 22:46:10 -0700284 mFlinger->eventControl(SurfaceFlinger::EVENT_VSYNC, false);
285 mPowerHAL.vsyncHint(false);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700286 mDebugVsyncEnabled = false;
287}
288
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800289void EventThread::dump(String8& result, char* buffer, size_t SIZE) const {
290 Mutex::Autolock _l(mLock);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700291 result.appendFormat("VSYNC state: %s\n",
292 mDebugVsyncEnabled?"enabled":"disabled");
Mathias Agopian22ffb112012-04-10 21:04:02 -0700293 result.appendFormat(" soft-vsync: %s\n",
294 mUseSoftwareVSync?"enabled":"disabled");
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700295 result.appendFormat(" numListeners=%u,\n events-delivered: %u\n",
Mathias Agopian10125f02012-08-17 15:06:02 -0700296 mDisplayEventConnections.size(), mVSyncCount);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700297 for (size_t i=0 ; i<mDisplayEventConnections.size() ; i++) {
298 sp<Connection> connection =
299 mDisplayEventConnections.itemAt(i).promote();
300 result.appendFormat(" %p: count=%d\n",
301 connection.get(), connection!=NULL ? connection->count : 0);
302 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800303}
304
305// ---------------------------------------------------------------------------
306
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700307EventThread::Connection::Connection(
308 const sp<EventThread>& eventThread)
309 : count(-1), mEventThread(eventThread), mChannel(new BitTube())
310{
311}
312
313EventThread::Connection::~Connection() {
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700314 // do nothing here -- clean-up will happen automatically
315 // when the main thread wakes up
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700316}
317
318void EventThread::Connection::onFirstRef() {
319 // NOTE: mEventThread doesn't hold a strong reference on us
320 mEventThread->registerDisplayEventConnection(this);
321}
322
323sp<BitTube> EventThread::Connection::getDataChannel() const {
324 return mChannel;
325}
326
327void EventThread::Connection::setVsyncRate(uint32_t count) {
328 mEventThread->setVsyncRate(count, this);
329}
330
331void EventThread::Connection::requestNextVsync() {
332 mEventThread->requestNextVsync(this);
333}
334
335status_t EventThread::Connection::postEvent(
336 const DisplayEventReceiver::Event& event) {
337 ssize_t size = DisplayEventReceiver::sendEvents(mChannel, &event, 1);
338 return size < 0 ? status_t(size) : status_t(NO_ERROR);
339}
340
341// ---------------------------------------------------------------------------
342
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800343}; // namespace android