Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(132)

Side by Side Diff: cc/texture_layer_unittest.cc

Issue 12603013: Part 10 of cc/ directory shuffles: layers (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « cc/texture_layer_impl.cc ('k') | cc/tiled_layer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "cc/texture_layer.h"
6
7 #include <string>
8
9 #include "base/callback.h"
10 #include "cc/base/thread.h"
11 #include "cc/test/fake_impl_proxy.h"
12 #include "cc/test/fake_layer_tree_host_client.h"
13 #include "cc/test/fake_layer_tree_host_impl.h"
14 #include "cc/test/layer_tree_test_common.h"
15 #include "cc/texture_layer_impl.h"
16 #include "cc/trees/layer_tree_host.h"
17 #include "cc/trees/layer_tree_impl.h"
18 #include "cc/trees/single_thread_proxy.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 using ::testing::Mock;
23 using ::testing::_;
24 using ::testing::AtLeast;
25 using ::testing::AnyNumber;
26
27 namespace cc {
28 namespace {
29
30 class MockLayerImplTreeHost : public LayerTreeHost {
31 public:
32 MockLayerImplTreeHost() : LayerTreeHost(&fake_client_, LayerTreeSettings()) {
33 Initialize(scoped_ptr<Thread>(NULL));
34 }
35
36 MOCK_METHOD0(AcquireLayerTextures, void());
37 MOCK_METHOD0(SetNeedsCommit, void());
38
39 private:
40 FakeLayerImplTreeHostClient fake_client_;
41 };
42
43 class TextureLayerTest : public testing::Test {
44 public:
45 TextureLayerTest() : host_impl_(&proxy_) {}
46
47 protected:
48 virtual void SetUp() {
49 layer_tree_host_.reset(new MockLayerImplTreeHost);
50 }
51
52 virtual void TearDown() {
53 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
54 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber());
55 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
56
57 layer_tree_host_->SetRootLayer(NULL);
58 layer_tree_host_.reset();
59 }
60
61 scoped_ptr<MockLayerImplTreeHost> layer_tree_host_;
62 FakeImplProxy proxy_;
63 FakeLayerTreeHostImpl host_impl_;
64 };
65
66 TEST_F(TextureLayerTest, SyncImplWhenChangingTextureId) {
67 scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL);
68 ASSERT_TRUE(test_layer);
69
70 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber());
71 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
72 layer_tree_host_->SetRootLayer(test_layer);
73 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
74 EXPECT_EQ(test_layer->layer_tree_host(), layer_tree_host_.get());
75
76 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
77 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
78 test_layer->SetTextureId(1);
79 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
80
81 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AtLeast(1));
82 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
83 test_layer->SetTextureId(2);
84 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
85
86 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AtLeast(1));
87 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
88 test_layer->SetTextureId(0);
89 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
90 }
91
92 TEST_F(TextureLayerTest, SyncImplWhenDrawing) {
93 gfx::RectF dirty_rect(0.f, 0.f, 1.f, 1.f);
94
95 scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL);
96 ASSERT_TRUE(test_layer);
97 scoped_ptr<TextureLayerImpl> impl_layer;
98 impl_layer = TextureLayerImpl::Create(host_impl_.active_tree(), 1, false);
99 ASSERT_TRUE(impl_layer);
100
101 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber());
102 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
103 layer_tree_host_->SetRootLayer(test_layer);
104 test_layer->SetTextureId(1);
105 test_layer->SetIsDrawable(true);
106 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
107 EXPECT_EQ(test_layer->layer_tree_host(), layer_tree_host_.get());
108
109 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(1);
110 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
111 test_layer->WillModifyTexture();
112 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
113
114 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
115 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(1);
116 test_layer->SetNeedsDisplayRect(dirty_rect);
117 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
118
119 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
120 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(1);
121 test_layer->PushPropertiesTo(impl_layer.get()); // fake commit
122 test_layer->SetIsDrawable(false);
123 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
124
125 // Verify that non-drawable layers don't signal the compositor,
126 // except for the first draw after last commit, which must acquire
127 // the texture.
128 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(1);
129 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
130 test_layer->WillModifyTexture();
131 test_layer->SetNeedsDisplayRect(dirty_rect);
132 test_layer->PushPropertiesTo(impl_layer.get()); // fake commit
133 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
134
135 // Second draw with layer in non-drawable state: no texture
136 // acquisition.
137 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
138 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
139 test_layer->WillModifyTexture();
140 test_layer->SetNeedsDisplayRect(dirty_rect);
141 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
142 }
143
144 TEST_F(TextureLayerTest, SyncImplWhenRemovingFromTree) {
145 scoped_refptr<Layer> root_layer = Layer::Create();
146 ASSERT_TRUE(root_layer);
147 scoped_refptr<Layer> child_layer = Layer::Create();
148 ASSERT_TRUE(child_layer);
149 root_layer->AddChild(child_layer);
150 scoped_refptr<TextureLayer> test_layer = TextureLayer::Create(NULL);
151 ASSERT_TRUE(test_layer);
152 test_layer->SetTextureId(0);
153 child_layer->AddChild(test_layer);
154
155 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AnyNumber());
156 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
157 layer_tree_host_->SetRootLayer(root_layer);
158 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
159
160 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
161 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
162 test_layer->RemoveFromParent();
163 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
164
165 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
166 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
167 child_layer->AddChild(test_layer);
168 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
169
170 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
171 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
172 test_layer->SetTextureId(1);
173 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
174
175 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(AtLeast(1));
176 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
177 test_layer->RemoveFromParent();
178 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
179 }
180
181 class MockMailboxCallback {
182 public:
183 MOCK_METHOD2(Release, void(const std::string& mailbox, unsigned sync_point));
184 };
185
186 struct CommonMailboxObjects {
187 CommonMailboxObjects()
188 : mailbox_name1_(64, '1'),
189 mailbox_name2_(64, '2'),
190 sync_point1_(1),
191 sync_point2_(2) {
192 release_mailbox1_ = base::Bind(&MockMailboxCallback::Release,
193 base::Unretained(&mock_callback_),
194 mailbox_name1_);
195 release_mailbox2_ = base::Bind(&MockMailboxCallback::Release,
196 base::Unretained(&mock_callback_),
197 mailbox_name2_);
198 gpu::Mailbox m1;
199 m1.SetName(reinterpret_cast<const int8*>(mailbox_name1_.data()));
200 mailbox1_ = TextureMailbox(m1, release_mailbox1_, sync_point1_);
201 gpu::Mailbox m2;
202 m2.SetName(reinterpret_cast<const int8*>(mailbox_name2_.data()));
203 mailbox2_ = TextureMailbox(m2, release_mailbox2_, sync_point2_);
204 }
205
206 std::string mailbox_name1_;
207 std::string mailbox_name2_;
208 MockMailboxCallback mock_callback_;
209 TextureMailbox::ReleaseCallback release_mailbox1_;
210 TextureMailbox::ReleaseCallback release_mailbox2_;
211 TextureMailbox mailbox1_;
212 TextureMailbox mailbox2_;
213 unsigned sync_point1_;
214 unsigned sync_point2_;
215 };
216
217 class TextureLayerWithMailboxTest : public TextureLayerTest {
218 protected:
219 virtual void TearDown() {
220 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
221 EXPECT_CALL(test_data_.mock_callback_,
222 Release(test_data_.mailbox_name1_,
223 test_data_.sync_point1_)).Times(1);
224 TextureLayerTest::TearDown();
225 }
226
227 CommonMailboxObjects test_data_;
228 };
229
230 TEST_F(TextureLayerWithMailboxTest, ReplaceMailboxOnMainThreadBeforeCommit) {
231 scoped_refptr<TextureLayer> test_layer = TextureLayer::CreateForMailbox();
232 ASSERT_TRUE(test_layer);
233
234 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
235 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
236 layer_tree_host_->SetRootLayer(test_layer);
237 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
238
239 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
240 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
241 test_layer->SetTextureMailbox(test_data_.mailbox1_);
242 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
243
244 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
245 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
246 EXPECT_CALL(test_data_.mock_callback_,
247 Release(test_data_.mailbox_name1_, test_data_.sync_point1_))
248 .Times(1);
249 test_layer->SetTextureMailbox(test_data_.mailbox2_);
250 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
251 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
252
253 EXPECT_CALL(*layer_tree_host_, AcquireLayerTextures()).Times(0);
254 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
255 EXPECT_CALL(test_data_.mock_callback_,
256 Release(test_data_.mailbox_name2_, test_data_.sync_point2_))
257 .Times(1);
258 test_layer->SetTextureMailbox(TextureMailbox());
259 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
260 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
261
262 // Test destructor.
263 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AtLeast(1));
264 test_layer->SetTextureMailbox(test_data_.mailbox1_);
265 }
266
267 class TextureLayerImplWithMailboxThreadedCallback : public ThreadedTest {
268 public:
269 TextureLayerImplWithMailboxThreadedCallback()
270 : callback_count_(0),
271 commit_count_(0) {}
272
273 // Make sure callback is received on main and doesn't block the impl thread.
274 void ReleaseCallback(unsigned sync_point) {
275 EXPECT_EQ(true, proxy()->IsMainThread());
276 ++callback_count_;
277 }
278
279 void SetMailbox(char mailbox_char) {
280 TextureMailbox mailbox(
281 std::string(64, mailbox_char),
282 base::Bind(
283 &TextureLayerImplWithMailboxThreadedCallback::ReleaseCallback,
284 base::Unretained(this)));
285 layer_->SetTextureMailbox(mailbox);
286 }
287
288 virtual void beginTest() OVERRIDE {
289 gfx::Size bounds(100, 100);
290 root_ = Layer::Create();
291 root_->SetAnchorPoint(gfx::PointF());
292 root_->SetBounds(bounds);
293
294 layer_ = TextureLayer::CreateForMailbox();
295 layer_->SetIsDrawable(true);
296 layer_->SetAnchorPoint(gfx::PointF());
297 layer_->SetBounds(bounds);
298
299 root_->AddChild(layer_);
300 m_layerTreeHost->SetRootLayer(root_);
301 m_layerTreeHost->SetViewportSize(bounds, bounds);
302 SetMailbox('1');
303 EXPECT_EQ(0, callback_count_);
304
305 // Case #1: change mailbox before the commit. The old mailbox should be
306 // released immediately.
307 SetMailbox('2');
308 EXPECT_EQ(1, callback_count_);
309 postSetNeedsCommitToMainThread();
310 }
311
312 virtual void didCommit() OVERRIDE {
313 ++commit_count_;
314 switch (commit_count_) {
315 case 1:
316 // Case #2: change mailbox after the commit (and draw), where the
317 // layer draws. The old mailbox should be released during the next
318 // commit.
319 SetMailbox('3');
320 EXPECT_EQ(1, callback_count_);
321 break;
322 case 2:
323 // Old mailbox was released, task was posted, but won't execute
324 // until this didCommit returns.
325 // TODO(piman): fix this.
326 EXPECT_EQ(1, callback_count_);
327 m_layerTreeHost->SetNeedsCommit();
328 break;
329 case 3:
330 EXPECT_EQ(2, callback_count_);
331 // Case #3: change mailbox when the layer doesn't draw. The old
332 // mailbox should be released during the next commit.
333 layer_->SetBounds(gfx::Size());
334 SetMailbox('4');
335 break;
336 case 4:
337 // Old mailbox was released, task was posted, but won't execute
338 // until this didCommit returns.
339 // TODO(piman): fix this.
340 EXPECT_EQ(2, callback_count_);
341 m_layerTreeHost->SetNeedsCommit();
342 break;
343 case 5:
344 EXPECT_EQ(3, callback_count_);
345 // Case #4: release mailbox that was committed but never drawn. The
346 // old mailbox should be released during the next commit.
347 layer_->SetTextureMailbox(TextureMailbox());
348 break;
349 case 6:
350 // Old mailbox was released, task was posted, but won't execute
351 // until this didCommit returns.
352 // TODO(piman): fix this.
353 EXPECT_EQ(3, callback_count_);
354 m_layerTreeHost->SetNeedsCommit();
355 break;
356 case 7:
357 EXPECT_EQ(4, callback_count_);
358 endTest();
359 break;
360 default:
361 NOTREACHED();
362 break;
363 }
364 }
365
366 virtual void afterTest() OVERRIDE {}
367
368 private:
369 int callback_count_;
370 int commit_count_;
371 scoped_refptr<Layer> root_;
372 scoped_refptr<TextureLayer> layer_;
373 };
374
375 SINGLE_AND_MULTI_THREAD_TEST_F(TextureLayerImplWithMailboxThreadedCallback);
376
377 class TextureLayerImplWithMailboxTest : public TextureLayerTest {
378 protected:
379 virtual void SetUp() {
380 TextureLayerTest::SetUp();
381 layer_tree_host_.reset(new MockLayerImplTreeHost);
382 EXPECT_TRUE(host_impl_.InitializeRenderer(createFakeOutputSurface()));
383 }
384
385 CommonMailboxObjects test_data_;
386 };
387
388 TEST_F(TextureLayerImplWithMailboxTest, TestImplLayerCallbacks) {
389 host_impl_.CreatePendingTree();
390 scoped_ptr<TextureLayerImpl> pending_layer;
391 pending_layer = TextureLayerImpl::Create(host_impl_.pending_tree(), 1, true);
392 ASSERT_TRUE(pending_layer);
393
394 scoped_ptr<LayerImpl> activeLayer(
395 pending_layer->CreateLayerImpl(host_impl_.active_tree()));
396 ASSERT_TRUE(activeLayer);
397
398 pending_layer->SetTextureMailbox(test_data_.mailbox1_);
399
400 // Test multiple commits without an activation.
401 EXPECT_CALL(test_data_.mock_callback_,
402 Release(test_data_.mailbox_name1_, test_data_.sync_point1_))
403 .Times(1);
404 pending_layer->SetTextureMailbox(test_data_.mailbox2_);
405 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
406
407 // Test callback after activation.
408 pending_layer->PushPropertiesTo(activeLayer.get());
409 activeLayer->DidBecomeActive();
410
411 EXPECT_CALL(test_data_.mock_callback_, Release(_, _)).Times(0);
412 pending_layer->SetTextureMailbox(test_data_.mailbox1_);
413 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
414
415 EXPECT_CALL(test_data_.mock_callback_, Release(test_data_.mailbox_name2_, _))
416 .Times(1);
417 pending_layer->PushPropertiesTo(activeLayer.get());
418 activeLayer->DidBecomeActive();
419 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
420
421 // Test resetting the mailbox.
422 EXPECT_CALL(test_data_.mock_callback_, Release(test_data_.mailbox_name1_, _))
423 .Times(1);
424 pending_layer->SetTextureMailbox(TextureMailbox());
425 pending_layer->PushPropertiesTo(activeLayer.get());
426 activeLayer->DidBecomeActive();
427 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
428
429 // Test destructor.
430 EXPECT_CALL(test_data_.mock_callback_,
431 Release(test_data_.mailbox_name1_, test_data_.sync_point1_))
432 .Times(1);
433 pending_layer->SetTextureMailbox(test_data_.mailbox1_);
434 }
435
436 TEST_F(TextureLayerImplWithMailboxTest,
437 TestDestructorCallbackOnCreatedResource) {
438 scoped_ptr<TextureLayerImpl> impl_layer;
439 impl_layer = TextureLayerImpl::Create(host_impl_.active_tree(), 1, true);
440 ASSERT_TRUE(impl_layer);
441
442 EXPECT_CALL(test_data_.mock_callback_, Release(test_data_.mailbox_name1_, _))
443 .Times(1);
444 impl_layer->SetTextureMailbox(test_data_.mailbox1_);
445 impl_layer->WillDraw(host_impl_.active_tree()->resource_provider());
446 impl_layer->DidDraw(host_impl_.active_tree()->resource_provider());
447 impl_layer->SetTextureMailbox(TextureMailbox());
448 }
449
450 TEST_F(TextureLayerImplWithMailboxTest, TestCallbackOnInUseResource) {
451 ResourceProvider* provider = host_impl_.active_tree()->resource_provider();
452 ResourceProvider::ResourceId id =
453 provider->CreateResourceFromTextureMailbox(test_data_.mailbox1_);
454 provider->AllocateForTesting(id);
455
456 // Transfer some resources to the parent.
457 ResourceProvider::ResourceIdArray resource_ids_to_transfer;
458 resource_ids_to_transfer.push_back(id);
459 TransferableResourceArray list;
460 provider->PrepareSendToParent(resource_ids_to_transfer, &list);
461 EXPECT_TRUE(provider->InUseByConsumer(id));
462 EXPECT_CALL(test_data_.mock_callback_, Release(_, _)).Times(0);
463 provider->DeleteResource(id);
464 Mock::VerifyAndClearExpectations(&test_data_.mock_callback_);
465 EXPECT_CALL(test_data_.mock_callback_, Release(test_data_.mailbox_name1_, _))
466 .Times(1);
467 provider->ReceiveFromParent(list);
468 }
469
470 } // namespace
471 } // namespace cc
OLDNEW
« no previous file with comments | « cc/texture_layer_impl.cc ('k') | cc/tiled_layer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698