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