| 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, |
| 60 int width, int height, GLenum internalformat, unsigned* image_id)); | 62 gfx::GpuMemoryBuffer*(size_t, size_t, unsigned)); |
| 61 MOCK_METHOD1(DeleteGpuMemoryBuffer, void(unsigned)); | |
| 62 // Workaround for mocking methods that return scoped_ptrs | |
| 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 | 63 |
| 70 private: | 64 private: |
| 71 DISALLOW_COPY_AND_ASSIGN(MockImageFactory); | 65 DISALLOW_COPY_AND_ASSIGN(MockGpuMemoryBufferFactory); |
| 72 }; | 66 }; |
| 73 | 67 |
| 74 class MockGpuMemoryBufferTest : public testing::Test { | 68 class MockGpuMemoryBufferTest : public testing::Test { |
| 75 protected: | 69 protected: |
| 76 virtual void SetUp() { | 70 virtual void SetUp() { |
| 77 GLManager::Options options; | 71 GLManager::Options options; |
| 78 image_manager_ = new ImageManager; | 72 image_manager_ = new ImageManager; |
| 79 image_factory_.reset( | 73 gpu_memory_buffer_factory_.reset(new MockGpuMemoryBufferFactory); |
| 80 new StrictMock<MockImageFactory>(image_manager_.get())); | |
| 81 options.image_manager = image_manager_.get(); | 74 options.image_manager = image_manager_.get(); |
| 82 options.image_factory = image_factory_.get(); | 75 options.gpu_memory_buffer_factory = gpu_memory_buffer_factory_.get(); |
| 83 | 76 |
| 84 gl_.Initialize(options); | 77 gl_.Initialize(options); |
| 85 gl_.MakeCurrent(); | 78 gl_.MakeCurrent(); |
| 86 | 79 |
| 87 glGenTextures(2, texture_ids_); | 80 glGenTextures(2, texture_ids_); |
| 88 glBindTexture(GL_TEXTURE_2D, texture_ids_[1]); | 81 glBindTexture(GL_TEXTURE_2D, texture_ids_[1]); |
| 89 | 82 |
| 90 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 83 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); | 84 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 92 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 85 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 93 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | 86 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 94 | 87 |
| 95 glGenFramebuffers(1, &framebuffer_id_); | 88 glGenFramebuffers(1, &framebuffer_id_); |
| 96 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id_); | 89 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id_); |
| 97 glFramebufferTexture2D(GL_FRAMEBUFFER, | 90 glFramebufferTexture2D(GL_FRAMEBUFFER, |
| 98 GL_COLOR_ATTACHMENT0, | 91 GL_COLOR_ATTACHMENT0, |
| 99 GL_TEXTURE_2D, | 92 GL_TEXTURE_2D, |
| 100 texture_ids_[1], | 93 texture_ids_[1], |
| 101 0); | 94 0); |
| 102 } | 95 } |
| 103 | 96 |
| 104 virtual void TearDown() { | 97 virtual void TearDown() { |
| 105 glDeleteTextures(2, texture_ids_); | 98 glDeleteTextures(2, texture_ids_); |
| 106 glDeleteFramebuffers(1, &framebuffer_id_); | 99 glDeleteFramebuffers(1, &framebuffer_id_); |
| 107 | 100 |
| 108 gl_.Destroy(); | 101 gl_.Destroy(); |
| 109 } | 102 } |
| 110 | 103 |
| 111 scoped_ptr<StrictMock<MockImageFactory> > image_factory_; | |
| 112 scoped_refptr<ImageManager> image_manager_; | 104 scoped_refptr<ImageManager> image_manager_; |
| 105 scoped_ptr<MockGpuMemoryBufferFactory> gpu_memory_buffer_factory_; |
| 113 GLManager gl_; | 106 GLManager gl_; |
| 114 GLuint texture_ids_[2]; | 107 GLuint texture_ids_[2]; |
| 115 GLuint framebuffer_id_; | 108 GLuint framebuffer_id_; |
| 116 }; | 109 }; |
| 117 | 110 |
| 118 // An end to end test that tests the whole GpuMemoryBuffer lifecycle. | 111 // An end to end test that tests the whole GpuMemoryBuffer lifecycle. |
| 119 TEST_F(MockGpuMemoryBufferTest, Lifecycle) { | 112 TEST_F(MockGpuMemoryBufferTest, Lifecycle) { |
| 120 size_t bytes = kImageWidth * kImageHeight * kImageBytesPerPixel; | 113 size_t bytes = kImageWidth * kImageHeight * kImageBytesPerPixel; |
| 121 uint8 pixels[1 * 4] = { 255u, 0u, 0u, 255u }; | 114 uint8 pixels[1 * 4] = { 255u, 0u, 0u, 255u }; |
| 122 | 115 |
| 123 // Buffer is owned and freed by GpuMemoryBufferTracker. | 116 // Buffer is owned and freed by GpuMemoryBufferTracker. |
| 124 StrictMock<MockGpuMemoryBuffer>* gpu_memory_buffer = | 117 StrictMock<MockGpuMemoryBuffer>* gpu_memory_buffer = |
| 125 new StrictMock<MockGpuMemoryBuffer>(kImageWidth, kImageHeight); | 118 new StrictMock<MockGpuMemoryBuffer>(kImageWidth, kImageHeight); |
| 126 base::SharedMemory shared_memory; | 119 base::SharedMemory shared_memory; |
| 127 shared_memory.CreateAnonymous(bytes); | 120 shared_memory.CreateAnonymous(bytes); |
| 128 | 121 |
| 129 base::SharedMemoryHandle duped_shared_memory_handle; | 122 base::SharedMemoryHandle duped_shared_memory_handle; |
| 130 shared_memory.ShareToProcess(base::GetCurrentProcessHandle(), | 123 shared_memory.ShareToProcess(base::GetCurrentProcessHandle(), |
| 131 &duped_shared_memory_handle); | 124 &duped_shared_memory_handle); |
| 132 gfx::GpuMemoryBufferHandle handle; | 125 gfx::GpuMemoryBufferHandle handle; |
| 133 handle.type = gfx::SHARED_MEMORY_BUFFER; | 126 handle.type = gfx::SHARED_MEMORY_BUFFER; |
| 134 handle.handle = duped_shared_memory_handle; | 127 handle.handle = duped_shared_memory_handle; |
| 135 | 128 |
| 136 const GLuint kImageId = 345u; | 129 EXPECT_CALL(*gpu_memory_buffer_factory_.get(), CreateGpuMemoryBuffer( |
| 137 | 130 kImageWidth, kImageHeight, GL_RGBA8_OES)) |
| 138 EXPECT_CALL(*image_factory_.get(), CreateGpuMemoryBufferMock( | |
| 139 kImageWidth, kImageHeight, GL_RGBA8_OES, _)) | |
| 140 .Times(1) | 131 .Times(1) |
| 141 .WillOnce(DoAll(SetArgPointee<3>(kImageId), Return(gpu_memory_buffer))) | 132 .WillOnce(Return(gpu_memory_buffer)) |
| 142 .RetiresOnSaturation(); | 133 .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()) | 134 EXPECT_CALL(*gpu_memory_buffer, GetHandle()) |
| 135 .Times(1) |
| 151 .WillOnce(Return(handle)) | 136 .WillOnce(Return(handle)) |
| 152 .RetiresOnSaturation(); | 137 .RetiresOnSaturation(); |
| 153 | 138 |
| 154 gfx::Size size(kImageWidth, kImageHeight); | 139 // Create the image. This should add the image ID to the ImageManager. |
| 155 scoped_refptr<gfx::GLImage> gl_image( | 140 GLuint image_id = glCreateImageCHROMIUM( |
| 156 gfx::GLImage::CreateGLImageForGpuMemoryBuffer( | 141 kImageWidth, kImageHeight, GL_RGBA8_OES); |
| 157 gpu_memory_buffer->GetHandle(), size)); | 142 EXPECT_NE(0u, image_id); |
| 158 image_manager_->AddImage(gl_image.get(), image_id); | 143 EXPECT_TRUE(image_manager_->LookupImage(image_id) != NULL); |
| 159 | 144 |
| 160 EXPECT_CALL(*gpu_memory_buffer, IsMapped()) | 145 EXPECT_CALL(*gpu_memory_buffer, IsMapped()) |
| 161 .WillOnce(Return(false)) | 146 .WillOnce(Return(false)) |
| 162 .RetiresOnSaturation(); | 147 .RetiresOnSaturation(); |
| 163 | 148 |
| 164 shared_memory.Map(bytes); | 149 shared_memory.Map(bytes); |
| 165 EXPECT_TRUE(shared_memory.memory()); | 150 EXPECT_TRUE(shared_memory.memory()); |
| 166 | 151 |
| 167 EXPECT_CALL(*gpu_memory_buffer, Map(_, _)) | 152 EXPECT_CALL(*gpu_memory_buffer, Map(_, _)) |
| 168 .Times(1) | 153 .Times(1) |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 GLTestHelper::CheckPixels(0, 0, kImageWidth, kImageHeight, 0, pixels); | 195 GLTestHelper::CheckPixels(0, 0, kImageWidth, kImageHeight, 0, pixels); |
| 211 EXPECT_TRUE(GL_NO_ERROR == glGetError()); | 196 EXPECT_TRUE(GL_NO_ERROR == glGetError()); |
| 212 | 197 |
| 213 // Release the image. | 198 // Release the image. |
| 214 glReleaseTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id); | 199 glReleaseTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id); |
| 215 | 200 |
| 216 // Destroy the image. | 201 // Destroy the image. |
| 217 EXPECT_CALL(*gpu_memory_buffer, Die()) | 202 EXPECT_CALL(*gpu_memory_buffer, Die()) |
| 218 .Times(1) | 203 .Times(1) |
| 219 .RetiresOnSaturation(); | 204 .RetiresOnSaturation(); |
| 220 | |
| 221 EXPECT_CALL(*image_factory_.get(), DeleteGpuMemoryBuffer(image_id)) | |
| 222 .Times(1) | |
| 223 .RetiresOnSaturation(); | |
| 224 | |
| 225 glDestroyImageCHROMIUM(image_id); | 205 glDestroyImageCHROMIUM(image_id); |
| 226 } | 206 } |
| 227 | 207 |
| 228 } // namespace gles2 | 208 } // namespace gles2 |
| 229 } // namespace gpu | 209 } // namespace gpu |
| OLD | NEW |