Fix VPN dialog password field focus
[chromium-blink-merge.git] / cc / trees / layer_tree_host_unittest_copyrequest.cc
blobb3725d4048619ad657ca75ddd81e52c4d895e402
1 // Copyright 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.
5 #include "cc/layers/layer_iterator.h"
6 #include "cc/output/copy_output_request.h"
7 #include "cc/output/copy_output_result.h"
8 #include "cc/test/fake_content_layer.h"
9 #include "cc/test/fake_content_layer_client.h"
10 #include "cc/test/fake_output_surface.h"
11 #include "cc/test/layer_tree_test.h"
12 #include "cc/trees/layer_tree_impl.h"
13 #include "gpu/GLES2/gl2extchromium.h"
15 namespace cc {
16 namespace {
18 // These tests only use direct rendering, as there is no output to copy for
19 // delegated renderers.
20 class LayerTreeHostCopyRequestTest : public LayerTreeTest {};
22 class LayerTreeHostCopyRequestTestMultipleRequests
23 : public LayerTreeHostCopyRequestTest {
24 protected:
25 void SetupTree() override {
26 root = FakeContentLayer::Create(layer_settings(), &client_);
27 root->SetBounds(gfx::Size(20, 20));
29 child = FakeContentLayer::Create(layer_settings(), &client_);
30 child->SetBounds(gfx::Size(10, 10));
31 root->AddChild(child);
33 layer_tree_host()->SetRootLayer(root);
34 LayerTreeHostCopyRequestTest::SetupTree();
37 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
39 void DidCommitAndDrawFrame() override { WaitForCallback(); }
41 void WaitForCallback() {
42 base::MessageLoop::current()->PostTask(
43 FROM_HERE,
44 base::Bind(&LayerTreeHostCopyRequestTestMultipleRequests::NextStep,
45 base::Unretained(this)));
48 void NextStep() {
49 int frame = layer_tree_host()->source_frame_number();
50 switch (frame) {
51 case 1:
52 child->RequestCopyOfOutput(CopyOutputRequest::CreateBitmapRequest(
53 base::Bind(&LayerTreeHostCopyRequestTestMultipleRequests::
54 CopyOutputCallback,
55 base::Unretained(this))));
56 EXPECT_EQ(0u, callbacks_.size());
57 break;
58 case 2:
59 if (callbacks_.size() < 1u) {
60 WaitForCallback();
61 return;
63 EXPECT_EQ(1u, callbacks_.size());
64 EXPECT_EQ(gfx::Size(10, 10).ToString(), callbacks_[0].ToString());
66 child->RequestCopyOfOutput(CopyOutputRequest::CreateBitmapRequest(
67 base::Bind(&LayerTreeHostCopyRequestTestMultipleRequests::
68 CopyOutputCallback,
69 base::Unretained(this))));
70 root->RequestCopyOfOutput(CopyOutputRequest::CreateBitmapRequest(
71 base::Bind(&LayerTreeHostCopyRequestTestMultipleRequests::
72 CopyOutputCallback,
73 base::Unretained(this))));
74 child->RequestCopyOfOutput(CopyOutputRequest::CreateBitmapRequest(
75 base::Bind(&LayerTreeHostCopyRequestTestMultipleRequests::
76 CopyOutputCallback,
77 base::Unretained(this))));
78 EXPECT_EQ(1u, callbacks_.size());
79 break;
80 case 3:
81 if (callbacks_.size() < 4u) {
82 WaitForCallback();
83 return;
85 EXPECT_EQ(4u, callbacks_.size());
86 // The child was copied to a bitmap and passed back twice.
87 EXPECT_EQ(gfx::Size(10, 10).ToString(), callbacks_[1].ToString());
88 EXPECT_EQ(gfx::Size(10, 10).ToString(), callbacks_[2].ToString());
89 // The root was copied to a bitmap and passed back also.
90 EXPECT_EQ(gfx::Size(20, 20).ToString(), callbacks_[3].ToString());
91 EndTest();
92 break;
96 void CopyOutputCallback(scoped_ptr<CopyOutputResult> result) {
97 EXPECT_TRUE(layer_tree_host()->proxy()->IsMainThread());
98 EXPECT_TRUE(result->HasBitmap());
99 scoped_ptr<SkBitmap> bitmap = result->TakeBitmap().Pass();
100 EXPECT_EQ(result->size().ToString(),
101 gfx::Size(bitmap->width(), bitmap->height()).ToString());
102 callbacks_.push_back(result->size());
105 void AfterTest() override { EXPECT_EQ(4u, callbacks_.size()); }
107 scoped_ptr<FakeOutputSurface> CreateFakeOutputSurface() override {
108 if (use_gl_renderer_)
109 return FakeOutputSurface::Create3d();
110 return FakeOutputSurface::CreateSoftware(
111 make_scoped_ptr(new SoftwareOutputDevice));
114 bool use_gl_renderer_;
115 std::vector<gfx::Size> callbacks_;
116 FakeContentLayerClient client_;
117 scoped_refptr<FakeContentLayer> root;
118 scoped_refptr<FakeContentLayer> child;
121 // Readback can't be done with a delegating renderer.
122 // Disabled due to flake: http://crbug.com/448521
123 TEST_F(LayerTreeHostCopyRequestTestMultipleRequests,
124 DISABLED_GLRenderer_RunSingleThread) {
125 use_gl_renderer_ = true;
126 RunTest(false, false, false);
129 TEST_F(LayerTreeHostCopyRequestTestMultipleRequests,
130 GLRenderer_RunMultiThread_MainThreadPainting) {
131 use_gl_renderer_ = true;
132 RunTest(true, false, false);
135 TEST_F(LayerTreeHostCopyRequestTestMultipleRequests,
136 SoftwareRenderer_RunSingleThread) {
137 use_gl_renderer_ = false;
138 RunTest(false, false, false);
141 TEST_F(LayerTreeHostCopyRequestTestMultipleRequests,
142 SoftwareRenderer_RunMultiThread_MainThreadPainting) {
143 use_gl_renderer_ = false;
144 RunTest(true, false, false);
147 class LayerTreeHostCopyRequestTestLayerDestroyed
148 : public LayerTreeHostCopyRequestTest {
149 protected:
150 void SetupTree() override {
151 root_ = FakeContentLayer::Create(layer_settings(), &client_);
152 root_->SetBounds(gfx::Size(20, 20));
154 main_destroyed_ = FakeContentLayer::Create(layer_settings(), &client_);
155 main_destroyed_->SetBounds(gfx::Size(15, 15));
156 root_->AddChild(main_destroyed_);
158 impl_destroyed_ = FakeContentLayer::Create(layer_settings(), &client_);
159 impl_destroyed_->SetBounds(gfx::Size(10, 10));
160 root_->AddChild(impl_destroyed_);
162 layer_tree_host()->SetRootLayer(root_);
163 LayerTreeHostCopyRequestTest::SetupTree();
166 void BeginTest() override {
167 callback_count_ = 0;
168 PostSetNeedsCommitToMainThread();
171 void DidCommit() override {
172 int frame = layer_tree_host()->source_frame_number();
173 switch (frame) {
174 case 1:
175 main_destroyed_->RequestCopyOfOutput(
176 CopyOutputRequest::CreateBitmapRequest(base::Bind(
177 &LayerTreeHostCopyRequestTestLayerDestroyed::CopyOutputCallback,
178 base::Unretained(this))));
179 impl_destroyed_->RequestCopyOfOutput(
180 CopyOutputRequest::CreateBitmapRequest(base::Bind(
181 &LayerTreeHostCopyRequestTestLayerDestroyed::CopyOutputCallback,
182 base::Unretained(this))));
183 EXPECT_EQ(0, callback_count_);
185 // Destroy the main thread layer right away.
186 main_destroyed_->RemoveFromParent();
187 main_destroyed_ = NULL;
189 // Should callback with a NULL bitmap.
190 EXPECT_EQ(1, callback_count_);
192 // Prevent drawing so we can't make a copy of the impl_destroyed layer.
193 layer_tree_host()->SetViewportSize(gfx::Size());
194 break;
195 case 2:
196 // Flush the message loops and make sure the callbacks run.
197 layer_tree_host()->SetNeedsCommit();
198 break;
199 case 3:
200 // No drawing means no readback yet.
201 EXPECT_EQ(1, callback_count_);
203 // Destroy the impl thread layer.
204 impl_destroyed_->RemoveFromParent();
205 impl_destroyed_ = NULL;
207 // No callback yet because it's on the impl side.
208 EXPECT_EQ(1, callback_count_);
209 break;
210 case 4:
211 // Flush the message loops and make sure the callbacks run.
212 layer_tree_host()->SetNeedsCommit();
213 break;
214 case 5:
215 // We should get another callback with a NULL bitmap.
216 EXPECT_EQ(2, callback_count_);
217 EndTest();
218 break;
222 void CopyOutputCallback(scoped_ptr<CopyOutputResult> result) {
223 EXPECT_TRUE(layer_tree_host()->proxy()->IsMainThread());
224 EXPECT_TRUE(result->IsEmpty());
225 ++callback_count_;
228 void AfterTest() override {}
230 int callback_count_;
231 FakeContentLayerClient client_;
232 scoped_refptr<FakeContentLayer> root_;
233 scoped_refptr<FakeContentLayer> main_destroyed_;
234 scoped_refptr<FakeContentLayer> impl_destroyed_;
237 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostCopyRequestTestLayerDestroyed);
239 class LayerTreeHostCopyRequestTestInHiddenSubtree
240 : public LayerTreeHostCopyRequestTest {
241 protected:
242 void SetupTree() override {
243 root_ = FakeContentLayer::Create(layer_settings(), &client_);
244 root_->SetBounds(gfx::Size(20, 20));
246 grand_parent_layer_ = FakeContentLayer::Create(layer_settings(), &client_);
247 grand_parent_layer_->SetBounds(gfx::Size(15, 15));
248 root_->AddChild(grand_parent_layer_);
250 // parent_layer_ owns a render surface.
251 parent_layer_ = FakeContentLayer::Create(layer_settings(), &client_);
252 parent_layer_->SetBounds(gfx::Size(15, 15));
253 parent_layer_->SetForceRenderSurface(true);
254 grand_parent_layer_->AddChild(parent_layer_);
256 copy_layer_ = FakeContentLayer::Create(layer_settings(), &client_);
257 copy_layer_->SetBounds(gfx::Size(10, 10));
258 parent_layer_->AddChild(copy_layer_);
260 layer_tree_host()->SetRootLayer(root_);
261 LayerTreeHostCopyRequestTest::SetupTree();
264 void AddCopyRequest(Layer* layer) {
265 layer->RequestCopyOfOutput(
266 CopyOutputRequest::CreateBitmapRequest(base::Bind(
267 &LayerTreeHostCopyRequestTestInHiddenSubtree::CopyOutputCallback,
268 base::Unretained(this))));
271 void BeginTest() override {
272 callback_count_ = 0;
273 PostSetNeedsCommitToMainThread();
275 AddCopyRequest(copy_layer_.get());
278 void CopyOutputCallback(scoped_ptr<CopyOutputResult> result) {
279 ++callback_count_;
280 EXPECT_TRUE(layer_tree_host()->proxy()->IsMainThread());
281 EXPECT_EQ(copy_layer_->bounds().ToString(), result->size().ToString())
282 << callback_count_;
283 switch (callback_count_) {
284 case 1:
285 // Hide the copy request layer.
286 grand_parent_layer_->SetHideLayerAndSubtree(false);
287 parent_layer_->SetHideLayerAndSubtree(false);
288 copy_layer_->SetHideLayerAndSubtree(true);
289 AddCopyRequest(copy_layer_.get());
290 break;
291 case 2:
292 // Hide the copy request layer's parent only.
293 grand_parent_layer_->SetHideLayerAndSubtree(false);
294 parent_layer_->SetHideLayerAndSubtree(true);
295 copy_layer_->SetHideLayerAndSubtree(false);
296 AddCopyRequest(copy_layer_.get());
297 break;
298 case 3:
299 // Hide the copy request layer's grand parent only.
300 grand_parent_layer_->SetHideLayerAndSubtree(true);
301 parent_layer_->SetHideLayerAndSubtree(false);
302 copy_layer_->SetHideLayerAndSubtree(false);
303 AddCopyRequest(copy_layer_.get());
304 break;
305 case 4:
306 // Hide the copy request layer's parent and grandparent.
307 grand_parent_layer_->SetHideLayerAndSubtree(true);
308 parent_layer_->SetHideLayerAndSubtree(true);
309 copy_layer_->SetHideLayerAndSubtree(false);
310 AddCopyRequest(copy_layer_.get());
311 break;
312 case 5:
313 // Hide the copy request layer as well as its parent and grandparent.
314 grand_parent_layer_->SetHideLayerAndSubtree(true);
315 parent_layer_->SetHideLayerAndSubtree(true);
316 copy_layer_->SetHideLayerAndSubtree(true);
317 AddCopyRequest(copy_layer_.get());
318 break;
319 case 6:
320 EndTest();
321 break;
325 void AfterTest() override {}
327 int callback_count_;
328 FakeContentLayerClient client_;
329 scoped_refptr<FakeContentLayer> root_;
330 scoped_refptr<FakeContentLayer> grand_parent_layer_;
331 scoped_refptr<FakeContentLayer> parent_layer_;
332 scoped_refptr<FakeContentLayer> copy_layer_;
335 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(
336 LayerTreeHostCopyRequestTestInHiddenSubtree);
338 class LayerTreeHostTestHiddenSurfaceNotAllocatedForSubtreeCopyRequest
339 : public LayerTreeHostCopyRequestTest {
340 protected:
341 void SetupTree() override {
342 root_ = FakeContentLayer::Create(layer_settings(), &client_);
343 root_->SetBounds(gfx::Size(20, 20));
345 grand_parent_layer_ = FakeContentLayer::Create(layer_settings(), &client_);
346 grand_parent_layer_->SetBounds(gfx::Size(15, 15));
347 grand_parent_layer_->SetHideLayerAndSubtree(true);
348 root_->AddChild(grand_parent_layer_);
350 // parent_layer_ owns a render surface.
351 parent_layer_ = FakeContentLayer::Create(layer_settings(), &client_);
352 parent_layer_->SetBounds(gfx::Size(15, 15));
353 parent_layer_->SetForceRenderSurface(true);
354 grand_parent_layer_->AddChild(parent_layer_);
356 copy_layer_ = FakeContentLayer::Create(layer_settings(), &client_);
357 copy_layer_->SetBounds(gfx::Size(10, 10));
358 parent_layer_->AddChild(copy_layer_);
360 layer_tree_host()->SetRootLayer(root_);
361 LayerTreeHostCopyRequestTest::SetupTree();
364 void BeginTest() override {
365 did_draw_ = false;
366 PostSetNeedsCommitToMainThread();
368 copy_layer_->RequestCopyOfOutput(
369 CopyOutputRequest::CreateBitmapRequest(base::Bind(
370 &LayerTreeHostTestHiddenSurfaceNotAllocatedForSubtreeCopyRequest::
371 CopyOutputCallback,
372 base::Unretained(this))));
375 void CopyOutputCallback(scoped_ptr<CopyOutputResult> result) {
376 EXPECT_TRUE(layer_tree_host()->proxy()->IsMainThread());
377 EXPECT_EQ(copy_layer_->bounds().ToString(), result->size().ToString());
378 EndTest();
381 void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override {
382 Renderer* renderer = host_impl->renderer();
384 LayerImpl* root = host_impl->active_tree()->root_layer();
385 LayerImpl* grand_parent = root->children()[0];
386 LayerImpl* parent = grand_parent->children()[0];
387 LayerImpl* copy_layer = parent->children()[0];
389 // |parent| owns a surface, but it was hidden and not part of the copy
390 // request so it should not allocate any resource.
391 EXPECT_FALSE(renderer->HasAllocatedResourcesForTesting(
392 parent->render_surface()->GetRenderPassId()));
394 // |copy_layer| should have been rendered to a texture since it was needed
395 // for a copy request.
396 EXPECT_TRUE(renderer->HasAllocatedResourcesForTesting(
397 copy_layer->render_surface()->GetRenderPassId()));
399 did_draw_ = true;
402 void AfterTest() override { EXPECT_TRUE(did_draw_); }
404 FakeContentLayerClient client_;
405 bool did_draw_;
406 scoped_refptr<FakeContentLayer> root_;
407 scoped_refptr<FakeContentLayer> grand_parent_layer_;
408 scoped_refptr<FakeContentLayer> parent_layer_;
409 scoped_refptr<FakeContentLayer> copy_layer_;
412 // No output to copy for delegated renderers.
413 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(
414 LayerTreeHostTestHiddenSurfaceNotAllocatedForSubtreeCopyRequest);
416 class LayerTreeHostCopyRequestTestClippedOut
417 : public LayerTreeHostCopyRequestTest {
418 protected:
419 void SetupTree() override {
420 root_ = FakeContentLayer::Create(layer_settings(), &client_);
421 root_->SetBounds(gfx::Size(20, 20));
423 parent_layer_ = FakeContentLayer::Create(layer_settings(), &client_);
424 parent_layer_->SetBounds(gfx::Size(15, 15));
425 parent_layer_->SetMasksToBounds(true);
426 root_->AddChild(parent_layer_);
428 copy_layer_ = FakeContentLayer::Create(layer_settings(), &client_);
429 copy_layer_->SetPosition(gfx::Point(15, 15));
430 copy_layer_->SetBounds(gfx::Size(10, 10));
431 parent_layer_->AddChild(copy_layer_);
433 layer_tree_host()->SetRootLayer(root_);
434 LayerTreeHostCopyRequestTest::SetupTree();
437 void BeginTest() override {
438 PostSetNeedsCommitToMainThread();
440 copy_layer_->RequestCopyOfOutput(CopyOutputRequest::CreateBitmapRequest(
441 base::Bind(&LayerTreeHostCopyRequestTestClippedOut::CopyOutputCallback,
442 base::Unretained(this))));
445 void CopyOutputCallback(scoped_ptr<CopyOutputResult> result) {
446 // We should still get a callback with no output if the copy requested layer
447 // was completely clipped away.
448 EXPECT_TRUE(layer_tree_host()->proxy()->IsMainThread());
449 EXPECT_EQ(gfx::Size().ToString(), result->size().ToString());
450 EndTest();
453 void AfterTest() override {}
455 FakeContentLayerClient client_;
456 scoped_refptr<FakeContentLayer> root_;
457 scoped_refptr<FakeContentLayer> parent_layer_;
458 scoped_refptr<FakeContentLayer> copy_layer_;
461 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(
462 LayerTreeHostCopyRequestTestClippedOut);
464 class LayerTreeHostTestAsyncTwoReadbacksWithoutDraw
465 : public LayerTreeHostCopyRequestTest {
466 protected:
467 void SetupTree() override {
468 root_ = FakeContentLayer::Create(layer_settings(), &client_);
469 root_->SetBounds(gfx::Size(20, 20));
471 copy_layer_ = FakeContentLayer::Create(layer_settings(), &client_);
472 copy_layer_->SetBounds(gfx::Size(10, 10));
473 root_->AddChild(copy_layer_);
475 layer_tree_host()->SetRootLayer(root_);
476 LayerTreeHostCopyRequestTest::SetupTree();
479 void AddCopyRequest(Layer* layer) {
480 layer->RequestCopyOfOutput(
481 CopyOutputRequest::CreateBitmapRequest(base::Bind(
482 &LayerTreeHostTestAsyncTwoReadbacksWithoutDraw::CopyOutputCallback,
483 base::Unretained(this))));
486 void BeginTest() override {
487 saw_copy_request_ = false;
488 callback_count_ = 0;
489 PostSetNeedsCommitToMainThread();
491 // Prevent drawing.
492 layer_tree_host()->SetViewportSize(gfx::Size(0, 0));
494 AddCopyRequest(copy_layer_.get());
497 void DidActivateTreeOnThread(LayerTreeHostImpl* impl) override {
498 if (impl->active_tree()->source_frame_number() == 0) {
499 LayerImpl* root = impl->active_tree()->root_layer();
500 EXPECT_TRUE(root->children()[0]->HasCopyRequest());
501 saw_copy_request_ = true;
505 void DidCommit() override {
506 if (layer_tree_host()->source_frame_number() == 1) {
507 // Allow drawing.
508 layer_tree_host()->SetViewportSize(gfx::Size(root_->bounds()));
510 AddCopyRequest(copy_layer_.get());
514 void CopyOutputCallback(scoped_ptr<CopyOutputResult> result) {
515 EXPECT_TRUE(layer_tree_host()->proxy()->IsMainThread());
516 EXPECT_EQ(copy_layer_->bounds().ToString(), result->size().ToString());
517 ++callback_count_;
519 if (callback_count_ == 2)
520 EndTest();
523 void AfterTest() override { EXPECT_TRUE(saw_copy_request_); }
525 bool saw_copy_request_;
526 int callback_count_;
527 FakeContentLayerClient client_;
528 scoped_refptr<FakeContentLayer> root_;
529 scoped_refptr<FakeContentLayer> copy_layer_;
532 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(
533 LayerTreeHostTestAsyncTwoReadbacksWithoutDraw);
535 class LayerTreeHostCopyRequestTestLostOutputSurface
536 : public LayerTreeHostCopyRequestTest {
537 protected:
538 scoped_ptr<FakeOutputSurface> CreateFakeOutputSurface() override {
539 if (!first_context_provider_.get()) {
540 first_context_provider_ = TestContextProvider::Create();
541 return FakeOutputSurface::Create3d(first_context_provider_);
544 EXPECT_FALSE(second_context_provider_.get());
545 second_context_provider_ = TestContextProvider::Create();
546 return FakeOutputSurface::Create3d(second_context_provider_);
549 void SetupTree() override {
550 root_ = FakeContentLayer::Create(layer_settings(), &client_);
551 root_->SetBounds(gfx::Size(20, 20));
553 copy_layer_ = FakeContentLayer::Create(layer_settings(), &client_);
554 copy_layer_->SetBounds(gfx::Size(10, 10));
555 root_->AddChild(copy_layer_);
557 layer_tree_host()->SetRootLayer(root_);
558 LayerTreeHostCopyRequestTest::SetupTree();
561 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
563 void CopyOutputCallback(scoped_ptr<CopyOutputResult> result) {
564 EXPECT_TRUE(layer_tree_host()->proxy()->IsMainThread());
565 EXPECT_EQ(gfx::Size(10, 10).ToString(), result->size().ToString());
566 EXPECT_TRUE(result->HasTexture());
568 // Save the result for later.
569 EXPECT_FALSE(result_);
570 result_ = result.Pass();
572 // Post a commit to lose the output surface.
573 layer_tree_host()->SetNeedsCommit();
576 void DidCommitAndDrawFrame() override {
577 switch (layer_tree_host()->source_frame_number()) {
578 case 1:
579 // The layers have been pushed to the impl side. The layer textures have
580 // been allocated.
582 // Request a copy of the layer. This will use another texture.
583 copy_layer_->RequestCopyOfOutput(CopyOutputRequest::CreateRequest(
584 base::Bind(&LayerTreeHostCopyRequestTestLostOutputSurface::
585 CopyOutputCallback,
586 base::Unretained(this))));
587 break;
588 case 4:
589 // With SingleThreadProxy it takes two commits to finally swap after a
590 // context loss.
591 case 5:
592 // Now destroy the CopyOutputResult, releasing the texture inside back
593 // to the compositor.
594 EXPECT_TRUE(result_);
595 result_ = nullptr;
597 // Check that it is released.
598 ImplThreadTaskRunner()->PostTask(
599 FROM_HERE,
600 base::Bind(&LayerTreeHostCopyRequestTestLostOutputSurface::
601 CheckNumTextures,
602 base::Unretained(this),
603 num_textures_after_loss_ - 1));
604 break;
608 void SwapBuffersOnThread(LayerTreeHostImpl* impl, bool result) override {
609 switch (impl->active_tree()->source_frame_number()) {
610 case 0:
611 // The layers have been drawn, so their textures have been allocated.
612 EXPECT_FALSE(result_);
613 num_textures_without_readback_ =
614 first_context_provider_->TestContext3d()->NumTextures();
615 break;
616 case 1:
617 // We did a readback, so there will be a readback texture around now.
618 EXPECT_LT(num_textures_without_readback_,
619 first_context_provider_->TestContext3d()->NumTextures());
620 break;
621 case 2:
622 // The readback texture is collected.
623 EXPECT_TRUE(result_);
625 // Lose the output surface.
626 first_context_provider_->TestContext3d()->loseContextCHROMIUM(
627 GL_GUILTY_CONTEXT_RESET_ARB, GL_INNOCENT_CONTEXT_RESET_ARB);
628 break;
629 case 3:
630 // With SingleThreadProxy it takes two commits to finally swap after a
631 // context loss.
632 case 4:
633 // The output surface has been recreated.
634 EXPECT_TRUE(second_context_provider_.get());
636 num_textures_after_loss_ =
637 first_context_provider_->TestContext3d()->NumTextures();
638 break;
642 void CheckNumTextures(size_t expected_num_textures) {
643 EXPECT_EQ(expected_num_textures,
644 first_context_provider_->TestContext3d()->NumTextures());
645 EndTest();
648 void AfterTest() override {}
650 scoped_refptr<TestContextProvider> first_context_provider_;
651 scoped_refptr<TestContextProvider> second_context_provider_;
652 size_t num_textures_without_readback_;
653 size_t num_textures_after_loss_;
654 FakeContentLayerClient client_;
655 scoped_refptr<FakeContentLayer> root_;
656 scoped_refptr<FakeContentLayer> copy_layer_;
657 scoped_ptr<CopyOutputResult> result_;
660 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(
661 LayerTreeHostCopyRequestTestLostOutputSurface);
663 class LayerTreeHostCopyRequestTestCountTextures
664 : public LayerTreeHostCopyRequestTest {
665 protected:
666 scoped_ptr<FakeOutputSurface> CreateFakeOutputSurface() override {
667 context_provider_ = TestContextProvider::Create();
668 return FakeOutputSurface::Create3d(context_provider_);
671 void SetupTree() override {
672 root_ = FakeContentLayer::Create(layer_settings(), &client_);
673 root_->SetBounds(gfx::Size(20, 20));
675 copy_layer_ = FakeContentLayer::Create(layer_settings(), &client_);
676 copy_layer_->SetBounds(gfx::Size(10, 10));
677 root_->AddChild(copy_layer_);
679 layer_tree_host()->SetRootLayer(root_);
680 LayerTreeHostCopyRequestTest::SetupTree();
683 void BeginTest() override {
684 num_textures_without_readback_ = 0;
685 num_textures_with_readback_ = 0;
686 waited_sync_point_after_readback_ = 0;
687 PostSetNeedsCommitToMainThread();
690 virtual void RequestCopy(Layer* layer) = 0;
692 void DidCommitAndDrawFrame() override {
693 switch (layer_tree_host()->source_frame_number()) {
694 case 1:
695 // The layers have been pushed to the impl side. The layer textures have
696 // been allocated.
697 RequestCopy(copy_layer_.get());
698 break;
702 void SwapBuffersOnThread(LayerTreeHostImpl* impl, bool result) override {
703 switch (impl->active_tree()->source_frame_number()) {
704 case 0:
705 // The layers have been drawn, so their textures have been allocated.
706 num_textures_without_readback_ =
707 context_provider_->TestContext3d()->NumTextures();
708 break;
709 case 1:
710 // We did a readback, so there will be a readback texture around now.
711 num_textures_with_readback_ =
712 context_provider_->TestContext3d()->NumTextures();
713 waited_sync_point_after_readback_ =
714 context_provider_->TestContext3d()->last_waited_sync_point();
716 MainThreadTaskRunner()->PostTask(
717 FROM_HERE,
718 base::Bind(&LayerTreeHostCopyRequestTestCountTextures::DoEndTest,
719 base::Unretained(this)));
720 break;
724 virtual void DoEndTest() { EndTest(); }
726 scoped_refptr<TestContextProvider> context_provider_;
727 size_t num_textures_without_readback_;
728 size_t num_textures_with_readback_;
729 unsigned waited_sync_point_after_readback_;
730 FakeContentLayerClient client_;
731 scoped_refptr<FakeContentLayer> root_;
732 scoped_refptr<FakeContentLayer> copy_layer_;
735 class LayerTreeHostCopyRequestTestCreatesTexture
736 : public LayerTreeHostCopyRequestTestCountTextures {
737 protected:
738 void RequestCopy(Layer* layer) override {
739 // Request a normal texture copy. This should create a new texture.
740 copy_layer_->RequestCopyOfOutput(
741 CopyOutputRequest::CreateRequest(base::Bind(
742 &LayerTreeHostCopyRequestTestCreatesTexture::CopyOutputCallback,
743 base::Unretained(this))));
746 void CopyOutputCallback(scoped_ptr<CopyOutputResult> result) {
747 EXPECT_FALSE(result->IsEmpty());
748 EXPECT_TRUE(result->HasTexture());
750 TextureMailbox mailbox;
751 scoped_ptr<SingleReleaseCallback> release;
752 result->TakeTexture(&mailbox, &release);
753 EXPECT_TRUE(release);
755 release->Run(0, false);
758 void AfterTest() override {
759 // No sync point was needed.
760 EXPECT_EQ(0u, waited_sync_point_after_readback_);
761 // Except the copy to have made another texture.
762 EXPECT_EQ(num_textures_without_readback_ + 1, num_textures_with_readback_);
766 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(
767 LayerTreeHostCopyRequestTestCreatesTexture);
769 class LayerTreeHostCopyRequestTestProvideTexture
770 : public LayerTreeHostCopyRequestTestCountTextures {
771 protected:
772 void BeginTest() override {
773 external_context_provider_ = TestContextProvider::Create();
774 EXPECT_TRUE(external_context_provider_->BindToCurrentThread());
775 LayerTreeHostCopyRequestTestCountTextures::BeginTest();
778 void CopyOutputCallback(scoped_ptr<CopyOutputResult> result) {
779 EXPECT_FALSE(result->IsEmpty());
780 EXPECT_TRUE(result->HasTexture());
782 TextureMailbox mailbox;
783 scoped_ptr<SingleReleaseCallback> release;
784 result->TakeTexture(&mailbox, &release);
785 EXPECT_FALSE(release);
788 void RequestCopy(Layer* layer) override {
789 // Request a copy to a provided texture. This should not create a new
790 // texture.
791 scoped_ptr<CopyOutputRequest> request =
792 CopyOutputRequest::CreateRequest(base::Bind(
793 &LayerTreeHostCopyRequestTestProvideTexture::CopyOutputCallback,
794 base::Unretained(this)));
796 gpu::gles2::GLES2Interface* gl = external_context_provider_->ContextGL();
797 gpu::Mailbox mailbox;
798 gl->GenMailboxCHROMIUM(mailbox.name);
799 sync_point_ = gl->InsertSyncPointCHROMIUM();
800 request->SetTextureMailbox(
801 TextureMailbox(mailbox, GL_TEXTURE_2D, sync_point_));
802 EXPECT_TRUE(request->has_texture_mailbox());
804 copy_layer_->RequestCopyOfOutput(request.Pass());
807 void AfterTest() override {
808 // Expect the compositor to have waited for the sync point in the provided
809 // TextureMailbox.
810 EXPECT_EQ(sync_point_, waited_sync_point_after_readback_);
811 // Except the copy to have *not* made another texture.
812 EXPECT_EQ(num_textures_without_readback_, num_textures_with_readback_);
815 scoped_refptr<TestContextProvider> external_context_provider_;
816 unsigned sync_point_;
819 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(
820 LayerTreeHostCopyRequestTestProvideTexture);
822 class LayerTreeHostCopyRequestTestDestroyBeforeCopy
823 : public LayerTreeHostCopyRequestTest {
824 protected:
825 void SetupTree() override {
826 root_ = FakeContentLayer::Create(layer_settings(), &client_);
827 root_->SetBounds(gfx::Size(20, 20));
829 copy_layer_ = FakeContentLayer::Create(layer_settings(), &client_);
830 copy_layer_->SetBounds(gfx::Size(10, 10));
831 root_->AddChild(copy_layer_);
833 layer_tree_host()->SetRootLayer(root_);
834 LayerTreeHostCopyRequestTest::SetupTree();
837 void BeginTest() override {
838 callback_count_ = 0;
839 PostSetNeedsCommitToMainThread();
842 void CopyOutputCallback(scoped_ptr<CopyOutputResult> result) {
843 EXPECT_TRUE(result->IsEmpty());
844 ++callback_count_;
847 void DidActivateTreeOnThread(LayerTreeHostImpl* impl) override {
848 MainThreadTaskRunner()->PostTask(
849 FROM_HERE,
850 base::Bind(&LayerTreeHostCopyRequestTestDestroyBeforeCopy::DidActivate,
851 base::Unretained(this)));
854 void DidActivate() {
855 switch (layer_tree_host()->source_frame_number()) {
856 case 1: {
857 EXPECT_EQ(0, callback_count_);
858 // Put a copy request on the layer, but then don't allow any
859 // drawing to take place.
860 scoped_ptr<CopyOutputRequest> request =
861 CopyOutputRequest::CreateRequest(
862 base::Bind(&LayerTreeHostCopyRequestTestDestroyBeforeCopy::
863 CopyOutputCallback,
864 base::Unretained(this)));
865 copy_layer_->RequestCopyOfOutput(request.Pass());
867 layer_tree_host()->SetViewportSize(gfx::Size());
868 break;
870 case 2:
871 EXPECT_EQ(0, callback_count_);
872 // Remove the copy layer before we were able to draw.
873 copy_layer_->RemoveFromParent();
874 break;
875 case 3:
876 EXPECT_EQ(1, callback_count_);
877 // Allow us to draw now.
878 layer_tree_host()->SetViewportSize(
879 layer_tree_host()->root_layer()->bounds());
880 break;
881 case 4:
882 EXPECT_EQ(1, callback_count_);
883 // We should not have crashed.
884 EndTest();
888 void AfterTest() override {}
890 int callback_count_;
891 FakeContentLayerClient client_;
892 scoped_refptr<FakeContentLayer> root_;
893 scoped_refptr<FakeContentLayer> copy_layer_;
896 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(
897 LayerTreeHostCopyRequestTestDestroyBeforeCopy);
899 class LayerTreeHostCopyRequestTestShutdownBeforeCopy
900 : public LayerTreeHostCopyRequestTest {
901 protected:
902 void SetupTree() override {
903 root_ = FakeContentLayer::Create(layer_settings(), &client_);
904 root_->SetBounds(gfx::Size(20, 20));
906 copy_layer_ = FakeContentLayer::Create(layer_settings(), &client_);
907 copy_layer_->SetBounds(gfx::Size(10, 10));
908 root_->AddChild(copy_layer_);
910 layer_tree_host()->SetRootLayer(root_);
911 LayerTreeHostCopyRequestTest::SetupTree();
914 void BeginTest() override {
915 callback_count_ = 0;
916 PostSetNeedsCommitToMainThread();
919 void CopyOutputCallback(scoped_ptr<CopyOutputResult> result) {
920 EXPECT_TRUE(result->IsEmpty());
921 ++callback_count_;
924 void DidActivateTreeOnThread(LayerTreeHostImpl* impl) override {
925 MainThreadTaskRunner()->PostTask(
926 FROM_HERE,
927 base::Bind(&LayerTreeHostCopyRequestTestShutdownBeforeCopy::DidActivate,
928 base::Unretained(this)));
931 void DidActivate() {
932 switch (layer_tree_host()->source_frame_number()) {
933 case 1: {
934 EXPECT_EQ(0, callback_count_);
935 // Put a copy request on the layer, but then don't allow any
936 // drawing to take place.
937 scoped_ptr<CopyOutputRequest> request =
938 CopyOutputRequest::CreateRequest(
939 base::Bind(&LayerTreeHostCopyRequestTestShutdownBeforeCopy::
940 CopyOutputCallback,
941 base::Unretained(this)));
942 copy_layer_->RequestCopyOfOutput(request.Pass());
944 layer_tree_host()->SetViewportSize(gfx::Size());
945 break;
947 case 2:
948 DestroyLayerTreeHost();
949 // End the test after the copy result has had a chance to get back to
950 // the main thread.
951 MainThreadTaskRunner()->PostTask(
952 FROM_HERE,
953 base::Bind(&LayerTreeHostCopyRequestTestShutdownBeforeCopy::EndTest,
954 base::Unretained(this)));
955 break;
959 void AfterTest() override { EXPECT_EQ(1, callback_count_); }
961 int callback_count_;
962 FakeContentLayerClient client_;
963 scoped_refptr<FakeContentLayer> root_;
964 scoped_refptr<FakeContentLayer> copy_layer_;
967 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(
968 LayerTreeHostCopyRequestTestShutdownBeforeCopy);
970 class LayerTreeHostCopyRequestTestMultipleDrawsHiddenCopyRequest
971 : public LayerTreeHostCopyRequestTest {
972 protected:
973 void SetupTree() override {
974 scoped_refptr<FakeContentLayer> root =
975 FakeContentLayer::Create(layer_settings(), &client_);
976 root->SetBounds(gfx::Size(20, 20));
978 child_ = FakeContentLayer::Create(layer_settings(), &client_);
979 child_->SetBounds(gfx::Size(10, 10));
980 root->AddChild(child_);
981 child_->SetHideLayerAndSubtree(true);
983 layer_tree_host()->SetRootLayer(root);
984 LayerTreeHostCopyRequestTest::SetupTree();
987 void BeginTest() override {
988 num_draws_ = 0;
989 copy_happened_ = false;
990 draw_happened_ = false;
991 PostSetNeedsCommitToMainThread();
994 void DidCommitAndDrawFrame() override {
995 // Send a copy request after the first commit.
996 if (layer_tree_host()->source_frame_number() == 1) {
997 child_->RequestCopyOfOutput(
998 CopyOutputRequest::CreateBitmapRequest(base::Bind(
999 &LayerTreeHostCopyRequestTestMultipleDrawsHiddenCopyRequest::
1000 CopyOutputCallback,
1001 base::Unretained(this))));
1005 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
1006 LayerTreeHostImpl::FrameData* frame_data,
1007 DrawResult draw_result) override {
1008 LayerImpl* root = host_impl->active_tree()->root_layer();
1009 LayerImpl* child = root->children()[0];
1011 bool saw_root = false;
1012 bool saw_child = false;
1013 for (LayerIterator<LayerImpl> it = LayerIterator<LayerImpl>::Begin(
1014 frame_data->render_surface_layer_list);
1015 it != LayerIterator<LayerImpl>::End(
1016 frame_data->render_surface_layer_list);
1017 ++it) {
1018 if (it.represents_itself()) {
1019 if (*it == root)
1020 saw_root = true;
1021 else if (*it == child)
1022 saw_child = true;
1023 else
1024 NOTREACHED();
1028 ++num_draws_;
1029 // The first draw has no copy request. The 2nd draw has a copy request, the
1030 // 3rd should not again.
1031 switch (num_draws_) {
1032 case 1:
1033 // Only the root layer draws, the child is hidden.
1034 EXPECT_TRUE(saw_root);
1035 EXPECT_FALSE(saw_child);
1036 break;
1037 case 2:
1038 // Copy happening here, the child will draw.
1039 EXPECT_TRUE(saw_root);
1040 EXPECT_TRUE(saw_child);
1041 // Make another draw happen after doing the copy request.
1042 host_impl->SetNeedsRedrawRect(gfx::Rect(1, 1));
1043 break;
1044 case 3:
1045 // If LayerTreeHostImpl does the wrong thing, it will try to draw the
1046 // layer which had a copy request. But only the root should draw.
1047 EXPECT_TRUE(saw_root);
1048 EXPECT_FALSE(saw_child);
1050 // End the test! Don't race with copy request callbacks, so post the end
1051 // to the main thread.
1052 draw_happened_ = true;
1053 MainThreadTaskRunner()->PostTask(
1054 FROM_HERE,
1055 base::Bind(
1056 &LayerTreeHostCopyRequestTestMultipleDrawsHiddenCopyRequest::
1057 TryEndTest,
1058 base::Unretained(this)));
1059 break;
1061 return draw_result;
1064 void CopyOutputCallback(scoped_ptr<CopyOutputResult> result) {
1065 EXPECT_FALSE(TestEnded());
1066 copy_happened_ = true;
1067 TryEndTest();
1070 void TryEndTest() {
1071 if (draw_happened_ && copy_happened_)
1072 EndTest();
1075 void AfterTest() override {}
1077 scoped_refptr<FakeContentLayer> child_;
1078 FakeContentLayerClient client_;
1079 int num_draws_;
1080 bool copy_happened_;
1081 bool draw_happened_;
1084 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(
1085 LayerTreeHostCopyRequestTestMultipleDrawsHiddenCopyRequest);
1087 } // namespace
1088 } // namespace cc