blob: ddca122788d16b514fddb06c3e8c8fcffa67e7ee [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"
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
Chris Craik764045d2016-07-06 17:14:05 -070034static CopyResult copyTextureInto(Caches& caches, RenderState& renderState,
John Reck95801462016-09-01 09:44:09 -070035 Texture& sourceTexture, Matrix4& texTransform, const Rect& srcRect,
36 SkBitmap* bitmap) {
John Reck10dd0582016-03-31 16:36:16 -070037 int destWidth = bitmap->width();
38 int destHeight = bitmap->height();
39 if (destWidth > caches.maxTextureSize
40 || destHeight > caches.maxTextureSize) {
41 ALOGW("Can't copy surface into bitmap, %dx%d exceeds max texture size %d",
42 destWidth, destHeight, caches.maxTextureSize);
John Recke94cbc72016-04-25 13:03:44 -070043 return CopyResult::DestinationInvalid;
John Reck10dd0582016-03-31 16:36:16 -070044 }
45 GLuint fbo = renderState.createFramebuffer();
46 if (!fbo) {
47 ALOGW("Could not obtain an FBO");
John Recke94cbc72016-04-25 13:03:44 -070048 return CopyResult::UnknownError;
John Reck10dd0582016-03-31 16:36:16 -070049 }
50
51 SkAutoLockPixels alp(*bitmap);
52
53 GLuint texture;
54
55 GLenum format;
56 GLenum type;
57
58 switch (bitmap->colorType()) {
59 case kAlpha_8_SkColorType:
60 format = GL_ALPHA;
61 type = GL_UNSIGNED_BYTE;
62 break;
63 case kRGB_565_SkColorType:
64 format = GL_RGB;
65 type = GL_UNSIGNED_SHORT_5_6_5;
66 break;
67 case kARGB_4444_SkColorType:
68 format = GL_RGBA;
69 type = GL_UNSIGNED_SHORT_4_4_4_4;
70 break;
71 case kN32_SkColorType:
72 default:
73 format = GL_RGBA;
74 type = GL_UNSIGNED_BYTE;
75 break;
76 }
77
78 renderState.bindFramebuffer(fbo);
79
80 // TODO: Use layerPool or something to get this maybe? But since we
81 // need explicit format control we can't currently.
82
83 // Setup the rendertarget
84 glGenTextures(1, &texture);
85 caches.textureState().activateTexture(0);
86 caches.textureState().bindTexture(texture);
87 glPixelStorei(GL_PACK_ALIGNMENT, bitmap->bytesPerPixel());
88 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
89 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
90 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
91 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
92 glTexImage2D(GL_TEXTURE_2D, 0, format, destWidth, destHeight,
93 0, format, type, nullptr);
94 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
95 GL_TEXTURE_2D, texture, 0);
96
Chris Craik764045d2016-07-06 17:14:05 -070097 {
98 // Draw & readback
99 renderState.setViewport(destWidth, destHeight);
100 renderState.scissor().setEnabled(false);
101 renderState.blend().syncEnabled();
102 renderState.stencil().disable();
103
John Reck95801462016-09-01 09:44:09 -0700104 Matrix4 croppedTexTransform(texTransform);
105 if (!srcRect.isEmpty()) {
106 croppedTexTransform.loadTranslate(srcRect.left / sourceTexture.width(),
107 srcRect.top / sourceTexture.height(), 0);
108 croppedTexTransform.scale(srcRect.getWidth() / sourceTexture.width(),
109 srcRect.getHeight() / sourceTexture.height(), 1);
110 croppedTexTransform.multiply(texTransform);
111 }
Chris Craik764045d2016-07-06 17:14:05 -0700112 Glop glop;
113 GlopBuilder(renderState, caches, &glop)
114 .setRoundRectClipState(nullptr)
115 .setMeshTexturedUnitQuad(nullptr)
John Reck95801462016-09-01 09:44:09 -0700116 .setFillExternalTexture(sourceTexture, croppedTexTransform)
Chris Craik764045d2016-07-06 17:14:05 -0700117 .setTransform(Matrix4::identity(), TransformFlags::None)
118 .setModelViewMapUnitToRect(Rect(destWidth, destHeight))
119 .build();
120 Matrix4 ortho;
121 ortho.loadOrtho(destWidth, destHeight);
122 renderState.render(glop, ortho);
123
124 glReadPixels(0, 0, bitmap->width(), bitmap->height(), format,
125 type, bitmap->getPixels());
126 }
127
128 // Cleanup
129 caches.textureState().deleteTexture(texture);
130 renderState.deleteFramebuffer(fbo);
131
132 GL_CHECKPOINT(MODERATE);
133
134 return CopyResult::Success;
135}
136
137CopyResult Readback::copySurfaceInto(renderthread::RenderThread& renderThread,
John Reck95801462016-09-01 09:44:09 -0700138 Surface& surface, const Rect& srcRect, SkBitmap* bitmap) {
139 ATRACE_CALL();
Chris Craik764045d2016-07-06 17:14:05 -0700140 renderThread.eglManager().initialize();
141
142 Caches& caches = Caches::getInstance();
143
John Reck10dd0582016-03-31 16:36:16 -0700144 // Setup the source
145 sp<GraphicBuffer> sourceBuffer;
146 sp<Fence> sourceFence;
John Reck2f69d6d2016-04-28 13:18:51 -0700147 Matrix4 texTransform;
148 status_t err = surface.getLastQueuedBuffer(&sourceBuffer, &sourceFence,
149 texTransform.data);
150 texTransform.invalidateType();
John Reck2f783272016-04-19 07:51:13 -0700151 if (err != NO_ERROR) {
152 ALOGW("Failed to get last queued buffer, error = %d", err);
John Recke94cbc72016-04-25 13:03:44 -0700153 return CopyResult::UnknownError;
John Reck2f783272016-04-19 07:51:13 -0700154 }
John Reck10dd0582016-03-31 16:36:16 -0700155 if (!sourceBuffer.get()) {
156 ALOGW("Surface doesn't have any previously queued frames, nothing to readback from");
John Recke94cbc72016-04-25 13:03:44 -0700157 return CopyResult::SourceEmpty;
158 }
159 if (sourceBuffer->getUsage() & GRALLOC_USAGE_PROTECTED) {
160 ALOGW("Surface is protected, unable to copy from it");
161 return CopyResult::SourceInvalid;
John Reck10dd0582016-03-31 16:36:16 -0700162 }
John Reck2f783272016-04-19 07:51:13 -0700163 err = sourceFence->wait(500 /* ms */);
John Reck10dd0582016-03-31 16:36:16 -0700164 if (err != NO_ERROR) {
165 ALOGE("Timeout (500ms) exceeded waiting for buffer fence, abandoning readback attempt");
John Recke94cbc72016-04-25 13:03:44 -0700166 return CopyResult::Timeout;
John Reck10dd0582016-03-31 16:36:16 -0700167 }
John Reck2f783272016-04-19 07:51:13 -0700168
169 // TODO: Can't use Image helper since it forces GL_TEXTURE_2D usage via
170 // GL_OES_EGL_image, which doesn't work since we need samplerExternalOES
171 // to be able to properly sample from the buffer.
172
173 // Create the EGLImage object that maps the GraphicBuffer
174 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
175 EGLClientBuffer clientBuffer = (EGLClientBuffer) sourceBuffer->getNativeBuffer();
176 EGLint attrs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE };
177
178 EGLImageKHR sourceImage = eglCreateImageKHR(display, EGL_NO_CONTEXT,
179 EGL_NATIVE_BUFFER_ANDROID, clientBuffer, attrs);
180
181 if (sourceImage == EGL_NO_IMAGE_KHR) {
John Reck8a29c0e2016-09-01 13:04:00 -0700182 ALOGW("eglCreateImageKHR failed (%#x)", eglGetError());
John Recke94cbc72016-04-25 13:03:44 -0700183 return CopyResult::UnknownError;
John Reck10dd0582016-03-31 16:36:16 -0700184 }
John Reck2f783272016-04-19 07:51:13 -0700185 GLuint sourceTexId;
186 // Create a 2D texture to sample from the EGLImage
187 glGenTextures(1, &sourceTexId);
Chris Craik764045d2016-07-06 17:14:05 -0700188 caches.textureState().bindTexture(GL_TEXTURE_EXTERNAL_OES, sourceTexId);
John Reck2f783272016-04-19 07:51:13 -0700189 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, sourceImage);
190
191 GLenum status = GL_NO_ERROR;
192 while ((status = glGetError()) != GL_NO_ERROR) {
John Reck8a29c0e2016-09-01 13:04:00 -0700193 ALOGW("glEGLImageTargetTexture2DOES failed (%#x)", status);
194 eglDestroyImageKHR(display, sourceImage);
John Recke94cbc72016-04-25 13:03:44 -0700195 return CopyResult::UnknownError;
John Reck2f783272016-04-19 07:51:13 -0700196 }
197
John Reck10dd0582016-03-31 16:36:16 -0700198 Texture sourceTexture(caches);
John Reck2f783272016-04-19 07:51:13 -0700199 sourceTexture.wrap(sourceTexId,
John Reck10dd0582016-03-31 16:36:16 -0700200 sourceBuffer->getWidth(), sourceBuffer->getHeight(), 0 /* total lie */);
201
John Reck8a29c0e2016-09-01 13:04:00 -0700202 CopyResult copyResult = copyTextureInto(caches, renderThread.renderState(),
John Reck95801462016-09-01 09:44:09 -0700203 sourceTexture, texTransform, srcRect, bitmap);
John Reck8a29c0e2016-09-01 13:04:00 -0700204 sourceTexture.deleteTexture();
205 // All we're flushing & finishing is the deletion of the texture since
206 // copyTextureInto already did a major flush & finish as an implicit
207 // part of glReadPixels, so this shouldn't pose any major stalls.
208 glFinish();
209 eglDestroyImageKHR(display, sourceImage);
210 return copyResult;
Chris Craik764045d2016-07-06 17:14:05 -0700211}
John Reck10dd0582016-03-31 16:36:16 -0700212
Chris Craik764045d2016-07-06 17:14:05 -0700213CopyResult Readback::copyTextureLayerInto(renderthread::RenderThread& renderThread,
214 Layer& layer, SkBitmap* bitmap) {
John Reck95801462016-09-01 09:44:09 -0700215 ATRACE_CALL();
Chris Craik764045d2016-07-06 17:14:05 -0700216 return copyTextureInto(Caches::getInstance(), renderThread.renderState(),
John Reck95801462016-09-01 09:44:09 -0700217 layer.getTexture(), layer.getTexTransform(), Rect(), bitmap);
John Reck10dd0582016-03-31 16:36:16 -0700218}
219
220} // namespace uirenderer
221} // namespace android