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