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" |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 12 | #include "GrContextPriv.h" |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 13 | #include "GrContextFactory.h" |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 14 | #include "GrShaderCaps.h" |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 15 | #include "GrSurfaceContext.h" |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 16 | #include "gl/GrGLGpu.h" |
| 17 | #include "gl/GrGLUtil.h" |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 18 | #include "gl/GLTestContext.h" |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 19 | |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 20 | using sk_gpu_test::GLTestContext; |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 21 | |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 22 | static void cleanup(GLTestContext* glctx0, GrGLuint texID0, GLTestContext* glctx1, GrContext* grctx1, |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 23 | const GrGLTextureInfo* grbackendtex1, GrEGLImage image1) { |
| 24 | if (glctx1) { |
| 25 | glctx1->makeCurrent(); |
| 26 | if (grctx1) { |
| 27 | if (grbackendtex1) { |
| 28 | GrGLGpu* gpu1 = static_cast<GrGLGpu*>(grctx1->getGpu()); |
| 29 | GrBackendObject handle = reinterpret_cast<GrBackendObject>(grbackendtex1); |
| 30 | gpu1->deleteTestingOnlyBackendTexture(handle, false); |
| 31 | } |
| 32 | grctx1->unref(); |
| 33 | } |
| 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) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 46 | GrContext* context0 = ctxInfo.grContext(); |
| 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 | } |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 55 | GrGLGpu* gpu0 = static_cast<GrGLGpu*>(context0->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 | } |
| 64 | GrContext* context1 = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)glCtx1->gl()); |
| 65 | const GrGLTextureInfo* backendTexture1 = nullptr; |
| 66 | GrEGLImage image = GR_EGL_NO_IMAGE; |
| 67 | GrGLTextureInfo externalTexture; |
| 68 | externalTexture.fID = 0; |
| 69 | |
| 70 | if (!context1) { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 71 | cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image); |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 72 | return; |
| 73 | } |
| 74 | |
| 75 | if (!glCtx1->gl()->hasExtension("EGL_KHR_image") || |
| 76 | !glCtx1->gl()->hasExtension("EGL_KHR_gl_texture_2D_image")) { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 77 | cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image); |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 78 | return; |
| 79 | } |
| 80 | |
| 81 | ///////////////////////////////// CONTEXT 1 /////////////////////////////////// |
| 82 | |
| 83 | // Use GL Context 1 to create a texture unknown to GrContext. |
| 84 | context1->flush(); |
| 85 | GrGpu* gpu1 = context1->getGpu(); |
| 86 | static const int kSize = 100; |
| 87 | backendTexture1 = reinterpret_cast<const GrGLTextureInfo*>( |
| 88 | gpu1->createTestingOnlyBackendTexture(nullptr, kSize, kSize, kRGBA_8888_GrPixelConfig)); |
| 89 | if (!backendTexture1 || !backendTexture1->fID) { |
| 90 | ERRORF(reporter, "Error creating texture for EGL Image"); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 91 | cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image); |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 92 | return; |
| 93 | } |
| 94 | if (GR_GL_TEXTURE_2D != backendTexture1->fTarget) { |
| 95 | ERRORF(reporter, "Expected backend texture to be 2D"); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 96 | cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image); |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | |
| 100 | // Wrap the texture in an EGLImage |
| 101 | image = glCtx1->texture2DToEGLImage(backendTexture1->fID); |
| 102 | if (GR_EGL_NO_IMAGE == image) { |
| 103 | ERRORF(reporter, "Error creating EGL Image from texture"); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 104 | cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image); |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 105 | return; |
| 106 | } |
| 107 | |
| 108 | // Populate the texture using GL context 1. Important to use TexSubImage as TexImage orphans |
| 109 | // the EGL image. Also, this must be done after creating the EGLImage as the texture |
| 110 | // contents may not be preserved when the image is created. |
| 111 | SkAutoTMalloc<uint32_t> pixels(kSize * kSize); |
| 112 | for (int i = 0; i < kSize*kSize; ++i) { |
| 113 | pixels.get()[i] = 0xDDAABBCC; |
| 114 | } |
| 115 | GR_GL_CALL(glCtx1->gl(), ActiveTexture(GR_GL_TEXTURE0)); |
| 116 | GR_GL_CALL(glCtx1->gl(), BindTexture(backendTexture1->fTarget, backendTexture1->fID)); |
| 117 | GR_GL_CALL(glCtx1->gl(), TexSubImage2D(backendTexture1->fTarget, 0, 0, 0, kSize, kSize, |
| 118 | GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, pixels.get())); |
| 119 | GR_GL_CALL(glCtx1->gl(), Finish()); |
| 120 | // We've been making direct GL calls in GL context 1, let GrContext 1 know its internal |
| 121 | // state is invalid. |
| 122 | context1->resetContext(); |
| 123 | |
| 124 | ///////////////////////////////// CONTEXT 0 /////////////////////////////////// |
| 125 | |
| 126 | // Make a new texture ID in GL Context 0 from the EGL Image |
| 127 | glCtx0->makeCurrent(); |
| 128 | externalTexture.fTarget = GR_GL_TEXTURE_EXTERNAL; |
| 129 | externalTexture.fID = glCtx0->eglImageToExternalTexture(image); |
| 130 | |
| 131 | // Wrap this texture ID in a GrTexture |
| 132 | GrBackendTextureDesc externalDesc; |
| 133 | externalDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 134 | externalDesc.fWidth = kSize; |
| 135 | externalDesc.fHeight = kSize; |
| 136 | externalDesc.fTextureHandle = reinterpret_cast<GrBackendObject>(&externalTexture); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 137 | |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 138 | sk_sp<GrSurfaceContext> surfaceContext = context0->contextPriv().makeBackendSurfaceContext( |
| 139 | externalDesc, nullptr); |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 140 | |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 141 | if (!surfaceContext) { |
| 142 | ERRORF(reporter, "Error wrapping external texture in GrSurfaceContext."); |
| 143 | cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image); |
| 144 | return; |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 145 | } |
| 146 | |
bsalomon | a98419b | 2015-11-23 07:09:50 -0800 | [diff] [blame] | 147 | // Should not be able to wrap as a RT |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 148 | { |
| 149 | externalDesc.fFlags = kRenderTarget_GrBackendTextureFlag; |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 150 | |
| 151 | sk_sp<GrSurfaceContext> temp = context0->contextPriv().makeBackendSurfaceContext( |
| 152 | externalDesc, nullptr); |
| 153 | if (temp) { |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 154 | ERRORF(reporter, "Should not be able to wrap an EXTERNAL texture as a RT."); |
| 155 | } |
| 156 | externalDesc.fFlags = kNone_GrBackendTextureFlag; |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 157 | } |
bsalomon | a98419b | 2015-11-23 07:09:50 -0800 | [diff] [blame] | 158 | |
| 159 | // Should not be able to wrap with a sample count |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 160 | { |
| 161 | externalDesc.fSampleCnt = 4; |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 162 | sk_sp<GrSurfaceContext> temp = context0->contextPriv().makeBackendSurfaceContext( |
| 163 | externalDesc, nullptr); |
| 164 | if (temp) { |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 165 | ERRORF(reporter, "Should not be able to wrap an EXTERNAL texture with MSAA."); |
| 166 | } |
| 167 | externalDesc.fSampleCnt = 0; |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 168 | } |
bsalomon | a98419b | 2015-11-23 07:09:50 -0800 | [diff] [blame] | 169 | |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 170 | test_read_pixels(reporter, context0, surfaceContext.get(), pixels.get(), |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 171 | "EGLImageTest-read"); |
bsalomon | a98419b | 2015-11-23 07:09:50 -0800 | [diff] [blame] | 172 | |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 173 | // We should not be able to write to a EXTERNAL texture |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 174 | test_write_pixels(reporter, context0, surfaceContext.get(), false, |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 175 | "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 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [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 |