blob: 7c1aebe0b8ae381fa3c5a26a176920c1be528346 [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 Agopian7d886472012-06-14 23:39:35 -070063 mCondition.broadcast();
Mathias Agopiand0566bc2011-11-17 17:49:17 -080064 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 Agopian7d886472012-06-14 23:39:35 -070071 mCondition.broadcast();
Mathias Agopiand0566bc2011-11-17 17:49:17 -080072 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;
Mathias Agopian7d886472012-06-14 23:39:35 -070088 mCondition.broadcast();
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 Agopian7d886472012-06-14 23:39:35 -070098 mCondition.broadcast();
Mathias Agopian478ae5e2011-12-06 17:22:19 -080099 }
Mathias Agopian23748662011-12-05 14:33:34 -0800100}
101
Mathias Agopian22ffb112012-04-10 21:04:02 -0700102void EventThread::onScreenReleased() {
103 Mutex::Autolock _l(mLock);
Mathias Agopian22ffb112012-04-10 21:04:02 -0700104 if (!mUseSoftwareVSync) {
Mathias Agopian7d886472012-06-14 23:39:35 -0700105 // disable reliance on h/w vsync
106 mUseSoftwareVSync = true;
107 mCondition.broadcast();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700108 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700109}
110
111void EventThread::onScreenAcquired() {
112 Mutex::Autolock _l(mLock);
Mathias Agopian7d886472012-06-14 23:39:35 -0700113 if (mUseSoftwareVSync) {
114 // resume use of h/w vsync
115 mUseSoftwareVSync = false;
116 mCondition.broadcast();
117 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700118}
119
120
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700121void EventThread::onVSyncReceived(int, nsecs_t timestamp) {
122 Mutex::Autolock _l(mLock);
123 mVSyncTimestamp = timestamp;
Mathias Agopian7d886472012-06-14 23:39:35 -0700124 mCondition.broadcast();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700125}
126
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800127bool EventThread::threadLoop() {
128
129 nsecs_t timestamp;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800130 DisplayEventReceiver::Event vsync;
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700131 Vector< wp<EventThread::Connection> > displayEventConnections;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800132
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700133 do {
Mathias Agopian23748662011-12-05 14:33:34 -0800134 Mutex::Autolock _l(mLock);
135 do {
Mathias Agopiand94d3b82012-04-08 15:01:31 -0700136 // latch VSYNC event if any
137 timestamp = mVSyncTimestamp;
138 mVSyncTimestamp = 0;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800139
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700140 // check if we should be waiting for VSYNC events
141 bool waitForNextVsync = false;
142 size_t count = mDisplayEventConnections.size();
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800143 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700144 sp<Connection> connection =
145 mDisplayEventConnections.itemAt(i).promote();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700146 if (connection!=0 && connection->count >= 0) {
147 // at least one continuous mode or active one-shot event
148 waitForNextVsync = true;
149 break;
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800150 }
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800151 }
Mathias Agopian23748662011-12-05 14:33:34 -0800152
Mathias Agopiand94d3b82012-04-08 15:01:31 -0700153 if (timestamp) {
154 if (!waitForNextVsync) {
155 // we received a VSYNC but we have no clients
156 // don't report it, and disable VSYNC events
Mathias Agopian22ffb112012-04-10 21:04:02 -0700157 disableVSyncLocked();
Mathias Agopiand94d3b82012-04-08 15:01:31 -0700158 } else {
159 // report VSYNC event
160 break;
161 }
162 } else {
163 // never disable VSYNC events immediately, instead
164 // we'll wait to receive the event and we'll
165 // reevaluate whether we need to dispatch it and/or
166 // disable VSYNC events then.
167 if (waitForNextVsync) {
168 // enable
Mathias Agopian22ffb112012-04-10 21:04:02 -0700169 enableVSyncLocked();
Mathias Agopiand94d3b82012-04-08 15:01:31 -0700170 }
171 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700172
173 // wait for something to happen
Mathias Agopianfca660c2012-04-12 20:43:16 -0700174 if (mUseSoftwareVSync && waitForNextVsync) {
Mathias Agopian22ffb112012-04-10 21:04:02 -0700175 // h/w vsync cannot be used (screen is off), so we use
176 // a timeout instead. it doesn't matter how imprecise this
177 // is, we just need to make sure to serve the clients
178 if (mCondition.waitRelative(mLock, ms2ns(16)) == TIMED_OUT) {
179 mVSyncTimestamp = systemTime(SYSTEM_TIME_MONOTONIC);
180 }
181 } else {
182 mCondition.wait(mLock);
183 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700184 } while(true);
185
186 // process vsync event
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700187 mDeliveredEvents++;
188 mLastVSyncTimestamp = timestamp;
189
190 // now see if we still need to report this VSYNC event
191 const size_t count = mDisplayEventConnections.size();
192 for (size_t i=0 ; i<count ; i++) {
193 bool reportVsync = false;
194 sp<Connection> connection =
195 mDisplayEventConnections.itemAt(i).promote();
196 if (connection == 0)
197 continue;
198
199 const int32_t count = connection->count;
200 if (count >= 1) {
201 if (count==1 || (mDeliveredEvents % count) == 0) {
202 // continuous event, and time to report it
203 reportVsync = true;
204 }
205 } else if (count >= -1) {
206 if (count == 0) {
207 // fired this time around
208 reportVsync = true;
209 }
210 connection->count--;
211 }
212 if (reportVsync) {
213 displayEventConnections.add(connection);
214 }
215 }
216 } while (!displayEventConnections.size());
217
218 // dispatch vsync events to listeners...
219 vsync.header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
220 vsync.header.timestamp = timestamp;
221 vsync.vsync.count = mDeliveredEvents;
Mathias Agopian23748662011-12-05 14:33:34 -0800222
223 const size_t count = displayEventConnections.size();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800224 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700225 sp<Connection> conn(displayEventConnections[i].promote());
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800226 // make sure the connection didn't die
227 if (conn != NULL) {
228 status_t err = conn->postEvent(vsync);
229 if (err == -EAGAIN || err == -EWOULDBLOCK) {
230 // The destination doesn't accept events anymore, it's probably
231 // full. For now, we just drop the events on the floor.
232 // Note that some events cannot be dropped and would have to be
233 // re-sent later. Right-now we don't have the ability to do
234 // this, but it doesn't matter for VSYNC.
235 } else if (err < 0) {
236 // handle any other error on the pipe as fatal. the only
237 // reasonable thing to do is to clean-up this connection.
238 // The most common error we'll get here is -EPIPE.
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800239 removeDisplayEventConnection(displayEventConnections[i]);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800240 }
Mathias Agopian23748662011-12-05 14:33:34 -0800241 } else {
242 // somehow the connection is dead, but we still have it in our list
243 // just clean the list.
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800244 removeDisplayEventConnection(displayEventConnections[i]);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800245 }
246 }
247
Mathias Agopian23748662011-12-05 14:33:34 -0800248 // clear all our references without holding mLock
249 displayEventConnections.clear();
250
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800251 return true;
252}
253
Mathias Agopian22ffb112012-04-10 21:04:02 -0700254void EventThread::enableVSyncLocked() {
255 if (!mUseSoftwareVSync) {
256 // never enable h/w VSYNC when screen is off
Mathias Agopian03e40722012-04-26 16:11:59 -0700257 mHw.eventControl(DisplayHardware::EVENT_VSYNC, 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 Agopian03e40722012-04-26 16:11:59 -0700263 mHw.eventControl(DisplayHardware::EVENT_VSYNC, false);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700264 mDebugVsyncEnabled = false;
265}
266
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800267status_t EventThread::readyToRun() {
Steve Blocka19954a2012-01-04 20:05:49 +0000268 ALOGI("EventThread ready to run.");
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800269 return NO_ERROR;
270}
271
272void EventThread::dump(String8& result, char* buffer, size_t SIZE) const {
273 Mutex::Autolock _l(mLock);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700274 result.appendFormat("VSYNC state: %s\n",
275 mDebugVsyncEnabled?"enabled":"disabled");
Mathias Agopian22ffb112012-04-10 21:04:02 -0700276 result.appendFormat(" soft-vsync: %s\n",
277 mUseSoftwareVSync?"enabled":"disabled");
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700278 result.appendFormat(" numListeners=%u,\n events-delivered: %u\n",
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800279 mDisplayEventConnections.size(), mDeliveredEvents);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700280 for (size_t i=0 ; i<mDisplayEventConnections.size() ; i++) {
281 sp<Connection> connection =
282 mDisplayEventConnections.itemAt(i).promote();
283 result.appendFormat(" %p: count=%d\n",
284 connection.get(), connection!=NULL ? connection->count : 0);
285 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800286}
287
288// ---------------------------------------------------------------------------
289
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700290EventThread::Connection::Connection(
291 const sp<EventThread>& eventThread)
292 : count(-1), mEventThread(eventThread), mChannel(new BitTube())
293{
294}
295
296EventThread::Connection::~Connection() {
297 mEventThread->unregisterDisplayEventConnection(this);
298}
299
300void EventThread::Connection::onFirstRef() {
301 // NOTE: mEventThread doesn't hold a strong reference on us
302 mEventThread->registerDisplayEventConnection(this);
303}
304
305sp<BitTube> EventThread::Connection::getDataChannel() const {
306 return mChannel;
307}
308
309void EventThread::Connection::setVsyncRate(uint32_t count) {
310 mEventThread->setVsyncRate(count, this);
311}
312
313void EventThread::Connection::requestNextVsync() {
314 mEventThread->requestNextVsync(this);
315}
316
317status_t EventThread::Connection::postEvent(
318 const DisplayEventReceiver::Event& event) {
319 ssize_t size = DisplayEventReceiver::sendEvents(mChannel, &event, 1);
320 return size < 0 ? status_t(size) : status_t(NO_ERROR);
321}
322
323// ---------------------------------------------------------------------------
324
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800325}; // namespace android