gl_image_io_surface_unittest.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2015 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_io_surface.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include "build/build_config.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. #include "ui/gfx/buffer_format_util.h"
  10. #include "ui/gfx/mac/io_surface.h"
  11. #include "ui/gl/test/gl_image_bind_test_template.h"
  12. #include "ui/gl/test/gl_image_test_template.h"
  13. #include "ui/gl/test/gl_image_zero_initialize_test_template.h"
  14. namespace gl {
  15. namespace {
  16. const uint8_t kImageColor[] = {0x30, 0x40, 0x10, 0xFF};
  17. template <gfx::BufferFormat format>
  18. class GLImageIOSurfaceTestDelegate : public GLImageTestDelegateBase {
  19. public:
  20. scoped_refptr<GLImage> CreateImage(const gfx::Size& size) const {
  21. scoped_refptr<GLImageIOSurface> image(GLImageIOSurface::Create(
  22. size, GLImageIOSurface::GetInternalFormatForTesting(format)));
  23. IOSurfaceRef surface_ref = gfx::CreateIOSurface(size, format);
  24. const uint32_t surface_plane = 0;
  25. bool rv = image->Initialize(surface_ref, surface_plane,
  26. gfx::GenericSharedMemoryId(1), format);
  27. EXPECT_TRUE(rv);
  28. return image;
  29. }
  30. scoped_refptr<GLImage> CreateSolidColorImage(const gfx::Size& size,
  31. const uint8_t color[4]) const {
  32. scoped_refptr<GLImageIOSurface> image(GLImageIOSurface::Create(
  33. size, GLImageIOSurface::GetInternalFormatForTesting(format)));
  34. IOSurfaceRef surface_ref = gfx::CreateIOSurface(size, format);
  35. const uint32_t surface_plane = 0;
  36. IOReturn status = IOSurfaceLock(surface_ref, 0, nullptr);
  37. EXPECT_NE(status, kIOReturnCannotLock);
  38. uint8_t corrected_color[4];
  39. if (format == gfx::BufferFormat::RGBA_8888) {
  40. // GL_RGBA is not supported by CGLTexImageIOSurface2D(), so we pretend it
  41. // is GL_BGRA, (see https://crbug.com/533677#c6) swizzle the channels for
  42. // the purpose of this test.
  43. corrected_color[0] = color[2];
  44. corrected_color[1] = color[1];
  45. corrected_color[2] = color[0];
  46. corrected_color[3] = color[3];
  47. } else {
  48. memcpy(corrected_color, color, std::size(corrected_color));
  49. }
  50. for (size_t plane = 0; plane < NumberOfPlanesForLinearBufferFormat(format);
  51. ++plane) {
  52. void* data = IOSurfaceGetBaseAddressOfPlane(surface_ref, plane);
  53. GLImageTestSupport::SetBufferDataToColor(
  54. size.width(), size.height(),
  55. IOSurfaceGetBytesPerRowOfPlane(surface_ref, plane), plane, format,
  56. corrected_color, static_cast<uint8_t*>(data));
  57. }
  58. IOSurfaceUnlock(surface_ref, 0, nullptr);
  59. bool rv = image->Initialize(surface_ref, surface_plane,
  60. gfx::GenericSharedMemoryId(1), format);
  61. EXPECT_TRUE(rv);
  62. return image;
  63. }
  64. unsigned GetTextureTarget() const { return GL_TEXTURE_RECTANGLE_ARB; }
  65. const uint8_t* GetImageColor() {
  66. if (format != gfx::BufferFormat::BGRX_8888)
  67. return kImageColor;
  68. // BGRX_8888 is actually treated as BGRA because many operations are broken
  69. // when binding an IOSurface as GL_RGB, see https://crbug.com/595948. This
  70. // makes the alpha value comparison fail, because we expect 0xFF but are
  71. // actually writing something (0xAA). Correct the alpha value for the test.
  72. static uint8_t bgrx_image_color[] = {kImageColor[0], kImageColor[1],
  73. kImageColor[2], 0xAA};
  74. return bgrx_image_color;
  75. }
  76. int GetAdmissibleError() const {
  77. return format == gfx::BufferFormat::YUV_420_BIPLANAR ? 1 : 0;
  78. }
  79. };
  80. using GLImageTestTypes = testing::Types<
  81. GLImageIOSurfaceTestDelegate<gfx::BufferFormat::RGBA_8888>,
  82. GLImageIOSurfaceTestDelegate<gfx::BufferFormat::BGRA_8888>,
  83. GLImageIOSurfaceTestDelegate<gfx::BufferFormat::BGRX_8888>,
  84. GLImageIOSurfaceTestDelegate<gfx::BufferFormat::RGBA_F16>,
  85. GLImageIOSurfaceTestDelegate<gfx::BufferFormat::YUV_420_BIPLANAR>,
  86. GLImageIOSurfaceTestDelegate<gfx::BufferFormat::BGRA_1010102>>;
  87. INSTANTIATE_TYPED_TEST_SUITE_P(GLImageIOSurface, GLImageTest, GLImageTestTypes);
  88. using GLImageRGBTestTypes = testing::Types<
  89. GLImageIOSurfaceTestDelegate<gfx::BufferFormat::RGBA_8888>,
  90. GLImageIOSurfaceTestDelegate<gfx::BufferFormat::BGRA_8888>,
  91. GLImageIOSurfaceTestDelegate<gfx::BufferFormat::BGRX_8888>,
  92. GLImageIOSurfaceTestDelegate<gfx::BufferFormat::RGBA_F16>,
  93. GLImageIOSurfaceTestDelegate<gfx::BufferFormat::BGRA_1010102>>;
  94. INSTANTIATE_TYPED_TEST_SUITE_P(GLImageIOSurface,
  95. GLImageZeroInitializeTest,
  96. GLImageRGBTestTypes);
  97. using GLImageBindTestTypes = testing::Types<
  98. GLImageIOSurfaceTestDelegate<gfx::BufferFormat::BGRA_8888>,
  99. GLImageIOSurfaceTestDelegate<gfx::BufferFormat::RGBA_8888>,
  100. GLImageIOSurfaceTestDelegate<gfx::BufferFormat::BGRX_8888>,
  101. GLImageIOSurfaceTestDelegate<gfx::BufferFormat::RGBA_F16>,
  102. GLImageIOSurfaceTestDelegate<gfx::BufferFormat::BGRA_1010102>>;
  103. INSTANTIATE_TYPED_TEST_SUITE_P(GLImageIOSurface,
  104. GLImageBindTest,
  105. GLImageBindTestTypes);
  106. INSTANTIATE_TYPED_TEST_SUITE_P(
  107. GLImageIOSurface,
  108. GLImageCopyTest,
  109. GLImageIOSurfaceTestDelegate<gfx::BufferFormat::YUV_420_BIPLANAR>);
  110. } // namespace
  111. } // namespace gl