From 0e9c51ec78df650e2a340978ccce920d419a9fa1 Mon Sep 17 00:00:00 2001 From: Andy McFadden Date: Wed, 9 Oct 2013 16:38:02 -0700 Subject: [PATCH] Fix blank / partial screenshots The screen capture code wasn't waiting for the render to finish, so sometimes you'd see an empty or partial image. Bug 11131777 Change-Id: Ic64087322ce3bb15bb5f4fb1eb07579880fe6197 --- services/surfaceflinger/SurfaceFlinger.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp index 3733ede88..4a1373e88 100644 --- a/services/surfaceflinger/SurfaceFlinger.cpp +++ b/services/surfaceflinger/SurfaceFlinger.cpp @@ -2982,6 +2982,27 @@ status_t SurfaceFlinger::captureScreenImplLocked( renderScreenImplLocked(hw, reqWidth, reqHeight, minLayerZ, maxLayerZ, true); + // Create a sync point and wait on it, so we know the buffer is + // ready before we pass it along. We can't trivially call glFlush(), + // so we use a wait flag instead. + // TODO: pass a sync fd to queueBuffer() and let the consumer wait. + EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL); + if (sync != EGL_NO_SYNC_KHR) { + EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync, + EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/); + EGLint eglErr = eglGetError(); + eglDestroySyncKHR(mEGLDisplay, sync); + if (result == EGL_TIMEOUT_EXPIRED_KHR) { + ALOGW("captureScreen: fence wait timed out"); + } else { + ALOGW_IF(eglErr != EGL_SUCCESS, + "captureScreen: error waiting on EGL fence: %#x", eglErr); + } + } else { + ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError()); + // not fatal + } + if (DEBUG_SCREENSHOTS) { uint32_t* pixels = new uint32_t[reqWidth*reqHeight]; getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);