1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdint.h>
#include <sys/types.h>
#include <gui/IDisplayEventConnection.h>
#include <gui/DisplayEventReceiver.h>
#include <utils/Errors.h>
#include "DisplayHardware/DisplayHardware.h"
#include "DisplayEventConnection.h"
#include "EventThread.h"
#include "SurfaceFlinger.h"
// ---------------------------------------------------------------------------
namespace android {
// ---------------------------------------------------------------------------
EventThread::EventThread(const sp<SurfaceFlinger>& flinger)
: mFlinger(flinger),
mHw(flinger->graphicPlane(0).displayHardware()),
mDeliveredEvents(0)
{
}
void EventThread::onFirstRef() {
run("EventThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
}
status_t EventThread::registerDisplayEventConnection(
const sp<DisplayEventConnection>& connection) {
Mutex::Autolock _l(mLock);
ConnectionInfo info;
mDisplayEventConnections.add(connection, info);
mCondition.signal();
return NO_ERROR;
}
status_t EventThread::unregisterDisplayEventConnection(
const wp<DisplayEventConnection>& connection) {
Mutex::Autolock _l(mLock);
mDisplayEventConnections.removeItem(connection);
mCondition.signal();
return NO_ERROR;
}
void EventThread::removeDisplayEventConnection(
const wp<DisplayEventConnection>& connection) {
Mutex::Autolock _l(mLock);
mDisplayEventConnections.removeItem(connection);
}
EventThread::ConnectionInfo* EventThread::getConnectionInfoLocked(
const wp<DisplayEventConnection>& connection) {
ssize_t index = mDisplayEventConnections.indexOfKey(connection);
if (index < 0) return NULL;
return &mDisplayEventConnections.editValueAt(index);
}
void EventThread::setVsyncRate(uint32_t count,
const wp<DisplayEventConnection>& connection) {
if (int32_t(count) >= 0) { // server must protect against bad params
Mutex::Autolock _l(mLock);
ConnectionInfo* info = getConnectionInfoLocked(connection);
if (info) {
info->count = (count == 0) ? -1 : count;
mCondition.signal();
}
}
}
void EventThread::requestNextVsync(
const wp<DisplayEventConnection>& connection) {
Mutex::Autolock _l(mLock);
ConnectionInfo* info = getConnectionInfoLocked(connection);
if (info) {
if (info->count < 0) {
info->count = 0;
}
mCondition.signal();
}
}
bool EventThread::threadLoop() {
nsecs_t timestamp;
DisplayEventReceiver::Event vsync;
Vector< wp<DisplayEventConnection> > displayEventConnections;
{ // scope for the lock
Mutex::Autolock _l(mLock);
do {
// see if we need to wait for the VSYNC at all
do {
bool waitForNextVsync = false;
size_t count = mDisplayEventConnections.size();
for (size_t i=0 ; i<count ; i++) {
const ConnectionInfo& info(
mDisplayEventConnections.valueAt(i));
if (info.count >= 0) {
// at least one continuous mode or active one-shot event
waitForNextVsync = true;
break;
}
}
if (waitForNextVsync)
break;
mCondition.wait(mLock);
} while(true);
// at least one listener requested VSYNC
mLock.unlock();
timestamp = mHw.waitForRefresh();
mLock.lock();
mDeliveredEvents++;
// now see if we still need to report this VSYNC event
bool reportVsync = false;
size_t count = mDisplayEventConnections.size();
for (size_t i=0 ; i<count ; i++) {
const ConnectionInfo& info(
mDisplayEventConnections.valueAt(i));
if (info.count >= 1) {
if (info.count==1 || (mDeliveredEvents % info.count) == 0) {
// continuous event, and time to report it
reportVsync = true;
}
} else if (info.count >= -1) {
ConnectionInfo& info(
mDisplayEventConnections.editValueAt(i));
if (info.count == 0) {
// fired this time around
reportVsync = true;
}
info.count--;
}
if (reportVsync) {
displayEventConnections.add(mDisplayEventConnections.keyAt(i));
}
}
if (reportVsync) {
break;
}
} while (true);
// dispatch vsync events to listeners...
vsync.header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
vsync.header.timestamp = timestamp;
vsync.vsync.count = mDeliveredEvents;
}
const size_t count = displayEventConnections.size();
for (size_t i=0 ; i<count ; i++) {
sp<DisplayEventConnection> conn(displayEventConnections[i].promote());
// make sure the connection didn't die
if (conn != NULL) {
status_t err = conn->postEvent(vsync);
if (err == -EAGAIN || err == -EWOULDBLOCK) {
// The destination doesn't accept events anymore, it's probably
// full. For now, we just drop the events on the floor.
// Note that some events cannot be dropped and would have to be
// re-sent later. Right-now we don't have the ability to do
// this, but it doesn't matter for VSYNC.
} else if (err < 0) {
// handle any other error on the pipe as fatal. the only
// reasonable thing to do is to clean-up this connection.
// The most common error we'll get here is -EPIPE.
removeDisplayEventConnection(displayEventConnections[i]);
}
} else {
// somehow the connection is dead, but we still have it in our list
// just clean the list.
removeDisplayEventConnection(displayEventConnections[i]);
}
}
// clear all our references without holding mLock
displayEventConnections.clear();
return true;
}
status_t EventThread::readyToRun() {
ALOGI("EventThread ready to run.");
return NO_ERROR;
}
void EventThread::dump(String8& result, char* buffer, size_t SIZE) const {
Mutex::Autolock _l(mLock);
result.append("VSYNC state:\n");
snprintf(buffer, SIZE, " numListeners=%u, events-delivered: %u\n",
mDisplayEventConnections.size(), mDeliveredEvents);
result.append(buffer);
}
// ---------------------------------------------------------------------------
}; // namespace android
|