gl_image_shared_memory_unittest.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 <stddef.h>
  5. #include <stdint.h>
  6. #include "base/system/sys_info.h"
  7. #include "build/build_config.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. #include "ui/gl/gl_image_shared_memory.h"
  10. #include "ui/gl/test/gl_image_test_template.h"
  11. namespace gl {
  12. namespace {
  13. const uint8_t kGreen[] = {0x0, 0x20, 0x0, 0xFF};
  14. template <gfx::BufferFormat format>
  15. class GLImageSharedMemoryTestDelegate : public GLImageTestDelegateBase {
  16. public:
  17. scoped_refptr<GLImage> CreateSolidColorImage(const gfx::Size& size,
  18. const uint8_t color[4]) const {
  19. DCHECK_EQ(NumberOfPlanesForLinearBufferFormat(format), 1u);
  20. base::UnsafeSharedMemoryRegion shared_memory_region =
  21. base::UnsafeSharedMemoryRegion::Create(
  22. gfx::BufferSizeForBufferFormat(size, format));
  23. base::WritableSharedMemoryMapping shared_memory_mapping =
  24. shared_memory_region.Map();
  25. DCHECK(shared_memory_mapping.IsValid());
  26. GLImageTestSupport::SetBufferDataToColor(
  27. size.width(), size.height(),
  28. static_cast<int>(RowSizeForBufferFormat(size.width(), format, 0)), 0,
  29. format, color, static_cast<uint8_t*>(shared_memory_mapping.memory()));
  30. auto image = base::MakeRefCounted<GLImageSharedMemory>(size);
  31. bool rv = image->Initialize(
  32. shared_memory_region, gfx::GenericSharedMemoryId(0), format, 0,
  33. gfx::RowSizeForBufferFormat(size.width(), format, 0));
  34. EXPECT_TRUE(rv);
  35. return image;
  36. }
  37. unsigned GetTextureTarget() const { return GL_TEXTURE_2D; }
  38. const uint8_t* GetImageColor() { return kGreen; }
  39. int GetAdmissibleError() const { return 0; }
  40. };
  41. using GLImageTestTypes = testing::Types<
  42. GLImageSharedMemoryTestDelegate<gfx::BufferFormat::BGR_565>,
  43. GLImageSharedMemoryTestDelegate<gfx::BufferFormat::RGBX_8888>,
  44. GLImageSharedMemoryTestDelegate<gfx::BufferFormat::RGBA_8888>,
  45. GLImageSharedMemoryTestDelegate<gfx::BufferFormat::BGRX_8888>,
  46. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  47. // Fails on Win nVidia and linux android: the test writes nothing (we read
  48. // back the color used to clear the buffer).
  49. // TODO(mcasas): enable those paltforms https://crbug.com/803451.
  50. GLImageSharedMemoryTestDelegate<gfx::BufferFormat::RGBA_1010102>,
  51. // https://crbug.com/830653
  52. #if !defined(ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER) && \
  53. !defined(THREAD_SANITIZER)
  54. GLImageSharedMemoryTestDelegate<gfx::BufferFormat::BGRA_1010102>,
  55. #endif
  56. #endif
  57. GLImageSharedMemoryTestDelegate<gfx::BufferFormat::BGRA_8888>>;
  58. INSTANTIATE_TYPED_TEST_SUITE_P(GLImageSharedMemory,
  59. GLImageTest,
  60. GLImageTestTypes);
  61. INSTANTIATE_TYPED_TEST_SUITE_P(GLImageSharedMemory,
  62. GLImageOddSizeTest,
  63. GLImageTestTypes);
  64. INSTANTIATE_TYPED_TEST_SUITE_P(GLImageSharedMemory,
  65. GLImageCopyTest,
  66. GLImageTestTypes);
  67. class GLImageSharedMemoryPoolTestDelegate : public GLImageTestDelegateBase {
  68. public:
  69. scoped_refptr<GLImage> CreateSolidColorImage(const gfx::Size& size,
  70. const uint8_t color[4]) const {
  71. // Create a shared memory segment that holds an image with a stride that is
  72. // twice the row size and 2 pages larger than image.
  73. size_t stride = gfx::RowSizeForBufferFormat(
  74. size.width(), gfx::BufferFormat::RGBA_8888, 0) *
  75. 2;
  76. size_t pool_size =
  77. stride * size.height() + base::SysInfo::VMAllocationGranularity() * 3;
  78. base::UnsafeSharedMemoryRegion shared_memory_region =
  79. base::UnsafeSharedMemoryRegion::Create(pool_size);
  80. base::WritableSharedMemoryMapping shared_memory_mapping =
  81. shared_memory_region.Map();
  82. DCHECK(shared_memory_mapping.IsValid());
  83. // Initialize memory to a value that is easy to recognize if test fails.
  84. memset(shared_memory_mapping.memory(), 0x55, pool_size);
  85. // Place buffer at a non-zero non-page-aligned offset in shared memory.
  86. size_t buffer_offset = 3 * base::SysInfo::VMAllocationGranularity() / 2;
  87. GLImageTestSupport::SetBufferDataToColor(
  88. size.width(), size.height(), static_cast<int>(stride), 0,
  89. gfx::BufferFormat::RGBA_8888, color,
  90. static_cast<uint8_t*>(shared_memory_mapping.memory()) + buffer_offset);
  91. auto image = base::MakeRefCounted<GLImageSharedMemory>(size);
  92. bool rv =
  93. image->Initialize(shared_memory_region, gfx::GenericSharedMemoryId(0),
  94. gfx::BufferFormat::RGBA_8888, buffer_offset, stride);
  95. EXPECT_TRUE(rv);
  96. return image;
  97. }
  98. unsigned GetTextureTarget() const { return GL_TEXTURE_2D; }
  99. const uint8_t* GetImageColor() { return kGreen; }
  100. int GetAdmissibleError() const { return 0; }
  101. };
  102. // Disabled on Windows, see crbug.com/1036138
  103. #if !BUILDFLAG(IS_WIN)
  104. INSTANTIATE_TYPED_TEST_SUITE_P(GLImageSharedMemoryPool,
  105. GLImageCopyTest,
  106. GLImageSharedMemoryPoolTestDelegate);
  107. #endif
  108. } // namespace
  109. } // namespace gl