From abe815dd6978b718c04f6e22e1a893d2b51d11a1 Mon Sep 17 00:00:00 2001 From: Mathias Agopian Date: Tue, 19 Mar 2013 22:22:21 -0700 Subject: rework how we take screenshots for a CPU consumer We're not using IMemoryHeap as a transport anymore, instead we're providing a CpuConsumer and use the IGraphicBufferProducer version of the screenshot API. However, some GPU drivers don't support properly a GPU to CPU path, to work around this, we use a temporary BufferQueue on the server side for the GL rendering, and we use glReadPixels into the CpuConsumer (we're now using a CPU to CPU path which is always supported). Currently this "wrapping" is always performed, but it can be bypassed on devices that support the GPU to CPU path. This also addresses a DoS attack vector on SurfaceFlinger, where an application could consume all of SF's filedescriptors by creating a lot of screenshots in a row. Bug: 8390553 Change-Id: I9e81514c2a7711b9bb393f74305be7d2abe08f1c --- libs/gui/SurfaceComposerClient.cpp | 78 ++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 29 deletions(-) (limited to 'libs/gui/SurfaceComposerClient.cpp') diff --git a/libs/gui/SurfaceComposerClient.cpp b/libs/gui/SurfaceComposerClient.cpp index 4a4c0c8df4..ec46fce403 100644 --- a/libs/gui/SurfaceComposerClient.cpp +++ b/libs/gui/SurfaceComposerClient.cpp @@ -31,6 +31,7 @@ #include +#include #include #include #include @@ -617,69 +618,88 @@ status_t ScreenshotClient::capture( sp s(ComposerService::getComposerService()); if (s == NULL) return NO_INIT; return s->captureScreen(display, producer, - reqWidth, reqHeight, minLayerZ, maxLayerZ); + reqWidth, reqHeight, minLayerZ, maxLayerZ, + false); } ScreenshotClient::ScreenshotClient() - : mWidth(0), mHeight(0), mFormat(PIXEL_FORMAT_NONE) { + : mHaveBuffer(false) { + memset(&mBuffer, 0, sizeof(mBuffer)); } -status_t ScreenshotClient::update(const sp& display) { - sp s(ComposerService::getComposerService()); - if (s == NULL) return NO_INIT; - mHeap = 0; - return s->captureScreen(display, &mHeap, - &mWidth, &mHeight, &mFormat, 0, 0, - 0, -1UL); +sp ScreenshotClient::getCpuConsumer() const { + if (mCpuConsumer == NULL) { + mCpuConsumer = new CpuConsumer(1); + mCpuConsumer->setName(String8("ScreenshotClient")); + } + return mCpuConsumer; } status_t ScreenshotClient::update(const sp& display, - uint32_t reqWidth, uint32_t reqHeight) { + uint32_t reqWidth, uint32_t reqHeight, + uint32_t minLayerZ, uint32_t maxLayerZ) { sp s(ComposerService::getComposerService()); if (s == NULL) return NO_INIT; - mHeap = 0; - return s->captureScreen(display, &mHeap, - &mWidth, &mHeight, &mFormat, reqWidth, reqHeight, - 0, -1UL); + sp cpuConsumer = getCpuConsumer(); + + if (mHaveBuffer) { + mCpuConsumer->unlockBuffer(mBuffer); + memset(&mBuffer, 0, sizeof(mBuffer)); + mHaveBuffer = false; + } + + status_t err = s->captureScreen(display,cpuConsumer->getBufferQueue(), + reqWidth, reqHeight, minLayerZ, maxLayerZ, true); + + if (err == NO_ERROR) { + err = mCpuConsumer->lockNextBuffer(&mBuffer); + if (err == NO_ERROR) { + mHaveBuffer = true; + } + } + return err; +} + +status_t ScreenshotClient::update(const sp& display) { + return ScreenshotClient::update(display, 0, 0, 0, -1UL); } status_t ScreenshotClient::update(const sp& display, - uint32_t reqWidth, uint32_t reqHeight, - uint32_t minLayerZ, uint32_t maxLayerZ) { - sp s(ComposerService::getComposerService()); - if (s == NULL) return NO_INIT; - mHeap = 0; - return s->captureScreen(display, &mHeap, - &mWidth, &mHeight, &mFormat, reqWidth, reqHeight, - minLayerZ, maxLayerZ); + uint32_t reqWidth, uint32_t reqHeight) { + return ScreenshotClient::update(display, reqWidth, reqHeight, 0, -1UL); } void ScreenshotClient::release() { - mHeap = 0; + if (mHaveBuffer) { + mCpuConsumer->unlockBuffer(mBuffer); + memset(&mBuffer, 0, sizeof(mBuffer)); + mHaveBuffer = false; + } + mCpuConsumer.clear(); } void const* ScreenshotClient::getPixels() const { - return mHeap->getBase(); + return mBuffer.data; } uint32_t ScreenshotClient::getWidth() const { - return mWidth; + return mBuffer.width; } uint32_t ScreenshotClient::getHeight() const { - return mHeight; + return mBuffer.height; } PixelFormat ScreenshotClient::getFormat() const { - return mFormat; + return mBuffer.format; } uint32_t ScreenshotClient::getStride() const { - return mWidth; + return mBuffer.stride; } size_t ScreenshotClient::getSize() const { - return mHeap->getSize(); + return mBuffer.stride * mBuffer.height * bytesPerPixel(mBuffer.format); } // ---------------------------------------------------------------------------- -- cgit v1.2.3-59-g8ed1b