bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #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" |
| 14 | #include "src/gpu/GrTextureContext.h" |
| 15 | #include "src/gpu/GrTexturePriv.h" |
| 16 | #include "src/gpu/GrTextureProxyPriv.h" |
| 17 | #include "src/gpu/gl/GrGLGpu.h" |
| 18 | #include "src/gpu/gl/GrGLUtil.h" |
| 19 | #include "tests/Test.h" |
| 20 | #include "tests/TestUtils.h" |
| 21 | #include "tools/gpu/GrContextFactory.h" |
| 22 | #include "tools/gpu/gl/GLTestContext.h" |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 23 | |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 24 | using sk_gpu_test::GLTestContext; |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 25 | |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 26 | static void cleanup(GLTestContext* glctx0, GrGLuint texID0, GLTestContext* glctx1, |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 27 | sk_sp<GrContext> grctx1, GrBackendTexture* backendTex1, |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 28 | GrEGLImage image1) { |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 29 | if (glctx1) { |
| 30 | glctx1->makeCurrent(); |
| 31 | if (grctx1) { |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 32 | if (backendTex1 && backendTex1->isValid()) { |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame] | 33 | grctx1->deleteBackendTexture(*backendTex1); |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 34 | } |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 35 | } |
| 36 | if (GR_EGL_NO_IMAGE != image1) { |
| 37 | glctx1->destroyEGLImage(image1); |
| 38 | } |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | glctx0->makeCurrent(); |
| 42 | if (texID0) { |
| 43 | GR_GL_CALL(glctx0->gl(), DeleteTextures(1, &texID0)); |
| 44 | } |
| 45 | } |
| 46 | |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 47 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(EGLImageTest, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 48 | GrContext* context0 = ctxInfo.grContext(); |
| 49 | sk_gpu_test::GLTestContext* glCtx0 = ctxInfo.glContext(); |
bsalomon | f2f1c17 | 2016-04-05 12:59:06 -0700 | [diff] [blame] | 50 | |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 51 | // Try to create a second GL context and then check if the contexts have necessary |
| 52 | // extensions to run this test. |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 53 | |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 54 | if (kGLES_GrGLStandard != glCtx0->gl()->fStandard) { |
| 55 | return; |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 56 | } |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 57 | GrGLGpu* gpu0 = static_cast<GrGLGpu*>(context0->priv().getGpu()); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 58 | if (!gpu0->glCaps().shaderCaps()->externalTextureSupport()) { |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 59 | return; |
| 60 | } |
| 61 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 62 | std::unique_ptr<GLTestContext> glCtx1 = glCtx0->makeNew(); |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 63 | if (!glCtx1) { |
| 64 | return; |
| 65 | } |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 66 | sk_sp<GrContext> context1 = GrContext::MakeGL(sk_ref_sp(glCtx1->gl())); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 67 | GrBackendTexture backendTexture1; |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 68 | GrEGLImage image = GR_EGL_NO_IMAGE; |
| 69 | GrGLTextureInfo externalTexture; |
| 70 | externalTexture.fID = 0; |
| 71 | |
| 72 | if (!context1) { |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 73 | cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image); |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 74 | return; |
| 75 | } |
| 76 | |
| 77 | if (!glCtx1->gl()->hasExtension("EGL_KHR_image") || |
| 78 | !glCtx1->gl()->hasExtension("EGL_KHR_gl_texture_2D_image")) { |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 79 | cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image); |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 80 | return; |
| 81 | } |
| 82 | |
| 83 | ///////////////////////////////// CONTEXT 1 /////////////////////////////////// |
| 84 | |
| 85 | // Use GL Context 1 to create a texture unknown to GrContext. |
| 86 | context1->flush(); |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 87 | static const int kSize = 100; |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 88 | backendTexture1 = |
Robert Phillips | 4bdd36f | 2019-06-04 11:03:06 -0400 | [diff] [blame] | 89 | context1->createBackendTexture(kSize, kSize, kRGBA_8888_SkColorType, |
| 90 | SkColors::kTransparent, |
Robert Phillips | da2e67a | 2019-07-01 15:04:06 -0400 | [diff] [blame] | 91 | GrMipMapped::kNo, GrRenderable::kNo, GrProtected::kNo); |
Greg Daniel | 5366e59 | 2018-01-10 09:57:53 -0500 | [diff] [blame] | 92 | |
Robert Phillips | 7c8af17 | 2019-07-08 15:55:24 -0400 | [diff] [blame] | 93 | if (!backendTexture1.isValid()) { |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 94 | ERRORF(reporter, "Error creating texture for EGL Image"); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 95 | cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image); |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 96 | return; |
| 97 | } |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 98 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 99 | GrGLTextureInfo texInfo; |
| 100 | if (!backendTexture1.getGLTextureInfo(&texInfo)) { |
| 101 | ERRORF(reporter, "Failed to get GrGLTextureInfo"); |
| 102 | return; |
| 103 | } |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 104 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 105 | if (GR_GL_TEXTURE_2D != texInfo.fTarget) { |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 106 | ERRORF(reporter, "Expected backend texture to be 2D"); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 107 | cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image); |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 108 | return; |
| 109 | } |
| 110 | |
| 111 | // Wrap the texture in an EGLImage |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 112 | image = glCtx1->texture2DToEGLImage(texInfo.fID); |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 113 | if (GR_EGL_NO_IMAGE == image) { |
| 114 | ERRORF(reporter, "Error creating EGL Image from texture"); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 115 | cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image); |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 116 | return; |
| 117 | } |
| 118 | |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 119 | // Since we are dealing with two different GL contexts here, we need to call finish so that the |
| 120 | // clearing of the texture that happens in createTextingOnlyBackendTexture occurs before we call |
| 121 | // TexSubImage below on the other context. Otherwise, it is possible the calls get reordered and |
| 122 | // the clearing overwrites the TexSubImage writes. |
| 123 | GR_GL_CALL(glCtx1->gl(), Finish()); |
| 124 | |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 125 | // Populate the texture using GL context 1. Important to use TexSubImage as TexImage orphans |
| 126 | // the EGL image. Also, this must be done after creating the EGLImage as the texture |
| 127 | // contents may not be preserved when the image is created. |
| 128 | SkAutoTMalloc<uint32_t> pixels(kSize * kSize); |
| 129 | for (int i = 0; i < kSize*kSize; ++i) { |
| 130 | pixels.get()[i] = 0xDDAABBCC; |
| 131 | } |
| 132 | GR_GL_CALL(glCtx1->gl(), ActiveTexture(GR_GL_TEXTURE0)); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 133 | GR_GL_CALL(glCtx1->gl(), BindTexture(texInfo.fTarget, texInfo.fID)); |
| 134 | GR_GL_CALL(glCtx1->gl(), TexSubImage2D(texInfo.fTarget, 0, 0, 0, kSize, kSize, |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 135 | GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, pixels.get())); |
| 136 | GR_GL_CALL(glCtx1->gl(), Finish()); |
| 137 | // We've been making direct GL calls in GL context 1, let GrContext 1 know its internal |
| 138 | // state is invalid. |
| 139 | context1->resetContext(); |
| 140 | |
| 141 | ///////////////////////////////// CONTEXT 0 /////////////////////////////////// |
| 142 | |
| 143 | // Make a new texture ID in GL Context 0 from the EGL Image |
| 144 | glCtx0->makeCurrent(); |
| 145 | externalTexture.fTarget = GR_GL_TEXTURE_EXTERNAL; |
| 146 | externalTexture.fID = glCtx0->eglImageToExternalTexture(image); |
Greg Daniel | 2b5be0a | 2019-06-13 16:50:49 -0400 | [diff] [blame] | 147 | externalTexture.fFormat = GR_GL_RGBA8; |
Robert Phillips | fee2b4e | 2017-06-08 18:22:53 -0400 | [diff] [blame] | 148 | if (0 == externalTexture.fID) { |
| 149 | ERRORF(reporter, "Error converting EGL Image back to texture"); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 150 | cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image); |
Robert Phillips | fee2b4e | 2017-06-08 18:22:53 -0400 | [diff] [blame] | 151 | return; |
| 152 | } |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 153 | |
| 154 | // Wrap this texture ID in a GrTexture |
Greg Daniel | 108bb23 | 2018-07-03 16:18:29 -0400 | [diff] [blame] | 155 | GrBackendTexture backendTex(kSize, kSize, GrMipMapped::kNo, externalTexture); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 156 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 157 | // TODO: If I make this TopLeft origin to match resolve_origin calls for kDefault, this test |
| 158 | // fails on the Nexus5. Why? |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 159 | auto surfaceContext = context0->priv().makeBackendTextureContext( |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 160 | backendTex, kBottomLeft_GrSurfaceOrigin, GrColorType::kRGBA_8888, kPremul_SkAlphaType, |
| 161 | nullptr); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 162 | |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 163 | if (!surfaceContext) { |
| 164 | ERRORF(reporter, "Error wrapping external texture in GrSurfaceContext."); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 165 | cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image); |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 166 | return; |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 169 | GrTextureProxy* proxy = surfaceContext->asTextureProxy(); |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 170 | REPORTER_ASSERT(reporter, proxy->mipMapped() == GrMipMapped::kNo); |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 171 | REPORTER_ASSERT(reporter, proxy->peekTexture()->texturePriv().mipMapped() == GrMipMapped::kNo); |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 172 | |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 173 | REPORTER_ASSERT(reporter, proxy->textureType() == GrTextureType::kExternal); |
| 174 | REPORTER_ASSERT(reporter, |
| 175 | proxy->peekTexture()->texturePriv().textureType() == GrTextureType::kExternal); |
| 176 | REPORTER_ASSERT(reporter, proxy->hasRestrictedSampling()); |
| 177 | REPORTER_ASSERT(reporter, proxy->peekTexture()->texturePriv().hasRestrictedSampling()); |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 178 | |
bsalomon | a98419b | 2015-11-23 07:09:50 -0800 | [diff] [blame] | 179 | // Should not be able to wrap as a RT |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 180 | { |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 181 | auto temp = context0->priv().makeBackendTextureRenderTargetContext( |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 182 | backendTex, kBottomLeft_GrSurfaceOrigin, 1, GrColorType::kRGBA_8888, nullptr); |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 183 | if (temp) { |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 184 | ERRORF(reporter, "Should not be able to wrap an EXTERNAL texture as a RT."); |
| 185 | } |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 186 | } |
bsalomon | a98419b | 2015-11-23 07:09:50 -0800 | [diff] [blame] | 187 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 188 | test_read_pixels(reporter, surfaceContext.get(), pixels.get(), "EGLImageTest-read"); |
bsalomon | a98419b | 2015-11-23 07:09:50 -0800 | [diff] [blame] | 189 | |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 190 | // We should not be able to write to a EXTERNAL texture |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 191 | test_write_pixels(reporter, surfaceContext.get(), false, "EGLImageTest-write"); |
bsalomon | a98419b | 2015-11-23 07:09:50 -0800 | [diff] [blame] | 192 | |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 193 | // Only test RT-config |
| 194 | // TODO: why do we always need to draw to copy from an external texture? |
Robert Phillips | f200a90 | 2017-01-30 13:27:37 -0500 | [diff] [blame] | 195 | test_copy_from_surface(reporter, context0, surfaceContext->asSurfaceProxy(), |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 196 | GrColorType::kRGBA_8888, pixels.get(), "EGLImageTest-copy"); |
bsalomon | a98419b | 2015-11-23 07:09:50 -0800 | [diff] [blame] | 197 | |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 198 | cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image); |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 199 | } |