blob: 311419dd2b657204b95505c1e9d970f8ac714b62 [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
19#include "Matrix.h"
20#include "Properties.h"
21#include <SkCanvas.h>
22#include <SkSurface.h>
Greg Danielac2d2322017-07-12 11:30:15 -040023#include <GrBackendSurface.h>
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050024#include <gl/GrGLInterface.h>
25#include <gl/GrGLTypes.h>
26#include <GLES2/gl2.h>
27#include <GLES2/gl2ext.h>
28
29using namespace android::uirenderer::renderthread;
30
31namespace android {
32namespace uirenderer {
33namespace skiapipeline {
34
35CopyResult SkiaOpenGLReadback::copyImageInto(EGLImageKHR eglImage, const Matrix4& imgTransform,
36 int imgWidth, int imgHeight, const Rect& srcRect, SkBitmap* bitmap) {
37
38 GLuint sourceTexId;
39 glGenTextures(1, &sourceTexId);
40 glBindTexture(GL_TEXTURE_EXTERNAL_OES, sourceTexId);
41 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, eglImage);
42
43 sk_sp<GrContext> grContext = sk_ref_sp(mRenderThread.getGrContext());
44 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) {
45 sk_sp<const GrGLInterface> glInterface(GrGLCreateNativeInterface());
46 LOG_ALWAYS_FATAL_IF(!glInterface.get());
47 grContext.reset(GrContext::Create(GrBackend::kOpenGL_GrBackend,
48 (GrBackendContext)glInterface.get()));
49 } else {
50 grContext->resetContext();
51 }
52
53 GrGLTextureInfo externalTexture;
54 externalTexture.fTarget = GL_TEXTURE_EXTERNAL_OES;
55 externalTexture.fID = sourceTexId;
56
Greg Danielac2d2322017-07-12 11:30:15 -040057 GrBackendTexture backendTexture(imgWidth, imgHeight, kRGBA_8888_GrPixelConfig, externalTexture);
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050058
59 CopyResult copyResult = CopyResult::UnknownError;
Greg Danielac2d2322017-07-12 11:30:15 -040060 sk_sp<SkImage> image(SkImage::MakeFromAdoptedTexture(grContext.get(), backendTexture,
61 kTopLeft_GrSurfaceOrigin));
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050062 if (image) {
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050063 SkMatrix textureMatrix;
64 imgTransform.copyTo(textureMatrix);
65
Stan Ilievdf6520e2017-07-17 18:50:16 -040066 // remove the y-flip applied to the matrix
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050067 SkMatrix yFlip = SkMatrix::MakeScale(1, -1);
68 yFlip.postTranslate(0,1);
69 textureMatrix.preConcat(yFlip);
70
Stan Ilievdf6520e2017-07-17 18:50:16 -040071 // multiply by image size, because textureMatrix maps to [0..1] range
72 textureMatrix[SkMatrix::kMTransX] *= imgWidth;
73 textureMatrix[SkMatrix::kMTransY] *= imgHeight;
74
75 // swap rotation and translation part of the matrix, because we convert from
76 // right-handed Cartesian to left-handed coordinate system.
77 std::swap(textureMatrix[SkMatrix::kMTransX], textureMatrix[SkMatrix::kMTransY]);
78 std::swap(textureMatrix[SkMatrix::kMSkewX], textureMatrix[SkMatrix::kMSkewY]);
79
80 // convert to Skia data structures
81 SkRect skiaSrcRect = srcRect.toSkRect();
82 SkMatrix textureMatrixInv;
83 SkRect skiaDestRect = SkRect::MakeWH(bitmap->width(), bitmap->height());
84 bool srcNotEmpty = false;
85 if (textureMatrix.invert(&textureMatrixInv)) {
86 if (skiaSrcRect.isEmpty()) {
87 skiaSrcRect = SkRect::MakeIWH(imgWidth, imgHeight);
88 srcNotEmpty = !skiaSrcRect.isEmpty();
89 } else {
90 // src and dest rectangles need to be converted into texture coordinates before the
91 // rotation matrix is applied (because drawImageRect preconcat its matrix).
92 textureMatrixInv.mapRect(&skiaSrcRect);
93 srcNotEmpty = skiaSrcRect.intersect(SkRect::MakeIWH(imgWidth, imgHeight));
94 }
95 textureMatrixInv.mapRect(&skiaDestRect);
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050096 }
97
Stan Ilievdf6520e2017-07-17 18:50:16 -040098 if (srcNotEmpty) {
Stan Iliev7bc3bc62017-05-24 13:28:36 -040099 // we render in an offscreen buffer to scale and to avoid an issue b/62262733
100 // with reading incorrect data from EGLImage backed SkImage (likely a driver bug)
101 sk_sp<SkSurface> scaledSurface = SkSurface::MakeRenderTarget(
102 grContext.get(), SkBudgeted::kYes, bitmap->info());
103 SkPaint paint;
104 paint.setBlendMode(SkBlendMode::kSrc);
Stan Ilievdf6520e2017-07-17 18:50:16 -0400105 scaledSurface->getCanvas()->concat(textureMatrix);
106 scaledSurface->getCanvas()->drawImageRect(image, skiaSrcRect, skiaDestRect, &paint);
107
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400108 image = scaledSurface->makeImageSnapshot();
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500109
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400110 if (image->readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), 0, 0)) {
John Reckabbedfc2017-07-06 15:27:23 -0700111 bitmap->notifyPixelsChanged();
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500112 copyResult = CopyResult::Success;
113 }
114 }
115 }
116
117 // make sure that we have deleted the texture (in the SkImage) before we
118 // destroy the EGLImage that it was created from
119 image.reset();
120 return copyResult;
121}
122
123} /* namespace skiapipeline */
124} /* namespace uirenderer */
125} /* namespace android */