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/gpu_memory_buffer_tracker_mock.h" | |
16 #include "gpu/command_buffer/client/image_factory_mock.h" | |
17 #include "gpu/command_buffer/service/image_manager.h" | |
18 #include "gpu/command_buffer/tests/gl_manager.h" | |
19 #include "gpu/command_buffer/tests/gl_test_utils.h" | |
20 #include "testing/gmock/include/gmock/gmock.h" | |
21 #include "testing/gtest/include/gtest/gtest.h" | |
22 #include "ui/gfx/native_widget_types.h" | |
23 #include "ui/gl/gl_image.h" | |
24 #include "ui/gl/gl_image_mock.h" | |
25 | |
26 using testing::_; | |
27 using testing::Return; | |
28 using testing::SetArgPointee; | |
29 using testing::StrictMock; | |
30 | |
31 namespace gpu { | |
32 namespace gles2 { | |
33 | |
34 static const int kGpuMemoryBufferWidth = 256; | |
35 static const int kGpuMemoryBufferHeight = 256; | |
36 static const int kGpuMemoryBufferBytesPerPixel = 4; | |
37 | |
38 class GLGpuMemoryBufferTest : public testing::Test { | |
39 protected: | |
40 virtual void SetUp() { | |
41 GLManager::Options options; | |
42 image_manager_ = new ImageManager; | |
43 options.image_manager = image_manager_; | |
44 | |
45 scoped_ptr<StrictMock<ImageFactoryMock> > image_factory( | |
46 new StrictMock<ImageFactoryMock>(image_manager_)); | |
47 image_factory_ = image_factory.get(); | |
reveman
2013/05/10 02:06:16
Saving a pointer and passing ownership to a differ
kaanb
2013/05/13 23:00:36
Done.
| |
48 | |
49 scoped_ptr<StrictMock<GpuMemoryBufferTrackerMock> > | |
50 gpu_memory_buffer_tracker( | |
51 new StrictMock<GpuMemoryBufferTrackerMock>(image_factory_)); | |
52 gpu_memory_buffer_tracker_ = gpu_memory_buffer_tracker.get(); | |
53 | |
54 gl_.Initialize(options); | |
55 | |
56 // GLES2Implementation will take ownership of ImageFactory and | |
57 // GpuMemoryBufferTracker. | |
58 gl_.gles2_implementation()->SetImageFactory( | |
59 image_factory.PassAs<ImageFactory>()); | |
60 gl_.gles2_implementation()->SetGpuMemoryBufferTracker( | |
61 gpu_memory_buffer_tracker.PassAs<GpuMemoryBufferTracker>()); | |
62 gl_.MakeCurrent(); | |
63 mapped_buffer_.reset(new uint8[ | |
64 kGpuMemoryBufferWidth * kGpuMemoryBufferHeight | |
65 * kGpuMemoryBufferBytesPerPixel]); | |
66 | |
67 gpu_memory_buffer_.reset( | |
68 new StrictMock<GpuMemoryBufferMock>(kGpuMemoryBufferWidth, | |
69 kGpuMemoryBufferHeight)); | |
70 } | |
71 | |
72 virtual void TearDown() { | |
73 gl_.Destroy(); | |
74 } | |
75 | |
76 // TODO(kaanb): do I need the ImageFactory mock? | |
77 StrictMock<ImageFactoryMock>* image_factory_; | |
78 scoped_refptr<ImageManager> image_manager_; | |
79 scoped_ptr<uint8[]> mapped_buffer_; | |
80 StrictMock<GpuMemoryBufferTrackerMock>* gpu_memory_buffer_tracker_; | |
81 scoped_ptr<StrictMock<GpuMemoryBufferMock> > gpu_memory_buffer_; | |
82 GLManager gl_; | |
83 }; | |
84 | |
85 // An end to end test that tests the whole GpuMemoryBuffer lifecycle. | |
86 TEST_F(GLGpuMemoryBufferTest, Lifecycle) { | |
87 // Create a client texture id. | |
88 | |
89 GLuint texture_id; | |
90 glGenTextures(1, &texture_id); | |
91 | |
92 // Create the GLImage and insert it into the ImageManager. | |
93 EXPECT_CALL(*gpu_memory_buffer_tracker_, CreateBuffer(_, _)) | |
94 .Times(1) | |
95 .WillOnce(Return(1)) | |
96 .RetiresOnSaturation(); | |
97 GLuint image_id = | |
98 glCreateImageCHROMIUM(kGpuMemoryBufferWidth, kGpuMemoryBufferHeight); | |
99 gfx::Size size(kGpuMemoryBufferWidth, kGpuMemoryBufferHeight); | |
100 gfx::GLImageMock* gl_image = new gfx::GLImageMock(NULL, size); | |
101 image_manager_->AddImage(gl_image, image_id); | |
102 | |
103 EXPECT_CALL(*gpu_memory_buffer_.get(), IsMapped()) | |
104 .WillOnce(Return(false)) // before Map() | |
105 .WillOnce(Return(true)) // after Map() and before Unmap() | |
106 .RetiresOnSaturation(); | |
107 | |
108 EXPECT_CALL(*gpu_memory_buffer_tracker_, GetBuffer(image_id)) | |
109 .WillOnce(Return(gpu_memory_buffer_.get())) // Map() | |
110 .WillOnce(Return(gpu_memory_buffer_.get())) // UnMap() | |
111 .WillOnce(Return(gpu_memory_buffer_.get())) // DestroyImage() | |
112 .RetiresOnSaturation(); | |
113 | |
114 EXPECT_CALL(*gpu_memory_buffer_.get(), Map(_, _)) | |
115 .Times(1) | |
116 .WillOnce(SetArgPointee<1>(mapped_buffer_.get())) | |
117 .RetiresOnSaturation(); | |
118 void* mapped_buffer = | |
119 glMapImageCHROMIUM(image_id, GL_WRITE_ONLY); | |
120 EXPECT_EQ(mapped_buffer_.get(), mapped_buffer); | |
121 | |
122 // Unmap the image. | |
123 EXPECT_CALL(*gpu_memory_buffer_.get(), Unmap()) | |
124 .Times(1) | |
125 .RetiresOnSaturation(); | |
126 glUnmapImageCHROMIUM(image_id); | |
127 | |
128 // Bind the texture and the image. | |
129 glBindTexture(GL_TEXTURE_2D, texture_id); | |
130 EXPECT_CALL(*gl_image, BindTexImage()) | |
131 .Times(1) | |
132 .WillOnce(Return(true)) | |
133 .RetiresOnSaturation(); | |
134 EXPECT_CALL(*gl_image, GetSize()) | |
135 .Times(1) | |
136 .WillOnce(Return(size)) | |
137 .RetiresOnSaturation(); | |
138 glBindTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id); | |
139 | |
140 // Destroy the image. | |
141 EXPECT_CALL(*gpu_memory_buffer_tracker_, RemoveBuffer(image_id)) | |
142 .Times(1) | |
143 .RetiresOnSaturation(); | |
144 EXPECT_CALL(*gpu_memory_buffer_.get(), Die()) | |
145 .Times(1) | |
146 .RetiresOnSaturation(); | |
147 glDestroyImageCHROMIUM(image_id); | |
148 | |
149 // Delete the texture. | |
150 glDeleteTextures(1, &texture_id); | |
151 } | |
152 | |
153 } // namespace gles2 | |
154 } // namespace gpu | |
OLD | NEW |