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 "base/sys_info.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "ui/gl/gl_image_shared_memory.h" | 8 #include "ui/gl/gl_image_shared_memory.h" |
| 9 #include "ui/gl/gl_utils.h" |
9 #include "ui/gl/test/gl_image_test_template.h" | 10 #include "ui/gl/test/gl_image_test_template.h" |
10 | 11 |
11 namespace gl { | 12 namespace gl { |
12 namespace { | 13 namespace { |
13 | 14 |
14 template <gfx::BufferFormat format> | |
15 class GLImageSharedMemoryTestDelegate { | 15 class GLImageSharedMemoryTestDelegate { |
16 public: | 16 public: |
17 scoped_refptr<gl::GLImage> CreateSolidColorImage( | 17 scoped_refptr<gl::GLImage> CreateSolidColorImage( |
18 const gfx::Size& size, | 18 const gfx::Size& size, |
| 19 gfx::BufferFormat format, |
19 const uint8_t color[4]) const { | 20 const uint8_t color[4]) const { |
20 DCHECK_EQ(NumberOfPlanesForBufferFormat(format), 1u); | 21 DCHECK_EQ(NumberOfPlanesForBufferFormat(format), 1u); |
21 base::SharedMemory shared_memory; | 22 base::SharedMemory shared_memory; |
22 bool rv = shared_memory.CreateAndMapAnonymous( | 23 bool rv = shared_memory.CreateAndMapAnonymous( |
23 gfx::BufferSizeForBufferFormat(size, format)); | 24 gfx::BufferSizeForBufferFormat(size, format)); |
24 DCHECK(rv); | 25 DCHECK(rv); |
25 GLImageTestSupport::SetBufferDataToColor( | 26 GLImageTestSupport::SetBufferDataToColor( |
26 size.width(), size.height(), | 27 size.width(), size.height(), |
27 static_cast<int>(RowSizeForBufferFormat(size.width(), format, 0)), 0, | 28 static_cast<int>(RowSizeForBufferFormat(size.width(), format, 0)), 0, |
28 format, color, reinterpret_cast<uint8_t*>(shared_memory.memory())); | 29 format, color, reinterpret_cast<uint8_t*>(shared_memory.memory())); |
29 scoped_refptr<gl::GLImageSharedMemory> image(new gl::GLImageSharedMemory( | 30 scoped_refptr<gl::GLImageSharedMemory> image( |
30 size, gl::GLImageMemory::GetInternalFormatForTesting(format))); | 31 new gl::GLImageSharedMemory(size, gl::GetTextureFormatFrom(format))); |
31 rv = image->Initialize( | 32 rv = image->Initialize( |
32 base::SharedMemory::DuplicateHandle(shared_memory.handle()), | 33 base::SharedMemory::DuplicateHandle(shared_memory.handle()), |
33 gfx::GenericSharedMemoryId(0), format, 0, | 34 gfx::GenericSharedMemoryId(0), format, 0, |
34 gfx::RowSizeForBufferFormat(size.width(), format, 0)); | 35 gfx::RowSizeForBufferFormat(size.width(), format, 0)); |
35 EXPECT_TRUE(rv); | 36 EXPECT_TRUE(rv); |
36 return image; | 37 return image; |
37 } | 38 } |
| 39 |
| 40 static bool IsSupported(gfx::BufferFormat format) { |
| 41 switch (format) { |
| 42 case gfx::BufferFormat::RGBX_8888: |
| 43 case gfx::BufferFormat::RGBA_8888: |
| 44 case gfx::BufferFormat::BGRX_8888: |
| 45 case gfx::BufferFormat::BGRA_8888: |
| 46 return true; |
| 47 default: |
| 48 return false; |
| 49 } |
| 50 return false; |
| 51 } |
38 }; | 52 }; |
39 | 53 |
40 using GLImageTestTypes = testing::Types< | |
41 GLImageSharedMemoryTestDelegate<gfx::BufferFormat::RGBX_8888>, | |
42 GLImageSharedMemoryTestDelegate<gfx::BufferFormat::RGBA_8888>, | |
43 GLImageSharedMemoryTestDelegate<gfx::BufferFormat::BGRX_8888>, | |
44 GLImageSharedMemoryTestDelegate<gfx::BufferFormat::BGRA_8888>>; | |
45 | |
46 INSTANTIATE_TYPED_TEST_CASE_P(GLImageSharedMemory, | 54 INSTANTIATE_TYPED_TEST_CASE_P(GLImageSharedMemory, |
47 GLImageTest, | 55 GLImageTest, |
48 GLImageTestTypes); | 56 GLImageSharedMemoryTestDelegate); |
49 | 57 |
50 INSTANTIATE_TYPED_TEST_CASE_P(GLImageSharedMemory, | 58 INSTANTIATE_TYPED_TEST_CASE_P(GLImageSharedMemory, |
51 GLImageCopyTest, | 59 GLImageCopyTest, |
52 GLImageTestTypes); | 60 GLImageSharedMemoryTestDelegate); |
53 | 61 |
54 class GLImageSharedMemoryPoolTestDelegate { | 62 class GLImageSharedMemoryPoolTestDelegate { |
55 public: | 63 public: |
56 scoped_refptr<gl::GLImage> CreateSolidColorImage( | 64 scoped_refptr<gl::GLImage> CreateSolidColorImage( |
57 const gfx::Size& size, | 65 const gfx::Size& size, |
| 66 gfx::BufferFormat format, |
58 const uint8_t color[4]) const { | 67 const uint8_t color[4]) const { |
59 // Create a shared memory segment that holds an image with a stride that is | 68 // Create a shared memory segment that holds an image with a stride that is |
60 // twice the row size and 2 pages larger than image. | 69 // twice the row size and 2 pages larger than image. |
61 size_t stride = gfx::RowSizeForBufferFormat( | 70 size_t stride = gfx::RowSizeForBufferFormat(size.width(), format, 0) * 2; |
62 size.width(), gfx::BufferFormat::RGBA_8888, 0) * | |
63 2; | |
64 size_t pool_size = | 71 size_t pool_size = |
65 stride * size.height() + base::SysInfo::VMAllocationGranularity() * 3; | 72 stride * size.height() + base::SysInfo::VMAllocationGranularity() * 3; |
66 base::SharedMemory shared_memory; | 73 base::SharedMemory shared_memory; |
67 bool rv = shared_memory.CreateAndMapAnonymous(pool_size); | 74 bool rv = shared_memory.CreateAndMapAnonymous(pool_size); |
68 DCHECK(rv); | 75 DCHECK(rv); |
69 // Initialize memory to a value that is easy to recognize if test fails. | 76 // Initialize memory to a value that is easy to recognize if test fails. |
70 memset(shared_memory.memory(), 0x55, pool_size); | 77 memset(shared_memory.memory(), 0x55, pool_size); |
71 // Place buffer at a non-zero non-page-aligned offset in shared memory. | 78 // Place buffer at a non-zero non-page-aligned offset in shared memory. |
72 size_t buffer_offset = 3 * base::SysInfo::VMAllocationGranularity() / 2; | 79 size_t buffer_offset = 3 * base::SysInfo::VMAllocationGranularity() / 2; |
73 GLImageTestSupport::SetBufferDataToColor( | 80 GLImageTestSupport::SetBufferDataToColor( |
74 size.width(), size.height(), static_cast<int>(stride), 0, | 81 size.width(), size.height(), static_cast<int>(stride), 0, format, color, |
75 gfx::BufferFormat::RGBA_8888, color, | |
76 reinterpret_cast<uint8_t*>(shared_memory.memory()) + buffer_offset); | 82 reinterpret_cast<uint8_t*>(shared_memory.memory()) + buffer_offset); |
77 scoped_refptr<gl::GLImageSharedMemory> image( | 83 scoped_refptr<gl::GLImageSharedMemory> image( |
78 new gl::GLImageSharedMemory(size, GL_RGBA)); | 84 new gl::GLImageSharedMemory(size, gl::GetTextureFormatFrom(format))); |
79 rv = image->Initialize( | 85 rv = image->Initialize( |
80 base::SharedMemory::DuplicateHandle(shared_memory.handle()), | 86 base::SharedMemory::DuplicateHandle(shared_memory.handle()), |
81 gfx::GenericSharedMemoryId(0), gfx::BufferFormat::RGBA_8888, | 87 gfx::GenericSharedMemoryId(0), format, buffer_offset, stride); |
82 buffer_offset, stride); | |
83 EXPECT_TRUE(rv); | 88 EXPECT_TRUE(rv); |
84 return image; | 89 return image; |
85 } | 90 } |
| 91 |
| 92 static bool IsSupported(gfx::BufferFormat format) { |
| 93 switch (format) { |
| 94 case gfx::BufferFormat::RGBA_8888: |
| 95 return true; |
| 96 default: |
| 97 return false; |
| 98 } |
| 99 return false; |
| 100 } |
86 }; | 101 }; |
87 | 102 |
88 INSTANTIATE_TYPED_TEST_CASE_P(GLImageSharedMemoryPool, | 103 INSTANTIATE_TYPED_TEST_CASE_P(GLImageSharedMemoryPool, |
89 GLImageCopyTest, | 104 GLImageCopyTest, |
90 GLImageSharedMemoryPoolTestDelegate); | 105 GLImageSharedMemoryPoolTestDelegate); |
91 | 106 |
92 } // namespace | 107 } // namespace |
93 } // namespace gl | 108 } // namespace gl |
OLD | NEW |