blob: 59390c0ce441add466e18dafbbdd791628a0a27e [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 Agopiana4cb35a2012-08-20 20:07:34 -070022#include <cutils/compiler.h>
23
Mathias Agopiancb9732a2012-04-03 17:48:03 -070024#include <gui/BitTube.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080025#include <gui/IDisplayEventConnection.h>
26#include <gui/DisplayEventReceiver.h>
27
28#include <utils/Errors.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070029#include <utils/String8.h>
Mathias Agopian841cde52012-03-01 15:44:37 -080030#include <utils/Trace.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080031
Mathias Agopiand0566bc2011-11-17 17:49:17 -080032#include "EventThread.h"
33#include "SurfaceFlinger.h"
34
35// ---------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -080036namespace android {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080037// ---------------------------------------------------------------------------
38
39EventThread::EventThread(const sp<SurfaceFlinger>& flinger)
Mathias Agopian86303202012-07-24 22:46:10 -070040 : mFlinger(flinger),
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070041 mVSyncTimestamp(0),
Mathias Agopian22ffb112012-04-10 21:04:02 -070042 mUseSoftwareVSync(false),
Mathias Agopian10125f02012-08-17 15:06:02 -070043 mVSyncCount(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 Agopian10125f02012-08-17 15:06:02 -0700120 mVSyncCount++;
Mathias Agopian7d886472012-06-14 23:39:35 -0700121 mCondition.broadcast();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700122}
123
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800124bool EventThread::threadLoop() {
125
126 nsecs_t timestamp;
Mathias Agopian10125f02012-08-17 15:06:02 -0700127 size_t vsyncCount;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800128 DisplayEventReceiver::Event vsync;
Mathias Agopian10125f02012-08-17 15:06:02 -0700129 Vector< sp<EventThread::Connection> > activeConnections;
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700130 Vector< sp<EventThread::Connection> > signalConnections;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800131
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700132 do {
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700133 // release our references
134 signalConnections.clear();
135 activeConnections.clear();
136
Mathias Agopian23748662011-12-05 14:33:34 -0800137 Mutex::Autolock _l(mLock);
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700138
Mathias Agopian10125f02012-08-17 15:06:02 -0700139 // latch VSYNC event if any
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700140 bool waitForVSync = false;
141 vsyncCount = mVSyncCount;
Mathias Agopian10125f02012-08-17 15:06:02 -0700142 timestamp = mVSyncTimestamp;
143 mVSyncTimestamp = 0;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800144
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700145 // find out connections waiting for VSYNC events
Mathias Agopian10125f02012-08-17 15:06:02 -0700146 size_t count = mDisplayEventConnections.size();
147 for (size_t i=0 ; i<count ; i++) {
148 sp<Connection> connection(mDisplayEventConnections[i].promote());
149 if (connection != NULL) {
150 activeConnections.add(connection);
151 if (connection->count >= 0) {
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700152 // we need vsync events because at least
153 // one connection is waiting for it
154 waitForVSync = true;
155 if (connection->count == 0) {
156 // fired this time around
157 if (timestamp) {
158 // only "consume" this event if we're going to
159 // report it
160 connection->count = -1;
161 }
162 signalConnections.add(connection);
163 } else if (connection->count == 1 ||
164 (vsyncCount % connection->count) == 0) {
165 // continuous event, and time to report it
166 signalConnections.add(connection);
167 }
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800168 }
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800169 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700170 }
Mathias Agopian10125f02012-08-17 15:06:02 -0700171
172 if (timestamp) {
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700173 // we have a vsync event we can dispatch
174 if (!waitForVSync) {
Mathias Agopian10125f02012-08-17 15:06:02 -0700175 // we received a VSYNC but we have no clients
176 // don't report it, and disable VSYNC events
177 disableVSyncLocked();
178 } else {
179 // report VSYNC event
180 break;
181 }
182 } else {
183 // never disable VSYNC events immediately, instead
184 // we'll wait to receive the event and we'll
185 // reevaluate whether we need to dispatch it and/or
186 // disable VSYNC events then.
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700187 if (waitForVSync) {
Mathias Agopian10125f02012-08-17 15:06:02 -0700188 // enable
189 enableVSyncLocked();
190 }
191 }
192
193 // wait for something to happen
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700194 if (CC_UNLIKELY(mUseSoftwareVSync && waitForVSync)) {
Mathias Agopian10125f02012-08-17 15:06:02 -0700195 // h/w vsync cannot be used (screen is off), so we use
196 // a timeout instead. it doesn't matter how imprecise this
197 // is, we just need to make sure to serve the clients
198 if (mCondition.waitRelative(mLock, ms2ns(16)) == TIMED_OUT) {
199 mVSyncTimestamp = systemTime(SYSTEM_TIME_MONOTONIC);
200 mVSyncCount++;
201 }
202 } else {
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700203 if (!timestamp || signalConnections.isEmpty()) {
204 // This is where we spend most of our time, waiting
205 // for a vsync events and registered clients
206 mCondition.wait(mLock);
207 }
Mathias Agopian10125f02012-08-17 15:06:02 -0700208 }
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700209 } while (!timestamp || signalConnections.isEmpty());
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700210
211 // dispatch vsync events to listeners...
212 vsync.header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
213 vsync.header.timestamp = timestamp;
Mathias Agopian10125f02012-08-17 15:06:02 -0700214 vsync.vsync.count = vsyncCount;
Mathias Agopian23748662011-12-05 14:33:34 -0800215
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700216 const size_t count = signalConnections.size();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800217 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700218 const sp<Connection>& conn(signalConnections[i]);
Mathias Agopian10125f02012-08-17 15:06:02 -0700219 // now see if we still need to report this VSYNC event
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700220 status_t err = conn->postEvent(vsync);
221 if (err == -EAGAIN || err == -EWOULDBLOCK) {
222 // The destination doesn't accept events anymore, it's probably
223 // full. For now, we just drop the events on the floor.
224 // Note that some events cannot be dropped and would have to be
225 // re-sent later. Right-now we don't have the ability to do
226 // this, but it doesn't matter for VSYNC.
227 } else if (err < 0) {
228 // handle any other error on the pipe as fatal. the only
229 // reasonable thing to do is to clean-up this connection.
230 // The most common error we'll get here is -EPIPE.
231 removeDisplayEventConnection(signalConnections[i]);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800232 }
233 }
234
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800235 return true;
236}
237
Mathias Agopian22ffb112012-04-10 21:04:02 -0700238void EventThread::enableVSyncLocked() {
239 if (!mUseSoftwareVSync) {
240 // never enable h/w VSYNC when screen is off
Mathias Agopian86303202012-07-24 22:46:10 -0700241 mFlinger->eventControl(SurfaceFlinger::EVENT_VSYNC, true);
242 mPowerHAL.vsyncHint(true);
Mathias Agopian22ffb112012-04-10 21:04:02 -0700243 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700244 mDebugVsyncEnabled = true;
245}
246
Mathias Agopian22ffb112012-04-10 21:04:02 -0700247void EventThread::disableVSyncLocked() {
Mathias Agopian86303202012-07-24 22:46:10 -0700248 mFlinger->eventControl(SurfaceFlinger::EVENT_VSYNC, false);
249 mPowerHAL.vsyncHint(false);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700250 mDebugVsyncEnabled = false;
251}
252
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800253status_t EventThread::readyToRun() {
Steve Blocka19954a2012-01-04 20:05:49 +0000254 ALOGI("EventThread ready to run.");
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800255 return NO_ERROR;
256}
257
258void EventThread::dump(String8& result, char* buffer, size_t SIZE) const {
259 Mutex::Autolock _l(mLock);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700260 result.appendFormat("VSYNC state: %s\n",
261 mDebugVsyncEnabled?"enabled":"disabled");
Mathias Agopian22ffb112012-04-10 21:04:02 -0700262 result.appendFormat(" soft-vsync: %s\n",
263 mUseSoftwareVSync?"enabled":"disabled");
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700264 result.appendFormat(" numListeners=%u,\n events-delivered: %u\n",
Mathias Agopian10125f02012-08-17 15:06:02 -0700265 mDisplayEventConnections.size(), mVSyncCount);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700266 for (size_t i=0 ; i<mDisplayEventConnections.size() ; i++) {
267 sp<Connection> connection =
268 mDisplayEventConnections.itemAt(i).promote();
269 result.appendFormat(" %p: count=%d\n",
270 connection.get(), connection!=NULL ? connection->count : 0);
271 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800272}
273
274// ---------------------------------------------------------------------------
275
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700276EventThread::Connection::Connection(
277 const sp<EventThread>& eventThread)
278 : count(-1), mEventThread(eventThread), mChannel(new BitTube())
279{
280}
281
282EventThread::Connection::~Connection() {
283 mEventThread->unregisterDisplayEventConnection(this);
284}
285
286void EventThread::Connection::onFirstRef() {
287 // NOTE: mEventThread doesn't hold a strong reference on us
288 mEventThread->registerDisplayEventConnection(this);
289}
290
291sp<BitTube> EventThread::Connection::getDataChannel() const {
292 return mChannel;
293}
294
295void EventThread::Connection::setVsyncRate(uint32_t count) {
296 mEventThread->setVsyncRate(count, this);
297}
298
299void EventThread::Connection::requestNextVsync() {
300 mEventThread->requestNextVsync(this);
301}
302
303status_t EventThread::Connection::postEvent(
304 const DisplayEventReceiver::Event& event) {
305 ssize_t size = DisplayEventReceiver::sendEvents(mChannel, &event, 1);
306 return size < 0 ? status_t(size) : status_t(NO_ERROR);
307}
308
309// ---------------------------------------------------------------------------
310
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800311}; // namespace android