OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <GLES2/gl2.h> |
| 6 #include <GLES2/gl2chromium.h> |
| 7 #include <GLES2/gl2ext.h> |
| 8 #include <GLES2/gl2extchromium.h> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/memory/ref_counted.h" |
| 12 #include "gpu/command_buffer/client/gles2_implementation.h" |
| 13 #include "gpu/command_buffer/client/gpu_memory_buffer_factory.h" |
| 14 #include "gpu/command_buffer/client/gpu_memory_buffer_mock.h" |
| 15 #include "gpu/command_buffer/client/image_factory_mock.h" |
| 16 #include "gpu/command_buffer/service/image_manager.h" |
| 17 #include "gpu/command_buffer/tests/gl_manager.h" |
| 18 #include "gpu/command_buffer/tests/gl_test_utils.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "ui/gfx/native_widget_types.h" |
| 22 #include "ui/gl/gl_image.h" |
| 23 #include "ui/gl/gl_image_mock.h" |
| 24 |
| 25 using testing::_; |
| 26 using testing::Return; |
| 27 using testing::SetArgPointee; |
| 28 using testing::StrictMock; |
| 29 |
| 30 namespace gpu { |
| 31 namespace gles2 { |
| 32 |
| 33 static const int kImageWidth = 256; |
| 34 static const int kImageHeight = 256; |
| 35 static const int kImageBytesPerPixel = 4; |
| 36 |
| 37 class MockGpuMemoryBufferTest : public testing::Test { |
| 38 protected: |
| 39 virtual void SetUp() { |
| 40 GLManager::Options options; |
| 41 image_manager_ = new ImageManager; |
| 42 image_factory_ = |
| 43 new StrictMock<ImageFactoryMock>(image_manager_); |
| 44 options.image_manager = image_manager_; |
| 45 options.image_factory = image_factory_; |
| 46 |
| 47 gl_.Initialize(options); |
| 48 gl_.MakeCurrent(); |
| 49 } |
| 50 |
| 51 virtual void TearDown() { |
| 52 gl_.Destroy(); |
| 53 } |
| 54 |
| 55 scoped_refptr<StrictMock<ImageFactoryMock> > image_factory_; |
| 56 scoped_refptr<ImageManager> image_manager_; |
| 57 GLManager gl_; |
| 58 }; |
| 59 |
| 60 // An end to end test that tests the whole GpuMemoryBuffer lifecycle. |
| 61 TEST_F(MockGpuMemoryBufferTest, Lifecycle) { |
| 62 // Create a client texture id. |
| 63 GLuint texture_id; |
| 64 glGenTextures(1, &texture_id); |
| 65 |
| 66 // Buffer is owned and freed by GpuMemoryBufferTracker. |
| 67 StrictMock<GpuMemoryBufferMock>* gpu_memory_buffer = |
| 68 new StrictMock<GpuMemoryBufferMock>(kImageWidth, kImageHeight); |
| 69 |
| 70 const GLuint kImageId = 345u; |
| 71 |
| 72 EXPECT_CALL(*image_factory_.get(), CreateGpuMemoryBufferMock( |
| 73 kImageWidth, kImageHeight, GL_RGBA8_OES, _)) |
| 74 .Times(1) |
| 75 .WillOnce(DoAll(SetArgPointee<3>(kImageId), |
| 76 Return(gpu_memory_buffer))) |
| 77 .RetiresOnSaturation(); |
| 78 |
| 79 // Create the GLImage and insert it into the ImageManager, which |
| 80 // would be done within CreateGpuMemoryBufferMock if it weren't a mock. |
| 81 GLuint image_id = glCreateImageCHROMIUM( |
| 82 kImageWidth, kImageHeight, GL_RGBA8_OES); |
| 83 EXPECT_EQ(kImageId, image_id); |
| 84 gfx::Size size(kImageWidth, kImageHeight); |
| 85 |
| 86 scoped_refptr<gfx::GLImageMock> gl_image( |
| 87 new gfx::GLImageMock(gpu_memory_buffer, size)); |
| 88 image_manager_->AddImage(gl_image.get(), image_id); |
| 89 |
| 90 EXPECT_CALL(*gpu_memory_buffer, IsMapped()) |
| 91 .WillOnce(Return(false)) |
| 92 .RetiresOnSaturation(); |
| 93 |
| 94 scoped_ptr<uint8[]> buffer_pixels(new uint8[ |
| 95 kImageWidth * kImageHeight * kImageBytesPerPixel]); |
| 96 |
| 97 EXPECT_CALL(*gpu_memory_buffer, Map(_, _)) |
| 98 .Times(1) |
| 99 .WillOnce(SetArgPointee<1>(buffer_pixels.get())) |
| 100 .RetiresOnSaturation(); |
| 101 void* mapped_buffer = |
| 102 glMapImageCHROMIUM(image_id, GL_WRITE_ONLY); |
| 103 EXPECT_EQ(buffer_pixels.get(), mapped_buffer); |
| 104 |
| 105 EXPECT_CALL(*gpu_memory_buffer, IsMapped()) |
| 106 .WillOnce(Return(true)) |
| 107 .RetiresOnSaturation(); |
| 108 |
| 109 // Unmap the image. |
| 110 EXPECT_CALL(*gpu_memory_buffer, Unmap()) |
| 111 .Times(1) |
| 112 .RetiresOnSaturation(); |
| 113 glUnmapImageCHROMIUM(image_id); |
| 114 |
| 115 // Bind the texture and the image. |
| 116 glBindTexture(GL_TEXTURE_2D, texture_id); |
| 117 EXPECT_CALL(*gl_image, BindTexImage()) |
| 118 .Times(1) |
| 119 .WillOnce(Return(true)) |
| 120 .RetiresOnSaturation(); |
| 121 EXPECT_CALL(*gl_image, GetSize()) |
| 122 .Times(1) |
| 123 .WillOnce(Return(size)) |
| 124 .RetiresOnSaturation(); |
| 125 glBindTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id); |
| 126 |
| 127 // Destroy the image. |
| 128 EXPECT_CALL(*gpu_memory_buffer, Die()) |
| 129 .Times(1) |
| 130 .RetiresOnSaturation(); |
| 131 |
| 132 EXPECT_CALL(*image_factory_.get(), DeleteGpuMemoryBuffer(image_id)) |
| 133 .Times(1) |
| 134 .RetiresOnSaturation(); |
| 135 |
| 136 glDestroyImageCHROMIUM(image_id); |
| 137 |
| 138 // Delete the texture. |
| 139 glDeleteTextures(1, &texture_id); |
| 140 } |
| 141 |
| 142 } // namespace gles2 |
| 143 } // namespace gpu |
OLD | NEW |