blob: da6d994f436c8647288f76066e5a2a0de6170987 [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
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050017#include "OpenGLReadback.h"
John Reck10dd0582016-03-31 16:36:16 -070018
19#include "Caches.h"
20#include "Image.h"
21#include "GlopBuilder.h"
Chris Craik764045d2016-07-06 17:14:05 -070022#include "Layer.h"
John Reck10dd0582016-03-31 16:36:16 -070023#include "renderstate/RenderState.h"
24#include "renderthread/EglManager.h"
25#include "utils/GLUtils.h"
26
27#include <GLES2/gl2.h>
28#include <ui/Fence.h>
29#include <ui/GraphicBuffer.h>
30
31namespace android {
32namespace uirenderer {
33
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050034CopyResult OpenGLReadback::copySurfaceInto(Surface& surface, const Rect& srcRect,
35 SkBitmap* bitmap) {
36 ATRACE_CALL();
37 mRenderThread.eglManager().initialize();
38
39 // Setup the source
40 sp<GraphicBuffer> sourceBuffer;
41 sp<Fence> sourceFence;
42 Matrix4 texTransform;
43 status_t err = surface.getLastQueuedBuffer(&sourceBuffer, &sourceFence,
44 texTransform.data);
45 texTransform.invalidateType();
46 if (err != NO_ERROR) {
47 ALOGW("Failed to get last queued buffer, error = %d", err);
48 return CopyResult::UnknownError;
49 }
50 if (!sourceBuffer.get()) {
51 ALOGW("Surface doesn't have any previously queued frames, nothing to readback from");
52 return CopyResult::SourceEmpty;
53 }
54 if (sourceBuffer->getUsage() & GRALLOC_USAGE_PROTECTED) {
55 ALOGW("Surface is protected, unable to copy from it");
56 return CopyResult::SourceInvalid;
57 }
58 err = sourceFence->wait(500 /* ms */);
59 if (err != NO_ERROR) {
60 ALOGE("Timeout (500ms) exceeded waiting for buffer fence, abandoning readback attempt");
61 return CopyResult::Timeout;
62 }
63
64 // TODO: Can't use Image helper since it forces GL_TEXTURE_2D usage via
65 // GL_OES_EGL_image, which doesn't work since we need samplerExternalOES
66 // to be able to properly sample from the buffer.
67
68 // Create the EGLImage object that maps the GraphicBuffer
69 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
70 EGLClientBuffer clientBuffer = (EGLClientBuffer) sourceBuffer->getNativeBuffer();
71 EGLint attrs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE };
72
73 EGLImageKHR sourceImage = eglCreateImageKHR(display, EGL_NO_CONTEXT,
74 EGL_NATIVE_BUFFER_ANDROID, clientBuffer, attrs);
75
76 if (sourceImage == EGL_NO_IMAGE_KHR) {
77 ALOGW("eglCreateImageKHR failed (%#x)", eglGetError());
78 return CopyResult::UnknownError;
79 }
80
81 CopyResult copyResult = copyImageInto(sourceImage, texTransform, sourceBuffer->getWidth(),
82 sourceBuffer->getHeight(), srcRect, bitmap);
83
84 // All we're flushing & finishing is the deletion of the texture since
85 // copyImageInto already did a major flush & finish as an implicit
86 // part of glReadPixels, so this shouldn't pose any major stalls.
87 glFinish();
88 eglDestroyImageKHR(display, sourceImage);
89 return copyResult;
90}
91
92////////////////////////////////////////////////////////////////////////////////
93////////////////////////////////////////////////////////////////////////////////
94
95inline CopyResult copyTextureInto(Caches& caches, RenderState& renderState,
96 Texture& sourceTexture, const Matrix4& texTransform, const Rect& srcRect,
John Reck95801462016-09-01 09:44:09 -070097 SkBitmap* bitmap) {
John Reck10dd0582016-03-31 16:36:16 -070098 int destWidth = bitmap->width();
99 int destHeight = bitmap->height();
100 if (destWidth > caches.maxTextureSize
101 || destHeight > caches.maxTextureSize) {
102 ALOGW("Can't copy surface into bitmap, %dx%d exceeds max texture size %d",
103 destWidth, destHeight, caches.maxTextureSize);
John Recke94cbc72016-04-25 13:03:44 -0700104 return CopyResult::DestinationInvalid;
John Reck10dd0582016-03-31 16:36:16 -0700105 }
106 GLuint fbo = renderState.createFramebuffer();
107 if (!fbo) {
108 ALOGW("Could not obtain an FBO");
John Recke94cbc72016-04-25 13:03:44 -0700109 return CopyResult::UnknownError;
John Reck10dd0582016-03-31 16:36:16 -0700110 }
111
112 SkAutoLockPixels alp(*bitmap);
113
114 GLuint texture;
115
116 GLenum format;
117 GLenum type;
118
119 switch (bitmap->colorType()) {
120 case kAlpha_8_SkColorType:
121 format = GL_ALPHA;
122 type = GL_UNSIGNED_BYTE;
123 break;
124 case kRGB_565_SkColorType:
125 format = GL_RGB;
126 type = GL_UNSIGNED_SHORT_5_6_5;
127 break;
128 case kARGB_4444_SkColorType:
129 format = GL_RGBA;
130 type = GL_UNSIGNED_SHORT_4_4_4_4;
131 break;
132 case kN32_SkColorType:
133 default:
134 format = GL_RGBA;
135 type = GL_UNSIGNED_BYTE;
136 break;
137 }
138
139 renderState.bindFramebuffer(fbo);
140
141 // TODO: Use layerPool or something to get this maybe? But since we
142 // need explicit format control we can't currently.
143
144 // Setup the rendertarget
145 glGenTextures(1, &texture);
146 caches.textureState().activateTexture(0);
147 caches.textureState().bindTexture(texture);
148 glPixelStorei(GL_PACK_ALIGNMENT, bitmap->bytesPerPixel());
149 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
150 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
151 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
152 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
153 glTexImage2D(GL_TEXTURE_2D, 0, format, destWidth, destHeight,
154 0, format, type, nullptr);
155 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
156 GL_TEXTURE_2D, texture, 0);
157
Chris Craik764045d2016-07-06 17:14:05 -0700158 {
159 // Draw & readback
160 renderState.setViewport(destWidth, destHeight);
161 renderState.scissor().setEnabled(false);
162 renderState.blend().syncEnabled();
163 renderState.stencil().disable();
164
John Reck95801462016-09-01 09:44:09 -0700165 Matrix4 croppedTexTransform(texTransform);
166 if (!srcRect.isEmpty()) {
167 croppedTexTransform.loadTranslate(srcRect.left / sourceTexture.width(),
168 srcRect.top / sourceTexture.height(), 0);
169 croppedTexTransform.scale(srcRect.getWidth() / sourceTexture.width(),
170 srcRect.getHeight() / sourceTexture.height(), 1);
171 croppedTexTransform.multiply(texTransform);
172 }
Chris Craik764045d2016-07-06 17:14:05 -0700173 Glop glop;
174 GlopBuilder(renderState, caches, &glop)
175 .setRoundRectClipState(nullptr)
176 .setMeshTexturedUnitQuad(nullptr)
John Reck95801462016-09-01 09:44:09 -0700177 .setFillExternalTexture(sourceTexture, croppedTexTransform)
Chris Craik764045d2016-07-06 17:14:05 -0700178 .setTransform(Matrix4::identity(), TransformFlags::None)
179 .setModelViewMapUnitToRect(Rect(destWidth, destHeight))
180 .build();
181 Matrix4 ortho;
182 ortho.loadOrtho(destWidth, destHeight);
183 renderState.render(glop, ortho);
184
185 glReadPixels(0, 0, bitmap->width(), bitmap->height(), format,
186 type, bitmap->getPixels());
187 }
188
189 // Cleanup
190 caches.textureState().deleteTexture(texture);
191 renderState.deleteFramebuffer(fbo);
192
193 GL_CHECKPOINT(MODERATE);
194
195 return CopyResult::Success;
196}
197
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500198CopyResult OpenGLReadbackImpl::copyImageInto(EGLImageKHR eglImage,
199 const Matrix4& imgTransform, int imgWidth, int imgHeight, const Rect& srcRect,
200 SkBitmap* bitmap) {
Chris Craik764045d2016-07-06 17:14:05 -0700201
202 Caches& caches = Caches::getInstance();
John Reck2f783272016-04-19 07:51:13 -0700203 GLuint sourceTexId;
204 // Create a 2D texture to sample from the EGLImage
205 glGenTextures(1, &sourceTexId);
Chris Craik764045d2016-07-06 17:14:05 -0700206 caches.textureState().bindTexture(GL_TEXTURE_EXTERNAL_OES, sourceTexId);
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500207 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, eglImage);
John Reck2f783272016-04-19 07:51:13 -0700208
209 GLenum status = GL_NO_ERROR;
210 while ((status = glGetError()) != GL_NO_ERROR) {
John Reck8a29c0e2016-09-01 13:04:00 -0700211 ALOGW("glEGLImageTargetTexture2DOES failed (%#x)", status);
John Recke94cbc72016-04-25 13:03:44 -0700212 return CopyResult::UnknownError;
John Reck2f783272016-04-19 07:51:13 -0700213 }
214
John Reck10dd0582016-03-31 16:36:16 -0700215 Texture sourceTexture(caches);
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500216 sourceTexture.wrap(sourceTexId, imgWidth, imgHeight, 0, 0 /* total lie */,
217 GL_TEXTURE_EXTERNAL_OES);
John Reck10dd0582016-03-31 16:36:16 -0700218
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500219 CopyResult copyResult = copyTextureInto(caches, mRenderThread.renderState(),
220 sourceTexture, imgTransform, srcRect, bitmap);
John Reck8a29c0e2016-09-01 13:04:00 -0700221 sourceTexture.deleteTexture();
John Reck8a29c0e2016-09-01 13:04:00 -0700222 return copyResult;
Chris Craik764045d2016-07-06 17:14:05 -0700223}
John Reck10dd0582016-03-31 16:36:16 -0700224
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500225bool OpenGLReadbackImpl::copyLayerInto(renderthread::RenderThread& renderThread,
Chris Craik764045d2016-07-06 17:14:05 -0700226 Layer& layer, SkBitmap* bitmap) {
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500227 return CopyResult::Success == copyTextureInto(Caches::getInstance(),
228 renderThread.renderState(), layer.getTexture(), layer.getTexTransform(),
229 Rect(), bitmap);
John Reck10dd0582016-03-31 16:36:16 -0700230}
231
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500232
John Reck10dd0582016-03-31 16:36:16 -0700233} // namespace uirenderer
234} // namespace android