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 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 19 | #include <GLES2/gl2.h> |
| 20 | #include <GLES2/gl2ext.h> |
| 21 | #include <GrBackendSurface.h> |
| 22 | #include <SkCanvas.h> |
| 23 | #include <SkSurface.h> |
| 24 | #include <gl/GrGLInterface.h> |
| 25 | #include <gl/GrGLTypes.h> |
Derek Sollenberger | 4170db3 | 2017-08-09 13:52:36 -0400 | [diff] [blame] | 26 | #include "DeviceInfo.h" |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 27 | #include "Matrix.h" |
| 28 | #include "Properties.h" |
Stan Iliev | d50edd0 | 2018-06-22 17:33:43 -0400 | [diff] [blame] | 29 | #include "utils/MathUtils.h" |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 30 | |
| 31 | using namespace android::uirenderer::renderthread; |
| 32 | |
| 33 | namespace android { |
| 34 | namespace uirenderer { |
| 35 | namespace skiapipeline { |
| 36 | |
| 37 | CopyResult SkiaOpenGLReadback::copyImageInto(EGLImageKHR eglImage, const Matrix4& imgTransform, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 38 | int imgWidth, int imgHeight, const Rect& srcRect, |
| 39 | SkBitmap* bitmap) { |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 40 | GLuint sourceTexId; |
| 41 | glGenTextures(1, &sourceTexId); |
| 42 | glBindTexture(GL_TEXTURE_EXTERNAL_OES, sourceTexId); |
| 43 | glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, eglImage); |
| 44 | |
| 45 | sk_sp<GrContext> grContext = sk_ref_sp(mRenderThread.getGrContext()); |
| 46 | if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) { |
| 47 | sk_sp<const GrGLInterface> glInterface(GrGLCreateNativeInterface()); |
| 48 | LOG_ALWAYS_FATAL_IF(!glInterface.get()); |
Greg Daniel | 660d6ec | 2017-12-08 11:44:27 -0500 | [diff] [blame] | 49 | grContext = GrContext::MakeGL(std::move(glInterface)); |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 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()) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 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; |
Stan Iliev | 08fc19a | 2017-07-24 10:20:33 -0400 | [diff] [blame] | 67 | } |
| 68 | |
Derek Sollenberger | 4170db3 | 2017-08-09 13:52:36 -0400 | [diff] [blame] | 69 | if (pixelConfig == kRGBA_half_GrPixelConfig && |
Stan Iliev | f1f3c38 | 2017-11-09 12:17:35 -0500 | [diff] [blame] | 70 | !grContext->caps()->isConfigRenderable(kRGBA_half_GrPixelConfig, false)) { |
Derek Sollenberger | 4170db3 | 2017-08-09 13:52:36 -0400 | [diff] [blame] | 71 | ALOGW("Can't copy surface into bitmap, RGBA_F16 config is not supported"); |
| 72 | return CopyResult::DestinationInvalid; |
| 73 | } |
| 74 | |
Stan Iliev | 08fc19a | 2017-07-24 10:20:33 -0400 | [diff] [blame] | 75 | GrBackendTexture backendTexture(imgWidth, imgHeight, pixelConfig, externalTexture); |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 76 | |
| 77 | CopyResult copyResult = CopyResult::UnknownError; |
Greg Daniel | ac2d232 | 2017-07-12 11:30:15 -0400 | [diff] [blame] | 78 | sk_sp<SkImage> image(SkImage::MakeFromAdoptedTexture(grContext.get(), backendTexture, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 79 | kTopLeft_GrSurfaceOrigin)); |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 80 | if (image) { |
Stan Iliev | cf91788 | 2017-10-27 19:29:44 -0400 | [diff] [blame] | 81 | int displayedWidth = imgWidth, displayedHeight = imgHeight; |
| 82 | // If this is a 90 or 270 degree rotation we need to swap width/height to get the device |
| 83 | // size. |
| 84 | if (imgTransform[Matrix4::kSkewX] >= 0.5f || imgTransform[Matrix4::kSkewX] <= -0.5f) { |
| 85 | std::swap(displayedWidth, displayedHeight); |
Stan Iliev | 1220a2a | 2017-07-19 18:09:25 -0400 | [diff] [blame] | 86 | } |
Stan Iliev | df6520e | 2017-07-17 18:50:16 -0400 | [diff] [blame] | 87 | SkRect skiaDestRect = SkRect::MakeWH(bitmap->width(), bitmap->height()); |
Stan Iliev | cf91788 | 2017-10-27 19:29:44 -0400 | [diff] [blame] | 88 | SkRect skiaSrcRect = srcRect.toSkRect(); |
| 89 | if (skiaSrcRect.isEmpty()) { |
| 90 | skiaSrcRect = SkRect::MakeIWH(displayedWidth, displayedHeight); |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 91 | } |
Stan Iliev | cf91788 | 2017-10-27 19:29:44 -0400 | [diff] [blame] | 92 | bool srcNotEmpty = skiaSrcRect.intersect(SkRect::MakeIWH(displayedWidth, displayedHeight)); |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 93 | |
Stan Iliev | df6520e | 2017-07-17 18:50:16 -0400 | [diff] [blame] | 94 | if (srcNotEmpty) { |
Stan Iliev | cf91788 | 2017-10-27 19:29:44 -0400 | [diff] [blame] | 95 | SkMatrix textureMatrixInv; |
| 96 | imgTransform.copyTo(textureMatrixInv); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 97 | // TODO: after skia bug https://bugs.chromium.org/p/skia/issues/detail?id=7075 is fixed |
Stan Iliev | cf91788 | 2017-10-27 19:29:44 -0400 | [diff] [blame] | 98 | // use bottom left origin and remove flipV and invert transformations. |
| 99 | SkMatrix flipV; |
| 100 | flipV.setAll(1, 0, 0, 0, -1, 1, 0, 0, 1); |
| 101 | textureMatrixInv.preConcat(flipV); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 102 | textureMatrixInv.preScale(1.0f / displayedWidth, 1.0f / displayedHeight); |
Stan Iliev | cf91788 | 2017-10-27 19:29:44 -0400 | [diff] [blame] | 103 | textureMatrixInv.postScale(imgWidth, imgHeight); |
| 104 | SkMatrix textureMatrix; |
| 105 | if (!textureMatrixInv.invert(&textureMatrix)) { |
| 106 | textureMatrix = textureMatrixInv; |
| 107 | } |
| 108 | |
| 109 | textureMatrixInv.mapRect(&skiaSrcRect); |
| 110 | textureMatrixInv.mapRect(&skiaDestRect); |
| 111 | |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 112 | // we render in an offscreen buffer to scale and to avoid an issue b/62262733 |
| 113 | // with reading incorrect data from EGLImage backed SkImage (likely a driver bug) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 114 | sk_sp<SkSurface> scaledSurface = |
| 115 | SkSurface::MakeRenderTarget(grContext.get(), SkBudgeted::kYes, bitmap->info()); |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 116 | SkPaint paint; |
| 117 | paint.setBlendMode(SkBlendMode::kSrc); |
Stan Iliev | cf91788 | 2017-10-27 19:29:44 -0400 | [diff] [blame] | 118 | // Apply a filter, which is matching OpenGL pipeline readback behaviour. Filter usage |
| 119 | // is codified by tests using golden images like DecodeAccuracyTest. |
Stan Iliev | d50edd0 | 2018-06-22 17:33:43 -0400 | [diff] [blame] | 120 | bool disableFilter = MathUtils::areEqual(skiaSrcRect.width(), skiaDestRect.width()) |
| 121 | && MathUtils::areEqual(skiaSrcRect.height(), skiaDestRect.height()); |
| 122 | if (!disableFilter) { |
Stan Iliev | cf91788 | 2017-10-27 19:29:44 -0400 | [diff] [blame] | 123 | paint.setFilterQuality(kLow_SkFilterQuality); |
| 124 | } |
Stan Iliev | df6520e | 2017-07-17 18:50:16 -0400 | [diff] [blame] | 125 | scaledSurface->getCanvas()->concat(textureMatrix); |
Derek Sollenberger | 6c2a9e2 | 2017-08-15 16:23:01 -0400 | [diff] [blame] | 126 | scaledSurface->getCanvas()->drawImageRect(image, skiaSrcRect, skiaDestRect, &paint, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 127 | SkCanvas::kFast_SrcRectConstraint); |
Stan Iliev | df6520e | 2017-07-17 18:50:16 -0400 | [diff] [blame] | 128 | |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 129 | image = scaledSurface->makeImageSnapshot(); |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 130 | |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 131 | if (image->readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), 0, 0)) { |
John Reck | abbedfc | 2017-07-06 15:27:23 -0700 | [diff] [blame] | 132 | bitmap->notifyPixelsChanged(); |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 133 | copyResult = CopyResult::Success; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // make sure that we have deleted the texture (in the SkImage) before we |
| 139 | // destroy the EGLImage that it was created from |
| 140 | image.reset(); |
| 141 | return copyResult; |
| 142 | } |
| 143 | |
| 144 | } /* namespace skiapipeline */ |
| 145 | } /* namespace uirenderer */ |
| 146 | } /* namespace android */ |