blob: 74a8395f9fff58e2f40377df91ba16553abbaac5 [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();
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050037 // Setup the source
38 sp<GraphicBuffer> sourceBuffer;
39 sp<Fence> sourceFence;
40 Matrix4 texTransform;
41 status_t err = surface.getLastQueuedBuffer(&sourceBuffer, &sourceFence,
42 texTransform.data);
43 texTransform.invalidateType();
44 if (err != NO_ERROR) {
45 ALOGW("Failed to get last queued buffer, error = %d", err);
46 return CopyResult::UnknownError;
47 }
48 if (!sourceBuffer.get()) {
49 ALOGW("Surface doesn't have any previously queued frames, nothing to readback from");
50 return CopyResult::SourceEmpty;
51 }
52 if (sourceBuffer->getUsage() & GRALLOC_USAGE_PROTECTED) {
53 ALOGW("Surface is protected, unable to copy from it");
54 return CopyResult::SourceInvalid;
55 }
56 err = sourceFence->wait(500 /* ms */);
57 if (err != NO_ERROR) {
58 ALOGE("Timeout (500ms) exceeded waiting for buffer fence, abandoning readback attempt");
59 return CopyResult::Timeout;
60 }
61
sergeyv59eecb522016-11-17 17:54:57 -080062 return copyGraphicBufferInto(sourceBuffer.get(), texTransform, srcRect, bitmap);
63}
64
65CopyResult OpenGLReadback::copyGraphicBufferInto(GraphicBuffer* graphicBuffer,
66 Matrix4& texTransform, const Rect& srcRect, SkBitmap* bitmap) {
67 mRenderThread.eglManager().initialize();
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050068 // TODO: Can't use Image helper since it forces GL_TEXTURE_2D usage via
69 // GL_OES_EGL_image, which doesn't work since we need samplerExternalOES
70 // to be able to properly sample from the buffer.
71
72 // Create the EGLImage object that maps the GraphicBuffer
73 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
sergeyv59eecb522016-11-17 17:54:57 -080074 EGLClientBuffer clientBuffer = (EGLClientBuffer) graphicBuffer->getNativeBuffer();
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050075 EGLint attrs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE };
76
77 EGLImageKHR sourceImage = eglCreateImageKHR(display, EGL_NO_CONTEXT,
78 EGL_NATIVE_BUFFER_ANDROID, clientBuffer, attrs);
79
80 if (sourceImage == EGL_NO_IMAGE_KHR) {
81 ALOGW("eglCreateImageKHR failed (%#x)", eglGetError());
82 return CopyResult::UnknownError;
83 }
84
John Reck912bebe2016-12-07 16:36:15 -080085 uint32_t width = graphicBuffer->getWidth();
86 uint32_t height = graphicBuffer->getHeight();
87 // If this is a 90 or 270 degree rotation we need to swap width/height
88 // This is a fuzzy way of checking that.
89 if (texTransform[Matrix4::kSkewX] >= 0.5f || texTransform[Matrix4::kSkewX] <= -0.5f) {
90 std::swap(width, height);
91 }
92 CopyResult copyResult = copyImageInto(sourceImage, texTransform, width, height,
93 srcRect, bitmap);
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050094
95 // All we're flushing & finishing is the deletion of the texture since
96 // copyImageInto already did a major flush & finish as an implicit
97 // part of glReadPixels, so this shouldn't pose any major stalls.
98 glFinish();
99 eglDestroyImageKHR(display, sourceImage);
100 return copyResult;
101}
102
sergeyv59eecb522016-11-17 17:54:57 -0800103CopyResult OpenGLReadback::copyGraphicBufferInto(GraphicBuffer* graphicBuffer, SkBitmap* bitmap) {
104 Rect srcRect;
105 Matrix4 transform;
106 transform.loadScale(1, -1, 1);
107 transform.translate(0, -1);
108 return copyGraphicBufferInto(graphicBuffer, transform, srcRect, bitmap);
109}
110
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500111////////////////////////////////////////////////////////////////////////////////
112////////////////////////////////////////////////////////////////////////////////
113
114inline CopyResult copyTextureInto(Caches& caches, RenderState& renderState,
115 Texture& sourceTexture, const Matrix4& texTransform, const Rect& srcRect,
John Reck95801462016-09-01 09:44:09 -0700116 SkBitmap* bitmap) {
John Reck10dd0582016-03-31 16:36:16 -0700117 int destWidth = bitmap->width();
118 int destHeight = bitmap->height();
119 if (destWidth > caches.maxTextureSize
120 || destHeight > caches.maxTextureSize) {
121 ALOGW("Can't copy surface into bitmap, %dx%d exceeds max texture size %d",
122 destWidth, destHeight, caches.maxTextureSize);
John Recke94cbc72016-04-25 13:03:44 -0700123 return CopyResult::DestinationInvalid;
John Reck10dd0582016-03-31 16:36:16 -0700124 }
125 GLuint fbo = renderState.createFramebuffer();
126 if (!fbo) {
127 ALOGW("Could not obtain an FBO");
John Recke94cbc72016-04-25 13:03:44 -0700128 return CopyResult::UnknownError;
John Reck10dd0582016-03-31 16:36:16 -0700129 }
130
131 SkAutoLockPixels alp(*bitmap);
132
133 GLuint texture;
134
135 GLenum format;
136 GLenum type;
137
138 switch (bitmap->colorType()) {
139 case kAlpha_8_SkColorType:
140 format = GL_ALPHA;
141 type = GL_UNSIGNED_BYTE;
142 break;
143 case kRGB_565_SkColorType:
144 format = GL_RGB;
145 type = GL_UNSIGNED_SHORT_5_6_5;
146 break;
147 case kARGB_4444_SkColorType:
148 format = GL_RGBA;
149 type = GL_UNSIGNED_SHORT_4_4_4_4;
150 break;
151 case kN32_SkColorType:
152 default:
153 format = GL_RGBA;
154 type = GL_UNSIGNED_BYTE;
155 break;
156 }
157
158 renderState.bindFramebuffer(fbo);
159
160 // TODO: Use layerPool or something to get this maybe? But since we
161 // need explicit format control we can't currently.
162
163 // Setup the rendertarget
164 glGenTextures(1, &texture);
165 caches.textureState().activateTexture(0);
166 caches.textureState().bindTexture(texture);
167 glPixelStorei(GL_PACK_ALIGNMENT, bitmap->bytesPerPixel());
168 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
169 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
170 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
171 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
172 glTexImage2D(GL_TEXTURE_2D, 0, format, destWidth, destHeight,
173 0, format, type, nullptr);
174 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
175 GL_TEXTURE_2D, texture, 0);
176
Chris Craik764045d2016-07-06 17:14:05 -0700177 {
178 // Draw & readback
179 renderState.setViewport(destWidth, destHeight);
180 renderState.scissor().setEnabled(false);
181 renderState.blend().syncEnabled();
182 renderState.stencil().disable();
183
John Reck95801462016-09-01 09:44:09 -0700184 Matrix4 croppedTexTransform(texTransform);
185 if (!srcRect.isEmpty()) {
186 croppedTexTransform.loadTranslate(srcRect.left / sourceTexture.width(),
187 srcRect.top / sourceTexture.height(), 0);
188 croppedTexTransform.scale(srcRect.getWidth() / sourceTexture.width(),
189 srcRect.getHeight() / sourceTexture.height(), 1);
190 croppedTexTransform.multiply(texTransform);
191 }
Chris Craik764045d2016-07-06 17:14:05 -0700192 Glop glop;
193 GlopBuilder(renderState, caches, &glop)
194 .setRoundRectClipState(nullptr)
195 .setMeshTexturedUnitQuad(nullptr)
John Reck95801462016-09-01 09:44:09 -0700196 .setFillExternalTexture(sourceTexture, croppedTexTransform)
Chris Craik764045d2016-07-06 17:14:05 -0700197 .setTransform(Matrix4::identity(), TransformFlags::None)
198 .setModelViewMapUnitToRect(Rect(destWidth, destHeight))
199 .build();
200 Matrix4 ortho;
201 ortho.loadOrtho(destWidth, destHeight);
202 renderState.render(glop, ortho);
203
204 glReadPixels(0, 0, bitmap->width(), bitmap->height(), format,
205 type, bitmap->getPixels());
206 }
207
208 // Cleanup
209 caches.textureState().deleteTexture(texture);
210 renderState.deleteFramebuffer(fbo);
211
212 GL_CHECKPOINT(MODERATE);
213
214 return CopyResult::Success;
215}
216
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500217CopyResult OpenGLReadbackImpl::copyImageInto(EGLImageKHR eglImage,
218 const Matrix4& imgTransform, int imgWidth, int imgHeight, const Rect& srcRect,
219 SkBitmap* bitmap) {
Chris Craik764045d2016-07-06 17:14:05 -0700220
221 Caches& caches = Caches::getInstance();
John Reck2f783272016-04-19 07:51:13 -0700222 GLuint sourceTexId;
223 // Create a 2D texture to sample from the EGLImage
224 glGenTextures(1, &sourceTexId);
Chris Craik764045d2016-07-06 17:14:05 -0700225 caches.textureState().bindTexture(GL_TEXTURE_EXTERNAL_OES, sourceTexId);
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500226 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, eglImage);
John Reck2f783272016-04-19 07:51:13 -0700227
228 GLenum status = GL_NO_ERROR;
229 while ((status = glGetError()) != GL_NO_ERROR) {
John Reck8a29c0e2016-09-01 13:04:00 -0700230 ALOGW("glEGLImageTargetTexture2DOES failed (%#x)", status);
John Recke94cbc72016-04-25 13:03:44 -0700231 return CopyResult::UnknownError;
John Reck2f783272016-04-19 07:51:13 -0700232 }
233
John Reck10dd0582016-03-31 16:36:16 -0700234 Texture sourceTexture(caches);
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500235 sourceTexture.wrap(sourceTexId, imgWidth, imgHeight, 0, 0 /* total lie */,
236 GL_TEXTURE_EXTERNAL_OES);
John Reck10dd0582016-03-31 16:36:16 -0700237
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500238 CopyResult copyResult = copyTextureInto(caches, mRenderThread.renderState(),
239 sourceTexture, imgTransform, srcRect, bitmap);
John Reck8a29c0e2016-09-01 13:04:00 -0700240 sourceTexture.deleteTexture();
John Reck8a29c0e2016-09-01 13:04:00 -0700241 return copyResult;
Chris Craik764045d2016-07-06 17:14:05 -0700242}
John Reck10dd0582016-03-31 16:36:16 -0700243
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500244bool OpenGLReadbackImpl::copyLayerInto(renderthread::RenderThread& renderThread,
Chris Craik764045d2016-07-06 17:14:05 -0700245 Layer& layer, SkBitmap* bitmap) {
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500246 return CopyResult::Success == copyTextureInto(Caches::getInstance(),
247 renderThread.renderState(), layer.getTexture(), layer.getTexTransform(),
248 Rect(), bitmap);
John Reck10dd0582016-03-31 16:36:16 -0700249}
250
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500251
John Reck10dd0582016-03-31 16:36:16 -0700252} // namespace uirenderer
253} // namespace android