blob: a9b0184d8ed67c0df25cd2d913ca354032e8f204 [file] [log] [blame]
bsalomon7ea33f52015-11-22 14:51:00 -08001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/gpu/GrContext.h"
9#include "include/gpu/GrTexture.h"
10#include "src/gpu/GrContextPriv.h"
11#include "src/gpu/GrRenderTargetContext.h"
12#include "src/gpu/GrShaderCaps.h"
13#include "src/gpu/GrSurfacePriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrTexturePriv.h"
15#include "src/gpu/GrTextureProxyPriv.h"
16#include "src/gpu/gl/GrGLGpu.h"
17#include "src/gpu/gl/GrGLUtil.h"
18#include "tests/Test.h"
19#include "tests/TestUtils.h"
20#include "tools/gpu/GrContextFactory.h"
21#include "tools/gpu/gl/GLTestContext.h"
bsalomon7ea33f52015-11-22 14:51:00 -080022
John Rosascoa9b348f2019-11-08 13:18:15 -080023#ifdef SK_GL
24
bsalomon273c0f52016-03-31 10:59:06 -070025using sk_gpu_test::GLTestContext;
bsalomon3724e572016-03-30 18:56:19 -070026
Greg Daniel02611d92017-07-25 10:05:01 -040027static void cleanup(GLTestContext* glctx0, GrGLuint texID0, GLTestContext* glctx1,
Robert Phillipsd21b2a52017-12-12 13:01:25 -050028 sk_sp<GrContext> grctx1, GrBackendTexture* backendTex1,
Greg Daniel02611d92017-07-25 10:05:01 -040029 GrEGLImage image1) {
bsalomon7ea33f52015-11-22 14:51:00 -080030 if (glctx1) {
31 glctx1->makeCurrent();
32 if (grctx1) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -050033 if (backendTex1 && backendTex1->isValid()) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -040034 grctx1->deleteBackendTexture(*backendTex1);
bsalomon7ea33f52015-11-22 14:51:00 -080035 }
bsalomon7ea33f52015-11-22 14:51:00 -080036 }
37 if (GR_EGL_NO_IMAGE != image1) {
38 glctx1->destroyEGLImage(image1);
39 }
bsalomon7ea33f52015-11-22 14:51:00 -080040 }
41
42 glctx0->makeCurrent();
43 if (texID0) {
44 GR_GL_CALL(glctx0->gl(), DeleteTextures(1, &texID0));
45 }
46}
47
bsalomon758586c2016-04-06 14:02:39 -070048DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(EGLImageTest, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070049 GrContext* context0 = ctxInfo.grContext();
50 sk_gpu_test::GLTestContext* glCtx0 = ctxInfo.glContext();
bsalomonf2f1c172016-04-05 12:59:06 -070051
kkinnunen59319222015-11-22 23:23:53 -080052 // Try to create a second GL context and then check if the contexts have necessary
53 // extensions to run this test.
bsalomon7ea33f52015-11-22 14:51:00 -080054
kkinnunen59319222015-11-22 23:23:53 -080055 if (kGLES_GrGLStandard != glCtx0->gl()->fStandard) {
56 return;
bsalomon7ea33f52015-11-22 14:51:00 -080057 }
Robert Phillips9da87e02019-02-04 13:26:26 -050058 GrGLGpu* gpu0 = static_cast<GrGLGpu*>(context0->priv().getGpu());
Brian Salomon1edc5b92016-11-29 13:43:46 -050059 if (!gpu0->glCaps().shaderCaps()->externalTextureSupport()) {
kkinnunen59319222015-11-22 23:23:53 -080060 return;
61 }
62
Ben Wagner145dbcd2016-11-03 14:40:50 -040063 std::unique_ptr<GLTestContext> glCtx1 = glCtx0->makeNew();
kkinnunen59319222015-11-22 23:23:53 -080064 if (!glCtx1) {
65 return;
66 }
Brian Salomon384fab42017-12-07 12:33:05 -050067 sk_sp<GrContext> context1 = GrContext::MakeGL(sk_ref_sp(glCtx1->gl()));
Robert Phillipsd21b2a52017-12-12 13:01:25 -050068 GrBackendTexture backendTexture1;
kkinnunen59319222015-11-22 23:23:53 -080069 GrEGLImage image = GR_EGL_NO_IMAGE;
70 GrGLTextureInfo externalTexture;
71 externalTexture.fID = 0;
72
73 if (!context1) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -050074 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
kkinnunen59319222015-11-22 23:23:53 -080075 return;
76 }
77
78 if (!glCtx1->gl()->hasExtension("EGL_KHR_image") ||
79 !glCtx1->gl()->hasExtension("EGL_KHR_gl_texture_2D_image")) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -050080 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
kkinnunen59319222015-11-22 23:23:53 -080081 return;
82 }
83
84 ///////////////////////////////// CONTEXT 1 ///////////////////////////////////
85
86 // Use GL Context 1 to create a texture unknown to GrContext.
87 context1->flush();
kkinnunen59319222015-11-22 23:23:53 -080088 static const int kSize = 100;
Robert Phillipsd21b2a52017-12-12 13:01:25 -050089 backendTexture1 =
Robert Phillips4bdd36f2019-06-04 11:03:06 -040090 context1->createBackendTexture(kSize, kSize, kRGBA_8888_SkColorType,
91 SkColors::kTransparent,
Robert Phillipsda2e67a2019-07-01 15:04:06 -040092 GrMipMapped::kNo, GrRenderable::kNo, GrProtected::kNo);
Greg Daniel5366e592018-01-10 09:57:53 -050093
Robert Phillips7c8af172019-07-08 15:55:24 -040094 if (!backendTexture1.isValid()) {
kkinnunen59319222015-11-22 23:23:53 -080095 ERRORF(reporter, "Error creating texture for EGL Image");
Robert Phillipsd21b2a52017-12-12 13:01:25 -050096 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
kkinnunen59319222015-11-22 23:23:53 -080097 return;
98 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -050099
Greg Daniel52e16d92018-04-10 09:34:07 -0400100 GrGLTextureInfo texInfo;
101 if (!backendTexture1.getGLTextureInfo(&texInfo)) {
102 ERRORF(reporter, "Failed to get GrGLTextureInfo");
103 return;
104 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500105
Greg Daniel52e16d92018-04-10 09:34:07 -0400106 if (GR_GL_TEXTURE_2D != texInfo.fTarget) {
kkinnunen59319222015-11-22 23:23:53 -0800107 ERRORF(reporter, "Expected backend texture to be 2D");
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500108 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
kkinnunen59319222015-11-22 23:23:53 -0800109 return;
110 }
111
112 // Wrap the texture in an EGLImage
Greg Daniel52e16d92018-04-10 09:34:07 -0400113 image = glCtx1->texture2DToEGLImage(texInfo.fID);
kkinnunen59319222015-11-22 23:23:53 -0800114 if (GR_EGL_NO_IMAGE == image) {
115 ERRORF(reporter, "Error creating EGL Image from texture");
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500116 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
kkinnunen59319222015-11-22 23:23:53 -0800117 return;
118 }
119
Greg Daniel261b8aa2017-10-23 09:37:36 -0400120 // Since we are dealing with two different GL contexts here, we need to call finish so that the
121 // clearing of the texture that happens in createTextingOnlyBackendTexture occurs before we call
122 // TexSubImage below on the other context. Otherwise, it is possible the calls get reordered and
123 // the clearing overwrites the TexSubImage writes.
124 GR_GL_CALL(glCtx1->gl(), Finish());
125
kkinnunen59319222015-11-22 23:23:53 -0800126 // Populate the texture using GL context 1. Important to use TexSubImage as TexImage orphans
127 // the EGL image. Also, this must be done after creating the EGLImage as the texture
128 // contents may not be preserved when the image is created.
129 SkAutoTMalloc<uint32_t> pixels(kSize * kSize);
130 for (int i = 0; i < kSize*kSize; ++i) {
131 pixels.get()[i] = 0xDDAABBCC;
132 }
133 GR_GL_CALL(glCtx1->gl(), ActiveTexture(GR_GL_TEXTURE0));
Greg Daniel52e16d92018-04-10 09:34:07 -0400134 GR_GL_CALL(glCtx1->gl(), BindTexture(texInfo.fTarget, texInfo.fID));
135 GR_GL_CALL(glCtx1->gl(), TexSubImage2D(texInfo.fTarget, 0, 0, 0, kSize, kSize,
kkinnunen59319222015-11-22 23:23:53 -0800136 GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, pixels.get()));
137 GR_GL_CALL(glCtx1->gl(), Finish());
138 // We've been making direct GL calls in GL context 1, let GrContext 1 know its internal
139 // state is invalid.
140 context1->resetContext();
141
142 ///////////////////////////////// CONTEXT 0 ///////////////////////////////////
143
144 // Make a new texture ID in GL Context 0 from the EGL Image
145 glCtx0->makeCurrent();
146 externalTexture.fTarget = GR_GL_TEXTURE_EXTERNAL;
147 externalTexture.fID = glCtx0->eglImageToExternalTexture(image);
Greg Daniel2b5be0a2019-06-13 16:50:49 -0400148 externalTexture.fFormat = GR_GL_RGBA8;
Robert Phillipsfee2b4e2017-06-08 18:22:53 -0400149 if (0 == externalTexture.fID) {
150 ERRORF(reporter, "Error converting EGL Image back to texture");
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500151 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
Robert Phillipsfee2b4e2017-06-08 18:22:53 -0400152 return;
153 }
kkinnunen59319222015-11-22 23:23:53 -0800154
155 // Wrap this texture ID in a GrTexture
Greg Daniel108bb232018-07-03 16:18:29 -0400156 GrBackendTexture backendTex(kSize, kSize, GrMipMapped::kNo, externalTexture);
Robert Phillipsd46697a2017-01-25 12:10:37 -0500157
Greg Danielbfa19c42019-12-19 16:41:40 -0500158 GrColorType colorType = GrColorType::kRGBA_8888;
159 SkAlphaType alphaType = kPremul_SkAlphaType;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000160 // TODO: If I make this TopLeft origin to match resolve_origin calls for kDefault, this test
161 // fails on the Nexus5. Why?
Greg Danielbfa19c42019-12-19 16:41:40 -0500162 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin;
163 sk_sp<GrSurfaceProxy> texProxy = context0->priv().proxyProvider()->wrapBackendTexture(
164 backendTex, colorType, origin, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
165 kRW_GrIOType);
166 if (!texProxy) {
167 ERRORF(reporter, "Error wrapping external texture in GrTextureProxy.");
168 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
169 return;
170 }
171 auto surfaceContext = GrSurfaceContext::Make(context0, std::move(texProxy), colorType,
172 alphaType, nullptr);
Robert Phillipsd46697a2017-01-25 12:10:37 -0500173
Robert Phillips26caf892017-01-27 10:58:31 -0500174 if (!surfaceContext) {
175 ERRORF(reporter, "Error wrapping external texture in GrSurfaceContext.");
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500176 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
Robert Phillips26caf892017-01-27 10:58:31 -0500177 return;
kkinnunen59319222015-11-22 23:23:53 -0800178 }
179
Robert Phillipsabf7b762018-03-21 12:13:37 -0400180 GrTextureProxy* proxy = surfaceContext->asTextureProxy();
Greg Daniel09c94002018-06-08 22:11:51 +0000181 REPORTER_ASSERT(reporter, proxy->mipMapped() == GrMipMapped::kNo);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400182 REPORTER_ASSERT(reporter, proxy->peekTexture()->texturePriv().mipMapped() == GrMipMapped::kNo);
Robert Phillipsabf7b762018-03-21 12:13:37 -0400183
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400184 REPORTER_ASSERT(reporter, proxy->textureType() == GrTextureType::kExternal);
185 REPORTER_ASSERT(reporter,
186 proxy->peekTexture()->texturePriv().textureType() == GrTextureType::kExternal);
187 REPORTER_ASSERT(reporter, proxy->hasRestrictedSampling());
188 REPORTER_ASSERT(reporter, proxy->peekTexture()->texturePriv().hasRestrictedSampling());
Robert Phillipsabf7b762018-03-21 12:13:37 -0400189
bsalomona98419b2015-11-23 07:09:50 -0800190 // Should not be able to wrap as a RT
Robert Phillipsd46697a2017-01-25 12:10:37 -0500191 {
Greg Danielba0ff782020-01-07 15:42:57 -0500192 auto temp = GrRenderTargetContext::MakeFromBackendTexture(
193 context0, colorType, nullptr, backendTex, 1, origin, nullptr, nullptr, nullptr);
Robert Phillips26caf892017-01-27 10:58:31 -0500194 if (temp) {
Robert Phillipsd46697a2017-01-25 12:10:37 -0500195 ERRORF(reporter, "Should not be able to wrap an EXTERNAL texture as a RT.");
196 }
kkinnunen59319222015-11-22 23:23:53 -0800197 }
bsalomona98419b2015-11-23 07:09:50 -0800198
Brian Salomon28a8f282019-10-24 20:07:39 -0400199 TestReadPixels(reporter, surfaceContext.get(), pixels.get(), "EGLImageTest-read");
bsalomona98419b2015-11-23 07:09:50 -0800200
Robert Phillips3500b772017-01-27 10:11:42 -0500201 // We should not be able to write to a EXTERNAL texture
Brian Salomon28a8f282019-10-24 20:07:39 -0400202 TestWritePixels(reporter, surfaceContext.get(), false, "EGLImageTest-write");
bsalomona98419b2015-11-23 07:09:50 -0800203
Robert Phillips3500b772017-01-27 10:11:42 -0500204 // Only test RT-config
205 // TODO: why do we always need to draw to copy from an external texture?
Brian Salomon28a8f282019-10-24 20:07:39 -0400206 TestCopyFromSurface(reporter, context0, surfaceContext->asSurfaceProxy(),
Greg Danielbfa19c42019-12-19 16:41:40 -0500207 colorType, pixels.get(), "EGLImageTest-copy");
bsalomona98419b2015-11-23 07:09:50 -0800208
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500209 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
bsalomon7ea33f52015-11-22 14:51:00 -0800210}
John Rosascoa9b348f2019-11-08 13:18:15 -0800211
212#endif // SK_GL