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