OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 <GLES2/gl2.h> | 5 #include <GLES2/gl2.h> |
6 #include <GLES2/gl2chromium.h> | 6 #include <GLES2/gl2chromium.h> |
7 #include <GLES2/gl2ext.h> | 7 #include <GLES2/gl2ext.h> |
8 #include <GLES2/gl2extchromium.h> | 8 #include <GLES2/gl2extchromium.h> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/process/process_handle.h" | 12 #include "base/process/process_handle.h" |
13 #include "gpu/command_buffer/client/gles2_implementation.h" | 13 #include "gpu/command_buffer/client/gles2_implementation.h" |
14 #include "gpu/command_buffer/client/gpu_memory_buffer_factory.h" | |
15 #include "gpu/command_buffer/service/command_buffer_service.h" | |
14 #include "gpu/command_buffer/service/image_manager.h" | 16 #include "gpu/command_buffer/service/image_manager.h" |
15 #include "gpu/command_buffer/tests/gl_manager.h" | 17 #include "gpu/command_buffer/tests/gl_manager.h" |
16 #include "gpu/command_buffer/tests/gl_test_utils.h" | 18 #include "gpu/command_buffer/tests/gl_test_utils.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "ui/gl/gl_image.h" | 21 #include "ui/gl/gl_image.h" |
20 | 22 |
21 using testing::_; | 23 using testing::_; |
22 using testing::IgnoreResult; | 24 using testing::IgnoreResult; |
23 using testing::InvokeWithoutArgs; | 25 using testing::InvokeWithoutArgs; |
(...skipping 20 matching lines...) Expand all Loading... | |
44 MOCK_METHOD0(Unmap, void()); | 46 MOCK_METHOD0(Unmap, void()); |
45 MOCK_CONST_METHOD0(IsMapped, bool()); | 47 MOCK_CONST_METHOD0(IsMapped, bool()); |
46 MOCK_CONST_METHOD0(GetStride, uint32()); | 48 MOCK_CONST_METHOD0(GetStride, uint32()); |
47 MOCK_CONST_METHOD0(GetHandle, gfx::GpuMemoryBufferHandle()); | 49 MOCK_CONST_METHOD0(GetHandle, gfx::GpuMemoryBufferHandle()); |
48 MOCK_METHOD0(Die, void()); | 50 MOCK_METHOD0(Die, void()); |
49 | 51 |
50 private: | 52 private: |
51 DISALLOW_COPY_AND_ASSIGN(MockGpuMemoryBuffer); | 53 DISALLOW_COPY_AND_ASSIGN(MockGpuMemoryBuffer); |
52 }; | 54 }; |
53 | 55 |
54 class MockImageFactory : public ImageFactory { | 56 class MockGpuMemoryBufferFactory : public GpuMemoryBufferFactory { |
55 public: | 57 public: |
56 MockImageFactory(ImageManager* image_manager) {} | 58 MockGpuMemoryBufferFactory() {} |
57 virtual ~MockImageFactory() {} | 59 virtual ~MockGpuMemoryBufferFactory() {} |
58 | 60 |
59 MOCK_METHOD4(CreateGpuMemoryBufferMock, gfx::GpuMemoryBuffer*( | 61 MOCK_METHOD3(CreateGpuMemoryBuffer, |
kaanb
2013/08/15 18:31:29
nit: parameter names are optional
reveman
2013/08/16 01:34:57
Removed them as that is consistent with above code
| |
60 int width, int height, GLenum internalformat, unsigned* image_id)); | 62 gfx::GpuMemoryBuffer*(size_t width, |
61 MOCK_METHOD1(DeleteGpuMemoryBuffer, void(unsigned)); | 63 size_t height, |
62 // Workaround for mocking methods that return scoped_ptrs | 64 unsigned internalformat)); |
63 virtual scoped_ptr<gfx::GpuMemoryBuffer> CreateGpuMemoryBuffer( | |
64 int width, int height, GLenum internalformat, | |
65 unsigned* image_id) OVERRIDE { | |
66 return scoped_ptr<gfx::GpuMemoryBuffer>(CreateGpuMemoryBufferMock( | |
67 width, height, internalformat, image_id)); | |
68 } | |
69 | 65 |
70 private: | 66 private: |
71 DISALLOW_COPY_AND_ASSIGN(MockImageFactory); | 67 DISALLOW_COPY_AND_ASSIGN(MockGpuMemoryBufferFactory); |
72 }; | 68 }; |
73 | 69 |
74 class MockGpuMemoryBufferTest : public testing::Test { | 70 class MockGpuMemoryBufferTest : public testing::Test { |
75 protected: | 71 protected: |
76 virtual void SetUp() { | 72 virtual void SetUp() { |
77 GLManager::Options options; | 73 GLManager::Options options; |
78 image_manager_ = new ImageManager; | 74 image_manager_ = new ImageManager; |
79 image_factory_.reset( | 75 gpu_memory_buffer_factory_.reset(new MockGpuMemoryBufferFactory); |
80 new StrictMock<MockImageFactory>(image_manager_.get())); | |
81 options.image_manager = image_manager_.get(); | 76 options.image_manager = image_manager_.get(); |
82 options.image_factory = image_factory_.get(); | 77 options.gpu_memory_buffer_factory = gpu_memory_buffer_factory_.get(); |
83 | 78 |
84 gl_.Initialize(options); | 79 gl_.Initialize(options); |
85 gl_.MakeCurrent(); | 80 gl_.MakeCurrent(); |
86 | 81 |
87 glGenTextures(2, texture_ids_); | 82 glGenTextures(2, texture_ids_); |
88 glBindTexture(GL_TEXTURE_2D, texture_ids_[1]); | 83 glBindTexture(GL_TEXTURE_2D, texture_ids_[1]); |
89 | 84 |
90 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 85 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
91 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 86 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
92 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 87 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
93 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | 88 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
94 | 89 |
95 glGenFramebuffers(1, &framebuffer_id_); | 90 glGenFramebuffers(1, &framebuffer_id_); |
96 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id_); | 91 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id_); |
97 glFramebufferTexture2D(GL_FRAMEBUFFER, | 92 glFramebufferTexture2D(GL_FRAMEBUFFER, |
98 GL_COLOR_ATTACHMENT0, | 93 GL_COLOR_ATTACHMENT0, |
99 GL_TEXTURE_2D, | 94 GL_TEXTURE_2D, |
100 texture_ids_[1], | 95 texture_ids_[1], |
101 0); | 96 0); |
102 } | 97 } |
103 | 98 |
104 virtual void TearDown() { | 99 virtual void TearDown() { |
105 glDeleteTextures(2, texture_ids_); | 100 glDeleteTextures(2, texture_ids_); |
106 glDeleteFramebuffers(1, &framebuffer_id_); | 101 glDeleteFramebuffers(1, &framebuffer_id_); |
107 | 102 |
108 gl_.Destroy(); | 103 gl_.Destroy(); |
109 } | 104 } |
110 | 105 |
111 scoped_ptr<StrictMock<MockImageFactory> > image_factory_; | |
112 scoped_refptr<ImageManager> image_manager_; | 106 scoped_refptr<ImageManager> image_manager_; |
107 scoped_ptr<MockGpuMemoryBufferFactory> gpu_memory_buffer_factory_; | |
113 GLManager gl_; | 108 GLManager gl_; |
114 GLuint texture_ids_[2]; | 109 GLuint texture_ids_[2]; |
115 GLuint framebuffer_id_; | 110 GLuint framebuffer_id_; |
116 }; | 111 }; |
117 | 112 |
118 // An end to end test that tests the whole GpuMemoryBuffer lifecycle. | 113 // An end to end test that tests the whole GpuMemoryBuffer lifecycle. |
119 TEST_F(MockGpuMemoryBufferTest, Lifecycle) { | 114 TEST_F(MockGpuMemoryBufferTest, Lifecycle) { |
120 size_t bytes = kImageWidth * kImageHeight * kImageBytesPerPixel; | 115 size_t bytes = kImageWidth * kImageHeight * kImageBytesPerPixel; |
121 uint8 pixels[1 * 4] = { 255u, 0u, 0u, 255u }; | 116 uint8 pixels[1 * 4] = { 255u, 0u, 0u, 255u }; |
122 | 117 |
123 // Buffer is owned and freed by GpuMemoryBufferTracker. | 118 // Buffer is owned and freed by GpuMemoryBufferTracker. |
124 StrictMock<MockGpuMemoryBuffer>* gpu_memory_buffer = | 119 StrictMock<MockGpuMemoryBuffer>* gpu_memory_buffer = |
125 new StrictMock<MockGpuMemoryBuffer>(kImageWidth, kImageHeight); | 120 new StrictMock<MockGpuMemoryBuffer>(kImageWidth, kImageHeight); |
126 base::SharedMemory shared_memory; | 121 base::SharedMemory shared_memory; |
127 shared_memory.CreateAnonymous(bytes); | 122 shared_memory.CreateAnonymous(bytes); |
128 | 123 |
129 base::SharedMemoryHandle duped_shared_memory_handle; | 124 base::SharedMemoryHandle duped_shared_memory_handle; |
130 shared_memory.ShareToProcess(base::GetCurrentProcessHandle(), | 125 shared_memory.ShareToProcess(base::GetCurrentProcessHandle(), |
131 &duped_shared_memory_handle); | 126 &duped_shared_memory_handle); |
132 gfx::GpuMemoryBufferHandle handle; | 127 gfx::GpuMemoryBufferHandle handle; |
133 handle.type = gfx::SHARED_MEMORY_BUFFER; | 128 handle.type = gfx::SHARED_MEMORY_BUFFER; |
134 handle.handle = duped_shared_memory_handle; | 129 handle.handle = duped_shared_memory_handle; |
135 | 130 |
136 const GLuint kImageId = 345u; | 131 EXPECT_CALL(*gpu_memory_buffer_factory_.get(), CreateGpuMemoryBuffer( |
137 | 132 kImageWidth, kImageHeight, GL_RGBA8_OES)) |
138 EXPECT_CALL(*image_factory_.get(), CreateGpuMemoryBufferMock( | |
139 kImageWidth, kImageHeight, GL_RGBA8_OES, _)) | |
140 .Times(1) | 133 .Times(1) |
141 .WillOnce(DoAll(SetArgPointee<3>(kImageId), Return(gpu_memory_buffer))) | 134 .WillOnce(Return(gpu_memory_buffer)) |
142 .RetiresOnSaturation(); | 135 .RetiresOnSaturation(); |
143 | |
144 // Create the GLImage and insert it into the ImageManager, which | |
145 // would be done within CreateGpuMemoryBufferMock if it weren't a mock. | |
146 GLuint image_id = glCreateImageCHROMIUM( | |
147 kImageWidth, kImageHeight, GL_RGBA8_OES); | |
148 EXPECT_EQ(kImageId, image_id); | |
149 | |
150 EXPECT_CALL(*gpu_memory_buffer, GetHandle()) | 136 EXPECT_CALL(*gpu_memory_buffer, GetHandle()) |
137 .Times(1) | |
151 .WillOnce(Return(handle)) | 138 .WillOnce(Return(handle)) |
152 .RetiresOnSaturation(); | 139 .RetiresOnSaturation(); |
153 | 140 |
154 gfx::Size size(kImageWidth, kImageHeight); | 141 // Create the image. This should add the image ID to the ImageManager. |
155 scoped_refptr<gfx::GLImage> gl_image( | 142 GLuint image_id = glCreateImageCHROMIUM( |
156 gfx::GLImage::CreateGLImageForGpuMemoryBuffer( | 143 kImageWidth, kImageHeight, GL_RGBA8_OES); |
157 gpu_memory_buffer->GetHandle(), size)); | 144 EXPECT_NE(0u, image_id); |
158 image_manager_->AddImage(gl_image.get(), image_id); | 145 EXPECT_TRUE(image_manager_->LookupImage(image_id) != NULL); |
159 | 146 |
160 EXPECT_CALL(*gpu_memory_buffer, IsMapped()) | 147 EXPECT_CALL(*gpu_memory_buffer, IsMapped()) |
161 .WillOnce(Return(false)) | 148 .WillOnce(Return(false)) |
162 .RetiresOnSaturation(); | 149 .RetiresOnSaturation(); |
163 | 150 |
164 shared_memory.Map(bytes); | 151 shared_memory.Map(bytes); |
165 EXPECT_TRUE(shared_memory.memory()); | 152 EXPECT_TRUE(shared_memory.memory()); |
166 | 153 |
167 EXPECT_CALL(*gpu_memory_buffer, Map(_, _)) | 154 EXPECT_CALL(*gpu_memory_buffer, Map(_, _)) |
168 .Times(1) | 155 .Times(1) |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
210 GLTestHelper::CheckPixels(0, 0, kImageWidth, kImageHeight, 0, pixels); | 197 GLTestHelper::CheckPixels(0, 0, kImageWidth, kImageHeight, 0, pixels); |
211 EXPECT_TRUE(GL_NO_ERROR == glGetError()); | 198 EXPECT_TRUE(GL_NO_ERROR == glGetError()); |
212 | 199 |
213 // Release the image. | 200 // Release the image. |
214 glReleaseTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id); | 201 glReleaseTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id); |
215 | 202 |
216 // Destroy the image. | 203 // Destroy the image. |
217 EXPECT_CALL(*gpu_memory_buffer, Die()) | 204 EXPECT_CALL(*gpu_memory_buffer, Die()) |
218 .Times(1) | 205 .Times(1) |
219 .RetiresOnSaturation(); | 206 .RetiresOnSaturation(); |
220 | |
221 EXPECT_CALL(*image_factory_.get(), DeleteGpuMemoryBuffer(image_id)) | |
222 .Times(1) | |
223 .RetiresOnSaturation(); | |
224 | |
225 glDestroyImageCHROMIUM(image_id); | 207 glDestroyImageCHROMIUM(image_id); |
226 } | 208 } |
227 | 209 |
228 } // namespace gles2 | 210 } // namespace gles2 |
229 } // namespace gpu | 211 } // namespace gpu |
OLD | NEW |