blob: 49596e143ebf6f01ca6cd3c2d5453f31d217b161 [file] [log] [blame]
John Reck10dd0582016-03-31 16:36:16 -07001/*
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
John Recke94cbc72016-04-25 13:03:44 -070033CopyResult Readback::copySurfaceInto(renderthread::RenderThread& renderThread,
John Reck10dd0582016-03-31 16:36:16 -070034 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);
John Recke94cbc72016-04-25 13:03:44 -070047 return CopyResult::DestinationInvalid;
John Reck10dd0582016-03-31 16:36:16 -070048 }
49 GLuint fbo = renderState.createFramebuffer();
50 if (!fbo) {
51 ALOGW("Could not obtain an FBO");
John Recke94cbc72016-04-25 13:03:44 -070052 return CopyResult::UnknownError;
John Reck10dd0582016-03-31 16:36:16 -070053 }
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;
John Reck2f783272016-04-19 07:51:13 -0700104 status_t err = surface.getLastQueuedBuffer(&sourceBuffer, &sourceFence);
105 if (err != NO_ERROR) {
106 ALOGW("Failed to get last queued buffer, error = %d", err);
John Recke94cbc72016-04-25 13:03:44 -0700107 return CopyResult::UnknownError;
John Reck2f783272016-04-19 07:51:13 -0700108 }
John Reck10dd0582016-03-31 16:36:16 -0700109 if (!sourceBuffer.get()) {
110 ALOGW("Surface doesn't have any previously queued frames, nothing to readback from");
John Recke94cbc72016-04-25 13:03:44 -0700111 return CopyResult::SourceEmpty;
112 }
113 if (sourceBuffer->getUsage() & GRALLOC_USAGE_PROTECTED) {
114 ALOGW("Surface is protected, unable to copy from it");
115 return CopyResult::SourceInvalid;
John Reck10dd0582016-03-31 16:36:16 -0700116 }
John Reck2f783272016-04-19 07:51:13 -0700117 err = sourceFence->wait(500 /* ms */);
John Reck10dd0582016-03-31 16:36:16 -0700118 if (err != NO_ERROR) {
119 ALOGE("Timeout (500ms) exceeded waiting for buffer fence, abandoning readback attempt");
John Recke94cbc72016-04-25 13:03:44 -0700120 return CopyResult::Timeout;
John Reck10dd0582016-03-31 16:36:16 -0700121 }
John Reck2f783272016-04-19 07:51:13 -0700122
123 // TODO: Can't use Image helper since it forces GL_TEXTURE_2D usage via
124 // GL_OES_EGL_image, which doesn't work since we need samplerExternalOES
125 // to be able to properly sample from the buffer.
126
127 // Create the EGLImage object that maps the GraphicBuffer
128 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
129 EGLClientBuffer clientBuffer = (EGLClientBuffer) sourceBuffer->getNativeBuffer();
130 EGLint attrs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE };
131
132 EGLImageKHR sourceImage = eglCreateImageKHR(display, EGL_NO_CONTEXT,
133 EGL_NATIVE_BUFFER_ANDROID, clientBuffer, attrs);
134
135 if (sourceImage == EGL_NO_IMAGE_KHR) {
136 ALOGW("Error creating image (%#x)", eglGetError());
John Recke94cbc72016-04-25 13:03:44 -0700137 return CopyResult::UnknownError;
John Reck10dd0582016-03-31 16:36:16 -0700138 }
John Reck2f783272016-04-19 07:51:13 -0700139 GLuint sourceTexId;
140 // Create a 2D texture to sample from the EGLImage
141 glGenTextures(1, &sourceTexId);
142 Caches::getInstance().textureState().bindTexture(GL_TEXTURE_EXTERNAL_OES, sourceTexId);
143 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, sourceImage);
144
145 GLenum status = GL_NO_ERROR;
146 while ((status = glGetError()) != GL_NO_ERROR) {
147 ALOGW("Error creating image (%#x)", status);
John Recke94cbc72016-04-25 13:03:44 -0700148 return CopyResult::UnknownError;
John Reck2f783272016-04-19 07:51:13 -0700149 }
150
John Reck10dd0582016-03-31 16:36:16 -0700151 Texture sourceTexture(caches);
John Reck2f783272016-04-19 07:51:13 -0700152 sourceTexture.wrap(sourceTexId,
John Reck10dd0582016-03-31 16:36:16 -0700153 sourceBuffer->getWidth(), sourceBuffer->getHeight(), 0 /* total lie */);
154
155 {
156 // Draw & readback
157 renderState.setViewport(destWidth, destHeight);
158 renderState.scissor().setEnabled(false);
159 renderState.blend().syncEnabled();
160 renderState.stencil().disable();
161
162 Rect destRect(destWidth, destHeight);
163 Glop glop;
164 GlopBuilder(renderState, caches, &glop)
165 .setRoundRectClipState(nullptr)
166 .setMeshTexturedUvQuad(nullptr, Rect(0, 1, 1, 0)) // TODO: simplify with VBO
John Reck2f783272016-04-19 07:51:13 -0700167 .setFillExternalTexture(sourceTexture)
John Reck10dd0582016-03-31 16:36:16 -0700168 .setTransform(Matrix4::identity(), TransformFlags::None)
169 .setModelViewMapUnitToRect(destRect)
170 .build();
171 Matrix4 ortho;
172 ortho.loadOrtho(destWidth, destHeight);
173 renderState.render(glop, ortho);
174
175 glReadPixels(0, 0, bitmap->width(), bitmap->height(), format,
176 type, bitmap->getPixels());
177 }
178
179 // Cleanup
180 caches.textureState().deleteTexture(texture);
181 renderState.deleteFramebuffer(fbo);
182
183 GL_CHECKPOINT(MODERATE);
184
John Recke94cbc72016-04-25 13:03:44 -0700185 return CopyResult::Success;
John Reck10dd0582016-03-31 16:36:16 -0700186}
187
188} // namespace uirenderer
189} // namespace android