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 && |
Stan Iliev | d56d218 | 2017-10-18 13:12:32 -0400 | [diff] [blame] | 76 | !grContext->caps()->isConfigTexturable(kRGBA_half_GrPixelConfig)) { |
Derek Sollenberger | 4170db3 | 2017-08-09 13:52:36 -0400 | [diff] [blame] | 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 | cf91788 | 2017-10-27 19:29:44 -0400 | [diff] [blame^] | 87 | int displayedWidth = imgWidth, displayedHeight = imgHeight; |
| 88 | // If this is a 90 or 270 degree rotation we need to swap width/height to get the device |
| 89 | // size. |
| 90 | if (imgTransform[Matrix4::kSkewX] >= 0.5f || imgTransform[Matrix4::kSkewX] <= -0.5f) { |
| 91 | std::swap(displayedWidth, displayedHeight); |
Stan Iliev | 1220a2a | 2017-07-19 18:09:25 -0400 | [diff] [blame] | 92 | } |
Stan Iliev | df6520e | 2017-07-17 18:50:16 -0400 | [diff] [blame] | 93 | SkRect skiaDestRect = SkRect::MakeWH(bitmap->width(), bitmap->height()); |
Stan Iliev | cf91788 | 2017-10-27 19:29:44 -0400 | [diff] [blame^] | 94 | SkRect skiaSrcRect = srcRect.toSkRect(); |
| 95 | if (skiaSrcRect.isEmpty()) { |
| 96 | skiaSrcRect = SkRect::MakeIWH(displayedWidth, displayedHeight); |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 97 | } |
Stan Iliev | cf91788 | 2017-10-27 19:29:44 -0400 | [diff] [blame^] | 98 | bool srcNotEmpty = skiaSrcRect.intersect(SkRect::MakeIWH(displayedWidth, displayedHeight)); |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 99 | |
Stan Iliev | df6520e | 2017-07-17 18:50:16 -0400 | [diff] [blame] | 100 | if (srcNotEmpty) { |
Stan Iliev | cf91788 | 2017-10-27 19:29:44 -0400 | [diff] [blame^] | 101 | SkMatrix textureMatrixInv; |
| 102 | imgTransform.copyTo(textureMatrixInv); |
| 103 | //TODO: after skia bug https://bugs.chromium.org/p/skia/issues/detail?id=7075 is fixed |
| 104 | // use bottom left origin and remove flipV and invert transformations. |
| 105 | SkMatrix flipV; |
| 106 | flipV.setAll(1, 0, 0, 0, -1, 1, 0, 0, 1); |
| 107 | textureMatrixInv.preConcat(flipV); |
| 108 | textureMatrixInv.preScale(1.0f/displayedWidth, 1.0f/displayedHeight); |
| 109 | textureMatrixInv.postScale(imgWidth, imgHeight); |
| 110 | SkMatrix textureMatrix; |
| 111 | if (!textureMatrixInv.invert(&textureMatrix)) { |
| 112 | textureMatrix = textureMatrixInv; |
| 113 | } |
| 114 | |
| 115 | textureMatrixInv.mapRect(&skiaSrcRect); |
| 116 | textureMatrixInv.mapRect(&skiaDestRect); |
| 117 | |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 118 | // we render in an offscreen buffer to scale and to avoid an issue b/62262733 |
| 119 | // with reading incorrect data from EGLImage backed SkImage (likely a driver bug) |
| 120 | sk_sp<SkSurface> scaledSurface = SkSurface::MakeRenderTarget( |
| 121 | grContext.get(), SkBudgeted::kYes, bitmap->info()); |
| 122 | SkPaint paint; |
| 123 | paint.setBlendMode(SkBlendMode::kSrc); |
Stan Iliev | cf91788 | 2017-10-27 19:29:44 -0400 | [diff] [blame^] | 124 | // Apply a filter, which is matching OpenGL pipeline readback behaviour. Filter usage |
| 125 | // is codified by tests using golden images like DecodeAccuracyTest. |
| 126 | if (skiaSrcRect.width() != bitmap->width() |
| 127 | || skiaSrcRect.height() != bitmap->height()) { |
| 128 | //TODO: apply filter always, but check if tests will be fine |
| 129 | paint.setFilterQuality(kLow_SkFilterQuality); |
| 130 | } |
Stan Iliev | df6520e | 2017-07-17 18:50:16 -0400 | [diff] [blame] | 131 | scaledSurface->getCanvas()->concat(textureMatrix); |
Derek Sollenberger | 6c2a9e2 | 2017-08-15 16:23:01 -0400 | [diff] [blame] | 132 | scaledSurface->getCanvas()->drawImageRect(image, skiaSrcRect, skiaDestRect, &paint, |
| 133 | SkCanvas::kFast_SrcRectConstraint); |
Stan Iliev | df6520e | 2017-07-17 18:50:16 -0400 | [diff] [blame] | 134 | |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 135 | image = scaledSurface->makeImageSnapshot(); |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 136 | |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 137 | if (image->readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), 0, 0)) { |
John Reck | abbedfc | 2017-07-06 15:27:23 -0700 | [diff] [blame] | 138 | bitmap->notifyPixelsChanged(); |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 139 | copyResult = CopyResult::Success; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // make sure that we have deleted the texture (in the SkImage) before we |
| 145 | // destroy the EGLImage that it was created from |
| 146 | image.reset(); |
| 147 | return copyResult; |
| 148 | } |
| 149 | |
| 150 | } /* namespace skiapipeline */ |
| 151 | } /* namespace uirenderer */ |
| 152 | } /* namespace android */ |