blob: 6796d7dee2a71f3adb98575c7c94172a0fa74710 [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()),
39 mDeliveredEvents(0)
40{
41}
42
43void EventThread::onFirstRef() {
44 run("EventThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
45}
46
47status_t EventThread::registerDisplayEventConnection(
48 const sp<DisplayEventConnection>& connection) {
49 Mutex::Autolock _l(mLock);
Mathias Agopian478ae5e2011-12-06 17:22:19 -080050 ConnectionInfo info;
51 mDisplayEventConnections.add(connection, info);
Mathias Agopiand0566bc2011-11-17 17:49:17 -080052 mCondition.signal();
53 return NO_ERROR;
54}
55
56status_t EventThread::unregisterDisplayEventConnection(
57 const wp<DisplayEventConnection>& connection) {
58 Mutex::Autolock _l(mLock);
Mathias Agopian478ae5e2011-12-06 17:22:19 -080059 mDisplayEventConnections.removeItem(connection);
Mathias Agopiand0566bc2011-11-17 17:49:17 -080060 mCondition.signal();
61 return NO_ERROR;
62}
63
Mathias Agopian478ae5e2011-12-06 17:22:19 -080064void EventThread::removeDisplayEventConnection(
Mathias Agopian23748662011-12-05 14:33:34 -080065 const wp<DisplayEventConnection>& connection) {
66 Mutex::Autolock _l(mLock);
Mathias Agopian478ae5e2011-12-06 17:22:19 -080067 mDisplayEventConnections.removeItem(connection);
68}
69
70EventThread::ConnectionInfo* EventThread::getConnectionInfoLocked(
71 const wp<DisplayEventConnection>& connection) {
72 ssize_t index = mDisplayEventConnections.indexOfKey(connection);
73 if (index < 0) return NULL;
74 return &mDisplayEventConnections.editValueAt(index);
75}
76
77void EventThread::setVsyncRate(uint32_t count,
78 const wp<DisplayEventConnection>& connection) {
79 if (int32_t(count) >= 0) { // server must protect against bad params
80 Mutex::Autolock _l(mLock);
81 ConnectionInfo* info = getConnectionInfoLocked(connection);
82 if (info) {
83 info->count = (count == 0) ? -1 : count;
84 mCondition.signal();
85 }
86 }
87}
88
89void EventThread::requestNextVsync(
90 const wp<DisplayEventConnection>& connection) {
91 Mutex::Autolock _l(mLock);
92 ConnectionInfo* info = getConnectionInfoLocked(connection);
93 if (info) {
94 if (info->count < 0) {
95 info->count = 0;
96 }
97 mCondition.signal();
98 }
Mathias Agopian23748662011-12-05 14:33:34 -080099}
100
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800101bool EventThread::threadLoop() {
102
103 nsecs_t timestamp;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800104 DisplayEventReceiver::Event vsync;
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800105 Vector< wp<DisplayEventConnection> > displayEventConnections;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800106
Mathias Agopian23748662011-12-05 14:33:34 -0800107 { // scope for the lock
108 Mutex::Autolock _l(mLock);
109 do {
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800110 // see if we need to wait for the VSYNC at all
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800111 do {
112 bool waitForNextVsync = false;
113 size_t count = mDisplayEventConnections.size();
114 for (size_t i=0 ; i<count ; i++) {
115 const ConnectionInfo& info(
116 mDisplayEventConnections.valueAt(i));
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800117 if (info.count >= 0) {
118 // at least one continuous mode or active one-shot event
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800119 waitForNextVsync = true;
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800120 break;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800121 }
122 }
123
124 if (waitForNextVsync)
125 break;
126
Mathias Agopian23748662011-12-05 14:33:34 -0800127 mCondition.wait(mLock);
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800128 } while(true);
Mathias Agopian23748662011-12-05 14:33:34 -0800129
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800130 // at least one listener requested VSYNC
Mathias Agopian23748662011-12-05 14:33:34 -0800131 mLock.unlock();
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800132 timestamp = mHw.waitForRefresh();
Mathias Agopian23748662011-12-05 14:33:34 -0800133 mLock.lock();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800134 mDeliveredEvents++;
Mathias Agopian23748662011-12-05 14:33:34 -0800135
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800136 // now see if we still need to report this VSYNC event
137 bool reportVsync = false;
138 size_t count = mDisplayEventConnections.size();
139 for (size_t i=0 ; i<count ; i++) {
140 const ConnectionInfo& info(
141 mDisplayEventConnections.valueAt(i));
142 if (info.count >= 1) {
143 if (info.count==1 || (mDeliveredEvents % info.count) == 0) {
144 // continuous event, and time to report it
145 reportVsync = true;
146 }
147 } else if (info.count >= -1) {
148 ConnectionInfo& info(
149 mDisplayEventConnections.editValueAt(i));
150 if (info.count == 0) {
151 // fired this time around
152 reportVsync = true;
153 }
154 info.count--;
155 }
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800156 if (reportVsync) {
157 displayEventConnections.add(mDisplayEventConnections.keyAt(i));
158 }
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800159 }
160
161 if (reportVsync) {
162 break;
163 }
164 } while (true);
Mathias Agopian23748662011-12-05 14:33:34 -0800165
Mathias Agopian23748662011-12-05 14:33:34 -0800166 // dispatch vsync events to listeners...
Mathias Agopian23748662011-12-05 14:33:34 -0800167 vsync.header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
168 vsync.header.timestamp = timestamp;
169 vsync.vsync.count = mDeliveredEvents;
Mathias Agopian23748662011-12-05 14:33:34 -0800170 }
171
172 const size_t count = displayEventConnections.size();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800173 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800174 sp<DisplayEventConnection> conn(displayEventConnections[i].promote());
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800175 // make sure the connection didn't die
176 if (conn != NULL) {
177 status_t err = conn->postEvent(vsync);
178 if (err == -EAGAIN || err == -EWOULDBLOCK) {
179 // The destination doesn't accept events anymore, it's probably
180 // full. For now, we just drop the events on the floor.
181 // Note that some events cannot be dropped and would have to be
182 // re-sent later. Right-now we don't have the ability to do
183 // this, but it doesn't matter for VSYNC.
184 } else if (err < 0) {
185 // handle any other error on the pipe as fatal. the only
186 // reasonable thing to do is to clean-up this connection.
187 // The most common error we'll get here is -EPIPE.
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800188 removeDisplayEventConnection(displayEventConnections[i]);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800189 }
Mathias Agopian23748662011-12-05 14:33:34 -0800190 } else {
191 // somehow the connection is dead, but we still have it in our list
192 // just clean the list.
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800193 removeDisplayEventConnection(displayEventConnections[i]);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800194 }
195 }
196
Mathias Agopian23748662011-12-05 14:33:34 -0800197 // clear all our references without holding mLock
198 displayEventConnections.clear();
199
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800200 return true;
201}
202
203status_t EventThread::readyToRun() {
Steve Blocka19954a2012-01-04 20:05:49 +0000204 ALOGI("EventThread ready to run.");
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800205 return NO_ERROR;
206}
207
208void EventThread::dump(String8& result, char* buffer, size_t SIZE) const {
209 Mutex::Autolock _l(mLock);
210 result.append("VSYNC state:\n");
211 snprintf(buffer, SIZE, " numListeners=%u, events-delivered: %u\n",
212 mDisplayEventConnections.size(), mDeliveredEvents);
213 result.append(buffer);
214}
215
216// ---------------------------------------------------------------------------
217
218}; // namespace android