blob: 5813eefbef16d4ce7edcf138d748052be1a6e96e [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 Agopiane2c4f4e2012-04-10 18:25:31 -070044 mDeliveredEvents(0),
45 mDebugVsyncEnabled(false)
Mathias Agopiand0566bc2011-11-17 17:49:17 -080046{
47}
48
49void EventThread::onFirstRef() {
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070050 mHw.setVSyncHandler(this);
Mathias Agopiand0566bc2011-11-17 17:49:17 -080051 run("EventThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
52}
53
Mathias Agopiancb9732a2012-04-03 17:48:03 -070054sp<EventThread::Connection> EventThread::createEventConnection() const {
55 return new Connection(const_cast<EventThread*>(this));
Mathias Agopian8aedd472012-01-24 16:39:14 -080056}
57
58nsecs_t EventThread::getLastVSyncTimestamp() const {
59 Mutex::Autolock _l(mLock);
60 return mLastVSyncTimestamp;
61}
62
63nsecs_t EventThread::getVSyncPeriod() const {
64 return mHw.getRefreshPeriod();
65
66}
67
Mathias Agopiand0566bc2011-11-17 17:49:17 -080068status_t EventThread::registerDisplayEventConnection(
Mathias Agopiancb9732a2012-04-03 17:48:03 -070069 const sp<EventThread::Connection>& connection) {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080070 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070071 mDisplayEventConnections.add(connection);
Mathias Agopiand0566bc2011-11-17 17:49:17 -080072 mCondition.signal();
73 return NO_ERROR;
74}
75
76status_t EventThread::unregisterDisplayEventConnection(
Mathias Agopiancb9732a2012-04-03 17:48:03 -070077 const wp<EventThread::Connection>& connection) {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080078 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070079 mDisplayEventConnections.remove(connection);
Mathias Agopiand0566bc2011-11-17 17:49:17 -080080 mCondition.signal();
81 return NO_ERROR;
82}
83
Mathias Agopian478ae5e2011-12-06 17:22:19 -080084void EventThread::removeDisplayEventConnection(
Mathias Agopiancb9732a2012-04-03 17:48:03 -070085 const wp<EventThread::Connection>& connection) {
Mathias Agopian23748662011-12-05 14:33:34 -080086 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070087 mDisplayEventConnections.remove(connection);
Mathias Agopian478ae5e2011-12-06 17:22:19 -080088}
89
90void EventThread::setVsyncRate(uint32_t count,
Mathias Agopiancb9732a2012-04-03 17:48:03 -070091 const sp<EventThread::Connection>& connection) {
Mathias Agopian478ae5e2011-12-06 17:22:19 -080092 if (int32_t(count) >= 0) { // server must protect against bad params
93 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070094 const int32_t new_count = (count == 0) ? -1 : count;
95 if (connection->count != new_count) {
96 connection->count = new_count;
97 mCondition.signal();
Mathias Agopian478ae5e2011-12-06 17:22:19 -080098 }
99 }
100}
101
102void EventThread::requestNextVsync(
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700103 const sp<EventThread::Connection>& connection) {
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800104 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700105 if (connection->count < 0) {
106 connection->count = 0;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800107 mCondition.signal();
108 }
Mathias Agopian23748662011-12-05 14:33:34 -0800109}
110
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700111void EventThread::onVSyncReceived(int, nsecs_t timestamp) {
112 Mutex::Autolock _l(mLock);
113 mVSyncTimestamp = timestamp;
114 mCondition.signal();
115}
116
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800117bool EventThread::threadLoop() {
118
119 nsecs_t timestamp;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800120 DisplayEventReceiver::Event vsync;
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700121 Vector< wp<EventThread::Connection> > displayEventConnections;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800122
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700123 do {
124
Mathias Agopian23748662011-12-05 14:33:34 -0800125 Mutex::Autolock _l(mLock);
126 do {
Mathias Agopiand94d3b82012-04-08 15:01:31 -0700127 // latch VSYNC event if any
128 timestamp = mVSyncTimestamp;
129 mVSyncTimestamp = 0;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800130
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700131 // check if we should be waiting for VSYNC events
132 bool waitForNextVsync = false;
133 size_t count = mDisplayEventConnections.size();
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800134 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700135 sp<Connection> connection =
136 mDisplayEventConnections.itemAt(i).promote();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700137 if (connection!=0 && connection->count >= 0) {
138 // at least one continuous mode or active one-shot event
139 waitForNextVsync = true;
140 break;
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800141 }
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800142 }
Mathias Agopian23748662011-12-05 14:33:34 -0800143
Mathias Agopiand94d3b82012-04-08 15:01:31 -0700144 if (timestamp) {
145 if (!waitForNextVsync) {
146 // we received a VSYNC but we have no clients
147 // don't report it, and disable VSYNC events
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700148 disableVSync();
Mathias Agopiand94d3b82012-04-08 15:01:31 -0700149 } else {
150 // report VSYNC event
151 break;
152 }
153 } else {
154 // never disable VSYNC events immediately, instead
155 // we'll wait to receive the event and we'll
156 // reevaluate whether we need to dispatch it and/or
157 // disable VSYNC events then.
158 if (waitForNextVsync) {
159 // enable
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700160 enableVSync();
Mathias Agopiand94d3b82012-04-08 15:01:31 -0700161 }
162 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700163
164 // wait for something to happen
165 mCondition.wait(mLock);
166 } while(true);
167
168 // process vsync event
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700169 mDeliveredEvents++;
170 mLastVSyncTimestamp = timestamp;
171
172 // now see if we still need to report this VSYNC event
173 const size_t count = mDisplayEventConnections.size();
174 for (size_t i=0 ; i<count ; i++) {
175 bool reportVsync = false;
176 sp<Connection> connection =
177 mDisplayEventConnections.itemAt(i).promote();
178 if (connection == 0)
179 continue;
180
181 const int32_t count = connection->count;
182 if (count >= 1) {
183 if (count==1 || (mDeliveredEvents % count) == 0) {
184 // continuous event, and time to report it
185 reportVsync = true;
186 }
187 } else if (count >= -1) {
188 if (count == 0) {
189 // fired this time around
190 reportVsync = true;
191 }
192 connection->count--;
193 }
194 if (reportVsync) {
195 displayEventConnections.add(connection);
196 }
197 }
198 } while (!displayEventConnections.size());
199
200 // dispatch vsync events to listeners...
201 vsync.header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
202 vsync.header.timestamp = timestamp;
203 vsync.vsync.count = mDeliveredEvents;
Mathias Agopian23748662011-12-05 14:33:34 -0800204
205 const size_t count = displayEventConnections.size();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800206 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700207 sp<Connection> conn(displayEventConnections[i].promote());
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800208 // make sure the connection didn't die
209 if (conn != NULL) {
210 status_t err = conn->postEvent(vsync);
211 if (err == -EAGAIN || err == -EWOULDBLOCK) {
212 // The destination doesn't accept events anymore, it's probably
213 // full. For now, we just drop the events on the floor.
214 // Note that some events cannot be dropped and would have to be
215 // re-sent later. Right-now we don't have the ability to do
216 // this, but it doesn't matter for VSYNC.
217 } else if (err < 0) {
218 // handle any other error on the pipe as fatal. the only
219 // reasonable thing to do is to clean-up this connection.
220 // The most common error we'll get here is -EPIPE.
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800221 removeDisplayEventConnection(displayEventConnections[i]);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800222 }
Mathias Agopian23748662011-12-05 14:33:34 -0800223 } else {
224 // somehow the connection is dead, but we still have it in our list
225 // just clean the list.
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800226 removeDisplayEventConnection(displayEventConnections[i]);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800227 }
228 }
229
Mathias Agopian23748662011-12-05 14:33:34 -0800230 // clear all our references without holding mLock
231 displayEventConnections.clear();
232
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800233 return true;
234}
235
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700236void EventThread::enableVSync() {
237 mHw.getHwComposer().eventControl(HWComposer::EVENT_VSYNC, true);
238 mDebugVsyncEnabled = true;
239}
240
241void EventThread::disableVSync() {
242 mHw.getHwComposer().eventControl(HWComposer::EVENT_VSYNC, false);
243 mDebugVsyncEnabled = false;
244}
245
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800246status_t EventThread::readyToRun() {
Steve Blocka19954a2012-01-04 20:05:49 +0000247 ALOGI("EventThread ready to run.");
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800248 return NO_ERROR;
249}
250
251void EventThread::dump(String8& result, char* buffer, size_t SIZE) const {
252 Mutex::Autolock _l(mLock);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700253 result.appendFormat("VSYNC state: %s\n",
254 mDebugVsyncEnabled?"enabled":"disabled");
255 result.appendFormat(" numListeners=%u,\n events-delivered: %u\n",
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800256 mDisplayEventConnections.size(), mDeliveredEvents);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700257 for (size_t i=0 ; i<mDisplayEventConnections.size() ; i++) {
258 sp<Connection> connection =
259 mDisplayEventConnections.itemAt(i).promote();
260 result.appendFormat(" %p: count=%d\n",
261 connection.get(), connection!=NULL ? connection->count : 0);
262 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800263}
264
265// ---------------------------------------------------------------------------
266
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700267EventThread::Connection::Connection(
268 const sp<EventThread>& eventThread)
269 : count(-1), mEventThread(eventThread), mChannel(new BitTube())
270{
271}
272
273EventThread::Connection::~Connection() {
274 mEventThread->unregisterDisplayEventConnection(this);
275}
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