blob: 24e9f2b43b41b871815c09ae43259ce844fa7023 [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 Agopianb4d18ed2012-09-20 23:14:05 -0700128 DisplayEventReceiver::Event event;
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700129 Vector< sp<EventThread::Connection> > signalConnections;
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700130 signalConnections = waitForEvent(&event);
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 Agopianb4d18ed2012-09-20 23:14:05 -0700136 // now see if we still need to report this event
137 status_t err = conn->postEvent(event);
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700138 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.
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700141 // FIXME: Note that some events cannot be dropped and would have
142 // to be re-sent later.
143 // Right-now we don't have the ability to do this.
144 ALOGW("EventThread: dropping event (%08x) for connection %p",
145 event.header.type, conn.get());
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700146 } else if (err < 0) {
147 // handle any other error on the pipe as fatal. the only
148 // reasonable thing to do is to clean-up this connection.
149 // The most common error we'll get here is -EPIPE.
150 removeDisplayEventConnection(signalConnections[i]);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800151 }
152 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800153 return true;
154}
155
Andy McFadden6bf552e2012-08-30 16:34:41 -0700156// This will return when (1) a vsync event has been received, and (2) there was
157// at least one connection interested in receiving it when we started waiting.
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700158Vector< sp<EventThread::Connection> > EventThread::waitForEvent(
159 DisplayEventReceiver::Event* event)
160{
161 Mutex::Autolock _l(mLock);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700162 Vector< sp<EventThread::Connection> > signalConnections;
163
164 do {
Mathias Agopian148994e2012-09-19 17:31:36 -0700165 bool eventPending = false;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700166 bool waitForVSync = false;
Mathias Agopian148994e2012-09-19 17:31:36 -0700167 size_t vsyncCount = mVSyncCount;
168 nsecs_t timestamp = mVSyncTimestamp;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700169 mVSyncTimestamp = 0;
170
Mathias Agopian148994e2012-09-19 17:31:36 -0700171 if (timestamp) {
172 // we have a vsync event to dispatch
173 event->header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
174 event->header.timestamp = timestamp;
175 event->vsync.count = vsyncCount;
176 } else {
177 eventPending = !mPendingEvents.isEmpty();
178 if (eventPending) {
179 // we have some other event to dispatch
180 *event = mPendingEvents[0];
181 mPendingEvents.removeAt(0);
182 }
183 }
184
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700185 // find out connections waiting for events
186 size_t count = mDisplayEventConnections.size();
187 for (size_t i=0 ; i<count ; i++) {
188 sp<Connection> connection(mDisplayEventConnections[i].promote());
189 if (connection != NULL) {
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700190 bool added = false;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700191 if (connection->count >= 0) {
192 // we need vsync events because at least
193 // one connection is waiting for it
194 waitForVSync = true;
195 if (timestamp) {
196 // we consume the event only if it's time
197 // (ie: we received a vsync event)
198 if (connection->count == 0) {
199 // fired this time around
200 connection->count = -1;
201 signalConnections.add(connection);
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700202 added = true;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700203 } else if (connection->count == 1 ||
204 (vsyncCount % connection->count) == 0) {
205 // continuous event, and time to report it
206 signalConnections.add(connection);
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700207 added = true;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700208 }
209 }
210 }
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700211
212 if (eventPending && !timestamp && !added) {
213 // we don't have a vsync event to process
214 // (timestamp==0), but we have some pending
215 // messages.
216 signalConnections.add(connection);
217 }
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700218 } else {
219 // we couldn't promote this reference, the connection has
220 // died, so clean-up!
221 mDisplayEventConnections.removeAt(i);
222 --i; --count;
223 }
224 }
225
226 // Here we figure out if we need to enable or disable vsyncs
227 if (timestamp && !waitForVSync) {
228 // we received a VSYNC but we have no clients
229 // don't report it, and disable VSYNC events
230 disableVSyncLocked();
231 } else if (!timestamp && waitForVSync) {
Andy McFadden6bf552e2012-08-30 16:34:41 -0700232 // we have at least one client, so we want vsync enabled
233 // (TODO: this function is called right after we finish
234 // notifying clients of a vsync, so this call will be made
235 // at the vsync rate, e.g. 60fps. If we can accurately
236 // track the current state we could avoid making this call
237 // so often.)
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700238 enableVSyncLocked();
239 }
240
Andy McFadden6bf552e2012-08-30 16:34:41 -0700241 // note: !timestamp implies signalConnections.isEmpty(), because we
242 // don't populate signalConnections if there's no vsync pending
Mathias Agopian148994e2012-09-19 17:31:36 -0700243 if (!timestamp && !eventPending) {
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700244 // wait for something to happen
Andy McFadden6bf552e2012-08-30 16:34:41 -0700245 if (waitForVSync) {
246 // This is where we spend most of our time, waiting
247 // for vsync events and new client registrations.
248 //
249 // If the screen is off, we can't use h/w vsync, so we
250 // use a 16ms timeout instead. It doesn't need to be
251 // precise, we just need to keep feeding our clients.
252 //
253 // We don't want to stall if there's a driver bug, so we
254 // use a (long) timeout when waiting for h/w vsync, and
255 // generate fake events when necessary.
256 bool softwareSync = mUseSoftwareVSync;
257 nsecs_t timeout = softwareSync ? ms2ns(16) : ms2ns(1000);
258 if (mCondition.waitRelative(mLock, timeout) == TIMED_OUT) {
259 if (!softwareSync) {
260 ALOGW("Timed out waiting for hw vsync; faking it");
261 }
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700262 mVSyncTimestamp = systemTime(SYSTEM_TIME_MONOTONIC);
263 mVSyncCount++;
264 }
265 } else {
Andy McFadden6bf552e2012-08-30 16:34:41 -0700266 // Nobody is interested in vsync, so we just want to sleep.
267 // h/w vsync should be disabled, so this will wait until we
268 // get a new connection, or an existing connection becomes
269 // interested in receiving vsync again.
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700270 mCondition.wait(mLock);
271 }
272 }
273 } while (signalConnections.isEmpty());
274
275 // here we're guaranteed to have a timestamp and some connections to signal
Andy McFadden6bf552e2012-08-30 16:34:41 -0700276 // (The connections might have dropped out of mDisplayEventConnections
277 // while we were asleep, but we'll still have strong references to them.)
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700278 return signalConnections;
279}
280
Mathias Agopian22ffb112012-04-10 21:04:02 -0700281void EventThread::enableVSyncLocked() {
282 if (!mUseSoftwareVSync) {
283 // never enable h/w VSYNC when screen is off
Mathias Agopian86303202012-07-24 22:46:10 -0700284 mFlinger->eventControl(SurfaceFlinger::EVENT_VSYNC, true);
285 mPowerHAL.vsyncHint(true);
Mathias Agopian22ffb112012-04-10 21:04:02 -0700286 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700287 mDebugVsyncEnabled = true;
288}
289
Mathias Agopian22ffb112012-04-10 21:04:02 -0700290void EventThread::disableVSyncLocked() {
Mathias Agopian86303202012-07-24 22:46:10 -0700291 mFlinger->eventControl(SurfaceFlinger::EVENT_VSYNC, false);
292 mPowerHAL.vsyncHint(false);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700293 mDebugVsyncEnabled = false;
294}
295
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800296void EventThread::dump(String8& result, char* buffer, size_t SIZE) const {
297 Mutex::Autolock _l(mLock);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700298 result.appendFormat("VSYNC state: %s\n",
299 mDebugVsyncEnabled?"enabled":"disabled");
Mathias Agopian22ffb112012-04-10 21:04:02 -0700300 result.appendFormat(" soft-vsync: %s\n",
301 mUseSoftwareVSync?"enabled":"disabled");
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700302 result.appendFormat(" numListeners=%u,\n events-delivered: %u\n",
Mathias Agopian10125f02012-08-17 15:06:02 -0700303 mDisplayEventConnections.size(), mVSyncCount);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700304 for (size_t i=0 ; i<mDisplayEventConnections.size() ; i++) {
305 sp<Connection> connection =
306 mDisplayEventConnections.itemAt(i).promote();
307 result.appendFormat(" %p: count=%d\n",
308 connection.get(), connection!=NULL ? connection->count : 0);
309 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800310}
311
312// ---------------------------------------------------------------------------
313
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700314EventThread::Connection::Connection(
315 const sp<EventThread>& eventThread)
316 : count(-1), mEventThread(eventThread), mChannel(new BitTube())
317{
318}
319
320EventThread::Connection::~Connection() {
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700321 // do nothing here -- clean-up will happen automatically
322 // when the main thread wakes up
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700323}
324
325void EventThread::Connection::onFirstRef() {
326 // NOTE: mEventThread doesn't hold a strong reference on us
327 mEventThread->registerDisplayEventConnection(this);
328}
329
330sp<BitTube> EventThread::Connection::getDataChannel() const {
331 return mChannel;
332}
333
334void EventThread::Connection::setVsyncRate(uint32_t count) {
335 mEventThread->setVsyncRate(count, this);
336}
337
338void EventThread::Connection::requestNextVsync() {
339 mEventThread->requestNextVsync(this);
340}
341
342status_t EventThread::Connection::postEvent(
343 const DisplayEventReceiver::Event& event) {
344 ssize_t size = DisplayEventReceiver::sendEvents(mChannel, &event, 1);
345 return size < 0 ? status_t(size) : status_t(NO_ERROR);
346}
347
348// ---------------------------------------------------------------------------
349
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800350}; // namespace android