gl_image_native_pixmap_unittest.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ui/gl/gl_image_native_pixmap.h"
  5. #include "build/build_config.h"
  6. #include "ui/gl/gl_bindings.h"
  7. #include "ui/gl/test/gl_image_test_template.h"
  8. // TODO(crbug.com/969798): Fix memory leaks in tests and re-enable on LSAN.
  9. #ifdef LEAK_SANITIZER
  10. #define MAYBE_GLTexture2DToDmabuf DISABLED_GLTexture2DToDmabuf
  11. #else
  12. #define MAYBE_GLTexture2DToDmabuf GLTexture2DToDmabuf
  13. #endif
  14. // TYPED_TEST_P() and REGISTER_TYPED_TEST_SUITE_P() don't do macro expansion on
  15. // their parameters, making the MAYBE_ technique above not work -- these macros
  16. // are a workaround.
  17. #define TYPED_TEST_P_WITH_EXPANSION(SuiteName, TestName) \
  18. TYPED_TEST_P(SuiteName, TestName)
  19. #define REGISTER_TYPED_TEST_SUITE_P_WITH_EXPANSION(SuiteName, ...) \
  20. REGISTER_TYPED_TEST_SUITE_P(SuiteName, __VA_ARGS__)
  21. namespace gl {
  22. namespace {
  23. const uint8_t kImageColor[] = {0x30, 0x40, 0x10, 0xFF};
  24. template <gfx::BufferFormat format>
  25. class GLImageNativePixmapTestDelegate : public GLImageTestDelegateBase {
  26. public:
  27. absl::optional<GLImplementationParts> GetPreferedGLImplementation()
  28. const override {
  29. #if BUILDFLAG(IS_WIN)
  30. return absl::optional<GLImplementationParts>(GLImplementationParts(
  31. kGLImplementationEGLANGLE, ANGLEImplementation::kNone));
  32. #else
  33. return absl::optional<GLImplementationParts>(
  34. GLImplementationParts(kGLImplementationEGLGLES2));
  35. #endif
  36. }
  37. bool SkipTest(GLDisplay* display) const override {
  38. GLDisplayEGL* display_egl = static_cast<GLDisplayEGL*>(display);
  39. if (!display_egl->ext->b_EGL_MESA_image_dma_buf_export) {
  40. LOG(WARNING) << "Skip test, missing extension "
  41. << "EGL_MESA_image_dma_buf_export";
  42. return true;
  43. }
  44. return false;
  45. }
  46. scoped_refptr<GLImageNativePixmap> CreateSolidColorImage(
  47. const gfx::Size& size,
  48. const uint8_t color[4]) const {
  49. GLuint texture_id = GLTestHelper::CreateTexture(GetTextureTarget());
  50. EXPECT_NE(0u, texture_id);
  51. std::unique_ptr<uint8_t[]> pixels(
  52. new uint8_t[BufferSizeForBufferFormat(size, format)]);
  53. GLImageTestSupport::SetBufferDataToColor(
  54. size.width(), size.height(),
  55. static_cast<int>(RowSizeForBufferFormat(size.width(), format, 0)), 0,
  56. format, color, pixels.get());
  57. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.width(), size.height(), 0,
  58. GL_RGBA, GL_UNSIGNED_BYTE, pixels.get());
  59. auto image = base::MakeRefCounted<gl::GLImageNativePixmap>(size, format);
  60. EXPECT_TRUE(image->InitializeFromTexture(texture_id));
  61. glDeleteTextures(1, &texture_id);
  62. return image;
  63. }
  64. unsigned GetTextureTarget() const { return GL_TEXTURE_2D; }
  65. const uint8_t* GetImageColor() const { return kImageColor; }
  66. int GetAdmissibleError() const { return 0; }
  67. gfx::BufferFormat GetBufferFormat() const { return format; }
  68. };
  69. template <typename GLImageTestDelegate>
  70. class GLImageNativePixmapToDmabufTest
  71. : public GLImageTest<GLImageTestDelegate> {};
  72. TYPED_TEST_SUITE_P(GLImageNativePixmapToDmabufTest);
  73. TYPED_TEST_P_WITH_EXPANSION(GLImageNativePixmapToDmabufTest,
  74. MAYBE_GLTexture2DToDmabuf) {
  75. if (this->delegate_.SkipTest(this->display_))
  76. return;
  77. const gfx::Size image_size(64, 64);
  78. const uint8_t* image_color = this->delegate_.GetImageColor();
  79. scoped_refptr<GLImageNativePixmap> image =
  80. this->delegate_.CreateSolidColorImage(image_size, image_color);
  81. ASSERT_TRUE(image);
  82. gfx::NativePixmapHandle native_pixmap_handle = image->ExportHandle();
  83. for (auto& plane : native_pixmap_handle.planes) {
  84. EXPECT_TRUE(plane.fd.is_valid());
  85. }
  86. }
  87. // This test verifies that GLImageNativePixmap can be exported as dmabuf fds.
  88. REGISTER_TYPED_TEST_SUITE_P_WITH_EXPANSION(GLImageNativePixmapToDmabufTest,
  89. MAYBE_GLTexture2DToDmabuf);
  90. using GLImageTestTypes = testing::Types<
  91. GLImageNativePixmapTestDelegate<gfx::BufferFormat::RGBX_8888>,
  92. GLImageNativePixmapTestDelegate<gfx::BufferFormat::RGBA_8888>,
  93. GLImageNativePixmapTestDelegate<gfx::BufferFormat::BGRX_8888>,
  94. GLImageNativePixmapTestDelegate<gfx::BufferFormat::BGRA_8888>,
  95. GLImageNativePixmapTestDelegate<gfx::BufferFormat::RGBA_1010102>,
  96. GLImageNativePixmapTestDelegate<gfx::BufferFormat::BGRA_1010102>>;
  97. #if !defined(MEMORY_SANITIZER)
  98. // Fails under MSAN: crbug.com/886995
  99. INSTANTIATE_TYPED_TEST_SUITE_P(GLImageNativePixmap,
  100. GLImageTest,
  101. GLImageTestTypes);
  102. INSTANTIATE_TYPED_TEST_SUITE_P(GLImageNativePixmap,
  103. GLImageOddSizeTest,
  104. GLImageTestTypes);
  105. INSTANTIATE_TYPED_TEST_SUITE_P(GLImageNativePixmap,
  106. GLImageNativePixmapToDmabufTest,
  107. GLImageTestTypes);
  108. #endif
  109. } // namespace
  110. } // namespace gl