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" |
| 14 | #include "gl/SkGLContext.h" |
| 15 | |
| 16 | static void cleanup(SkGLContext* glctx0, GrGLuint texID0, SkGLContext* glctx1, GrContext* grctx1, |
| 17 | const GrGLTextureInfo* grbackendtex1, GrEGLImage image1) { |
| 18 | if (glctx1) { |
| 19 | glctx1->makeCurrent(); |
| 20 | if (grctx1) { |
| 21 | if (grbackendtex1) { |
| 22 | GrGLGpu* gpu1 = static_cast<GrGLGpu*>(grctx1->getGpu()); |
| 23 | GrBackendObject handle = reinterpret_cast<GrBackendObject>(grbackendtex1); |
| 24 | gpu1->deleteTestingOnlyBackendTexture(handle, false); |
| 25 | } |
| 26 | grctx1->unref(); |
| 27 | } |
| 28 | if (GR_EGL_NO_IMAGE != image1) { |
| 29 | glctx1->destroyEGLImage(image1); |
| 30 | } |
| 31 | glctx1->unref(); |
| 32 | } |
| 33 | |
| 34 | glctx0->makeCurrent(); |
| 35 | if (texID0) { |
| 36 | GR_GL_CALL(glctx0->gl(), DeleteTextures(1, &texID0)); |
| 37 | } |
| 38 | } |
| 39 | |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame^] | 40 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(EGLImageTest, reporter, context0, glCtx0) { |
| 41 | // Try to create a second GL context and then check if the contexts have necessary |
| 42 | // extensions to run this test. |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 43 | |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame^] | 44 | if (kGLES_GrGLStandard != glCtx0->gl()->fStandard) { |
| 45 | return; |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 46 | } |
kkinnunen | 5931922 | 2015-11-22 23:23:53 -0800 | [diff] [blame^] | 47 | GrGLGpu* gpu0 = static_cast<GrGLGpu*>(context0->getGpu()); |
| 48 | if (!gpu0->glCaps().externalTextureSupport()) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | SkGLContext* glCtx1 = glCtx0->createNew(); |
| 53 | if (!glCtx1) { |
| 54 | return; |
| 55 | } |
| 56 | GrContext* context1 = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)glCtx1->gl()); |
| 57 | const GrGLTextureInfo* backendTexture1 = nullptr; |
| 58 | GrEGLImage image = GR_EGL_NO_IMAGE; |
| 59 | GrGLTextureInfo externalTexture; |
| 60 | externalTexture.fID = 0; |
| 61 | |
| 62 | if (!context1) { |
| 63 | cleanup(glCtx0, externalTexture.fID, glCtx1, context1, backendTexture1, image); |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | if (!glCtx1->gl()->hasExtension("EGL_KHR_image") || |
| 68 | !glCtx1->gl()->hasExtension("EGL_KHR_gl_texture_2D_image")) { |
| 69 | cleanup(glCtx0, externalTexture.fID, glCtx1, context1, backendTexture1, image); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | ///////////////////////////////// CONTEXT 1 /////////////////////////////////// |
| 74 | |
| 75 | // Use GL Context 1 to create a texture unknown to GrContext. |
| 76 | context1->flush(); |
| 77 | GrGpu* gpu1 = context1->getGpu(); |
| 78 | static const int kSize = 100; |
| 79 | backendTexture1 = reinterpret_cast<const GrGLTextureInfo*>( |
| 80 | gpu1->createTestingOnlyBackendTexture(nullptr, kSize, kSize, kRGBA_8888_GrPixelConfig)); |
| 81 | if (!backendTexture1 || !backendTexture1->fID) { |
| 82 | ERRORF(reporter, "Error creating texture for EGL Image"); |
| 83 | cleanup(glCtx0, externalTexture.fID, glCtx1, context1, backendTexture1, image); |
| 84 | return; |
| 85 | } |
| 86 | if (GR_GL_TEXTURE_2D != backendTexture1->fTarget) { |
| 87 | ERRORF(reporter, "Expected backend texture to be 2D"); |
| 88 | cleanup(glCtx0, externalTexture.fID, glCtx1, context1, backendTexture1, image); |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | // Wrap the texture in an EGLImage |
| 93 | image = glCtx1->texture2DToEGLImage(backendTexture1->fID); |
| 94 | if (GR_EGL_NO_IMAGE == image) { |
| 95 | ERRORF(reporter, "Error creating EGL Image from texture"); |
| 96 | cleanup(glCtx0, externalTexture.fID, glCtx1, context1, backendTexture1, image); |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | // Populate the texture using GL context 1. Important to use TexSubImage as TexImage orphans |
| 101 | // the EGL image. Also, this must be done after creating the EGLImage as the texture |
| 102 | // contents may not be preserved when the image is created. |
| 103 | SkAutoTMalloc<uint32_t> pixels(kSize * kSize); |
| 104 | for (int i = 0; i < kSize*kSize; ++i) { |
| 105 | pixels.get()[i] = 0xDDAABBCC; |
| 106 | } |
| 107 | GR_GL_CALL(glCtx1->gl(), ActiveTexture(GR_GL_TEXTURE0)); |
| 108 | GR_GL_CALL(glCtx1->gl(), BindTexture(backendTexture1->fTarget, backendTexture1->fID)); |
| 109 | GR_GL_CALL(glCtx1->gl(), TexSubImage2D(backendTexture1->fTarget, 0, 0, 0, kSize, kSize, |
| 110 | GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, pixels.get())); |
| 111 | GR_GL_CALL(glCtx1->gl(), Finish()); |
| 112 | // We've been making direct GL calls in GL context 1, let GrContext 1 know its internal |
| 113 | // state is invalid. |
| 114 | context1->resetContext(); |
| 115 | |
| 116 | ///////////////////////////////// CONTEXT 0 /////////////////////////////////// |
| 117 | |
| 118 | // Make a new texture ID in GL Context 0 from the EGL Image |
| 119 | glCtx0->makeCurrent(); |
| 120 | externalTexture.fTarget = GR_GL_TEXTURE_EXTERNAL; |
| 121 | externalTexture.fID = glCtx0->eglImageToExternalTexture(image); |
| 122 | |
| 123 | // Wrap this texture ID in a GrTexture |
| 124 | GrBackendTextureDesc externalDesc; |
| 125 | externalDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 126 | externalDesc.fWidth = kSize; |
| 127 | externalDesc.fHeight = kSize; |
| 128 | externalDesc.fTextureHandle = reinterpret_cast<GrBackendObject>(&externalTexture); |
| 129 | SkAutoTUnref<GrTexture> externalTextureObj( |
| 130 | context0->textureProvider()->wrapBackendTexture(externalDesc)); |
| 131 | if (!externalTextureObj) { |
| 132 | ERRORF(reporter, "Error wrapping external texture in GrTexture."); |
| 133 | cleanup(glCtx0, externalTexture.fID, glCtx1, context1, backendTexture1, image); |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | // Read the pixels and see if we get the values set in GL context 1 |
| 138 | memset(pixels.get(), 0, sizeof(uint32_t)*kSize*kSize); |
| 139 | bool read = externalTextureObj->readPixels(0, 0, kSize, kSize, kRGBA_8888_GrPixelConfig, |
| 140 | pixels.get()); |
| 141 | if (!read) { |
| 142 | ERRORF(reporter, "Error reading external texture."); |
| 143 | cleanup(glCtx0, externalTexture.fID, glCtx1, context1, backendTexture1, image); |
| 144 | return; |
| 145 | } |
| 146 | for (int i = 0; i < kSize*kSize; ++i) { |
| 147 | if (pixels.get()[i] != 0xDDAABBCC) { |
| 148 | ERRORF(reporter, "Error, external texture pixel value %d should be 0xDDAABBCC," |
| 149 | " got 0x%08x.", pixels.get()[i]); |
| 150 | break; |
| 151 | } |
| 152 | } |
| 153 | cleanup(glCtx0, externalTexture.fID, glCtx1, context1, backendTexture1, image); |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | #endif |