Skip to content

Commit bec0a86

Browse files
author
Jeff Brown
committed
Refactor DisplayEventReceiver read loop.
Change-Id: I98ef802ec0ca48f768e3b0920e1b4b4f7f141050
1 parent ebb2d8d commit bec0a86

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

core/jni/android_view_DisplayEventReceiver.cpp

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ class NativeDisplayEventReceiver : public RefBase {
4949

5050
status_t initialize();
5151
status_t scheduleVsync();
52-
static int handleReceiveCallback(int receiveFd, int events, void* data);
5352

5453
protected:
5554
virtual ~NativeDisplayEventReceiver();
@@ -59,6 +58,9 @@ class NativeDisplayEventReceiver : public RefBase {
5958
sp<Looper> mLooper;
6059
DisplayEventReceiver mReceiver;
6160
bool mWaitingForVsync;
61+
62+
static int handleReceiveCallback(int receiveFd, int events, void* data);
63+
bool readLastVsyncMessage(nsecs_t* outTimestamp, uint32_t* outCount);
6264
};
6365

6466

@@ -100,16 +102,9 @@ status_t NativeDisplayEventReceiver::scheduleVsync() {
100102
ALOGV("receiver %p ~ Scheduling vsync.", this);
101103

102104
// Drain all pending events.
103-
DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
104-
ssize_t n;
105-
while ((n = mReceiver.getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
106-
ALOGV("receiver %p ~ Drained %d events.", this, int(n));
107-
}
108-
109-
if (n < 0) {
110-
ALOGW("Failed to drain events from display event receiver, status=%d", status_t(n));
111-
return status_t(n);
112-
}
105+
nsecs_t vsyncTimestamp;
106+
uint32_t vsyncCount;
107+
readLastVsyncMessage(&vsyncTimestamp, &vsyncCount);
113108

114109
status_t status = mReceiver.requestNextVsync();
115110
if (status) {
@@ -138,23 +133,9 @@ int NativeDisplayEventReceiver::handleReceiveCallback(int receiveFd, int events,
138133
}
139134

140135
// Drain all pending events, keep the last vsync.
141-
nsecs_t vsyncTimestamp = -1;
142-
uint32_t vsyncCount = 0;
143-
144-
DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
145-
ssize_t n;
146-
while ((n = r->mReceiver.getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
147-
ALOGV("receiver %p ~ Read %d events.", data, int(n));
148-
while (n-- > 0) {
149-
if (buf[n].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
150-
vsyncTimestamp = buf[n].header.timestamp;
151-
vsyncCount = buf[n].vsync.count;
152-
break; // stop at last vsync in the buffer
153-
}
154-
}
155-
}
156-
157-
if (vsyncTimestamp < 0) {
136+
nsecs_t vsyncTimestamp;
137+
uint32_t vsyncCount;
138+
if (!r->readLastVsyncMessage(&vsyncTimestamp, &vsyncCount)) {
158139
ALOGV("receiver %p ~ Woke up but there was no vsync pulse!", data);
159140
return 1; // keep the callback, did not obtain a vsync pulse
160141
}
@@ -179,6 +160,26 @@ int NativeDisplayEventReceiver::handleReceiveCallback(int receiveFd, int events,
179160
return 1; // keep the callback
180161
}
181162

163+
bool NativeDisplayEventReceiver::readLastVsyncMessage(
164+
nsecs_t* outTimestamp, uint32_t* outCount) {
165+
DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
166+
ssize_t n;
167+
while ((n = mReceiver.getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
168+
ALOGV("receiver %p ~ Read %d events.", this, int(n));
169+
while (n-- > 0) {
170+
if (buf[n].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
171+
*outTimestamp = buf[n].header.timestamp;
172+
*outCount = buf[n].vsync.count;
173+
return true; // stop at last vsync in the buffer
174+
}
175+
}
176+
}
177+
if (n < 0) {
178+
ALOGW("Failed to get events from display event receiver, status=%d", status_t(n));
179+
}
180+
return false;
181+
}
182+
182183

183184
static jint nativeInit(JNIEnv* env, jclass clazz, jobject receiverObj,
184185
jobject messageQueueObj) {

0 commit comments

Comments
 (0)