blob: 92d4266a8fd9d9321d18a1e106263822e289ddec [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
17#include <stdint.h>
18#include <sys/types.h>
19
20#include <gui/IDisplayEventConnection.h>
21#include <gui/DisplayEventReceiver.h>
22
23#include <utils/Errors.h>
24
25#include "DisplayHardware/DisplayHardware.h"
26#include "DisplayEventConnection.h"
27#include "EventThread.h"
28#include "SurfaceFlinger.h"
29
30// ---------------------------------------------------------------------------
31
32namespace android {
33
34// ---------------------------------------------------------------------------
35
36EventThread::EventThread(const sp<SurfaceFlinger>& flinger)
37 : mFlinger(flinger),
38 mHw(flinger->graphicPlane(0).displayHardware()),
Mathias Agopian8aedd472012-01-24 16:39:14 -080039 mLastVSyncTimestamp(0),
Mathias Agopiand0566bc2011-11-17 17:49:17 -080040 mDeliveredEvents(0)
41{
42}
43
44void EventThread::onFirstRef() {
45 run("EventThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
46}
47
Mathias Agopian8aedd472012-01-24 16:39:14 -080048sp<DisplayEventConnection> EventThread::createEventConnection() const {
49 return new DisplayEventConnection(const_cast<EventThread*>(this));
50}
51
52nsecs_t EventThread::getLastVSyncTimestamp() const {
53 Mutex::Autolock _l(mLock);
54 return mLastVSyncTimestamp;
55}
56
57nsecs_t EventThread::getVSyncPeriod() const {
58 return mHw.getRefreshPeriod();
59
60}
61
Mathias Agopiand0566bc2011-11-17 17:49:17 -080062status_t EventThread::registerDisplayEventConnection(
63 const sp<DisplayEventConnection>& connection) {
64 Mutex::Autolock _l(mLock);
Mathias Agopian478ae5e2011-12-06 17:22:19 -080065 ConnectionInfo info;
66 mDisplayEventConnections.add(connection, info);
Mathias Agopiand0566bc2011-11-17 17:49:17 -080067 mCondition.signal();
68 return NO_ERROR;
69}
70
71status_t EventThread::unregisterDisplayEventConnection(
72 const wp<DisplayEventConnection>& connection) {
73 Mutex::Autolock _l(mLock);
Mathias Agopian478ae5e2011-12-06 17:22:19 -080074 mDisplayEventConnections.removeItem(connection);
Mathias Agopiand0566bc2011-11-17 17:49:17 -080075 mCondition.signal();
76 return NO_ERROR;
77}
78
Mathias Agopian478ae5e2011-12-06 17:22:19 -080079void EventThread::removeDisplayEventConnection(
Mathias Agopian23748662011-12-05 14:33:34 -080080 const wp<DisplayEventConnection>& connection) {
81 Mutex::Autolock _l(mLock);
Mathias Agopian478ae5e2011-12-06 17:22:19 -080082 mDisplayEventConnections.removeItem(connection);
83}
84
85EventThread::ConnectionInfo* EventThread::getConnectionInfoLocked(
86 const wp<DisplayEventConnection>& connection) {
87 ssize_t index = mDisplayEventConnections.indexOfKey(connection);
88 if (index < 0) return NULL;
89 return &mDisplayEventConnections.editValueAt(index);
90}
91
92void EventThread::setVsyncRate(uint32_t count,
93 const wp<DisplayEventConnection>& connection) {
94 if (int32_t(count) >= 0) { // server must protect against bad params
95 Mutex::Autolock _l(mLock);
96 ConnectionInfo* info = getConnectionInfoLocked(connection);
97 if (info) {
Mathias Agopian8aedd472012-01-24 16:39:14 -080098 const int32_t new_count = (count == 0) ? -1 : count;
99 if (info->count != new_count) {
100 info->count = new_count;
101 mCondition.signal();
102 }
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800103 }
104 }
105}
106
107void EventThread::requestNextVsync(
108 const wp<DisplayEventConnection>& connection) {
109 Mutex::Autolock _l(mLock);
110 ConnectionInfo* info = getConnectionInfoLocked(connection);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800111 if (info && info->count < 0) {
112 info->count = 0;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800113 mCondition.signal();
114 }
Mathias Agopian23748662011-12-05 14:33:34 -0800115}
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 Agopian616c0cd2012-01-12 16:13:54 -0800121 Vector< wp<DisplayEventConnection> > displayEventConnections;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800122
Mathias Agopian23748662011-12-05 14:33:34 -0800123 { // scope for the lock
124 Mutex::Autolock _l(mLock);
125 do {
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800126 // see if we need to wait for the VSYNC at all
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800127 do {
128 bool waitForNextVsync = false;
129 size_t count = mDisplayEventConnections.size();
130 for (size_t i=0 ; i<count ; i++) {
131 const ConnectionInfo& info(
132 mDisplayEventConnections.valueAt(i));
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800133 if (info.count >= 0) {
134 // at least one continuous mode or active one-shot event
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800135 waitForNextVsync = true;
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800136 break;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800137 }
138 }
139
140 if (waitForNextVsync)
141 break;
142
Mathias Agopian23748662011-12-05 14:33:34 -0800143 mCondition.wait(mLock);
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800144 } while(true);
Mathias Agopian23748662011-12-05 14:33:34 -0800145
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800146 // at least one listener requested VSYNC
Mathias Agopian23748662011-12-05 14:33:34 -0800147 mLock.unlock();
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800148 timestamp = mHw.waitForRefresh();
Mathias Agopian23748662011-12-05 14:33:34 -0800149 mLock.lock();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800150 mDeliveredEvents++;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800151 mLastVSyncTimestamp = timestamp;
Mathias Agopian23748662011-12-05 14:33:34 -0800152
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800153 // now see if we still need to report this VSYNC event
154 bool reportVsync = false;
155 size_t count = mDisplayEventConnections.size();
156 for (size_t i=0 ; i<count ; i++) {
157 const ConnectionInfo& info(
158 mDisplayEventConnections.valueAt(i));
159 if (info.count >= 1) {
160 if (info.count==1 || (mDeliveredEvents % info.count) == 0) {
161 // continuous event, and time to report it
162 reportVsync = true;
163 }
164 } else if (info.count >= -1) {
165 ConnectionInfo& info(
166 mDisplayEventConnections.editValueAt(i));
167 if (info.count == 0) {
168 // fired this time around
169 reportVsync = true;
170 }
171 info.count--;
172 }
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800173 if (reportVsync) {
174 displayEventConnections.add(mDisplayEventConnections.keyAt(i));
175 }
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800176 }
177
178 if (reportVsync) {
179 break;
180 }
181 } while (true);
Mathias Agopian23748662011-12-05 14:33:34 -0800182
Mathias Agopian23748662011-12-05 14:33:34 -0800183 // dispatch vsync events to listeners...
Mathias Agopian23748662011-12-05 14:33:34 -0800184 vsync.header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
185 vsync.header.timestamp = timestamp;
186 vsync.vsync.count = mDeliveredEvents;
Mathias Agopian23748662011-12-05 14:33:34 -0800187 }
188
189 const size_t count = displayEventConnections.size();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800190 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800191 sp<DisplayEventConnection> conn(displayEventConnections[i].promote());
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800192 // make sure the connection didn't die
193 if (conn != NULL) {
194 status_t err = conn->postEvent(vsync);
195 if (err == -EAGAIN || err == -EWOULDBLOCK) {
196 // The destination doesn't accept events anymore, it's probably
197 // full. For now, we just drop the events on the floor.
198 // Note that some events cannot be dropped and would have to be
199 // re-sent later. Right-now we don't have the ability to do
200 // this, but it doesn't matter for VSYNC.
201 } else if (err < 0) {
202 // handle any other error on the pipe as fatal. the only
203 // reasonable thing to do is to clean-up this connection.
204 // The most common error we'll get here is -EPIPE.
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800205 removeDisplayEventConnection(displayEventConnections[i]);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800206 }
Mathias Agopian23748662011-12-05 14:33:34 -0800207 } else {
208 // somehow the connection is dead, but we still have it in our list
209 // just clean the list.
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800210 removeDisplayEventConnection(displayEventConnections[i]);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800211 }
212 }
213
Mathias Agopian23748662011-12-05 14:33:34 -0800214 // clear all our references without holding mLock
215 displayEventConnections.clear();
216
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800217 return true;
218}
219
220status_t EventThread::readyToRun() {
Steve Blocka19954a2012-01-04 20:05:49 +0000221 ALOGI("EventThread ready to run.");
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800222 return NO_ERROR;
223}
224
225void EventThread::dump(String8& result, char* buffer, size_t SIZE) const {
226 Mutex::Autolock _l(mLock);
227 result.append("VSYNC state:\n");
228 snprintf(buffer, SIZE, " numListeners=%u, events-delivered: %u\n",
229 mDisplayEventConnections.size(), mDeliveredEvents);
230 result.append(buffer);
231}
232
233// ---------------------------------------------------------------------------
234
235}; // namespace android