Readback.cpp revision 10dd0585c11dcedb5a271d54e645594f1d215d5c
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "Readback.h"
18
19#include "Caches.h"
20#include "Image.h"
21#include "GlopBuilder.h"
22#include "renderstate/RenderState.h"
23#include "renderthread/EglManager.h"
24#include "utils/GLUtils.h"
25
26#include <GLES2/gl2.h>
27#include <ui/Fence.h>
28#include <ui/GraphicBuffer.h>
29
30namespace android {
31namespace uirenderer {
32
33bool Readback::copySurfaceInto(renderthread::RenderThread& renderThread,
34        Surface& surface, SkBitmap* bitmap) {
35    // TODO: Clean this up and unify it with LayerRenderer::copyLayer,
36    // of which most of this is copied from.
37    renderThread.eglManager().initialize();
38
39    Caches& caches = Caches::getInstance();
40    RenderState& renderState = renderThread.renderState();
41    int destWidth = bitmap->width();
42    int destHeight = bitmap->height();
43    if (destWidth > caches.maxTextureSize
44                || destHeight > caches.maxTextureSize) {
45        ALOGW("Can't copy surface into bitmap, %dx%d exceeds max texture size %d",
46                destWidth, destHeight, caches.maxTextureSize);
47        return false;
48    }
49    GLuint fbo = renderState.createFramebuffer();
50    if (!fbo) {
51        ALOGW("Could not obtain an FBO");
52        return false;
53    }
54
55    SkAutoLockPixels alp(*bitmap);
56
57    GLuint texture;
58
59    GLenum format;
60    GLenum type;
61
62    switch (bitmap->colorType()) {
63        case kAlpha_8_SkColorType:
64            format = GL_ALPHA;
65            type = GL_UNSIGNED_BYTE;
66            break;
67        case kRGB_565_SkColorType:
68            format = GL_RGB;
69            type = GL_UNSIGNED_SHORT_5_6_5;
70            break;
71        case kARGB_4444_SkColorType:
72            format = GL_RGBA;
73            type = GL_UNSIGNED_SHORT_4_4_4_4;
74            break;
75        case kN32_SkColorType:
76        default:
77            format = GL_RGBA;
78            type = GL_UNSIGNED_BYTE;
79            break;
80    }
81
82    renderState.bindFramebuffer(fbo);
83
84    // TODO: Use layerPool or something to get this maybe? But since we
85    // need explicit format control we can't currently.
86
87    // Setup the rendertarget
88    glGenTextures(1, &texture);
89    caches.textureState().activateTexture(0);
90    caches.textureState().bindTexture(texture);
91    glPixelStorei(GL_PACK_ALIGNMENT, bitmap->bytesPerPixel());
92    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
93    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
94    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
95    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
96    glTexImage2D(GL_TEXTURE_2D, 0, format, destWidth, destHeight,
97            0, format, type, nullptr);
98    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
99            GL_TEXTURE_2D, texture, 0);
100
101    // Setup the source
102    sp<GraphicBuffer> sourceBuffer;
103    sp<Fence> sourceFence;
104    // FIXME: Waiting on an API from libgui for this
105    // surface.getLastQueuedBuffer(&sourceBuffer, &sourceFence);
106    if (!sourceBuffer.get()) {
107        ALOGW("Surface doesn't have any previously queued frames, nothing to readback from");
108        return false;
109    }
110    int err = sourceFence->wait(500 /* ms */);
111    if (err != NO_ERROR) {
112        ALOGE("Timeout (500ms) exceeded waiting for buffer fence, abandoning readback attempt");
113        return false;
114    }
115    Image sourceImage(sourceBuffer);
116    if (!sourceImage.getTexture()) {
117        ALOGW("Failed to make an EGLImage from the GraphicBuffer");
118        return false;
119    }
120    Texture sourceTexture(caches);
121    sourceTexture.wrap(sourceImage.getTexture(),
122            sourceBuffer->getWidth(), sourceBuffer->getHeight(), 0 /* total lie */);
123
124    {
125        // Draw & readback
126        renderState.setViewport(destWidth, destHeight);
127        renderState.scissor().setEnabled(false);
128        renderState.blend().syncEnabled();
129        renderState.stencil().disable();
130
131        Rect destRect(destWidth, destHeight);
132        Glop glop;
133        GlopBuilder(renderState, caches, &glop)
134                .setRoundRectClipState(nullptr)
135                .setMeshTexturedUvQuad(nullptr, Rect(0, 1, 1, 0)) // TODO: simplify with VBO
136                .setFillLayer(sourceTexture, nullptr, 1.0f, SkXfermode::kSrc_Mode,
137                        Blend::ModeOrderSwap::NoSwap)
138                .setTransform(Matrix4::identity(), TransformFlags::None)
139                .setModelViewMapUnitToRect(destRect)
140                .build();
141        Matrix4 ortho;
142        ortho.loadOrtho(destWidth, destHeight);
143        renderState.render(glop, ortho);
144
145        glReadPixels(0, 0, bitmap->width(), bitmap->height(), format,
146                type, bitmap->getPixels());
147    }
148
149    // Cleanup
150    caches.textureState().deleteTexture(texture);
151    renderState.deleteFramebuffer(fbo);
152
153    GL_CHECKPOINT(MODERATE);
154
155    return true;
156}
157
158} // namespace uirenderer
159} // namespace android
160