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"
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"
35 bool ReturnedResourceLower(const ReturnedResource
& a
,
36 const ReturnedResource
& b
) {
40 // Tests if the list of resources matches an expectation, modulo the order.
41 bool ResourcesMatch(ReturnedResourceArray actual
,
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
;
52 EXPECT_LT(actual_index
, actual
.size());
53 if (actual_index
>= actual
.size())
55 EXPECT_EQ(actual
[actual_index
].id
, expected
[expected_index
]);
56 if (actual
[actual_index
].id
!= expected
[expected_index
])
58 EXPECT_GT(actual
[actual_index
].count
, 0);
59 if (actual
[actual_index
].count
<= 0) {
62 --actual
[actual_index
].count
;
63 if (actual
[actual_index
].count
== 0)
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
{
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),
87 frame
->render_pass_list
.push_back(root_pass
.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),
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
,
133 frame
->render_pass_list
.push_back(root_pass
.Pass());
137 void AddTransferableResource(DelegatedFrameData
* frame
,
138 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
, ResourceId resource_id
) {
151 RenderPass
* render_pass
= frame
->render_pass_list
[0];
152 SharedQuadState
* sqs
= render_pass
->CreateAndAppendSharedQuadState();
153 TextureDrawQuad
* quad
=
154 render_pass
->CreateAndAppendDrawQuad
<TextureDrawQuad
>();
155 float vertex_opacity
[4] = { 1.f
, 1.f
, 1.f
, 1.f
};
157 gfx::Rect(0, 0, 10, 10),
158 gfx::Rect(0, 0, 10, 10),
159 gfx::Rect(0, 0, 10, 10),
162 gfx::PointF(0.f
, 0.f
),
163 gfx::PointF(1.f
, 1.f
),
170 void AddRenderPass(DelegatedFrameData
* frame
,
172 const gfx::Rect
& output_rect
,
173 const gfx::Rect
& damage_rect
,
174 const FilterOperations
& filters
,
175 const FilterOperations
& background_filters
) {
176 for (size_t i
= 0; i
< frame
->render_pass_list
.size(); ++i
)
177 DCHECK(id
!= frame
->render_pass_list
[i
]->id
);
179 scoped_ptr
<RenderPass
> pass(RenderPass::Create());
184 frame
->render_pass_list
.push_back(pass
.Pass());
186 RenderPass
* render_pass
= frame
->render_pass_list
[0];
187 SharedQuadState
* sqs
= render_pass
->CreateAndAppendSharedQuadState();
188 RenderPassDrawQuad
* quad
=
189 render_pass
->CreateAndAppendDrawQuad
<RenderPassDrawQuad
>();
203 static ResourceId
AppendResourceId(
204 std::vector
<ResourceId
>* resources_in_last_sent_frame
,
205 ResourceId resource_id
) {
206 resources_in_last_sent_frame
->push_back(resource_id
);
210 void ReturnUnusedResourcesFromParent(LayerTreeHostImpl
* host_impl
) {
211 DelegatedFrameData
* delegated_frame_data
=
212 output_surface()->last_sent_frame().delegated_frame_data
.get();
213 if (!delegated_frame_data
)
216 std::vector
<ResourceId
> resources_in_last_sent_frame
;
217 for (size_t i
= 0; i
< delegated_frame_data
->resource_list
.size(); ++i
) {
218 resources_in_last_sent_frame
.push_back(
219 delegated_frame_data
->resource_list
[i
].id
);
222 std::vector
<ResourceId
> resources_to_return
;
224 const TransferableResourceArray
& resources_held_by_parent
=
225 output_surface()->resources_held_by_parent();
226 for (size_t i
= 0; i
< resources_held_by_parent
.size(); ++i
) {
227 ResourceId resource_in_parent
= resources_held_by_parent
[i
].id
;
228 bool resource_in_parent_is_not_part_of_frame
=
229 std::find(resources_in_last_sent_frame
.begin(),
230 resources_in_last_sent_frame
.end(),
231 resource_in_parent
) == resources_in_last_sent_frame
.end();
232 if (resource_in_parent_is_not_part_of_frame
)
233 resources_to_return
.push_back(resource_in_parent
);
236 if (resources_to_return
.empty())
239 CompositorFrameAck ack
;
240 for (size_t i
= 0; i
< resources_to_return
.size(); ++i
)
241 output_surface()->ReturnResource(resources_to_return
[i
], &ack
);
242 host_impl
->ReclaimResources(&ack
);
245 void ReturnAllResourcesFromParent(LayerTreeHostImpl
* host_impl
) {
246 DelegatedFrameData
* delegated_frame_data
=
247 output_surface()->last_sent_frame().delegated_frame_data
.get();
248 if (!delegated_frame_data
)
251 const TransferableResourceArray
& resources_held_by_parent
=
252 output_surface()->resources_held_by_parent();
254 if (resources_held_by_parent
.empty())
257 CompositorFrameAck ack
;
258 for (size_t i
= 0; i
< resources_held_by_parent
.size(); ++i
)
259 output_surface()->ReturnResource(resources_held_by_parent
[i
].id
, &ack
);
260 host_impl
->ReclaimResources(&ack
);
264 class LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
265 : public LayerTreeHostDelegatedTest
,
266 public DelegatedFrameResourceCollectionClient
{
268 LayerTreeHostDelegatedTestCaseSingleDelegatedLayer()
269 : resource_collection_(new DelegatedFrameResourceCollection
),
271 resource_collection_
->SetClient(this);
274 void SetupTree() override
{
275 root_
= Layer::Create(layer_settings());
276 root_
->SetBounds(gfx::Size(15, 15));
278 layer_tree_host()->SetRootLayer(root_
);
279 LayerTreeHostDelegatedTest::SetupTree();
282 void BeginTest() override
{
283 resource_collection_
->SetClient(this);
284 PostSetNeedsCommitToMainThread();
287 void SetFrameData(scoped_ptr
<DelegatedFrameData
> frame_data
) {
288 RenderPass
* root_pass
= frame_data
->render_pass_list
.back();
289 gfx::Size frame_size
= root_pass
->output_rect
.size();
291 if (frame_provider_
.get() && frame_size
== frame_provider_
->frame_size()) {
292 frame_provider_
->SetFrameData(frame_data
.Pass());
296 if (delegated_
.get()) {
297 delegated_
->RemoveFromParent();
299 frame_provider_
= NULL
;
302 frame_provider_
= new DelegatedFrameProvider(resource_collection_
.get(),
305 delegated_
= CreateDelegatedLayer(frame_provider_
.get());
308 scoped_refptr
<DelegatedRendererLayer
> CreateDelegatedLayer(
309 DelegatedFrameProvider
* frame_provider
) {
310 scoped_refptr
<DelegatedRendererLayer
> delegated
=
311 FakeDelegatedRendererLayer::Create(layer_settings(), frame_provider
);
312 delegated
->SetBounds(gfx::Size(10, 10));
313 delegated
->SetIsDrawable(true);
315 root_
->AddChild(delegated
);
319 void AfterTest() override
{ resource_collection_
->SetClient(NULL
); }
321 // DelegatedFrameProviderClient implementation.
322 void UnusedResourcesAreAvailable() override
{ available_
= true; }
324 bool TestAndResetAvailable() {
325 bool available
= available_
;
331 scoped_refptr
<DelegatedFrameResourceCollection
> resource_collection_
;
332 scoped_refptr
<DelegatedFrameProvider
> frame_provider_
;
333 scoped_refptr
<Layer
> root_
;
334 scoped_refptr
<DelegatedRendererLayer
> delegated_
;
338 class LayerTreeHostDelegatedTestCreateChildId
339 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
341 LayerTreeHostDelegatedTestCreateChildId()
342 : LayerTreeHostDelegatedTestCaseSingleDelegatedLayer(),
344 did_reset_child_id_(false) {}
346 void DidCommit() override
{
349 SetFrameData(CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1)));
352 void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) override
{
353 if (host_impl
->active_tree()->source_frame_number() < 1)
356 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
357 FakeDelegatedRendererLayerImpl
* delegated_impl
=
358 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
360 TestContextProvider
* context_provider
= static_cast<TestContextProvider
*>(
361 host_impl
->output_surface()->context_provider());
364 switch (num_activates_
) {
366 EXPECT_TRUE(delegated_impl
->ChildId());
367 EXPECT_FALSE(did_reset_child_id_
);
369 context_provider
->ContextGL()->LoseContextCHROMIUM(
370 GL_GUILTY_CONTEXT_RESET_ARB
,
371 GL_INNOCENT_CONTEXT_RESET_ARB
);
372 context_provider
->ContextGL()->Flush();
375 EXPECT_TRUE(delegated_impl
->ChildId());
376 EXPECT_TRUE(did_reset_child_id_
);
382 void InitializedRendererOnThread(LayerTreeHostImpl
* host_impl
,
383 bool success
) override
{
384 EXPECT_TRUE(success
);
386 if (num_activates_
< 2)
389 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
390 FakeDelegatedRendererLayerImpl
* delegated_impl
=
391 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
393 EXPECT_EQ(2, num_activates_
);
394 EXPECT_FALSE(delegated_impl
->ChildId());
395 did_reset_child_id_
= true;
400 bool did_reset_child_id_
;
403 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestCreateChildId
);
405 class LayerTreeHostDelegatedTestDontUseLostChildIdAfterCommit
406 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
408 void BeginTest() override
{
409 SetFrameData(CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1)));
410 LayerTreeHostDelegatedTestCaseSingleDelegatedLayer::BeginTest();
413 void CommitCompleteOnThread(LayerTreeHostImpl
* host_impl
) override
{
414 // Act like the context was lost while the layer is in the pending tree.
415 LayerImpl
* root_impl
= host_impl
->sync_tree()->root_layer();
416 FakeDelegatedRendererLayerImpl
* delegated_impl
=
417 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
418 delegated_impl
->ReleaseResources();
421 void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) override
{
422 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
423 FakeDelegatedRendererLayerImpl
* delegated_impl
=
424 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
426 // Should not try to activate a frame without a child id. If we did try to
427 // activate we would crash.
428 EXPECT_FALSE(delegated_impl
->ChildId());
433 SINGLE_AND_MULTI_THREAD_TEST_F(
434 LayerTreeHostDelegatedTestDontUseLostChildIdAfterCommit
);
436 // Test that we can gracefully handle invalid frames after the context was lost.
437 // For example, we might be trying to use the previous frame in that case and
438 // have to make sure we don't crash because our resource accounting goes wrong.
439 class LayerTreeHostDelegatedTestInvalidFrameAfterContextLost
440 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
442 LayerTreeHostDelegatedTestInvalidFrameAfterContextLost()
443 : num_activates_(0), num_output_surfaces_initialized_(0) {}
445 void DidCommit() override
{
448 scoped_ptr
<DelegatedFrameData
> frame1
=
449 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
450 AddTextureQuad(frame1
.get(), 999);
451 AddTransferableResource(frame1
.get(), 999);
452 SetFrameData(frame1
.Pass());
455 void DidInitializeOutputSurface() override
{
456 if (!num_output_surfaces_initialized_
++)
459 scoped_refptr
<DelegatedRendererLayer
> old_delegated
= delegated_
;
461 CreateInvalidFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1)));
462 // Make sure we end up using the same layer, or we won't test the right
463 // thing, which is to make sure we can handle an invalid frame when using
464 // a stale layer from before the context was lost.
465 DCHECK(delegated_
.get() == old_delegated
.get());
468 void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) override
{
469 if (host_impl
->active_tree()->source_frame_number() < 1)
472 TestContextProvider
* context_provider
= static_cast<TestContextProvider
*>(
473 host_impl
->output_surface()->context_provider());
476 switch (num_activates_
) {
478 context_provider
->ContextGL()->LoseContextCHROMIUM(
479 GL_GUILTY_CONTEXT_RESET_ARB
,
480 GL_INNOCENT_CONTEXT_RESET_ARB
);
488 void InitializedRendererOnThread(LayerTreeHostImpl
* host_impl
,
489 bool success
) override
{
490 EXPECT_TRUE(success
);
492 if (num_activates_
< 2)
495 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
496 FakeDelegatedRendererLayerImpl
* delegated_impl
=
497 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
499 EXPECT_EQ(2, num_activates_
);
500 // Resources should have gotten cleared after the context was lost.
501 EXPECT_EQ(0U, delegated_impl
->Resources().size());
504 void AfterTest() override
{
505 LayerTreeHostDelegatedTestCaseSingleDelegatedLayer::AfterTest();
506 EXPECT_EQ(2, num_output_surfaces_initialized_
);
511 int num_output_surfaces_initialized_
;
514 SINGLE_AND_MULTI_THREAD_TEST_F(
515 LayerTreeHostDelegatedTestInvalidFrameAfterContextLost
);
517 class LayerTreeHostDelegatedTestLayerUsesFrameDamage
518 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
520 void DidCommit() override
{
521 int next_source_frame_number
= layer_tree_host()->source_frame_number();
522 switch (next_source_frame_number
) {
524 // The first time the layer gets a frame the whole layer should be
527 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1)));
530 // A different frame size will damage the whole layer.
532 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(0, 0, 0, 0)));
535 // Should create a total amount of gfx::Rect(2, 2, 8, 6) damage:
536 // (2, 2, 10, 6) clamped to the root output rect.
538 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(2, 2, 5, 5)));
540 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(7, 2, 5, 6)));
543 // Should create zero damage.
544 layer_tree_host()->SetNeedsCommit();
547 // Should damage the full viewport.
548 delegated_
->SetBounds(gfx::Size(2, 2));
551 // Should create zero damage.
552 layer_tree_host()->SetNeedsCommit();
555 // Should damage the full layer, tho the frame size is not changing.
556 delegated_
->SetBounds(gfx::Size(6, 6));
558 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(1, 1, 2, 2)));
561 // Should create zero damage.
562 layer_tree_host()->SetNeedsCommit();
565 // Should create zero damage.
566 layer_tree_host()->SetNeedsCommit();
569 // Changing the frame size damages the full layer.
571 CreateFrameData(gfx::Rect(0, 0, 5, 5), gfx::Rect(4, 4, 1, 1)));
574 // An invalid frame isn't used, so it should not cause damage.
575 SetFrameData(CreateInvalidFrameData(gfx::Rect(0, 0, 5, 5),
576 gfx::Rect(4, 4, 1, 1)));
579 // Should create gfx::Rect(1, 1, 2, 2) of damage.
581 CreateFrameData(gfx::Rect(0, 0, 5, 5), gfx::Rect(1, 1, 2, 2)));
584 // Should create zero damage.
585 layer_tree_host()->SetNeedsCommit();
588 // Moving the layer out of the tree and back in will damage the whole
590 delegated_
->RemoveFromParent();
591 layer_tree_host()->root_layer()->AddChild(delegated_
);
594 // Make a larger frame with lots of damage. Then a frame smaller than
595 // the first frame's damage. The entire layer should be damaged, but
598 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(0, 0, 10, 10)));
600 CreateFrameData(gfx::Rect(0, 0, 5, 5), gfx::Rect(1, 1, 2, 2)));
603 // Make a frame with lots of damage. Then replace it with a frame with
604 // no damage. The entire layer should be damaged, but nothing more.
606 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(0, 0, 10, 10)));
608 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(0, 0, 0, 0)));
611 // Make another layer that uses the same frame provider. The new layer
612 // should be damaged.
613 delegated_copy_
= CreateDelegatedLayer(frame_provider_
.get());
614 delegated_copy_
->SetPosition(gfx::Point(5, 0));
616 // Also set a new frame.
618 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(4, 0, 1, 1)));
621 // Set another new frame, both layers should be damaged in the same
624 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(3, 3, 1, 1)));
629 DrawResult
PrepareToDrawOnThread(LayerTreeHostImpl
* host_impl
,
630 LayerTreeHostImpl::FrameData
* frame
,
631 DrawResult draw_result
) override
{
632 EXPECT_EQ(DRAW_SUCCESS
, draw_result
);
634 gfx::Rect damage_rect
;
635 if (!frame
->has_no_damage
) {
636 damage_rect
= frame
->render_passes
.back()->damage_rect
;
638 // If there is no damage, then we have no render passes to send.
639 EXPECT_TRUE(frame
->render_passes
.empty());
642 switch (host_impl
->active_tree()->source_frame_number()) {
644 // First frame is damaged because of viewport resize.
645 EXPECT_EQ(gfx::Rect(15, 15).ToString(), damage_rect
.ToString());
648 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect
.ToString());
651 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect
.ToString());
654 EXPECT_EQ(gfx::Rect(2, 2, 8, 6).ToString(), damage_rect
.ToString());
657 EXPECT_EQ(gfx::Rect().ToString(), damage_rect
.ToString());
660 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect
.ToString());
663 EXPECT_EQ(gfx::Rect().ToString(), damage_rect
.ToString());
666 EXPECT_EQ(gfx::Rect(6, 6).ToString(), damage_rect
.ToString());
669 EXPECT_EQ(gfx::Rect().ToString(), damage_rect
.ToString());
672 EXPECT_EQ(gfx::Rect().ToString(), damage_rect
.ToString());
675 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect
.ToString());
678 EXPECT_EQ(gfx::Rect().ToString(), damage_rect
.ToString());
681 EXPECT_EQ(gfx::Rect(1, 1, 2, 2).ToString(), damage_rect
.ToString());
684 EXPECT_EQ(gfx::Rect().ToString(), damage_rect
.ToString());
687 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect
.ToString());
690 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect
.ToString());
693 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect
.ToString());
696 EXPECT_EQ(gfx::UnionRects(gfx::Rect(5, 0, 10, 10),
697 gfx::Rect(4, 0, 1, 1)).ToString(),
698 damage_rect
.ToString());
701 EXPECT_EQ(gfx::Rect(3, 3, 6, 1).ToString(), damage_rect
.ToString());
710 scoped_refptr
<DelegatedRendererLayer
> delegated_copy_
;
713 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestLayerUsesFrameDamage
);
715 class LayerTreeHostDelegatedTestMergeResources
716 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
718 void BeginTest() override
{
719 // Push two frames to the delegated renderer layer with no commit between.
721 // The first frame has resource 999.
722 scoped_ptr
<DelegatedFrameData
> frame1
=
723 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
724 AddTextureQuad(frame1
.get(), 999);
725 AddTransferableResource(frame1
.get(), 999);
726 SetFrameData(frame1
.Pass());
728 // The second frame uses resource 999 still, but also adds 555.
729 scoped_ptr
<DelegatedFrameData
> frame2
=
730 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
731 AddTextureQuad(frame2
.get(), 999);
732 AddTransferableResource(frame2
.get(), 999);
733 AddTextureQuad(frame2
.get(), 555);
734 AddTransferableResource(frame2
.get(), 555);
735 SetFrameData(frame2
.Pass());
737 // The resource 999 from frame1 is returned since it is still on the main
739 ReturnedResourceArray returned_resources
;
740 resource_collection_
->TakeUnusedResourcesForChildCompositor(
741 &returned_resources
);
743 unsigned expected
[] = {999};
744 EXPECT_RESOURCES(expected
, returned_resources
);
745 EXPECT_TRUE(TestAndResetAvailable());
748 PostSetNeedsCommitToMainThread();
751 void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) override
{
752 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
753 FakeDelegatedRendererLayerImpl
* delegated_impl
=
754 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
756 const ResourceProvider::ResourceIdMap
& map
=
757 host_impl
->resource_provider()->GetChildToParentMap(
758 delegated_impl
->ChildId());
760 // Both frames' resources should be in the parent's resource provider.
761 EXPECT_EQ(2u, map
.size());
762 EXPECT_EQ(1u, map
.count(999));
763 EXPECT_EQ(1u, map
.count(555));
765 EXPECT_EQ(2u, delegated_impl
->Resources().size());
766 EXPECT_EQ(1u, delegated_impl
->Resources().count(999));
767 EXPECT_EQ(1u, delegated_impl
->Resources().count(555));
773 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestMergeResources
);
775 class LayerTreeHostDelegatedTestRemapResourcesInQuads
776 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
778 void BeginTest() override
{
779 // Generate a frame with two resources in it.
780 scoped_ptr
<DelegatedFrameData
> frame
=
781 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
782 AddTextureQuad(frame
.get(), 999);
783 AddTransferableResource(frame
.get(), 999);
784 AddTextureQuad(frame
.get(), 555);
785 AddTransferableResource(frame
.get(), 555);
786 SetFrameData(frame
.Pass());
788 PostSetNeedsCommitToMainThread();
791 void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) override
{
792 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
793 FakeDelegatedRendererLayerImpl
* delegated_impl
=
794 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
796 const ResourceProvider::ResourceIdMap
& map
=
797 host_impl
->resource_provider()->GetChildToParentMap(
798 delegated_impl
->ChildId());
800 // The frame's resource should be in the parent's resource provider.
801 EXPECT_EQ(2u, map
.size());
802 EXPECT_EQ(1u, map
.count(999));
803 EXPECT_EQ(1u, map
.count(555));
805 ResourceId parent_resource_id1
= map
.find(999)->second
;
806 EXPECT_NE(parent_resource_id1
, 999u);
807 ResourceId parent_resource_id2
= map
.find(555)->second
;
808 EXPECT_NE(parent_resource_id2
, 555u);
810 // The resources in the quads should be remapped to the parent's namespace.
811 const TextureDrawQuad
* quad1
= TextureDrawQuad::MaterialCast(
812 delegated_impl
->RenderPassesInDrawOrder()[0]->quad_list
.ElementAt(0));
813 EXPECT_EQ(parent_resource_id1
, quad1
->resource_id());
814 const TextureDrawQuad
* quad2
= TextureDrawQuad::MaterialCast(
815 delegated_impl
->RenderPassesInDrawOrder()[0]->quad_list
.ElementAt(1));
816 EXPECT_EQ(parent_resource_id2
, quad2
->resource_id());
822 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestRemapResourcesInQuads
);
824 class LayerTreeHostDelegatedTestReturnUnusedResources
825 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
827 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
829 void DidCommitAndDrawFrame() override
{
830 scoped_ptr
<DelegatedFrameData
> frame
;
831 ReturnedResourceArray resources
;
833 int next_source_frame_number
= layer_tree_host()->source_frame_number();
834 switch (next_source_frame_number
) {
836 // Generate a frame with two resources in it.
837 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
838 AddTextureQuad(frame
.get(), 999);
839 AddTransferableResource(frame
.get(), 999);
840 AddTextureQuad(frame
.get(), 555);
841 AddTransferableResource(frame
.get(), 555);
842 SetFrameData(frame
.Pass());
845 // All of the resources are in use.
846 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
847 EXPECT_EQ(0u, resources
.size());
848 EXPECT_FALSE(TestAndResetAvailable());
850 // Keep using 999 but stop using 555.
851 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
852 AddTextureQuad(frame
.get(), 999);
853 AddTransferableResource(frame
.get(), 999);
854 AddTextureQuad(frame
.get(), 444);
855 AddTransferableResource(frame
.get(), 444);
856 SetFrameData(frame
.Pass());
859 // 555 is no longer in use.
860 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
862 unsigned expected
[] = {555};
863 EXPECT_RESOURCES(expected
, resources
);
864 EXPECT_TRUE(TestAndResetAvailable());
867 // Stop using any resources.
868 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
869 SetFrameData(frame
.Pass());
872 // Postpone collecting resources for a frame. They should still be there
874 layer_tree_host()->SetNeedsCommit();
877 // 444 and 999 are no longer in use. We sent two refs to 999, so we
878 // should get two back.
879 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
881 unsigned expected
[] = {444, 999, 999};
882 EXPECT_RESOURCES(expected
, resources
);
883 EXPECT_TRUE(TestAndResetAvailable());
889 // Resources are never immediately released.
890 ReturnedResourceArray empty_resources
;
891 resource_collection_
->TakeUnusedResourcesForChildCompositor(
893 EXPECT_EQ(0u, empty_resources
.size());
894 EXPECT_FALSE(TestAndResetAvailable());
897 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
898 ReturnUnusedResourcesFromParent(host_impl
);
902 SINGLE_AND_MULTI_THREAD_TEST_F(
903 LayerTreeHostDelegatedTestReturnUnusedResources
);
905 class LayerTreeHostDelegatedTestReusedResources
906 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
908 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
910 void DidCommitAndDrawFrame() override
{
911 scoped_ptr
<DelegatedFrameData
> frame
;
912 ReturnedResourceArray resources
;
914 int next_source_frame_number
= layer_tree_host()->source_frame_number();
915 switch (next_source_frame_number
) {
917 // Generate a frame with some resources in it.
918 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
919 AddTextureQuad(frame
.get(), 999);
920 AddTransferableResource(frame
.get(), 999);
921 AddTextureQuad(frame
.get(), 555);
922 AddTransferableResource(frame
.get(), 555);
923 AddTextureQuad(frame
.get(), 444);
924 AddTransferableResource(frame
.get(), 444);
925 SetFrameData(frame
.Pass());
928 // All of the resources are in use.
929 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
930 EXPECT_EQ(0u, resources
.size());
931 EXPECT_FALSE(TestAndResetAvailable());
933 // Keep using 999 but stop using 555 and 444.
934 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
935 AddTextureQuad(frame
.get(), 999);
936 AddTransferableResource(frame
.get(), 999);
937 SetFrameData(frame
.Pass());
939 // Resource are not immediately released.
940 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
941 EXPECT_EQ(0u, resources
.size());
942 EXPECT_FALSE(TestAndResetAvailable());
944 // Now using 555 and 444 again, but not 999.
945 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
946 AddTextureQuad(frame
.get(), 555);
947 AddTransferableResource(frame
.get(), 555);
948 AddTextureQuad(frame
.get(), 444);
949 AddTransferableResource(frame
.get(), 444);
950 SetFrameData(frame
.Pass());
953 // The 999 resource is the only unused one. Two references were sent, so
954 // two should be returned.
955 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
957 unsigned expected
[] = {999, 999};
958 EXPECT_RESOURCES(expected
, resources
);
959 EXPECT_TRUE(TestAndResetAvailable());
966 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
967 ReturnUnusedResourcesFromParent(host_impl
);
971 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestReusedResources
);
973 class LayerTreeHostDelegatedTestFrameBeforeAck
974 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
976 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
978 void DidCommitAndDrawFrame() override
{
979 scoped_ptr
<DelegatedFrameData
> frame
;
980 ReturnedResourceArray resources
;
982 int next_source_frame_number
= layer_tree_host()->source_frame_number();
983 switch (next_source_frame_number
) {
985 // Generate a frame with some resources in it.
986 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
987 AddTextureQuad(frame
.get(), 999);
988 AddTransferableResource(frame
.get(), 999);
989 AddTextureQuad(frame
.get(), 555);
990 AddTransferableResource(frame
.get(), 555);
991 AddTextureQuad(frame
.get(), 444);
992 AddTransferableResource(frame
.get(), 444);
993 SetFrameData(frame
.Pass());
996 // All of the resources are in use.
997 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
998 EXPECT_EQ(0u, resources
.size());
999 EXPECT_FALSE(TestAndResetAvailable());
1001 // Keep using 999 but stop using 555 and 444.
1002 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1003 AddTextureQuad(frame
.get(), 999);
1004 AddTransferableResource(frame
.get(), 999);
1005 SetFrameData(frame
.Pass());
1007 // Resource are not immediately released.
1008 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1009 EXPECT_EQ(0u, resources
.size());
1010 EXPECT_FALSE(TestAndResetAvailable());
1012 // The parent compositor (this one) does a commit.
1015 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1017 unsigned expected
[] = {444, 555};
1018 EXPECT_RESOURCES(expected
, resources
);
1019 EXPECT_TRUE(TestAndResetAvailable());
1022 // The child compositor sends a frame referring to resources not in the
1024 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1025 AddTextureQuad(frame
.get(), 999);
1026 AddTextureQuad(frame
.get(), 555);
1027 AddTextureQuad(frame
.get(), 444);
1028 SetFrameData(frame
.Pass());
1033 void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) override
{
1034 if (host_impl
->active_tree()->source_frame_number() != 3)
1037 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
1038 FakeDelegatedRendererLayerImpl
* delegated_impl
=
1039 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
1041 const ResourceProvider::ResourceIdMap
& map
=
1042 host_impl
->resource_provider()->GetChildToParentMap(
1043 delegated_impl
->ChildId());
1045 // The bad frame should be dropped. So we should only have one quad (the
1046 // one with resource 999) on the impl tree. And only 999 will be present
1047 // in the parent's resource provider.
1048 EXPECT_EQ(1u, map
.size());
1049 EXPECT_EQ(1u, map
.count(999));
1051 EXPECT_EQ(1u, delegated_impl
->Resources().size());
1052 EXPECT_EQ(1u, delegated_impl
->Resources().count(999));
1054 const RenderPass
* pass
= delegated_impl
->RenderPassesInDrawOrder()[0];
1055 EXPECT_EQ(1u, pass
->quad_list
.size());
1056 const TextureDrawQuad
* quad
=
1057 TextureDrawQuad::MaterialCast(pass
->quad_list
.front());
1058 EXPECT_EQ(map
.find(999)->second
, quad
->resource_id());
1063 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
1064 ReturnUnusedResourcesFromParent(host_impl
);
1068 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestFrameBeforeAck
);
1070 class LayerTreeHostDelegatedTestFrameBeforeTakeResources
1071 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
1073 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
1075 void DidCommitAndDrawFrame() override
{
1076 scoped_ptr
<DelegatedFrameData
> frame
;
1077 ReturnedResourceArray resources
;
1079 int next_source_frame_number
= layer_tree_host()->source_frame_number();
1080 switch (next_source_frame_number
) {
1082 // Generate a frame with some resources in it.
1083 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1084 AddTextureQuad(frame
.get(), 999);
1085 AddTransferableResource(frame
.get(), 999);
1086 AddTextureQuad(frame
.get(), 555);
1087 AddTransferableResource(frame
.get(), 555);
1088 AddTextureQuad(frame
.get(), 444);
1089 AddTransferableResource(frame
.get(), 444);
1090 SetFrameData(frame
.Pass());
1093 // All of the resources are in use.
1094 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1095 EXPECT_EQ(0u, resources
.size());
1096 EXPECT_FALSE(TestAndResetAvailable());
1098 // Keep using 999 but stop using 555 and 444.
1099 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1100 AddTextureQuad(frame
.get(), 999);
1101 AddTransferableResource(frame
.get(), 999);
1102 SetFrameData(frame
.Pass());
1104 // Resource are not immediately released.
1105 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1106 EXPECT_EQ(0u, resources
.size());
1107 EXPECT_FALSE(TestAndResetAvailable());
1109 // The parent compositor (this one) does a commit.
1112 // The child compositor sends a frame before taking resources back
1113 // from the previous commit. This frame makes use of the resources 555
1114 // and 444, which were just released during commit.
1115 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1116 AddTextureQuad(frame
.get(), 999);
1117 AddTransferableResource(frame
.get(), 999);
1118 AddTextureQuad(frame
.get(), 555);
1119 AddTransferableResource(frame
.get(), 555);
1120 AddTextureQuad(frame
.get(), 444);
1121 AddTransferableResource(frame
.get(), 444);
1122 SetFrameData(frame
.Pass());
1124 // The resources are used by the new frame but are returned anyway since
1125 // we passed them again.
1126 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1128 unsigned expected
[] = {444, 555};
1129 EXPECT_RESOURCES(expected
, resources
);
1130 EXPECT_TRUE(TestAndResetAvailable());
1134 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1135 EXPECT_EQ(0u, resources
.size());
1136 EXPECT_FALSE(TestAndResetAvailable());
1142 void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) override
{
1143 if (host_impl
->active_tree()->source_frame_number() != 3)
1146 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
1147 FakeDelegatedRendererLayerImpl
* delegated_impl
=
1148 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
1150 const ResourceProvider::ResourceIdMap
& map
=
1151 host_impl
->resource_provider()->GetChildToParentMap(
1152 delegated_impl
->ChildId());
1154 // The third frame has all of the resources in it again, the delegated
1155 // renderer layer should continue to own the resources for it.
1156 EXPECT_EQ(3u, map
.size());
1157 EXPECT_EQ(1u, map
.count(999));
1158 EXPECT_EQ(1u, map
.count(555));
1159 EXPECT_EQ(1u, map
.count(444));
1161 EXPECT_EQ(3u, delegated_impl
->Resources().size());
1162 EXPECT_EQ(1u, delegated_impl
->Resources().count(999));
1163 EXPECT_EQ(1u, delegated_impl
->Resources().count(555));
1164 EXPECT_EQ(1u, delegated_impl
->Resources().count(444));
1166 const RenderPass
* pass
= delegated_impl
->RenderPassesInDrawOrder()[0];
1167 EXPECT_EQ(3u, pass
->quad_list
.size());
1168 const TextureDrawQuad
* quad1
=
1169 TextureDrawQuad::MaterialCast(pass
->quad_list
.ElementAt(0));
1170 EXPECT_EQ(map
.find(999)->second
, quad1
->resource_id());
1171 const TextureDrawQuad
* quad2
=
1172 TextureDrawQuad::MaterialCast(pass
->quad_list
.ElementAt(1));
1173 EXPECT_EQ(map
.find(555)->second
, quad2
->resource_id());
1174 const TextureDrawQuad
* quad3
=
1175 TextureDrawQuad::MaterialCast(pass
->quad_list
.ElementAt(2));
1176 EXPECT_EQ(map
.find(444)->second
, quad3
->resource_id());
1179 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
1180 ReturnUnusedResourcesFromParent(host_impl
);
1184 SINGLE_AND_MULTI_THREAD_TEST_F(
1185 LayerTreeHostDelegatedTestFrameBeforeTakeResources
);
1187 class LayerTreeHostDelegatedTestBadFrame
1188 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
1190 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
1192 void DidCommitAndDrawFrame() override
{
1193 scoped_ptr
<DelegatedFrameData
> frame
;
1194 ReturnedResourceArray resources
;
1196 int next_source_frame_number
= layer_tree_host()->source_frame_number();
1197 switch (next_source_frame_number
) {
1199 // Generate a frame with some resources in it.
1200 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1201 AddTextureQuad(frame
.get(), 999);
1202 AddTransferableResource(frame
.get(), 999);
1203 AddTextureQuad(frame
.get(), 555);
1204 AddTransferableResource(frame
.get(), 555);
1205 SetFrameData(frame
.Pass());
1208 // All of the resources are in use.
1209 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1210 EXPECT_EQ(0u, resources
.size());
1211 EXPECT_FALSE(TestAndResetAvailable());
1213 // Generate a bad frame with a resource the layer doesn't have. The
1214 // 885 and 775 resources are unknown, while ownership of the legit 444
1215 // resource is passed in here. The bad frame does not use any of the
1216 // previous resources, 999 or 555.
1217 // A bad quad is present both before and after the good quad.
1218 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1219 AddTextureQuad(frame
.get(), 885);
1220 AddTextureQuad(frame
.get(), 444);
1221 AddTransferableResource(frame
.get(), 444);
1222 AddTextureQuad(frame
.get(), 775);
1223 SetFrameData(frame
.Pass());
1225 // The parent compositor (this one) does a commit.
1228 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1229 EXPECT_EQ(0u, resources
.size());
1230 EXPECT_FALSE(TestAndResetAvailable());
1232 // Now send a good frame with 999 again.
1233 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1234 AddTextureQuad(frame
.get(), 999);
1235 SetFrameData(frame
.Pass());
1237 // The bad frame's resource is given back to the child compositor.
1238 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1240 unsigned expected
[] = {444};
1241 EXPECT_RESOURCES(expected
, resources
);
1242 EXPECT_TRUE(TestAndResetAvailable());
1246 // The unused 555 from the last good frame is now released.
1247 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1249 unsigned expected
[] = {555};
1250 EXPECT_RESOURCES(expected
, resources
);
1251 EXPECT_TRUE(TestAndResetAvailable());
1259 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
1260 if (host_impl
->active_tree()->source_frame_number() < 1)
1263 ReturnUnusedResourcesFromParent(host_impl
);
1265 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
1266 FakeDelegatedRendererLayerImpl
* delegated_impl
=
1267 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
1269 const ResourceProvider::ResourceIdMap
& map
=
1270 host_impl
->resource_provider()->GetChildToParentMap(
1271 delegated_impl
->ChildId());
1273 switch (host_impl
->active_tree()->source_frame_number()) {
1275 // We have the first good frame with just 999 and 555 in it.
1277 EXPECT_EQ(2u, map
.size());
1278 EXPECT_EQ(1u, map
.count(999));
1279 EXPECT_EQ(1u, map
.count(555));
1281 EXPECT_EQ(2u, delegated_impl
->Resources().size());
1282 EXPECT_EQ(1u, delegated_impl
->Resources().count(999));
1283 EXPECT_EQ(1u, delegated_impl
->Resources().count(555));
1285 const RenderPass
* pass
= delegated_impl
->RenderPassesInDrawOrder()[0];
1286 EXPECT_EQ(2u, pass
->quad_list
.size());
1287 const TextureDrawQuad
* quad1
=
1288 TextureDrawQuad::MaterialCast(pass
->quad_list
.ElementAt(0));
1289 EXPECT_EQ(map
.find(999)->second
, quad1
->resource_id());
1290 const TextureDrawQuad
* quad2
=
1291 TextureDrawQuad::MaterialCast(pass
->quad_list
.ElementAt(1));
1292 EXPECT_EQ(map
.find(555)->second
, quad2
->resource_id());
1296 // We only keep resources from the last valid frame.
1297 EXPECT_EQ(2u, map
.size());
1298 EXPECT_EQ(1u, map
.count(999));
1299 EXPECT_EQ(1u, map
.count(555));
1301 EXPECT_EQ(2u, delegated_impl
->Resources().size());
1302 EXPECT_EQ(1u, delegated_impl
->Resources().count(999));
1303 EXPECT_EQ(1u, delegated_impl
->Resources().count(555));
1305 // The bad frame is dropped though, we still have the frame with 999 and
1307 const RenderPass
* pass
= delegated_impl
->RenderPassesInDrawOrder()[0];
1308 EXPECT_EQ(2u, pass
->quad_list
.size());
1309 const TextureDrawQuad
* quad1
=
1310 TextureDrawQuad::MaterialCast(pass
->quad_list
.ElementAt(0));
1311 EXPECT_EQ(map
.find(999)->second
, quad1
->resource_id());
1312 const TextureDrawQuad
* quad2
=
1313 TextureDrawQuad::MaterialCast(pass
->quad_list
.ElementAt(1));
1314 EXPECT_EQ(map
.find(555)->second
, quad2
->resource_id());
1318 // We have the new good frame with just 999 in it.
1319 EXPECT_EQ(1u, map
.size());
1320 EXPECT_EQ(1u, map
.count(999));
1322 EXPECT_EQ(1u, delegated_impl
->Resources().size());
1323 EXPECT_EQ(1u, delegated_impl
->Resources().count(999));
1325 const RenderPass
* pass
= delegated_impl
->RenderPassesInDrawOrder()[0];
1326 EXPECT_EQ(1u, pass
->quad_list
.size());
1327 const TextureDrawQuad
* quad1
=
1328 TextureDrawQuad::MaterialCast(pass
->quad_list
.front());
1329 EXPECT_EQ(map
.find(999)->second
, quad1
->resource_id());
1336 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestBadFrame
);
1338 class LayerTreeHostDelegatedTestUnnamedResource
1339 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
1341 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
1343 void DidCommitAndDrawFrame() override
{
1344 scoped_ptr
<DelegatedFrameData
> frame
;
1345 ReturnedResourceArray resources
;
1347 int next_source_frame_number
= layer_tree_host()->source_frame_number();
1348 switch (next_source_frame_number
) {
1350 // This frame includes two resources in it, but only uses one.
1351 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1352 AddTransferableResource(frame
.get(), 999);
1353 AddTextureQuad(frame
.get(), 555);
1354 AddTransferableResource(frame
.get(), 555);
1355 SetFrameData(frame
.Pass());
1358 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1359 EXPECT_EQ(0u, resources
.size());
1360 EXPECT_FALSE(TestAndResetAvailable());
1362 // Now send an empty frame.
1363 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1364 SetFrameData(frame
.Pass());
1366 // The unused resource should be returned.
1367 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1369 unsigned expected
[] = {999};
1370 EXPECT_RESOURCES(expected
, resources
);
1371 EXPECT_TRUE(TestAndResetAvailable());
1379 void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) override
{
1380 if (host_impl
->active_tree()->source_frame_number() != 1)
1383 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
1384 FakeDelegatedRendererLayerImpl
* delegated_impl
=
1385 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
1387 const ResourceProvider::ResourceIdMap
& map
=
1388 host_impl
->resource_provider()->GetChildToParentMap(
1389 delegated_impl
->ChildId());
1391 // The layer only held on to the resource that was used.
1392 EXPECT_EQ(1u, map
.size());
1393 EXPECT_EQ(1u, map
.count(555));
1395 EXPECT_EQ(1u, delegated_impl
->Resources().size());
1396 EXPECT_EQ(1u, delegated_impl
->Resources().count(555));
1400 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestUnnamedResource
);
1402 class LayerTreeHostDelegatedTestDontLeakResource
1403 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
1405 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
1407 void DidCommitAndDrawFrame() override
{
1408 scoped_ptr
<DelegatedFrameData
> frame
;
1409 ReturnedResourceArray resources
;
1411 int next_source_frame_number
= layer_tree_host()->source_frame_number();
1412 switch (next_source_frame_number
) {
1414 // This frame includes two resources in it.
1415 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1416 AddTextureQuad(frame
.get(), 999);
1417 AddTransferableResource(frame
.get(), 999);
1418 AddTextureQuad(frame
.get(), 555);
1419 AddTransferableResource(frame
.get(), 555);
1420 SetFrameData(frame
.Pass());
1422 // But then we immediately stop using 999.
1423 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1424 AddTextureQuad(frame
.get(), 555);
1425 AddTransferableResource(frame
.get(), 555);
1426 SetFrameData(frame
.Pass());
1429 // The unused resources should be returned. 555 is still used, but it's
1430 // returned once to account for the first frame.
1431 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1433 unsigned expected
[] = {555, 999};
1434 EXPECT_RESOURCES(expected
, resources
);
1435 EXPECT_TRUE(TestAndResetAvailable());
1437 // Send a frame with no resources in it.
1438 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1439 SetFrameData(frame
.Pass());
1442 // The now unused resource 555 should be returned.
1444 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1446 unsigned expected
[] = {555};
1447 EXPECT_RESOURCES(expected
, resources
);
1448 EXPECT_TRUE(TestAndResetAvailable());
1455 void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) override
{
1456 if (host_impl
->active_tree()->source_frame_number() != 1)
1459 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
1460 FakeDelegatedRendererLayerImpl
* delegated_impl
=
1461 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
1463 const ResourceProvider::ResourceIdMap
& map
=
1464 host_impl
->resource_provider()->GetChildToParentMap(
1465 delegated_impl
->ChildId());
1467 // The layer only held on to the resource that was used.
1468 EXPECT_EQ(1u, map
.size());
1469 EXPECT_EQ(1u, map
.count(555));
1471 EXPECT_EQ(1u, delegated_impl
->Resources().size());
1472 EXPECT_EQ(1u, delegated_impl
->Resources().count(555));
1475 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
1476 ReturnUnusedResourcesFromParent(host_impl
);
1480 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestDontLeakResource
);
1482 class LayerTreeHostDelegatedTestResourceSentToParent
1483 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
1485 void DidCommitAndDrawFrame() override
{
1486 scoped_ptr
<DelegatedFrameData
> frame
;
1487 ReturnedResourceArray resources
;
1489 int next_source_frame_number
= layer_tree_host()->source_frame_number();
1490 switch (next_source_frame_number
) {
1492 // This frame includes two resources in it.
1493 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1494 AddTextureQuad(frame
.get(), 999);
1495 AddTransferableResource(frame
.get(), 999);
1496 AddTextureQuad(frame
.get(), 555);
1497 AddTransferableResource(frame
.get(), 555);
1498 SetFrameData(frame
.Pass());
1501 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1502 EXPECT_EQ(0u, resources
.size());
1503 EXPECT_FALSE(TestAndResetAvailable());
1505 // 999 is in use in the grandparent compositor, generate a frame without
1507 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1508 AddTextureQuad(frame
.get(), 555);
1509 AddTransferableResource(frame
.get(), 555);
1510 SetFrameData(frame
.Pass());
1513 // Since 999 is in the grandparent it is not returned.
1514 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1515 EXPECT_EQ(0u, resources
.size());
1516 EXPECT_FALSE(TestAndResetAvailable());
1518 // The impl side will get back the resource at some point.
1519 ImplThreadTaskRunner()->PostTask(FROM_HERE
,
1520 receive_resource_on_thread_
);
1525 void ReceiveResourceOnThread(LayerTreeHostImpl
* host_impl
) {
1526 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
1527 FakeDelegatedRendererLayerImpl
* delegated_impl
=
1528 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
1530 const ResourceProvider::ResourceIdMap
& map
=
1531 host_impl
->resource_provider()->GetChildToParentMap(
1532 delegated_impl
->ChildId());
1534 // Receive 999 back from the grandparent.
1535 CompositorFrameAck ack
;
1536 output_surface()->ReturnResource(map
.find(999)->second
, &ack
);
1537 host_impl
->ReclaimResources(&ack
);
1540 void UnusedResourcesAreAvailable() override
{
1541 EXPECT_EQ(3, layer_tree_host()->source_frame_number());
1543 ReturnedResourceArray resources
;
1545 // 999 was returned from the grandparent and could be released.
1546 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1548 unsigned expected
[] = {999};
1549 EXPECT_RESOURCES(expected
, resources
);
1555 void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) override
{
1556 if (host_impl
->active_tree()->source_frame_number() < 1)
1559 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
1560 FakeDelegatedRendererLayerImpl
* delegated_impl
=
1561 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
1563 const ResourceProvider::ResourceIdMap
& map
=
1564 host_impl
->resource_provider()->GetChildToParentMap(
1565 delegated_impl
->ChildId());
1567 switch (host_impl
->active_tree()->source_frame_number()) {
1569 EXPECT_EQ(2u, map
.size());
1570 EXPECT_EQ(1u, map
.count(999));
1571 EXPECT_EQ(1u, map
.count(555));
1573 EXPECT_EQ(2u, delegated_impl
->Resources().size());
1574 EXPECT_EQ(1u, delegated_impl
->Resources().count(999));
1575 EXPECT_EQ(1u, delegated_impl
->Resources().count(555));
1577 // The 999 resource will be sent to a grandparent compositor.
1581 EXPECT_EQ(2u, map
.size());
1582 EXPECT_EQ(1u, map
.count(999));
1583 EXPECT_EQ(1u, map
.count(555));
1585 // 999 is in the parent, so not held by delegated renderer layer.
1586 EXPECT_EQ(1u, delegated_impl
->Resources().size());
1587 EXPECT_EQ(1u, delegated_impl
->Resources().count(555));
1589 receive_resource_on_thread_
=
1590 base::Bind(&LayerTreeHostDelegatedTestResourceSentToParent::
1591 ReceiveResourceOnThread
,
1592 base::Unretained(this),
1597 // 999 should be released.
1598 EXPECT_EQ(1u, map
.size());
1599 EXPECT_EQ(1u, map
.count(555));
1601 EXPECT_EQ(1u, delegated_impl
->Resources().size());
1602 EXPECT_EQ(1u, delegated_impl
->Resources().count(map
.find(555)->second
));
1607 base::Closure receive_resource_on_thread_
;
1610 SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(
1611 LayerTreeHostDelegatedTestResourceSentToParent
);
1613 class LayerTreeHostDelegatedTestCommitWithoutTake
1614 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
1616 void BeginTest() override
{
1617 // Prevent drawing with resources that are sent to the grandparent.
1618 layer_tree_host()->SetViewportSize(gfx::Size());
1619 PostSetNeedsCommitToMainThread();
1622 void DidCommit() override
{
1623 scoped_ptr
<DelegatedFrameData
> frame
;
1624 ReturnedResourceArray resources
;
1626 int next_source_frame_number
= layer_tree_host()->source_frame_number();
1627 switch (next_source_frame_number
) {
1629 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1630 AddTextureQuad(frame
.get(), 999);
1631 AddTransferableResource(frame
.get(), 999);
1632 AddTextureQuad(frame
.get(), 555);
1633 AddTransferableResource(frame
.get(), 555);
1634 AddTextureQuad(frame
.get(), 444);
1635 AddTransferableResource(frame
.get(), 444);
1636 SetFrameData(frame
.Pass());
1639 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1640 EXPECT_EQ(0u, resources
.size());
1641 EXPECT_FALSE(TestAndResetAvailable());
1643 // Stop using 999 and 444 in this frame and commit.
1644 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1645 AddTextureQuad(frame
.get(), 555);
1646 AddTransferableResource(frame
.get(), 555);
1647 SetFrameData(frame
.Pass());
1648 // 999 and 444 will be returned for frame 1, but not 555 since it's in
1649 // the current frame.
1652 // Don't take resources here, but set a new frame that uses 999 again.
1653 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1654 AddTextureQuad(frame
.get(), 999);
1655 AddTransferableResource(frame
.get(), 999);
1656 AddTextureQuad(frame
.get(), 555);
1657 AddTransferableResource(frame
.get(), 555);
1658 SetFrameData(frame
.Pass());
1661 // 555 from frame 1 and 2 isn't returned since it's still in use. 999
1662 // from frame 1 is returned though.
1663 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1665 unsigned expected
[] = {444, 999};
1666 EXPECT_RESOURCES(expected
, resources
);
1667 EXPECT_TRUE(TestAndResetAvailable());
1670 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1671 SetFrameData(frame
.Pass());
1672 // 555 will be returned 3 times for frames 1 2 and 3, and 999 will be
1673 // returned once for frame 3.
1676 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1678 unsigned expected
[] = {555, 555, 555, 999};
1679 EXPECT_RESOURCES(expected
, resources
);
1680 EXPECT_TRUE(TestAndResetAvailable());
1688 void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) override
{
1689 if (host_impl
->active_tree()->source_frame_number() < 1)
1692 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
1693 FakeDelegatedRendererLayerImpl
* delegated_impl
=
1694 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
1696 const ResourceProvider::ResourceIdMap
& map
=
1697 host_impl
->resource_provider()->GetChildToParentMap(
1698 delegated_impl
->ChildId());
1700 switch (host_impl
->active_tree()->source_frame_number()) {
1702 EXPECT_EQ(3u, map
.size());
1703 EXPECT_EQ(1u, map
.count(999));
1704 EXPECT_EQ(1u, map
.count(555));
1705 EXPECT_EQ(1u, map
.count(444));
1707 EXPECT_EQ(3u, delegated_impl
->Resources().size());
1708 EXPECT_EQ(1u, delegated_impl
->Resources().count(999));
1709 EXPECT_EQ(1u, delegated_impl
->Resources().count(555));
1710 EXPECT_EQ(1u, delegated_impl
->Resources().count(444));
1713 EXPECT_EQ(1u, map
.size());
1714 EXPECT_EQ(1u, map
.count(555));
1716 EXPECT_EQ(1u, delegated_impl
->Resources().size());
1717 EXPECT_EQ(1u, delegated_impl
->Resources().count(555));
1720 EXPECT_EQ(2u, map
.size());
1721 EXPECT_EQ(1u, map
.count(999));
1722 EXPECT_EQ(1u, map
.count(555));
1724 EXPECT_EQ(2u, delegated_impl
->Resources().size());
1725 EXPECT_EQ(1u, delegated_impl
->Resources().count(999));
1726 EXPECT_EQ(1u, delegated_impl
->Resources().count(555));
1731 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestCommitWithoutTake
);
1733 class DelegatedFrameIsActivatedDuringCommit
1734 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
1736 DelegatedFrameIsActivatedDuringCommit() : returned_resource_count_(0) {}
1738 void BeginTest() override
{
1739 activate_count_
= 0;
1741 scoped_ptr
<DelegatedFrameData
> frame
=
1742 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1743 AddTextureQuad(frame
.get(), 999);
1744 AddTransferableResource(frame
.get(), 999);
1745 SetFrameData(frame
.Pass());
1747 PostSetNeedsCommitToMainThread();
1750 void WillActivateTreeOnThread(LayerTreeHostImpl
* impl
) override
{
1754 void DidCommit() override
{
1755 switch (layer_tree_host()->source_frame_number()) {
1757 // The first frame has been activated. Set a new frame, and
1758 // expect the next commit to finish *after* it is activated.
1759 scoped_ptr
<DelegatedFrameData
> frame
=
1760 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1761 AddTextureQuad(frame
.get(), 555);
1762 AddTransferableResource(frame
.get(), 555);
1763 SetFrameData(frame
.Pass());
1767 // The second frame has been activated. Remove the layer from
1768 // the tree to cause another commit/activation. The commit should
1769 // finish *after* the layer is removed from the active tree.
1770 delegated_
->RemoveFromParent();
1773 // Finish the test by releasing resources on the next frame.
1774 scoped_ptr
<DelegatedFrameData
> frame
=
1775 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1776 SetFrameData(frame
.Pass());
1781 void CommitCompleteOnThread(LayerTreeHostImpl
* host_impl
) override
{
1782 switch (host_impl
->active_tree()->source_frame_number()) {
1784 // The activate for the 1st frame should have happened before now.
1785 EXPECT_EQ(1, activate_count_
);
1789 // The activate for the 2nd frame should have happened before now.
1790 EXPECT_EQ(2, activate_count_
);
1794 // The activate to remove the layer should have happened before now.
1795 EXPECT_EQ(3, activate_count_
);
1805 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
1806 ReturnUnusedResourcesFromParent(host_impl
);
1809 void UnusedResourcesAreAvailable() override
{
1810 LayerTreeHostDelegatedTestCaseSingleDelegatedLayer::
1811 UnusedResourcesAreAvailable();
1812 ReturnedResourceArray resources
;
1813 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1814 EXPECT_TRUE(TestAndResetAvailable());
1815 returned_resource_count_
+= resources
.size();
1816 if (returned_resource_count_
== 2)
1820 int activate_count_
;
1821 size_t returned_resource_count_
;
1824 SINGLE_AND_MULTI_THREAD_TEST_F(
1825 DelegatedFrameIsActivatedDuringCommit
);
1827 class LayerTreeHostDelegatedTestTwoImplLayers
1828 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
1830 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
1832 void DidCommitAndDrawFrame() override
{
1833 scoped_ptr
<DelegatedFrameData
> frame
;
1834 ReturnedResourceArray resources
;
1836 int next_source_frame_number
= layer_tree_host()->source_frame_number();
1837 switch (next_source_frame_number
) {
1839 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1840 AddTextureQuad(frame
.get(), 999);
1841 AddTransferableResource(frame
.get(), 999);
1842 AddTextureQuad(frame
.get(), 555);
1843 AddTransferableResource(frame
.get(), 555);
1844 SetFrameData(frame
.Pass());
1847 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1848 EXPECT_EQ(0u, resources
.size());
1849 EXPECT_FALSE(TestAndResetAvailable());
1851 // Remove the delegated layer and replace it with a new one. Use the
1852 // same frame and resources for it.
1853 delegated_
->RemoveFromParent();
1854 delegated_
= CreateDelegatedLayer(frame_provider_
.get());
1857 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1858 EXPECT_EQ(0u, resources
.size());
1859 EXPECT_FALSE(TestAndResetAvailable());
1861 // Use a frame with no resources in it.
1862 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1863 SetFrameData(frame
.Pass());
1866 // We gave one frame to the frame provider, so we should get one
1867 // ref back for each resource.
1868 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1870 unsigned expected
[] = {555, 999};
1871 EXPECT_RESOURCES(expected
, resources
);
1872 EXPECT_TRUE(TestAndResetAvailable());
1879 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
1880 ReturnUnusedResourcesFromParent(host_impl
);
1884 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestTwoImplLayers
);
1886 class LayerTreeHostDelegatedTestTwoImplLayersTwoFrames
1887 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
1889 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
1891 void DidCommitAndDrawFrame() override
{
1892 scoped_ptr
<DelegatedFrameData
> frame
;
1893 ReturnedResourceArray resources
;
1895 int next_source_frame_number
= layer_tree_host()->source_frame_number();
1896 switch (next_source_frame_number
) {
1898 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1899 AddTextureQuad(frame
.get(), 999);
1900 AddTransferableResource(frame
.get(), 999);
1901 AddTextureQuad(frame
.get(), 555);
1902 AddTransferableResource(frame
.get(), 555);
1903 SetFrameData(frame
.Pass());
1906 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1907 EXPECT_EQ(0u, resources
.size());
1908 EXPECT_FALSE(TestAndResetAvailable());
1910 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1911 AddTextureQuad(frame
.get(), 999);
1912 AddTransferableResource(frame
.get(), 999);
1913 AddTextureQuad(frame
.get(), 555);
1914 AddTransferableResource(frame
.get(), 555);
1916 // Remove the delegated layer and replace it with a new one. Make a new
1917 // frame but with the same resources for it.
1918 delegated_
->RemoveFromParent();
1921 frame_provider_
->SetFrameData(frame
.Pass());
1922 delegated_
= CreateDelegatedLayer(frame_provider_
.get());
1925 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1926 EXPECT_EQ(0u, resources
.size());
1927 EXPECT_FALSE(TestAndResetAvailable());
1929 // Use a frame with no resources in it.
1930 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1931 SetFrameData(frame
.Pass());
1934 // We gave two frames to the frame provider, so we should get two
1935 // refs back for each resource.
1936 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1938 unsigned expected
[] = {555, 555, 999, 999};
1939 EXPECT_RESOURCES(expected
, resources
);
1940 EXPECT_TRUE(TestAndResetAvailable());
1947 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
1948 ReturnUnusedResourcesFromParent(host_impl
);
1952 SINGLE_AND_MULTI_THREAD_TEST_F(
1953 LayerTreeHostDelegatedTestTwoImplLayersTwoFrames
);
1955 class LayerTreeHostDelegatedTestTwoLayers
1956 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
1958 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
1960 void DidCommitAndDrawFrame() override
{
1961 scoped_ptr
<DelegatedFrameData
> frame
;
1962 ReturnedResourceArray resources
;
1964 int next_source_frame_number
= layer_tree_host()->source_frame_number();
1965 switch (next_source_frame_number
) {
1967 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1968 AddTextureQuad(frame
.get(), 999);
1969 AddTransferableResource(frame
.get(), 999);
1970 AddTextureQuad(frame
.get(), 555);
1971 AddTransferableResource(frame
.get(), 555);
1973 // Create a DelegatedRendererLayer using the frame.
1974 SetFrameData(frame
.Pass());
1977 // Create a second DelegatedRendererLayer using the same frame provider.
1978 delegated_thief_
= CreateDelegatedLayer(frame_provider_
.get());
1979 root_
->AddChild(delegated_thief_
);
1981 // And drop our ref on the frame provider so only the layers keep it
1983 frame_provider_
= NULL
;
1986 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1987 EXPECT_EQ(0u, resources
.size());
1988 EXPECT_FALSE(TestAndResetAvailable());
1990 // Remove one delegated layer from the tree. No resources should be
1992 delegated_
->RemoveFromParent();
1995 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1996 EXPECT_EQ(0u, resources
.size());
1997 EXPECT_FALSE(TestAndResetAvailable());
1999 // Put the first layer back, and remove the other layer and destroy it.
2000 // No resources should be returned yet.
2001 root_
->AddChild(delegated_
);
2002 delegated_thief_
->RemoveFromParent();
2003 delegated_thief_
= NULL
;
2006 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
2007 EXPECT_EQ(0u, resources
.size());
2008 EXPECT_FALSE(TestAndResetAvailable());
2010 // Remove the first layer from the tree again. The resources are still
2011 // held by the main thread layer.
2012 delegated_
->RemoveFromParent();
2015 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
2016 EXPECT_EQ(0u, resources
.size());
2017 EXPECT_FALSE(TestAndResetAvailable());
2019 // Destroy the layer and the resources should be returned immediately.
2022 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
2024 unsigned expected
[] = {555, 999};
2025 EXPECT_RESOURCES(expected
, resources
);
2026 EXPECT_TRUE(TestAndResetAvailable());
2033 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
2034 ReturnUnusedResourcesFromParent(host_impl
);
2037 scoped_refptr
<DelegatedRendererLayer
> delegated_thief_
;
2040 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestTwoLayers
);
2042 class LayerTreeHostDelegatedTestRemoveAndAddToTree
2043 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
2045 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
2047 void DidCommitAndDrawFrame() override
{
2048 scoped_ptr
<DelegatedFrameData
> frame
;
2049 ReturnedResourceArray resources
;
2051 int next_source_frame_number
= layer_tree_host()->source_frame_number();
2052 switch (next_source_frame_number
) {
2054 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2055 AddTextureQuad(frame
.get(), 999);
2056 AddTransferableResource(frame
.get(), 999);
2057 AddTextureQuad(frame
.get(), 555);
2058 AddTransferableResource(frame
.get(), 555);
2060 // Create a DelegatedRendererLayer using the frame.
2061 SetFrameData(frame
.Pass());
2064 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
2065 EXPECT_EQ(0u, resources
.size());
2066 EXPECT_FALSE(TestAndResetAvailable());
2068 // Remove the layer from the tree. The resources should not be returned
2069 // since they are still on the main thread layer.
2070 delegated_
->RemoveFromParent();
2073 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
2074 EXPECT_EQ(0u, resources
.size());
2075 EXPECT_FALSE(TestAndResetAvailable());
2077 // Add the layer back to the tree.
2078 layer_tree_host()->root_layer()->AddChild(delegated_
);
2081 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
2082 EXPECT_EQ(0u, resources
.size());
2083 EXPECT_FALSE(TestAndResetAvailable());
2085 // Set a new frame. Resources should be returned.
2086 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2087 AddTextureQuad(frame
.get(), 888);
2088 AddTransferableResource(frame
.get(), 888);
2089 AddTextureQuad(frame
.get(), 777);
2090 AddTransferableResource(frame
.get(), 777);
2091 SetFrameData(frame
.Pass());
2094 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
2096 unsigned expected
[] = {555, 999};
2097 EXPECT_RESOURCES(expected
, resources
);
2098 EXPECT_TRUE(TestAndResetAvailable());
2101 // Destroy the layer.
2102 delegated_
->RemoveFromParent();
2106 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
2107 EXPECT_EQ(0u, resources
.size());
2108 EXPECT_FALSE(TestAndResetAvailable());
2110 // Destroy the frame provider. Resources should be returned.
2111 frame_provider_
= NULL
;
2113 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
2115 unsigned expected
[] = {777, 888};
2116 EXPECT_RESOURCES(expected
, resources
);
2117 EXPECT_TRUE(TestAndResetAvailable());
2124 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
2125 ReturnUnusedResourcesFromParent(host_impl
);
2128 scoped_refptr
<DelegatedRendererLayer
> delegated_thief_
;
2131 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestRemoveAndAddToTree
);
2133 class LayerTreeHostDelegatedTestRemoveAndChangeResources
2134 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
2136 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
2138 void DidCommitAndDrawFrame() override
{
2139 scoped_ptr
<DelegatedFrameData
> frame
;
2140 ReturnedResourceArray resources
;
2142 int next_source_frame_number
= layer_tree_host()->source_frame_number();
2143 switch (next_source_frame_number
) {
2145 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2146 AddTextureQuad(frame
.get(), 999);
2147 AddTransferableResource(frame
.get(), 999);
2148 AddTextureQuad(frame
.get(), 555);
2149 AddTransferableResource(frame
.get(), 555);
2151 // Create a DelegatedRendererLayer using the frame.
2152 SetFrameData(frame
.Pass());
2155 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
2156 EXPECT_EQ(0u, resources
.size());
2157 EXPECT_FALSE(TestAndResetAvailable());
2159 // Remove the layer from the tree. The resources should not be returned
2160 // since they are still on the main thread layer.
2161 delegated_
->RemoveFromParent();
2164 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
2165 EXPECT_EQ(0u, resources
.size());
2166 EXPECT_FALSE(TestAndResetAvailable());
2168 // Set a new frame. Resources should be returned immediately.
2169 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2170 AddTextureQuad(frame
.get(), 888);
2171 AddTransferableResource(frame
.get(), 888);
2172 AddTextureQuad(frame
.get(), 777);
2173 AddTransferableResource(frame
.get(), 777);
2174 SetFrameData(frame
.Pass());
2176 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
2178 unsigned expected
[] = {555, 999};
2179 EXPECT_RESOURCES(expected
, resources
);
2180 EXPECT_TRUE(TestAndResetAvailable());
2184 // Destroy the frame provider.
2185 frame_provider_
= NULL
;
2187 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
2188 EXPECT_EQ(0u, resources
.size());
2189 EXPECT_FALSE(TestAndResetAvailable());
2191 // Destroy the layer. Resources should be returned.
2194 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
2196 unsigned expected
[] = {777, 888};
2197 EXPECT_RESOURCES(expected
, resources
);
2198 EXPECT_TRUE(TestAndResetAvailable());
2205 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
2206 ReturnUnusedResourcesFromParent(host_impl
);
2209 scoped_refptr
<DelegatedRendererLayer
> delegated_thief_
;
2212 SINGLE_AND_MULTI_THREAD_TEST_F(
2213 LayerTreeHostDelegatedTestRemoveAndChangeResources
);
2215 class LayerTreeHostDelegatedTestActiveFrameIsValid
2216 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
2218 LayerTreeHostDelegatedTestActiveFrameIsValid()
2219 : drew_with_pending_tree_(false) {}
2221 void DidCommitAndDrawFrame() override
{
2222 scoped_ptr
<DelegatedFrameData
> frame
;
2223 switch (layer_tree_host()->source_frame_number()) {
2225 // This frame goes to the active tree.
2226 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2227 AddTextureQuad(frame
.get(), 999);
2228 AddTransferableResource(frame
.get(), 999);
2229 SetFrameData(frame
.Pass());
2232 // This frame stops in the pending tree while we redraw the active tree.
2233 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2234 AddTextureQuad(frame
.get(), 555);
2235 AddTransferableResource(frame
.get(), 555);
2236 SetFrameData(frame
.Pass());
2241 void DrawLayersOnThread(LayerTreeHostImpl
* host_impl
) override
{
2242 if (host_impl
->active_tree()->source_frame_number() < 1)
2245 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
2246 FakeDelegatedRendererLayerImpl
* delegated_impl
=
2247 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
2248 const ResourceProvider::ResourceIdMap
& map
=
2249 host_impl
->resource_provider()->GetChildToParentMap(
2250 delegated_impl
->ChildId());
2252 switch (host_impl
->active_tree()->source_frame_number()) {
2254 if (!host_impl
->pending_tree()) {
2255 // Frame 2 is blocked from activating until another draw happens with
2256 // Frame 1. This ensures we draw a different active frame from
2257 // what's in the pending tree.
2258 host_impl
->BlockNotifyReadyToActivateForTesting(true);
2259 host_impl
->SetNeedsRedrawRect(gfx::Rect(1, 1));
2263 // The resources in the active tree should be valid.
2264 EXPECT_EQ(1u, map
.count(999));
2266 host_impl
->BlockNotifyReadyToActivateForTesting(false);
2267 drew_with_pending_tree_
= true;
2270 EXPECT_TRUE(drew_with_pending_tree_
);
2272 // The resources in the active tree should be valid.
2273 EXPECT_EQ(1u, map
.count(555));
2279 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
2280 // Return everything so that we can reliably delete resources that lose
2281 // their references. This would happen if the tab was backgrounded or
2282 // the parent decided to drop all resources for some reason.
2283 ReturnAllResourcesFromParent(host_impl
);
2286 bool drew_with_pending_tree_
;
2289 // This test blocks activation which is not supported for single thread mode.
2290 MULTI_THREAD_BLOCKNOTIFY_TEST_F(LayerTreeHostDelegatedTestActiveFrameIsValid
);