John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 1 | /* |
| 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 Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 17 | #include "OpenGLReadback.h" |
John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 18 | |
| 19 | #include "Caches.h" |
| 20 | #include "Image.h" |
| 21 | #include "GlopBuilder.h" |
Greg Daniel | 8cd3edf | 2017-01-09 14:15:41 -0500 | [diff] [blame] | 22 | #include "GlLayer.h" |
John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 23 | #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> |
Mathias Agopian | 3d7d5f9 | 2017-05-01 15:13:20 -0700 | [diff] [blame] | 30 | #include <gui/Surface.h> |
John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | namespace uirenderer { |
| 34 | |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 35 | CopyResult OpenGLReadback::copySurfaceInto(Surface& surface, const Rect& srcRect, |
| 36 | SkBitmap* bitmap) { |
| 37 | ATRACE_CALL(); |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 38 | // Setup the source |
| 39 | sp<GraphicBuffer> sourceBuffer; |
| 40 | sp<Fence> sourceFence; |
| 41 | Matrix4 texTransform; |
| 42 | status_t err = surface.getLastQueuedBuffer(&sourceBuffer, &sourceFence, |
| 43 | texTransform.data); |
| 44 | texTransform.invalidateType(); |
| 45 | if (err != NO_ERROR) { |
| 46 | ALOGW("Failed to get last queued buffer, error = %d", err); |
| 47 | return CopyResult::UnknownError; |
| 48 | } |
| 49 | if (!sourceBuffer.get()) { |
| 50 | ALOGW("Surface doesn't have any previously queued frames, nothing to readback from"); |
| 51 | return CopyResult::SourceEmpty; |
| 52 | } |
| 53 | if (sourceBuffer->getUsage() & GRALLOC_USAGE_PROTECTED) { |
| 54 | ALOGW("Surface is protected, unable to copy from it"); |
| 55 | return CopyResult::SourceInvalid; |
| 56 | } |
| 57 | err = sourceFence->wait(500 /* ms */); |
| 58 | if (err != NO_ERROR) { |
| 59 | ALOGE("Timeout (500ms) exceeded waiting for buffer fence, abandoning readback attempt"); |
| 60 | return CopyResult::Timeout; |
| 61 | } |
| 62 | |
sergeyv | 59eecb52 | 2016-11-17 17:54:57 -0800 | [diff] [blame] | 63 | return copyGraphicBufferInto(sourceBuffer.get(), texTransform, srcRect, bitmap); |
| 64 | } |
| 65 | |
| 66 | CopyResult OpenGLReadback::copyGraphicBufferInto(GraphicBuffer* graphicBuffer, |
| 67 | Matrix4& texTransform, const Rect& srcRect, SkBitmap* bitmap) { |
| 68 | mRenderThread.eglManager().initialize(); |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 69 | // TODO: Can't use Image helper since it forces GL_TEXTURE_2D usage via |
| 70 | // GL_OES_EGL_image, which doesn't work since we need samplerExternalOES |
| 71 | // to be able to properly sample from the buffer. |
| 72 | |
| 73 | // Create the EGLImage object that maps the GraphicBuffer |
| 74 | EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
sergeyv | 59eecb52 | 2016-11-17 17:54:57 -0800 | [diff] [blame] | 75 | EGLClientBuffer clientBuffer = (EGLClientBuffer) graphicBuffer->getNativeBuffer(); |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 76 | EGLint attrs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE }; |
| 77 | |
| 78 | EGLImageKHR sourceImage = eglCreateImageKHR(display, EGL_NO_CONTEXT, |
| 79 | EGL_NATIVE_BUFFER_ANDROID, clientBuffer, attrs); |
| 80 | |
| 81 | if (sourceImage == EGL_NO_IMAGE_KHR) { |
| 82 | ALOGW("eglCreateImageKHR failed (%#x)", eglGetError()); |
| 83 | return CopyResult::UnknownError; |
| 84 | } |
| 85 | |
John Reck | 912bebe | 2016-12-07 16:36:15 -0800 | [diff] [blame] | 86 | uint32_t width = graphicBuffer->getWidth(); |
| 87 | uint32_t height = graphicBuffer->getHeight(); |
| 88 | // If this is a 90 or 270 degree rotation we need to swap width/height |
| 89 | // This is a fuzzy way of checking that. |
| 90 | if (texTransform[Matrix4::kSkewX] >= 0.5f || texTransform[Matrix4::kSkewX] <= -0.5f) { |
| 91 | std::swap(width, height); |
| 92 | } |
| 93 | CopyResult copyResult = copyImageInto(sourceImage, texTransform, width, height, |
| 94 | srcRect, bitmap); |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 95 | |
| 96 | // All we're flushing & finishing is the deletion of the texture since |
| 97 | // copyImageInto already did a major flush & finish as an implicit |
| 98 | // part of glReadPixels, so this shouldn't pose any major stalls. |
| 99 | glFinish(); |
| 100 | eglDestroyImageKHR(display, sourceImage); |
| 101 | return copyResult; |
| 102 | } |
| 103 | |
sergeyv | 59eecb52 | 2016-11-17 17:54:57 -0800 | [diff] [blame] | 104 | CopyResult OpenGLReadback::copyGraphicBufferInto(GraphicBuffer* graphicBuffer, SkBitmap* bitmap) { |
| 105 | Rect srcRect; |
| 106 | Matrix4 transform; |
| 107 | transform.loadScale(1, -1, 1); |
| 108 | transform.translate(0, -1); |
| 109 | return copyGraphicBufferInto(graphicBuffer, transform, srcRect, bitmap); |
| 110 | } |
| 111 | |
John Reck | eb418eda | 2016-12-15 10:26:33 -0800 | [diff] [blame] | 112 | static float sFlipVInit[16] = { |
| 113 | 1, 0, 0, 0, |
| 114 | 0, -1, 0, 0, |
| 115 | 0, 0, 1, 0, |
| 116 | 0, 1, 0, 1, |
| 117 | }; |
| 118 | |
| 119 | static const Matrix4 sFlipV(sFlipVInit); |
| 120 | |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 121 | //////////////////////////////////////////////////////////////////////////////// |
| 122 | //////////////////////////////////////////////////////////////////////////////// |
| 123 | |
| 124 | inline CopyResult copyTextureInto(Caches& caches, RenderState& renderState, |
| 125 | Texture& sourceTexture, const Matrix4& texTransform, const Rect& srcRect, |
John Reck | 9580146 | 2016-09-01 09:44:09 -0700 | [diff] [blame] | 126 | SkBitmap* bitmap) { |
John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 127 | int destWidth = bitmap->width(); |
| 128 | int destHeight = bitmap->height(); |
| 129 | if (destWidth > caches.maxTextureSize |
| 130 | || destHeight > caches.maxTextureSize) { |
| 131 | ALOGW("Can't copy surface into bitmap, %dx%d exceeds max texture size %d", |
| 132 | destWidth, destHeight, caches.maxTextureSize); |
John Reck | e94cbc7 | 2016-04-25 13:03:44 -0700 | [diff] [blame] | 133 | return CopyResult::DestinationInvalid; |
John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 134 | } |
Romain Guy | 9505a65 | 2016-12-14 09:43:50 -0800 | [diff] [blame] | 135 | |
Romain Guy | 88e060f | 2017-06-16 18:07:54 -0700 | [diff] [blame] | 136 | if (bitmap->colorType() == kRGBA_F16_SkColorType && !caches.extensions().hasFloatTextures()) { |
Romain Guy | 9505a65 | 2016-12-14 09:43:50 -0800 | [diff] [blame] | 137 | ALOGW("Can't copy surface into bitmap, RGBA_F16 config is not supported"); |
| 138 | return CopyResult::DestinationInvalid; |
| 139 | } |
| 140 | |
John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 141 | GLuint fbo = renderState.createFramebuffer(); |
| 142 | if (!fbo) { |
| 143 | ALOGW("Could not obtain an FBO"); |
John Reck | e94cbc7 | 2016-04-25 13:03:44 -0700 | [diff] [blame] | 144 | return CopyResult::UnknownError; |
John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 145 | } |
| 146 | |
John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 147 | GLuint texture; |
| 148 | |
| 149 | GLenum format; |
Romain Guy | 88e060f | 2017-06-16 18:07:54 -0700 | [diff] [blame] | 150 | GLenum internalFormat; |
John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 151 | GLenum type; |
| 152 | |
| 153 | switch (bitmap->colorType()) { |
| 154 | case kAlpha_8_SkColorType: |
| 155 | format = GL_ALPHA; |
Romain Guy | 88e060f | 2017-06-16 18:07:54 -0700 | [diff] [blame] | 156 | internalFormat = GL_ALPHA; |
John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 157 | type = GL_UNSIGNED_BYTE; |
| 158 | break; |
| 159 | case kRGB_565_SkColorType: |
| 160 | format = GL_RGB; |
Romain Guy | 88e060f | 2017-06-16 18:07:54 -0700 | [diff] [blame] | 161 | internalFormat = GL_RGB; |
John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 162 | type = GL_UNSIGNED_SHORT_5_6_5; |
| 163 | break; |
| 164 | case kARGB_4444_SkColorType: |
| 165 | format = GL_RGBA; |
Romain Guy | 88e060f | 2017-06-16 18:07:54 -0700 | [diff] [blame] | 166 | internalFormat = GL_RGBA; |
John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 167 | type = GL_UNSIGNED_SHORT_4_4_4_4; |
| 168 | break; |
Romain Guy | 88e060f | 2017-06-16 18:07:54 -0700 | [diff] [blame] | 169 | case kRGBA_F16_SkColorType: |
| 170 | format = GL_RGBA; |
| 171 | internalFormat = GL_RGBA16F; |
| 172 | type = GL_HALF_FLOAT; |
| 173 | break; |
John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 174 | case kN32_SkColorType: |
| 175 | default: |
| 176 | format = GL_RGBA; |
Romain Guy | 88e060f | 2017-06-16 18:07:54 -0700 | [diff] [blame] | 177 | internalFormat = GL_RGBA; |
John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 178 | type = GL_UNSIGNED_BYTE; |
| 179 | break; |
| 180 | } |
| 181 | |
| 182 | renderState.bindFramebuffer(fbo); |
| 183 | |
| 184 | // TODO: Use layerPool or something to get this maybe? But since we |
| 185 | // need explicit format control we can't currently. |
| 186 | |
| 187 | // Setup the rendertarget |
| 188 | glGenTextures(1, &texture); |
| 189 | caches.textureState().activateTexture(0); |
| 190 | caches.textureState().bindTexture(texture); |
| 191 | glPixelStorei(GL_PACK_ALIGNMENT, bitmap->bytesPerPixel()); |
| 192 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 193 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 194 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 195 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
Romain Guy | 88e060f | 2017-06-16 18:07:54 -0700 | [diff] [blame] | 196 | glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, destWidth, destHeight, |
John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 197 | 0, format, type, nullptr); |
| 198 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, |
| 199 | GL_TEXTURE_2D, texture, 0); |
| 200 | |
Chris Craik | 764045d | 2016-07-06 17:14:05 -0700 | [diff] [blame] | 201 | { |
John Reck | 7bf96a0 | 2017-06-28 11:08:07 -0700 | [diff] [blame] | 202 | bool requiresFilter; |
Chris Craik | 764045d | 2016-07-06 17:14:05 -0700 | [diff] [blame] | 203 | // Draw & readback |
| 204 | renderState.setViewport(destWidth, destHeight); |
| 205 | renderState.scissor().setEnabled(false); |
| 206 | renderState.blend().syncEnabled(); |
| 207 | renderState.stencil().disable(); |
| 208 | |
John Reck | 9580146 | 2016-09-01 09:44:09 -0700 | [diff] [blame] | 209 | Matrix4 croppedTexTransform(texTransform); |
| 210 | if (!srcRect.isEmpty()) { |
John Reck | eb418eda | 2016-12-15 10:26:33 -0800 | [diff] [blame] | 211 | // We flipV to convert to 0,0 top-left for the srcRect |
| 212 | // coordinates then flip back to 0,0 bottom-left for |
| 213 | // GLES coordinates. |
| 214 | croppedTexTransform.multiply(sFlipV); |
| 215 | croppedTexTransform.translate(srcRect.left / sourceTexture.width(), |
John Reck | 9580146 | 2016-09-01 09:44:09 -0700 | [diff] [blame] | 216 | srcRect.top / sourceTexture.height(), 0); |
| 217 | croppedTexTransform.scale(srcRect.getWidth() / sourceTexture.width(), |
| 218 | srcRect.getHeight() / sourceTexture.height(), 1); |
John Reck | eb418eda | 2016-12-15 10:26:33 -0800 | [diff] [blame] | 219 | croppedTexTransform.multiply(sFlipV); |
John Reck | 7bf96a0 | 2017-06-28 11:08:07 -0700 | [diff] [blame] | 220 | requiresFilter = srcRect.getWidth() != (float) destWidth |
| 221 | || srcRect.getHeight() != (float) destHeight; |
| 222 | } else { |
| 223 | requiresFilter = sourceTexture.width() != (uint32_t) destWidth |
| 224 | || sourceTexture.height() != (uint32_t) destHeight; |
John Reck | 9580146 | 2016-09-01 09:44:09 -0700 | [diff] [blame] | 225 | } |
Chris Craik | 764045d | 2016-07-06 17:14:05 -0700 | [diff] [blame] | 226 | Glop glop; |
| 227 | GlopBuilder(renderState, caches, &glop) |
| 228 | .setRoundRectClipState(nullptr) |
| 229 | .setMeshTexturedUnitQuad(nullptr) |
John Reck | 7bf96a0 | 2017-06-28 11:08:07 -0700 | [diff] [blame] | 230 | .setFillExternalTexture(sourceTexture, croppedTexTransform, requiresFilter) |
Chris Craik | 764045d | 2016-07-06 17:14:05 -0700 | [diff] [blame] | 231 | .setTransform(Matrix4::identity(), TransformFlags::None) |
| 232 | .setModelViewMapUnitToRect(Rect(destWidth, destHeight)) |
| 233 | .build(); |
| 234 | Matrix4 ortho; |
| 235 | ortho.loadOrtho(destWidth, destHeight); |
| 236 | renderState.render(glop, ortho); |
| 237 | |
Romain Guy | 88e060f | 2017-06-16 18:07:54 -0700 | [diff] [blame] | 238 | // TODO: We should convert to linear space when the target is RGBA16F |
Chris Craik | 764045d | 2016-07-06 17:14:05 -0700 | [diff] [blame] | 239 | glReadPixels(0, 0, bitmap->width(), bitmap->height(), format, |
| 240 | type, bitmap->getPixels()); |
| 241 | } |
| 242 | |
| 243 | // Cleanup |
| 244 | caches.textureState().deleteTexture(texture); |
| 245 | renderState.deleteFramebuffer(fbo); |
| 246 | |
| 247 | GL_CHECKPOINT(MODERATE); |
| 248 | |
| 249 | return CopyResult::Success; |
| 250 | } |
| 251 | |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 252 | CopyResult OpenGLReadbackImpl::copyImageInto(EGLImageKHR eglImage, |
| 253 | const Matrix4& imgTransform, int imgWidth, int imgHeight, const Rect& srcRect, |
| 254 | SkBitmap* bitmap) { |
Chris Craik | 764045d | 2016-07-06 17:14:05 -0700 | [diff] [blame] | 255 | |
| 256 | Caches& caches = Caches::getInstance(); |
John Reck | 2f78327 | 2016-04-19 07:51:13 -0700 | [diff] [blame] | 257 | GLuint sourceTexId; |
| 258 | // Create a 2D texture to sample from the EGLImage |
| 259 | glGenTextures(1, &sourceTexId); |
Chris Craik | 764045d | 2016-07-06 17:14:05 -0700 | [diff] [blame] | 260 | caches.textureState().bindTexture(GL_TEXTURE_EXTERNAL_OES, sourceTexId); |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 261 | glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, eglImage); |
John Reck | 2f78327 | 2016-04-19 07:51:13 -0700 | [diff] [blame] | 262 | |
| 263 | GLenum status = GL_NO_ERROR; |
| 264 | while ((status = glGetError()) != GL_NO_ERROR) { |
John Reck | 8a29c0e | 2016-09-01 13:04:00 -0700 | [diff] [blame] | 265 | ALOGW("glEGLImageTargetTexture2DOES failed (%#x)", status); |
John Reck | e94cbc7 | 2016-04-25 13:03:44 -0700 | [diff] [blame] | 266 | return CopyResult::UnknownError; |
John Reck | 2f78327 | 2016-04-19 07:51:13 -0700 | [diff] [blame] | 267 | } |
| 268 | |
John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 269 | Texture sourceTexture(caches); |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 270 | sourceTexture.wrap(sourceTexId, imgWidth, imgHeight, 0, 0 /* total lie */, |
| 271 | GL_TEXTURE_EXTERNAL_OES); |
John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 272 | |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 273 | CopyResult copyResult = copyTextureInto(caches, mRenderThread.renderState(), |
| 274 | sourceTexture, imgTransform, srcRect, bitmap); |
John Reck | 8a29c0e | 2016-09-01 13:04:00 -0700 | [diff] [blame] | 275 | sourceTexture.deleteTexture(); |
John Reck | 8a29c0e | 2016-09-01 13:04:00 -0700 | [diff] [blame] | 276 | return copyResult; |
Chris Craik | 764045d | 2016-07-06 17:14:05 -0700 | [diff] [blame] | 277 | } |
John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 278 | |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 279 | bool OpenGLReadbackImpl::copyLayerInto(renderthread::RenderThread& renderThread, |
Greg Daniel | 8cd3edf | 2017-01-09 14:15:41 -0500 | [diff] [blame] | 280 | GlLayer& layer, SkBitmap* bitmap) { |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 281 | return CopyResult::Success == copyTextureInto(Caches::getInstance(), |
| 282 | renderThread.renderState(), layer.getTexture(), layer.getTexTransform(), |
| 283 | Rect(), bitmap); |
John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 286 | |
John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 287 | } // namespace uirenderer |
| 288 | } // namespace android |