blob: 408159b8f33a892c76dd19fdcd4bccc1853e8e61 [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
sergeyv59eecb522016-11-17 17:54:57 -080085 CopyResult copyResult = copyImageInto(sourceImage, texTransform, graphicBuffer->getWidth(),
86 graphicBuffer->getHeight(), srcRect, bitmap);
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050087
88 // All we're flushing & finishing is the deletion of the texture since
89 // copyImageInto already did a major flush & finish as an implicit
90 // part of glReadPixels, so this shouldn't pose any major stalls.
91 glFinish();
92 eglDestroyImageKHR(display, sourceImage);
93 return copyResult;
94}
95
sergeyv59eecb522016-11-17 17:54:57 -080096CopyResult OpenGLReadback::copyGraphicBufferInto(GraphicBuffer* graphicBuffer, SkBitmap* bitmap) {
97 Rect srcRect;
98 Matrix4 transform;
99 transform.loadScale(1, -1, 1);
100 transform.translate(0, -1);
101 return copyGraphicBufferInto(graphicBuffer, transform, srcRect, bitmap);
102}
103
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500104////////////////////////////////////////////////////////////////////////////////
105////////////////////////////////////////////////////////////////////////////////
106
107inline CopyResult copyTextureInto(Caches& caches, RenderState& renderState,
108 Texture& sourceTexture, const Matrix4& texTransform, const Rect& srcRect,
John Reck95801462016-09-01 09:44:09 -0700109 SkBitmap* bitmap) {
John Reck10dd0582016-03-31 16:36:16 -0700110 int destWidth = bitmap->width();
111 int destHeight = bitmap->height();
112 if (destWidth > caches.maxTextureSize
113 || destHeight > caches.maxTextureSize) {
114 ALOGW("Can't copy surface into bitmap, %dx%d exceeds max texture size %d",
115 destWidth, destHeight, caches.maxTextureSize);
John Recke94cbc72016-04-25 13:03:44 -0700116 return CopyResult::DestinationInvalid;
John Reck10dd0582016-03-31 16:36:16 -0700117 }
118 GLuint fbo = renderState.createFramebuffer();
119 if (!fbo) {
120 ALOGW("Could not obtain an FBO");
John Recke94cbc72016-04-25 13:03:44 -0700121 return CopyResult::UnknownError;
John Reck10dd0582016-03-31 16:36:16 -0700122 }
123
124 SkAutoLockPixels alp(*bitmap);
125
126 GLuint texture;
127
128 GLenum format;
129 GLenum type;
130
131 switch (bitmap->colorType()) {
132 case kAlpha_8_SkColorType:
133 format = GL_ALPHA;
134 type = GL_UNSIGNED_BYTE;
135 break;
136 case kRGB_565_SkColorType:
137 format = GL_RGB;
138 type = GL_UNSIGNED_SHORT_5_6_5;
139 break;
140 case kARGB_4444_SkColorType:
141 format = GL_RGBA;
142 type = GL_UNSIGNED_SHORT_4_4_4_4;
143 break;
144 case kN32_SkColorType:
145 default:
146 format = GL_RGBA;
147 type = GL_UNSIGNED_BYTE;
148 break;
149 }
150
151 renderState.bindFramebuffer(fbo);
152
153 // TODO: Use layerPool or something to get this maybe? But since we
154 // need explicit format control we can't currently.
155
156 // Setup the rendertarget
157 glGenTextures(1, &texture);
158 caches.textureState().activateTexture(0);
159 caches.textureState().bindTexture(texture);
160 glPixelStorei(GL_PACK_ALIGNMENT, bitmap->bytesPerPixel());
161 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
162 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
163 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
164 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
165 glTexImage2D(GL_TEXTURE_2D, 0, format, destWidth, destHeight,
166 0, format, type, nullptr);
167 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
168 GL_TEXTURE_2D, texture, 0);
169
Chris Craik764045d2016-07-06 17:14:05 -0700170 {
171 // Draw & readback
172 renderState.setViewport(destWidth, destHeight);
173 renderState.scissor().setEnabled(false);
174 renderState.blend().syncEnabled();
175 renderState.stencil().disable();
176
John Reck95801462016-09-01 09:44:09 -0700177 Matrix4 croppedTexTransform(texTransform);
178 if (!srcRect.isEmpty()) {
179 croppedTexTransform.loadTranslate(srcRect.left / sourceTexture.width(),
180 srcRect.top / sourceTexture.height(), 0);
181 croppedTexTransform.scale(srcRect.getWidth() / sourceTexture.width(),
182 srcRect.getHeight() / sourceTexture.height(), 1);
183 croppedTexTransform.multiply(texTransform);
184 }
Chris Craik764045d2016-07-06 17:14:05 -0700185 Glop glop;
186 GlopBuilder(renderState, caches, &glop)
187 .setRoundRectClipState(nullptr)
188 .setMeshTexturedUnitQuad(nullptr)
John Reck95801462016-09-01 09:44:09 -0700189 .setFillExternalTexture(sourceTexture, croppedTexTransform)
Chris Craik764045d2016-07-06 17:14:05 -0700190 .setTransform(Matrix4::identity(), TransformFlags::None)
191 .setModelViewMapUnitToRect(Rect(destWidth, destHeight))
192 .build();
193 Matrix4 ortho;
194 ortho.loadOrtho(destWidth, destHeight);
195 renderState.render(glop, ortho);
196
197 glReadPixels(0, 0, bitmap->width(), bitmap->height(), format,
198 type, bitmap->getPixels());
199 }
200
201 // Cleanup
202 caches.textureState().deleteTexture(texture);
203 renderState.deleteFramebuffer(fbo);
204
205 GL_CHECKPOINT(MODERATE);
206
207 return CopyResult::Success;
208}
209
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500210CopyResult OpenGLReadbackImpl::copyImageInto(EGLImageKHR eglImage,
211 const Matrix4& imgTransform, int imgWidth, int imgHeight, const Rect& srcRect,
212 SkBitmap* bitmap) {
Chris Craik764045d2016-07-06 17:14:05 -0700213
214 Caches& caches = Caches::getInstance();
John Reck2f783272016-04-19 07:51:13 -0700215 GLuint sourceTexId;
216 // Create a 2D texture to sample from the EGLImage
217 glGenTextures(1, &sourceTexId);
Chris Craik764045d2016-07-06 17:14:05 -0700218 caches.textureState().bindTexture(GL_TEXTURE_EXTERNAL_OES, sourceTexId);
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500219 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, eglImage);
John Reck2f783272016-04-19 07:51:13 -0700220
221 GLenum status = GL_NO_ERROR;
222 while ((status = glGetError()) != GL_NO_ERROR) {
John Reck8a29c0e2016-09-01 13:04:00 -0700223 ALOGW("glEGLImageTargetTexture2DOES failed (%#x)", status);
John Recke94cbc72016-04-25 13:03:44 -0700224 return CopyResult::UnknownError;
John Reck2f783272016-04-19 07:51:13 -0700225 }
226
John Reck10dd0582016-03-31 16:36:16 -0700227 Texture sourceTexture(caches);
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500228 sourceTexture.wrap(sourceTexId, imgWidth, imgHeight, 0, 0 /* total lie */,
229 GL_TEXTURE_EXTERNAL_OES);
John Reck10dd0582016-03-31 16:36:16 -0700230
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500231 CopyResult copyResult = copyTextureInto(caches, mRenderThread.renderState(),
232 sourceTexture, imgTransform, srcRect, bitmap);
John Reck8a29c0e2016-09-01 13:04:00 -0700233 sourceTexture.deleteTexture();
John Reck8a29c0e2016-09-01 13:04:00 -0700234 return copyResult;
Chris Craik764045d2016-07-06 17:14:05 -0700235}
John Reck10dd0582016-03-31 16:36:16 -0700236
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500237bool OpenGLReadbackImpl::copyLayerInto(renderthread::RenderThread& renderThread,
Chris Craik764045d2016-07-06 17:14:05 -0700238 Layer& layer, SkBitmap* bitmap) {
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500239 return CopyResult::Success == copyTextureInto(Caches::getInstance(),
240 renderThread.renderState(), layer.getTexture(), layer.getTexTransform(),
241 Rect(), bitmap);
John Reck10dd0582016-03-31 16:36:16 -0700242}
243
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500244
John Reck10dd0582016-03-31 16:36:16 -0700245} // namespace uirenderer
246} // namespace android