2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
20 #include <sys/types.h>
22 #include <cutils/compiler.h>
24 #include <gui/BitTube.h>
25 #include <gui/IDisplayEventConnection.h>
26 #include <gui/DisplayEventReceiver.h>
28 #include <utils/Errors.h>
29 #include <utils/String8.h>
30 #include <utils/Trace.h>
32 #include "EventThread.h"
33 #include "SurfaceFlinger.h"
35 // ---------------------------------------------------------------------------
37 // ---------------------------------------------------------------------------
39 EventThread::EventThread(const sp<SurfaceFlinger>& flinger)
41 mUseSoftwareVSync(false),
42 mDebugVsyncEnabled(false) {
44 for (int32_t i=0 ; i<HWC_NUM_DISPLAY_TYPES ; i++) {
45 mVSyncEvent[i].header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
46 mVSyncEvent[i].header.id = 0;
47 mVSyncEvent[i].header.timestamp = 0;
48 mVSyncEvent[i].vsync.count = 0;
52 void EventThread::onFirstRef() {
53 run("EventThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
56 sp<EventThread::Connection> EventThread::createEventConnection() const {
57 return new Connection(const_cast<EventThread*>(this));
60 status_t EventThread::registerDisplayEventConnection(
61 const sp<EventThread::Connection>& connection) {
62 Mutex::Autolock _l(mLock);
63 mDisplayEventConnections.add(connection);
64 mCondition.broadcast();
68 void EventThread::removeDisplayEventConnection(
69 const wp<EventThread::Connection>& connection) {
70 Mutex::Autolock _l(mLock);
71 mDisplayEventConnections.remove(connection);
74 void EventThread::setVsyncRate(uint32_t count,
75 const sp<EventThread::Connection>& connection) {
76 if (int32_t(count) >= 0) { // server must protect against bad params
77 Mutex::Autolock _l(mLock);
78 const int32_t new_count = (count == 0) ? -1 : count;
79 if (connection->count != new_count) {
80 connection->count = new_count;
81 mCondition.broadcast();
86 void EventThread::requestNextVsync(
87 const sp<EventThread::Connection>& connection) {
88 Mutex::Autolock _l(mLock);
89 if (connection->count < 0) {
90 connection->count = 0;
91 mCondition.broadcast();
95 void EventThread::onScreenReleased() {
96 Mutex::Autolock _l(mLock);
97 if (!mUseSoftwareVSync) {
98 // disable reliance on h/w vsync
99 mUseSoftwareVSync = true;
100 mCondition.broadcast();
104 void EventThread::onScreenAcquired() {
105 Mutex::Autolock _l(mLock);
106 if (mUseSoftwareVSync) {
107 // resume use of h/w vsync
108 mUseSoftwareVSync = false;
109 mCondition.broadcast();
114 void EventThread::onVSyncReceived(int type, nsecs_t timestamp) {
115 ALOGE_IF(type >= HWC_NUM_DISPLAY_TYPES,
116 "received vsync event for an invalid display (id=%d)", type);
118 Mutex::Autolock _l(mLock);
119 if (type < HWC_NUM_DISPLAY_TYPES) {
120 mVSyncEvent[type].header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
121 mVSyncEvent[type].header.id = type;
122 mVSyncEvent[type].header.timestamp = timestamp;
123 mVSyncEvent[type].vsync.count++;
124 mCondition.broadcast();
128 void EventThread::onHotplugReceived(int type, bool connected) {
129 ALOGE_IF(type >= HWC_NUM_DISPLAY_TYPES,
130 "received hotplug event for an invalid display (id=%d)", type);
132 Mutex::Autolock _l(mLock);
133 if (type < HWC_NUM_DISPLAY_TYPES) {
134 DisplayEventReceiver::Event event;
135 event.header.type = DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG;
136 event.header.id = type;
137 event.header.timestamp = systemTime();
138 event.hotplug.connected = connected;
139 mPendingEvents.add(event);
140 mCondition.broadcast();
144 bool EventThread::threadLoop() {
145 DisplayEventReceiver::Event event;
146 Vector< sp<EventThread::Connection> > signalConnections;
147 signalConnections = waitForEvent(&event);
149 // dispatch events to listeners...
150 const size_t count = signalConnections.size();
151 for (size_t i=0 ; i<count ; i++) {
152 const sp<Connection>& conn(signalConnections[i]);
153 // now see if we still need to report this event
154 status_t err = conn->postEvent(event);
155 if (err == -EAGAIN || err == -EWOULDBLOCK) {
156 // The destination doesn't accept events anymore, it's probably
157 // full. For now, we just drop the events on the floor.
158 // FIXME: Note that some events cannot be dropped and would have
159 // to be re-sent later.
160 // Right-now we don't have the ability to do this.
161 ALOGW("EventThread: dropping event (%08x) for connection %p",
162 event.header.type, conn.get());
163 } else if (err < 0) {
164 // handle any other error on the pipe as fatal. the only
165 // reasonable thing to do is to clean-up this connection.
166 // The most common error we'll get here is -EPIPE.
167 removeDisplayEventConnection(signalConnections[i]);
173 // This will return when (1) a vsync event has been received, and (2) there was
174 // at least one connection interested in receiving it when we started waiting.
175 Vector< sp<EventThread::Connection> > EventThread::waitForEvent(
176 DisplayEventReceiver::Event* event)
178 Mutex::Autolock _l(mLock);
179 Vector< sp<EventThread::Connection> > signalConnections;
182 bool eventPending = false;
183 bool waitForVSync = false;
185 size_t vsyncCount = 0;
186 nsecs_t timestamp = 0;
187 for (int32_t i=0 ; i<HWC_NUM_DISPLAY_TYPES ; i++) {
188 timestamp = mVSyncEvent[i].header.timestamp;
190 // we have a vsync event to dispatch
191 *event = mVSyncEvent[i];
192 mVSyncEvent[i].header.timestamp = 0;
193 vsyncCount = mVSyncEvent[i].vsync.count;
199 // no vsync event, see if there are some other event
200 eventPending = !mPendingEvents.isEmpty();
202 // we have some other event to dispatch
203 *event = mPendingEvents[0];
204 mPendingEvents.removeAt(0);
208 // find out connections waiting for events
209 size_t count = mDisplayEventConnections.size();
210 for (size_t i=0 ; i<count ; i++) {
211 sp<Connection> connection(mDisplayEventConnections[i].promote());
212 if (connection != NULL) {
214 if (connection->count >= 0) {
215 // we need vsync events because at least
216 // one connection is waiting for it
219 // we consume the event only if it's time
220 // (ie: we received a vsync event)
221 if (connection->count == 0) {
222 // fired this time around
223 connection->count = -1;
224 signalConnections.add(connection);
226 } else if (connection->count == 1 ||
227 (vsyncCount % connection->count) == 0) {
228 // continuous event, and time to report it
229 signalConnections.add(connection);
235 if (eventPending && !timestamp && !added) {
236 // we don't have a vsync event to process
237 // (timestamp==0), but we have some pending
239 signalConnections.add(connection);
242 // we couldn't promote this reference, the connection has
243 // died, so clean-up!
244 mDisplayEventConnections.removeAt(i);
249 // Here we figure out if we need to enable or disable vsyncs
250 if (timestamp && !waitForVSync) {
251 // we received a VSYNC but we have no clients
252 // don't report it, and disable VSYNC events
253 disableVSyncLocked();
254 } else if (!timestamp && waitForVSync) {
255 // we have at least one client, so we want vsync enabled
256 // (TODO: this function is called right after we finish
257 // notifying clients of a vsync, so this call will be made
258 // at the vsync rate, e.g. 60fps. If we can accurately
259 // track the current state we could avoid making this call
264 // note: !timestamp implies signalConnections.isEmpty(), because we
265 // don't populate signalConnections if there's no vsync pending
266 if (!timestamp && !eventPending) {
267 // wait for something to happen
269 // This is where we spend most of our time, waiting
270 // for vsync events and new client registrations.
272 // If the screen is off, we can't use h/w vsync, so we
273 // use a 16ms timeout instead. It doesn't need to be
274 // precise, we just need to keep feeding our clients.
276 // We don't want to stall if there's a driver bug, so we
277 // use a (long) timeout when waiting for h/w vsync, and
278 // generate fake events when necessary.
279 bool softwareSync = mUseSoftwareVSync;
280 nsecs_t timeout = softwareSync ? ms2ns(16) : ms2ns(1000);
281 if (mCondition.waitRelative(mLock, timeout) == TIMED_OUT) {
283 ALOGW("Timed out waiting for hw vsync; faking it");
285 // FIXME: how do we decide which display id the fake
287 mVSyncEvent[0].header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
288 mVSyncEvent[0].header.id = HWC_DISPLAY_PRIMARY;
289 mVSyncEvent[0].header.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
290 mVSyncEvent[0].vsync.count++;
293 // Nobody is interested in vsync, so we just want to sleep.
294 // h/w vsync should be disabled, so this will wait until we
295 // get a new connection, or an existing connection becomes
296 // interested in receiving vsync again.
297 mCondition.wait(mLock);
300 } while (signalConnections.isEmpty());
302 // here we're guaranteed to have a timestamp and some connections to signal
303 // (The connections might have dropped out of mDisplayEventConnections
304 // while we were asleep, but we'll still have strong references to them.)
305 return signalConnections;
308 void EventThread::enableVSyncLocked() {
309 if (!mUseSoftwareVSync) {
310 // never enable h/w VSYNC when screen is off
311 mFlinger->eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
312 mPowerHAL.vsyncHint(true);
314 mDebugVsyncEnabled = true;
317 void EventThread::disableVSyncLocked() {
318 mFlinger->eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
319 mPowerHAL.vsyncHint(false);
320 mDebugVsyncEnabled = false;
323 void EventThread::dump(String8& result) const {
324 Mutex::Autolock _l(mLock);
325 result.appendFormat("VSYNC state: %s\n",
326 mDebugVsyncEnabled?"enabled":"disabled");
327 result.appendFormat(" soft-vsync: %s\n",
328 mUseSoftwareVSync?"enabled":"disabled");
329 result.appendFormat(" numListeners=%u,\n events-delivered: %u\n",
330 mDisplayEventConnections.size(),
331 mVSyncEvent[HWC_DISPLAY_PRIMARY].vsync.count);
332 for (size_t i=0 ; i<mDisplayEventConnections.size() ; i++) {
333 sp<Connection> connection =
334 mDisplayEventConnections.itemAt(i).promote();
335 result.appendFormat(" %p: count=%d\n",
336 connection.get(), connection!=NULL ? connection->count : 0);
340 // ---------------------------------------------------------------------------
342 EventThread::Connection::Connection(
343 const sp<EventThread>& eventThread)
344 : count(-1), mEventThread(eventThread), mChannel(new BitTube())
348 EventThread::Connection::~Connection() {
349 // do nothing here -- clean-up will happen automatically
350 // when the main thread wakes up
353 void EventThread::Connection::onFirstRef() {
354 // NOTE: mEventThread doesn't hold a strong reference on us
355 mEventThread->registerDisplayEventConnection(this);
358 sp<BitTube> EventThread::Connection::getDataChannel() const {
362 void EventThread::Connection::setVsyncRate(uint32_t count) {
363 mEventThread->setVsyncRate(count, this);
366 void EventThread::Connection::requestNextVsync() {
367 mEventThread->requestNextVsync(this);
370 status_t EventThread::Connection::postEvent(
371 const DisplayEventReceiver::Event& event) {
372 ssize_t size = DisplayEventReceiver::sendEvents(mChannel, &event, 1);
373 return size < 0 ? status_t(size) : status_t(NO_ERROR);
376 // ---------------------------------------------------------------------------
378 }; // namespace android