Skip to content

Commit

Permalink
Add back support for glReadPixels screenshot path.
Browse files Browse the repository at this point in the history
Thanks to @rmcc and @Hashcode for doing this with KitKat, I just adapted it to Lollipop here.
[hashcode0f@gmail.com: adjusted for Android N.  Device must set:
TARGET_FORCE_SCREENSHOT_CPU_PATH := true]

Change-Id: I687b28487690bf512731aa5ef531d7bf8e62b3cd
  • Loading branch information
MWisBest authored and andi34 committed Sep 17, 2016
1 parent 500ea81 commit a6d425f
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 54 deletions.
9 changes: 8 additions & 1 deletion include/gui/ISurfaceComposer.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
#include <gui/IGraphicBufferAlloc.h>
#include <gui/ISurfaceComposerClient.h>

#ifndef FORCE_SCREENSHOT_CPU_PATH
#define SS_CPU_CONSUMER false
#else
#define SS_CPU_CONSUMER true
#endif

namespace android {
// ----------------------------------------------------------------------------

Expand Down Expand Up @@ -145,7 +151,8 @@ class ISurfaceComposer: public IInterface {
Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
uint32_t minLayerZ, uint32_t maxLayerZ,
bool useIdentityTransform,
Rotation rotation = eRotateNone) = 0;
Rotation rotation = eRotateNone,
bool isCpuConsumer = SS_CPU_CONSUMER) = 0;

