OLD | NEW |
(Empty) | |
| 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, G
rContext* 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>(grbac
kendtex1); |
| 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 |
| 40 DEF_GPUTEST(EGLImageTest, reporter, factory) { |
| 41 for (int glCtxType = 0; glCtxType < GrContextFactory::kGLContextTypeCnt; ++g
lCtxType) { |
| 42 GrContextFactory::GLContextType type = (GrContextFactory::GLContextType)
glCtxType; |
| 43 if (!GrContextFactory::IsRenderingGLContext(type)) { |
| 44 continue; |
| 45 } |
| 46 |
| 47 // Try to create a second GL context and then check if the contexts have
necessary |
| 48 // extensions to run this test. |
| 49 |
| 50 GrContext* context0 = factory->get(type); |
| 51 if (!context0) { |
| 52 continue; |
| 53 } |
| 54 SkGLContext* glCtx0 = factory->getGLContext(type); |
| 55 SkASSERT(glCtx0); |
| 56 if (kGLES_GrGLStandard != glCtx0->gl()->fStandard) { |
| 57 continue; |
| 58 } |
| 59 GrGLGpu* gpu0 = static_cast<GrGLGpu*>(context0->getGpu()); |
| 60 if (!gpu0->glCaps().externalTextureSupport()) { |
| 61 continue; |
| 62 } |
| 63 |
| 64 SkGLContext* glCtx1 = glCtx0->createNew(); |
| 65 if (!glCtx1) { |
| 66 continue; |
| 67 } |
| 68 GrContext* context1 = GrContext::Create(kOpenGL_GrBackend, (GrBackendCon
text)glCtx1->gl()); |
| 69 const GrGLTextureInfo* backendTexture1 = nullptr; |
| 70 GrEGLImage image = GR_EGL_NO_IMAGE; |
| 71 GrGLTextureInfo externalTexture; |
| 72 externalTexture.fID = 0; |
| 73 |
| 74 if (!context1) { |
| 75 cleanup(glCtx0, externalTexture.fID, glCtx1, context1, backendTextur
e1, image); |
| 76 continue; |
| 77 } |
| 78 |
| 79 if (!glCtx1->gl()->hasExtension("EGL_KHR_image") || |
| 80 !glCtx1->gl()->hasExtension("EGL_KHR_gl_texture_2D_image")) { |
| 81 cleanup(glCtx0, externalTexture.fID, glCtx1, context1, backendTextur
e1, image); |
| 82 continue; |
| 83 } |
| 84 |
| 85 ///////////////////////////////// CONTEXT 1 /////////////////////////////////// |
| 86 |
| 87 // Use GL Context 1 to create a texture unknown to GrContext. |
| 88 context1->flush(); |
| 89 GrGpu* gpu1 = context1->getGpu(); |
| 90 static const int kSize = 100; |
| 91 backendTexture1 = reinterpret_cast<const GrGLTextureInfo*>( |
| 92 gpu1->createTestingOnlyBackendTexture(nullptr, kSize, kSize, kRGBA_8
888_GrPixelConfig)); |
| 93 if (!backendTexture1 || !backendTexture1->fID) { |
| 94 ERRORF(reporter, "Error creating texture for EGL Image"); |
| 95 cleanup(glCtx0, externalTexture.fID, glCtx1, context1, backendTextur
e1, image); |
| 96 continue; |
| 97 } |
| 98 if (GR_GL_TEXTURE_2D != backendTexture1->fTarget) { |
| 99 ERRORF(reporter, "Expected backend texture to be 2D"); |
| 100 cleanup(glCtx0, externalTexture.fID, glCtx1, context1, backendTextur
e1, image); |
| 101 continue; |
| 102 } |
| 103 |
| 104 // Wrap the texture in an EGLImage |
| 105 image = glCtx1->texture2DToEGLImage(backendTexture1->fID); |
| 106 if (GR_EGL_NO_IMAGE == image) { |
| 107 ERRORF(reporter, "Error creating EGL Image from texture"); |
| 108 cleanup(glCtx0, externalTexture.fID, glCtx1, context1, backendTextur
e1, image); |
| 109 continue; |
| 110 } |
| 111 |
| 112 // Populate the texture using GL context 1. Important to use TexSubImage
as TexImage orphans |
| 113 // the EGL image. Also, this must be done after creating the EGLImage as
the texture |
| 114 // contents may not be preserved when the image is created. |
| 115 SkAutoTMalloc<uint32_t> pixels(kSize * kSize); |
| 116 for (int i = 0; i < kSize*kSize; ++i) { |
| 117 pixels.get()[i] = 0xDDAABBCC; |
| 118 } |
| 119 GR_GL_CALL(glCtx1->gl(), ActiveTexture(GR_GL_TEXTURE0)); |
| 120 GR_GL_CALL(glCtx1->gl(), BindTexture(backendTexture1->fTarget, backendTe
xture1->fID)); |
| 121 GR_GL_CALL(glCtx1->gl(), TexSubImage2D(backendTexture1->fTarget, 0, 0, 0
, kSize, kSize, |
| 122 GR_GL_RGBA, GR_GL_UNSIGNED_BYTE,
pixels.get())); |
| 123 GR_GL_CALL(glCtx1->gl(), Finish()); |
| 124 // We've been making direct GL calls in GL context 1, let GrContext 1 kn
ow its internal |
| 125 // state is invalid. |
| 126 context1->resetContext(); |
| 127 |
| 128 ///////////////////////////////// CONTEXT 0 /////////////////////////////////// |
| 129 |
| 130 // Make a new texture ID in GL Context 0 from the EGL Image |
| 131 glCtx0->makeCurrent(); |
| 132 externalTexture.fTarget = GR_GL_TEXTURE_EXTERNAL; |
| 133 externalTexture.fID = glCtx0->eglImageToExternalTexture(image); |
| 134 |
| 135 // Wrap this texture ID in a GrTexture |
| 136 GrBackendTextureDesc externalDesc; |
| 137 externalDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 138 externalDesc.fWidth = kSize; |
| 139 externalDesc.fHeight = kSize; |
| 140 externalDesc.fTextureHandle = reinterpret_cast<GrBackendObject>(&externa
lTexture); |
| 141 SkAutoTUnref<GrTexture> externalTextureObj( |
| 142 context0->textureProvider()->wrapBackendTexture(externalDesc)); |
| 143 if (!externalTextureObj) { |
| 144 ERRORF(reporter, "Error wrapping external texture in GrTexture."); |
| 145 cleanup(glCtx0, externalTexture.fID, glCtx1, context1, backendTextur
e1, image); |
| 146 continue; |
| 147 } |
| 148 |
| 149 // Read the pixels and see if we get the values set in GL context 1 |
| 150 memset(pixels.get(), 0, sizeof(uint32_t)*kSize*kSize); |
| 151 bool read = externalTextureObj->readPixels(0, 0, kSize, kSize, kRGBA_888
8_GrPixelConfig, |
| 152 pixels.get()); |
| 153 if (!read) { |
| 154 ERRORF(reporter, "Error reading external texture."); |
| 155 cleanup(glCtx0, externalTexture.fID, glCtx1, context1, backendTextur
e1, image); |
| 156 continue; |
| 157 } |
| 158 for (int i = 0; i < kSize*kSize; ++i) { |
| 159 if (pixels.get()[i] != 0xDDAABBCC) { |
| 160 ERRORF(reporter, "Error, external texture pixel value %d should
be 0xDDAABBCC," |
| 161 " got 0x%08x.", pixels.get()[i]); |
| 162 break; |
| 163 } |
| 164 } |
| 165 cleanup(glCtx0, externalTexture.fID, glCtx1, context1, backendTexture1,
image); |
| 166 } |
| 167 } |
| 168 |
| 169 #endif |
OLD | NEW |