blob: d86c28090fa733181da08fe383e86e0b19a71a84 [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 Agopiancb9732a2012-04-03 17:48:03 -070022#include <gui/BitTube.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080023#include <gui/IDisplayEventConnection.h>
24#include <gui/DisplayEventReceiver.h>
25
26#include <utils/Errors.h>
Mathias Agopian841cde52012-03-01 15:44:37 -080027#include <utils/Trace.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080028
29#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080030#include "EventThread.h"
31#include "SurfaceFlinger.h"
32
33// ---------------------------------------------------------------------------
34
35namespace android {
36
37// ---------------------------------------------------------------------------
38
39EventThread::EventThread(const sp<SurfaceFlinger>& flinger)
40 : mFlinger(flinger),
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070041 mHw(flinger->graphicPlane(0).editDisplayHardware()),
Mathias Agopian8aedd472012-01-24 16:39:14 -080042 mLastVSyncTimestamp(0),
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070043 mVSyncTimestamp(0),
Mathias Agopian22ffb112012-04-10 21:04:02 -070044 mUseSoftwareVSync(false),
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -070045 mDeliveredEvents(0),
46 mDebugVsyncEnabled(false)
Mathias Agopiand0566bc2011-11-17 17:49:17 -080047{
48}
49
50void EventThread::onFirstRef() {
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070051 mHw.setVSyncHandler(this);
Mathias Agopiand0566bc2011-11-17 17:49:17 -080052 run("EventThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
53}
54
Mathias Agopiancb9732a2012-04-03 17:48:03 -070055sp<EventThread::Connection> EventThread::createEventConnection() const {
56 return new Connection(const_cast<EventThread*>(this));
Mathias Agopian8aedd472012-01-24 16:39:14 -080057}
58
Mathias Agopiand0566bc2011-11-17 17:49:17 -080059status_t EventThread::registerDisplayEventConnection(
Mathias Agopiancb9732a2012-04-03 17:48:03 -070060 const sp<EventThread::Connection>& connection) {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080061 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070062 mDisplayEventConnections.add(connection);
Mathias Agopiand0566bc2011-11-17 17:49:17 -080063 mCondition.signal();
64 return NO_ERROR;
65}
66
67status_t EventThread::unregisterDisplayEventConnection(
Mathias Agopiancb9732a2012-04-03 17:48:03 -070068 const wp<EventThread::Connection>& connection) {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080069 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070070 mDisplayEventConnections.remove(connection);
Mathias Agopiand0566bc2011-11-17 17:49:17 -080071 mCondition.signal();
72 return NO_ERROR;
73}
74
Mathias Agopian478ae5e2011-12-06 17:22:19 -080075void EventThread::removeDisplayEventConnection(
Mathias Agopiancb9732a2012-04-03 17:48:03 -070076 const wp<EventThread::Connection>& connection) {
Mathias Agopian23748662011-12-05 14:33:34 -080077 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070078 mDisplayEventConnections.remove(connection);
Mathias Agopian478ae5e2011-12-06 17:22:19 -080079}
80
81void EventThread::setVsyncRate(uint32_t count,
Mathias Agopiancb9732a2012-04-03 17:48:03 -070082 const sp<EventThread::Connection>& connection) {
Mathias Agopian478ae5e2011-12-06 17:22:19 -080083 if (int32_t(count) >= 0) { // server must protect against bad params
84 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070085 const int32_t new_count = (count == 0) ? -1 : count;
86 if (connection->count != new_count) {
87 connection->count = new_count;
88 mCondition.signal();
Mathias Agopian478ae5e2011-12-06 17:22:19 -080089 }
90 }
91}
92
93void EventThread::requestNextVsync(
Mathias Agopiancb9732a2012-04-03 17:48:03 -070094 const sp<EventThread::Connection>& connection) {
Mathias Agopian478ae5e2011-12-06 17:22:19 -080095 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070096 if (connection->count < 0) {
97 connection->count = 0;
Mathias Agopian478ae5e2011-12-06 17:22:19 -080098 mCondition.signal();
99 }
Mathias Agopian23748662011-12-05 14:33:34 -0800100}
101
Mathias Agopian22ffb112012-04-10 21:04:02 -0700102void EventThread::onScreenReleased() {
103 Mutex::Autolock _l(mLock);
104 // wait for an eventual pending vsync to be serviced
105 if (!mUseSoftwareVSync) {
106 while (mVSyncTimestamp) {
107 mCondition.wait(mLock);
108 }
109 }
110 // disable reliance on h/w vsync
111 mUseSoftwareVSync = true;
112}
113
114void EventThread::onScreenAcquired() {
115 Mutex::Autolock _l(mLock);
116 mUseSoftwareVSync = false;
117}
118
119
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700120void EventThread::onVSyncReceived(int, nsecs_t timestamp) {
121 Mutex::Autolock _l(mLock);
122 mVSyncTimestamp = timestamp;
123 mCondition.signal();
124}
125
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800126bool EventThread::threadLoop() {
127
128 nsecs_t timestamp;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800129 DisplayEventReceiver::Event vsync;
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700130 Vector< wp<EventThread::Connection> > displayEventConnections;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800131
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700132 do {
Mathias Agopian23748662011-12-05 14:33:34 -0800133 Mutex::Autolock _l(mLock);
134 do {
Mathias Agopiand94d3b82012-04-08 15:01:31 -0700135 // latch VSYNC event if any
136 timestamp = mVSyncTimestamp;
137 mVSyncTimestamp = 0;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800138
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700139 // check if we should be waiting for VSYNC events
140 bool waitForNextVsync = false;
141 size_t count = mDisplayEventConnections.size();
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800142 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700143 sp<Connection> connection =
144 mDisplayEventConnections.itemAt(i).promote();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700145 if (connection!=0 && connection->count >= 0) {
146 // at least one continuous mode or active one-shot event
147 waitForNextVsync = true;
148 break;
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800149 }
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800150 }
Mathias Agopian23748662011-12-05 14:33:34 -0800151
Mathias Agopiand94d3b82012-04-08 15:01:31 -0700152 if (timestamp) {
153 if (!waitForNextVsync) {
154 // we received a VSYNC but we have no clients
155 // don't report it, and disable VSYNC events
Mathias Agopian22ffb112012-04-10 21:04:02 -0700156 disableVSyncLocked();
Mathias Agopiand94d3b82012-04-08 15:01:31 -0700157 } else {
158 // report VSYNC event
159 break;
160 }
161 } else {
162 // never disable VSYNC events immediately, instead
163 // we'll wait to receive the event and we'll
164 // reevaluate whether we need to dispatch it and/or
165 // disable VSYNC events then.
166 if (waitForNextVsync) {
167 // enable
Mathias Agopian22ffb112012-04-10 21:04:02 -0700168 enableVSyncLocked();
Mathias Agopiand94d3b82012-04-08 15:01:31 -0700169 }
170 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700171
172 // wait for something to happen
Mathias Agopian22ffb112012-04-10 21:04:02 -0700173 if (mUseSoftwareVSync == true) {
174 // h/w vsync cannot be used (screen is off), so we use
175 // a timeout instead. it doesn't matter how imprecise this
176 // is, we just need to make sure to serve the clients
177 if (mCondition.waitRelative(mLock, ms2ns(16)) == TIMED_OUT) {
178 mVSyncTimestamp = systemTime(SYSTEM_TIME_MONOTONIC);
179 }
180 } else {
181 mCondition.wait(mLock);
182 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700183 } while(true);
184
185 // process vsync event
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700186 mDeliveredEvents++;
187 mLastVSyncTimestamp = timestamp;
188
189 // now see if we still need to report this VSYNC event
190 const size_t count = mDisplayEventConnections.size();
191 for (size_t i=0 ; i<count ; i++) {
192 bool reportVsync = false;
193 sp<Connection> connection =
194 mDisplayEventConnections.itemAt(i).promote();
195 if (connection == 0)
196 continue;
197
198 const int32_t count = connection->count;
199 if (count >= 1) {
200 if (count==1 || (mDeliveredEvents % count) == 0) {
201 // continuous event, and time to report it
202 reportVsync = true;
203 }
204 } else if (count >= -1) {
205 if (count == 0) {
206 // fired this time around
207 reportVsync = true;
208 }
209 connection->count--;
210 }
211 if (reportVsync) {
212 displayEventConnections.add(connection);
213 }
214 }
215 } while (!displayEventConnections.size());
216
217 // dispatch vsync events to listeners...
218 vsync.header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
219 vsync.header.timestamp = timestamp;
220 vsync.vsync.count = mDeliveredEvents;
Mathias Agopian23748662011-12-05 14:33:34 -0800221
222 const size_t count = displayEventConnections.size();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800223 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700224 sp<Connection> conn(displayEventConnections[i].promote());
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800225 // make sure the connection didn't die
226 if (conn != NULL) {
227 status_t err = conn->postEvent(vsync);
228 if (err == -EAGAIN || err == -EWOULDBLOCK) {
229 // The destination doesn't accept events anymore, it's probably
230 // full. For now, we just drop the events on the floor.
231 // Note that some events cannot be dropped and would have to be
232 // re-sent later. Right-now we don't have the ability to do
233 // this, but it doesn't matter for VSYNC.
234 } else if (err < 0) {
235 // handle any other error on the pipe as fatal. the only
236 // reasonable thing to do is to clean-up this connection.
237 // The most common error we'll get here is -EPIPE.
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800238 removeDisplayEventConnection(displayEventConnections[i]);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800239 }
Mathias Agopian23748662011-12-05 14:33:34 -0800240 } else {
241 // somehow the connection is dead, but we still have it in our list
242 // just clean the list.
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800243 removeDisplayEventConnection(displayEventConnections[i]);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800244 }
245 }
246
Mathias Agopian23748662011-12-05 14:33:34 -0800247 // clear all our references without holding mLock
248 displayEventConnections.clear();
249
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800250 return true;
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
256 mHw.getHwComposer().eventControl(HWComposer::EVENT_VSYNC, true);
257 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700258 mDebugVsyncEnabled = true;
259}
260
Mathias Agopian22ffb112012-04-10 21:04:02 -0700261void EventThread::disableVSyncLocked() {
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700262 mHw.getHwComposer().eventControl(HWComposer::EVENT_VSYNC, false);
263 mDebugVsyncEnabled = false;
264}
265
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800266status_t EventThread::readyToRun() {
Steve Blocka19954a2012-01-04 20:05:49 +0000267 ALOGI("EventThread ready to run.");
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800268 return NO_ERROR;
269}
270
271void EventThread::dump(String8& result, char* buffer, size_t SIZE) const {
272 Mutex::Autolock _l(mLock);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700273 result.appendFormat("VSYNC state: %s\n",
274 mDebugVsyncEnabled?"enabled":"disabled");
Mathias Agopian22ffb112012-04-10 21:04:02 -0700275 result.appendFormat(" soft-vsync: %s\n",
276 mUseSoftwareVSync?"enabled":"disabled");
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700277 result.appendFormat(" numListeners=%u,\n events-delivered: %u\n",
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800278 mDisplayEventConnections.size(), mDeliveredEvents);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700279 for (size_t i=0 ; i<mDisplayEventConnections.size() ; i++) {
280 sp<Connection> connection =
281 mDisplayEventConnections.itemAt(i).promote();
282 result.appendFormat(" %p: count=%d\n",
283 connection.get(), connection!=NULL ? connection->count : 0);
284 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800285}
286
287// ---------------------------------------------------------------------------
288
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700289EventThread::Connection::Connection(
290 const sp<EventThread>& eventThread)
291 : count(-1), mEventThread(eventThread), mChannel(new BitTube())
292{
293}
294
295EventThread::Connection::~Connection() {
296 mEventThread->unregisterDisplayEventConnection(this);
297}
298
299void EventThread::Connection::onFirstRef() {
300 // NOTE: mEventThread doesn't hold a strong reference on us
301 mEventThread->registerDisplayEventConnection(this);
302}
303
304sp<BitTube> EventThread::Connection::getDataChannel() const {
305 return mChannel;
306}
307
308void EventThread::Connection::setVsyncRate(uint32_t count) {
309 mEventThread->setVsyncRate(count, this);
310}
311
312void EventThread::Connection::requestNextVsync() {
313 mEventThread->requestNextVsync(this);
314}
315
316status_t EventThread::Connection::postEvent(
317 const DisplayEventReceiver::Event& event) {
318 ssize_t size = DisplayEventReceiver::sendEvents(mChannel, &event, 1);
319 return size < 0 ? status_t(size) : status_t(NO_ERROR);
320}
321
322// ---------------------------------------------------------------------------
323
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800324}; // namespace android