Disable flaky UnloadTest.CrossSiteInfiniteBeforeUnloadSync on Mac.
[chromium-blink-merge.git] / cc / trees / layer_tree_host_unittest_delegated.cc
blob6d0fb0a886adec78f31fdd634580e33c088d46da
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/trees/layer_tree_host.h"
7 #include <algorithm>
9 #include "base/bind.h"
10 #include "base/location.h"
11 #include "base/synchronization/waitable_event.h"
12 #include "base/threading/thread.h"
13 #include "base/time/time.h"
14 #include "cc/layers/delegated_frame_provider.h"
15 #include "cc/layers/delegated_frame_resource_collection.h"
16 #include "cc/layers/delegated_renderer_layer.h"
17 #include "cc/layers/delegated_renderer_layer_impl.h"
18 #include "cc/output/compositor_frame.h"
19 #include "cc/output/compositor_frame_ack.h"
20 #include "cc/output/delegated_frame_data.h"
21 #include "cc/quads/render_pass_draw_quad.h"
22 #include "cc/quads/shared_quad_state.h"
23 #include "cc/quads/texture_draw_quad.h"
24 #include "cc/resources/returned_resource.h"
25 #include "cc/test/fake_delegated_renderer_layer.h"
26 #include "cc/test/fake_delegated_renderer_layer_impl.h"
27 #include "cc/test/fake_output_surface.h"
28 #include "cc/test/layer_tree_test.h"
29 #include "cc/trees/layer_tree_impl.h"
30 #include "gpu/GLES2/gl2extchromium.h"
32 namespace cc {
33 namespace {
35 bool ReturnedResourceLower(const ReturnedResource& a,
36 const ReturnedResource& b) {
37 return a.id < b.id;
40 // Tests if the list of resources matches an expectation, modulo the order.
41 bool ResourcesMatch(ReturnedResourceArray actual,
42 unsigned* expected,
43 size_t expected_count) {
44 std::sort(actual.begin(), actual.end(), ReturnedResourceLower);
45 std::sort(expected, expected + expected_count);
46 size_t actual_index = 0;
48 // for each element of the expected array, count off one of the actual array
49 // (after checking it matches).
50 for (size_t expected_index = 0; expected_index < expected_count;
51 ++expected_index) {
52 EXPECT_LT(actual_index, actual.size());
53 if (actual_index >= actual.size())
54 return false;
55 EXPECT_EQ(actual[actual_index].id, expected[expected_index]);
56 if (actual[actual_index].id != expected[expected_index])
57 return false;
58 EXPECT_GT(actual[actual_index].count, 0);
59 if (actual[actual_index].count <= 0) {
60 return false;
61 } else {
62 --actual[actual_index].count;
63 if (actual[actual_index].count == 0)
64 ++actual_index;
67 EXPECT_EQ(actual_index, actual.size());
68 return actual_index == actual.size();
71 #define EXPECT_RESOURCES(expected, actual) \
72 EXPECT_TRUE(ResourcesMatch(actual, expected, arraysize(expected)));
74 // These tests deal with delegated renderer layers.
75 class LayerTreeHostDelegatedTest : public LayerTreeTest {
76 protected:
77 scoped_ptr<DelegatedFrameData> CreateFrameData(
78 const gfx::Rect& root_output_rect,
79 const gfx::Rect& root_damage_rect) {
80 scoped_ptr<DelegatedFrameData> frame(new DelegatedFrameData);
82 scoped_ptr<RenderPass> root_pass(RenderPass::Create());
83 root_pass->SetNew(RenderPassId(1, 1),
84 root_output_rect,
85 root_damage_rect,
86 gfx::Transform());
87 frame->render_pass_list.push_back(root_pass.Pass());
88 return frame.Pass();
91 scoped_ptr<DelegatedFrameData> CreateInvalidFrameData(
92 const gfx::Rect& root_output_rect,
93 const gfx::Rect& root_damage_rect) {
94 scoped_ptr<DelegatedFrameData> frame(new DelegatedFrameData);
96 scoped_ptr<RenderPass> root_pass(RenderPass::Create());
97 root_pass->SetNew(RenderPassId(1, 1),
98 root_output_rect,
99 root_damage_rect,
100 gfx::Transform());
102 SharedQuadState* shared_quad_state =
103 root_pass->CreateAndAppendSharedQuadState();
105 gfx::Rect rect = root_output_rect;
106 gfx::Rect opaque_rect = root_output_rect;
107 gfx::Rect visible_rect = root_output_rect;
108 // An invalid resource id! The resource isn't part of the frame.
109 unsigned resource_id = 5;
110 bool premultiplied_alpha = false;
111 gfx::PointF uv_top_left = gfx::PointF(0.f, 0.f);
112 gfx::PointF uv_bottom_right = gfx::PointF(1.f, 1.f);
113 SkColor background_color = 0;
114 float vertex_opacity[4] = {1.f, 1.f, 1.f, 1.f};
115 bool flipped = false;
116 bool nearest_neighbor = false;
118 TextureDrawQuad* invalid_draw_quad =
119 root_pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
120 invalid_draw_quad->SetNew(shared_quad_state,
121 rect,
122 opaque_rect,
123 visible_rect,
124 resource_id,
125 premultiplied_alpha,
126 uv_top_left,
127 uv_bottom_right,
128 background_color,
129 vertex_opacity,
130 flipped,
131 nearest_neighbor);
133 frame->render_pass_list.push_back(root_pass.Pass());
134 return frame.Pass();
137 void AddTransferableResource(DelegatedFrameData* frame,
138 ResourceProvider::ResourceId resource_id) {
139 TransferableResource resource;
140 resource.id = resource_id;
141 resource.mailbox_holder.texture_target = GL_TEXTURE_2D;
142 GLbyte arbitrary_mailbox[GL_MAILBOX_SIZE_CHROMIUM] = {
143 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2,
144 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4,
145 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4};
146 resource.mailbox_holder.mailbox.SetName(arbitrary_mailbox);
147 frame->resource_list.push_back(resource);
150 void AddTextureQuad(DelegatedFrameData* frame,
151 ResourceProvider::ResourceId resource_id) {
152 RenderPass* render_pass = frame->render_pass_list[0];
153 SharedQuadState* sqs = render_pass->CreateAndAppendSharedQuadState();
154 TextureDrawQuad* quad =
155 render_pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
156 float vertex_opacity[4] = { 1.f, 1.f, 1.f, 1.f };
157 quad->SetNew(sqs,
158 gfx::Rect(0, 0, 10, 10),
159 gfx::Rect(0, 0, 10, 10),
160 gfx::Rect(0, 0, 10, 10),
161 resource_id,
162 false,
163 gfx::PointF(0.f, 0.f),
164 gfx::PointF(1.f, 1.f),
165 SK_ColorTRANSPARENT,
166 vertex_opacity,
167 false,
168 false);
171 void AddRenderPass(DelegatedFrameData* frame,
172 RenderPassId id,
173 const gfx::Rect& output_rect,
174 const gfx::Rect& damage_rect,
175 const FilterOperations& filters,
176 const FilterOperations& background_filters) {
177 for (size_t i = 0; i < frame->render_pass_list.size(); ++i)
178 DCHECK(id != frame->render_pass_list[i]->id);
180 scoped_ptr<RenderPass> pass(RenderPass::Create());
181 pass->SetNew(id,
182 output_rect,
183 damage_rect,
184 gfx::Transform());
185 frame->render_pass_list.push_back(pass.Pass());
187 RenderPass* render_pass = frame->render_pass_list[0];
188 SharedQuadState* sqs = render_pass->CreateAndAppendSharedQuadState();
189 RenderPassDrawQuad* quad =
190 render_pass->CreateAndAppendDrawQuad<RenderPassDrawQuad>();
192 quad->SetNew(sqs,
193 output_rect,
194 output_rect,
197 gfx::Vector2dF(),
198 gfx::Size(),
199 filters,
200 gfx::Vector2dF(),
201 background_filters);
204 static ResourceProvider::ResourceId AppendResourceId(
205 std::vector<ResourceProvider::ResourceId>* resources_in_last_sent_frame,
206 ResourceProvider::ResourceId resource_id) {
207 resources_in_last_sent_frame->push_back(resource_id);
208 return resource_id;
211 void ReturnUnusedResourcesFromParent(LayerTreeHostImpl* host_impl) {
212 DelegatedFrameData* delegated_frame_data =
213 output_surface()->last_sent_frame().delegated_frame_data.get();
214 if (!delegated_frame_data)
215 return;
217 std::vector<ResourceProvider::ResourceId> resources_in_last_sent_frame;
218 for (size_t i = 0; i < delegated_frame_data->resource_list.size(); ++i) {
219 resources_in_last_sent_frame.push_back(
220 delegated_frame_data->resource_list[i].id);
223 std::vector<ResourceProvider::ResourceId> resources_to_return;
225 const TransferableResourceArray& resources_held_by_parent =
226 output_surface()->resources_held_by_parent();
227 for (size_t i = 0; i < resources_held_by_parent.size(); ++i) {
228 ResourceProvider::ResourceId resource_in_parent =
229 resources_held_by_parent[i].id;
230 bool resource_in_parent_is_not_part_of_frame =
231 std::find(resources_in_last_sent_frame.begin(),
232 resources_in_last_sent_frame.end(),
233 resource_in_parent) == resources_in_last_sent_frame.end();
234 if (resource_in_parent_is_not_part_of_frame)
235 resources_to_return.push_back(resource_in_parent);
238 if (resources_to_return.empty())
239 return;
241 CompositorFrameAck ack;
242 for (size_t i = 0; i < resources_to_return.size(); ++i)
243 output_surface()->ReturnResource(resources_to_return[i], &ack);
244 host_impl->ReclaimResources(&ack);
247 void ReturnAllResourcesFromParent(LayerTreeHostImpl* host_impl) {
248 DelegatedFrameData* delegated_frame_data =
249 output_surface()->last_sent_frame().delegated_frame_data.get();
250 if (!delegated_frame_data)
251 return;
253 const TransferableResourceArray& resources_held_by_parent =
254 output_surface()->resources_held_by_parent();
256 if (resources_held_by_parent.empty())
257 return;
259 CompositorFrameAck ack;
260 for (size_t i = 0; i < resources_held_by_parent.size(); ++i)
261 output_surface()->ReturnResource(resources_held_by_parent[i].id, &ack);
262 host_impl->ReclaimResources(&ack);
266 class LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
267 : public LayerTreeHostDelegatedTest,
268 public DelegatedFrameResourceCollectionClient {
269 public:
270 LayerTreeHostDelegatedTestCaseSingleDelegatedLayer()
271 : resource_collection_(new DelegatedFrameResourceCollection),
272 available_(false) {
273 resource_collection_->SetClient(this);
276 void SetupTree() override {
277 root_ = Layer::Create();
278 root_->SetBounds(gfx::Size(15, 15));
280 layer_tree_host()->SetRootLayer(root_);
281 LayerTreeHostDelegatedTest::SetupTree();
284 void BeginTest() override {
285 resource_collection_->SetClient(this);
286 PostSetNeedsCommitToMainThread();
289 void SetFrameData(scoped_ptr<DelegatedFrameData> frame_data) {
290 RenderPass* root_pass = frame_data->render_pass_list.back();
291 gfx::Size frame_size = root_pass->output_rect.size();
293 if (frame_provider_.get() && frame_size == frame_provider_->frame_size()) {
294 frame_provider_->SetFrameData(frame_data.Pass());
295 return;
298 if (delegated_.get()) {
299 delegated_->RemoveFromParent();
300 delegated_ = NULL;
301 frame_provider_ = NULL;
304 frame_provider_ = new DelegatedFrameProvider(resource_collection_.get(),
305 frame_data.Pass());
307 delegated_ = CreateDelegatedLayer(frame_provider_.get());
310 scoped_refptr<DelegatedRendererLayer> CreateDelegatedLayer(
311 DelegatedFrameProvider* frame_provider) {
312 scoped_refptr<DelegatedRendererLayer> delegated =
313 FakeDelegatedRendererLayer::Create(frame_provider);
314 delegated->SetBounds(gfx::Size(10, 10));
315 delegated->SetIsDrawable(true);
317 root_->AddChild(delegated);
318 return delegated;
321 void AfterTest() override { resource_collection_->SetClient(NULL); }
323 // DelegatedFrameProviderClient implementation.
324 void UnusedResourcesAreAvailable() override { available_ = true; }
326 bool TestAndResetAvailable() {
327 bool available = available_;
328 available_ = false;
329 return available;
332 protected:
333 scoped_refptr<DelegatedFrameResourceCollection> resource_collection_;
334 scoped_refptr<DelegatedFrameProvider> frame_provider_;
335 scoped_refptr<Layer> root_;
336 scoped_refptr<DelegatedRendererLayer> delegated_;
337 bool available_;
340 class LayerTreeHostDelegatedTestCreateChildId
341 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
342 public:
343 LayerTreeHostDelegatedTestCreateChildId()
344 : LayerTreeHostDelegatedTestCaseSingleDelegatedLayer(),
345 num_activates_(0),
346 did_reset_child_id_(false) {}
348 void DidCommit() override {
349 if (TestEnded())
350 return;
351 SetFrameData(CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1)));
354 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
355 if (host_impl->active_tree()->source_frame_number() < 1)
356 return;
358 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
359 FakeDelegatedRendererLayerImpl* delegated_impl =
360 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
362 TestContextProvider* context_provider = static_cast<TestContextProvider*>(
363 host_impl->output_surface()->context_provider());
365 ++num_activates_;
366 switch (num_activates_) {
367 case 2:
368 EXPECT_TRUE(delegated_impl->ChildId());
369 EXPECT_FALSE(did_reset_child_id_);
371 context_provider->ContextGL()->LoseContextCHROMIUM(
372 GL_GUILTY_CONTEXT_RESET_ARB,
373 GL_INNOCENT_CONTEXT_RESET_ARB);
374 context_provider->ContextGL()->Flush();
375 break;
376 case 3:
377 EXPECT_TRUE(delegated_impl->ChildId());
378 EXPECT_TRUE(did_reset_child_id_);
379 EndTest();
380 break;
384 void InitializedRendererOnThread(LayerTreeHostImpl* host_impl,
385 bool success) override {
386 EXPECT_TRUE(success);
388 if (num_activates_ < 2)
389 return;
391 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
392 FakeDelegatedRendererLayerImpl* delegated_impl =
393 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
395 EXPECT_EQ(2, num_activates_);
396 EXPECT_FALSE(delegated_impl->ChildId());
397 did_reset_child_id_ = true;
400 protected:
401 int num_activates_;
402 bool did_reset_child_id_;
405 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestCreateChildId);
407 // Test that we can gracefully handle invalid frames after the context was lost.
408 // For example, we might be trying to use the previous frame in that case and
409 // have to make sure we don't crash because our resource accounting goes wrong.
410 class LayerTreeHostDelegatedTestInvalidFrameAfterContextLost
411 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
412 public:
413 LayerTreeHostDelegatedTestInvalidFrameAfterContextLost()
414 : num_activates_(0), num_output_surfaces_initialized_(0) {}
416 void DidCommit() override {
417 if (TestEnded())
418 return;
419 scoped_ptr<DelegatedFrameData> frame1 =
420 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
421 AddTextureQuad(frame1.get(), 999);
422 AddTransferableResource(frame1.get(), 999);
423 SetFrameData(frame1.Pass());
426 void DidInitializeOutputSurface() override {
427 if (!num_output_surfaces_initialized_++)
428 return;
430 scoped_refptr<DelegatedRendererLayer> old_delegated = delegated_;
431 SetFrameData(
432 CreateInvalidFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1)));
433 // Make sure we end up using the same layer, or we won't test the right
434 // thing, which is to make sure we can handle an invalid frame when using
435 // a stale layer from before the context was lost.
436 DCHECK(delegated_.get() == old_delegated.get());
439 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
440 if (host_impl->active_tree()->source_frame_number() < 1)
441 return;
443 TestContextProvider* context_provider = static_cast<TestContextProvider*>(
444 host_impl->output_surface()->context_provider());
446 ++num_activates_;
447 switch (num_activates_) {
448 case 2:
449 context_provider->ContextGL()->LoseContextCHROMIUM(
450 GL_GUILTY_CONTEXT_RESET_ARB,
451 GL_INNOCENT_CONTEXT_RESET_ARB);
452 break;
453 case 3:
454 EndTest();
455 break;
459 void InitializedRendererOnThread(LayerTreeHostImpl* host_impl,
460 bool success) override {
461 EXPECT_TRUE(success);
463 if (num_activates_ < 2)
464 return;
466 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
467 FakeDelegatedRendererLayerImpl* delegated_impl =
468 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
470 EXPECT_EQ(2, num_activates_);
471 // Resources should have gotten cleared after the context was lost.
472 EXPECT_EQ(0U, delegated_impl->Resources().size());
475 void AfterTest() override {
476 LayerTreeHostDelegatedTestCaseSingleDelegatedLayer::AfterTest();
477 EXPECT_EQ(2, num_output_surfaces_initialized_);
480 protected:
481 int num_activates_;
482 int num_output_surfaces_initialized_;
485 SINGLE_AND_MULTI_THREAD_TEST_F(
486 LayerTreeHostDelegatedTestInvalidFrameAfterContextLost);
488 class LayerTreeHostDelegatedTestLayerUsesFrameDamage
489 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
490 public:
491 void DidCommit() override {
492 int next_source_frame_number = layer_tree_host()->source_frame_number();
493 switch (next_source_frame_number) {
494 case 1:
495 // The first time the layer gets a frame the whole layer should be
496 // damaged.
497 SetFrameData(
498 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1)));
499 break;
500 case 2:
501 // A different frame size will damage the whole layer.
502 SetFrameData(
503 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(0, 0, 0, 0)));
504 break;
505 case 3:
506 // Should create a total amount of gfx::Rect(2, 2, 8, 6) damage:
507 // (2, 2, 10, 6) clamped to the root output rect.
508 SetFrameData(
509 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(2, 2, 5, 5)));
510 SetFrameData(
511 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(7, 2, 5, 6)));
512 break;
513 case 4:
514 // Should create zero damage.
515 layer_tree_host()->SetNeedsCommit();
516 break;
517 case 5:
518 // Should damage the full viewport.
519 delegated_->SetBounds(gfx::Size(2, 2));
520 break;
521 case 6:
522 // Should create zero damage.
523 layer_tree_host()->SetNeedsCommit();
524 break;
525 case 7:
526 // Should damage the full layer, tho the frame size is not changing.
527 delegated_->SetBounds(gfx::Size(6, 6));
528 SetFrameData(
529 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(1, 1, 2, 2)));
530 break;
531 case 8:
532 // Should create zero damage.
533 layer_tree_host()->SetNeedsCommit();
534 break;
535 case 9:
536 // Should create zero damage.
537 layer_tree_host()->SetNeedsCommit();
538 break;
539 case 10:
540 // Changing the frame size damages the full layer.
541 SetFrameData(
542 CreateFrameData(gfx::Rect(0, 0, 5, 5), gfx::Rect(4, 4, 1, 1)));
543 break;
544 case 11:
545 // An invalid frame isn't used, so it should not cause damage.
546 SetFrameData(CreateInvalidFrameData(gfx::Rect(0, 0, 5, 5),
547 gfx::Rect(4, 4, 1, 1)));
548 break;
549 case 12:
550 // Should create gfx::Rect(1, 1, 2, 2) of damage.
551 SetFrameData(
552 CreateFrameData(gfx::Rect(0, 0, 5, 5), gfx::Rect(1, 1, 2, 2)));
553 break;
554 case 13:
555 // Should create zero damage.
556 layer_tree_host()->SetNeedsCommit();
557 break;
558 case 14:
559 // Moving the layer out of the tree and back in will damage the whole
560 // impl layer.
561 delegated_->RemoveFromParent();
562 layer_tree_host()->root_layer()->AddChild(delegated_);
563 break;
564 case 15:
565 // Make a larger frame with lots of damage. Then a frame smaller than
566 // the first frame's damage. The entire layer should be damaged, but
567 // nothing more.
568 SetFrameData(
569 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(0, 0, 10, 10)));
570 SetFrameData(
571 CreateFrameData(gfx::Rect(0, 0, 5, 5), gfx::Rect(1, 1, 2, 2)));
572 break;
573 case 16:
574 // Make a frame with lots of damage. Then replace it with a frame with
575 // no damage. The entire layer should be damaged, but nothing more.
576 SetFrameData(
577 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(0, 0, 10, 10)));
578 SetFrameData(
579 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(0, 0, 0, 0)));
580 break;
581 case 17:
582 // Make another layer that uses the same frame provider. The new layer
583 // should be damaged.
584 delegated_copy_ = CreateDelegatedLayer(frame_provider_.get());
585 delegated_copy_->SetPosition(gfx::Point(5, 0));
587 // Also set a new frame.
588 SetFrameData(
589 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(4, 0, 1, 1)));
590 break;
591 case 18:
592 // Set another new frame, both layers should be damaged in the same
593 // ways.
594 SetFrameData(
595 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(3, 3, 1, 1)));
596 break;
600 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
601 LayerTreeHostImpl::FrameData* frame,
602 DrawResult draw_result) override {
603 EXPECT_EQ(DRAW_SUCCESS, draw_result);
605 gfx::Rect damage_rect;
606 if (!frame->has_no_damage) {
607 damage_rect = frame->render_passes.back()->damage_rect;
608 } else {
609 // If there is no damage, then we have no render passes to send.
610 EXPECT_TRUE(frame->render_passes.empty());
613 switch (host_impl->active_tree()->source_frame_number()) {
614 case 0:
615 // First frame is damaged because of viewport resize.
616 EXPECT_EQ(gfx::Rect(15, 15).ToString(), damage_rect.ToString());
617 break;
618 case 1:
619 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect.ToString());
620 break;
621 case 2:
622 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect.ToString());
623 break;
624 case 3:
625 EXPECT_EQ(gfx::Rect(2, 2, 8, 6).ToString(), damage_rect.ToString());
626 break;
627 case 4:
628 EXPECT_EQ(gfx::Rect().ToString(), damage_rect.ToString());
629 break;
630 case 5:
631 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect.ToString());
632 break;
633 case 6:
634 EXPECT_EQ(gfx::Rect().ToString(), damage_rect.ToString());
635 break;
636 case 7:
637 EXPECT_EQ(gfx::Rect(6, 6).ToString(), damage_rect.ToString());
638 break;
639 case 8:
640 EXPECT_EQ(gfx::Rect().ToString(), damage_rect.ToString());
641 break;
642 case 9:
643 EXPECT_EQ(gfx::Rect().ToString(), damage_rect.ToString());
644 break;
645 case 10:
646 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect.ToString());
647 break;
648 case 11:
649 EXPECT_EQ(gfx::Rect().ToString(), damage_rect.ToString());
650 break;
651 case 12:
652 EXPECT_EQ(gfx::Rect(1, 1, 2, 2).ToString(), damage_rect.ToString());
653 break;
654 case 13:
655 EXPECT_EQ(gfx::Rect().ToString(), damage_rect.ToString());
656 break;
657 case 14:
658 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect.ToString());
659 break;
660 case 15:
661 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect.ToString());
662 break;
663 case 16:
664 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect.ToString());
665 break;
666 case 17:
667 EXPECT_EQ(gfx::UnionRects(gfx::Rect(5, 0, 10, 10),
668 gfx::Rect(4, 0, 1, 1)).ToString(),
669 damage_rect.ToString());
670 break;
671 case 18:
672 EXPECT_EQ(gfx::Rect(3, 3, 6, 1).ToString(), damage_rect.ToString());
673 EndTest();
674 break;
677 return draw_result;
680 protected:
681 scoped_refptr<DelegatedRendererLayer> delegated_copy_;
684 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestLayerUsesFrameDamage);
686 class LayerTreeHostDelegatedTestMergeResources
687 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
688 public:
689 void BeginTest() override {
690 // Push two frames to the delegated renderer layer with no commit between.
692 // The first frame has resource 999.
693 scoped_ptr<DelegatedFrameData> frame1 =
694 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
695 AddTextureQuad(frame1.get(), 999);
696 AddTransferableResource(frame1.get(), 999);
697 SetFrameData(frame1.Pass());
699 // The second frame uses resource 999 still, but also adds 555.
700 scoped_ptr<DelegatedFrameData> frame2 =
701 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
702 AddTextureQuad(frame2.get(), 999);
703 AddTransferableResource(frame2.get(), 999);
704 AddTextureQuad(frame2.get(), 555);
705 AddTransferableResource(frame2.get(), 555);
706 SetFrameData(frame2.Pass());
708 // The resource 999 from frame1 is returned since it is still on the main
709 // thread.
710 ReturnedResourceArray returned_resources;
711 resource_collection_->TakeUnusedResourcesForChildCompositor(
712 &returned_resources);
714 unsigned expected[] = {999};
715 EXPECT_RESOURCES(expected, returned_resources);
716 EXPECT_TRUE(TestAndResetAvailable());
719 PostSetNeedsCommitToMainThread();
722 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
723 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
724 FakeDelegatedRendererLayerImpl* delegated_impl =
725 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
727 const ResourceProvider::ResourceIdMap& map =
728 host_impl->resource_provider()->GetChildToParentMap(
729 delegated_impl->ChildId());
731 // Both frames' resources should be in the parent's resource provider.
732 EXPECT_EQ(2u, map.size());
733 EXPECT_EQ(1u, map.count(999));
734 EXPECT_EQ(1u, map.count(555));
736 EXPECT_EQ(2u, delegated_impl->Resources().size());
737 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
738 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
740 EndTest();
744 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestMergeResources);
746 class LayerTreeHostDelegatedTestRemapResourcesInQuads
747 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
748 public:
749 void BeginTest() override {
750 // Generate a frame with two resources in it.
751 scoped_ptr<DelegatedFrameData> frame =
752 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
753 AddTextureQuad(frame.get(), 999);
754 AddTransferableResource(frame.get(), 999);
755 AddTextureQuad(frame.get(), 555);
756 AddTransferableResource(frame.get(), 555);
757 SetFrameData(frame.Pass());
759 PostSetNeedsCommitToMainThread();
762 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
763 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
764 FakeDelegatedRendererLayerImpl* delegated_impl =
765 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
767 const ResourceProvider::ResourceIdMap& map =
768 host_impl->resource_provider()->GetChildToParentMap(
769 delegated_impl->ChildId());
771 // The frame's resource should be in the parent's resource provider.
772 EXPECT_EQ(2u, map.size());
773 EXPECT_EQ(1u, map.count(999));
774 EXPECT_EQ(1u, map.count(555));
776 ResourceProvider::ResourceId parent_resource_id1 = map.find(999)->second;
777 EXPECT_NE(parent_resource_id1, 999u);
778 ResourceProvider::ResourceId parent_resource_id2 = map.find(555)->second;
779 EXPECT_NE(parent_resource_id2, 555u);
781 // The resources in the quads should be remapped to the parent's namespace.
782 const TextureDrawQuad* quad1 = TextureDrawQuad::MaterialCast(
783 delegated_impl->RenderPassesInDrawOrder()[0]->quad_list.ElementAt(0));
784 EXPECT_EQ(parent_resource_id1, quad1->resource_id);
785 const TextureDrawQuad* quad2 = TextureDrawQuad::MaterialCast(
786 delegated_impl->RenderPassesInDrawOrder()[0]->quad_list.ElementAt(1));
787 EXPECT_EQ(parent_resource_id2, quad2->resource_id);
789 EndTest();
793 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestRemapResourcesInQuads);
795 class LayerTreeHostDelegatedTestReturnUnusedResources
796 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
797 public:
798 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
800 void DidCommitAndDrawFrame() override {
801 scoped_ptr<DelegatedFrameData> frame;
802 ReturnedResourceArray resources;
804 int next_source_frame_number = layer_tree_host()->source_frame_number();
805 switch (next_source_frame_number) {
806 case 1:
807 // Generate a frame with two resources in it.
808 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
809 AddTextureQuad(frame.get(), 999);
810 AddTransferableResource(frame.get(), 999);
811 AddTextureQuad(frame.get(), 555);
812 AddTransferableResource(frame.get(), 555);
813 SetFrameData(frame.Pass());
814 break;
815 case 2:
816 // All of the resources are in use.
817 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
818 EXPECT_EQ(0u, resources.size());
819 EXPECT_FALSE(TestAndResetAvailable());
821 // Keep using 999 but stop using 555.
822 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
823 AddTextureQuad(frame.get(), 999);
824 AddTransferableResource(frame.get(), 999);
825 AddTextureQuad(frame.get(), 444);
826 AddTransferableResource(frame.get(), 444);
827 SetFrameData(frame.Pass());
828 break;
829 case 3:
830 // 555 is no longer in use.
831 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
833 unsigned expected[] = {555};
834 EXPECT_RESOURCES(expected, resources);
835 EXPECT_TRUE(TestAndResetAvailable());
838 // Stop using any resources.
839 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
840 SetFrameData(frame.Pass());
841 break;
842 case 4:
843 // Postpone collecting resources for a frame. They should still be there
844 // the next frame.
845 layer_tree_host()->SetNeedsCommit();
846 return;
847 case 5:
848 // 444 and 999 are no longer in use. We sent two refs to 999, so we
849 // should get two back.
850 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
852 unsigned expected[] = {444, 999, 999};
853 EXPECT_RESOURCES(expected, resources);
854 EXPECT_TRUE(TestAndResetAvailable());
856 EndTest();
857 break;
860 // Resources are never immediately released.
861 ReturnedResourceArray empty_resources;
862 resource_collection_->TakeUnusedResourcesForChildCompositor(
863 &empty_resources);
864 EXPECT_EQ(0u, empty_resources.size());
865 EXPECT_FALSE(TestAndResetAvailable());
868 void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, bool result) override {
869 ReturnUnusedResourcesFromParent(host_impl);
873 SINGLE_AND_MULTI_THREAD_TEST_F(
874 LayerTreeHostDelegatedTestReturnUnusedResources);
876 class LayerTreeHostDelegatedTestReusedResources
877 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
878 public:
879 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
881 void DidCommitAndDrawFrame() override {
882 scoped_ptr<DelegatedFrameData> frame;
883 ReturnedResourceArray resources;
885 int next_source_frame_number = layer_tree_host()->source_frame_number();
886 switch (next_source_frame_number) {
887 case 1:
888 // Generate a frame with some resources in it.
889 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
890 AddTextureQuad(frame.get(), 999);
891 AddTransferableResource(frame.get(), 999);
892 AddTextureQuad(frame.get(), 555);
893 AddTransferableResource(frame.get(), 555);
894 AddTextureQuad(frame.get(), 444);
895 AddTransferableResource(frame.get(), 444);
896 SetFrameData(frame.Pass());
897 break;
898 case 2:
899 // All of the resources are in use.
900 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
901 EXPECT_EQ(0u, resources.size());
902 EXPECT_FALSE(TestAndResetAvailable());
904 // Keep using 999 but stop using 555 and 444.
905 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
906 AddTextureQuad(frame.get(), 999);
907 AddTransferableResource(frame.get(), 999);
908 SetFrameData(frame.Pass());
910 // Resource are not immediately released.
911 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
912 EXPECT_EQ(0u, resources.size());
913 EXPECT_FALSE(TestAndResetAvailable());
915 // Now using 555 and 444 again, but not 999.
916 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
917 AddTextureQuad(frame.get(), 555);
918 AddTransferableResource(frame.get(), 555);
919 AddTextureQuad(frame.get(), 444);
920 AddTransferableResource(frame.get(), 444);
921 SetFrameData(frame.Pass());
922 break;
923 case 3:
924 // The 999 resource is the only unused one. Two references were sent, so
925 // two should be returned.
926 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
928 unsigned expected[] = {999, 999};
929 EXPECT_RESOURCES(expected, resources);
930 EXPECT_TRUE(TestAndResetAvailable());
932 EndTest();
933 break;
937 void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, bool result) override {
938 ReturnUnusedResourcesFromParent(host_impl);
942 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestReusedResources);
944 class LayerTreeHostDelegatedTestFrameBeforeAck
945 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
946 public:
947 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
949 void DidCommitAndDrawFrame() override {
950 scoped_ptr<DelegatedFrameData> frame;
951 ReturnedResourceArray resources;
953 int next_source_frame_number = layer_tree_host()->source_frame_number();
954 switch (next_source_frame_number) {
955 case 1:
956 // Generate a frame with some resources in it.
957 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
958 AddTextureQuad(frame.get(), 999);
959 AddTransferableResource(frame.get(), 999);
960 AddTextureQuad(frame.get(), 555);
961 AddTransferableResource(frame.get(), 555);
962 AddTextureQuad(frame.get(), 444);
963 AddTransferableResource(frame.get(), 444);
964 SetFrameData(frame.Pass());
965 break;
966 case 2:
967 // All of the resources are in use.
968 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
969 EXPECT_EQ(0u, resources.size());
970 EXPECT_FALSE(TestAndResetAvailable());
972 // Keep using 999 but stop using 555 and 444.
973 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
974 AddTextureQuad(frame.get(), 999);
975 AddTransferableResource(frame.get(), 999);
976 SetFrameData(frame.Pass());
978 // Resource are not immediately released.
979 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
980 EXPECT_EQ(0u, resources.size());
981 EXPECT_FALSE(TestAndResetAvailable());
983 // The parent compositor (this one) does a commit.
984 break;
985 case 3:
986 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
988 unsigned expected[] = {444, 555};
989 EXPECT_RESOURCES(expected, resources);
990 EXPECT_TRUE(TestAndResetAvailable());
993 // The child compositor sends a frame referring to resources not in the
994 // frame.
995 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
996 AddTextureQuad(frame.get(), 999);
997 AddTextureQuad(frame.get(), 555);
998 AddTextureQuad(frame.get(), 444);
999 SetFrameData(frame.Pass());
1000 break;
1004 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
1005 if (host_impl->active_tree()->source_frame_number() != 3)
1006 return;
1008 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1009 FakeDelegatedRendererLayerImpl* delegated_impl =
1010 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1012 const ResourceProvider::ResourceIdMap& map =
1013 host_impl->resource_provider()->GetChildToParentMap(
1014 delegated_impl->ChildId());
1016 // The bad frame should be dropped. So we should only have one quad (the
1017 // one with resource 999) on the impl tree. And only 999 will be present
1018 // in the parent's resource provider.
1019 EXPECT_EQ(1u, map.size());
1020 EXPECT_EQ(1u, map.count(999));
1022 EXPECT_EQ(1u, delegated_impl->Resources().size());
1023 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1025 const RenderPass* pass = delegated_impl->RenderPassesInDrawOrder()[0];
1026 EXPECT_EQ(1u, pass->quad_list.size());
1027 const TextureDrawQuad* quad =
1028 TextureDrawQuad::MaterialCast(pass->quad_list.front());
1029 EXPECT_EQ(map.find(999)->second, quad->resource_id);
1031 EndTest();
1034 void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, bool result) override {
1035 ReturnUnusedResourcesFromParent(host_impl);
1039 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestFrameBeforeAck);
1041 class LayerTreeHostDelegatedTestFrameBeforeTakeResources
1042 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1043 public:
1044 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
1046 void DidCommitAndDrawFrame() override {
1047 scoped_ptr<DelegatedFrameData> frame;
1048 ReturnedResourceArray resources;
1050 int next_source_frame_number = layer_tree_host()->source_frame_number();
1051 switch (next_source_frame_number) {
1052 case 1:
1053 // Generate a frame with some resources in it.
1054 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1055 AddTextureQuad(frame.get(), 999);
1056 AddTransferableResource(frame.get(), 999);
1057 AddTextureQuad(frame.get(), 555);
1058 AddTransferableResource(frame.get(), 555);
1059 AddTextureQuad(frame.get(), 444);
1060 AddTransferableResource(frame.get(), 444);
1061 SetFrameData(frame.Pass());
1062 break;
1063 case 2:
1064 // All of the resources are in use.
1065 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1066 EXPECT_EQ(0u, resources.size());
1067 EXPECT_FALSE(TestAndResetAvailable());
1069 // Keep using 999 but stop using 555 and 444.
1070 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1071 AddTextureQuad(frame.get(), 999);
1072 AddTransferableResource(frame.get(), 999);
1073 SetFrameData(frame.Pass());
1075 // Resource are not immediately released.
1076 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1077 EXPECT_EQ(0u, resources.size());
1078 EXPECT_FALSE(TestAndResetAvailable());
1080 // The parent compositor (this one) does a commit.
1081 break;
1082 case 3:
1083 // The child compositor sends a frame before taking resources back
1084 // from the previous commit. This frame makes use of the resources 555
1085 // and 444, which were just released during commit.
1086 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1087 AddTextureQuad(frame.get(), 999);
1088 AddTransferableResource(frame.get(), 999);
1089 AddTextureQuad(frame.get(), 555);
1090 AddTransferableResource(frame.get(), 555);
1091 AddTextureQuad(frame.get(), 444);
1092 AddTransferableResource(frame.get(), 444);
1093 SetFrameData(frame.Pass());
1095 // The resources are used by the new frame but are returned anyway since
1096 // we passed them again.
1097 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1099 unsigned expected[] = {444, 555};
1100 EXPECT_RESOURCES(expected, resources);
1101 EXPECT_TRUE(TestAndResetAvailable());
1103 break;
1104 case 4:
1105 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1106 EXPECT_EQ(0u, resources.size());
1107 EXPECT_FALSE(TestAndResetAvailable());
1108 EndTest();
1109 break;
1113 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
1114 if (host_impl->active_tree()->source_frame_number() != 3)
1115 return;
1117 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1118 FakeDelegatedRendererLayerImpl* delegated_impl =
1119 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1121 const ResourceProvider::ResourceIdMap& map =
1122 host_impl->resource_provider()->GetChildToParentMap(
1123 delegated_impl->ChildId());
1125 // The third frame has all of the resources in it again, the delegated
1126 // renderer layer should continue to own the resources for it.
1127 EXPECT_EQ(3u, map.size());
1128 EXPECT_EQ(1u, map.count(999));
1129 EXPECT_EQ(1u, map.count(555));
1130 EXPECT_EQ(1u, map.count(444));
1132 EXPECT_EQ(3u, delegated_impl->Resources().size());
1133 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1134 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1135 EXPECT_EQ(1u, delegated_impl->Resources().count(444));
1137 const RenderPass* pass = delegated_impl->RenderPassesInDrawOrder()[0];
1138 EXPECT_EQ(3u, pass->quad_list.size());
1139 const TextureDrawQuad* quad1 =
1140 TextureDrawQuad::MaterialCast(pass->quad_list.ElementAt(0));
1141 EXPECT_EQ(map.find(999)->second, quad1->resource_id);
1142 const TextureDrawQuad* quad2 =
1143 TextureDrawQuad::MaterialCast(pass->quad_list.ElementAt(1));
1144 EXPECT_EQ(map.find(555)->second, quad2->resource_id);
1145 const TextureDrawQuad* quad3 =
1146 TextureDrawQuad::MaterialCast(pass->quad_list.ElementAt(2));
1147 EXPECT_EQ(map.find(444)->second, quad3->resource_id);
1150 void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, bool result) override {
1151 ReturnUnusedResourcesFromParent(host_impl);
1155 SINGLE_AND_MULTI_THREAD_TEST_F(
1156 LayerTreeHostDelegatedTestFrameBeforeTakeResources);
1158 class LayerTreeHostDelegatedTestBadFrame
1159 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1160 public:
1161 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
1163 void DidCommitAndDrawFrame() override {
1164 scoped_ptr<DelegatedFrameData> frame;
1165 ReturnedResourceArray resources;
1167 int next_source_frame_number = layer_tree_host()->source_frame_number();
1168 switch (next_source_frame_number) {
1169 case 1:
1170 // Generate a frame with some resources in it.
1171 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1172 AddTextureQuad(frame.get(), 999);
1173 AddTransferableResource(frame.get(), 999);
1174 AddTextureQuad(frame.get(), 555);
1175 AddTransferableResource(frame.get(), 555);
1176 SetFrameData(frame.Pass());
1177 break;
1178 case 2:
1179 // All of the resources are in use.
1180 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1181 EXPECT_EQ(0u, resources.size());
1182 EXPECT_FALSE(TestAndResetAvailable());
1184 // Generate a bad frame with a resource the layer doesn't have. The
1185 // 885 and 775 resources are unknown, while ownership of the legit 444
1186 // resource is passed in here. The bad frame does not use any of the
1187 // previous resources, 999 or 555.
1188 // A bad quad is present both before and after the good quad.
1189 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1190 AddTextureQuad(frame.get(), 885);
1191 AddTextureQuad(frame.get(), 444);
1192 AddTransferableResource(frame.get(), 444);
1193 AddTextureQuad(frame.get(), 775);
1194 SetFrameData(frame.Pass());
1196 // The parent compositor (this one) does a commit.
1197 break;
1198 case 3:
1199 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1200 EXPECT_EQ(0u, resources.size());
1201 EXPECT_FALSE(TestAndResetAvailable());
1203 // Now send a good frame with 999 again.
1204 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1205 AddTextureQuad(frame.get(), 999);
1206 SetFrameData(frame.Pass());
1208 // The bad frame's resource is given back to the child compositor.
1209 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1211 unsigned expected[] = {444};
1212 EXPECT_RESOURCES(expected, resources);
1213 EXPECT_TRUE(TestAndResetAvailable());
1215 break;
1216 case 4:
1217 // The unused 555 from the last good frame is now released.
1218 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1220 unsigned expected[] = {555};
1221 EXPECT_RESOURCES(expected, resources);
1222 EXPECT_TRUE(TestAndResetAvailable());
1225 EndTest();
1226 break;
1230 void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, bool result) override {
1231 if (host_impl->active_tree()->source_frame_number() < 1)
1232 return;
1234 ReturnUnusedResourcesFromParent(host_impl);
1236 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1237 FakeDelegatedRendererLayerImpl* delegated_impl =
1238 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1240 const ResourceProvider::ResourceIdMap& map =
1241 host_impl->resource_provider()->GetChildToParentMap(
1242 delegated_impl->ChildId());
1244 switch (host_impl->active_tree()->source_frame_number()) {
1245 case 1: {
1246 // We have the first good frame with just 999 and 555 in it.
1247 // layer.
1248 EXPECT_EQ(2u, map.size());
1249 EXPECT_EQ(1u, map.count(999));
1250 EXPECT_EQ(1u, map.count(555));
1252 EXPECT_EQ(2u, delegated_impl->Resources().size());
1253 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1254 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1256 const RenderPass* pass = delegated_impl->RenderPassesInDrawOrder()[0];
1257 EXPECT_EQ(2u, pass->quad_list.size());
1258 const TextureDrawQuad* quad1 =
1259 TextureDrawQuad::MaterialCast(pass->quad_list.ElementAt(0));
1260 EXPECT_EQ(map.find(999)->second, quad1->resource_id);
1261 const TextureDrawQuad* quad2 =
1262 TextureDrawQuad::MaterialCast(pass->quad_list.ElementAt(1));
1263 EXPECT_EQ(map.find(555)->second, quad2->resource_id);
1264 break;
1266 case 2: {
1267 // We only keep resources from the last valid frame.
1268 EXPECT_EQ(2u, map.size());
1269 EXPECT_EQ(1u, map.count(999));
1270 EXPECT_EQ(1u, map.count(555));
1272 EXPECT_EQ(2u, delegated_impl->Resources().size());
1273 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1274 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1276 // The bad frame is dropped though, we still have the frame with 999 and
1277 // 555 in it.
1278 const RenderPass* pass = delegated_impl->RenderPassesInDrawOrder()[0];
1279 EXPECT_EQ(2u, pass->quad_list.size());
1280 const TextureDrawQuad* quad1 =
1281 TextureDrawQuad::MaterialCast(pass->quad_list.ElementAt(0));
1282 EXPECT_EQ(map.find(999)->second, quad1->resource_id);
1283 const TextureDrawQuad* quad2 =
1284 TextureDrawQuad::MaterialCast(pass->quad_list.ElementAt(1));
1285 EXPECT_EQ(map.find(555)->second, quad2->resource_id);
1286 break;
1288 case 3: {
1289 // We have the new good frame with just 999 in it.
1290 EXPECT_EQ(1u, map.size());
1291 EXPECT_EQ(1u, map.count(999));
1293 EXPECT_EQ(1u, delegated_impl->Resources().size());
1294 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1296 const RenderPass* pass = delegated_impl->RenderPassesInDrawOrder()[0];
1297 EXPECT_EQ(1u, pass->quad_list.size());
1298 const TextureDrawQuad* quad1 =
1299 TextureDrawQuad::MaterialCast(pass->quad_list.front());
1300 EXPECT_EQ(map.find(999)->second, quad1->resource_id);
1301 break;
1307 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestBadFrame);
1309 class LayerTreeHostDelegatedTestUnnamedResource
1310 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1311 public:
1312 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
1314 void DidCommitAndDrawFrame() override {
1315 scoped_ptr<DelegatedFrameData> frame;
1316 ReturnedResourceArray resources;
1318 int next_source_frame_number = layer_tree_host()->source_frame_number();
1319 switch (next_source_frame_number) {
1320 case 1:
1321 // This frame includes two resources in it, but only uses one.
1322 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1323 AddTransferableResource(frame.get(), 999);
1324 AddTextureQuad(frame.get(), 555);
1325 AddTransferableResource(frame.get(), 555);
1326 SetFrameData(frame.Pass());
1327 break;
1328 case 2:
1329 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1330 EXPECT_EQ(0u, resources.size());
1331 EXPECT_FALSE(TestAndResetAvailable());
1333 // Now send an empty frame.
1334 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1335 SetFrameData(frame.Pass());
1337 // The unused resource should be returned.
1338 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1340 unsigned expected[] = {999};
1341 EXPECT_RESOURCES(expected, resources);
1342 EXPECT_TRUE(TestAndResetAvailable());
1345 EndTest();
1346 break;
1350 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
1351 if (host_impl->active_tree()->source_frame_number() != 1)
1352 return;
1354 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1355 FakeDelegatedRendererLayerImpl* delegated_impl =
1356 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1358 const ResourceProvider::ResourceIdMap& map =
1359 host_impl->resource_provider()->GetChildToParentMap(
1360 delegated_impl->ChildId());
1362 // The layer only held on to the resource that was used.
1363 EXPECT_EQ(1u, map.size());
1364 EXPECT_EQ(1u, map.count(555));
1366 EXPECT_EQ(1u, delegated_impl->Resources().size());
1367 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1371 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestUnnamedResource);
1373 class LayerTreeHostDelegatedTestDontLeakResource
1374 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1375 public:
1376 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
1378 void DidCommitAndDrawFrame() override {
1379 scoped_ptr<DelegatedFrameData> frame;
1380 ReturnedResourceArray resources;
1382 int next_source_frame_number = layer_tree_host()->source_frame_number();
1383 switch (next_source_frame_number) {
1384 case 1:
1385 // This frame includes two resources in it.
1386 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1387 AddTextureQuad(frame.get(), 999);
1388 AddTransferableResource(frame.get(), 999);
1389 AddTextureQuad(frame.get(), 555);
1390 AddTransferableResource(frame.get(), 555);
1391 SetFrameData(frame.Pass());
1393 // But then we immediately stop using 999.
1394 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1395 AddTextureQuad(frame.get(), 555);
1396 AddTransferableResource(frame.get(), 555);
1397 SetFrameData(frame.Pass());
1398 break;
1399 case 2:
1400 // The unused resources should be returned. 555 is still used, but it's
1401 // returned once to account for the first frame.
1402 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1404 unsigned expected[] = {555, 999};
1405 EXPECT_RESOURCES(expected, resources);
1406 EXPECT_TRUE(TestAndResetAvailable());
1408 // Send a frame with no resources in it.
1409 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1410 SetFrameData(frame.Pass());
1411 break;
1412 case 3:
1413 // The now unused resource 555 should be returned.
1414 resources.clear();
1415 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1417 unsigned expected[] = {555};
1418 EXPECT_RESOURCES(expected, resources);
1419 EXPECT_TRUE(TestAndResetAvailable());
1421 EndTest();
1422 break;
1426 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
1427 if (host_impl->active_tree()->source_frame_number() != 1)
1428 return;
1430 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1431 FakeDelegatedRendererLayerImpl* delegated_impl =
1432 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1434 const ResourceProvider::ResourceIdMap& map =
1435 host_impl->resource_provider()->GetChildToParentMap(
1436 delegated_impl->ChildId());
1438 // The layer only held on to the resource that was used.
1439 EXPECT_EQ(1u, map.size());
1440 EXPECT_EQ(1u, map.count(555));
1442 EXPECT_EQ(1u, delegated_impl->Resources().size());
1443 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1446 void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, bool result) override {
1447 ReturnUnusedResourcesFromParent(host_impl);
1451 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestDontLeakResource);
1453 class LayerTreeHostDelegatedTestResourceSentToParent
1454 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1455 public:
1456 void DidCommitAndDrawFrame() override {
1457 scoped_ptr<DelegatedFrameData> frame;
1458 ReturnedResourceArray resources;
1460 int next_source_frame_number = layer_tree_host()->source_frame_number();
1461 switch (next_source_frame_number) {
1462 case 1:
1463 // This frame includes two resources in it.
1464 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1465 AddTextureQuad(frame.get(), 999);
1466 AddTransferableResource(frame.get(), 999);
1467 AddTextureQuad(frame.get(), 555);
1468 AddTransferableResource(frame.get(), 555);
1469 SetFrameData(frame.Pass());
1470 break;
1471 case 2:
1472 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1473 EXPECT_EQ(0u, resources.size());
1474 EXPECT_FALSE(TestAndResetAvailable());
1476 // 999 is in use in the grandparent compositor, generate a frame without
1477 // it present.
1478 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1479 AddTextureQuad(frame.get(), 555);
1480 AddTransferableResource(frame.get(), 555);
1481 SetFrameData(frame.Pass());
1482 break;
1483 case 3:
1484 // Since 999 is in the grandparent it is not returned.
1485 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1486 EXPECT_EQ(0u, resources.size());
1487 EXPECT_FALSE(TestAndResetAvailable());
1489 // The impl side will get back the resource at some point.
1490 ImplThreadTaskRunner()->PostTask(FROM_HERE,
1491 receive_resource_on_thread_);
1492 break;
1496 void ReceiveResourceOnThread(LayerTreeHostImpl* host_impl) {
1497 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1498 FakeDelegatedRendererLayerImpl* delegated_impl =
1499 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1501 const ResourceProvider::ResourceIdMap& map =
1502 host_impl->resource_provider()->GetChildToParentMap(
1503 delegated_impl->ChildId());
1505 // Receive 999 back from the grandparent.
1506 CompositorFrameAck ack;
1507 output_surface()->ReturnResource(map.find(999)->second, &ack);
1508 host_impl->ReclaimResources(&ack);
1511 void UnusedResourcesAreAvailable() override {
1512 EXPECT_EQ(3, layer_tree_host()->source_frame_number());
1514 ReturnedResourceArray resources;
1516 // 999 was returned from the grandparent and could be released.
1517 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1519 unsigned expected[] = {999};
1520 EXPECT_RESOURCES(expected, resources);
1523 EndTest();
1526 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
1527 if (host_impl->active_tree()->source_frame_number() < 1)
1528 return;
1530 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1531 FakeDelegatedRendererLayerImpl* delegated_impl =
1532 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1534 const ResourceProvider::ResourceIdMap& map =
1535 host_impl->resource_provider()->GetChildToParentMap(
1536 delegated_impl->ChildId());
1538 switch (host_impl->active_tree()->source_frame_number()) {
1539 case 1: {
1540 EXPECT_EQ(2u, map.size());
1541 EXPECT_EQ(1u, map.count(999));
1542 EXPECT_EQ(1u, map.count(555));
1544 EXPECT_EQ(2u, delegated_impl->Resources().size());
1545 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1546 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1548 // The 999 resource will be sent to a grandparent compositor.
1549 break;
1551 case 2: {
1552 EXPECT_EQ(2u, map.size());
1553 EXPECT_EQ(1u, map.count(999));
1554 EXPECT_EQ(1u, map.count(555));
1556 // 999 is in the parent, so not held by delegated renderer layer.
1557 EXPECT_EQ(1u, delegated_impl->Resources().size());
1558 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1560 receive_resource_on_thread_ =
1561 base::Bind(&LayerTreeHostDelegatedTestResourceSentToParent::
1562 ReceiveResourceOnThread,
1563 base::Unretained(this),
1564 host_impl);
1565 break;
1567 case 3:
1568 // 999 should be released.
1569 EXPECT_EQ(1u, map.size());
1570 EXPECT_EQ(1u, map.count(555));
1572 EXPECT_EQ(1u, delegated_impl->Resources().size());
1573 EXPECT_EQ(1u, delegated_impl->Resources().count(map.find(555)->second));
1574 break;
1578 base::Closure receive_resource_on_thread_;
1581 SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(
1582 LayerTreeHostDelegatedTestResourceSentToParent);
1584 class LayerTreeHostDelegatedTestCommitWithoutTake
1585 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1586 public:
1587 void BeginTest() override {
1588 // Prevent drawing with resources that are sent to the grandparent.
1589 layer_tree_host()->SetViewportSize(gfx::Size());
1590 PostSetNeedsCommitToMainThread();
1593 void DidCommit() override {
1594 scoped_ptr<DelegatedFrameData> frame;
1595 ReturnedResourceArray resources;
1597 int next_source_frame_number = layer_tree_host()->source_frame_number();
1598 switch (next_source_frame_number) {
1599 case 1:
1600 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1601 AddTextureQuad(frame.get(), 999);
1602 AddTransferableResource(frame.get(), 999);
1603 AddTextureQuad(frame.get(), 555);
1604 AddTransferableResource(frame.get(), 555);
1605 AddTextureQuad(frame.get(), 444);
1606 AddTransferableResource(frame.get(), 444);
1607 SetFrameData(frame.Pass());
1608 break;
1609 case 2:
1610 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1611 EXPECT_EQ(0u, resources.size());
1612 EXPECT_FALSE(TestAndResetAvailable());
1614 // Stop using 999 and 444 in this frame and commit.
1615 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1616 AddTextureQuad(frame.get(), 555);
1617 AddTransferableResource(frame.get(), 555);
1618 SetFrameData(frame.Pass());
1619 // 999 and 444 will be returned for frame 1, but not 555 since it's in
1620 // the current frame.
1621 break;
1622 case 3:
1623 // Don't take resources here, but set a new frame that uses 999 again.
1624 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1625 AddTextureQuad(frame.get(), 999);
1626 AddTransferableResource(frame.get(), 999);
1627 AddTextureQuad(frame.get(), 555);
1628 AddTransferableResource(frame.get(), 555);
1629 SetFrameData(frame.Pass());
1630 break;
1631 case 4:
1632 // 555 from frame 1 and 2 isn't returned since it's still in use. 999
1633 // from frame 1 is returned though.
1634 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1636 unsigned expected[] = {444, 999};
1637 EXPECT_RESOURCES(expected, resources);
1638 EXPECT_TRUE(TestAndResetAvailable());
1641 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1642 SetFrameData(frame.Pass());
1643 // 555 will be returned 3 times for frames 1 2 and 3, and 999 will be
1644 // returned once for frame 3.
1645 break;
1646 case 5:
1647 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1649 unsigned expected[] = {555, 555, 555, 999};
1650 EXPECT_RESOURCES(expected, resources);
1651 EXPECT_TRUE(TestAndResetAvailable());
1654 EndTest();
1655 break;
1659 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
1660 if (host_impl->active_tree()->source_frame_number() < 1)
1661 return;
1663 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1664 FakeDelegatedRendererLayerImpl* delegated_impl =
1665 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1667 const ResourceProvider::ResourceIdMap& map =
1668 host_impl->resource_provider()->GetChildToParentMap(
1669 delegated_impl->ChildId());
1671 switch (host_impl->active_tree()->source_frame_number()) {
1672 case 1:
1673 EXPECT_EQ(3u, map.size());
1674 EXPECT_EQ(1u, map.count(999));
1675 EXPECT_EQ(1u, map.count(555));
1676 EXPECT_EQ(1u, map.count(444));
1678 EXPECT_EQ(3u, delegated_impl->Resources().size());
1679 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1680 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1681 EXPECT_EQ(1u, delegated_impl->Resources().count(444));
1682 break;
1683 case 2:
1684 EXPECT_EQ(1u, map.size());
1685 EXPECT_EQ(1u, map.count(555));
1687 EXPECT_EQ(1u, delegated_impl->Resources().size());
1688 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1689 break;
1690 case 3:
1691 EXPECT_EQ(2u, map.size());
1692 EXPECT_EQ(1u, map.count(999));
1693 EXPECT_EQ(1u, map.count(555));
1695 EXPECT_EQ(2u, delegated_impl->Resources().size());
1696 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1697 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1702 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestCommitWithoutTake);
1704 class DelegatedFrameIsActivatedDuringCommit
1705 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1706 protected:
1707 DelegatedFrameIsActivatedDuringCommit() : returned_resource_count_(0) {}
1709 void BeginTest() override {
1710 activate_count_ = 0;
1712 scoped_ptr<DelegatedFrameData> frame =
1713 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1714 AddTextureQuad(frame.get(), 999);
1715 AddTransferableResource(frame.get(), 999);
1716 SetFrameData(frame.Pass());
1718 PostSetNeedsCommitToMainThread();
1721 void WillActivateTreeOnThread(LayerTreeHostImpl* impl) override {
1722 ++activate_count_;
1725 void DidCommit() override {
1726 switch (layer_tree_host()->source_frame_number()) {
1727 case 1: {
1728 // The first frame has been activated. Set a new frame, and
1729 // expect the next commit to finish *after* it is activated.
1730 scoped_ptr<DelegatedFrameData> frame =
1731 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1732 AddTextureQuad(frame.get(), 555);
1733 AddTransferableResource(frame.get(), 555);
1734 SetFrameData(frame.Pass());
1735 break;
1737 case 2:
1738 // The second frame has been activated. Remove the layer from
1739 // the tree to cause another commit/activation. The commit should
1740 // finish *after* the layer is removed from the active tree.
1741 delegated_->RemoveFromParent();
1742 break;
1743 case 3:
1744 // Finish the test by releasing resources on the next frame.
1745 scoped_ptr<DelegatedFrameData> frame =
1746 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1747 SetFrameData(frame.Pass());
1748 break;
1752 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
1753 switch (host_impl->active_tree()->source_frame_number()) {
1754 case 0: {
1755 // The activate for the 1st frame should have happened before now.
1756 EXPECT_EQ(1, activate_count_);
1757 break;
1759 case 1: {
1760 // The activate for the 2nd frame should have happened before now.
1761 EXPECT_EQ(2, activate_count_);
1762 break;
1764 case 2: {
1765 // The activate to remove the layer should have happened before now.
1766 EXPECT_EQ(3, activate_count_);
1767 break;
1769 case 3: {
1770 NOTREACHED();
1771 break;
1776 void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, bool result) override {
1777 ReturnUnusedResourcesFromParent(host_impl);
1780 void UnusedResourcesAreAvailable() override {
1781 LayerTreeHostDelegatedTestCaseSingleDelegatedLayer::
1782 UnusedResourcesAreAvailable();
1783 ReturnedResourceArray resources;
1784 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1785 EXPECT_TRUE(TestAndResetAvailable());
1786 returned_resource_count_ += resources.size();
1787 if (returned_resource_count_ == 2)
1788 EndTest();
1791 int activate_count_;
1792 size_t returned_resource_count_;
1795 SINGLE_AND_MULTI_THREAD_TEST_F(
1796 DelegatedFrameIsActivatedDuringCommit);
1798 class LayerTreeHostDelegatedTestTwoImplLayers
1799 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1800 public:
1801 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
1803 void DidCommitAndDrawFrame() override {
1804 scoped_ptr<DelegatedFrameData> frame;
1805 ReturnedResourceArray resources;
1807 int next_source_frame_number = layer_tree_host()->source_frame_number();
1808 switch (next_source_frame_number) {
1809 case 1:
1810 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1811 AddTextureQuad(frame.get(), 999);
1812 AddTransferableResource(frame.get(), 999);
1813 AddTextureQuad(frame.get(), 555);
1814 AddTransferableResource(frame.get(), 555);
1815 SetFrameData(frame.Pass());
1816 break;
1817 case 2:
1818 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1819 EXPECT_EQ(0u, resources.size());
1820 EXPECT_FALSE(TestAndResetAvailable());
1822 // Remove the delegated layer and replace it with a new one. Use the
1823 // same frame and resources for it.
1824 delegated_->RemoveFromParent();
1825 delegated_ = CreateDelegatedLayer(frame_provider_.get());
1826 break;
1827 case 3:
1828 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1829 EXPECT_EQ(0u, resources.size());
1830 EXPECT_FALSE(TestAndResetAvailable());
1832 // Use a frame with no resources in it.
1833 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1834 SetFrameData(frame.Pass());
1835 break;
1836 case 4:
1837 // We gave one frame to the frame provider, so we should get one
1838 // ref back for each resource.
1839 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1841 unsigned expected[] = {555, 999};
1842 EXPECT_RESOURCES(expected, resources);
1843 EXPECT_TRUE(TestAndResetAvailable());
1845 EndTest();
1846 break;
1850 void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, bool result) override {
1851 ReturnUnusedResourcesFromParent(host_impl);
1855 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestTwoImplLayers);
1857 class LayerTreeHostDelegatedTestTwoImplLayersTwoFrames
1858 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1859 public:
1860 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
1862 void DidCommitAndDrawFrame() override {
1863 scoped_ptr<DelegatedFrameData> frame;
1864 ReturnedResourceArray resources;
1866 int next_source_frame_number = layer_tree_host()->source_frame_number();
1867 switch (next_source_frame_number) {
1868 case 1:
1869 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1870 AddTextureQuad(frame.get(), 999);
1871 AddTransferableResource(frame.get(), 999);
1872 AddTextureQuad(frame.get(), 555);
1873 AddTransferableResource(frame.get(), 555);
1874 SetFrameData(frame.Pass());
1875 break;
1876 case 2:
1877 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1878 EXPECT_EQ(0u, resources.size());
1879 EXPECT_FALSE(TestAndResetAvailable());
1881 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1882 AddTextureQuad(frame.get(), 999);
1883 AddTransferableResource(frame.get(), 999);
1884 AddTextureQuad(frame.get(), 555);
1885 AddTransferableResource(frame.get(), 555);
1887 // Remove the delegated layer and replace it with a new one. Make a new
1888 // frame but with the same resources for it.
1889 delegated_->RemoveFromParent();
1890 delegated_ = NULL;
1892 frame_provider_->SetFrameData(frame.Pass());
1893 delegated_ = CreateDelegatedLayer(frame_provider_.get());
1894 break;
1895 case 3:
1896 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1897 EXPECT_EQ(0u, resources.size());
1898 EXPECT_FALSE(TestAndResetAvailable());
1900 // Use a frame with no resources in it.
1901 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1902 SetFrameData(frame.Pass());
1903 break;
1904 case 4:
1905 // We gave two frames to the frame provider, so we should get two
1906 // refs back for each resource.
1907 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1909 unsigned expected[] = {555, 555, 999, 999};
1910 EXPECT_RESOURCES(expected, resources);
1911 EXPECT_TRUE(TestAndResetAvailable());
1913 EndTest();
1914 break;
1918 void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, bool result) override {
1919 ReturnUnusedResourcesFromParent(host_impl);
1923 SINGLE_AND_MULTI_THREAD_TEST_F(
1924 LayerTreeHostDelegatedTestTwoImplLayersTwoFrames);
1926 class LayerTreeHostDelegatedTestTwoLayers
1927 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1928 public:
1929 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
1931 void DidCommitAndDrawFrame() override {
1932 scoped_ptr<DelegatedFrameData> frame;
1933 ReturnedResourceArray resources;
1935 int next_source_frame_number = layer_tree_host()->source_frame_number();
1936 switch (next_source_frame_number) {
1937 case 1:
1938 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1939 AddTextureQuad(frame.get(), 999);
1940 AddTransferableResource(frame.get(), 999);
1941 AddTextureQuad(frame.get(), 555);
1942 AddTransferableResource(frame.get(), 555);
1944 // Create a DelegatedRendererLayer using the frame.
1945 SetFrameData(frame.Pass());
1946 break;
1947 case 2:
1948 // Create a second DelegatedRendererLayer using the same frame provider.
1949 delegated_thief_ = CreateDelegatedLayer(frame_provider_.get());
1950 root_->AddChild(delegated_thief_);
1952 // And drop our ref on the frame provider so only the layers keep it
1953 // alive.
1954 frame_provider_ = NULL;
1955 break;
1956 case 3:
1957 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1958 EXPECT_EQ(0u, resources.size());
1959 EXPECT_FALSE(TestAndResetAvailable());
1961 // Remove one delegated layer from the tree. No resources should be
1962 // returned yet.
1963 delegated_->RemoveFromParent();
1964 break;
1965 case 4:
1966 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1967 EXPECT_EQ(0u, resources.size());
1968 EXPECT_FALSE(TestAndResetAvailable());
1970 // Put the first layer back, and remove the other layer and destroy it.
1971 // No resources should be returned yet.
1972 root_->AddChild(delegated_);
1973 delegated_thief_->RemoveFromParent();
1974 delegated_thief_ = NULL;
1975 break;
1976 case 5:
1977 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1978 EXPECT_EQ(0u, resources.size());
1979 EXPECT_FALSE(TestAndResetAvailable());
1981 // Remove the first layer from the tree again. The resources are still
1982 // held by the main thread layer.
1983 delegated_->RemoveFromParent();
1984 break;
1985 case 6:
1986 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1987 EXPECT_EQ(0u, resources.size());
1988 EXPECT_FALSE(TestAndResetAvailable());
1990 // Destroy the layer and the resources should be returned immediately.
1991 delegated_ = NULL;
1993 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1995 unsigned expected[] = {555, 999};
1996 EXPECT_RESOURCES(expected, resources);
1997 EXPECT_TRUE(TestAndResetAvailable());
1999 EndTest();
2000 break;
2004 void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, bool result) override {
2005 ReturnUnusedResourcesFromParent(host_impl);
2008 scoped_refptr<DelegatedRendererLayer> delegated_thief_;
2011 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestTwoLayers);
2013 class LayerTreeHostDelegatedTestRemoveAndAddToTree
2014 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
2015 public:
2016 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
2018 void DidCommitAndDrawFrame() override {
2019 scoped_ptr<DelegatedFrameData> frame;
2020 ReturnedResourceArray resources;
2022 int next_source_frame_number = layer_tree_host()->source_frame_number();
2023 switch (next_source_frame_number) {
2024 case 1:
2025 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2026 AddTextureQuad(frame.get(), 999);
2027 AddTransferableResource(frame.get(), 999);
2028 AddTextureQuad(frame.get(), 555);
2029 AddTransferableResource(frame.get(), 555);
2031 // Create a DelegatedRendererLayer using the frame.
2032 SetFrameData(frame.Pass());
2033 break;
2034 case 2:
2035 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2036 EXPECT_EQ(0u, resources.size());
2037 EXPECT_FALSE(TestAndResetAvailable());
2039 // Remove the layer from the tree. The resources should not be returned
2040 // since they are still on the main thread layer.
2041 delegated_->RemoveFromParent();
2042 break;
2043 case 3:
2044 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2045 EXPECT_EQ(0u, resources.size());
2046 EXPECT_FALSE(TestAndResetAvailable());
2048 // Add the layer back to the tree.
2049 layer_tree_host()->root_layer()->AddChild(delegated_);
2050 break;
2051 case 4:
2052 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2053 EXPECT_EQ(0u, resources.size());
2054 EXPECT_FALSE(TestAndResetAvailable());
2056 // Set a new frame. Resources should be returned.
2057 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2058 AddTextureQuad(frame.get(), 888);
2059 AddTransferableResource(frame.get(), 888);
2060 AddTextureQuad(frame.get(), 777);
2061 AddTransferableResource(frame.get(), 777);
2062 SetFrameData(frame.Pass());
2063 break;
2064 case 5:
2065 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2067 unsigned expected[] = {555, 999};
2068 EXPECT_RESOURCES(expected, resources);
2069 EXPECT_TRUE(TestAndResetAvailable());
2072 // Destroy the layer.
2073 delegated_->RemoveFromParent();
2074 delegated_ = NULL;
2075 break;
2076 case 6:
2077 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2078 EXPECT_EQ(0u, resources.size());
2079 EXPECT_FALSE(TestAndResetAvailable());
2081 // Destroy the frame provider. Resources should be returned.
2082 frame_provider_ = NULL;
2084 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2086 unsigned expected[] = {777, 888};
2087 EXPECT_RESOURCES(expected, resources);
2088 EXPECT_TRUE(TestAndResetAvailable());
2090 EndTest();
2091 break;
2095 void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, bool result) override {
2096 ReturnUnusedResourcesFromParent(host_impl);
2099 scoped_refptr<DelegatedRendererLayer> delegated_thief_;
2102 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestRemoveAndAddToTree);
2104 class LayerTreeHostDelegatedTestRemoveAndChangeResources
2105 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
2106 public:
2107 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
2109 void DidCommitAndDrawFrame() override {
2110 scoped_ptr<DelegatedFrameData> frame;
2111 ReturnedResourceArray resources;
2113 int next_source_frame_number = layer_tree_host()->source_frame_number();
2114 switch (next_source_frame_number) {
2115 case 1:
2116 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2117 AddTextureQuad(frame.get(), 999);
2118 AddTransferableResource(frame.get(), 999);
2119 AddTextureQuad(frame.get(), 555);
2120 AddTransferableResource(frame.get(), 555);
2122 // Create a DelegatedRendererLayer using the frame.
2123 SetFrameData(frame.Pass());
2124 break;
2125 case 2:
2126 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2127 EXPECT_EQ(0u, resources.size());
2128 EXPECT_FALSE(TestAndResetAvailable());
2130 // Remove the layer from the tree. The resources should not be returned
2131 // since they are still on the main thread layer.
2132 delegated_->RemoveFromParent();
2133 break;
2134 case 3:
2135 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2136 EXPECT_EQ(0u, resources.size());
2137 EXPECT_FALSE(TestAndResetAvailable());
2139 // Set a new frame. Resources should be returned immediately.
2140 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2141 AddTextureQuad(frame.get(), 888);
2142 AddTransferableResource(frame.get(), 888);
2143 AddTextureQuad(frame.get(), 777);
2144 AddTransferableResource(frame.get(), 777);
2145 SetFrameData(frame.Pass());
2147 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2149 unsigned expected[] = {555, 999};
2150 EXPECT_RESOURCES(expected, resources);
2151 EXPECT_TRUE(TestAndResetAvailable());
2152 resources.clear();
2155 // Destroy the frame provider.
2156 frame_provider_ = NULL;
2158 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2159 EXPECT_EQ(0u, resources.size());
2160 EXPECT_FALSE(TestAndResetAvailable());
2162 // Destroy the layer. Resources should be returned.
2163 delegated_ = NULL;
2165 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2167 unsigned expected[] = {777, 888};
2168 EXPECT_RESOURCES(expected, resources);
2169 EXPECT_TRUE(TestAndResetAvailable());
2171 EndTest();
2172 break;
2176 void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, bool result) override {
2177 ReturnUnusedResourcesFromParent(host_impl);
2180 scoped_refptr<DelegatedRendererLayer> delegated_thief_;
2183 SINGLE_AND_MULTI_THREAD_TEST_F(
2184 LayerTreeHostDelegatedTestRemoveAndChangeResources);
2186 class LayerTreeHostDelegatedTestActiveFrameIsValid
2187 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
2188 public:
2189 LayerTreeHostDelegatedTestActiveFrameIsValid()
2190 : drew_with_pending_tree_(false) {}
2192 void DidCommitAndDrawFrame() override {
2193 scoped_ptr<DelegatedFrameData> frame;
2194 switch (layer_tree_host()->source_frame_number()) {
2195 case 1:
2196 // This frame goes to the active tree.
2197 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2198 AddTextureQuad(frame.get(), 999);
2199 AddTransferableResource(frame.get(), 999);
2200 SetFrameData(frame.Pass());
2201 break;
2202 case 2:
2203 // This frame stops in the pending tree while we redraw the active tree.
2204 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2205 AddTextureQuad(frame.get(), 555);
2206 AddTransferableResource(frame.get(), 555);
2207 SetFrameData(frame.Pass());
2208 break;
2212 void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override {
2213 if (host_impl->active_tree()->source_frame_number() < 1)
2214 return;
2216 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
2217 FakeDelegatedRendererLayerImpl* delegated_impl =
2218 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
2219 const ResourceProvider::ResourceIdMap& map =
2220 host_impl->resource_provider()->GetChildToParentMap(
2221 delegated_impl->ChildId());
2223 switch (host_impl->active_tree()->source_frame_number()) {
2224 case 1:
2225 if (!host_impl->pending_tree()) {
2226 // Frame 2 is blocked from activating until another draw happens with
2227 // Frame 1. This ensures we draw a different active frame from
2228 // what's in the pending tree.
2229 host_impl->BlockNotifyReadyToActivateForTesting(true);
2230 host_impl->SetNeedsRedrawRect(gfx::Rect(1, 1));
2231 break;
2234 // The resources in the active tree should be valid.
2235 EXPECT_EQ(1u, map.count(999));
2237 host_impl->BlockNotifyReadyToActivateForTesting(false);
2238 drew_with_pending_tree_ = true;
2239 break;
2240 case 2:
2241 EXPECT_TRUE(drew_with_pending_tree_);
2243 // The resources in the active tree should be valid.
2244 EXPECT_EQ(1u, map.count(555));
2245 EndTest();
2246 break;
2250 void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, bool result) override {
2251 // Return everything so that we can reliably delete resources that lose
2252 // their references. This would happen if the tab was backgrounded or
2253 // the parent decided to drop all resources for some reason.
2254 ReturnAllResourcesFromParent(host_impl);
2257 bool drew_with_pending_tree_;
2260 MULTI_THREAD_IMPL_TEST_F(LayerTreeHostDelegatedTestActiveFrameIsValid);
2262 } // namespace
2263 } // namespace cc