blob: 89697d7445c692f3128e39b6bef21fee6720b6e6 [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>
23#include <gl/GrGLInterface.h>
24#include <gl/GrGLTypes.h>
25#include <GLES2/gl2.h>
26#include <GLES2/gl2ext.h>
27
28using namespace android::uirenderer::renderthread;
29
30namespace android {
31namespace uirenderer {
32namespace skiapipeline {
33
34CopyResult SkiaOpenGLReadback::copyImageInto(EGLImageKHR eglImage, const Matrix4& imgTransform,
35 int imgWidth, int imgHeight, const Rect& srcRect, SkBitmap* bitmap) {
36
37 GLuint sourceTexId;
38 glGenTextures(1, &sourceTexId);
39 glBindTexture(GL_TEXTURE_EXTERNAL_OES, sourceTexId);
40 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, eglImage);
41
42 sk_sp<GrContext> grContext = sk_ref_sp(mRenderThread.getGrContext());
43 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) {
44 sk_sp<const GrGLInterface> glInterface(GrGLCreateNativeInterface());
45 LOG_ALWAYS_FATAL_IF(!glInterface.get());
46 grContext.reset(GrContext::Create(GrBackend::kOpenGL_GrBackend,
47 (GrBackendContext)glInterface.get()));
48 } else {
49 grContext->resetContext();
50 }
51
52 GrGLTextureInfo externalTexture;
53 externalTexture.fTarget = GL_TEXTURE_EXTERNAL_OES;
54 externalTexture.fID = sourceTexId;
55
56 GrBackendTextureDesc textureDescription;
57 textureDescription.fWidth = imgWidth;
58 textureDescription.fHeight = imgHeight;
59 textureDescription.fConfig = kRGBA_8888_GrPixelConfig;
60 textureDescription.fOrigin = kTopLeft_GrSurfaceOrigin;
61 textureDescription.fTextureHandle = reinterpret_cast<GrBackendObject>(&externalTexture);
62
63 CopyResult copyResult = CopyResult::UnknownError;
64 sk_sp<SkImage> image(SkImage::MakeFromAdoptedTexture(grContext.get(), textureDescription));
65 if (image) {
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050066 // convert to Skia data structures
67 const SkRect bufferRect = SkRect::MakeIWH(imgWidth, imgHeight);
68 SkRect skiaSrcRect = srcRect.toSkRect();
69 SkMatrix textureMatrix;
70 imgTransform.copyTo(textureMatrix);
71
72 // remove the y-flip applied to the matrix so that we can scale the srcRect.
73 // This flip is not needed as we specify the origin of the texture when we
74 // wrap it as an SkImage.
75 SkMatrix yFlip = SkMatrix::MakeScale(1, -1);
76 yFlip.postTranslate(0,1);
77 textureMatrix.preConcat(yFlip);
78
79 // copy the entire src if the rect is empty
80 if (skiaSrcRect.isEmpty()) {
81 skiaSrcRect = bufferRect;
82 }
83
84 // since the y-flip has been removed we can simply scale & translate
85 // the source rectangle
86 textureMatrix.mapRect(&skiaSrcRect);
87
88 if (skiaSrcRect.intersect(bufferRect)) {
Stan Iliev7bc3bc62017-05-24 13:28:36 -040089 // we render in an offscreen buffer to scale and to avoid an issue b/62262733
90 // with reading incorrect data from EGLImage backed SkImage (likely a driver bug)
91 sk_sp<SkSurface> scaledSurface = SkSurface::MakeRenderTarget(
92 grContext.get(), SkBudgeted::kYes, bitmap->info());
93 SkPaint paint;
94 paint.setBlendMode(SkBlendMode::kSrc);
95 scaledSurface->getCanvas()->drawImageRect(image, skiaSrcRect,
96 SkRect::MakeWH(bitmap->width(), bitmap->height()), &paint);
97 image = scaledSurface->makeImageSnapshot();
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050098
Stan Iliev7bc3bc62017-05-24 13:28:36 -040099 if (image->readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), 0, 0)) {
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500100 copyResult = CopyResult::Success;
101 }
102 }
103 }
104
105 // make sure that we have deleted the texture (in the SkImage) before we
106 // destroy the EGLImage that it was created from
107 image.reset();
108 return copyResult;
109}
110
111} /* namespace skiapipeline */
112} /* namespace uirenderer */
113} /* namespace android */