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