/* Clears the frame statistics for animations.
*
Expand Down
3 changes: 3 additions & 0 deletions libs/gui/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ LOCAL_SHARED_LIBRARIES := \
libutils \
liblog

ifeq ($(TARGET_FORCE_SCREENSHOT_CPU_PATH),true)
LOCAL_CFLAGS += -DFORCE_SCREENSHOT_CPU_PATH
endif

LOCAL_MODULE := libgui

Expand Down
9 changes: 6 additions & 3 deletions libs/gui/ISurfaceComposer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ class BpSurfaceComposer : public BpInterface<ISurfaceComposer>
Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
uint32_t minLayerZ, uint32_t maxLayerZ,
bool useIdentityTransform,
ISurfaceComposer::Rotation rotation)
ISurfaceComposer::Rotation rotation,
bool isCpuConsumer)
{
Parcel data, reply;
data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
Expand All @@ -117,6 +118,7 @@ class BpSurfaceComposer : public BpInterface<ISurfaceComposer>
data.writeUint32(maxLayerZ);
data.writeInt32(static_cast<int32_t>(useIdentityTransform));
data.writeInt32(static_cast<int32_t>(rotation));
data.writeInt32(isCpuConsumer);
remote()->transact(BnSurfaceComposer::CAPTURE_SCREEN, data, &reply);
return reply.readInt32();
}
Expand Down Expand Up @@ -384,11 +386,12 @@ status_t BnSurfaceComposer::onTransact(
uint32_t maxLayerZ = data.readUint32();
bool useIdentityTransform = static_cast<bool>(data.readInt32());
int32_t rotation = data.readInt32();

bool isCpuConsumer = data.readInt32();
status_t res = captureScreen(display, producer,
sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
useIdentityTransform,
static_cast<ISurfaceComposer::Rotation>(rotation));
static_cast<ISurfaceComposer::Rotation>(rotation),
isCpuConsumer);
reply->writeInt32(res);
return NO_ERROR;
}
Expand Down
3 changes: 3 additions & 0 deletions services/surfaceflinger/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ else
DisplayHardware/HWComposer_hwc1.cpp
endif

ifeq ($(TARGET_FORCE_SCREENSHOT_CPU_PATH),true)
LOCAL_CFLAGS += -DFORCE_SCREENSHOT_CPU_PATH
endif
ifeq ($(TARGET_BOARD_PLATFORM),omap4)
LOCAL_CFLAGS += -DHAS_CONTEXT_PRIORITY
endif
Expand Down
44 changes: 30 additions & 14 deletions services/surfaceflinger/RenderEngine/GLES11RenderEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,28 +239,44 @@ void GLES11RenderEngine::disableBlending() {
}

void GLES11RenderEngine::bindImageAsFramebuffer(EGLImageKHR image,
uint32_t* texName, uint32_t* fbName, uint32_t* status) {
uint32_t* texName, uint32_t* fbName, uint32_t* status,
bool useReadPixels, int reqWidth, int reqHeight) {
GLuint tname, name;
// turn our EGLImage into a texture
glGenTextures(1, &tname);
glBindTexture(GL_TEXTURE_2D, tname);
glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);

// create a Framebuffer Object to render into
glGenFramebuffersOES(1, &name);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);

if (!useReadPixels) {
// turn our EGLImage into a texture
glGenTextures(1, &tname);
glBindTexture(GL_TEXTURE_2D, tname);
glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);

// create a Framebuffer Object to render into
glGenFramebuffersOES(1, &name);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
} else {
// since we're going to use glReadPixels() anyways,
// use an intermediate renderbuffer instead
glGenRenderbuffersOES(1, &tname);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, reqWidth, reqHeight);
// create a FBO to render into
glGenFramebuffersOES(1, &name);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
}
*status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
*texName = tname;
*fbName = name;
}

void GLES11RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) {
void GLES11RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName,
bool useReadPixels) {
glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
glDeleteFramebuffersOES(1, &fbName);
glDeleteTextures(1, &texName);
if (!useReadPixels)
glDeleteTextures(1, &texName);
else
glDeleteRenderbuffersOES(1, &texName);
}

void GLES11RenderEngine::setupFillWithColor(float r, float g, float b, float a) {
Expand Down
5 changes: 3 additions & 2 deletions services/surfaceflinger/RenderEngine/GLES11RenderEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ class GLES11RenderEngine : public RenderEngine {
GLint mMaxTextureSize;

virtual void bindImageAsFramebuffer(EGLImageKHR image,
uint32_t* texName, uint32_t* fbName, uint32_t* status);
virtual void unbindFramebuffer(uint32_t texName, uint32_t fbName);
uint32_t* texName, uint32_t* fbName, uint32_t* status,
bool useReadPixels, int reqWidth, int reqHeight);
virtual void unbindFramebuffer(uint32_t texName, uint32_t fbName, bool useReadPixels);

public:
GLES11RenderEngine();
Expand Down
41 changes: 29 additions & 12 deletions services/surfaceflinger/RenderEngine/GLES20RenderEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,27 +208,44 @@ void GLES20RenderEngine::disableBlending() {


void GLES20RenderEngine::bindImageAsFramebuffer(EGLImageKHR image,
uint32_t* texName, uint32_t* fbName, uint32_t* status) {
uint32_t* texName, uint32_t* fbName, uint32_t* status,
bool useReadPixels, int reqWidth, int reqHeight) {
GLuint tname, name;
// turn our EGLImage into a texture
glGenTextures(1, &tname);
glBindTexture(GL_TEXTURE_2D, tname);
glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);

// create a Framebuffer Object to render into
glGenFramebuffers(1, &name);
glBindFramebuffer(GL_FRAMEBUFFER, name);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0);
if (!useReadPixels) {
// turn our EGLImage into a texture
glGenTextures(1, &tname);
glBindTexture(GL_TEXTURE_2D, tname);
glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);

// create a Framebuffer Object to render into
glGenFramebuffers(1, &name);
glBindFramebuffer(GL_FRAMEBUFFER, name);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0);
} else {
// since we're going to use glReadPixels() anyways,
// use an intermediate renderbuffer instead
glGenRenderbuffers(1, &tname);
glBindRenderbuffer(GL_RENDERBUFFER, tname);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8_OES, reqWidth, reqHeight);
// create a FBO to render into
glGenFramebuffers(1, &name);
glBindFramebuffer(GL_FRAMEBUFFER, name);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, tname);
}

*status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
*texName = tname;
*fbName = name;
}

void GLES20RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) {
void GLES20RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName,
bool useReadPixels) {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDeleteFramebuffers(1, &fbName);
glDeleteTextures(1, &texName);
if (!useReadPixels)
glDeleteTextures(1, &texName);
else
glDeleteRenderbuffers(1, &texName);
}

void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) {
Expand Down
5 changes: 3 additions & 2 deletions services/surfaceflinger/RenderEngine/GLES20RenderEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ class GLES20RenderEngine : public RenderEngine {
Vector<Group> mGroupStack;

virtual void bindImageAsFramebuffer(EGLImageKHR image,
uint32_t* texName, uint32_t* fbName, uint32_t* status);
virtual void unbindFramebuffer(uint32_t texName, uint32_t fbName);
uint32_t* texName, uint32_t* fbName, uint32_t* status,
bool useReadPixels, int reqWidth, int reqHeight);
virtual void unbindFramebuffer(uint32_t texName, uint32_t fbName, bool useReadPixels);

public:
GLES20RenderEngine();
Expand Down
8 changes: 5 additions & 3 deletions services/surfaceflinger/RenderEngine/RenderEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,17 +261,19 @@ void RenderEngine::dump(String8& result) {
// ---------------------------------------------------------------------------

RenderEngine::BindImageAsFramebuffer::BindImageAsFramebuffer(
RenderEngine& engine, EGLImageKHR image) : mEngine(engine)
RenderEngine& engine, EGLImageKHR image, bool useReadPixels,
int reqWidth, int reqHeight) : mEngine(engine), mUseReadPixels(useReadPixels)
{
mEngine.bindImageAsFramebuffer(image, &mTexName, &mFbName, &mStatus);
mEngine.bindImageAsFramebuffer(image, &mTexName, &mFbName, &mStatus,
useReadPixels, reqWidth, reqHeight);

ALOGE_IF(mStatus != GL_FRAMEBUFFER_COMPLETE_OES,
"glCheckFramebufferStatusOES error %d", mStatus);
}

RenderEngine::BindImageAsFramebuffer::~BindImageAsFramebuffer() {
// back to main framebuffer
mEngine.unbindFramebuffer(mTexName, mFbName);
mEngine.unbindFramebuffer(mTexName, mFbName, mUseReadPixels);
}

status_t RenderEngine::BindImageAsFramebuffer::getStatus() const {
Expand Down
7 changes: 4 additions & 3 deletions services/surfaceflinger/RenderEngine/RenderEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class RenderEngine {
EGLContext mEGLContext;
void setEGLHandles(EGLConfig config, EGLContext ctxt);

virtual void bindImageAsFramebuffer(EGLImageKHR image, uint32_t* texName, uint32_t* fbName, uint32_t* status) = 0;
virtual void unbindFramebuffer(uint32_t texName, uint32_t fbName) = 0;
virtual void bindImageAsFramebuffer(EGLImageKHR image, uint32_t* texName, uint32_t* fbName, uint32_t* status, bool useReadPixels, int reqWidth, int reqHeight) = 0;
virtual void unbindFramebuffer(uint32_t texName, uint32_t fbName, bool useReadPixels) = 0;

protected:
RenderEngine();
Expand Down Expand Up @@ -83,8 +83,9 @@ class RenderEngine {
RenderEngine& mEngine;
uint32_t mTexName, mFbName;
uint32_t mStatus;
bool mUseReadPixels;
public:
BindImageAsFramebuffer(RenderEngine& engine, EGLImageKHR image);
BindImageAsFramebuffer(RenderEngine& engine, EGLImageKHR image, bool useReadPixels, int reqWidth, int reqHeight);
~BindImageAsFramebuffer();
int getStatus() const;
};
Expand Down
26 changes: 20 additions & 6 deletions services/surfaceflinger/SurfaceFlinger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3213,7 +3213,8 @@ status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
const sp<IGraphicBufferProducer>& producer,
Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
uint32_t minLayerZ, uint32_t maxLayerZ,
bool useIdentityTransform, ISurfaceComposer::Rotation rotation) {
bool useIdentityTransform, ISurfaceComposer::Rotation rotation,
bool useReadPixels) {

if (CC_UNLIKELY(display == 0))
return BAD_VALUE;
Expand Down Expand Up @@ -3257,6 +3258,7 @@ status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
bool useIdentityTransform;
Transform::orientation_flags rotation;
status_t result;
bool useReadPixels;
bool isLocalScreenshot;
public:
MessageCaptureScreen(SurfaceFlinger* flinger,
Expand All @@ -3266,12 +3268,13 @@ status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
uint32_t minLayerZ, uint32_t maxLayerZ,
bool useIdentityTransform,
Transform::orientation_flags rotation,
bool isLocalScreenshot)
bool isLocalScreenshot, bool useReadPixels)
: flinger(flinger), display(display), producer(producer),
sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
useIdentityTransform(useIdentityTransform),
rotation(rotation), result(PERMISSION_DENIED),
useReadPixels(useReadPixels),
isLocalScreenshot(isLocalScreenshot)
{
}
Expand All @@ -3281,9 +3284,10 @@ status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
virtual bool handler() {
Mutex::Autolock _l(flinger->mStateLock);
sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
bool useReadPixels = this->useReadPixels && !flinger->mGpuToCpuSupported;
result = flinger->captureScreenImplLocked(hw, producer,
sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
useIdentityTransform, rotation, isLocalScreenshot);
useIdentityTransform, rotation, isLocalScreenshot, useReadPixels);
static_cast<GraphicProducerWrapper*>(IInterface::asBinder(producer).get())->exit(result);
return true;
}
Expand All @@ -3299,7 +3303,7 @@ status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
sp<MessageBase> msg = new MessageCaptureScreen(this,
display, IGraphicBufferProducer::asInterface( wrapper ),
sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
useIdentityTransform, rotationFlags, isLocalScreenshot);
useIdentityTransform, rotationFlags, isLocalScreenshot, useReadPixels);

status_t res = postMessageAsync(msg);
if (res == NO_ERROR) {
Expand Down Expand Up @@ -3382,7 +3386,7 @@ status_t SurfaceFlinger::captureScreenImplLocked(
Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
uint32_t minLayerZ, uint32_t maxLayerZ,
bool useIdentityTransform, Transform::orientation_flags rotation,
bool isLocalScreenshot)
bool isLocalScreenshot, bool useReadPixels)
{
ATRACE_CALL();

Expand Down Expand Up @@ -3452,7 +3456,7 @@ status_t SurfaceFlinger::captureScreenImplLocked(
if (image != EGL_NO_IMAGE_KHR) {
// this binds the given EGLImage as a framebuffer for the
// duration of this scope.
RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image, useReadPixels, reqWidth, reqHeight);
if (imageBond.getStatus() == NO_ERROR) {
// this will in fact render into our dequeued buffer
// via an FBO, which means we didn't have to create
Expand Down Expand Up @@ -3499,6 +3503,16 @@ status_t SurfaceFlinger::captureScreenImplLocked(
ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
}
}

if (useReadPixels) {
sp<GraphicBuffer> buf = static_cast<GraphicBuffer*>(buffer);
void* vaddr;
if (buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, &vaddr) == NO_ERROR) {
getRenderEngine().readPixels(0, 0, buffer->stride, reqHeight, (uint32_t *)vaddr);
buf->unlock();
}
}

if (DEBUG_SCREENSHOTS) {
uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
Expand Down
5 changes: 3 additions & 2 deletions services/surfaceflinger/SurfaceFlinger.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ class SurfaceFlinger : public BnSurfaceComposer,
const sp<IGraphicBufferProducer>& producer,
Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
uint32_t minLayerZ, uint32_t maxLayerZ,
bool useIdentityTransform, ISurfaceComposer::Rotation rotation);
bool useIdentityTransform, ISurfaceComposer::Rotation rotation,
bool useReadPixels);
virtual status_t getDisplayStats(const sp<IBinder>& display,
DisplayStatInfo* stats);
virtual status_t getDisplayConfigs(const sp<IBinder>& display,
Expand Down Expand Up @@ -337,7 +338,7 @@ class SurfaceFlinger : public BnSurfaceComposer,
Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
uint32_t minLayerZ, uint32_t maxLayerZ,
bool useIdentityTransform, Transform::orientation_flags rotation,
bool isLocalScreenshot);
bool isLocalScreenshot, bool useReadPixels);

/* ------------------------------------------------------------------------
* EGL
Expand Down

0 comments on commit a6d425f

Please sign in to comment.