blob: 208910a06eaaa03a67cc142166dad150f299c534 [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
John Reck1bcacfd2017-11-03 10:12:19 -070019#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 Sollenberger4170db32017-08-09 13:52:36 -040026#include "DeviceInfo.h"
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050027#include "Matrix.h"
28#include "Properties.h"
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050029
30using namespace android::uirenderer::renderthread;
31
32namespace android {
33namespace uirenderer {
34namespace skiapipeline {
35
36CopyResult SkiaOpenGLReadback::copyImageInto(EGLImageKHR eglImage, const Matrix4& imgTransform,
John Reck1bcacfd2017-11-03 10:12:19 -070037 int imgWidth, int imgHeight, const Rect& srcRect,
38 SkBitmap* bitmap) {
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050039 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());
Greg Daniel660d6ec2017-12-08 11:44:27 -050048 grContext = GrContext::MakeGL(std::move(glInterface));
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050049 } else {
50 grContext->resetContext();
51 }
52
Greg Danielc9da8e82018-03-21 10:50:24 -040053 if (bitmap->colorType() == kRGBA_F16_SkColorType &&
54 !grContext->colorTypeSupportedAsSurface(bitmap->colorType())) {
Derek Sollenberger4170db32017-08-09 13:52:36 -040055 ALOGW("Can't copy surface into bitmap, RGBA_F16 config is not supported");
56 return CopyResult::DestinationInvalid;
57 }
58
Greg Danielc9da8e82018-03-21 10:50:24 -040059 GrGLTextureInfo externalTexture;
60 externalTexture.fTarget = GL_TEXTURE_EXTERNAL_OES;
61 externalTexture.fID = sourceTexId;
62 switch (bitmap->colorType()) {
63 case kRGBA_F16_SkColorType:
64 externalTexture.fFormat = GL_RGBA16F;
65 break;
66 case kN32_SkColorType:
67 default:
68 externalTexture.fFormat = GL_RGBA8;
69 break;
70 }
71
72 GrBackendTexture backendTexture(imgWidth, imgHeight, GrMipMapped::kNo, externalTexture);
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050073
74 CopyResult copyResult = CopyResult::UnknownError;
Greg Danielac2d2322017-07-12 11:30:15 -040075 sk_sp<SkImage> image(SkImage::MakeFromAdoptedTexture(grContext.get(), backendTexture,
Greg Danielc9da8e82018-03-21 10:50:24 -040076 kTopLeft_GrSurfaceOrigin,
77 bitmap->colorType()));
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050078 if (image) {
Stan Ilievcf917882017-10-27 19:29:44 -040079 int displayedWidth = imgWidth, displayedHeight = imgHeight;
80 // If this is a 90 or 270 degree rotation we need to swap width/height to get the device
81 // size.
82 if (imgTransform[Matrix4::kSkewX] >= 0.5f || imgTransform[Matrix4::kSkewX] <= -0.5f) {
83 std::swap(displayedWidth, displayedHeight);
Stan Iliev1220a2a2017-07-19 18:09:25 -040084 }
Stan Ilievdf6520e2017-07-17 18:50:16 -040085 SkRect skiaDestRect = SkRect::MakeWH(bitmap->width(), bitmap->height());
Stan Ilievcf917882017-10-27 19:29:44 -040086 SkRect skiaSrcRect = srcRect.toSkRect();
87 if (skiaSrcRect.isEmpty()) {
88 skiaSrcRect = SkRect::MakeIWH(displayedWidth, displayedHeight);
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050089 }
Stan Ilievcf917882017-10-27 19:29:44 -040090 bool srcNotEmpty = skiaSrcRect.intersect(SkRect::MakeIWH(displayedWidth, displayedHeight));
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050091
Stan Ilievdf6520e2017-07-17 18:50:16 -040092 if (srcNotEmpty) {
Stan Ilievcf917882017-10-27 19:29:44 -040093 SkMatrix textureMatrixInv;
94 imgTransform.copyTo(textureMatrixInv);
John Reck1bcacfd2017-11-03 10:12:19 -070095 // TODO: after skia bug https://bugs.chromium.org/p/skia/issues/detail?id=7075 is fixed
Stan Ilievcf917882017-10-27 19:29:44 -040096 // use bottom left origin and remove flipV and invert transformations.
97 SkMatrix flipV;
98 flipV.setAll(1, 0, 0, 0, -1, 1, 0, 0, 1);
99 textureMatrixInv.preConcat(flipV);
John Reck1bcacfd2017-11-03 10:12:19 -0700100 textureMatrixInv.preScale(1.0f / displayedWidth, 1.0f / displayedHeight);
Stan Ilievcf917882017-10-27 19:29:44 -0400101 textureMatrixInv.postScale(imgWidth, imgHeight);
102 SkMatrix textureMatrix;
103 if (!textureMatrixInv.invert(&textureMatrix)) {
104 textureMatrix = textureMatrixInv;
105 }
106
107 textureMatrixInv.mapRect(&skiaSrcRect);
108 textureMatrixInv.mapRect(&skiaDestRect);
109
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400110 // we render in an offscreen buffer to scale and to avoid an issue b/62262733
111 // with reading incorrect data from EGLImage backed SkImage (likely a driver bug)
John Reck1bcacfd2017-11-03 10:12:19 -0700112 sk_sp<SkSurface> scaledSurface =
113 SkSurface::MakeRenderTarget(grContext.get(), SkBudgeted::kYes, bitmap->info());
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400114 SkPaint paint;
115 paint.setBlendMode(SkBlendMode::kSrc);
Stan Ilievcf917882017-10-27 19:29:44 -0400116 // Apply a filter, which is matching OpenGL pipeline readback behaviour. Filter usage
117 // is codified by tests using golden images like DecodeAccuracyTest.
John Reck1bcacfd2017-11-03 10:12:19 -0700118 if (skiaSrcRect.width() != bitmap->width() ||
119 skiaSrcRect.height() != bitmap->height()) {
120 // TODO: apply filter always, but check if tests will be fine
Stan Ilievcf917882017-10-27 19:29:44 -0400121 paint.setFilterQuality(kLow_SkFilterQuality);
122 }
Stan Ilievdf6520e2017-07-17 18:50:16 -0400123 scaledSurface->getCanvas()->concat(textureMatrix);
Derek Sollenberger6c2a9e22017-08-15 16:23:01 -0400124 scaledSurface->getCanvas()->drawImageRect(image, skiaSrcRect, skiaDestRect, &paint,
John Reck1bcacfd2017-11-03 10:12:19 -0700125 SkCanvas::kFast_SrcRectConstraint);
Stan Ilievdf6520e2017-07-17 18:50:16 -0400126
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400127 image = scaledSurface->makeImageSnapshot();
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500128
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400129 if (image->readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), 0, 0)) {
John Reckabbedfc2017-07-06 15:27:23 -0700130 bitmap->notifyPixelsChanged();
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500131 copyResult = CopyResult::Success;
132 }
133 }
134 }
135
136 // make sure that we have deleted the texture (in the SkImage) before we
137 // destroy the EGLImage that it was created from
138 image.reset();
John Reck44627c22018-04-12 13:55:38 -0700139 glFinish();
140
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500141 return copyResult;
142}
143
144} /* namespace skiapipeline */
145} /* namespace uirenderer */
146} /* namespace android */