blob: ade9f751f086e54dc2d2c42f157d5216f58d533a [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 Agopian3ee454a2012-08-27 16:28:24 -0700109void EventThread::onVSyncReceived(const wp<IBinder>&, 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 Agopiand0566bc2011-11-17 17:49:17 -0800116bool EventThread::threadLoop() {
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800117 DisplayEventReceiver::Event vsync;
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700118 Vector< sp<EventThread::Connection> > signalConnections;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700119 signalConnections = waitForEvent(&vsync);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700120
121 // dispatch vsync events to listeners...
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700122 const size_t count = signalConnections.size();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800123 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700124 const sp<Connection>& conn(signalConnections[i]);
Mathias Agopian10125f02012-08-17 15:06:02 -0700125 // now see if we still need to report this VSYNC event
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700126 status_t err = conn->postEvent(vsync);
127 if (err == -EAGAIN || err == -EWOULDBLOCK) {
128 // The destination doesn't accept events anymore, it's probably
129 // full. For now, we just drop the events on the floor.
130 // Note that some events cannot be dropped and would have to be
131 // re-sent later. Right-now we don't have the ability to do
132 // this, but it doesn't matter for VSYNC.
133 } else if (err < 0) {
134 // handle any other error on the pipe as fatal. the only
135 // reasonable thing to do is to clean-up this connection.
136 // The most common error we'll get here is -EPIPE.
137 removeDisplayEventConnection(signalConnections[i]);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800138 }
139 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800140 return true;
141}
142
Andy McFadden6bf552e2012-08-30 16:34:41 -0700143// This will return when (1) a vsync event has been received, and (2) there was
144// at least one connection interested in receiving it when we started waiting.
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700145Vector< sp<EventThread::Connection> > EventThread::waitForEvent(
146 DisplayEventReceiver::Event* event)
147{
148 Mutex::Autolock _l(mLock);
149
150 size_t vsyncCount;
151 nsecs_t timestamp;
152 Vector< sp<EventThread::Connection> > signalConnections;
153
154 do {
155 // latch VSYNC event if any
156 bool waitForVSync = false;
157 vsyncCount = mVSyncCount;
158 timestamp = mVSyncTimestamp;
159 mVSyncTimestamp = 0;
160
161 // find out connections waiting for events
162 size_t count = mDisplayEventConnections.size();
163 for (size_t i=0 ; i<count ; i++) {
164 sp<Connection> connection(mDisplayEventConnections[i].promote());
165 if (connection != NULL) {
166 if (connection->count >= 0) {
167 // we need vsync events because at least
168 // one connection is waiting for it
169 waitForVSync = true;
170 if (timestamp) {
171 // we consume the event only if it's time
172 // (ie: we received a vsync event)
173 if (connection->count == 0) {
174 // fired this time around
175 connection->count = -1;
176 signalConnections.add(connection);
177 } else if (connection->count == 1 ||
178 (vsyncCount % connection->count) == 0) {
179 // continuous event, and time to report it
180 signalConnections.add(connection);
181 }
182 }
183 }
184 } else {
185 // we couldn't promote this reference, the connection has
186 // died, so clean-up!
187 mDisplayEventConnections.removeAt(i);
188 --i; --count;
189 }
190 }
191
192 // Here we figure out if we need to enable or disable vsyncs
193 if (timestamp && !waitForVSync) {
194 // we received a VSYNC but we have no clients
195 // don't report it, and disable VSYNC events
196 disableVSyncLocked();
197 } else if (!timestamp && waitForVSync) {
Andy McFadden6bf552e2012-08-30 16:34:41 -0700198 // we have at least one client, so we want vsync enabled
199 // (TODO: this function is called right after we finish
200 // notifying clients of a vsync, so this call will be made
201 // at the vsync rate, e.g. 60fps. If we can accurately
202 // track the current state we could avoid making this call
203 // so often.)
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700204 enableVSyncLocked();
205 }
206
Andy McFadden6bf552e2012-08-30 16:34:41 -0700207 // note: !timestamp implies signalConnections.isEmpty(), because we
208 // don't populate signalConnections if there's no vsync pending
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700209 if (!timestamp) {
210 // wait for something to happen
Andy McFadden6bf552e2012-08-30 16:34:41 -0700211 if (waitForVSync) {
212 // This is where we spend most of our time, waiting
213 // for vsync events and new client registrations.
214 //
215 // If the screen is off, we can't use h/w vsync, so we
216 // use a 16ms timeout instead. It doesn't need to be
217 // precise, we just need to keep feeding our clients.
218 //
219 // We don't want to stall if there's a driver bug, so we
220 // use a (long) timeout when waiting for h/w vsync, and
221 // generate fake events when necessary.
222 bool softwareSync = mUseSoftwareVSync;
223 nsecs_t timeout = softwareSync ? ms2ns(16) : ms2ns(1000);
224 if (mCondition.waitRelative(mLock, timeout) == TIMED_OUT) {
225 if (!softwareSync) {
226 ALOGW("Timed out waiting for hw vsync; faking it");
227 }
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700228 mVSyncTimestamp = systemTime(SYSTEM_TIME_MONOTONIC);
229 mVSyncCount++;
230 }
231 } else {
Andy McFadden6bf552e2012-08-30 16:34:41 -0700232 // Nobody is interested in vsync, so we just want to sleep.
233 // h/w vsync should be disabled, so this will wait until we
234 // get a new connection, or an existing connection becomes
235 // interested in receiving vsync again.
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700236 mCondition.wait(mLock);
237 }
238 }
239 } while (signalConnections.isEmpty());
240
241 // here we're guaranteed to have a timestamp and some connections to signal
Andy McFadden6bf552e2012-08-30 16:34:41 -0700242 // (The connections might have dropped out of mDisplayEventConnections
243 // while we were asleep, but we'll still have strong references to them.)
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700244
Andy McFadden6bf552e2012-08-30 16:34:41 -0700245 // fill in vsync event info
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700246 event->header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
247 event->header.timestamp = timestamp;
248 event->vsync.count = vsyncCount;
249
250 return signalConnections;
251}
252
Mathias Agopian22ffb112012-04-10 21:04:02 -0700253void EventThread::enableVSyncLocked() {
254 if (!mUseSoftwareVSync) {
255 // never enable h/w VSYNC when screen is off
Mathias Agopian86303202012-07-24 22:46:10 -0700256 mFlinger->eventControl(SurfaceFlinger::EVENT_VSYNC, true);
257 mPowerHAL.vsyncHint(true);
Mathias Agopian22ffb112012-04-10 21:04:02 -0700258 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700259 mDebugVsyncEnabled = true;
260}
261
Mathias Agopian22ffb112012-04-10 21:04:02 -0700262void EventThread::disableVSyncLocked() {
Mathias Agopian86303202012-07-24 22:46:10 -0700263 mFlinger->eventControl(SurfaceFlinger::EVENT_VSYNC, false);
264 mPowerHAL.vsyncHint(false);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700265 mDebugVsyncEnabled = false;
266}
267
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800268status_t EventThread::readyToRun() {
Steve Blocka19954a2012-01-04 20:05:49 +0000269 ALOGI("EventThread ready to run.");
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800270 return NO_ERROR;
271}
272
273void EventThread::dump(String8& result, char* buffer, size_t SIZE) const {
274 Mutex::Autolock _l(mLock);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700275 result.appendFormat("VSYNC state: %s\n",
276 mDebugVsyncEnabled?"enabled":"disabled");
Mathias Agopian22ffb112012-04-10 21:04:02 -0700277 result.appendFormat(" soft-vsync: %s\n",
278 mUseSoftwareVSync?"enabled":"disabled");
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700279 result.appendFormat(" numListeners=%u,\n events-delivered: %u\n",
Mathias Agopian10125f02012-08-17 15:06:02 -0700280 mDisplayEventConnections.size(), mVSyncCount);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700281 for (size_t i=0 ; i<mDisplayEventConnections.size() ; i++) {
282 sp<Connection> connection =
283 mDisplayEventConnections.itemAt(i).promote();
284 result.appendFormat(" %p: count=%d\n",
285 connection.get(), connection!=NULL ? connection->count : 0);
286 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800287}
288
289// ---------------------------------------------------------------------------
290
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700291EventThread::Connection::Connection(
292 const sp<EventThread>& eventThread)
293 : count(-1), mEventThread(eventThread), mChannel(new BitTube())
294{
295}
296
297EventThread::Connection::~Connection() {
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700298 // do nothing here -- clean-up will happen automatically
299 // when the main thread wakes up
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700300}
301
302void EventThread::Connection::onFirstRef() {
303 // NOTE: mEventThread doesn't hold a strong reference on us
304 mEventThread->registerDisplayEventConnection(this);
305}
306
307sp<BitTube> EventThread::Connection::getDataChannel() const {
308 return mChannel;
309}
310
311void EventThread::Connection::setVsyncRate(uint32_t count) {
312 mEventThread->setVsyncRate(count, this);
313}
314
315void EventThread::Connection::requestNextVsync() {
316 mEventThread->requestNextVsync(this);
317}
318
319status_t EventThread::Connection::postEvent(
320 const DisplayEventReceiver::Event& event) {
321 ssize_t size = DisplayEventReceiver::sendEvents(mChannel, &event, 1);
322 return size < 0 ? status_t(size) : status_t(NO_ERROR);
323}
324
325// ---------------------------------------------------------------------------
326
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800327}; // namespace android