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" |
| 9 | #if SK_SUPPORT_GPU |
| 10 | #include "GrContext.h" |
| 11 | #include "GrContextFactory.h" |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 12 | #include "GrShaderCaps.h" |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 13 | #include "GrSurfaceContext.h" |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 14 | #include "gl/GrGLGpu.h" |
| 15 | #include "gl/GrGLUtil.h" |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 16 | #include "gl/GLTestContext.h" |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 17 | |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 18 | using sk_gpu_test::GLTestContext; |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 19 | |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 20 | static void cleanup(GLTestContext* glctx0, GrGLuint texID0, GLTestContext* glctx1, GrContext* grctx1, |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 21 | const GrGLTextureInfo* grbackendtex1, GrEGLImage image1) { |
| 22 | if (glctx1) { |
| 23 | glctx1->makeCurrent(); |
| 24 | if (grctx1) { |
| 25 | if (grbackendtex1) { |
| 26 | GrGLGpu* gpu1 = static_cast<GrGLGpu*>(grctx1->getGpu()); |
| 27 | GrBackendObject handle = reinterpret_cast<GrBackendObject>(grbackendtex1); |
| 28 | gpu1->deleteTestingOnlyBackendTexture(handle, false); |
| 29 | } |
| 30 | grctx1->unref(); |
| 31 | } |
| 32 | if (GR_EGL_NO_IMAGE != image1) { |
| 33 | glctx1->destroyEGLImage(image1); |
| 34 | } |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | glctx0->makeCurrent(); |
| 38 | if (texID0) { |
| 39 | GR_GL_CALL(glctx0->gl(), DeleteTextures(1, &texID0)); |
| 40 | } |
| 41 | } |
| 42 | |
bsalomon | a98419b | 2015-11-23 07:09:50 -0800 | [diff] [blame] | 43 | static void test_read_pixels(skiatest::Reporter* reporter, GrContext* context, |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 44 | GrSurface* externalTexture, uint32_t expectedPixelValues[]) { |
bsalomon | a98419b | 2015-11-23 07:09:50 -0800 | [diff] [blame] | 45 | int pixelCnt = externalTexture->width() * externalTexture->height(); |
| 46 | SkAutoTMalloc<uint32_t> pixels(pixelCnt); |
| 47 | memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt); |
| 48 | bool read = externalTexture->readPixels(0, 0, externalTexture->width(), |
| 49 | externalTexture->height(), kRGBA_8888_GrPixelConfig, |
| 50 | pixels.get()); |
| 51 | if (!read) { |
| 52 | ERRORF(reporter, "Error reading external texture."); |
| 53 | } |
| 54 | for (int i = 0; i < pixelCnt; ++i) { |
| 55 | if (pixels.get()[i] != expectedPixelValues[i]) { |
| 56 | ERRORF(reporter, "Error, external texture pixel value %d should be 0x%08x," |
| 57 | " got 0x%08x.", i, expectedPixelValues[i], pixels.get()[i]); |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | static void test_write_pixels(skiatest::Reporter* reporter, GrContext* context, |
| 64 | GrTexture* externalTexture) { |
| 65 | int pixelCnt = externalTexture->width() * externalTexture->height(); |
| 66 | SkAutoTMalloc<uint32_t> pixels(pixelCnt); |
| 67 | memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt); |
| 68 | bool write = externalTexture->writePixels(0, 0, 0, 0, kRGBA_8888_GrPixelConfig, pixels.get()); |
| 69 | REPORTER_ASSERT_MESSAGE(reporter, !write, "Should not be able to write to a EXTERNAL" |
| 70 | " texture."); |
| 71 | } |
| 72 | |
| 73 | static void test_copy_surface(skiatest::Reporter* reporter, GrContext* context, |
| 74 | GrTexture* externalTexture, uint32_t expectedPixelValues[]) { |
| 75 | GrSurfaceDesc copyDesc; |
| 76 | copyDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 77 | copyDesc.fWidth = externalTexture->width(); |
| 78 | copyDesc.fHeight = externalTexture->height(); |
| 79 | copyDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 80 | |
| 81 | sk_sp<GrSurfaceProxy> copy(GrSurfaceProxy::TestCopy(context, copyDesc, |
| 82 | externalTexture, SkBudgeted::kYes)); |
| 83 | |
| 84 | GrSurface* copySurf = copy->instantiate(context->textureProvider()); |
| 85 | |
| 86 | test_read_pixels(reporter, context, copySurf, expectedPixelValues); |
bsalomon | a98419b | 2015-11-23 07:09:50 -0800 | [diff] [blame] | 87 | } |
| 88 | |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 89 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(EGLImageTest, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 90 | GrContext* context0 = ctxInfo.grContext(); |
| 91 | sk_gpu_test::GLTestContext* glCtx0 = ctxInfo.glContext(); |
bsalomon | f2f1c17 | 2016-04-05 12:59:06 -0700 | [diff] [blame] | 92 | |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 93 | // Try to create a second GL context and then check if the contexts have necessary |
| 94 | // extensions to run this test. |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 95 | |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 96 | if (kGLES_GrGLStandard != glCtx0->gl()->fStandard) { |
| 97 | return; |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 98 | } |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 99 | GrGLGpu* gpu0 = static_cast<GrGLGpu*>(context0->getGpu()); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 100 | if (!gpu0->glCaps().shaderCaps()->externalTextureSupport()) { |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 101 | return; |
| 102 | } |
| 103 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 104 | std::unique_ptr<GLTestContext> glCtx1 = glCtx0->makeNew(); |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 105 | if (!glCtx1) { |
| 106 | return; |
| 107 | } |
| 108 | GrContext* context1 = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)glCtx1->gl()); |
| 109 | const GrGLTextureInfo* backendTexture1 = nullptr; |
| 110 | GrEGLImage image = GR_EGL_NO_IMAGE; |
| 111 | GrGLTextureInfo externalTexture; |
| 112 | externalTexture.fID = 0; |
| 113 | |
| 114 | if (!context1) { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [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 | |
| 119 | if (!glCtx1->gl()->hasExtension("EGL_KHR_image") || |
| 120 | !glCtx1->gl()->hasExtension("EGL_KHR_gl_texture_2D_image")) { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 121 | cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image); |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 122 | return; |
| 123 | } |
| 124 | |
| 125 | ///////////////////////////////// CONTEXT 1 /////////////////////////////////// |
| 126 | |
| 127 | // Use GL Context 1 to create a texture unknown to GrContext. |
| 128 | context1->flush(); |
| 129 | GrGpu* gpu1 = context1->getGpu(); |
| 130 | static const int kSize = 100; |
| 131 | backendTexture1 = reinterpret_cast<const GrGLTextureInfo*>( |
| 132 | gpu1->createTestingOnlyBackendTexture(nullptr, kSize, kSize, kRGBA_8888_GrPixelConfig)); |
| 133 | if (!backendTexture1 || !backendTexture1->fID) { |
| 134 | ERRORF(reporter, "Error creating texture for EGL Image"); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 135 | cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image); |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 136 | return; |
| 137 | } |
| 138 | if (GR_GL_TEXTURE_2D != backendTexture1->fTarget) { |
| 139 | ERRORF(reporter, "Expected backend texture to be 2D"); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 140 | cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image); |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 141 | return; |
| 142 | } |
| 143 | |
| 144 | // Wrap the texture in an EGLImage |
| 145 | image = glCtx1->texture2DToEGLImage(backendTexture1->fID); |
| 146 | if (GR_EGL_NO_IMAGE == image) { |
| 147 | ERRORF(reporter, "Error creating EGL Image from texture"); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 148 | cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image); |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 149 | return; |
| 150 | } |
| 151 | |
| 152 | // Populate the texture using GL context 1. Important to use TexSubImage as TexImage orphans |
| 153 | // the EGL image. Also, this must be done after creating the EGLImage as the texture |
| 154 | // contents may not be preserved when the image is created. |
| 155 | SkAutoTMalloc<uint32_t> pixels(kSize * kSize); |
| 156 | for (int i = 0; i < kSize*kSize; ++i) { |
| 157 | pixels.get()[i] = 0xDDAABBCC; |
| 158 | } |
| 159 | GR_GL_CALL(glCtx1->gl(), ActiveTexture(GR_GL_TEXTURE0)); |
| 160 | GR_GL_CALL(glCtx1->gl(), BindTexture(backendTexture1->fTarget, backendTexture1->fID)); |
| 161 | GR_GL_CALL(glCtx1->gl(), TexSubImage2D(backendTexture1->fTarget, 0, 0, 0, kSize, kSize, |
| 162 | GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, pixels.get())); |
| 163 | GR_GL_CALL(glCtx1->gl(), Finish()); |
| 164 | // We've been making direct GL calls in GL context 1, let GrContext 1 know its internal |
| 165 | // state is invalid. |
| 166 | context1->resetContext(); |
| 167 | |
| 168 | ///////////////////////////////// CONTEXT 0 /////////////////////////////////// |
| 169 | |
| 170 | // Make a new texture ID in GL Context 0 from the EGL Image |
| 171 | glCtx0->makeCurrent(); |
| 172 | externalTexture.fTarget = GR_GL_TEXTURE_EXTERNAL; |
| 173 | externalTexture.fID = glCtx0->eglImageToExternalTexture(image); |
| 174 | |
| 175 | // Wrap this texture ID in a GrTexture |
| 176 | GrBackendTextureDesc externalDesc; |
| 177 | externalDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 178 | externalDesc.fWidth = kSize; |
| 179 | externalDesc.fHeight = kSize; |
| 180 | externalDesc.fTextureHandle = reinterpret_cast<GrBackendObject>(&externalTexture); |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 181 | sk_sp<GrTexture> externalTextureObj( |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 182 | context0->textureProvider()->wrapBackendTexture(externalDesc)); |
| 183 | if (!externalTextureObj) { |
| 184 | ERRORF(reporter, "Error wrapping external texture in GrTexture."); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 185 | cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image); |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 186 | return; |
| 187 | } |
| 188 | |
bsalomon | a98419b | 2015-11-23 07:09:50 -0800 | [diff] [blame] | 189 | // Should not be able to wrap as a RT |
| 190 | externalDesc.fFlags = kRenderTarget_GrBackendTextureFlag; |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 191 | sk_sp<GrTexture> externalTextureRTObj( |
bsalomon | a98419b | 2015-11-23 07:09:50 -0800 | [diff] [blame] | 192 | context0->textureProvider()->wrapBackendTexture(externalDesc)); |
| 193 | if (externalTextureRTObj) { |
| 194 | ERRORF(reporter, "Should not be able to wrap an EXTERNAL texture as a RT."); |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 195 | } |
bsalomon | a98419b | 2015-11-23 07:09:50 -0800 | [diff] [blame] | 196 | externalDesc.fFlags = kNone_GrBackendTextureFlag; |
| 197 | |
| 198 | // Should not be able to wrap with a sample count |
| 199 | externalDesc.fSampleCnt = 4; |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 200 | sk_sp<GrTexture> externalTextureMSAAObj( |
bsalomon | a98419b | 2015-11-23 07:09:50 -0800 | [diff] [blame] | 201 | context0->textureProvider()->wrapBackendTexture(externalDesc)); |
| 202 | if (externalTextureMSAAObj) { |
| 203 | ERRORF(reporter, "Should not be able to wrap an EXTERNAL texture with MSAA."); |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame] | 204 | } |
bsalomon | a98419b | 2015-11-23 07:09:50 -0800 | [diff] [blame] | 205 | externalDesc.fSampleCnt = 0; |
| 206 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 207 | test_read_pixels(reporter, context0, externalTextureObj.get(), pixels.get()); |
bsalomon | a98419b | 2015-11-23 07:09:50 -0800 | [diff] [blame] | 208 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 209 | test_write_pixels(reporter, context0, externalTextureObj.get()); |
bsalomon | a98419b | 2015-11-23 07:09:50 -0800 | [diff] [blame] | 210 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 211 | test_copy_surface(reporter, context0, externalTextureObj.get(), pixels.get()); |
bsalomon | a98419b | 2015-11-23 07:09:50 -0800 | [diff] [blame] | 212 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 213 | cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image); |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | #endif |