OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 "cc/layers/texture_layer.h" | 5 #include "cc/layers/texture_layer.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/run_loop.h" | |
11 #include "cc/layers/texture_layer_client.h" | 10 #include "cc/layers/texture_layer_client.h" |
12 #include "cc/layers/texture_layer_impl.h" | 11 #include "cc/layers/texture_layer_impl.h" |
13 #include "cc/test/fake_impl_proxy.h" | 12 #include "cc/test/fake_impl_proxy.h" |
14 #include "cc/test/fake_layer_tree_host_client.h" | 13 #include "cc/test/fake_layer_tree_host_client.h" |
15 #include "cc/test/fake_layer_tree_host_impl.h" | 14 #include "cc/test/fake_layer_tree_host_impl.h" |
16 #include "cc/test/layer_test_common.h" | 15 #include "cc/test/layer_test_common.h" |
17 #include "cc/test/layer_tree_test.h" | 16 #include "cc/test/layer_tree_test.h" |
18 #include "cc/trees/layer_tree_host.h" | 17 #include "cc/trees/layer_tree_host.h" |
19 #include "cc/trees/layer_tree_impl.h" | 18 #include "cc/trees/layer_tree_impl.h" |
20 #include "cc/trees/single_thread_proxy.h" | 19 #include "cc/trees/single_thread_proxy.h" |
21 #include "gpu/GLES2/gl2extchromium.h" | 20 #include "gpu/GLES2/gl2extchromium.h" |
22 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
24 | 23 |
25 using ::testing::Mock; | 24 using ::testing::Mock; |
26 using ::testing::_; | 25 using ::testing::_; |
27 using ::testing::AtLeast; | 26 using ::testing::AtLeast; |
28 using ::testing::AnyNumber; | 27 using ::testing::AnyNumber; |
29 using ::testing::InvokeWithoutArgs; | |
30 | 28 |
31 namespace cc { | 29 namespace cc { |
32 namespace { | 30 namespace { |
33 | 31 |
34 class MockLayerTreeHost : public LayerTreeHost { | 32 class MockLayerTreeHost : public LayerTreeHost { |
35 public: | 33 public: |
36 explicit MockLayerTreeHost(LayerTreeHostClient* client) | 34 explicit MockLayerTreeHost(LayerTreeHostClient* client) |
37 : LayerTreeHost(client, LayerTreeSettings()) { | 35 : LayerTreeHost(client, LayerTreeSettings()) { |
38 Initialize(NULL); | 36 Initialize(NULL); |
39 } | 37 } |
40 | 38 |
41 MOCK_METHOD0(AcquireLayerTextures, void()); | 39 MOCK_METHOD0(AcquireLayerTextures, void()); |
42 MOCK_METHOD0(SetNeedsCommit, void()); | 40 MOCK_METHOD0(SetNeedsCommit, void()); |
43 MOCK_METHOD1(StartRateLimiter, void(WebKit::WebGraphicsContext3D* context)); | 41 MOCK_METHOD1(StartRateLimiter, void(WebKit::WebGraphicsContext3D* context)); |
44 MOCK_METHOD1(StopRateLimiter, void(WebKit::WebGraphicsContext3D* context)); | 42 MOCK_METHOD1(StopRateLimiter, void(WebKit::WebGraphicsContext3D* context)); |
45 }; | 43 }; |
46 | 44 |
47 class FakeTextureLayerClient : public TextureLayerClient { | |
48 public: | |
49 FakeTextureLayerClient() | |
50 : context_(TestWebGraphicsContext3D::Create()), | |
51 texture_(0), | |
52 mailbox_changed_(true) {} | |
53 | |
54 virtual unsigned PrepareTexture() OVERRIDE { | |
55 return texture_; | |
56 } | |
57 | |
58 virtual WebKit::WebGraphicsContext3D* Context3d() OVERRIDE { | |
59 return context_.get(); | |
60 } | |
61 | |
62 virtual bool PrepareTextureMailbox(TextureMailbox* mailbox, | |
63 bool use_shared_memory) OVERRIDE { | |
64 if (!mailbox_changed_) | |
65 return false; | |
66 | |
67 *mailbox = mailbox_; | |
68 mailbox_changed_ = false; | |
69 return true; | |
70 } | |
71 | |
72 void set_texture(unsigned texture) { | |
73 texture_ = texture; | |
74 } | |
75 | |
76 void set_mailbox(const TextureMailbox& mailbox) { | |
77 mailbox_ = mailbox; | |
78 mailbox_changed_ = true; | |
79 } | |
80 | |
81 private: | |
82 scoped_ptr<TestWebGraphicsContext3D> context_; | |
83 unsigned texture_; | |
84 TextureMailbox mailbox_; | |
85 bool mailbox_changed_; | |
86 DISALLOW_COPY_AND_ASSIGN(FakeTextureLayerClient); | |
87 }; | |
88 | |
89 class MockMailboxCallback { | |
90 public: | |
91 MOCK_METHOD3(Release, void(const std::string& mailbox, | |
92 unsigned sync_point, | |
93 bool lost_resource)); | |
94 MOCK_METHOD3(Release2, void(base::SharedMemory* shared_memory, | |
95 unsigned sync_point, | |
96 bool lost_resource)); | |
97 }; | |
98 | |
99 struct CommonMailboxObjects { | |
100 CommonMailboxObjects() | |
101 : mailbox_name1_(64, '1'), | |
102 mailbox_name2_(64, '2'), | |
103 sync_point1_(1), | |
104 sync_point2_(2), | |
105 shared_memory_(new base::SharedMemory) { | |
106 release_mailbox1_ = base::Bind(&MockMailboxCallback::Release, | |
107 base::Unretained(&mock_callback_), | |
108 mailbox_name1_); | |
109 release_mailbox2_ = base::Bind(&MockMailboxCallback::Release, | |
110 base::Unretained(&mock_callback_), | |
111 mailbox_name2_); | |
112 gpu::Mailbox m1; | |
113 m1.SetName(reinterpret_cast<const int8*>(mailbox_name1_.data())); | |
114 mailbox1_ = TextureMailbox(m1, release_mailbox1_, sync_point1_); | |
115 gpu::Mailbox m2; | |
116 m2.SetName(reinterpret_cast<const int8*>(mailbox_name2_.data())); | |
117 mailbox2_ = TextureMailbox(m2, release_mailbox2_, sync_point2_); | |
118 | |
119 gfx::Size size(128, 128); | |
120 EXPECT_TRUE(shared_memory_->CreateAndMapAnonymous(4 * size.GetArea())); | |
121 release_mailbox3_ = base::Bind(&MockMailboxCallback::Release2, | |
122 base::Unretained(&mock_callback_), | |
123 shared_memory_.get()); | |
124 mailbox3_ = TextureMailbox(shared_memory_.get(), size, release_mailbox3_); | |
125 } | |
126 | |
127 std::string mailbox_name1_; | |
128 std::string mailbox_name2_; | |
129 MockMailboxCallback mock_callback_; | |
130 TextureMailbox::ReleaseCallback release_mailbox1_; | |
131 TextureMailbox::ReleaseCallback release_mailbox2_; | |
132 TextureMailbox::ReleaseCallback release_mailbox3_; | |
133 TextureMailbox mailbox1_; | |
134 TextureMailbox mailbox2_; | |
135 TextureMailbox mailbox3_; | |
136 unsigned sync_point1_; | |
137 unsigned sync_point2_; | |
138 scoped_ptr<base::SharedMemory> shared_memory_; | |
139 }; | |
140 | |
141 class TextureLayerTest : public testing::Test { | 45 class TextureLayerTest : public testing::Test { |
142 public: | 46 public: |
143 TextureLayerTest() | 47 TextureLayerTest() |
144 : fake_client_( | 48 : fake_client_( |
145 FakeLayerTreeHostClient(FakeLayerTreeHostClient::DIRECT_3D)), | 49 FakeLayerTreeHostClient(FakeLayerTreeHostClient::DIRECT_3D)), |
146 host_impl_(&proxy_) {} | 50 host_impl_(&proxy_) {} |
147 | 51 |
148 protected: | 52 protected: |
149 virtual void SetUp() { | 53 virtual void SetUp() { |
150 layer_tree_host_.reset(new MockLayerTreeHost(&fake_client_)); | 54 layer_tree_host_.reset(new MockLayerTreeHost(&fake_client_)); |
151 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); | 55 } |
152 layer_tree_host_->SetViewportSize(gfx::Size(10, 10)); | |
153 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); | |
154 } | |
155 | 56 |
156 virtual void TearDown() { | 57 virtual void TearDown() { |
157 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); | 58 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
158 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber()); | 59 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber()); |
159 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); | 60 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); |
160 | 61 |
161 layer_tree_host_->SetRootLayer(NULL); | 62 layer_tree_host_->SetRootLayer(NULL); |
162 layer_tree_host_.reset(); | 63 layer_tree_host_.reset(); |
163 } | 64 } |
164 | 65 |
165 scoped_ptr<MockLayerTreeHost> layer_tree_host_; | 66 scoped_ptr<MockLayerTreeHost> layer_tree_host_; |
166 FakeImplProxy proxy_; | 67 FakeImplProxy proxy_; |
167 FakeLayerTreeHostClient fake_client_; | 68 FakeLayerTreeHostClient fake_client_; |
168 FakeLayerTreeHostImpl host_impl_; | 69 FakeLayerTreeHostImpl host_impl_; |
169 }; | 70 }; |
170 | 71 |
171 TEST_F(TextureLayerTest, SyncImplWhenClearingTexture) { | 72 TEST_F(TextureLayerTest, SyncImplWhenChangingTextureId) { |
172 FakeTextureLayerClient client; | 73 scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL); |
173 scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(&client); | |
174 ASSERT_TRUE(test_layer.get()); | 74 ASSERT_TRUE(test_layer.get()); |
175 test_layer->SetIsDrawable(true); | |
176 test_layer->SetBounds(gfx::Size(10, 10)); | |
177 | 75 |
178 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); | 76 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber()); |
179 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); | 77 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); |
180 layer_tree_host_->SetRootLayer(test_layer); | 78 layer_tree_host_->SetRootLayer(test_layer); |
181 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); | 79 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
182 EXPECT_EQ(test_layer->layer_tree_host(), layer_tree_host_.get()); | 80 EXPECT_EQ(test_layer->layer_tree_host(), layer_tree_host_.get()); |
183 | 81 |
184 // Clearing the texture before we gave one should not sync. | |
185 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); | 82 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
186 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); | 83 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
187 test_layer->ClearTexture(); | 84 test_layer->SetTextureId(1); |
188 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); | 85 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
189 | 86 |
190 // Give a texture to the layer through the client. | 87 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AtLeast(1)); |
191 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); | |
192 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); | 88 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
193 client.set_texture(client.Context3d()->createTexture()); | 89 test_layer->SetTextureId(2); |
194 test_layer->SetNeedsDisplay(); | |
195 // Force a commit. | |
196 layer_tree_host_->Composite(base::TimeTicks()); | |
197 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); | 90 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
198 | 91 |
199 // Clearing the texture should sync. | |
200 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AtLeast(1)); | 92 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AtLeast(1)); |
201 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); | 93 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
202 test_layer->ClearTexture(); | 94 test_layer->SetTextureId(0); |
203 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); | |
204 | |
205 // But only once. | |
206 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); | |
207 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); | |
208 test_layer->ClearTexture(); | |
209 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); | |
210 | |
211 // Force a commit to give another texture. | |
212 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); | |
213 test_layer->SetNeedsDisplay(); | |
214 layer_tree_host_->Composite(base::TimeTicks()); | |
215 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); | |
216 | |
217 // Make undrawable and commit. | |
218 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); | |
219 test_layer->SetIsDrawable(false); | |
220 layer_tree_host_->Composite(base::TimeTicks()); | |
221 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); | |
222 | |
223 // Clearing textures should not sync. | |
224 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); | |
225 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); | |
226 test_layer->ClearTexture(); | |
227 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); | 95 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
228 } | 96 } |
229 | 97 |
230 TEST_F(TextureLayerTest, SyncImplWhenClearingMailbox) { | 98 TEST_F(TextureLayerTest, SyncImplWhenDrawing) { |
231 CommonMailboxObjects mailboxes; | 99 gfx::RectF dirty_rect(0.f, 0.f, 1.f, 1.f); |
232 FakeTextureLayerClient client; | 100 |
233 scoped_refptr<TextureLayer> test_layer = | 101 scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL); |
234 TextureLayer::CreateForMailbox(&client); | |
235 ASSERT_TRUE(test_layer.get()); | 102 ASSERT_TRUE(test_layer.get()); |
236 test_layer->SetIsDrawable(true); | 103 scoped_ptr<TextureLayerImpl> impl_layer; |
237 test_layer->SetBounds(gfx::Size(10, 10)); | 104 impl_layer = TextureLayerImpl::Create(host_impl_.active_tree(), 1, false); |
238 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); | 105 ASSERT_TRUE(impl_layer); |
| 106 |
| 107 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber()); |
239 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); | 108 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); |
240 layer_tree_host_->SetRootLayer(test_layer); | 109 layer_tree_host_->SetRootLayer(test_layer); |
| 110 test_layer->SetTextureId(1); |
| 111 test_layer->SetIsDrawable(true); |
241 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); | 112 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
242 EXPECT_EQ(test_layer->layer_tree_host(), layer_tree_host_.get()); | 113 EXPECT_EQ(test_layer->layer_tree_host(), layer_tree_host_.get()); |
243 | 114 |
244 // Clearing the mailbox before we gave one should not sync. | 115 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(1); |
245 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); | 116 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0); |
246 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); | 117 test_layer->WillModifyTexture(); |
247 test_layer->ClearTexture(); | |
248 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); | 118 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
249 | 119 |
250 // Give a mailbox to the layer through the client. | |
251 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); | 120 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
252 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); | 121 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(1); |
253 client.set_mailbox(mailboxes.mailbox1_); | 122 test_layer->SetNeedsDisplayRect(dirty_rect); |
254 test_layer->SetNeedsDisplay(); | |
255 // Force a commit. | |
256 layer_tree_host_->Composite(base::TimeTicks()); | |
257 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); | 123 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
258 | 124 |
259 // Clearing the mailbox should sync. | 125 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
260 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AtLeast(1)); | 126 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(1); |
261 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); | 127 test_layer->PushPropertiesTo(impl_layer.get()); // fake commit |
262 test_layer->ClearTexture(); | 128 test_layer->SetIsDrawable(false); |
263 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); | 129 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
264 | 130 |
265 // But only once. | 131 // Verify that non-drawable layers don't signal the compositor, |
266 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); | 132 // except for the first draw after last commit, which must acquire |
267 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); | 133 // the texture. |
268 test_layer->ClearTexture(); | 134 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(1); |
| 135 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0); |
| 136 test_layer->WillModifyTexture(); |
| 137 test_layer->SetNeedsDisplayRect(dirty_rect); |
| 138 test_layer->PushPropertiesTo(impl_layer.get()); // fake commit |
269 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); | 139 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
270 | 140 |
271 // Force a commit to give another mailbox. | 141 // Second draw with layer in non-drawable state: no texture |
| 142 // acquisition. |
272 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); | 143 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
273 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); | 144 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0); |
274 client.set_mailbox(mailboxes.mailbox2_); | 145 test_layer->WillModifyTexture(); |
275 test_layer->SetNeedsDisplay(); | 146 test_layer->SetNeedsDisplayRect(dirty_rect); |
276 // Commit will return mailbox1. | |
277 layer_tree_host_->Composite(base::TimeTicks()); | |
278 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); | 147 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
279 | |
280 // Wait for mailbox callback. | |
281 { | |
282 base::RunLoop run_loop; | |
283 EXPECT_CALL(mailboxes.mock_callback_, | |
284 Release(mailboxes.mailbox_name1_, _, false)) | |
285 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); | |
286 run_loop.Run(); | |
287 } | |
288 | |
289 // Make undrawable and commit. | |
290 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); | |
291 test_layer->SetIsDrawable(false); | |
292 layer_tree_host_->Composite(base::TimeTicks()); | |
293 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); | |
294 | |
295 // Clearing textures should not sync. | |
296 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); | |
297 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); | |
298 test_layer->ClearTexture(); | |
299 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); | |
300 | |
301 // Commit will return the mailbox. | |
302 layer_tree_host_->Composite(base::TimeTicks()); | |
303 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); | |
304 | |
305 // Wait for mailbox callback. | |
306 { | |
307 base::RunLoop run_loop; | |
308 EXPECT_CALL(mailboxes.mock_callback_, | |
309 Release(mailboxes.mailbox_name2_, _, false)) | |
310 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); | |
311 run_loop.Run(); | |
312 } | |
313 } | 148 } |
314 | 149 |
315 TEST_F(TextureLayerTest, SyncImplWhenRemovingFromTree) { | 150 TEST_F(TextureLayerTest, SyncImplWhenRemovingFromTree) { |
316 scoped_refptr<Layer> root_layer = Layer::Create(); | 151 scoped_refptr<Layer> root_layer = Layer::Create(); |
317 ASSERT_TRUE(root_layer.get()); | 152 ASSERT_TRUE(root_layer.get()); |
318 scoped_refptr<Layer> child_layer = Layer::Create(); | 153 scoped_refptr<Layer> child_layer = Layer::Create(); |
319 ASSERT_TRUE(child_layer.get()); | 154 ASSERT_TRUE(child_layer.get()); |
320 root_layer->AddChild(child_layer); | 155 root_layer->AddChild(child_layer); |
321 FakeTextureLayerClient client; | 156 scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL); |
322 scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(&client); | |
323 test_layer->SetIsDrawable(true); | |
324 test_layer->SetBounds(gfx::Size(10, 10)); | |
325 ASSERT_TRUE(test_layer.get()); | 157 ASSERT_TRUE(test_layer.get()); |
| 158 test_layer->SetTextureId(0); |
326 child_layer->AddChild(test_layer); | 159 child_layer->AddChild(test_layer); |
327 | 160 |
328 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber()); | 161 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber()); |
329 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); | 162 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); |
330 layer_tree_host_->SetRootLayer(root_layer); | 163 layer_tree_host_->SetRootLayer(root_layer); |
331 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); | 164 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
332 | 165 |
333 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); | 166 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
334 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); | 167 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
335 test_layer->RemoveFromParent(); | 168 test_layer->RemoveFromParent(); |
336 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); | 169 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
337 | 170 |
338 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); | 171 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
339 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); | 172 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
340 child_layer->AddChild(test_layer); | 173 child_layer->AddChild(test_layer); |
341 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); | 174 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
342 | 175 |
343 // Give a texture to the layer through the client. | |
344 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); | 176 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0); |
345 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); | 177 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
346 client.set_texture(client.Context3d()->createTexture()); | 178 test_layer->SetTextureId(1); |
347 test_layer->SetNeedsDisplay(); | |
348 // Force a commit. | |
349 layer_tree_host_->Composite(base::TimeTicks()); | |
350 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); | 179 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
351 | 180 |
352 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AtLeast(1)); | 181 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AtLeast(1)); |
353 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); | 182 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1)); |
354 test_layer->RemoveFromParent(); | 183 test_layer->RemoveFromParent(); |
355 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); | 184 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
356 } | 185 } |
357 | 186 |
358 TEST_F(TextureLayerTest, CheckPropertyChangeCausesCorrectBehavior) { | 187 TEST_F(TextureLayerTest, CheckPropertyChangeCausesCorrectBehavior) { |
359 scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL); | 188 scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL); |
360 EXPECT_SET_NEEDS_COMMIT(1, layer_tree_host_->SetRootLayer(test_layer)); | 189 layer_tree_host_->SetRootLayer(test_layer); |
361 | 190 |
362 // Test properties that should call SetNeedsCommit. All properties need to | 191 // Test properties that should call SetNeedsCommit. All properties need to |
363 // be set to new values in order for SetNeedsCommit to be called. | 192 // be set to new values in order for SetNeedsCommit to be called. |
364 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFlipped(false)); | 193 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFlipped(false)); |
365 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetUV( | 194 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetUV( |
366 gfx::PointF(0.25f, 0.25f), gfx::PointF(0.75f, 0.75f))); | 195 gfx::PointF(0.25f, 0.25f), gfx::PointF(0.75f, 0.75f))); |
367 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetVertexOpacity( | 196 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetVertexOpacity( |
368 0.5f, 0.5f, 0.5f, 0.5f)); | 197 0.5f, 0.5f, 0.5f, 0.5f)); |
369 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetPremultipliedAlpha(false)); | 198 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetPremultipliedAlpha(false)); |
370 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBlendBackgroundColor(true)); | 199 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBlendBackgroundColor(true)); |
371 EXPECT_SET_NEEDS_COMMIT(1, test_layer->ClearTexture()); | 200 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTextureId(1)); |
| 201 |
| 202 // Calling SetTextureId can call AcquireLayerTextures. |
| 203 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber()); |
372 } | 204 } |
373 | 205 |
| 206 class FakeTextureLayerClient : public TextureLayerClient { |
| 207 public: |
| 208 FakeTextureLayerClient() : context_(TestWebGraphicsContext3D::Create()) {} |
| 209 |
| 210 virtual unsigned PrepareTexture() OVERRIDE { |
| 211 return 0; |
| 212 } |
| 213 |
| 214 virtual WebKit::WebGraphicsContext3D* Context3d() OVERRIDE { |
| 215 return context_.get(); |
| 216 } |
| 217 |
| 218 virtual bool PrepareTextureMailbox(TextureMailbox* mailbox, |
| 219 bool use_shared_memory) OVERRIDE { |
| 220 *mailbox = TextureMailbox(); |
| 221 return true; |
| 222 } |
| 223 |
| 224 private: |
| 225 scoped_ptr<TestWebGraphicsContext3D> context_; |
| 226 DISALLOW_COPY_AND_ASSIGN(FakeTextureLayerClient); |
| 227 }; |
| 228 |
374 TEST_F(TextureLayerTest, RateLimiter) { | 229 TEST_F(TextureLayerTest, RateLimiter) { |
375 FakeTextureLayerClient client; | 230 FakeTextureLayerClient client; |
376 scoped_refptr<TextureLayer> test_layer = TextureLayer::CreateForMailbox( | 231 scoped_refptr<TextureLayer> test_layer = TextureLayer::CreateForMailbox( |
377 &client); | 232 &client); |
378 test_layer->SetIsDrawable(true); | 233 test_layer->SetIsDrawable(true); |
379 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); | 234 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); |
380 layer_tree_host_->SetRootLayer(test_layer); | 235 layer_tree_host_->SetRootLayer(test_layer); |
381 | 236 |
382 // Don't rate limit until we invalidate. | 237 // Don't rate limit until we invalidate. |
383 EXPECT_CALL(*layer_tree_host_, StartRateLimiter(_)).Times(0); | 238 EXPECT_CALL(*layer_tree_host_, StartRateLimiter(_)).Times(0); |
(...skipping 29 matching lines...) Expand all Loading... |
413 EXPECT_CALL(*layer_tree_host_, StartRateLimiter(client.Context3d())); | 268 EXPECT_CALL(*layer_tree_host_, StartRateLimiter(client.Context3d())); |
414 test_layer->SetNeedsDisplay(); | 269 test_layer->SetNeedsDisplay(); |
415 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); | 270 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
416 | 271 |
417 // Stop rate limiter when we're removed from the tree. | 272 // Stop rate limiter when we're removed from the tree. |
418 EXPECT_CALL(*layer_tree_host_, StopRateLimiter(client.Context3d())); | 273 EXPECT_CALL(*layer_tree_host_, StopRateLimiter(client.Context3d())); |
419 layer_tree_host_->SetRootLayer(NULL); | 274 layer_tree_host_->SetRootLayer(NULL); |
420 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); | 275 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); |
421 } | 276 } |
422 | 277 |
| 278 class MockMailboxCallback { |
| 279 public: |
| 280 MOCK_METHOD3(Release, void(const std::string& mailbox, |
| 281 unsigned sync_point, |
| 282 bool lost_resource)); |
| 283 MOCK_METHOD3(Release2, void(base::SharedMemory* shared_memory, |
| 284 unsigned sync_point, |
| 285 bool lost_resource)); |
| 286 }; |
| 287 |
| 288 struct CommonMailboxObjects { |
| 289 CommonMailboxObjects() |
| 290 : mailbox_name1_(64, '1'), |
| 291 mailbox_name2_(64, '2'), |
| 292 sync_point1_(1), |
| 293 sync_point2_(2), |
| 294 shared_memory_(new base::SharedMemory) { |
| 295 release_mailbox1_ = base::Bind(&MockMailboxCallback::Release, |
| 296 base::Unretained(&mock_callback_), |
| 297 mailbox_name1_); |
| 298 release_mailbox2_ = base::Bind(&MockMailboxCallback::Release, |
| 299 base::Unretained(&mock_callback_), |
| 300 mailbox_name2_); |
| 301 gpu::Mailbox m1; |
| 302 m1.SetName(reinterpret_cast<const int8*>(mailbox_name1_.data())); |
| 303 mailbox1_ = TextureMailbox(m1, release_mailbox1_, sync_point1_); |
| 304 gpu::Mailbox m2; |
| 305 m2.SetName(reinterpret_cast<const int8*>(mailbox_name2_.data())); |
| 306 mailbox2_ = TextureMailbox(m2, release_mailbox2_, sync_point2_); |
| 307 |
| 308 gfx::Size size(128, 128); |
| 309 EXPECT_TRUE(shared_memory_->CreateAndMapAnonymous(4 * size.GetArea())); |
| 310 release_mailbox3_ = base::Bind(&MockMailboxCallback::Release2, |
| 311 base::Unretained(&mock_callback_), |
| 312 shared_memory_.get()); |
| 313 mailbox3_ = TextureMailbox(shared_memory_.get(), size, release_mailbox3_); |
| 314 } |
| 315 |
| 316 std::string mailbox_name1_; |
| 317 std::string mailbox_name2_; |
| 318 MockMailboxCallback mock_callback_; |
| 319 TextureMailbox::ReleaseCallback release_mailbox1_; |
| 320 TextureMailbox::ReleaseCallback release_mailbox2_; |
| 321 TextureMailbox::ReleaseCallback release_mailbox3_; |
| 322 TextureMailbox mailbox1_; |
| 323 TextureMailbox mailbox2_; |
| 324 TextureMailbox mailbox3_; |
| 325 unsigned sync_point1_; |
| 326 unsigned sync_point2_; |
| 327 scoped_ptr<base::SharedMemory> shared_memory_; |
| 328 }; |
| 329 |
423 class TextureLayerWithMailboxTest : public TextureLayerTest { | 330 class TextureLayerWithMailboxTest : public TextureLayerTest { |
424 protected: | 331 protected: |
425 virtual void TearDown() { | 332 virtual void TearDown() { |
426 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); | 333 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_); |
427 EXPECT_CALL(test_data_.mock_callback_, | 334 EXPECT_CALL(test_data_.mock_callback_, |
428 Release(test_data_.mailbox_name1_, | 335 Release(test_data_.mailbox_name1_, |
429 test_data_.sync_point1_, | 336 test_data_.sync_point1_, |
430 false)).Times(1); | 337 false)).Times(1); |
431 TextureLayerTest::TearDown(); | 338 TextureLayerTest::TearDown(); |
432 } | 339 } |
(...skipping 603 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1036 scoped_refptr<TextureLayer> texture_layer_; | 943 scoped_refptr<TextureLayer> texture_layer_; |
1037 scoped_ptr<TestWebGraphicsContext3D> texture_context_; | 944 scoped_ptr<TestWebGraphicsContext3D> texture_context_; |
1038 unsigned texture_; | 945 unsigned texture_; |
1039 int draw_count_; | 946 int draw_count_; |
1040 }; | 947 }; |
1041 | 948 |
1042 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TextureLayerLostContextTest); | 949 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TextureLayerLostContextTest); |
1043 | 950 |
1044 } // namespace | 951 } // namespace |
1045 } // namespace cc | 952 } // namespace cc |
OLD | NEW |