blob: dcda67ef1861ed036f2cb0d32abb8fa12d915bc9 [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
Mathias Agopian478ae5e2011-12-06 17:22:19 -080063void EventThread::removeDisplayEventConnection(
Mathias Agopiancb9732a2012-04-03 17:48:03 -070064 const wp<EventThread::Connection>& connection) {
Mathias Agopian23748662011-12-05 14:33:34 -080065 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070066 mDisplayEventConnections.remove(connection);
Mathias Agopian478ae5e2011-12-06 17:22:19 -080067}
68
69void EventThread::setVsyncRate(uint32_t count,
Mathias Agopiancb9732a2012-04-03 17:48:03 -070070 const sp<EventThread::Connection>& connection) {
Mathias Agopian478ae5e2011-12-06 17:22:19 -080071 if (int32_t(count) >= 0) { // server must protect against bad params
72 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070073 const int32_t new_count = (count == 0) ? -1 : count;
74 if (connection->count != new_count) {
75 connection->count = new_count;
Mathias Agopian7d886472012-06-14 23:39:35 -070076 mCondition.broadcast();
Mathias Agopian478ae5e2011-12-06 17:22:19 -080077 }
78 }
79}
80
81void EventThread::requestNextVsync(
Mathias Agopiancb9732a2012-04-03 17:48:03 -070082 const sp<EventThread::Connection>& connection) {
Mathias Agopian478ae5e2011-12-06 17:22:19 -080083 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070084 if (connection->count < 0) {
85 connection->count = 0;
Mathias Agopian7d886472012-06-14 23:39:35 -070086 mCondition.broadcast();
Mathias Agopian478ae5e2011-12-06 17:22:19 -080087 }
Mathias Agopian23748662011-12-05 14:33:34 -080088}
89
Mathias Agopian22ffb112012-04-10 21:04:02 -070090void EventThread::onScreenReleased() {
91 Mutex::Autolock _l(mLock);
Mathias Agopian22ffb112012-04-10 21:04:02 -070092 if (!mUseSoftwareVSync) {
Mathias Agopian7d886472012-06-14 23:39:35 -070093 // disable reliance on h/w vsync
94 mUseSoftwareVSync = true;
95 mCondition.broadcast();
Mathias Agopian22ffb112012-04-10 21:04:02 -070096 }
Mathias Agopian22ffb112012-04-10 21:04:02 -070097}
98
99void EventThread::onScreenAcquired() {
100 Mutex::Autolock _l(mLock);
Mathias Agopian7d886472012-06-14 23:39:35 -0700101 if (mUseSoftwareVSync) {
102 // resume use of h/w vsync
103 mUseSoftwareVSync = false;
104 mCondition.broadcast();
105 }
Mathias Agopian22ffb112012-04-10 21:04:02 -0700106}
107
108
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700109void EventThread::onVSyncReceived(const wp<IBinder>&, nsecs_t timestamp) {
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700110 Mutex::Autolock _l(mLock);
111 mVSyncTimestamp = timestamp;
Mathias Agopian10125f02012-08-17 15:06:02 -0700112 mVSyncCount++;
Mathias Agopian7d886472012-06-14 23:39:35 -0700113 mCondition.broadcast();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700114}
115
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800116bool EventThread::threadLoop() {
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800117 DisplayEventReceiver::Event vsync;
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700118 Vector< sp<EventThread::Connection> > signalConnections;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700119 signalConnections = waitForEvent(&vsync);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700120
121 // dispatch vsync events to listeners...
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700122 const size_t count = signalConnections.size();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800123 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700124 const sp<Connection>& conn(signalConnections[i]);
Mathias Agopian10125f02012-08-17 15:06:02 -0700125 // now see if we still need to report this VSYNC event
Mathias Agopiana4cb35a2012-08-20 20:07:34 -0700126 status_t err = conn->postEvent(vsync);
127 if (err == -EAGAIN || err == -EWOULDBLOCK) {
128 // The destination doesn't accept events anymore, it's probably
129 // full. For now, we just drop the events on the floor.
130 // Note that some events cannot be dropped and would have to be
131 // re-sent later. Right-now we don't have the ability to do
132 // this, but it doesn't matter for VSYNC.
133 } else if (err < 0) {
134 // handle any other error on the pipe as fatal. the only
135 // reasonable thing to do is to clean-up this connection.
136 // The most common error we'll get here is -EPIPE.
137 removeDisplayEventConnection(signalConnections[i]);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800138 }
139 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800140 return true;
141}
142
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700143Vector< sp<EventThread::Connection> > EventThread::waitForEvent(
144 DisplayEventReceiver::Event* event)
145{
146 Mutex::Autolock _l(mLock);
147
148 size_t vsyncCount;
149 nsecs_t timestamp;
150 Vector< sp<EventThread::Connection> > signalConnections;
151
152 do {
153 // latch VSYNC event if any
154 bool waitForVSync = false;
155 vsyncCount = mVSyncCount;
156 timestamp = mVSyncTimestamp;
157 mVSyncTimestamp = 0;
158
159 // find out connections waiting for events
160 size_t count = mDisplayEventConnections.size();
161 for (size_t i=0 ; i<count ; i++) {
162 sp<Connection> connection(mDisplayEventConnections[i].promote());
163 if (connection != NULL) {
164 if (connection->count >= 0) {
165 // we need vsync events because at least
166 // one connection is waiting for it
167 waitForVSync = true;
168 if (timestamp) {
169 // we consume the event only if it's time
170 // (ie: we received a vsync event)
171 if (connection->count == 0) {
172 // fired this time around
173 connection->count = -1;
174 signalConnections.add(connection);
175 } else if (connection->count == 1 ||
176 (vsyncCount % connection->count) == 0) {
177 // continuous event, and time to report it
178 signalConnections.add(connection);
179 }
180 }
181 }
182 } else {
183 // we couldn't promote this reference, the connection has
184 // died, so clean-up!
185 mDisplayEventConnections.removeAt(i);
186 --i; --count;
187 }
188 }
189
190 // Here we figure out if we need to enable or disable vsyncs
191 if (timestamp && !waitForVSync) {
192 // we received a VSYNC but we have no clients
193 // don't report it, and disable VSYNC events
194 disableVSyncLocked();
195 } else if (!timestamp && waitForVSync) {
196 enableVSyncLocked();
197 }
198
199 // note: !timestamp implies signalConnections.isEmpty()
200 if (!timestamp) {
201 // wait for something to happen
202 if (CC_UNLIKELY(mUseSoftwareVSync && waitForVSync)) {
203 // h/w vsync cannot be used (screen is off), so we use
204 // a timeout instead. it doesn't matter how imprecise this
205 // is, we just need to make sure to serve the clients
206 if (mCondition.waitRelative(mLock, ms2ns(16)) == TIMED_OUT) {
207 mVSyncTimestamp = systemTime(SYSTEM_TIME_MONOTONIC);
208 mVSyncCount++;
209 }
210 } else {
211 // This is where we spend most of our time, waiting
212 // for a vsync events and registered clients
213 mCondition.wait(mLock);
214 }
215 }
216 } while (signalConnections.isEmpty());
217
218 // here we're guaranteed to have a timestamp and some connections to signal
219
220 // dispatch vsync events to listeners...
221 event->header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
222 event->header.timestamp = timestamp;
223 event->vsync.count = vsyncCount;
224
225 return signalConnections;
226}
227
Mathias Agopian22ffb112012-04-10 21:04:02 -0700228void EventThread::enableVSyncLocked() {
229 if (!mUseSoftwareVSync) {
230 // never enable h/w VSYNC when screen is off
Mathias Agopian86303202012-07-24 22:46:10 -0700231 mFlinger->eventControl(SurfaceFlinger::EVENT_VSYNC, true);
232 mPowerHAL.vsyncHint(true);
Mathias Agopian22ffb112012-04-10 21:04:02 -0700233 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700234 mDebugVsyncEnabled = true;
235}
236
Mathias Agopian22ffb112012-04-10 21:04:02 -0700237void EventThread::disableVSyncLocked() {
Mathias Agopian86303202012-07-24 22:46:10 -0700238 mFlinger->eventControl(SurfaceFlinger::EVENT_VSYNC, false);
239 mPowerHAL.vsyncHint(false);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700240 mDebugVsyncEnabled = false;
241}
242
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800243status_t EventThread::readyToRun() {
Steve Blocka19954a2012-01-04 20:05:49 +0000244 ALOGI("EventThread ready to run.");
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800245 return NO_ERROR;
246}
247
248void EventThread::dump(String8& result, char* buffer, size_t SIZE) const {
249 Mutex::Autolock _l(mLock);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700250 result.appendFormat("VSYNC state: %s\n",
251 mDebugVsyncEnabled?"enabled":"disabled");
Mathias Agopian22ffb112012-04-10 21:04:02 -0700252 result.appendFormat(" soft-vsync: %s\n",
253 mUseSoftwareVSync?"enabled":"disabled");
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700254 result.appendFormat(" numListeners=%u,\n events-delivered: %u\n",
Mathias Agopian10125f02012-08-17 15:06:02 -0700255 mDisplayEventConnections.size(), mVSyncCount);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700256 for (size_t i=0 ; i<mDisplayEventConnections.size() ; i++) {
257 sp<Connection> connection =
258 mDisplayEventConnections.itemAt(i).promote();
259 result.appendFormat(" %p: count=%d\n",
260 connection.get(), connection!=NULL ? connection->count : 0);
261 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800262}
263
264// ---------------------------------------------------------------------------
265
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700266EventThread::Connection::Connection(
267 const sp<EventThread>& eventThread)
268 : count(-1), mEventThread(eventThread), mChannel(new BitTube())
269{
270}
271
272EventThread::Connection::~Connection() {
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700273 // do nothing here -- clean-up will happen automatically
274 // when the main thread wakes up
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700275}
276
277void EventThread::Connection::onFirstRef() {
278 // NOTE: mEventThread doesn't hold a strong reference on us
279 mEventThread->registerDisplayEventConnection(this);
280}
281
282sp<BitTube> EventThread::Connection::getDataChannel() const {
283 return mChannel;
284}
285
286void EventThread::Connection::setVsyncRate(uint32_t count) {
287 mEventThread->setVsyncRate(count, this);
288}
289
290void EventThread::Connection::requestNextVsync() {
291 mEventThread->requestNextVsync(this);
292}
293
294status_t EventThread::Connection::postEvent(
295 const DisplayEventReceiver::Event& event) {
296 ssize_t size = DisplayEventReceiver::sendEvents(mChannel, &event, 1);
297 return size < 0 ? status_t(size) : status_t(NO_ERROR);
298}
299
300// ---------------------------------------------------------------------------
301
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800302}; // namespace android