blob: b2863921065ae81a24bf5d8f9fbf47dd2eb48e05 [file] [log] [blame]
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -05001/*
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 Sollenberger4170db32017-08-09 13:52:36 -040019#include "DeviceInfo.h"
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050020#include "Matrix.h"
21#include "Properties.h"
22#include <SkCanvas.h>
23#include <SkSurface.h>
Greg Danielac2d2322017-07-12 11:30:15 -040024#include <GrBackendSurface.h>
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050025#include <gl/GrGLInterface.h>
26#include <gl/GrGLTypes.h>
27#include <GLES2/gl2.h>
28#include <GLES2/gl2ext.h>
29
30using namespace android::uirenderer::renderthread;
31
32namespace android {
33namespace uirenderer {
34namespace skiapipeline {
35
36CopyResult 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 Iliev08fc19a2017-07-24 10:20:33 -040058 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 Sollenberger4170db32017-08-09 13:52:36 -040069 /* 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 Ilievd56d2182017-10-18 13:12:32 -040076 !grContext->caps()->isConfigTexturable(kRGBA_half_GrPixelConfig)) {
Derek Sollenberger4170db32017-08-09 13:52:36 -040077 ALOGW("Can't copy surface into bitmap, RGBA_F16 config is not supported");
78 return CopyResult::DestinationInvalid;
79 }
80
Stan Iliev08fc19a2017-07-24 10:20:33 -040081 GrBackendTexture backendTexture(imgWidth, imgHeight, pixelConfig, externalTexture);
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050082
83 CopyResult copyResult = CopyResult::UnknownError;
Greg Danielac2d2322017-07-12 11:30:15 -040084 sk_sp<SkImage> image(SkImage::MakeFromAdoptedTexture(grContext.get(), backendTexture,
85 kTopLeft_GrSurfaceOrigin));
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050086 if (image) {
Stan Ilievcf917882017-10-27 19:29:44 -040087 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 Iliev1220a2a2017-07-19 18:09:25 -040092 }
Stan Ilievdf6520e2017-07-17 18:50:16 -040093 SkRect skiaDestRect = SkRect::MakeWH(bitmap->width(), bitmap->height());
Stan Ilievcf917882017-10-27 19:29:44 -040094 SkRect skiaSrcRect = srcRect.toSkRect();
95 if (skiaSrcRect.isEmpty()) {
96 skiaSrcRect = SkRect::MakeIWH(displayedWidth, displayedHeight);
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050097 }
Stan Ilievcf917882017-10-27 19:29:44 -040098 bool srcNotEmpty = skiaSrcRect.intersect(SkRect::MakeIWH(displayedWidth, displayedHeight));
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050099
Stan Ilievdf6520e2017-07-17 18:50:16 -0400100 if (srcNotEmpty) {
Stan Ilievcf917882017-10-27 19:29:44 -0400101 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 Iliev7bc3bc62017-05-24 13:28:36 -0400118 // 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 Ilievcf917882017-10-27 19:29:44 -0400124 // 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 Ilievdf6520e2017-07-17 18:50:16 -0400131 scaledSurface->getCanvas()->concat(textureMatrix);
Derek Sollenberger6c2a9e22017-08-15 16:23:01 -0400132 scaledSurface->getCanvas()->drawImageRect(image, skiaSrcRect, skiaDestRect, &paint,
133 SkCanvas::kFast_SrcRectConstraint);
Stan Ilievdf6520e2017-07-17 18:50:16 -0400134
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400135 image = scaledSurface->makeImageSnapshot();
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500136
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400137 if (image->readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), 0, 0)) {
John Reckabbedfc2017-07-06 15:27:23 -0700138 bitmap->notifyPixelsChanged();
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500139 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 */