John Reck | 44627c2 | 2018-04-12 13:55:38 -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 | |
| 17 | #include "EglReadback.h" |
| 18 | |
| 19 | #include "renderthread/EglManager.h" |
| 20 | |
| 21 | #include <gui/Surface.h> |
| 22 | #include <ui/Fence.h> |
| 23 | #include <ui/GraphicBuffer.h> |
| 24 | |
| 25 | namespace android { |
| 26 | namespace uirenderer { |
| 27 | |
| 28 | CopyResult EglReadback::copySurfaceInto(Surface& surface, const Rect& srcRect, |
| 29 | SkBitmap* bitmap) { |
| 30 | ATRACE_CALL(); |
| 31 | // Setup the source |
| 32 | sp<GraphicBuffer> sourceBuffer; |
| 33 | sp<Fence> sourceFence; |
| 34 | Matrix4 texTransform; |
| 35 | status_t err = surface.getLastQueuedBuffer(&sourceBuffer, &sourceFence, texTransform.data); |
| 36 | texTransform.invalidateType(); |
| 37 | if (err != NO_ERROR) { |
| 38 | ALOGW("Failed to get last queued buffer, error = %d", err); |
| 39 | return CopyResult::UnknownError; |
| 40 | } |
| 41 | if (!sourceBuffer.get()) { |
| 42 | ALOGW("Surface doesn't have any previously queued frames, nothing to readback from"); |
| 43 | return CopyResult::SourceEmpty; |
| 44 | } |
| 45 | if (sourceBuffer->getUsage() & GRALLOC_USAGE_PROTECTED) { |
| 46 | ALOGW("Surface is protected, unable to copy from it"); |
| 47 | return CopyResult::SourceInvalid; |
| 48 | } |
| 49 | err = sourceFence->wait(500 /* ms */); |
| 50 | if (err != NO_ERROR) { |
| 51 | ALOGE("Timeout (500ms) exceeded waiting for buffer fence, abandoning readback attempt"); |
| 52 | return CopyResult::Timeout; |
| 53 | } |
| 54 | |
| 55 | return copyGraphicBufferInto(sourceBuffer.get(), texTransform, srcRect, bitmap); |
| 56 | } |
| 57 | |
| 58 | CopyResult EglReadback::copyGraphicBufferInto(GraphicBuffer* graphicBuffer, |
| 59 | Matrix4& texTransform, const Rect& srcRect, |
| 60 | SkBitmap* bitmap) { |
| 61 | mRenderThread.eglManager().initialize(); |
| 62 | // TODO: Can't use Image helper since it forces GL_TEXTURE_2D usage via |
| 63 | // GL_OES_EGL_image, which doesn't work since we need samplerExternalOES |
| 64 | // to be able to properly sample from the buffer. |
| 65 | |
| 66 | // Create the EGLImage object that maps the GraphicBuffer |
| 67 | EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 68 | EGLClientBuffer clientBuffer = (EGLClientBuffer)graphicBuffer->getNativeBuffer(); |
| 69 | EGLint attrs[] = {EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE}; |
| 70 | |
| 71 | EGLImageKHR sourceImage = eglCreateImageKHR(display, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, |
| 72 | clientBuffer, attrs); |
| 73 | |
| 74 | if (sourceImage == EGL_NO_IMAGE_KHR) { |
| 75 | ALOGW("eglCreateImageKHR failed (%#x)", eglGetError()); |
| 76 | return CopyResult::UnknownError; |
| 77 | } |
| 78 | |
| 79 | uint32_t width = graphicBuffer->getWidth(); |
| 80 | uint32_t height = graphicBuffer->getHeight(); |
| 81 | CopyResult copyResult = |
| 82 | copyImageInto(sourceImage, texTransform, width, height, srcRect, bitmap); |
| 83 | |
| 84 | eglDestroyImageKHR(display, sourceImage); |
| 85 | return copyResult; |
| 86 | } |
| 87 | |
| 88 | CopyResult EglReadback::copyGraphicBufferInto(GraphicBuffer* graphicBuffer, SkBitmap* bitmap) { |
| 89 | Rect srcRect; |
| 90 | Matrix4 transform; |
| 91 | transform.loadScale(1, -1, 1); |
| 92 | transform.translate(0, -1); |
| 93 | return copyGraphicBufferInto(graphicBuffer, transform, srcRect, bitmap); |
| 94 | } |
| 95 | |
| 96 | } // namespace uirenderer |
| 97 | } // namespace android |