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