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