blob: 65a8ebba2ae7a15d1ff1e505e74ea0152b0cd002 [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,
35 Texture& sourceTexture, Matrix4& texTransform, SkBitmap* bitmap) {
John Reck10dd0582016-03-31 16:36:16 -070036 int destWidth = bitmap->width();
37 int destHeight = bitmap->height();
38 if (destWidth > caches.maxTextureSize
39 || destHeight > caches.maxTextureSize) {
40 ALOGW("Can't copy surface into bitmap, %dx%d exceeds max texture size %d",
41 destWidth, destHeight, caches.maxTextureSize);
John Recke94cbc72016-04-25 13:03:44 -070042 return CopyResult::DestinationInvalid;
John Reck10dd0582016-03-31 16:36:16 -070043 }
44 GLuint fbo = renderState.createFramebuffer();
45 if (!fbo) {
46 ALOGW("Could not obtain an FBO");
John Recke94cbc72016-04-25 13:03:44 -070047 return CopyResult::UnknownError;
John Reck10dd0582016-03-31 16:36:16 -070048 }
49
50 SkAutoLockPixels alp(*bitmap);
51
52 GLuint texture;
53
54 GLenum format;
55 GLenum type;
56
57 switch (bitmap->colorType()) {
58 case kAlpha_8_SkColorType:
59 format = GL_ALPHA;
60 type = GL_UNSIGNED_BYTE;
61 break;
62 case kRGB_565_SkColorType:
63 format = GL_RGB;
64 type = GL_UNSIGNED_SHORT_5_6_5;
65 break;
66 case kARGB_4444_SkColorType:
67 format = GL_RGBA;
68 type = GL_UNSIGNED_SHORT_4_4_4_4;
69 break;
70 case kN32_SkColorType:
71 default:
72 format = GL_RGBA;
73 type = GL_UNSIGNED_BYTE;
74 break;
75 }
76
77 renderState.bindFramebuffer(fbo);
78
79 // TODO: Use layerPool or something to get this maybe? But since we
80 // need explicit format control we can't currently.
81
82 // Setup the rendertarget
83 glGenTextures(1, &texture);
84 caches.textureState().activateTexture(0);
85 caches.textureState().bindTexture(texture);
86 glPixelStorei(GL_PACK_ALIGNMENT, bitmap->bytesPerPixel());
87 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
88 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
89 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
90 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
91 glTexImage2D(GL_TEXTURE_2D, 0, format, destWidth, destHeight,
92 0, format, type, nullptr);
93 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
94 GL_TEXTURE_2D, texture, 0);
95
Chris Craik764045d2016-07-06 17:14:05 -070096 {
97 // Draw & readback
98 renderState.setViewport(destWidth, destHeight);
99 renderState.scissor().setEnabled(false);
100 renderState.blend().syncEnabled();
101 renderState.stencil().disable();
102
103 Glop glop;
104 GlopBuilder(renderState, caches, &glop)
105 .setRoundRectClipState(nullptr)
106 .setMeshTexturedUnitQuad(nullptr)
107 .setFillExternalTexture(sourceTexture, texTransform)
108 .setTransform(Matrix4::identity(), TransformFlags::None)
109 .setModelViewMapUnitToRect(Rect(destWidth, destHeight))
110 .build();
111 Matrix4 ortho;
112 ortho.loadOrtho(destWidth, destHeight);
113 renderState.render(glop, ortho);
114
115 glReadPixels(0, 0, bitmap->width(), bitmap->height(), format,
116 type, bitmap->getPixels());
117 }
118
119 // Cleanup
120 caches.textureState().deleteTexture(texture);
121 renderState.deleteFramebuffer(fbo);
122
123 GL_CHECKPOINT(MODERATE);
124
125 return CopyResult::Success;
126}
127
128CopyResult Readback::copySurfaceInto(renderthread::RenderThread& renderThread,
129 Surface& surface, SkBitmap* bitmap) {
130 renderThread.eglManager().initialize();
131
132 Caches& caches = Caches::getInstance();
133
John Reck10dd0582016-03-31 16:36:16 -0700134 // Setup the source
135 sp<GraphicBuffer> sourceBuffer;
136 sp<Fence> sourceFence;
John Reck2f69d6d2016-04-28 13:18:51 -0700137 Matrix4 texTransform;
138 status_t err = surface.getLastQueuedBuffer(&sourceBuffer, &sourceFence,
139 texTransform.data);
140 texTransform.invalidateType();
John Reck2f783272016-04-19 07:51:13 -0700141 if (err != NO_ERROR) {
142 ALOGW("Failed to get last queued buffer, error = %d", err);
John Recke94cbc72016-04-25 13:03:44 -0700143 return CopyResult::UnknownError;
John Reck2f783272016-04-19 07:51:13 -0700144 }
John Reck10dd0582016-03-31 16:36:16 -0700145 if (!sourceBuffer.get()) {
146 ALOGW("Surface doesn't have any previously queued frames, nothing to readback from");
John Recke94cbc72016-04-25 13:03:44 -0700147 return CopyResult::SourceEmpty;
148 }
149 if (sourceBuffer->getUsage() & GRALLOC_USAGE_PROTECTED) {
150 ALOGW("Surface is protected, unable to copy from it");
151 return CopyResult::SourceInvalid;
John Reck10dd0582016-03-31 16:36:16 -0700152 }
John Reck2f783272016-04-19 07:51:13 -0700153 err = sourceFence->wait(500 /* ms */);
John Reck10dd0582016-03-31 16:36:16 -0700154 if (err != NO_ERROR) {
155 ALOGE("Timeout (500ms) exceeded waiting for buffer fence, abandoning readback attempt");
John Recke94cbc72016-04-25 13:03:44 -0700156 return CopyResult::Timeout;
John Reck10dd0582016-03-31 16:36:16 -0700157 }
John Reck2f783272016-04-19 07:51:13 -0700158
159 // TODO: Can't use Image helper since it forces GL_TEXTURE_2D usage via
160 // GL_OES_EGL_image, which doesn't work since we need samplerExternalOES
161 // to be able to properly sample from the buffer.
162
163 // Create the EGLImage object that maps the GraphicBuffer
164 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
165 EGLClientBuffer clientBuffer = (EGLClientBuffer) sourceBuffer->getNativeBuffer();
166 EGLint attrs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE };
167
168 EGLImageKHR sourceImage = eglCreateImageKHR(display, EGL_NO_CONTEXT,
169 EGL_NATIVE_BUFFER_ANDROID, clientBuffer, attrs);
170
171 if (sourceImage == EGL_NO_IMAGE_KHR) {
John Reck8a29c0e2016-09-01 13:04:00 -0700172 ALOGW("eglCreateImageKHR failed (%#x)", eglGetError());
John Recke94cbc72016-04-25 13:03:44 -0700173 return CopyResult::UnknownError;
John Reck10dd0582016-03-31 16:36:16 -0700174 }
John Reck2f783272016-04-19 07:51:13 -0700175 GLuint sourceTexId;
176 // Create a 2D texture to sample from the EGLImage
177 glGenTextures(1, &sourceTexId);
Chris Craik764045d2016-07-06 17:14:05 -0700178 caches.textureState().bindTexture(GL_TEXTURE_EXTERNAL_OES, sourceTexId);
John Reck2f783272016-04-19 07:51:13 -0700179 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, sourceImage);
180
181 GLenum status = GL_NO_ERROR;
182 while ((status = glGetError()) != GL_NO_ERROR) {
John Reck8a29c0e2016-09-01 13:04:00 -0700183 ALOGW("glEGLImageTargetTexture2DOES failed (%#x)", status);
184 eglDestroyImageKHR(display, sourceImage);
John Recke94cbc72016-04-25 13:03:44 -0700185 return CopyResult::UnknownError;
John Reck2f783272016-04-19 07:51:13 -0700186 }
187
John Reck10dd0582016-03-31 16:36:16 -0700188 Texture sourceTexture(caches);
John Reck2f783272016-04-19 07:51:13 -0700189 sourceTexture.wrap(sourceTexId,
John Reck10dd0582016-03-31 16:36:16 -0700190 sourceBuffer->getWidth(), sourceBuffer->getHeight(), 0 /* total lie */);
191
John Reck8a29c0e2016-09-01 13:04:00 -0700192 CopyResult copyResult = copyTextureInto(caches, renderThread.renderState(),
193 sourceTexture, texTransform, bitmap);
194 sourceTexture.deleteTexture();
195 // All we're flushing & finishing is the deletion of the texture since
196 // copyTextureInto already did a major flush & finish as an implicit
197 // part of glReadPixels, so this shouldn't pose any major stalls.
198 glFinish();
199 eglDestroyImageKHR(display, sourceImage);
200 return copyResult;
Chris Craik764045d2016-07-06 17:14:05 -0700201}
John Reck10dd0582016-03-31 16:36:16 -0700202
Chris Craik764045d2016-07-06 17:14:05 -0700203CopyResult Readback::copyTextureLayerInto(renderthread::RenderThread& renderThread,
204 Layer& layer, SkBitmap* bitmap) {
205 return copyTextureInto(Caches::getInstance(), renderThread.renderState(),
206 layer.getTexture(), layer.getTexTransform(), bitmap);
John Reck10dd0582016-03-31 16:36:16 -0700207}
208
209} // namespace uirenderer
210} // namespace android