Landing Recent QUIC changes until 8/19/2015 17:00 UTC.
[chromium-blink-merge.git] / cc / trees / layer_tree_host_unittest_context.cc
blobe8e26ca80f2f2e5f3a010b2488c3112a2a10860b
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.
5 #include "cc/trees/layer_tree_host.h"
7 #include "base/basictypes.h"
8 #include "cc/layers/delegated_frame_provider.h"
9 #include "cc/layers/delegated_frame_resource_collection.h"
10 #include "cc/layers/heads_up_display_layer.h"
11 #include "cc/layers/io_surface_layer.h"
12 #include "cc/layers/layer_impl.h"
13 #include "cc/layers/painted_scrollbar_layer.h"
14 #include "cc/layers/picture_layer.h"
15 #include "cc/layers/texture_layer.h"
16 #include "cc/layers/texture_layer_impl.h"
17 #include "cc/layers/video_layer.h"
18 #include "cc/layers/video_layer_impl.h"
19 #include "cc/output/filter_operations.h"
20 #include "cc/resources/single_release_callback.h"
21 #include "cc/test/failure_output_surface.h"
22 #include "cc/test/fake_content_layer_client.h"
23 #include "cc/test/fake_delegated_renderer_layer.h"
24 #include "cc/test/fake_delegated_renderer_layer_impl.h"
25 #include "cc/test/fake_layer_tree_host_client.h"
26 #include "cc/test/fake_output_surface.h"
27 #include "cc/test/fake_output_surface_client.h"
28 #include "cc/test/fake_painted_scrollbar_layer.h"
29 #include "cc/test/fake_picture_layer.h"
30 #include "cc/test/fake_picture_layer_impl.h"
31 #include "cc/test/fake_resource_provider.h"
32 #include "cc/test/fake_scoped_ui_resource.h"
33 #include "cc/test/fake_scrollbar.h"
34 #include "cc/test/fake_video_frame_provider.h"
35 #include "cc/test/layer_tree_test.h"
36 #include "cc/test/render_pass_test_utils.h"
37 #include "cc/test/test_context_provider.h"
38 #include "cc/test/test_shared_bitmap_manager.h"
39 #include "cc/test/test_web_graphics_context_3d.h"
40 #include "cc/trees/layer_tree_host_impl.h"
41 #include "cc/trees/layer_tree_impl.h"
42 #include "cc/trees/single_thread_proxy.h"
43 #include "gpu/GLES2/gl2extchromium.h"
44 #include "media/base/media.h"
46 using media::VideoFrame;
48 namespace cc {
49 namespace {
51 // These tests deal with losing the 3d graphics context.
52 class LayerTreeHostContextTest : public LayerTreeTest {
53 public:
54 LayerTreeHostContextTest()
55 : LayerTreeTest(),
56 context3d_(NULL),
57 times_to_fail_create_(0),
58 times_to_lose_during_commit_(0),
59 times_to_lose_during_draw_(0),
60 times_to_fail_recreate_(0),
61 times_to_expect_create_failed_(0),
62 times_create_failed_(0),
63 committed_at_least_once_(false),
64 context_should_support_io_surface_(false),
65 fallback_context_works_(false),
66 async_output_surface_creation_(false) {
67 media::InitializeMediaLibrary();
70 void LoseContext() {
71 // CreateFakeOutputSurface happens on a different thread, so lock context3d_
72 // to make sure we don't set it to null after recreating it there.
73 base::AutoLock lock(context3d_lock_);
74 // For sanity-checking tests, they should only call this when the
75 // context is not lost.
76 CHECK(context3d_);
77 context3d_->loseContextCHROMIUM(GL_GUILTY_CONTEXT_RESET_ARB,
78 GL_INNOCENT_CONTEXT_RESET_ARB);
79 context3d_ = NULL;
82 virtual scoped_ptr<TestWebGraphicsContext3D> CreateContext3d() {
83 return TestWebGraphicsContext3D::Create();
86 scoped_ptr<FakeOutputSurface> CreateFakeOutputSurface() override {
87 if (times_to_fail_create_) {
88 --times_to_fail_create_;
89 ExpectCreateToFail();
90 return make_scoped_ptr(new FailureOutputSurface(delegating_renderer()));
93 scoped_ptr<TestWebGraphicsContext3D> context3d = CreateContext3d();
94 base::AutoLock lock(context3d_lock_);
95 context3d_ = context3d.get();
97 if (context_should_support_io_surface_) {
98 context3d_->set_have_extension_io_surface(true);
99 context3d_->set_have_extension_egl_image(true);
102 if (delegating_renderer())
103 return FakeOutputSurface::CreateDelegating3d(context3d.Pass());
104 else
105 return FakeOutputSurface::Create3d(context3d.Pass());
108 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
109 LayerTreeHostImpl::FrameData* frame,
110 DrawResult draw_result) override {
111 if (draw_result == DRAW_ABORTED_MISSING_HIGH_RES_CONTENT) {
112 // Only valid for single-threaded compositing, which activates
113 // immediately and will try to draw again when content has finished.
114 DCHECK(!host_impl->proxy()->HasImplThread());
115 return draw_result;
117 EXPECT_EQ(DRAW_SUCCESS, draw_result);
118 if (!times_to_lose_during_draw_)
119 return draw_result;
121 --times_to_lose_during_draw_;
122 LoseContext();
124 times_to_fail_create_ = times_to_fail_recreate_;
125 times_to_fail_recreate_ = 0;
127 return draw_result;
130 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
131 committed_at_least_once_ = true;
133 if (!times_to_lose_during_commit_)
134 return;
135 --times_to_lose_during_commit_;
136 LoseContext();
138 times_to_fail_create_ = times_to_fail_recreate_;
139 times_to_fail_recreate_ = 0;
142 void DidFailToInitializeOutputSurface() override { ++times_create_failed_; }
144 void TearDown() override {
145 LayerTreeTest::TearDown();
146 EXPECT_EQ(times_to_expect_create_failed_, times_create_failed_);
149 void ExpectCreateToFail() { ++times_to_expect_create_failed_; }
151 protected:
152 // Protects use of context3d_ so LoseContext and CreateFakeOutputSurface
153 // can both use it on different threads.
154 base::Lock context3d_lock_;
155 TestWebGraphicsContext3D* context3d_;
157 int times_to_fail_create_;
158 int times_to_lose_during_commit_;
159 int times_to_lose_during_draw_;
160 int times_to_fail_recreate_;
161 int times_to_expect_create_failed_;
162 int times_create_failed_;
163 bool committed_at_least_once_;
164 bool context_should_support_io_surface_;
165 bool fallback_context_works_;
166 bool async_output_surface_creation_;
169 class LayerTreeHostContextTestLostContextSucceeds
170 : public LayerTreeHostContextTest {
171 public:
172 LayerTreeHostContextTestLostContextSucceeds()
173 : LayerTreeHostContextTest(),
174 test_case_(0),
175 num_losses_(0),
176 num_losses_last_test_case_(-1),
177 recovered_context_(true),
178 first_initialized_(false) {}
180 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
182 void RequestNewOutputSurface() override {
183 if (async_output_surface_creation_) {
184 MainThreadTaskRunner()->PostTask(
185 FROM_HERE, base::Bind(&LayerTreeHostContextTestLostContextSucceeds::
186 CreateAndSetOutputSurface,
187 base::Unretained(this)));
188 } else {
189 CreateAndSetOutputSurface();
193 void CreateAndSetOutputSurface() {
194 scoped_ptr<OutputSurface> surface(
195 LayerTreeHostContextTest::CreateOutputSurface());
196 CHECK(surface);
197 layer_tree_host()->SetOutputSurface(surface.Pass());
200 void DidInitializeOutputSurface() override {
201 if (first_initialized_)
202 ++num_losses_;
203 else
204 first_initialized_ = true;
206 recovered_context_ = true;
209 void AfterTest() override { EXPECT_EQ(11u, test_case_); }
211 void DidCommitAndDrawFrame() override {
212 // If the last frame had a context loss, then we'll commit again to
213 // recover.
214 if (!recovered_context_)
215 return;
216 if (times_to_lose_during_commit_)
217 return;
218 if (times_to_lose_during_draw_)
219 return;
221 recovered_context_ = false;
222 if (NextTestCase())
223 InvalidateAndSetNeedsCommit();
224 else
225 EndTest();
228 virtual void InvalidateAndSetNeedsCommit() {
229 // Cause damage so we try to draw.
230 layer_tree_host()->root_layer()->SetNeedsDisplay();
231 layer_tree_host()->SetNeedsCommit();
234 bool NextTestCase() {
235 static const TestCase kTests[] = {
236 // Losing the context and failing to recreate it (or losing it again
237 // immediately) a small number of times should succeed.
239 1, // times_to_lose_during_commit
240 0, // times_to_lose_during_draw
241 0, // times_to_fail_recreate
242 false, // fallback_context_works
243 false, // async_output_surface_creation
246 0, // times_to_lose_during_commit
247 1, // times_to_lose_during_draw
248 0, // times_to_fail_recreate
249 false, // fallback_context_works
250 false, // async_output_surface_creation
253 1, // times_to_lose_during_commit
254 0, // times_to_lose_during_draw
255 3, // times_to_fail_recreate
256 false, // fallback_context_works
257 false, // async_output_surface_creation
260 0, // times_to_lose_during_commit
261 1, // times_to_lose_during_draw
262 3, // times_to_fail_recreate
263 false, // fallback_context_works
264 false, // async_output_surface_creation
267 0, // times_to_lose_during_commit
268 1, // times_to_lose_during_draw
269 3, // times_to_fail_recreate
270 false, // fallback_context_works
271 true, // async_output_surface_creation
273 // Losing the context and recreating it any number of times should
274 // succeed.
276 10, // times_to_lose_during_commit
277 0, // times_to_lose_during_draw
278 0, // times_to_fail_recreate
279 false, // fallback_context_works
280 false, // async_output_surface_creation
283 0, // times_to_lose_during_commit
284 10, // times_to_lose_during_draw
285 0, // times_to_fail_recreate
286 false, // fallback_context_works
287 false, // async_output_surface_creation
290 10, // times_to_lose_during_commit
291 0, // times_to_lose_during_draw
292 0, // times_to_fail_recreate
293 false, // fallback_context_works
294 true, // async_output_surface_creation
297 0, // times_to_lose_during_commit
298 10, // times_to_lose_during_draw
299 0, // times_to_fail_recreate
300 false, // fallback_context_works
301 true, // async_output_surface_creation
303 // Losing the context, failing to reinitialize it, and making a fallback
304 // context should work.
306 0, // times_to_lose_during_commit
307 1, // times_to_lose_during_draw
308 0, // times_to_fail_recreate
309 true, // fallback_context_works
310 false, // async_output_surface_creation
313 0, // times_to_lose_during_commit
314 1, // times_to_lose_during_draw
315 0, // times_to_fail_recreate
316 true, // fallback_context_works
317 true, // async_output_surface_creation
321 if (test_case_ >= arraysize(kTests))
322 return false;
323 // Make sure that we lost our context at least once in the last test run so
324 // the test did something.
325 EXPECT_GT(num_losses_, num_losses_last_test_case_);
326 num_losses_last_test_case_ = num_losses_;
328 times_to_lose_during_commit_ =
329 kTests[test_case_].times_to_lose_during_commit;
330 times_to_lose_during_draw_ = kTests[test_case_].times_to_lose_during_draw;
331 times_to_fail_recreate_ = kTests[test_case_].times_to_fail_recreate;
332 fallback_context_works_ = kTests[test_case_].fallback_context_works;
333 async_output_surface_creation_ =
334 kTests[test_case_].async_output_surface_creation;
335 ++test_case_;
336 return true;
339 struct TestCase {
340 int times_to_lose_during_commit;
341 int times_to_lose_during_draw;
342 int times_to_fail_recreate;
343 bool fallback_context_works;
344 bool async_output_surface_creation;
347 protected:
348 size_t test_case_;
349 int num_losses_;
350 int num_losses_last_test_case_;
351 bool recovered_context_;
352 bool first_initialized_;
355 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostContextTestLostContextSucceeds);
357 class LayerTreeHostClientNotReadyDoesNotCreateOutputSurface
358 : public LayerTreeHostContextTest {
359 public:
360 LayerTreeHostClientNotReadyDoesNotCreateOutputSurface()
361 : LayerTreeHostContextTest() {}
363 void WillBeginTest() override {
364 // Override and do not signal SetLayerTreeHostClientReady.
367 void BeginTest() override {
368 PostSetNeedsCommitToMainThread();
369 EndTest();
372 scoped_ptr<OutputSurface> CreateOutputSurface() override {
373 EXPECT_TRUE(false);
374 return nullptr;
377 void DidInitializeOutputSurface() override { EXPECT_TRUE(false); }
379 void AfterTest() override {}
382 SINGLE_AND_MULTI_THREAD_TEST_F(
383 LayerTreeHostClientNotReadyDoesNotCreateOutputSurface);
385 class MultipleCompositeDoesNotCreateOutputSurface
386 : public LayerTreeHostContextTest {
387 public:
388 MultipleCompositeDoesNotCreateOutputSurface()
389 : LayerTreeHostContextTest(), request_count_(0) {}
391 void InitializeSettings(LayerTreeSettings* settings) override {
392 settings->single_thread_proxy_scheduler = false;
393 settings->use_zero_copy = true;
394 settings->use_one_copy = false;
397 void RequestNewOutputSurface() override {
398 EXPECT_GE(1, ++request_count_);
399 EndTest();
402 void BeginTest() override {
403 layer_tree_host()->Composite(base::TimeTicks::FromInternalValue(1));
404 layer_tree_host()->Composite(base::TimeTicks::FromInternalValue(2));
407 scoped_ptr<OutputSurface> CreateOutputSurface() override {
408 EXPECT_TRUE(false);
409 return nullptr;
412 void DidInitializeOutputSurface() override { EXPECT_TRUE(false); }
414 void AfterTest() override {}
416 int request_count_;
419 // This test uses Composite() which only exists for single thread.
420 SINGLE_THREAD_TEST_F(MultipleCompositeDoesNotCreateOutputSurface);
422 // This test makes sure that once a SingleThreadProxy issues a
423 // DidFailToInitializeOutputSurface, that future Composite calls will not
424 // trigger additional requests for output surfaces.
425 class FailedCreateDoesNotCreateExtraOutputSurface
426 : public LayerTreeHostContextTest {
427 public:
428 FailedCreateDoesNotCreateExtraOutputSurface()
429 : LayerTreeHostContextTest(), num_requests_(0), has_failed_(false) {}
431 void InitializeSettings(LayerTreeSettings* settings) override {
432 settings->single_thread_proxy_scheduler = false;
433 settings->use_zero_copy = true;
434 settings->use_one_copy = false;
437 void RequestNewOutputSurface() override {
438 num_requests_++;
439 // There should be one initial request and then one request from
440 // the LayerTreeTest test hooks DidFailToInitializeOutputSurface (which is
441 // hard to skip). This second request is just ignored and is test cruft.
442 EXPECT_LE(num_requests_, 2);
443 if (num_requests_ > 1)
444 return;
445 ExpectCreateToFail();
446 layer_tree_host()->SetOutputSurface(
447 make_scoped_ptr(new FailureOutputSurface(false)));
450 void BeginTest() override {
451 // First composite tries to create a surface.
452 layer_tree_host()->Composite(base::TimeTicks::FromInternalValue(1));
453 EXPECT_EQ(num_requests_, 2);
454 EXPECT_TRUE(has_failed_);
456 // Second composite should not request or fail.
457 layer_tree_host()->Composite(base::TimeTicks::FromInternalValue(2));
458 EXPECT_EQ(num_requests_, 2);
459 EndTest();
462 void DidInitializeOutputSurface() override { EXPECT_TRUE(false); }
464 void DidFailToInitializeOutputSurface() override {
465 LayerTreeHostContextTest::DidFailToInitializeOutputSurface();
466 EXPECT_FALSE(has_failed_);
467 has_failed_ = true;
470 void AfterTest() override {}
472 int num_requests_;
473 bool has_failed_;
476 // This test uses Composite() which only exists for single thread.
477 SINGLE_THREAD_TEST_F(FailedCreateDoesNotCreateExtraOutputSurface);
479 class LayerTreeHostContextTestCommitAfterDelayedOutputSurface
480 : public LayerTreeHostContextTest {
481 public:
482 LayerTreeHostContextTestCommitAfterDelayedOutputSurface()
483 : LayerTreeHostContextTest(), creating_output_(false) {}
485 void InitializeSettings(LayerTreeSettings* settings) override {
486 settings->single_thread_proxy_scheduler = false;
487 settings->use_zero_copy = true;
488 settings->use_one_copy = false;
491 void RequestNewOutputSurface() override {
492 MainThreadTaskRunner()->PostTask(
493 FROM_HERE,
494 base::Bind(&LayerTreeHostContextTestCommitAfterDelayedOutputSurface::
495 CreateAndSetOutputSurface,
496 base::Unretained(this)));
499 void CreateAndSetOutputSurface() {
500 creating_output_ = true;
501 layer_tree_host()->SetOutputSurface(
502 LayerTreeHostContextTest::CreateOutputSurface());
505 void BeginTest() override {
506 layer_tree_host()->Composite(base::TimeTicks::FromInternalValue(1));
509 void ScheduleComposite() override {
510 if (creating_output_)
511 EndTest();
514 void AfterTest() override {}
516 bool creating_output_;
519 // This test uses Composite() which only exists for single thread.
520 SINGLE_THREAD_TEST_F(LayerTreeHostContextTestCommitAfterDelayedOutputSurface);
522 class LayerTreeHostContextTestAvoidUnnecessaryComposite
523 : public LayerTreeHostContextTest {
524 public:
525 LayerTreeHostContextTestAvoidUnnecessaryComposite()
526 : LayerTreeHostContextTest(), in_composite_(false) {}
528 void InitializeSettings(LayerTreeSettings* settings) override {
529 settings->single_thread_proxy_scheduler = false;
530 settings->use_zero_copy = true;
531 settings->use_one_copy = false;
534 void RequestNewOutputSurface() override {
535 layer_tree_host()->SetOutputSurface(
536 LayerTreeHostContextTest::CreateOutputSurface());
537 EndTest();
540 void BeginTest() override {
541 in_composite_ = true;
542 layer_tree_host()->Composite(base::TimeTicks::FromInternalValue(1));
543 in_composite_ = false;
546 void ScheduleComposite() override { EXPECT_FALSE(in_composite_); }
548 void AfterTest() override {}
550 bool in_composite_;
553 // This test uses Composite() which only exists for single thread.
554 SINGLE_THREAD_TEST_F(LayerTreeHostContextTestAvoidUnnecessaryComposite);
556 // This test uses PictureLayer to check for a working context.
557 class LayerTreeHostContextTestLostContextSucceedsWithContent
558 : public LayerTreeHostContextTestLostContextSucceeds {
559 public:
560 void SetupTree() override {
561 root_ = Layer::Create(layer_settings());
562 root_->SetBounds(gfx::Size(10, 10));
563 root_->SetIsDrawable(true);
565 // Paint non-solid color.
566 SkPaint paint;
567 paint.setColor(SkColorSetARGB(100, 80, 200, 200));
568 client_.add_draw_rect(gfx::Rect(0, 0, 5, 5), paint);
570 layer_ = FakePictureLayer::Create(layer_settings(), &client_);
571 layer_->SetBounds(gfx::Size(10, 10));
572 layer_->SetIsDrawable(true);
574 root_->AddChild(layer_);
576 layer_tree_host()->SetRootLayer(root_);
577 LayerTreeHostContextTest::SetupTree();
580 void InvalidateAndSetNeedsCommit() override {
581 // Invalidate the render surface so we don't try to use a cached copy of the
582 // surface. We want to make sure to test the drawing paths for drawing to
583 // a child surface.
584 layer_->SetNeedsDisplay();
585 LayerTreeHostContextTestLostContextSucceeds::InvalidateAndSetNeedsCommit();
588 void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override {
589 FakePictureLayerImpl* picture_impl = static_cast<FakePictureLayerImpl*>(
590 host_impl->active_tree()->root_layer()->children()[0]);
591 EXPECT_TRUE(picture_impl->HighResTiling()
592 ->TileAt(0, 0)
593 ->draw_info()
594 .IsReadyToDraw());
597 protected:
598 FakeContentLayerClient client_;
599 scoped_refptr<Layer> root_;
600 scoped_refptr<Layer> layer_;
603 SINGLE_AND_MULTI_THREAD_TEST_F(
604 LayerTreeHostContextTestLostContextSucceedsWithContent);
606 class LayerTreeHostContextTestCreateOutputSurfaceFailsOnce
607 : public LayerTreeHostContextTest {
608 public:
609 LayerTreeHostContextTestCreateOutputSurfaceFailsOnce()
610 : times_to_fail_(1), times_initialized_(0) {
611 times_to_fail_create_ = times_to_fail_;
614 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
616 void DidInitializeOutputSurface() override { times_initialized_++; }
618 void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override { EndTest(); }
620 void AfterTest() override {
621 EXPECT_EQ(times_to_fail_, times_create_failed_);
622 EXPECT_NE(0, times_initialized_);
625 private:
626 int times_to_fail_;
627 int times_initialized_;
630 SINGLE_AND_MULTI_THREAD_TEST_F(
631 LayerTreeHostContextTestCreateOutputSurfaceFailsOnce);
633 class LayerTreeHostContextTestLostContextAndEvictTextures
634 : public LayerTreeHostContextTest {
635 public:
636 LayerTreeHostContextTestLostContextAndEvictTextures()
637 : LayerTreeHostContextTest(),
638 impl_host_(0),
639 num_commits_(0),
640 lost_context_(false) {}
642 void SetupTree() override {
643 // Paint non-solid color.
644 SkPaint paint;
645 paint.setColor(SkColorSetARGB(100, 80, 200, 200));
646 client_.add_draw_rect(gfx::Rect(0, 0, 5, 5), paint);
648 scoped_refptr<FakePictureLayer> picture_layer =
649 FakePictureLayer::Create(layer_settings(), &client_);
650 picture_layer->SetBounds(gfx::Size(10, 20));
651 layer_tree_host()->SetRootLayer(picture_layer);
653 LayerTreeHostContextTest::SetupTree();
656 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
658 void PostEvictTextures() {
659 if (HasImplThread()) {
660 ImplThreadTaskRunner()->PostTask(
661 FROM_HERE,
662 base::Bind(&LayerTreeHostContextTestLostContextAndEvictTextures::
663 EvictTexturesOnImplThread,
664 base::Unretained(this)));
665 } else {
666 DebugScopedSetImplThread impl(proxy());
667 EvictTexturesOnImplThread();
671 void EvictTexturesOnImplThread() {
672 impl_host_->EvictTexturesForTesting();
674 if (lose_after_evict_) {
675 LoseContext();
676 lost_context_ = true;
680 void DidCommitAndDrawFrame() override {
681 if (num_commits_ > 1)
682 return;
683 PostEvictTextures();
686 void CommitCompleteOnThread(LayerTreeHostImpl* impl) override {
687 LayerTreeHostContextTest::CommitCompleteOnThread(impl);
688 if (num_commits_ > 1)
689 return;
690 ++num_commits_;
691 if (!lose_after_evict_) {
692 LoseContext();
693 lost_context_ = true;
697 void DrawLayersOnThread(LayerTreeHostImpl* impl) override {
698 FakePictureLayerImpl* picture_impl =
699 static_cast<FakePictureLayerImpl*>(impl->active_tree()->root_layer());
700 EXPECT_TRUE(picture_impl->HighResTiling()
701 ->TileAt(0, 0)
702 ->draw_info()
703 .IsReadyToDraw());
705 impl_host_ = impl;
706 if (lost_context_)
707 EndTest();
710 void DidInitializeOutputSurface() override {}
712 void AfterTest() override {}
714 protected:
715 bool lose_after_evict_;
716 FakeContentLayerClient client_;
717 LayerTreeHostImpl* impl_host_;
718 int num_commits_;
719 bool lost_context_;
722 TEST_F(LayerTreeHostContextTestLostContextAndEvictTextures,
723 LoseAfterEvict_SingleThread_DirectRenderer) {
724 lose_after_evict_ = true;
725 RunTest(false, false);
728 TEST_F(LayerTreeHostContextTestLostContextAndEvictTextures,
729 LoseAfterEvict_SingleThread_DelegatingRenderer) {
730 lose_after_evict_ = true;
731 RunTest(false, true);
734 TEST_F(LayerTreeHostContextTestLostContextAndEvictTextures,
735 LoseAfterEvict_MultiThread_DirectRenderer) {
736 lose_after_evict_ = true;
737 RunTest(true, false);
740 TEST_F(LayerTreeHostContextTestLostContextAndEvictTextures,
741 LoseAfterEvict_MultiThread_DelegatingRenderer) {
742 lose_after_evict_ = true;
743 RunTest(true, true);
746 TEST_F(LayerTreeHostContextTestLostContextAndEvictTextures,
747 LoseBeforeEvict_SingleThread_DirectRenderer) {
748 lose_after_evict_ = false;
749 RunTest(false, false);
752 TEST_F(LayerTreeHostContextTestLostContextAndEvictTextures,
753 LoseBeforeEvict_SingleThread_DelegatingRenderer) {
754 lose_after_evict_ = false;
755 RunTest(false, true);
758 TEST_F(LayerTreeHostContextTestLostContextAndEvictTextures,
759 LoseBeforeEvict_MultiThread_DirectRenderer) {
760 lose_after_evict_ = false;
761 RunTest(true, false);
764 TEST_F(LayerTreeHostContextTestLostContextAndEvictTextures,
765 LoseBeforeEvict_MultiThread_DelegatingRenderer) {
766 lose_after_evict_ = false;
767 RunTest(true, true);
770 class LayerTreeHostContextTestLayersNotified : public LayerTreeHostContextTest {
771 public:
772 LayerTreeHostContextTestLayersNotified()
773 : LayerTreeHostContextTest(), num_commits_(0) {}
775 void SetupTree() override {
776 root_ = FakePictureLayer::Create(layer_settings(), &client_);
777 child_ = FakePictureLayer::Create(layer_settings(), &client_);
778 grandchild_ = FakePictureLayer::Create(layer_settings(), &client_);
780 root_->AddChild(child_);
781 child_->AddChild(grandchild_);
783 layer_tree_host()->SetRootLayer(root_);
784 LayerTreeHostContextTest::SetupTree();
787 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
789 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
790 LayerTreeHostContextTest::DidActivateTreeOnThread(host_impl);
792 FakePictureLayerImpl* root_picture = NULL;
793 FakePictureLayerImpl* child_picture = NULL;
794 FakePictureLayerImpl* grandchild_picture = NULL;
796 root_picture = static_cast<FakePictureLayerImpl*>(
797 host_impl->active_tree()->root_layer());
798 child_picture =
799 static_cast<FakePictureLayerImpl*>(root_picture->children()[0]);
800 grandchild_picture =
801 static_cast<FakePictureLayerImpl*>(child_picture->children()[0]);
803 ++num_commits_;
804 switch (num_commits_) {
805 case 1:
806 EXPECT_EQ(0u, root_picture->release_resources_count());
807 EXPECT_EQ(0u, child_picture->release_resources_count());
808 EXPECT_EQ(0u, grandchild_picture->release_resources_count());
810 // Lose the context and struggle to recreate it.
811 LoseContext();
812 times_to_fail_create_ = 1;
813 break;
814 case 2:
815 EXPECT_TRUE(root_picture->release_resources_count());
816 EXPECT_TRUE(child_picture->release_resources_count());
817 EXPECT_TRUE(grandchild_picture->release_resources_count());
819 EndTest();
820 break;
821 default:
822 NOTREACHED();
826 void AfterTest() override {}
828 private:
829 int num_commits_;
831 FakeContentLayerClient client_;
832 scoped_refptr<Layer> root_;
833 scoped_refptr<Layer> child_;
834 scoped_refptr<Layer> grandchild_;
837 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostContextTestLayersNotified);
839 class LayerTreeHostContextTestDontUseLostResources
840 : public LayerTreeHostContextTest {
841 public:
842 LayerTreeHostContextTestDontUseLostResources() : lost_context_(false) {
843 context_should_support_io_surface_ = true;
845 child_output_surface_ = FakeOutputSurface::Create3d();
846 child_output_surface_->BindToClient(&output_surface_client_);
847 shared_bitmap_manager_.reset(new TestSharedBitmapManager());
848 child_resource_provider_ = FakeResourceProvider::Create(
849 child_output_surface_.get(), shared_bitmap_manager_.get());
852 static void EmptyReleaseCallback(unsigned sync_point, bool lost) {}
854 void SetupTree() override {
855 gpu::gles2::GLES2Interface* gl =
856 child_output_surface_->context_provider()->ContextGL();
858 scoped_ptr<DelegatedFrameData> frame_data(new DelegatedFrameData);
860 scoped_ptr<RenderPass> pass_for_quad = RenderPass::Create();
861 pass_for_quad->SetNew(
862 // AppendOneOfEveryQuadType() makes a RenderPass quad with this id.
863 RenderPassId(2, 1),
864 gfx::Rect(0, 0, 10, 10),
865 gfx::Rect(0, 0, 10, 10),
866 gfx::Transform());
868 scoped_ptr<RenderPass> pass = RenderPass::Create();
869 pass->SetNew(RenderPassId(1, 1),
870 gfx::Rect(0, 0, 10, 10),
871 gfx::Rect(0, 0, 10, 10),
872 gfx::Transform());
873 uint32_t mailbox_sync_point;
874 AddOneOfEveryQuadType(pass.get(), child_resource_provider_.get(),
875 RenderPassId(2, 1), &mailbox_sync_point);
877 frame_data->render_pass_list.push_back(pass_for_quad.Pass());
878 frame_data->render_pass_list.push_back(pass.Pass());
880 delegated_resource_collection_ = new DelegatedFrameResourceCollection;
881 delegated_frame_provider_ = new DelegatedFrameProvider(
882 delegated_resource_collection_.get(), frame_data.Pass());
884 ResourceId resource = child_resource_provider_->CreateResource(
885 gfx::Size(4, 4), GL_CLAMP_TO_EDGE,
886 ResourceProvider::TEXTURE_HINT_IMMUTABLE, RGBA_8888);
887 ResourceProvider::ScopedWriteLockGL lock(child_resource_provider_.get(),
888 resource);
890 gpu::Mailbox mailbox;
891 gl->GenMailboxCHROMIUM(mailbox.name);
892 GLuint sync_point = gl->InsertSyncPointCHROMIUM();
894 scoped_refptr<Layer> root = Layer::Create(layer_settings());
895 root->SetBounds(gfx::Size(10, 10));
896 root->SetIsDrawable(true);
898 scoped_refptr<FakeDelegatedRendererLayer> delegated =
899 FakeDelegatedRendererLayer::Create(layer_settings(),
900 delegated_frame_provider_.get());
901 delegated->SetBounds(gfx::Size(10, 10));
902 delegated->SetIsDrawable(true);
903 root->AddChild(delegated);
905 scoped_refptr<PictureLayer> layer =
906 PictureLayer::Create(layer_settings(), &client_);
907 layer->SetBounds(gfx::Size(10, 10));
908 layer->SetIsDrawable(true);
909 root->AddChild(layer);
911 scoped_refptr<TextureLayer> texture =
912 TextureLayer::CreateForMailbox(layer_settings_, NULL);
913 texture->SetBounds(gfx::Size(10, 10));
914 texture->SetIsDrawable(true);
915 texture->SetTextureMailbox(
916 TextureMailbox(mailbox, GL_TEXTURE_2D, sync_point),
917 SingleReleaseCallback::Create(
918 base::Bind(&LayerTreeHostContextTestDontUseLostResources::
919 EmptyReleaseCallback)));
920 root->AddChild(texture);
922 scoped_refptr<PictureLayer> mask =
923 PictureLayer::Create(layer_settings_, &client_);
924 mask->SetBounds(gfx::Size(10, 10));
926 scoped_refptr<PictureLayer> layer_with_mask =
927 PictureLayer::Create(layer_settings_, &client_);
928 layer_with_mask->SetBounds(gfx::Size(10, 10));
929 layer_with_mask->SetIsDrawable(true);
930 layer_with_mask->SetMaskLayer(mask.get());
931 root->AddChild(layer_with_mask);
933 scoped_refptr<VideoLayer> video_color = VideoLayer::Create(
934 layer_settings_, &color_frame_provider_, media::VIDEO_ROTATION_0);
935 video_color->SetBounds(gfx::Size(10, 10));
936 video_color->SetIsDrawable(true);
937 root->AddChild(video_color);
939 scoped_refptr<VideoLayer> video_hw = VideoLayer::Create(
940 layer_settings_, &hw_frame_provider_, media::VIDEO_ROTATION_0);
941 video_hw->SetBounds(gfx::Size(10, 10));
942 video_hw->SetIsDrawable(true);
943 root->AddChild(video_hw);
945 scoped_refptr<VideoLayer> video_scaled_hw = VideoLayer::Create(
946 layer_settings_, &scaled_hw_frame_provider_, media::VIDEO_ROTATION_0);
947 video_scaled_hw->SetBounds(gfx::Size(10, 10));
948 video_scaled_hw->SetIsDrawable(true);
949 root->AddChild(video_scaled_hw);
951 color_video_frame_ = VideoFrame::CreateColorFrame(
952 gfx::Size(4, 4), 0x80, 0x80, 0x80, base::TimeDelta());
953 hw_video_frame_ = VideoFrame::WrapNativeTexture(
954 media::PIXEL_FORMAT_ARGB,
955 gpu::MailboxHolder(mailbox, GL_TEXTURE_2D, sync_point),
956 media::VideoFrame::ReleaseMailboxCB(), gfx::Size(4, 4),
957 gfx::Rect(0, 0, 4, 4), gfx::Size(4, 4), base::TimeDelta());
958 scaled_hw_video_frame_ = VideoFrame::WrapNativeTexture(
959 media::PIXEL_FORMAT_ARGB,
960 gpu::MailboxHolder(mailbox, GL_TEXTURE_2D, sync_point),
961 media::VideoFrame::ReleaseMailboxCB(), gfx::Size(4, 4),
962 gfx::Rect(0, 0, 3, 2), gfx::Size(4, 4), base::TimeDelta());
964 color_frame_provider_.set_frame(color_video_frame_);
965 hw_frame_provider_.set_frame(hw_video_frame_);
966 scaled_hw_frame_provider_.set_frame(scaled_hw_video_frame_);
968 scoped_refptr<IOSurfaceLayer> io_surface =
969 IOSurfaceLayer::Create(layer_settings_);
970 io_surface->SetBounds(gfx::Size(10, 10));
971 io_surface->SetIsDrawable(true);
972 io_surface->SetIOSurfaceProperties(1, gfx::Size(10, 10));
973 root->AddChild(io_surface);
975 // Enable the hud.
976 LayerTreeDebugState debug_state;
977 debug_state.show_property_changed_rects = true;
978 layer_tree_host()->SetDebugState(debug_state);
980 scoped_refptr<PaintedScrollbarLayer> scrollbar =
981 PaintedScrollbarLayer::Create(
982 layer_settings_, scoped_ptr<Scrollbar>(new FakeScrollbar).Pass(),
983 layer->id());
984 scrollbar->SetBounds(gfx::Size(10, 10));
985 scrollbar->SetIsDrawable(true);
986 root->AddChild(scrollbar);
988 layer_tree_host()->SetRootLayer(root);
989 LayerTreeHostContextTest::SetupTree();
992 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
994 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
995 LayerTreeHostContextTest::CommitCompleteOnThread(host_impl);
997 if (host_impl->active_tree()->source_frame_number() == 3) {
998 // On the third commit we're recovering from context loss. Hardware
999 // video frames should not be reused by the VideoFrameProvider, but
1000 // software frames can be.
1001 hw_frame_provider_.set_frame(NULL);
1002 scaled_hw_frame_provider_.set_frame(NULL);
1006 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
1007 LayerTreeHostImpl::FrameData* frame,
1008 DrawResult draw_result) override {
1009 if (host_impl->active_tree()->source_frame_number() == 2) {
1010 // Lose the context during draw on the second commit. This will cause
1011 // a third commit to recover.
1012 context3d_->set_times_bind_texture_succeeds(0);
1014 return draw_result;
1017 scoped_ptr<FakeOutputSurface> CreateFakeOutputSurface() override {
1018 // This will get called twice:
1019 // First when we create the initial output surface...
1020 if (layer_tree_host()->source_frame_number() > 0) {
1021 // ... and then again after we forced the context to be lost.
1022 lost_context_ = true;
1024 return LayerTreeHostContextTest::CreateFakeOutputSurface();
1027 void DidCommitAndDrawFrame() override {
1028 ASSERT_TRUE(layer_tree_host()->hud_layer());
1029 // End the test once we know the 3nd frame drew.
1030 if (layer_tree_host()->source_frame_number() < 5) {
1031 layer_tree_host()->root_layer()->SetNeedsDisplay();
1032 layer_tree_host()->SetNeedsCommit();
1033 } else {
1034 EndTest();
1038 void AfterTest() override { EXPECT_TRUE(lost_context_); }
1040 private:
1041 FakeContentLayerClient client_;
1042 bool lost_context_;
1044 FakeOutputSurfaceClient output_surface_client_;
1045 scoped_ptr<FakeOutputSurface> child_output_surface_;
1046 scoped_ptr<SharedBitmapManager> shared_bitmap_manager_;
1047 scoped_ptr<ResourceProvider> child_resource_provider_;
1049 scoped_refptr<DelegatedFrameResourceCollection>
1050 delegated_resource_collection_;
1051 scoped_refptr<DelegatedFrameProvider> delegated_frame_provider_;
1053 scoped_refptr<VideoFrame> color_video_frame_;
1054 scoped_refptr<VideoFrame> hw_video_frame_;
1055 scoped_refptr<VideoFrame> scaled_hw_video_frame_;
1057 FakeVideoFrameProvider color_frame_provider_;
1058 FakeVideoFrameProvider hw_frame_provider_;
1059 FakeVideoFrameProvider scaled_hw_frame_provider_;
1061 LayerSettings layer_settings_;
1064 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostContextTestDontUseLostResources);
1066 class LayerTreeHostContextTestImplSidePainting
1067 : public LayerTreeHostContextTest {
1068 public:
1069 void SetupTree() override {
1070 scoped_refptr<Layer> root = Layer::Create(layer_settings());
1071 root->SetBounds(gfx::Size(10, 10));
1072 root->SetIsDrawable(true);
1074 scoped_refptr<PictureLayer> picture =
1075 PictureLayer::Create(layer_settings(), &client_);
1076 picture->SetBounds(gfx::Size(10, 10));
1077 picture->SetIsDrawable(true);
1078 root->AddChild(picture);
1080 layer_tree_host()->SetRootLayer(root);
1081 LayerTreeHostContextTest::SetupTree();
1084 void BeginTest() override {
1085 times_to_lose_during_commit_ = 1;
1086 PostSetNeedsCommitToMainThread();
1089 void AfterTest() override {}
1091 void DidInitializeOutputSurface() override { EndTest(); }
1093 private:
1094 FakeContentLayerClient client_;
1097 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostContextTestImplSidePainting);
1099 class ScrollbarLayerLostContext : public LayerTreeHostContextTest {
1100 public:
1101 ScrollbarLayerLostContext() : commits_(0) {}
1103 void BeginTest() override {
1104 scoped_refptr<Layer> scroll_layer = Layer::Create(layer_settings());
1105 scrollbar_layer_ = FakePaintedScrollbarLayer::Create(
1106 layer_settings(), false, true, scroll_layer->id());
1107 scrollbar_layer_->SetBounds(gfx::Size(10, 100));
1108 layer_tree_host()->root_layer()->AddChild(scrollbar_layer_);
1109 layer_tree_host()->root_layer()->AddChild(scroll_layer);
1110 PostSetNeedsCommitToMainThread();
1113 void AfterTest() override {}
1115 void CommitCompleteOnThread(LayerTreeHostImpl* impl) override {
1116 LayerTreeHostContextTest::CommitCompleteOnThread(impl);
1118 ++commits_;
1119 switch (commits_) {
1120 case 1:
1121 // First (regular) update, we should upload 2 resources (thumb, and
1122 // backtrack).
1123 EXPECT_EQ(1, scrollbar_layer_->update_count());
1124 LoseContext();
1125 break;
1126 case 2:
1127 // Second update, after the lost context, we should still upload 2
1128 // resources even if the contents haven't changed.
1129 EXPECT_EQ(2, scrollbar_layer_->update_count());
1130 EndTest();
1131 break;
1132 default:
1133 NOTREACHED();
1137 private:
1138 int commits_;
1139 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer_;
1142 SINGLE_AND_MULTI_THREAD_TEST_F(ScrollbarLayerLostContext);
1144 class UIResourceLostTest : public LayerTreeHostContextTest {
1145 public:
1146 UIResourceLostTest() : time_step_(0) {}
1147 void InitializeSettings(LayerTreeSettings* settings) override {
1148 settings->renderer_settings.texture_id_allocation_chunk_size = 1;
1150 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
1151 void AfterTest() override {}
1153 // This is called on the main thread after each commit and
1154 // DidActivateTreeOnThread, with the value of time_step_ at the time
1155 // of the call to DidActivateTreeOnThread. Similar tests will do
1156 // work on the main thread in DidCommit but that is unsuitable because
1157 // the main thread work for these tests must happen after
1158 // DidActivateTreeOnThread, which happens after DidCommit with impl-side
1159 // painting.
1160 virtual void StepCompleteOnMainThread(int time_step) = 0;
1162 // Called after DidActivateTreeOnThread. If this is done during the commit,
1163 // the call to StepCompleteOnMainThread will not occur until after
1164 // the commit completes, because the main thread is blocked.
1165 void PostStepCompleteToMainThread() {
1166 proxy()->MainThreadTaskRunner()->PostTask(
1167 FROM_HERE,
1168 base::Bind(&UIResourceLostTest::StepCompleteOnMainThreadInternal,
1169 base::Unretained(this),
1170 time_step_));
1173 void PostLoseContextToImplThread() {
1174 EXPECT_TRUE(layer_tree_host()->proxy()->IsMainThread());
1175 ImplThreadTaskRunner()->PostTask(
1176 FROM_HERE,
1177 base::Bind(&LayerTreeHostContextTest::LoseContext,
1178 base::Unretained(this)));
1181 protected:
1182 int time_step_;
1183 scoped_ptr<FakeScopedUIResource> ui_resource_;
1185 private:
1186 void StepCompleteOnMainThreadInternal(int step) {
1187 EXPECT_TRUE(layer_tree_host()->proxy()->IsMainThread());
1188 StepCompleteOnMainThread(step);
1192 class UIResourceLostTestSimple : public UIResourceLostTest {
1193 public:
1194 // This is called when the new layer tree has been activated.
1195 virtual void StepCompleteOnImplThread(LayerTreeHostImpl* impl) = 0;
1197 void DidActivateTreeOnThread(LayerTreeHostImpl* impl) override {
1198 StepCompleteOnImplThread(impl);
1199 PostStepCompleteToMainThread();
1200 ++time_step_;
1204 // Losing context after an UI resource has been created.
1205 class UIResourceLostAfterCommit : public UIResourceLostTestSimple {
1206 public:
1207 void StepCompleteOnMainThread(int step) override {
1208 EXPECT_TRUE(layer_tree_host()->proxy()->IsMainThread());
1209 switch (step) {
1210 case 0:
1211 ui_resource_ = FakeScopedUIResource::Create(layer_tree_host());
1212 // Expects a valid UIResourceId.
1213 EXPECT_NE(0, ui_resource_->id());
1214 PostSetNeedsCommitToMainThread();
1215 break;
1216 case 4:
1217 // Release resource before ending the test.
1218 ui_resource_ = nullptr;
1219 EndTest();
1220 break;
1221 case 5:
1222 NOTREACHED();
1223 break;
1227 void StepCompleteOnImplThread(LayerTreeHostImpl* impl) override {
1228 LayerTreeHostContextTest::CommitCompleteOnThread(impl);
1229 switch (time_step_) {
1230 case 1:
1231 // The resource should have been created on LTHI after the commit.
1232 EXPECT_NE(0u, impl->ResourceIdForUIResource(ui_resource_->id()));
1233 PostSetNeedsCommitToMainThread();
1234 break;
1235 case 2:
1236 LoseContext();
1237 break;
1238 case 3:
1239 // The resources should have been recreated. The bitmap callback should
1240 // have been called once with the resource_lost flag set to true.
1241 EXPECT_EQ(1, ui_resource_->lost_resource_count);
1242 // Resource Id on the impl-side have been recreated as well. Note
1243 // that the same UIResourceId persists after the context lost.
1244 EXPECT_NE(0u, impl->ResourceIdForUIResource(ui_resource_->id()));
1245 PostSetNeedsCommitToMainThread();
1246 break;
1251 SINGLE_AND_MULTI_THREAD_TEST_F(UIResourceLostAfterCommit);
1253 // Losing context before UI resource requests can be commited. Three sequences
1254 // of creation/deletion are considered:
1255 // 1. Create one resource -> Context Lost => Expect the resource to have been
1256 // created.
1257 // 2. Delete an exisiting resource (test_id0_) -> create a second resource
1258 // (test_id1_) -> Context Lost => Expect the test_id0_ to be removed and
1259 // test_id1_ to have been created.
1260 // 3. Create one resource -> Delete that same resource -> Context Lost => Expect
1261 // the resource to not exist in the manager.
1262 class UIResourceLostBeforeCommit : public UIResourceLostTestSimple {
1263 public:
1264 UIResourceLostBeforeCommit() : test_id0_(0), test_id1_(0) {}
1266 void StepCompleteOnMainThread(int step) override {
1267 switch (step) {
1268 case 0:
1269 ui_resource_ = FakeScopedUIResource::Create(layer_tree_host());
1270 // Lose the context on the impl thread before the commit.
1271 PostLoseContextToImplThread();
1272 break;
1273 case 2:
1274 // Sequence 2:
1275 // Currently one resource has been created.
1276 test_id0_ = ui_resource_->id();
1277 // Delete this resource.
1278 ui_resource_ = nullptr;
1279 // Create another resource.
1280 ui_resource_ = FakeScopedUIResource::Create(layer_tree_host());
1281 test_id1_ = ui_resource_->id();
1282 // Sanity check that two resource creations return different ids.
1283 EXPECT_NE(test_id0_, test_id1_);
1284 // Lose the context on the impl thread before the commit.
1285 PostLoseContextToImplThread();
1286 break;
1287 case 3:
1288 // Clear the manager of resources.
1289 ui_resource_ = nullptr;
1290 PostSetNeedsCommitToMainThread();
1291 break;
1292 case 4:
1293 // Sequence 3:
1294 ui_resource_ = FakeScopedUIResource::Create(layer_tree_host());
1295 test_id0_ = ui_resource_->id();
1296 // Sanity check the UIResourceId should not be 0.
1297 EXPECT_NE(0, test_id0_);
1298 // Usually ScopedUIResource are deleted from the manager in their
1299 // destructor (so usually ui_resource_ = nullptr). But here we need
1300 // ui_resource_ for the next step, so call DeleteUIResource directly.
1301 layer_tree_host()->DeleteUIResource(test_id0_);
1302 // Delete the resouce and then lose the context.
1303 PostLoseContextToImplThread();
1304 break;
1305 case 5:
1306 // Release resource before ending the test.
1307 ui_resource_ = nullptr;
1308 EndTest();
1309 break;
1310 case 6:
1311 NOTREACHED();
1312 break;
1316 void StepCompleteOnImplThread(LayerTreeHostImpl* impl) override {
1317 LayerTreeHostContextTest::CommitCompleteOnThread(impl);
1318 switch (time_step_) {
1319 case 1:
1320 // Sequence 1 (continued):
1321 // The first context lost happens before the resources were created,
1322 // and because it resulted in no resources being destroyed, it does not
1323 // trigger resource re-creation.
1324 EXPECT_EQ(1, ui_resource_->resource_create_count);
1325 EXPECT_EQ(0, ui_resource_->lost_resource_count);
1326 // Resource Id on the impl-side has been created.
1327 PostSetNeedsCommitToMainThread();
1328 break;
1329 case 3:
1330 // Sequence 2 (continued):
1331 // The previous resource should have been deleted.
1332 EXPECT_EQ(0u, impl->ResourceIdForUIResource(test_id0_));
1333 // The second resource should have been created.
1334 EXPECT_NE(0u, impl->ResourceIdForUIResource(test_id1_));
1335 // The second resource called the resource callback once and since the
1336 // context is lost, a "resource lost" callback was also issued.
1337 EXPECT_EQ(2, ui_resource_->resource_create_count);
1338 EXPECT_EQ(1, ui_resource_->lost_resource_count);
1339 break;
1340 case 5:
1341 // Sequence 3 (continued):
1342 // Expect the resource callback to have been called once.
1343 EXPECT_EQ(1, ui_resource_->resource_create_count);
1344 // No "resource lost" callbacks.
1345 EXPECT_EQ(0, ui_resource_->lost_resource_count);
1346 // The UI resource id should not be valid
1347 EXPECT_EQ(0u, impl->ResourceIdForUIResource(test_id0_));
1348 break;
1352 private:
1353 UIResourceId test_id0_;
1354 UIResourceId test_id1_;
1357 SINGLE_AND_MULTI_THREAD_TEST_F(UIResourceLostBeforeCommit);
1359 // Losing UI resource before the pending trees is activated but after the
1360 // commit. Impl-side-painting only.
1361 class UIResourceLostBeforeActivateTree : public UIResourceLostTest {
1362 void StepCompleteOnMainThread(int step) override {
1363 EXPECT_TRUE(layer_tree_host()->proxy()->IsMainThread());
1364 switch (step) {
1365 case 0:
1366 ui_resource_ = FakeScopedUIResource::Create(layer_tree_host());
1367 PostSetNeedsCommitToMainThread();
1368 break;
1369 case 3:
1370 test_id_ = ui_resource_->id();
1371 ui_resource_ = nullptr;
1372 PostSetNeedsCommitToMainThread();
1373 break;
1374 case 5:
1375 // Release resource before ending the test.
1376 ui_resource_ = nullptr;
1377 EndTest();
1378 break;
1379 case 6:
1380 // Make sure no extra commits happened.
1381 NOTREACHED();
1385 void CommitCompleteOnThread(LayerTreeHostImpl* impl) override {
1386 LayerTreeHostContextTest::CommitCompleteOnThread(impl);
1387 switch (time_step_) {
1388 case 2:
1389 PostSetNeedsCommitToMainThread();
1390 break;
1391 case 4:
1392 PostSetNeedsCommitToMainThread();
1393 break;
1397 void WillActivateTreeOnThread(LayerTreeHostImpl* impl) override {
1398 switch (time_step_) {
1399 case 1:
1400 // The resource creation callback has been called.
1401 EXPECT_EQ(1, ui_resource_->resource_create_count);
1402 // The resource is not yet lost (sanity check).
1403 EXPECT_EQ(0, ui_resource_->lost_resource_count);
1404 // The resource should not have been created yet on the impl-side.
1405 EXPECT_EQ(0u, impl->ResourceIdForUIResource(ui_resource_->id()));
1406 LoseContext();
1407 break;
1408 case 3:
1409 LoseContext();
1410 break;
1414 void DidActivateTreeOnThread(LayerTreeHostImpl* impl) override {
1415 LayerTreeHostContextTest::DidActivateTreeOnThread(impl);
1416 switch (time_step_) {
1417 case 1:
1418 // The pending requests on the impl-side should have been processed.
1419 EXPECT_NE(0u, impl->ResourceIdForUIResource(ui_resource_->id()));
1420 break;
1421 case 2:
1422 // The "lost resource" callback should have been called once.
1423 EXPECT_EQ(1, ui_resource_->lost_resource_count);
1424 break;
1425 case 4:
1426 // The resource is deleted and should not be in the manager. Use
1427 // test_id_ since ui_resource_ has been deleted.
1428 EXPECT_EQ(0u, impl->ResourceIdForUIResource(test_id_));
1429 break;
1432 PostStepCompleteToMainThread();
1433 ++time_step_;
1436 private:
1437 UIResourceId test_id_;
1440 SINGLE_AND_MULTI_THREAD_TEST_F(UIResourceLostBeforeActivateTree);
1442 // Resources evicted explicitly and by visibility changes.
1443 class UIResourceLostEviction : public UIResourceLostTestSimple {
1444 public:
1445 void StepCompleteOnMainThread(int step) override {
1446 EXPECT_TRUE(layer_tree_host()->proxy()->IsMainThread());
1447 switch (step) {
1448 case 0:
1449 ui_resource_ = FakeScopedUIResource::Create(layer_tree_host());
1450 EXPECT_NE(0, ui_resource_->id());
1451 PostSetNeedsCommitToMainThread();
1452 break;
1453 case 2:
1454 // Make the tree not visible.
1455 PostSetVisibleToMainThread(false);
1456 break;
1457 case 3:
1458 // Release resource before ending the test.
1459 ui_resource_ = nullptr;
1460 EndTest();
1461 break;
1462 case 4:
1463 NOTREACHED();
1467 void DidSetVisibleOnImplTree(LayerTreeHostImpl* impl, bool visible) override {
1468 TestWebGraphicsContext3D* context = TestContext();
1469 if (!visible) {
1470 // All resources should have been evicted.
1471 ASSERT_EQ(0u, context->NumTextures());
1472 EXPECT_EQ(0u, impl->ResourceIdForUIResource(ui_resource_->id()));
1473 EXPECT_EQ(2, ui_resource_->resource_create_count);
1474 EXPECT_EQ(1, ui_resource_->lost_resource_count);
1475 // Drawing is disabled both because of the evicted resources and
1476 // because the renderer is not visible.
1477 EXPECT_FALSE(impl->CanDraw());
1478 // Make the renderer visible again.
1479 PostSetVisibleToMainThread(true);
1483 void StepCompleteOnImplThread(LayerTreeHostImpl* impl) override {
1484 TestWebGraphicsContext3D* context = TestContext();
1485 LayerTreeHostContextTest::CommitCompleteOnThread(impl);
1486 switch (time_step_) {
1487 case 1:
1488 // The resource should have been created on LTHI after the commit.
1489 ASSERT_EQ(1u, context->NumTextures());
1490 EXPECT_NE(0u, impl->ResourceIdForUIResource(ui_resource_->id()));
1491 EXPECT_EQ(1, ui_resource_->resource_create_count);
1492 EXPECT_EQ(0, ui_resource_->lost_resource_count);
1493 EXPECT_TRUE(impl->CanDraw());
1494 // Evict all UI resources. This will trigger a commit.
1495 impl->EvictAllUIResources();
1496 ASSERT_EQ(0u, context->NumTextures());
1497 EXPECT_EQ(0u, impl->ResourceIdForUIResource(ui_resource_->id()));
1498 EXPECT_EQ(1, ui_resource_->resource_create_count);
1499 EXPECT_EQ(0, ui_resource_->lost_resource_count);
1500 EXPECT_FALSE(impl->CanDraw());
1501 break;
1502 case 2:
1503 // The resource should have been recreated.
1504 ASSERT_EQ(1u, context->NumTextures());
1505 EXPECT_NE(0u, impl->ResourceIdForUIResource(ui_resource_->id()));
1506 EXPECT_EQ(2, ui_resource_->resource_create_count);
1507 EXPECT_EQ(1, ui_resource_->lost_resource_count);
1508 EXPECT_TRUE(impl->CanDraw());
1509 break;
1510 case 3:
1511 // The resource should have been recreated after visibility was
1512 // restored.
1513 ASSERT_EQ(1u, context->NumTextures());
1514 EXPECT_NE(0u, impl->ResourceIdForUIResource(ui_resource_->id()));
1515 EXPECT_EQ(3, ui_resource_->resource_create_count);
1516 EXPECT_EQ(2, ui_resource_->lost_resource_count);
1517 EXPECT_TRUE(impl->CanDraw());
1518 break;
1523 SINGLE_AND_MULTI_THREAD_TEST_F(UIResourceLostEviction);
1525 class LayerTreeHostContextTestLoseAfterSendingBeginMainFrame
1526 : public LayerTreeHostContextTest {
1527 protected:
1528 void BeginTest() override {
1529 deferred_ = false;
1530 PostSetNeedsCommitToMainThread();
1533 void ScheduledActionWillSendBeginMainFrame() override {
1534 if (deferred_)
1535 return;
1536 deferred_ = true;
1538 // Defer commits before the BeginFrame arrives, causing it to be delayed.
1539 PostSetDeferCommitsToMainThread(true);
1540 // Meanwhile, lose the context while we are in defer commits.
1541 ImplThreadTaskRunner()->PostTask(
1542 FROM_HERE,
1543 base::Bind(&LayerTreeHostContextTestLoseAfterSendingBeginMainFrame::
1544 LoseContextOnImplThread,
1545 base::Unretained(this)));
1548 void LoseContextOnImplThread() {
1549 LoseContext();
1551 // After losing the context, stop deferring commits.
1552 PostSetDeferCommitsToMainThread(false);
1555 void WillBeginMainFrame() override {
1556 // Don't begin a frame with a lost surface.
1557 EXPECT_FALSE(layer_tree_host()->output_surface_lost());
1560 void DidCommitAndDrawFrame() override { EndTest(); }
1562 void AfterTest() override {}
1564 bool deferred_;
1567 SINGLE_AND_MULTI_THREAD_TEST_F(
1568 LayerTreeHostContextTestLoseAfterSendingBeginMainFrame);
1570 } // namespace
1571 } // namespace cc