blob: cc4418648ee6cf6e290bcc35f9042c65266ad6a1 [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 Agopiand0566bc2011-11-17 17:49:17 -080044 mDeliveredEvents(0)
45{
46}
47
48void EventThread::onFirstRef() {
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070049 mHw.setVSyncHandler(this);
Mathias Agopiand0566bc2011-11-17 17:49:17 -080050 run("EventThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
51}
52
Mathias Agopiancb9732a2012-04-03 17:48:03 -070053sp<EventThread::Connection> EventThread::createEventConnection() const {
54 return new Connection(const_cast<EventThread*>(this));
Mathias Agopian8aedd472012-01-24 16:39:14 -080055}
56
57nsecs_t EventThread::getLastVSyncTimestamp() const {
58 Mutex::Autolock _l(mLock);
59 return mLastVSyncTimestamp;
60}
61
62nsecs_t EventThread::getVSyncPeriod() const {
63 return mHw.getRefreshPeriod();
64
65}
66
Mathias Agopiand0566bc2011-11-17 17:49:17 -080067status_t EventThread::registerDisplayEventConnection(
Mathias Agopiancb9732a2012-04-03 17:48:03 -070068 const sp<EventThread::Connection>& connection) {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080069 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070070 mDisplayEventConnections.add(connection);
Mathias Agopiand0566bc2011-11-17 17:49:17 -080071 mCondition.signal();
72 return NO_ERROR;
73}
74
75status_t EventThread::unregisterDisplayEventConnection(
Mathias Agopiancb9732a2012-04-03 17:48:03 -070076 const wp<EventThread::Connection>& connection) {
Mathias Agopiand0566bc2011-11-17 17:49:17 -080077 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070078 mDisplayEventConnections.remove(connection);
Mathias Agopiand0566bc2011-11-17 17:49:17 -080079 mCondition.signal();
80 return NO_ERROR;
81}
82
Mathias Agopian478ae5e2011-12-06 17:22:19 -080083void EventThread::removeDisplayEventConnection(
Mathias Agopiancb9732a2012-04-03 17:48:03 -070084 const wp<EventThread::Connection>& connection) {
Mathias Agopian23748662011-12-05 14:33:34 -080085 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070086 mDisplayEventConnections.remove(connection);
Mathias Agopian478ae5e2011-12-06 17:22:19 -080087}
88
89void EventThread::setVsyncRate(uint32_t count,
Mathias Agopiancb9732a2012-04-03 17:48:03 -070090 const sp<EventThread::Connection>& connection) {
Mathias Agopian478ae5e2011-12-06 17:22:19 -080091 if (int32_t(count) >= 0) { // server must protect against bad params
92 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -070093 const int32_t new_count = (count == 0) ? -1 : count;
94 if (connection->count != new_count) {
95 connection->count = new_count;
96 mCondition.signal();
Mathias Agopian478ae5e2011-12-06 17:22:19 -080097 }
98 }
99}
100
101void EventThread::requestNextVsync(
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700102 const sp<EventThread::Connection>& connection) {
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800103 Mutex::Autolock _l(mLock);
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700104 if (connection->count < 0) {
105 connection->count = 0;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800106 mCondition.signal();
107 }
Mathias Agopian23748662011-12-05 14:33:34 -0800108}
109
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700110void EventThread::onVSyncReceived(int, nsecs_t timestamp) {
111 Mutex::Autolock _l(mLock);
112 mVSyncTimestamp = timestamp;
113 mCondition.signal();
114}
115
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800116bool EventThread::threadLoop() {
117
118 nsecs_t timestamp;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800119 DisplayEventReceiver::Event vsync;
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700120 Vector< wp<EventThread::Connection> > displayEventConnections;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800121
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700122 do {
123
Mathias Agopian23748662011-12-05 14:33:34 -0800124 Mutex::Autolock _l(mLock);
125 do {
Mathias Agopiand94d3b82012-04-08 15:01:31 -0700126 // latch VSYNC event if any
127 timestamp = mVSyncTimestamp;
128 mVSyncTimestamp = 0;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800129
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700130 // check if we should be waiting for VSYNC events
131 bool waitForNextVsync = false;
132 size_t count = mDisplayEventConnections.size();
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800133 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700134 sp<Connection> connection =
135 mDisplayEventConnections.itemAt(i).promote();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700136 if (connection!=0 && connection->count >= 0) {
137 // at least one continuous mode or active one-shot event
138 waitForNextVsync = true;
139 break;
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800140 }
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800141 }
Mathias Agopian23748662011-12-05 14:33:34 -0800142
Mathias Agopiand94d3b82012-04-08 15:01:31 -0700143 if (timestamp) {
144 if (!waitForNextVsync) {
145 // we received a VSYNC but we have no clients
146 // don't report it, and disable VSYNC events
147 mHw.getHwComposer().eventControl(
148 HWComposer::EVENT_VSYNC, false);
149 } 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
160 mHw.getHwComposer().eventControl(
161 HWComposer::EVENT_VSYNC, true);
162 }
163 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700164
165 // wait for something to happen
166 mCondition.wait(mLock);
167 } while(true);
168
169 // process vsync event
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700170 mDeliveredEvents++;
171 mLastVSyncTimestamp = timestamp;
172
173 // now see if we still need to report this VSYNC event
174 const size_t count = mDisplayEventConnections.size();
175 for (size_t i=0 ; i<count ; i++) {
176 bool reportVsync = false;
177 sp<Connection> connection =
178 mDisplayEventConnections.itemAt(i).promote();
179 if (connection == 0)
180 continue;
181
182 const int32_t count = connection->count;
183 if (count >= 1) {
184 if (count==1 || (mDeliveredEvents % count) == 0) {
185 // continuous event, and time to report it
186 reportVsync = true;
187 }
188 } else if (count >= -1) {
189 if (count == 0) {
190 // fired this time around
191 reportVsync = true;
192 }
193 connection->count--;
194 }
195 if (reportVsync) {
196 displayEventConnections.add(connection);
197 }
198 }
199 } while (!displayEventConnections.size());
200
201 // dispatch vsync events to listeners...
202 vsync.header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
203 vsync.header.timestamp = timestamp;
204 vsync.vsync.count = mDeliveredEvents;
Mathias Agopian23748662011-12-05 14:33:34 -0800205
206 const size_t count = displayEventConnections.size();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800207 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700208 sp<Connection> conn(displayEventConnections[i].promote());
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800209 // make sure the connection didn't die
210 if (conn != NULL) {
211 status_t err = conn->postEvent(vsync);
212 if (err == -EAGAIN || err == -EWOULDBLOCK) {
213 // The destination doesn't accept events anymore, it's probably
214 // full. For now, we just drop the events on the floor.
215 // Note that some events cannot be dropped and would have to be
216 // re-sent later. Right-now we don't have the ability to do
217 // this, but it doesn't matter for VSYNC.
218 } else if (err < 0) {
219 // handle any other error on the pipe as fatal. the only
220 // reasonable thing to do is to clean-up this connection.
221 // The most common error we'll get here is -EPIPE.
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800222 removeDisplayEventConnection(displayEventConnections[i]);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800223 }
Mathias Agopian23748662011-12-05 14:33:34 -0800224 } else {
225 // somehow the connection is dead, but we still have it in our list
226 // just clean the list.
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800227 removeDisplayEventConnection(displayEventConnections[i]);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800228 }
229 }
230
Mathias Agopian23748662011-12-05 14:33:34 -0800231 // clear all our references without holding mLock
232 displayEventConnections.clear();
233
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800234 return true;
235}
236
237status_t EventThread::readyToRun() {
Steve Blocka19954a2012-01-04 20:05:49 +0000238 ALOGI("EventThread ready to run.");
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800239 return NO_ERROR;
240}
241
242void EventThread::dump(String8& result, char* buffer, size_t SIZE) const {
243 Mutex::Autolock _l(mLock);
244 result.append("VSYNC state:\n");
245 snprintf(buffer, SIZE, " numListeners=%u, events-delivered: %u\n",
246 mDisplayEventConnections.size(), mDeliveredEvents);
247 result.append(buffer);
248}
249
250// ---------------------------------------------------------------------------
251
Mathias Agopiancb9732a2012-04-03 17:48:03 -0700252EventThread::Connection::Connection(
253 const sp<EventThread>& eventThread)
254 : count(-1), mEventThread(eventThread), mChannel(new BitTube())
255{
256}
257
258EventThread::Connection::~Connection() {
259 mEventThread->unregisterDisplayEventConnection(this);
260}
261
262void EventThread::Connection::onFirstRef() {
263 // NOTE: mEventThread doesn't hold a strong reference on us
264 mEventThread->registerDisplayEventConnection(this);
265}
266
267sp<BitTube> EventThread::Connection::getDataChannel() const {
268 return mChannel;
269}
270
271void EventThread::Connection::setVsyncRate(uint32_t count) {
272 mEventThread->setVsyncRate(count, this);
273}
274
275void EventThread::Connection::requestNextVsync() {
276 mEventThread->requestNextVsync(this);
277}
278
279status_t EventThread::Connection::postEvent(
280 const DisplayEventReceiver::Event& event) {
281 ssize_t size = DisplayEventReceiver::sendEvents(mChannel, &event, 1);
282 return size < 0 ? status_t(size) : status_t(NO_ERROR);
283}
284
285// ---------------------------------------------------------------------------
286
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800287}; // namespace android