blob: 9b61fa9a69f5780c3fb5ebea8a7ecbad7b00a95f [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 Agopian921e6ac2012-07-23 23:11:29 -070027#include <utils/String8.h>
Mathias Agopian841cde52012-03-01 15:44:37 -080028#include <utils/Trace.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080029
Mathias Agopiand0566bc2011-11-17 17:49:17 -080030#include "EventThread.h"
31#include "SurfaceFlinger.h"
32
33// ---------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -080034namespace android {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080035// ---------------------------------------------------------------------------
36
37EventThread::EventThread(const sp<SurfaceFlinger>& flinger)
Mathias Agopian86303202012-07-24 22:46:10 -070038 : mFlinger(flinger),
Mathias Agopian8aedd472012-01-24 16:39:14 -080039 mLastVSyncTimestamp(0),
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070040 mVSyncTimestamp(0),
Mathias Agopian22ffb112012-04-10 21:04:02 -070041 mUseSoftwareVSync(false),
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -070042 mDeliveredEvents(0),
Mathias Agopian921e6ac2012-07-23 23:11:29 -070043 mDebugVsyncEnabled(false) {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080044}
45
46void EventThread::onFirstRef() {
47 run("EventThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
48}
49
Mathias Agopiancb9732a2012-04-03 17:48:03 -070050sp<EventThread::Connection> EventThread::createEventConnection() const {
51 return new Connection(const_cast<EventThread*>(this));
Mathias Agopian8aedd472012-01-24 16:39:14 -080052}
53
Mathias Agopiand0566bc2011-11-17 17:49:17 -080054status_t EventThread::registerDisplayEventConnection(
Mathias Agopiancb9732a2012-04-03 17:48:03 -070055 const sp<EventThread::Connection>& connection) {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080056 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070057 mDisplayEventConnections.add(connection);
Mathias Agopian7d886472012-06-14 23:39:35 -070058 mCondition.broadcast();
Mathias Agopiand0566bc2011-11-17 17:49:17 -080059 return NO_ERROR;
60}
61
62status_t EventThread::unregisterDisplayEventConnection(
Mathias Agopiancb9732a2012-04-03 17:48:03 -070063 const wp<EventThread::Connection>& connection) {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080064 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070065 mDisplayEventConnections.remove(connection);
Mathias Agopian7d886472012-06-14 23:39:35 -070066 mCondition.broadcast();
Mathias Agopiand0566bc2011-11-17 17:49:17 -080067 return NO_ERROR;
68}
69
Mathias Agopian478ae5e2011-12-06 17:22:19 -080070void EventThread::removeDisplayEventConnection(
Mathias Agopiancb9732a2012-04-03 17:48:03 -070071 const wp<EventThread::Connection>& connection) {
Mathias Agopian23748662011-12-05 14:33:34 -080072 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070073 mDisplayEventConnections.remove(connection);
Mathias Agopian478ae5e2011-12-06 17:22:19 -080074}
75
76void EventThread::setVsyncRate(uint32_t count,
Mathias Agopiancb9732a2012-04-03 17:48:03 -070077 const sp<EventThread::Connection>& connection) {
Mathias Agopian478ae5e2011-12-06 17:22:19 -080078 if (int32_t(count) >= 0) { // server must protect against bad params
79 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070080 const int32_t new_count = (count == 0) ? -1 : count;
81 if (connection->count != new_count) {
82 connection->count = new_count;
Mathias Agopian7d886472012-06-14 23:39:35 -070083 mCondition.broadcast();
Mathias Agopian478ae5e2011-12-06 17:22:19 -080084 }
85 }
86}
87
88void EventThread::requestNextVsync(
Mathias Agopiancb9732a2012-04-03 17:48:03 -070089 const sp<EventThread::Connection>& connection) {
Mathias Agopian478ae5e2011-12-06 17:22:19 -080090 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070091 if (connection->count < 0) {
92 connection->count = 0;
Mathias Agopian7d886472012-06-14 23:39:35 -070093 mCondition.broadcast();
Mathias Agopian478ae5e2011-12-06 17:22:19 -080094 }
Mathias Agopian23748662011-12-05 14:33:34 -080095}
96
Mathias Agopian22ffb112012-04-10 21:04:02 -070097void EventThread::onScreenReleased() {
98 Mutex::Autolock _l(mLock);
Mathias Agopian22ffb112012-04-10 21:04:02 -070099 if (!mUseSoftwareVSync) {
Mathias Agopian7d886472012-06-14 23:39:35 -0700100 // disable reliance on h/w vsync
101 mUseSoftwareVSync = true;
102 mCondition.broadcast();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700103 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700104}
105
106void EventThread::onScreenAcquired() {
107 Mutex::Autolock _l(mLock);
Mathias Agopian7d886472012-06-14 23:39:35 -0700108 if (mUseSoftwareVSync) {
109 // resume use of h/w vsync
110 mUseSoftwareVSync = false;
111 mCondition.broadcast();
112 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700113}
114
115
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700116void EventThread::onVSyncReceived(int, nsecs_t timestamp) {
117 Mutex::Autolock _l(mLock);
118 mVSyncTimestamp = timestamp;
Mathias Agopian7d886472012-06-14 23:39:35 -0700119 mCondition.broadcast();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700120}
121
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800122bool EventThread::threadLoop() {
123
124 nsecs_t timestamp;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800125 DisplayEventReceiver::Event vsync;
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700126 Vector< wp<EventThread::Connection> > displayEventConnections;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800127
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700128 do {
Mathias Agopian23748662011-12-05 14:33:34 -0800129 Mutex::Autolock _l(mLock);
130 do {
Mathias Agopiand94d3b82012-04-08 15:01:31 -0700131 // latch VSYNC event if any
132 timestamp = mVSyncTimestamp;
133 mVSyncTimestamp = 0;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800134
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700135 // check if we should be waiting for VSYNC events
136 bool waitForNextVsync = false;
137 size_t count = mDisplayEventConnections.size();
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800138 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700139 sp<Connection> connection =
140 mDisplayEventConnections.itemAt(i).promote();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700141 if (connection!=0 && connection->count >= 0) {
142 // at least one continuous mode or active one-shot event
143 waitForNextVsync = true;
144 break;
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800145 }
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800146 }
Mathias Agopian23748662011-12-05 14:33:34 -0800147
Mathias Agopiand94d3b82012-04-08 15:01:31 -0700148 if (timestamp) {
149 if (!waitForNextVsync) {
150 // we received a VSYNC but we have no clients
151 // don't report it, and disable VSYNC events
Mathias Agopian22ffb112012-04-10 21:04:02 -0700152 disableVSyncLocked();
Mathias Agopiand94d3b82012-04-08 15:01:31 -0700153 } else {
154 // report VSYNC event
155 break;
156 }
157 } else {
158 // never disable VSYNC events immediately, instead
159 // we'll wait to receive the event and we'll
160 // reevaluate whether we need to dispatch it and/or
161 // disable VSYNC events then.
162 if (waitForNextVsync) {
163 // enable
Mathias Agopian22ffb112012-04-10 21:04:02 -0700164 enableVSyncLocked();
Mathias Agopiand94d3b82012-04-08 15:01:31 -0700165 }
166 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700167
168 // wait for something to happen
Mathias Agopianfca660c2012-04-12 20:43:16 -0700169 if (mUseSoftwareVSync && waitForNextVsync) {
Mathias Agopian22ffb112012-04-10 21:04:02 -0700170 // h/w vsync cannot be used (screen is off), so we use
171 // a timeout instead. it doesn't matter how imprecise this
172 // is, we just need to make sure to serve the clients
173 if (mCondition.waitRelative(mLock, ms2ns(16)) == TIMED_OUT) {
174 mVSyncTimestamp = systemTime(SYSTEM_TIME_MONOTONIC);
175 }
176 } else {
177 mCondition.wait(mLock);
178 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700179 } while(true);
180
181 // process vsync event
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700182 mDeliveredEvents++;
183 mLastVSyncTimestamp = timestamp;
184
185 // now see if we still need to report this VSYNC event
186 const size_t count = mDisplayEventConnections.size();
187 for (size_t i=0 ; i<count ; i++) {
188 bool reportVsync = false;
189 sp<Connection> connection =
190 mDisplayEventConnections.itemAt(i).promote();
191 if (connection == 0)
192 continue;
193
194 const int32_t count = connection->count;
195 if (count >= 1) {
196 if (count==1 || (mDeliveredEvents % count) == 0) {
197 // continuous event, and time to report it
198 reportVsync = true;
199 }
200 } else if (count >= -1) {
201 if (count == 0) {
202 // fired this time around
203 reportVsync = true;
204 }
205 connection->count--;
206 }
207 if (reportVsync) {
208 displayEventConnections.add(connection);
209 }
210 }
211 } while (!displayEventConnections.size());
212
213 // dispatch vsync events to listeners...
214 vsync.header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
215 vsync.header.timestamp = timestamp;
216 vsync.vsync.count = mDeliveredEvents;
Mathias Agopian23748662011-12-05 14:33:34 -0800217
218 const size_t count = displayEventConnections.size();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800219 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700220 sp<Connection> conn(displayEventConnections[i].promote());
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800221 // make sure the connection didn't die
222 if (conn != NULL) {
223 status_t err = conn->postEvent(vsync);
224 if (err == -EAGAIN || err == -EWOULDBLOCK) {
225 // The destination doesn't accept events anymore, it's probably
226 // full. For now, we just drop the events on the floor.
227 // Note that some events cannot be dropped and would have to be
228 // re-sent later. Right-now we don't have the ability to do
229 // this, but it doesn't matter for VSYNC.
230 } else if (err < 0) {
231 // handle any other error on the pipe as fatal. the only
232 // reasonable thing to do is to clean-up this connection.
233 // The most common error we'll get here is -EPIPE.
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800234 removeDisplayEventConnection(displayEventConnections[i]);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800235 }
Mathias Agopian23748662011-12-05 14:33:34 -0800236 } else {
237 // somehow the connection is dead, but we still have it in our list
238 // just clean the list.
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800239 removeDisplayEventConnection(displayEventConnections[i]);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800240 }
241 }
242
Mathias Agopian23748662011-12-05 14:33:34 -0800243 // clear all our references without holding mLock
244 displayEventConnections.clear();
245
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800246 return true;
247}
248
Mathias Agopian22ffb112012-04-10 21:04:02 -0700249void EventThread::enableVSyncLocked() {
250 if (!mUseSoftwareVSync) {
251 // never enable h/w VSYNC when screen is off
Mathias Agopian86303202012-07-24 22:46:10 -0700252 mFlinger->eventControl(SurfaceFlinger::EVENT_VSYNC, true);
253 mPowerHAL.vsyncHint(true);
Mathias Agopian22ffb112012-04-10 21:04:02 -0700254 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700255 mDebugVsyncEnabled = true;
256}
257
Mathias Agopian22ffb112012-04-10 21:04:02 -0700258void EventThread::disableVSyncLocked() {
Mathias Agopian86303202012-07-24 22:46:10 -0700259 mFlinger->eventControl(SurfaceFlinger::EVENT_VSYNC, false);
260 mPowerHAL.vsyncHint(false);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700261 mDebugVsyncEnabled = false;
262}
263
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800264status_t EventThread::readyToRun() {
Steve Blocka19954a2012-01-04 20:05:49 +0000265 ALOGI("EventThread ready to run.");
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800266 return NO_ERROR;
267}
268
269void EventThread::dump(String8& result, char* buffer, size_t SIZE) const {
270 Mutex::Autolock _l(mLock);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700271 result.appendFormat("VSYNC state: %s\n",
272 mDebugVsyncEnabled?"enabled":"disabled");
Mathias Agopian22ffb112012-04-10 21:04:02 -0700273 result.appendFormat(" soft-vsync: %s\n",
274 mUseSoftwareVSync?"enabled":"disabled");
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700275 result.appendFormat(" numListeners=%u,\n events-delivered: %u\n",
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800276 mDisplayEventConnections.size(), mDeliveredEvents);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700277 for (size_t i=0 ; i<mDisplayEventConnections.size() ; i++) {
278 sp<Connection> connection =
279 mDisplayEventConnections.itemAt(i).promote();
280 result.appendFormat(" %p: count=%d\n",
281 connection.get(), connection!=NULL ? connection->count : 0);
282 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800283}
284
285// ---------------------------------------------------------------------------
286
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700287EventThread::Connection::Connection(
288 const sp<EventThread>& eventThread)
289 : count(-1), mEventThread(eventThread), mChannel(new BitTube())
290{
291}
292
293EventThread::Connection::~Connection() {
294 mEventThread->unregisterDisplayEventConnection(this);
295}
296
297void EventThread::Connection::onFirstRef() {
298 // NOTE: mEventThread doesn't hold a strong reference on us
299 mEventThread->registerDisplayEventConnection(this);
300}
301
302sp<BitTube> EventThread::Connection::getDataChannel() const {
303 return mChannel;
304}
305
306void EventThread::Connection::setVsyncRate(uint32_t count) {
307 mEventThread->setVsyncRate(count, this);
308}
309
310void EventThread::Connection::requestNextVsync() {
311 mEventThread->requestNextVsync(this);
312}
313
314status_t EventThread::Connection::postEvent(
315 const DisplayEventReceiver::Event& event) {
316 ssize_t size = DisplayEventReceiver::sendEvents(mChannel, &event, 1);
317 return size < 0 ? status_t(size) : status_t(NO_ERROR);
318}
319
320// ---------------------------------------------------------------------------
321
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800322}; // namespace android