OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/shared_memory.h" | 5 #include "base/memory/shared_memory.h" |
| 6 #include "base/sys_info.h" |
6 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
7 #include "ui/gl/gl_image_shared_memory.h" | 8 #include "ui/gl/gl_image_shared_memory.h" |
8 #include "ui/gl/test/gl_image_test_template.h" | 9 #include "ui/gl/test/gl_image_test_template.h" |
9 | 10 |
10 namespace gfx { | 11 namespace gfx { |
11 namespace { | 12 namespace { |
12 | 13 |
13 template <BufferFormat format> | 14 template <BufferFormat format> |
14 class GLImageSharedMemoryTestDelegate { | 15 class GLImageSharedMemoryTestDelegate { |
15 public: | 16 public: |
16 scoped_refptr<GLImage> CreateSolidColorImage(const Size& size, | 17 scoped_refptr<GLImage> CreateSolidColorImage(const Size& size, |
17 const uint8_t color[4]) const { | 18 const uint8_t color[4]) const { |
18 DCHECK_EQ(NumberOfPlanesForBufferFormat(format), 1u); | 19 DCHECK_EQ(NumberOfPlanesForBufferFormat(format), 1u); |
19 base::SharedMemory shared_memory; | 20 base::SharedMemory shared_memory; |
20 bool rv = shared_memory.CreateAndMapAnonymous( | 21 bool rv = shared_memory.CreateAndMapAnonymous( |
21 BufferSizeForBufferFormat(size, format)); | 22 BufferSizeForBufferFormat(size, format)); |
22 DCHECK(rv); | 23 DCHECK(rv); |
23 GLImageTestSupport::SetBufferDataToColor( | 24 GLImageTestSupport::SetBufferDataToColor( |
24 size.width(), size.height(), | 25 size.width(), size.height(), |
25 static_cast<int>(RowSizeForBufferFormat(size.width(), format, 0)), | 26 static_cast<int>(RowSizeForBufferFormat(size.width(), format, 0)), |
26 format, color, reinterpret_cast<uint8_t*>(shared_memory.memory())); | 27 format, color, reinterpret_cast<uint8_t*>(shared_memory.memory())); |
27 scoped_refptr<GLImageSharedMemory> image(new GLImageSharedMemory( | 28 scoped_refptr<GLImageSharedMemory> image(new GLImageSharedMemory( |
28 size, GLImageMemory::GetInternalFormatForTesting(format))); | 29 size, GLImageMemory::GetInternalFormatForTesting(format))); |
29 rv = image->Initialize( | 30 rv = image->Initialize( |
30 base::SharedMemory::DuplicateHandle(shared_memory.handle()), | 31 base::SharedMemory::DuplicateHandle(shared_memory.handle()), |
31 GenericSharedMemoryId(0), format); | 32 GenericSharedMemoryId(0), format, 0); |
32 EXPECT_TRUE(rv); | 33 EXPECT_TRUE(rv); |
33 return image; | 34 return image; |
34 } | 35 } |
35 }; | 36 }; |
36 | 37 |
37 using GLImageTestTypes = | 38 using GLImageTestTypes = |
38 testing::Types<GLImageSharedMemoryTestDelegate<BufferFormat::RGBX_8888>, | 39 testing::Types<GLImageSharedMemoryTestDelegate<BufferFormat::RGBX_8888>, |
39 GLImageSharedMemoryTestDelegate<BufferFormat::RGBA_8888>, | 40 GLImageSharedMemoryTestDelegate<BufferFormat::RGBA_8888>, |
40 GLImageSharedMemoryTestDelegate<BufferFormat::BGRX_8888>, | 41 GLImageSharedMemoryTestDelegate<BufferFormat::BGRX_8888>, |
41 GLImageSharedMemoryTestDelegate<BufferFormat::BGRA_8888>>; | 42 GLImageSharedMemoryTestDelegate<BufferFormat::BGRA_8888>>; |
42 | 43 |
43 INSTANTIATE_TYPED_TEST_CASE_P(GLImageSharedMemory, | 44 INSTANTIATE_TYPED_TEST_CASE_P(GLImageSharedMemory, |
44 GLImageTest, | 45 GLImageTest, |
45 GLImageTestTypes); | 46 GLImageTestTypes); |
46 | 47 |
47 INSTANTIATE_TYPED_TEST_CASE_P(GLImageSharedMemory, | 48 INSTANTIATE_TYPED_TEST_CASE_P(GLImageSharedMemory, |
48 GLImageCopyTest, | 49 GLImageCopyTest, |
49 GLImageTestTypes); | 50 GLImageTestTypes); |
50 | 51 |
| 52 class GLImageSharedMemoryPoolTestDelegate { |
| 53 public: |
| 54 scoped_refptr<GLImage> CreateSolidColorImage(const Size& size, |
| 55 const uint8_t color[4]) const { |
| 56 // Create a shared memory segment that is 2 pages larger than image. |
| 57 size_t pool_size = |
| 58 BufferSizeForBufferFormat(size, BufferFormat::RGBA_8888) + |
| 59 base::SysInfo::VMAllocationGranularity() * 3; |
| 60 base::SharedMemory shared_memory; |
| 61 bool rv = shared_memory.CreateAndMapAnonymous(pool_size); |
| 62 DCHECK(rv); |
| 63 // Initialize memory to a value that is easy to recognize if test fails. |
| 64 memset(shared_memory.memory(), 0x55, pool_size); |
| 65 // Place buffer at a non-zero non-page-aligned offset in shared memory. |
| 66 size_t buffer_offset = 3 * base::SysInfo::VMAllocationGranularity() / 2; |
| 67 GLImageTestSupport::SetBufferDataToColor( |
| 68 size.width(), size.height(), |
| 69 static_cast<int>( |
| 70 RowSizeForBufferFormat(size.width(), BufferFormat::RGBA_8888, 0)), |
| 71 BufferFormat::RGBA_8888, color, |
| 72 reinterpret_cast<uint8_t*>(shared_memory.memory()) + buffer_offset); |
| 73 scoped_refptr<GLImageSharedMemory> image( |
| 74 new GLImageSharedMemory(size, GL_RGBA)); |
| 75 rv = image->Initialize( |
| 76 base::SharedMemory::DuplicateHandle(shared_memory.handle()), |
| 77 GenericSharedMemoryId(0), BufferFormat::RGBA_8888, buffer_offset); |
| 78 EXPECT_TRUE(rv); |
| 79 return image; |
| 80 } |
| 81 }; |
| 82 |
| 83 INSTANTIATE_TYPED_TEST_CASE_P(GLImageSharedMemoryPool, |
| 84 GLImageCopyTest, |
| 85 GLImageSharedMemoryPoolTestDelegate); |
| 86 |
51 } // namespace | 87 } // namespace |
52 } // namespace gfx | 88 } // namespace gfx |
OLD | NEW |