EGLImageTest.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. #include "include/gpu/GrContext.h"
  8. #include "include/gpu/GrTexture.h"
  9. #include "src/gpu/GrContextPriv.h"
  10. #include "src/gpu/GrRenderTargetContext.h"
  11. #include "src/gpu/GrShaderCaps.h"
  12. #include "src/gpu/GrSurfacePriv.h"
  13. #include "src/gpu/GrTextureContext.h"
  14. #include "src/gpu/GrTexturePriv.h"
  15. #include "src/gpu/GrTextureProxyPriv.h"
  16. #include "src/gpu/gl/GrGLGpu.h"
  17. #include "src/gpu/gl/GrGLUtil.h"
  18. #include "tests/Test.h"
  19. #include "tests/TestUtils.h"
  20. #include "tools/gpu/GrContextFactory.h"
  21. #include "tools/gpu/gl/GLTestContext.h"
  22. using sk_gpu_test::GLTestContext;
  23. static void cleanup(GLTestContext* glctx0, GrGLuint texID0, GLTestContext* glctx1,
  24. sk_sp<GrContext> grctx1, GrBackendTexture* backendTex1,
  25. GrEGLImage image1) {
  26. if (glctx1) {
  27. glctx1->makeCurrent();
  28. if (grctx1) {
  29. if (backendTex1 && backendTex1->isValid()) {
  30. grctx1->deleteBackendTexture(*backendTex1);
  31. }
  32. }
  33. if (GR_EGL_NO_IMAGE != image1) {
  34. glctx1->destroyEGLImage(image1);
  35. }
  36. }
  37. glctx0->makeCurrent();
  38. if (texID0) {
  39. GR_GL_CALL(glctx0->gl(), DeleteTextures(1, &texID0));
  40. }
  41. }
  42. DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(EGLImageTest, reporter, ctxInfo) {
  43. GrContext* context0 = ctxInfo.grContext();
  44. sk_gpu_test::GLTestContext* glCtx0 = ctxInfo.glContext();
  45. // Try to create a second GL context and then check if the contexts have necessary
  46. // extensions to run this test.
  47. if (kGLES_GrGLStandard != glCtx0->gl()->fStandard) {
  48. return;
  49. }
  50. GrGLGpu* gpu0 = static_cast<GrGLGpu*>(context0->priv().getGpu());
  51. if (!gpu0->glCaps().shaderCaps()->externalTextureSupport()) {
  52. return;
  53. }
  54. std::unique_ptr<GLTestContext> glCtx1 = glCtx0->makeNew();
  55. if (!glCtx1) {
  56. return;
  57. }
  58. sk_sp<GrContext> context1 = GrContext::MakeGL(sk_ref_sp(glCtx1->gl()));
  59. GrBackendTexture backendTexture1;
  60. GrEGLImage image = GR_EGL_NO_IMAGE;
  61. GrGLTextureInfo externalTexture;
  62. externalTexture.fID = 0;
  63. if (!context1) {
  64. cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
  65. return;
  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.get(), context1, &backendTexture1, image);
  70. return;
  71. }
  72. ///////////////////////////////// CONTEXT 1 ///////////////////////////////////
  73. // Use GL Context 1 to create a texture unknown to GrContext.
  74. context1->flush();
  75. static const int kSize = 100;
  76. backendTexture1 =
  77. context1->createBackendTexture(kSize, kSize, kRGBA_8888_SkColorType,
  78. SkColors::kTransparent,
  79. GrMipMapped::kNo, GrRenderable::kNo, GrProtected::kNo);
  80. if (!backendTexture1.isValid()) {
  81. ERRORF(reporter, "Error creating texture for EGL Image");
  82. cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
  83. return;
  84. }
  85. GrGLTextureInfo texInfo;
  86. if (!backendTexture1.getGLTextureInfo(&texInfo)) {
  87. ERRORF(reporter, "Failed to get GrGLTextureInfo");
  88. return;
  89. }
  90. if (GR_GL_TEXTURE_2D != texInfo.fTarget) {
  91. ERRORF(reporter, "Expected backend texture to be 2D");
  92. cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
  93. return;
  94. }
  95. // Wrap the texture in an EGLImage
  96. image = glCtx1->texture2DToEGLImage(texInfo.fID);
  97. if (GR_EGL_NO_IMAGE == image) {
  98. ERRORF(reporter, "Error creating EGL Image from texture");
  99. cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
  100. return;
  101. }
  102. // Since we are dealing with two different GL contexts here, we need to call finish so that the
  103. // clearing of the texture that happens in createTextingOnlyBackendTexture occurs before we call
  104. // TexSubImage below on the other context. Otherwise, it is possible the calls get reordered and
  105. // the clearing overwrites the TexSubImage writes.
  106. GR_GL_CALL(glCtx1->gl(), Finish());
  107. // Populate the texture using GL context 1. Important to use TexSubImage as TexImage orphans
  108. // the EGL image. Also, this must be done after creating the EGLImage as the texture
  109. // contents may not be preserved when the image is created.
  110. SkAutoTMalloc<uint32_t> pixels(kSize * kSize);
  111. for (int i = 0; i < kSize*kSize; ++i) {
  112. pixels.get()[i] = 0xDDAABBCC;
  113. }
  114. GR_GL_CALL(glCtx1->gl(), ActiveTexture(GR_GL_TEXTURE0));
  115. GR_GL_CALL(glCtx1->gl(), BindTexture(texInfo.fTarget, texInfo.fID));
  116. GR_GL_CALL(glCtx1->gl(), TexSubImage2D(texInfo.fTarget, 0, 0, 0, kSize, kSize,
  117. GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, pixels.get()));
  118. GR_GL_CALL(glCtx1->gl(), Finish());
  119. // We've been making direct GL calls in GL context 1, let GrContext 1 know its internal
  120. // state is invalid.
  121. context1->resetContext();
  122. ///////////////////////////////// CONTEXT 0 ///////////////////////////////////
  123. // Make a new texture ID in GL Context 0 from the EGL Image
  124. glCtx0->makeCurrent();
  125. externalTexture.fTarget = GR_GL_TEXTURE_EXTERNAL;
  126. externalTexture.fID = glCtx0->eglImageToExternalTexture(image);
  127. externalTexture.fFormat = GR_GL_RGBA8;
  128. if (0 == externalTexture.fID) {
  129. ERRORF(reporter, "Error converting EGL Image back to texture");
  130. cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
  131. return;
  132. }
  133. // Wrap this texture ID in a GrTexture
  134. GrBackendTexture backendTex(kSize, kSize, GrMipMapped::kNo, externalTexture);
  135. backendTex.setPixelConfig(kRGBA_8888_GrPixelConfig);
  136. // TODO: If I make this TopLeft origin to match resolve_origin calls for kDefault, this test
  137. // fails on the Nexus5. Why?
  138. sk_sp<GrTextureContext> surfaceContext = context0->priv().makeBackendTextureContext(
  139. backendTex, kBottomLeft_GrSurfaceOrigin, GrColorType::kRGBA_8888, kPremul_SkAlphaType,
  140. nullptr);
  141. if (!surfaceContext) {
  142. ERRORF(reporter, "Error wrapping external texture in GrSurfaceContext.");
  143. cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
  144. return;
  145. }
  146. GrTextureProxy* proxy = surfaceContext->asTextureProxy();
  147. REPORTER_ASSERT(reporter, proxy->mipMapped() == GrMipMapped::kNo);
  148. REPORTER_ASSERT(reporter, proxy->peekTexture()->texturePriv().mipMapped() == GrMipMapped::kNo);
  149. REPORTER_ASSERT(reporter, proxy->textureType() == GrTextureType::kExternal);
  150. REPORTER_ASSERT(reporter,
  151. proxy->peekTexture()->texturePriv().textureType() == GrTextureType::kExternal);
  152. REPORTER_ASSERT(reporter, proxy->hasRestrictedSampling());
  153. REPORTER_ASSERT(reporter, proxy->peekTexture()->texturePriv().hasRestrictedSampling());
  154. // Should not be able to wrap as a RT
  155. {
  156. sk_sp<GrRenderTargetContext> temp = context0->priv().makeBackendTextureRenderTargetContext(
  157. backendTex, kBottomLeft_GrSurfaceOrigin, 1, GrColorType::kRGBA_8888, nullptr);
  158. if (temp) {
  159. ERRORF(reporter, "Should not be able to wrap an EXTERNAL texture as a RT.");
  160. }
  161. }
  162. test_read_pixels(reporter, surfaceContext.get(), pixels.get(), "EGLImageTest-read");
  163. // We should not be able to write to a EXTERNAL texture
  164. test_write_pixels(reporter, surfaceContext.get(), false, "EGLImageTest-write");
  165. // Only test RT-config
  166. // TODO: why do we always need to draw to copy from an external texture?
  167. test_copy_from_surface(reporter, context0, surfaceContext->asSurfaceProxy(),
  168. GrColorType::kRGBA_8888, pixels.get(), "EGLImageTest-copy");
  169. cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
  170. }