blob: 3d7957794e92c2a63eb81e6043f83f4417db7d88 [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 Agopian1b031492012-06-20 17:51:20 -070030#include "DisplayHardware.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080031#include "EventThread.h"
32#include "SurfaceFlinger.h"
33
34// ---------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -080035namespace android {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080036// ---------------------------------------------------------------------------
37
38EventThread::EventThread(const sp<SurfaceFlinger>& flinger)
Mathias Agopian86303202012-07-24 22:46:10 -070039 : mFlinger(flinger),
Mathias Agopian8aedd472012-01-24 16:39:14 -080040 mLastVSyncTimestamp(0),
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070041 mVSyncTimestamp(0),
Mathias Agopian22ffb112012-04-10 21:04:02 -070042 mUseSoftwareVSync(false),
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -070043 mDeliveredEvents(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
63status_t EventThread::unregisterDisplayEventConnection(
Mathias Agopiancb9732a2012-04-03 17:48:03 -070064 const wp<EventThread::Connection>& connection) {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080065 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070066 mDisplayEventConnections.remove(connection);
Mathias Agopian7d886472012-06-14 23:39:35 -070067 mCondition.broadcast();
Mathias Agopiand0566bc2011-11-17 17:49:17 -080068 return NO_ERROR;
69}
70
Mathias Agopian478ae5e2011-12-06 17:22:19 -080071void EventThread::removeDisplayEventConnection(
Mathias Agopiancb9732a2012-04-03 17:48:03 -070072 const wp<EventThread::Connection>& connection) {
Mathias Agopian23748662011-12-05 14:33:34 -080073 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070074 mDisplayEventConnections.remove(connection);
Mathias Agopian478ae5e2011-12-06 17:22:19 -080075}
76
77void EventThread::setVsyncRate(uint32_t count,
Mathias Agopiancb9732a2012-04-03 17:48:03 -070078 const sp<EventThread::Connection>& connection) {
Mathias Agopian478ae5e2011-12-06 17:22:19 -080079 if (int32_t(count) >= 0) { // server must protect against bad params
80 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070081 const int32_t new_count = (count == 0) ? -1 : count;
82 if (connection->count != new_count) {
83 connection->count = new_count;
Mathias Agopian7d886472012-06-14 23:39:35 -070084 mCondition.broadcast();
Mathias Agopian478ae5e2011-12-06 17:22:19 -080085 }
86 }
87}
88
89void EventThread::requestNextVsync(
Mathias Agopiancb9732a2012-04-03 17:48:03 -070090 const sp<EventThread::Connection>& connection) {
Mathias Agopian478ae5e2011-12-06 17:22:19 -080091 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070092 if (connection->count < 0) {
93 connection->count = 0;
Mathias Agopian7d886472012-06-14 23:39:35 -070094 mCondition.broadcast();
Mathias Agopian478ae5e2011-12-06 17:22:19 -080095 }
Mathias Agopian23748662011-12-05 14:33:34 -080096}
97
Mathias Agopian22ffb112012-04-10 21:04:02 -070098void EventThread::onScreenReleased() {
99 Mutex::Autolock _l(mLock);
Mathias Agopian22ffb112012-04-10 21:04:02 -0700100 if (!mUseSoftwareVSync) {
Mathias Agopian7d886472012-06-14 23:39:35 -0700101 // disable reliance on h/w vsync
102 mUseSoftwareVSync = true;
103 mCondition.broadcast();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700104 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700105}
106
107void EventThread::onScreenAcquired() {
108 Mutex::Autolock _l(mLock);
Mathias Agopian7d886472012-06-14 23:39:35 -0700109 if (mUseSoftwareVSync) {
110 // resume use of h/w vsync
111 mUseSoftwareVSync = false;
112 mCondition.broadcast();
113 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700114}
115
116
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700117void EventThread::onVSyncReceived(int, nsecs_t timestamp) {
118 Mutex::Autolock _l(mLock);
119 mVSyncTimestamp = timestamp;
Mathias Agopian7d886472012-06-14 23:39:35 -0700120 mCondition.broadcast();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700121}
122
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800123bool EventThread::threadLoop() {
124
125 nsecs_t timestamp;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800126 DisplayEventReceiver::Event vsync;
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700127 Vector< wp<EventThread::Connection> > displayEventConnections;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800128
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700129 do {
Mathias Agopian23748662011-12-05 14:33:34 -0800130 Mutex::Autolock _l(mLock);
131 do {
Mathias Agopiand94d3b82012-04-08 15:01:31 -0700132 // latch VSYNC event if any
133 timestamp = mVSyncTimestamp;
134 mVSyncTimestamp = 0;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800135
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700136 // check if we should be waiting for VSYNC events
137 bool waitForNextVsync = false;
138 size_t count = mDisplayEventConnections.size();
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800139 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700140 sp<Connection> connection =
141 mDisplayEventConnections.itemAt(i).promote();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700142 if (connection!=0 && connection->count >= 0) {
143 // at least one continuous mode or active one-shot event
144 waitForNextVsync = true;
145 break;
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800146 }
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800147 }
Mathias Agopian23748662011-12-05 14:33:34 -0800148
Mathias Agopiand94d3b82012-04-08 15:01:31 -0700149 if (timestamp) {
150 if (!waitForNextVsync) {
151 // we received a VSYNC but we have no clients
152 // don't report it, and disable VSYNC events
Mathias Agopian22ffb112012-04-10 21:04:02 -0700153 disableVSyncLocked();
Mathias Agopiand94d3b82012-04-08 15:01:31 -0700154 } else {
155 // report VSYNC event
156 break;
157 }
158 } else {
159 // never disable VSYNC events immediately, instead
160 // we'll wait to receive the event and we'll
161 // reevaluate whether we need to dispatch it and/or
162 // disable VSYNC events then.
163 if (waitForNextVsync) {
164 // enable
Mathias Agopian22ffb112012-04-10 21:04:02 -0700165 enableVSyncLocked();
Mathias Agopiand94d3b82012-04-08 15:01:31 -0700166 }
167 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700168
169 // wait for something to happen
Mathias Agopianfca660c2012-04-12 20:43:16 -0700170 if (mUseSoftwareVSync && waitForNextVsync) {
Mathias Agopian22ffb112012-04-10 21:04:02 -0700171 // h/w vsync cannot be used (screen is off), so we use
172 // a timeout instead. it doesn't matter how imprecise this
173 // is, we just need to make sure to serve the clients
174 if (mCondition.waitRelative(mLock, ms2ns(16)) == TIMED_OUT) {
175 mVSyncTimestamp = systemTime(SYSTEM_TIME_MONOTONIC);
176 }
177 } else {
178 mCondition.wait(mLock);
179 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700180 } while(true);
181
182 // process vsync event
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700183 mDeliveredEvents++;
184 mLastVSyncTimestamp = timestamp;
185
186 // now see if we still need to report this VSYNC event
187 const size_t count = mDisplayEventConnections.size();
188 for (size_t i=0 ; i<count ; i++) {
189 bool reportVsync = false;
190 sp<Connection> connection =
191 mDisplayEventConnections.itemAt(i).promote();
192 if (connection == 0)
193 continue;
194
195 const int32_t count = connection->count;
196 if (count >= 1) {
197 if (count==1 || (mDeliveredEvents % count) == 0) {
198 // continuous event, and time to report it
199 reportVsync = true;
200 }
201 } else if (count >= -1) {
202 if (count == 0) {
203 // fired this time around
204 reportVsync = true;
205 }
206 connection->count--;
207 }
208 if (reportVsync) {
209 displayEventConnections.add(connection);
210 }
211 }
212 } while (!displayEventConnections.size());
213
214 // dispatch vsync events to listeners...
215 vsync.header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
216 vsync.header.timestamp = timestamp;
217 vsync.vsync.count = mDeliveredEvents;
Mathias Agopian23748662011-12-05 14:33:34 -0800218
219 const size_t count = displayEventConnections.size();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800220 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700221 sp<Connection> conn(displayEventConnections[i].promote());
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800222 // make sure the connection didn't die
223 if (conn != NULL) {
224 status_t err = conn->postEvent(vsync);
225 if (err == -EAGAIN || err == -EWOULDBLOCK) {
226 // The destination doesn't accept events anymore, it's probably
227 // full. For now, we just drop the events on the floor.
228 // Note that some events cannot be dropped and would have to be
229 // re-sent later. Right-now we don't have the ability to do
230 // this, but it doesn't matter for VSYNC.
231 } else if (err < 0) {
232 // handle any other error on the pipe as fatal. the only
233 // reasonable thing to do is to clean-up this connection.
234 // The most common error we'll get here is -EPIPE.
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800235 removeDisplayEventConnection(displayEventConnections[i]);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800236 }
Mathias Agopian23748662011-12-05 14:33:34 -0800237 } else {
238 // somehow the connection is dead, but we still have it in our list
239 // just clean the list.
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800240 removeDisplayEventConnection(displayEventConnections[i]);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800241 }
242 }
243
Mathias Agopian23748662011-12-05 14:33:34 -0800244 // clear all our references without holding mLock
245 displayEventConnections.clear();
246
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800247 return true;
248}
249
Mathias Agopian22ffb112012-04-10 21:04:02 -0700250void EventThread::enableVSyncLocked() {
251 if (!mUseSoftwareVSync) {
252 // never enable h/w VSYNC when screen is off
Mathias Agopian86303202012-07-24 22:46:10 -0700253 mFlinger->eventControl(SurfaceFlinger::EVENT_VSYNC, true);
254 mPowerHAL.vsyncHint(true);
Mathias Agopian22ffb112012-04-10 21:04:02 -0700255 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700256 mDebugVsyncEnabled = true;
257}
258
Mathias Agopian22ffb112012-04-10 21:04:02 -0700259void EventThread::disableVSyncLocked() {
Mathias Agopian86303202012-07-24 22:46:10 -0700260 mFlinger->eventControl(SurfaceFlinger::EVENT_VSYNC, false);
261 mPowerHAL.vsyncHint(false);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700262 mDebugVsyncEnabled = false;
263}
264
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800265status_t EventThread::readyToRun() {
Steve Blocka19954a2012-01-04 20:05:49 +0000266 ALOGI("EventThread ready to run.");
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800267 return NO_ERROR;
268}
269
270void EventThread::dump(String8& result, char* buffer, size_t SIZE) const {
271 Mutex::Autolock _l(mLock);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700272 result.appendFormat("VSYNC state: %s\n",
273 mDebugVsyncEnabled?"enabled":"disabled");
Mathias Agopian22ffb112012-04-10 21:04:02 -0700274 result.appendFormat(" soft-vsync: %s\n",
275 mUseSoftwareVSync?"enabled":"disabled");
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700276 result.appendFormat(" numListeners=%u,\n events-delivered: %u\n",
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800277 mDisplayEventConnections.size(), mDeliveredEvents);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700278 for (size_t i=0 ; i<mDisplayEventConnections.size() ; i++) {
279 sp<Connection> connection =
280 mDisplayEventConnections.itemAt(i).promote();
281 result.appendFormat(" %p: count=%d\n",
282 connection.get(), connection!=NULL ? connection->count : 0);
283 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800284}
285
286// ---------------------------------------------------------------------------
287
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700288EventThread::Connection::Connection(
289 const sp<EventThread>& eventThread)
290 : count(-1), mEventThread(eventThread), mChannel(new BitTube())
291{
292}
293
294EventThread::Connection::~Connection() {
295 mEventThread->unregisterDisplayEventConnection(this);
296}
297
298void EventThread::Connection::onFirstRef() {
299 // NOTE: mEventThread doesn't hold a strong reference on us
300 mEventThread->registerDisplayEventConnection(this);
301}
302
303sp<BitTube> EventThread::Connection::getDataChannel() const {
304 return mChannel;
305}
306
307void EventThread::Connection::setVsyncRate(uint32_t count) {
308 mEventThread->setVsyncRate(count, this);
309}
310
311void EventThread::Connection::requestNextVsync() {
312 mEventThread->requestNextVsync(this);
313}
314
315status_t EventThread::Connection::postEvent(
316 const DisplayEventReceiver::Event& event) {
317 ssize_t size = DisplayEventReceiver::sendEvents(mChannel, &event, 1);
318 return size < 0 ? status_t(size) : status_t(NO_ERROR);
319}
320
321// ---------------------------------------------------------------------------
322
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800323}; // namespace android