Index: gpu/command_buffer/tests/gl_gpu_memory_buffer_unittests.cc |
diff --git a/gpu/command_buffer/tests/gl_gpu_memory_buffer_unittests.cc b/gpu/command_buffer/tests/gl_gpu_memory_buffer_unittests.cc |
index 4647465abd8bb75abb84dbc2cebf997050ddf112..c7f0e025216b80494f993f47d2073e64cb1df101 100644 |
--- a/gpu/command_buffer/tests/gl_gpu_memory_buffer_unittests.cc |
+++ b/gpu/command_buffer/tests/gl_gpu_memory_buffer_unittests.cc |
@@ -9,17 +9,16 @@ |
#include "base/bind.h" |
#include "base/memory/ref_counted.h" |
+#include "base/process_util.h" |
#include "gpu/command_buffer/client/gles2_implementation.h" |
-#include "gpu/command_buffer/client/gpu_memory_buffer_mock.h" |
-#include "gpu/command_buffer/client/image_factory_mock.h" |
+#include "gpu/command_buffer/client/gpu_memory_buffer_factory.h" |
+#include "gpu/command_buffer/service/command_buffer_service.h" |
#include "gpu/command_buffer/service/image_manager.h" |
#include "gpu/command_buffer/tests/gl_manager.h" |
#include "gpu/command_buffer/tests/gl_test_utils.h" |
#include "testing/gmock/include/gmock/gmock.h" |
#include "testing/gtest/include/gtest/gtest.h" |
-#include "ui/gfx/native_widget_types.h" |
#include "ui/gl/gl_image.h" |
-#include "ui/gl/gl_image_mock.h" |
using testing::_; |
using testing::IgnoreResult; |
@@ -32,77 +31,143 @@ using testing::StrictMock; |
namespace gpu { |
namespace gles2 { |
-static const int kImageWidth = 256; |
-static const int kImageHeight = 256; |
+static const int kImageWidth = 32; |
+static const int kImageHeight = 32; |
static const int kImageBytesPerPixel = 4; |
+class MockGpuMemoryBuffer : public gfx::GpuMemoryBuffer { |
+ public: |
+ MockGpuMemoryBuffer(int width, int height) {} |
+ virtual ~MockGpuMemoryBuffer() { |
+ Die(); |
+ } |
+ |
+ MOCK_METHOD2(Map, void(gfx::GpuMemoryBuffer::AccessMode, void**)); |
+ MOCK_METHOD0(Unmap, void()); |
+ MOCK_CONST_METHOD0(IsMapped, bool()); |
+ MOCK_CONST_METHOD0(GetStride, uint32()); |
+ MOCK_CONST_METHOD0(GetHandle, gfx::GpuMemoryBufferHandle()); |
+ MOCK_METHOD0(Die, void()); |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(MockGpuMemoryBuffer); |
+}; |
+ |
+class MockGpuMemoryBufferFactory : public GpuMemoryBufferFactory { |
+ public: |
+ MockGpuMemoryBufferFactory() {} |
+ virtual ~MockGpuMemoryBufferFactory() {} |
+ |
+ MOCK_METHOD3(CreateGpuMemoryBuffer, |
+ gfx::GpuMemoryBuffer*(size_t width, |
+ size_t height, |
+ unsigned internalformat)); |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(MockGpuMemoryBufferFactory); |
+}; |
+ |
class MockGpuMemoryBufferTest : public testing::Test { |
protected: |
virtual void SetUp() { |
GLManager::Options options; |
image_manager_ = new ImageManager; |
- image_factory_.reset( |
- new StrictMock<ImageFactoryMock>(image_manager_.get())); |
+ gpu_memory_buffer_factory_.reset(new MockGpuMemoryBufferFactory); |
options.image_manager = image_manager_.get(); |
- options.image_factory = image_factory_.get(); |
+ options.gpu_memory_buffer_factory = gpu_memory_buffer_factory_.get(); |
gl_.Initialize(options); |
gl_.MakeCurrent(); |
+ |
+ glGenTextures(2, texture_ids_); |
+ glBindTexture(GL_TEXTURE_2D, texture_ids_[1]); |
+ |
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
+ |
+ glGenFramebuffers(1, &framebuffer_id_); |
+ glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id_); |
+ glFramebufferTexture2D(GL_FRAMEBUFFER, |
+ GL_COLOR_ATTACHMENT0, |
+ GL_TEXTURE_2D, |
+ texture_ids_[1], |
+ 0); |
} |
virtual void TearDown() { |
+ glDeleteTextures(2, texture_ids_); |
+ glDeleteFramebuffers(1, &framebuffer_id_); |
+ |
gl_.Destroy(); |
} |
- scoped_ptr<StrictMock<ImageFactoryMock> > image_factory_; |
scoped_refptr<ImageManager> image_manager_; |
+ scoped_ptr<MockGpuMemoryBufferFactory> gpu_memory_buffer_factory_; |
GLManager gl_; |
+ GLuint texture_ids_[2]; |
+ GLuint framebuffer_id_; |
}; |
// An end to end test that tests the whole GpuMemoryBuffer lifecycle. |
TEST_F(MockGpuMemoryBufferTest, Lifecycle) { |
- // Create a client texture id. |
- GLuint texture_id; |
- glGenTextures(1, &texture_id); |
+ size_t bytes = kImageWidth * kImageHeight * kImageBytesPerPixel; |
+ uint8 pixels[1 * 4] = { 255u, 0u, 0u, 255u }; |
// Buffer is owned and freed by GpuMemoryBufferTracker. |
- StrictMock<GpuMemoryBufferMock>* gpu_memory_buffer = |
- new StrictMock<GpuMemoryBufferMock>(kImageWidth, kImageHeight); |
- |
- const GLuint kImageId = 345u; |
- |
- EXPECT_CALL(*image_factory_.get(), CreateGpuMemoryBufferMock( |
- kImageWidth, kImageHeight, GL_RGBA8_OES, _)) |
+ StrictMock<MockGpuMemoryBuffer>* gpu_memory_buffer = |
+ new StrictMock<MockGpuMemoryBuffer>(kImageWidth, kImageHeight); |
+ base::SharedMemory shared_memory; |
+ shared_memory.CreateAnonymous(bytes); |
+ |
+ base::SharedMemoryHandle duped_shared_memory_handle; |
+ shared_memory.ShareToProcess(base::GetCurrentProcessHandle(), |
+ &duped_shared_memory_handle); |
+ gfx::GpuMemoryBufferHandle handle( |
+ duped_shared_memory_handle, gfx::SHARED_MEMORY_BUFFER); |
+ |
+ EXPECT_CALL(*gpu_memory_buffer_factory_.get(), CreateGpuMemoryBuffer( |
+ kImageWidth, kImageHeight, GL_RGBA8_OES)) |
.Times(1) |
- .WillOnce(DoAll(SetArgPointee<3>(kImageId), |
- Return(gpu_memory_buffer))) |
+ .WillOnce(Return(gpu_memory_buffer)) |
+ .RetiresOnSaturation(); |
+ EXPECT_CALL(*gpu_memory_buffer, GetHandle()) |
+ .Times(1) |
+ .WillOnce(Return(handle)) |
.RetiresOnSaturation(); |
- // Create the GLImage and insert it into the ImageManager, which |
- // would be done within CreateGpuMemoryBufferMock if it weren't a mock. |
+ // Create the image. This should add the image ID to the ImageManager. |
GLuint image_id = glCreateImageCHROMIUM( |
kImageWidth, kImageHeight, GL_RGBA8_OES); |
- EXPECT_EQ(kImageId, image_id); |
- |
- gfx::Size size(kImageWidth, kImageHeight); |
- scoped_refptr<gfx::GLImageMock> gl_image( |
- new gfx::GLImageMock(gpu_memory_buffer, size)); |
- image_manager_->AddImage(gl_image.get(), image_id); |
+ EXPECT_NE(0u, image_id); |
+ EXPECT_TRUE(image_manager_->LookupImage(image_id) != NULL); |
EXPECT_CALL(*gpu_memory_buffer, IsMapped()) |
.WillOnce(Return(false)) |
.RetiresOnSaturation(); |
- scoped_ptr<uint8[]> buffer_pixels(new uint8[ |
- kImageWidth * kImageHeight * kImageBytesPerPixel]); |
+ shared_memory.Map(bytes); |
+ EXPECT_TRUE(shared_memory.memory()); |
EXPECT_CALL(*gpu_memory_buffer, Map(_, _)) |
.Times(1) |
- .WillOnce(SetArgPointee<1>(buffer_pixels.get())) |
+ .WillOnce(SetArgPointee<1>(shared_memory.memory())) |
.RetiresOnSaturation(); |
- void* mapped_buffer = |
- glMapImageCHROMIUM(image_id, GL_WRITE_ONLY); |
- EXPECT_EQ(buffer_pixels.get(), mapped_buffer); |
+ uint8* mapped_buffer = static_cast<uint8*>( |
+ glMapImageCHROMIUM(image_id, GL_WRITE_ONLY)); |
+ ASSERT_TRUE(mapped_buffer != NULL); |
+ |
+ // Assign a value to each pixel. |
+ int stride = kImageWidth * kImageBytesPerPixel; |
+ for (int x = 0; x < kImageWidth; ++x) { |
+ for (int y = 0; y < kImageHeight; ++y) { |
+ mapped_buffer[y * stride + x * kImageBytesPerPixel + 0] = pixels[2]; |
+ mapped_buffer[y * stride + x * kImageBytesPerPixel + 1] = pixels[1]; |
+ mapped_buffer[y * stride + x * kImageBytesPerPixel + 2] = pixels[0]; |
+ mapped_buffer[y * stride + x * kImageBytesPerPixel + 3] = pixels[3]; |
+ } |
+ } |
EXPECT_CALL(*gpu_memory_buffer, IsMapped()) |
.WillOnce(Return(true)) |
@@ -115,26 +180,30 @@ TEST_F(MockGpuMemoryBufferTest, Lifecycle) { |
glUnmapImageCHROMIUM(image_id); |
// Bind the texture and the image. |
- glBindTexture(GL_TEXTURE_2D, texture_id); |
- EXPECT_CALL(*gl_image.get(), BindTexImage()).Times(1).WillOnce(Return(true)) |
- .RetiresOnSaturation(); |
- EXPECT_CALL(*gl_image.get(), GetSize()).Times(1).WillOnce(Return(size)) |
- .RetiresOnSaturation(); |
+ glBindTexture(GL_TEXTURE_2D, texture_ids_[0]); |
glBindTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id); |
+ // Copy texture so we can verify result using CheckPixels. |
+ glCopyTextureCHROMIUM(GL_TEXTURE_2D, |
+ texture_ids_[0], |
+ texture_ids_[1], |
+ 0, |
+ GL_RGBA, |
+ GL_UNSIGNED_BYTE); |
+ EXPECT_TRUE(glGetError() == GL_NO_ERROR); |
+ |
+ // Check if pixels match the values that were assigned to the mapped buffer. |
+ GLTestHelper::CheckPixels(0, 0, kImageWidth, kImageHeight, 0, pixels); |
+ EXPECT_TRUE(GL_NO_ERROR == glGetError()); |
+ |
+ // Release the image. |
+ glReleaseTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id); |
+ |
// Destroy the image. |
EXPECT_CALL(*gpu_memory_buffer, Die()) |
.Times(1) |
.RetiresOnSaturation(); |
- |
- EXPECT_CALL(*image_factory_.get(), DeleteGpuMemoryBuffer(image_id)) |
- .Times(1) |
- .RetiresOnSaturation(); |
- |
glDestroyImageCHROMIUM(image_id); |
- |
- // Delete the texture. |
- glDeleteTextures(1, &texture_id); |
} |
} // namespace gles2 |