Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [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 "SkiaOpenGLReadback.h" |
| 18 | |
Derek Sollenberger | 4170db3 | 2017-08-09 13:52:36 -0400 | [diff] [blame] | 19 | #include "DeviceInfo.h" |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 20 | #include "Matrix.h" |
| 21 | #include "Properties.h" |
| 22 | #include <SkCanvas.h> |
| 23 | #include <SkSurface.h> |
Greg Daniel | ac2d232 | 2017-07-12 11:30:15 -0400 | [diff] [blame] | 24 | #include <GrBackendSurface.h> |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 25 | #include <gl/GrGLInterface.h> |
| 26 | #include <gl/GrGLTypes.h> |
| 27 | #include <GLES2/gl2.h> |
| 28 | #include <GLES2/gl2ext.h> |
| 29 | |
| 30 | using namespace android::uirenderer::renderthread; |
| 31 | |
| 32 | namespace android { |
| 33 | namespace uirenderer { |
| 34 | namespace skiapipeline { |
| 35 | |
| 36 | CopyResult SkiaOpenGLReadback::copyImageInto(EGLImageKHR eglImage, const Matrix4& imgTransform, |
| 37 | int imgWidth, int imgHeight, const Rect& srcRect, SkBitmap* bitmap) { |
| 38 | |
| 39 | GLuint sourceTexId; |
| 40 | glGenTextures(1, &sourceTexId); |
| 41 | glBindTexture(GL_TEXTURE_EXTERNAL_OES, sourceTexId); |
| 42 | glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, eglImage); |
| 43 | |
| 44 | sk_sp<GrContext> grContext = sk_ref_sp(mRenderThread.getGrContext()); |
| 45 | if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) { |
| 46 | sk_sp<const GrGLInterface> glInterface(GrGLCreateNativeInterface()); |
| 47 | LOG_ALWAYS_FATAL_IF(!glInterface.get()); |
| 48 | grContext.reset(GrContext::Create(GrBackend::kOpenGL_GrBackend, |
| 49 | (GrBackendContext)glInterface.get())); |
| 50 | } else { |
| 51 | grContext->resetContext(); |
| 52 | } |
| 53 | |
| 54 | GrGLTextureInfo externalTexture; |
| 55 | externalTexture.fTarget = GL_TEXTURE_EXTERNAL_OES; |
| 56 | externalTexture.fID = sourceTexId; |
| 57 | |
Stan Iliev | 08fc19a | 2017-07-24 10:20:33 -0400 | [diff] [blame] | 58 | GrPixelConfig pixelConfig; |
| 59 | switch (bitmap->colorType()) { |
| 60 | case kRGBA_F16_SkColorType: |
| 61 | pixelConfig = kRGBA_half_GrPixelConfig; |
| 62 | break; |
| 63 | case kN32_SkColorType: |
| 64 | default: |
| 65 | pixelConfig = kRGBA_8888_GrPixelConfig; |
| 66 | break; |
| 67 | } |
| 68 | |
Derek Sollenberger | 4170db3 | 2017-08-09 13:52:36 -0400 | [diff] [blame] | 69 | /* Ideally, we would call grContext->caps()->isConfigRenderable(...). We |
| 70 | * currently can't do that since some devices (i.e. SwiftShader) supports all |
| 71 | * the appropriate half float extensions, but only allow the buffer to be read |
| 72 | * back as full floats. We can relax this extension if Skia implements support |
| 73 | * for reading back float buffers (skbug.com/6945). |
| 74 | */ |
| 75 | if (pixelConfig == kRGBA_half_GrPixelConfig && |
| 76 | !DeviceInfo::get()->extensions().hasFloatTextures()) { |
| 77 | ALOGW("Can't copy surface into bitmap, RGBA_F16 config is not supported"); |
| 78 | return CopyResult::DestinationInvalid; |
| 79 | } |
| 80 | |
Stan Iliev | 08fc19a | 2017-07-24 10:20:33 -0400 | [diff] [blame] | 81 | GrBackendTexture backendTexture(imgWidth, imgHeight, pixelConfig, externalTexture); |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 82 | |
| 83 | CopyResult copyResult = CopyResult::UnknownError; |
Greg Daniel | ac2d232 | 2017-07-12 11:30:15 -0400 | [diff] [blame] | 84 | sk_sp<SkImage> image(SkImage::MakeFromAdoptedTexture(grContext.get(), backendTexture, |
| 85 | kTopLeft_GrSurfaceOrigin)); |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 86 | if (image) { |
Stan Iliev | 1220a2a | 2017-07-19 18:09:25 -0400 | [diff] [blame] | 87 | // Convert imgTransform matrix from right to left handed coordinate system. |
| 88 | // If we have a matrix transformation in right handed coordinate system |
| 89 | //|ScaleX, SkewX, TransX| same transform in left handed is |ScaleX, SkewX, TransX | |
| 90 | //|SkewY, ScaleY, TransY| |-SkewY, -ScaleY, 1-TransY| |
| 91 | //|0, 0, 1 | |0, 0, 1 | |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 92 | SkMatrix textureMatrix; |
Stan Iliev | 1220a2a | 2017-07-19 18:09:25 -0400 | [diff] [blame] | 93 | textureMatrix.setIdentity(); |
| 94 | textureMatrix[SkMatrix::kMScaleX] = imgTransform[Matrix4::kScaleX]; |
| 95 | textureMatrix[SkMatrix::kMScaleY] = -imgTransform[Matrix4::kScaleY]; |
| 96 | textureMatrix[SkMatrix::kMSkewX] = imgTransform[Matrix4::kSkewX]; |
| 97 | textureMatrix[SkMatrix::kMSkewY] = -imgTransform[Matrix4::kSkewY]; |
| 98 | textureMatrix[SkMatrix::kMTransX] = imgTransform[Matrix4::kTranslateX]; |
| 99 | textureMatrix[SkMatrix::kMTransY] = 1-imgTransform[Matrix4::kTranslateY]; |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 100 | |
Stan Iliev | 1220a2a | 2017-07-19 18:09:25 -0400 | [diff] [blame] | 101 | // textureMatrix maps 2D texture coordinates of the form (s, t, 1) with s and t in the |
| 102 | // inclusive range [0, 1] to the texture (see GLConsumer::getTransformMatrix comments). |
| 103 | // Convert textureMatrix to translate in real texture dimensions. Texture width and |
| 104 | // height are affected by the orientation (width and height swapped for 90/270 rotation). |
| 105 | if (textureMatrix[SkMatrix::kMSkewX] >= 0.5f || textureMatrix[SkMatrix::kMSkewX] <= -0.5f) { |
| 106 | textureMatrix[SkMatrix::kMTransX] *= imgHeight; |
| 107 | textureMatrix[SkMatrix::kMTransY] *= imgWidth; |
| 108 | } else { |
| 109 | textureMatrix[SkMatrix::kMTransX] *= imgWidth; |
| 110 | textureMatrix[SkMatrix::kMTransY] *= imgHeight; |
| 111 | } |
Stan Iliev | df6520e | 2017-07-17 18:50:16 -0400 | [diff] [blame] | 112 | |
| 113 | // convert to Skia data structures |
| 114 | SkRect skiaSrcRect = srcRect.toSkRect(); |
| 115 | SkMatrix textureMatrixInv; |
| 116 | SkRect skiaDestRect = SkRect::MakeWH(bitmap->width(), bitmap->height()); |
| 117 | bool srcNotEmpty = false; |
| 118 | if (textureMatrix.invert(&textureMatrixInv)) { |
| 119 | if (skiaSrcRect.isEmpty()) { |
| 120 | skiaSrcRect = SkRect::MakeIWH(imgWidth, imgHeight); |
| 121 | srcNotEmpty = !skiaSrcRect.isEmpty(); |
| 122 | } else { |
| 123 | // src and dest rectangles need to be converted into texture coordinates before the |
| 124 | // rotation matrix is applied (because drawImageRect preconcat its matrix). |
| 125 | textureMatrixInv.mapRect(&skiaSrcRect); |
| 126 | srcNotEmpty = skiaSrcRect.intersect(SkRect::MakeIWH(imgWidth, imgHeight)); |
| 127 | } |
| 128 | textureMatrixInv.mapRect(&skiaDestRect); |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 129 | } |
| 130 | |
Stan Iliev | df6520e | 2017-07-17 18:50:16 -0400 | [diff] [blame] | 131 | if (srcNotEmpty) { |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 132 | // we render in an offscreen buffer to scale and to avoid an issue b/62262733 |
| 133 | // with reading incorrect data from EGLImage backed SkImage (likely a driver bug) |
| 134 | sk_sp<SkSurface> scaledSurface = SkSurface::MakeRenderTarget( |
| 135 | grContext.get(), SkBudgeted::kYes, bitmap->info()); |
| 136 | SkPaint paint; |
| 137 | paint.setBlendMode(SkBlendMode::kSrc); |
Stan Iliev | df6520e | 2017-07-17 18:50:16 -0400 | [diff] [blame] | 138 | scaledSurface->getCanvas()->concat(textureMatrix); |
Derek Sollenberger | 6c2a9e2 | 2017-08-15 16:23:01 -0400 | [diff] [blame] | 139 | scaledSurface->getCanvas()->drawImageRect(image, skiaSrcRect, skiaDestRect, &paint, |
| 140 | SkCanvas::kFast_SrcRectConstraint); |
Stan Iliev | df6520e | 2017-07-17 18:50:16 -0400 | [diff] [blame] | 141 | |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 142 | image = scaledSurface->makeImageSnapshot(); |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 143 | |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 144 | if (image->readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), 0, 0)) { |
John Reck | abbedfc | 2017-07-06 15:27:23 -0700 | [diff] [blame] | 145 | bitmap->notifyPixelsChanged(); |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 146 | copyResult = CopyResult::Success; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // make sure that we have deleted the texture (in the SkImage) before we |
| 152 | // destroy the EGLImage that it was created from |
| 153 | image.reset(); |
| 154 | return copyResult; |
| 155 | } |
| 156 | |
| 157 | } /* namespace skiapipeline */ |
| 158 | } /* namespace uirenderer */ |
| 159 | } /* namespace android */ |