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;
117 TextureDrawQuad
* invalid_draw_quad
=
118 root_pass
->CreateAndAppendDrawQuad
<TextureDrawQuad
>();
119 invalid_draw_quad
->SetNew(shared_quad_state
,
131 frame
->render_pass_list
.push_back(root_pass
.Pass());
135 void AddTransferableResource(DelegatedFrameData
* frame
,
136 ResourceProvider::ResourceId resource_id
) {
137 TransferableResource resource
;
138 resource
.id
= resource_id
;
139 resource
.mailbox_holder
.texture_target
= GL_TEXTURE_2D
;
140 GLbyte arbitrary_mailbox
[GL_MAILBOX_SIZE_CHROMIUM
] = {
141 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2,
142 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4,
143 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4};
144 resource
.mailbox_holder
.mailbox
.SetName(arbitrary_mailbox
);
145 frame
->resource_list
.push_back(resource
);
148 void AddTextureQuad(DelegatedFrameData
* frame
,
149 ResourceProvider::ResourceId resource_id
) {
150 RenderPass
* render_pass
= frame
->render_pass_list
[0];
151 SharedQuadState
* sqs
= render_pass
->CreateAndAppendSharedQuadState();
152 TextureDrawQuad
* quad
=
153 render_pass
->CreateAndAppendDrawQuad
<TextureDrawQuad
>();
154 float vertex_opacity
[4] = { 1.f
, 1.f
, 1.f
, 1.f
};
156 gfx::Rect(0, 0, 10, 10),
157 gfx::Rect(0, 0, 10, 10),
158 gfx::Rect(0, 0, 10, 10),
161 gfx::PointF(0.f
, 0.f
),
162 gfx::PointF(1.f
, 1.f
),
168 void AddRenderPass(DelegatedFrameData
* frame
,
170 const gfx::Rect
& output_rect
,
171 const gfx::Rect
& damage_rect
,
172 const FilterOperations
& filters
,
173 const FilterOperations
& background_filters
) {
174 for (size_t i
= 0; i
< frame
->render_pass_list
.size(); ++i
)
175 DCHECK(id
!= frame
->render_pass_list
[i
]->id
);
177 scoped_ptr
<RenderPass
> pass(RenderPass::Create());
182 frame
->render_pass_list
.push_back(pass
.Pass());
184 RenderPass
* render_pass
= frame
->render_pass_list
[0];
185 SharedQuadState
* sqs
= render_pass
->CreateAndAppendSharedQuadState();
186 RenderPassDrawQuad
* quad
=
187 render_pass
->CreateAndAppendDrawQuad
<RenderPassDrawQuad
>();
201 static ResourceProvider::ResourceId
AppendResourceId(
202 std::vector
<ResourceProvider::ResourceId
>* resources_in_last_sent_frame
,
203 ResourceProvider::ResourceId resource_id
) {
204 resources_in_last_sent_frame
->push_back(resource_id
);
208 void ReturnUnusedResourcesFromParent(LayerTreeHostImpl
* host_impl
) {
209 DelegatedFrameData
* delegated_frame_data
=
210 output_surface()->last_sent_frame().delegated_frame_data
.get();
211 if (!delegated_frame_data
)
214 std::vector
<ResourceProvider::ResourceId
> resources_in_last_sent_frame
;
215 for (size_t i
= 0; i
< delegated_frame_data
->resource_list
.size(); ++i
) {
216 resources_in_last_sent_frame
.push_back(
217 delegated_frame_data
->resource_list
[i
].id
);
220 std::vector
<ResourceProvider::ResourceId
> resources_to_return
;
222 const TransferableResourceArray
& resources_held_by_parent
=
223 output_surface()->resources_held_by_parent();
224 for (size_t i
= 0; i
< resources_held_by_parent
.size(); ++i
) {
225 ResourceProvider::ResourceId resource_in_parent
=
226 resources_held_by_parent
[i
].id
;
227 bool resource_in_parent_is_not_part_of_frame
=
228 std::find(resources_in_last_sent_frame
.begin(),
229 resources_in_last_sent_frame
.end(),
230 resource_in_parent
) == resources_in_last_sent_frame
.end();
231 if (resource_in_parent_is_not_part_of_frame
)
232 resources_to_return
.push_back(resource_in_parent
);
235 if (resources_to_return
.empty())
238 CompositorFrameAck ack
;
239 for (size_t i
= 0; i
< resources_to_return
.size(); ++i
)
240 output_surface()->ReturnResource(resources_to_return
[i
], &ack
);
241 host_impl
->ReclaimResources(&ack
);
245 class LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
246 : public LayerTreeHostDelegatedTest
,
247 public DelegatedFrameResourceCollectionClient
{
249 LayerTreeHostDelegatedTestCaseSingleDelegatedLayer()
250 : resource_collection_(new DelegatedFrameResourceCollection
),
252 resource_collection_
->SetClient(this);
255 void SetupTree() override
{
256 root_
= Layer::Create();
257 root_
->SetBounds(gfx::Size(15, 15));
259 layer_tree_host()->SetRootLayer(root_
);
260 LayerTreeHostDelegatedTest::SetupTree();
263 void BeginTest() override
{
264 resource_collection_
->SetClient(this);
265 PostSetNeedsCommitToMainThread();
268 void SetFrameData(scoped_ptr
<DelegatedFrameData
> frame_data
) {
269 RenderPass
* root_pass
= frame_data
->render_pass_list
.back();
270 gfx::Size frame_size
= root_pass
->output_rect
.size();
272 if (frame_provider_
.get() && frame_size
== frame_provider_
->frame_size()) {
273 frame_provider_
->SetFrameData(frame_data
.Pass());
277 if (delegated_
.get()) {
278 delegated_
->RemoveFromParent();
280 frame_provider_
= NULL
;
283 frame_provider_
= new DelegatedFrameProvider(resource_collection_
.get(),
286 delegated_
= CreateDelegatedLayer(frame_provider_
.get());
289 scoped_refptr
<DelegatedRendererLayer
> CreateDelegatedLayer(
290 DelegatedFrameProvider
* frame_provider
) {
291 scoped_refptr
<DelegatedRendererLayer
> delegated
=
292 FakeDelegatedRendererLayer::Create(frame_provider
);
293 delegated
->SetBounds(gfx::Size(10, 10));
294 delegated
->SetIsDrawable(true);
296 root_
->AddChild(delegated
);
300 void AfterTest() override
{ resource_collection_
->SetClient(NULL
); }
302 // DelegatedFrameProviderClient implementation.
303 void UnusedResourcesAreAvailable() override
{ available_
= true; }
305 bool TestAndResetAvailable() {
306 bool available
= available_
;
312 scoped_refptr
<DelegatedFrameResourceCollection
> resource_collection_
;
313 scoped_refptr
<DelegatedFrameProvider
> frame_provider_
;
314 scoped_refptr
<Layer
> root_
;
315 scoped_refptr
<DelegatedRendererLayer
> delegated_
;
319 class LayerTreeHostDelegatedTestCreateChildId
320 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
322 LayerTreeHostDelegatedTestCreateChildId()
323 : LayerTreeHostDelegatedTestCaseSingleDelegatedLayer(),
325 did_reset_child_id_(false) {}
327 void DidCommit() override
{
330 SetFrameData(CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1)));
333 void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) override
{
334 if (host_impl
->active_tree()->source_frame_number() < 1)
337 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
338 FakeDelegatedRendererLayerImpl
* delegated_impl
=
339 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
341 TestContextProvider
* context_provider
= static_cast<TestContextProvider
*>(
342 host_impl
->output_surface()->context_provider());
345 switch (num_activates_
) {
347 EXPECT_TRUE(delegated_impl
->ChildId());
348 EXPECT_FALSE(did_reset_child_id_
);
350 context_provider
->ContextGL()->LoseContextCHROMIUM(
351 GL_GUILTY_CONTEXT_RESET_ARB
,
352 GL_INNOCENT_CONTEXT_RESET_ARB
);
353 context_provider
->ContextGL()->Flush();
356 EXPECT_TRUE(delegated_impl
->ChildId());
357 EXPECT_TRUE(did_reset_child_id_
);
363 void InitializedRendererOnThread(LayerTreeHostImpl
* host_impl
,
364 bool success
) override
{
365 EXPECT_TRUE(success
);
367 if (num_activates_
< 2)
370 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
371 FakeDelegatedRendererLayerImpl
* delegated_impl
=
372 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
374 EXPECT_EQ(2, num_activates_
);
375 EXPECT_FALSE(delegated_impl
->ChildId());
376 did_reset_child_id_
= true;
381 bool did_reset_child_id_
;
384 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestCreateChildId
);
386 // Test that we can gracefully handle invalid frames after the context was lost.
387 // For example, we might be trying to use the previous frame in that case and
388 // have to make sure we don't crash because our resource accounting goes wrong.
389 class LayerTreeHostDelegatedTestInvalidFrameAfterContextLost
390 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
392 LayerTreeHostDelegatedTestInvalidFrameAfterContextLost()
393 : num_activates_(0), num_output_surfaces_initialized_(0) {}
395 void DidCommit() override
{
398 scoped_ptr
<DelegatedFrameData
> frame1
=
399 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
400 AddTextureQuad(frame1
.get(), 999);
401 AddTransferableResource(frame1
.get(), 999);
402 SetFrameData(frame1
.Pass());
405 void DidInitializeOutputSurface() override
{
406 if (!num_output_surfaces_initialized_
++)
409 scoped_refptr
<DelegatedRendererLayer
> old_delegated
= delegated_
;
411 CreateInvalidFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1)));
412 // Make sure we end up using the same layer, or we won't test the right
413 // thing, which is to make sure we can handle an invalid frame when using
414 // a stale layer from before the context was lost.
415 DCHECK(delegated_
.get() == old_delegated
.get());
418 void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) override
{
419 if (host_impl
->active_tree()->source_frame_number() < 1)
422 TestContextProvider
* context_provider
= static_cast<TestContextProvider
*>(
423 host_impl
->output_surface()->context_provider());
426 switch (num_activates_
) {
428 context_provider
->ContextGL()->LoseContextCHROMIUM(
429 GL_GUILTY_CONTEXT_RESET_ARB
,
430 GL_INNOCENT_CONTEXT_RESET_ARB
);
438 void InitializedRendererOnThread(LayerTreeHostImpl
* host_impl
,
439 bool success
) override
{
440 EXPECT_TRUE(success
);
442 if (num_activates_
< 2)
445 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
446 FakeDelegatedRendererLayerImpl
* delegated_impl
=
447 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
449 EXPECT_EQ(2, num_activates_
);
450 // Resources should have gotten cleared after the context was lost.
451 EXPECT_EQ(0U, delegated_impl
->Resources().size());
454 void AfterTest() override
{
455 LayerTreeHostDelegatedTestCaseSingleDelegatedLayer::AfterTest();
456 EXPECT_EQ(2, num_output_surfaces_initialized_
);
461 int num_output_surfaces_initialized_
;
464 SINGLE_AND_MULTI_THREAD_TEST_F(
465 LayerTreeHostDelegatedTestInvalidFrameAfterContextLost
);
467 class LayerTreeHostDelegatedTestLayerUsesFrameDamage
468 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
470 LayerTreeHostDelegatedTestLayerUsesFrameDamage()
471 : LayerTreeHostDelegatedTestCaseSingleDelegatedLayer(),
472 first_draw_for_source_frame_(true) {}
474 void DidCommit() override
{
475 int next_source_frame_number
= layer_tree_host()->source_frame_number();
476 switch (next_source_frame_number
) {
478 // The first time the layer gets a frame the whole layer should be
481 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1)));
484 // A different frame size will damage the whole layer.
486 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(0, 0, 0, 0)));
489 // Should create a total amount of gfx::Rect(2, 2, 8, 6) damage:
490 // (2, 2, 10, 6) clamped to the root output rect.
492 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(2, 2, 5, 5)));
494 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(7, 2, 5, 6)));
497 // Should create zero damage.
498 layer_tree_host()->SetNeedsCommit();
501 // Should damage the full viewport.
502 delegated_
->SetBounds(gfx::Size(2, 2));
505 // Should create zero damage.
506 layer_tree_host()->SetNeedsCommit();
509 // Should damage the full layer, tho the frame size is not changing.
510 delegated_
->SetBounds(gfx::Size(6, 6));
512 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(1, 1, 2, 2)));
515 // Should create zero damage.
516 layer_tree_host()->SetNeedsCommit();
519 // Should create zero damage.
520 layer_tree_host()->SetNeedsCommit();
523 // Changing the frame size damages the full layer.
525 CreateFrameData(gfx::Rect(0, 0, 5, 5), gfx::Rect(4, 4, 1, 1)));
528 // An invalid frame isn't used, so it should not cause damage.
529 SetFrameData(CreateInvalidFrameData(gfx::Rect(0, 0, 5, 5),
530 gfx::Rect(4, 4, 1, 1)));
533 // Should create gfx::Rect(1, 1, 2, 2) of damage.
535 CreateFrameData(gfx::Rect(0, 0, 5, 5), gfx::Rect(1, 1, 2, 2)));
538 // Should create zero damage.
539 layer_tree_host()->SetNeedsCommit();
542 // Moving the layer out of the tree and back in will damage the whole
544 delegated_
->RemoveFromParent();
545 layer_tree_host()->root_layer()->AddChild(delegated_
);
548 // Make a larger frame with lots of damage. Then a frame smaller than
549 // the first frame's damage. The entire layer should be damaged, but
552 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(0, 0, 10, 10)));
554 CreateFrameData(gfx::Rect(0, 0, 5, 5), gfx::Rect(1, 1, 2, 2)));
557 // Make a frame with lots of damage. Then replace it with a frame with
558 // no damage. The entire layer should be damaged, but nothing more.
560 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(0, 0, 10, 10)));
562 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(0, 0, 0, 0)));
565 // Make another layer that uses the same frame provider. The new layer
566 // should be damaged.
567 delegated_copy_
= CreateDelegatedLayer(frame_provider_
.get());
568 delegated_copy_
->SetPosition(gfx::Point(5, 0));
570 // Also set a new frame.
572 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(4, 0, 1, 1)));
575 // Set another new frame, both layers should be damaged in the same
578 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(3, 3, 1, 1)));
581 first_draw_for_source_frame_
= true;
584 DrawResult
PrepareToDrawOnThread(LayerTreeHostImpl
* host_impl
,
585 LayerTreeHostImpl::FrameData
* frame
,
586 DrawResult draw_result
) override
{
587 EXPECT_EQ(DRAW_SUCCESS
, draw_result
);
589 if (!first_draw_for_source_frame_
)
592 gfx::Rect damage_rect
;
593 if (!frame
->has_no_damage
) {
594 damage_rect
= frame
->render_passes
.back()->damage_rect
;
596 // If there is no damage, then we have no render passes to send.
597 EXPECT_TRUE(frame
->render_passes
.empty());
600 switch (host_impl
->active_tree()->source_frame_number()) {
602 // First frame is damaged because of viewport resize.
603 EXPECT_EQ(gfx::Rect(15, 15).ToString(), damage_rect
.ToString());
606 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect
.ToString());
609 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect
.ToString());
612 EXPECT_EQ(gfx::Rect(2, 2, 8, 6).ToString(), damage_rect
.ToString());
615 EXPECT_EQ(gfx::Rect().ToString(), damage_rect
.ToString());
618 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect
.ToString());
621 EXPECT_EQ(gfx::Rect().ToString(), damage_rect
.ToString());
624 EXPECT_EQ(gfx::Rect(6, 6).ToString(), damage_rect
.ToString());
627 EXPECT_EQ(gfx::Rect().ToString(), damage_rect
.ToString());
630 EXPECT_EQ(gfx::Rect().ToString(), damage_rect
.ToString());
633 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect
.ToString());
636 EXPECT_EQ(gfx::Rect().ToString(), damage_rect
.ToString());
639 EXPECT_EQ(gfx::Rect(1, 1, 2, 2).ToString(), damage_rect
.ToString());
642 EXPECT_EQ(gfx::Rect().ToString(), damage_rect
.ToString());
645 EXPECT_EQ(gfx::Rect(10, 10).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::UnionRects(gfx::Rect(5, 0, 10, 10),
655 gfx::Rect(4, 0, 1, 1)).ToString(),
656 damage_rect
.ToString());
659 EXPECT_EQ(gfx::Rect(3, 3, 6, 1).ToString(), damage_rect
.ToString());
668 scoped_refptr
<DelegatedRendererLayer
> delegated_copy_
;
669 bool first_draw_for_source_frame_
;
672 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestLayerUsesFrameDamage
);
674 class LayerTreeHostDelegatedTestMergeResources
675 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
677 void BeginTest() override
{
678 // Push two frames to the delegated renderer layer with no commit between.
680 // The first frame has resource 999.
681 scoped_ptr
<DelegatedFrameData
> frame1
=
682 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
683 AddTextureQuad(frame1
.get(), 999);
684 AddTransferableResource(frame1
.get(), 999);
685 SetFrameData(frame1
.Pass());
687 // The second frame uses resource 999 still, but also adds 555.
688 scoped_ptr
<DelegatedFrameData
> frame2
=
689 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
690 AddTextureQuad(frame2
.get(), 999);
691 AddTransferableResource(frame2
.get(), 999);
692 AddTextureQuad(frame2
.get(), 555);
693 AddTransferableResource(frame2
.get(), 555);
694 SetFrameData(frame2
.Pass());
696 // The resource 999 from frame1 is returned since it is still on the main
698 ReturnedResourceArray returned_resources
;
699 resource_collection_
->TakeUnusedResourcesForChildCompositor(
700 &returned_resources
);
702 unsigned expected
[] = {999};
703 EXPECT_RESOURCES(expected
, returned_resources
);
704 EXPECT_TRUE(TestAndResetAvailable());
707 PostSetNeedsCommitToMainThread();
710 void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) override
{
711 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
712 FakeDelegatedRendererLayerImpl
* delegated_impl
=
713 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
715 const ResourceProvider::ResourceIdMap
& map
=
716 host_impl
->resource_provider()->GetChildToParentMap(
717 delegated_impl
->ChildId());
719 // Both frames' resources should be in the parent's resource provider.
720 EXPECT_EQ(2u, map
.size());
721 EXPECT_EQ(1u, map
.count(999));
722 EXPECT_EQ(1u, map
.count(555));
724 EXPECT_EQ(2u, delegated_impl
->Resources().size());
725 EXPECT_EQ(1u, delegated_impl
->Resources().count(999));
726 EXPECT_EQ(1u, delegated_impl
->Resources().count(555));
732 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestMergeResources
);
734 class LayerTreeHostDelegatedTestRemapResourcesInQuads
735 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
737 void BeginTest() override
{
738 // Generate a frame with two resources in it.
739 scoped_ptr
<DelegatedFrameData
> frame
=
740 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
741 AddTextureQuad(frame
.get(), 999);
742 AddTransferableResource(frame
.get(), 999);
743 AddTextureQuad(frame
.get(), 555);
744 AddTransferableResource(frame
.get(), 555);
745 SetFrameData(frame
.Pass());
747 PostSetNeedsCommitToMainThread();
750 void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) override
{
751 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
752 FakeDelegatedRendererLayerImpl
* delegated_impl
=
753 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
755 const ResourceProvider::ResourceIdMap
& map
=
756 host_impl
->resource_provider()->GetChildToParentMap(
757 delegated_impl
->ChildId());
759 // The frame's resource should be in the parent's resource provider.
760 EXPECT_EQ(2u, map
.size());
761 EXPECT_EQ(1u, map
.count(999));
762 EXPECT_EQ(1u, map
.count(555));
764 ResourceProvider::ResourceId parent_resource_id1
= map
.find(999)->second
;
765 EXPECT_NE(parent_resource_id1
, 999u);
766 ResourceProvider::ResourceId parent_resource_id2
= map
.find(555)->second
;
767 EXPECT_NE(parent_resource_id2
, 555u);
769 // The resources in the quads should be remapped to the parent's namespace.
770 const TextureDrawQuad
* quad1
= TextureDrawQuad::MaterialCast(
771 delegated_impl
->RenderPassesInDrawOrder()[0]->quad_list
.ElementAt(0));
772 EXPECT_EQ(parent_resource_id1
, quad1
->resource_id
);
773 const TextureDrawQuad
* quad2
= TextureDrawQuad::MaterialCast(
774 delegated_impl
->RenderPassesInDrawOrder()[0]->quad_list
.ElementAt(1));
775 EXPECT_EQ(parent_resource_id2
, quad2
->resource_id
);
781 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestRemapResourcesInQuads
);
783 class LayerTreeHostDelegatedTestReturnUnusedResources
784 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
786 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
788 void DidCommitAndDrawFrame() override
{
789 scoped_ptr
<DelegatedFrameData
> frame
;
790 ReturnedResourceArray resources
;
792 int next_source_frame_number
= layer_tree_host()->source_frame_number();
793 switch (next_source_frame_number
) {
795 // Generate a frame with two resources in it.
796 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
797 AddTextureQuad(frame
.get(), 999);
798 AddTransferableResource(frame
.get(), 999);
799 AddTextureQuad(frame
.get(), 555);
800 AddTransferableResource(frame
.get(), 555);
801 SetFrameData(frame
.Pass());
804 // All of the resources are in use.
805 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
806 EXPECT_EQ(0u, resources
.size());
807 EXPECT_FALSE(TestAndResetAvailable());
809 // Keep using 999 but stop using 555.
810 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
811 AddTextureQuad(frame
.get(), 999);
812 AddTransferableResource(frame
.get(), 999);
813 AddTextureQuad(frame
.get(), 444);
814 AddTransferableResource(frame
.get(), 444);
815 SetFrameData(frame
.Pass());
818 // 555 is no longer in use.
819 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
821 unsigned expected
[] = {555};
822 EXPECT_RESOURCES(expected
, resources
);
823 EXPECT_TRUE(TestAndResetAvailable());
826 // Stop using any resources.
827 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
828 SetFrameData(frame
.Pass());
831 // Postpone collecting resources for a frame. They should still be there
833 layer_tree_host()->SetNeedsCommit();
836 // 444 and 999 are no longer in use. We sent two refs to 999, so we
837 // should get two back.
838 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
840 unsigned expected
[] = {444, 999, 999};
841 EXPECT_RESOURCES(expected
, resources
);
842 EXPECT_TRUE(TestAndResetAvailable());
848 // Resources are never immediately released.
849 ReturnedResourceArray empty_resources
;
850 resource_collection_
->TakeUnusedResourcesForChildCompositor(
852 EXPECT_EQ(0u, empty_resources
.size());
853 EXPECT_FALSE(TestAndResetAvailable());
856 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
857 ReturnUnusedResourcesFromParent(host_impl
);
861 SINGLE_AND_MULTI_THREAD_TEST_F(
862 LayerTreeHostDelegatedTestReturnUnusedResources
);
864 class LayerTreeHostDelegatedTestReusedResources
865 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
867 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
869 void DidCommitAndDrawFrame() override
{
870 scoped_ptr
<DelegatedFrameData
> frame
;
871 ReturnedResourceArray resources
;
873 int next_source_frame_number
= layer_tree_host()->source_frame_number();
874 switch (next_source_frame_number
) {
876 // Generate a frame with some resources in it.
877 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
878 AddTextureQuad(frame
.get(), 999);
879 AddTransferableResource(frame
.get(), 999);
880 AddTextureQuad(frame
.get(), 555);
881 AddTransferableResource(frame
.get(), 555);
882 AddTextureQuad(frame
.get(), 444);
883 AddTransferableResource(frame
.get(), 444);
884 SetFrameData(frame
.Pass());
887 // All of the resources are in use.
888 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
889 EXPECT_EQ(0u, resources
.size());
890 EXPECT_FALSE(TestAndResetAvailable());
892 // Keep using 999 but stop using 555 and 444.
893 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
894 AddTextureQuad(frame
.get(), 999);
895 AddTransferableResource(frame
.get(), 999);
896 SetFrameData(frame
.Pass());
898 // Resource are not immediately released.
899 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
900 EXPECT_EQ(0u, resources
.size());
901 EXPECT_FALSE(TestAndResetAvailable());
903 // Now using 555 and 444 again, but not 999.
904 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
905 AddTextureQuad(frame
.get(), 555);
906 AddTransferableResource(frame
.get(), 555);
907 AddTextureQuad(frame
.get(), 444);
908 AddTransferableResource(frame
.get(), 444);
909 SetFrameData(frame
.Pass());
912 // The 999 resource is the only unused one. Two references were sent, so
913 // two should be returned.
914 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
916 unsigned expected
[] = {999, 999};
917 EXPECT_RESOURCES(expected
, resources
);
918 EXPECT_TRUE(TestAndResetAvailable());
925 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
926 ReturnUnusedResourcesFromParent(host_impl
);
930 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestReusedResources
);
932 class LayerTreeHostDelegatedTestFrameBeforeAck
933 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
935 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
937 void DidCommitAndDrawFrame() override
{
938 scoped_ptr
<DelegatedFrameData
> frame
;
939 ReturnedResourceArray resources
;
941 int next_source_frame_number
= layer_tree_host()->source_frame_number();
942 switch (next_source_frame_number
) {
944 // Generate a frame with some resources in it.
945 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
946 AddTextureQuad(frame
.get(), 999);
947 AddTransferableResource(frame
.get(), 999);
948 AddTextureQuad(frame
.get(), 555);
949 AddTransferableResource(frame
.get(), 555);
950 AddTextureQuad(frame
.get(), 444);
951 AddTransferableResource(frame
.get(), 444);
952 SetFrameData(frame
.Pass());
955 // All of the resources are in use.
956 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
957 EXPECT_EQ(0u, resources
.size());
958 EXPECT_FALSE(TestAndResetAvailable());
960 // Keep using 999 but stop using 555 and 444.
961 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
962 AddTextureQuad(frame
.get(), 999);
963 AddTransferableResource(frame
.get(), 999);
964 SetFrameData(frame
.Pass());
966 // Resource are not immediately released.
967 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
968 EXPECT_EQ(0u, resources
.size());
969 EXPECT_FALSE(TestAndResetAvailable());
971 // The parent compositor (this one) does a commit.
974 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
976 unsigned expected
[] = {444, 555};
977 EXPECT_RESOURCES(expected
, resources
);
978 EXPECT_TRUE(TestAndResetAvailable());
981 // The child compositor sends a frame referring to resources not in the
983 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
984 AddTextureQuad(frame
.get(), 999);
985 AddTextureQuad(frame
.get(), 555);
986 AddTextureQuad(frame
.get(), 444);
987 SetFrameData(frame
.Pass());
992 void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) override
{
993 if (host_impl
->active_tree()->source_frame_number() != 3)
996 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
997 FakeDelegatedRendererLayerImpl
* delegated_impl
=
998 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
1000 const ResourceProvider::ResourceIdMap
& map
=
1001 host_impl
->resource_provider()->GetChildToParentMap(
1002 delegated_impl
->ChildId());
1004 // The bad frame should be dropped. So we should only have one quad (the
1005 // one with resource 999) on the impl tree. And only 999 will be present
1006 // in the parent's resource provider.
1007 EXPECT_EQ(1u, map
.size());
1008 EXPECT_EQ(1u, map
.count(999));
1010 EXPECT_EQ(1u, delegated_impl
->Resources().size());
1011 EXPECT_EQ(1u, delegated_impl
->Resources().count(999));
1013 const RenderPass
* pass
= delegated_impl
->RenderPassesInDrawOrder()[0];
1014 EXPECT_EQ(1u, pass
->quad_list
.size());
1015 const TextureDrawQuad
* quad
=
1016 TextureDrawQuad::MaterialCast(pass
->quad_list
.front());
1017 EXPECT_EQ(map
.find(999)->second
, quad
->resource_id
);
1022 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
1023 ReturnUnusedResourcesFromParent(host_impl
);
1027 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestFrameBeforeAck
);
1029 class LayerTreeHostDelegatedTestFrameBeforeTakeResources
1030 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
1032 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
1034 void DidCommitAndDrawFrame() override
{
1035 scoped_ptr
<DelegatedFrameData
> frame
;
1036 ReturnedResourceArray resources
;
1038 int next_source_frame_number
= layer_tree_host()->source_frame_number();
1039 switch (next_source_frame_number
) {
1041 // Generate a frame with some resources in it.
1042 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1043 AddTextureQuad(frame
.get(), 999);
1044 AddTransferableResource(frame
.get(), 999);
1045 AddTextureQuad(frame
.get(), 555);
1046 AddTransferableResource(frame
.get(), 555);
1047 AddTextureQuad(frame
.get(), 444);
1048 AddTransferableResource(frame
.get(), 444);
1049 SetFrameData(frame
.Pass());
1052 // All of the resources are in use.
1053 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1054 EXPECT_EQ(0u, resources
.size());
1055 EXPECT_FALSE(TestAndResetAvailable());
1057 // Keep using 999 but stop using 555 and 444.
1058 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1059 AddTextureQuad(frame
.get(), 999);
1060 AddTransferableResource(frame
.get(), 999);
1061 SetFrameData(frame
.Pass());
1063 // Resource are not immediately released.
1064 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1065 EXPECT_EQ(0u, resources
.size());
1066 EXPECT_FALSE(TestAndResetAvailable());
1068 // The parent compositor (this one) does a commit.
1071 // The child compositor sends a frame before taking resources back
1072 // from the previous commit. This frame makes use of the resources 555
1073 // and 444, which were just released during commit.
1074 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1075 AddTextureQuad(frame
.get(), 999);
1076 AddTransferableResource(frame
.get(), 999);
1077 AddTextureQuad(frame
.get(), 555);
1078 AddTransferableResource(frame
.get(), 555);
1079 AddTextureQuad(frame
.get(), 444);
1080 AddTransferableResource(frame
.get(), 444);
1081 SetFrameData(frame
.Pass());
1083 // The resources are used by the new frame but are returned anyway since
1084 // we passed them again.
1085 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1087 unsigned expected
[] = {444, 555};
1088 EXPECT_RESOURCES(expected
, resources
);
1089 EXPECT_TRUE(TestAndResetAvailable());
1093 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1094 EXPECT_EQ(0u, resources
.size());
1095 EXPECT_FALSE(TestAndResetAvailable());
1101 void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) override
{
1102 if (host_impl
->active_tree()->source_frame_number() != 3)
1105 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
1106 FakeDelegatedRendererLayerImpl
* delegated_impl
=
1107 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
1109 const ResourceProvider::ResourceIdMap
& map
=
1110 host_impl
->resource_provider()->GetChildToParentMap(
1111 delegated_impl
->ChildId());
1113 // The third frame has all of the resources in it again, the delegated
1114 // renderer layer should continue to own the resources for it.
1115 EXPECT_EQ(3u, map
.size());
1116 EXPECT_EQ(1u, map
.count(999));
1117 EXPECT_EQ(1u, map
.count(555));
1118 EXPECT_EQ(1u, map
.count(444));
1120 EXPECT_EQ(3u, delegated_impl
->Resources().size());
1121 EXPECT_EQ(1u, delegated_impl
->Resources().count(999));
1122 EXPECT_EQ(1u, delegated_impl
->Resources().count(555));
1123 EXPECT_EQ(1u, delegated_impl
->Resources().count(444));
1125 const RenderPass
* pass
= delegated_impl
->RenderPassesInDrawOrder()[0];
1126 EXPECT_EQ(3u, pass
->quad_list
.size());
1127 const TextureDrawQuad
* quad1
=
1128 TextureDrawQuad::MaterialCast(pass
->quad_list
.ElementAt(0));
1129 EXPECT_EQ(map
.find(999)->second
, quad1
->resource_id
);
1130 const TextureDrawQuad
* quad2
=
1131 TextureDrawQuad::MaterialCast(pass
->quad_list
.ElementAt(1));
1132 EXPECT_EQ(map
.find(555)->second
, quad2
->resource_id
);
1133 const TextureDrawQuad
* quad3
=
1134 TextureDrawQuad::MaterialCast(pass
->quad_list
.ElementAt(2));
1135 EXPECT_EQ(map
.find(444)->second
, quad3
->resource_id
);
1138 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
1139 ReturnUnusedResourcesFromParent(host_impl
);
1143 SINGLE_AND_MULTI_THREAD_TEST_F(
1144 LayerTreeHostDelegatedTestFrameBeforeTakeResources
);
1146 class LayerTreeHostDelegatedTestBadFrame
1147 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
1149 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
1151 void DidCommitAndDrawFrame() override
{
1152 scoped_ptr
<DelegatedFrameData
> frame
;
1153 ReturnedResourceArray resources
;
1155 int next_source_frame_number
= layer_tree_host()->source_frame_number();
1156 switch (next_source_frame_number
) {
1158 // Generate a frame with some resources in it.
1159 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1160 AddTextureQuad(frame
.get(), 999);
1161 AddTransferableResource(frame
.get(), 999);
1162 AddTextureQuad(frame
.get(), 555);
1163 AddTransferableResource(frame
.get(), 555);
1164 SetFrameData(frame
.Pass());
1167 // All of the resources are in use.
1168 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1169 EXPECT_EQ(0u, resources
.size());
1170 EXPECT_FALSE(TestAndResetAvailable());
1172 // Generate a bad frame with a resource the layer doesn't have. The
1173 // 885 and 775 resources are unknown, while ownership of the legit 444
1174 // resource is passed in here. The bad frame does not use any of the
1175 // previous resources, 999 or 555.
1176 // A bad quad is present both before and after the good quad.
1177 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1178 AddTextureQuad(frame
.get(), 885);
1179 AddTextureQuad(frame
.get(), 444);
1180 AddTransferableResource(frame
.get(), 444);
1181 AddTextureQuad(frame
.get(), 775);
1182 SetFrameData(frame
.Pass());
1184 // The parent compositor (this one) does a commit.
1187 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1188 EXPECT_EQ(0u, resources
.size());
1189 EXPECT_FALSE(TestAndResetAvailable());
1191 // Now send a good frame with 999 again.
1192 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1193 AddTextureQuad(frame
.get(), 999);
1194 SetFrameData(frame
.Pass());
1196 // The bad frame's resource is given back to the child compositor.
1197 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1199 unsigned expected
[] = {444};
1200 EXPECT_RESOURCES(expected
, resources
);
1201 EXPECT_TRUE(TestAndResetAvailable());
1205 // The unused 555 from the last good frame is now released.
1206 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1208 unsigned expected
[] = {555};
1209 EXPECT_RESOURCES(expected
, resources
);
1210 EXPECT_TRUE(TestAndResetAvailable());
1218 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
1219 if (host_impl
->active_tree()->source_frame_number() < 1)
1222 ReturnUnusedResourcesFromParent(host_impl
);
1224 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
1225 FakeDelegatedRendererLayerImpl
* delegated_impl
=
1226 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
1228 const ResourceProvider::ResourceIdMap
& map
=
1229 host_impl
->resource_provider()->GetChildToParentMap(
1230 delegated_impl
->ChildId());
1232 switch (host_impl
->active_tree()->source_frame_number()) {
1234 // We have the first good frame with just 990 and 555 in it.
1236 EXPECT_EQ(2u, map
.size());
1237 EXPECT_EQ(1u, map
.count(999));
1238 EXPECT_EQ(1u, map
.count(555));
1240 EXPECT_EQ(2u, delegated_impl
->Resources().size());
1241 EXPECT_EQ(1u, delegated_impl
->Resources().count(999));
1242 EXPECT_EQ(1u, delegated_impl
->Resources().count(555));
1244 const RenderPass
* pass
= delegated_impl
->RenderPassesInDrawOrder()[0];
1245 EXPECT_EQ(2u, pass
->quad_list
.size());
1246 const TextureDrawQuad
* quad1
=
1247 TextureDrawQuad::MaterialCast(pass
->quad_list
.ElementAt(0));
1248 EXPECT_EQ(map
.find(999)->second
, quad1
->resource_id
);
1249 const TextureDrawQuad
* quad2
=
1250 TextureDrawQuad::MaterialCast(pass
->quad_list
.ElementAt(1));
1251 EXPECT_EQ(map
.find(555)->second
, quad2
->resource_id
);
1255 // We only keep resources from the last valid frame.
1256 EXPECT_EQ(2u, map
.size());
1257 EXPECT_EQ(1u, map
.count(999));
1258 EXPECT_EQ(1u, map
.count(555));
1260 EXPECT_EQ(2u, delegated_impl
->Resources().size());
1261 EXPECT_EQ(1u, delegated_impl
->Resources().count(999));
1262 EXPECT_EQ(1u, delegated_impl
->Resources().count(555));
1264 // The bad frame is dropped though, we still have the frame with 999 and
1266 const RenderPass
* pass
= delegated_impl
->RenderPassesInDrawOrder()[0];
1267 EXPECT_EQ(2u, pass
->quad_list
.size());
1268 const TextureDrawQuad
* quad1
=
1269 TextureDrawQuad::MaterialCast(pass
->quad_list
.ElementAt(0));
1270 EXPECT_EQ(map
.find(999)->second
, quad1
->resource_id
);
1271 const TextureDrawQuad
* quad2
=
1272 TextureDrawQuad::MaterialCast(pass
->quad_list
.ElementAt(1));
1273 EXPECT_EQ(map
.find(555)->second
, quad2
->resource_id
);
1277 // We have the new good frame with just 999 in it.
1278 EXPECT_EQ(1u, map
.size());
1279 EXPECT_EQ(1u, map
.count(999));
1281 EXPECT_EQ(1u, delegated_impl
->Resources().size());
1282 EXPECT_EQ(1u, delegated_impl
->Resources().count(999));
1284 const RenderPass
* pass
= delegated_impl
->RenderPassesInDrawOrder()[0];
1285 EXPECT_EQ(1u, pass
->quad_list
.size());
1286 const TextureDrawQuad
* quad1
=
1287 TextureDrawQuad::MaterialCast(pass
->quad_list
.front());
1288 EXPECT_EQ(map
.find(999)->second
, quad1
->resource_id
);
1295 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestBadFrame
);
1297 class LayerTreeHostDelegatedTestUnnamedResource
1298 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
1300 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
1302 void DidCommit() override
{
1303 scoped_ptr
<DelegatedFrameData
> frame
;
1304 ReturnedResourceArray resources
;
1306 int next_source_frame_number
= layer_tree_host()->source_frame_number();
1307 switch (next_source_frame_number
) {
1309 // This frame includes two resources in it, but only uses one.
1310 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1311 AddTransferableResource(frame
.get(), 999);
1312 AddTextureQuad(frame
.get(), 555);
1313 AddTransferableResource(frame
.get(), 555);
1314 SetFrameData(frame
.Pass());
1317 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1318 EXPECT_EQ(0u, resources
.size());
1319 EXPECT_FALSE(TestAndResetAvailable());
1321 // Now send an empty frame.
1322 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1323 SetFrameData(frame
.Pass());
1325 // The unused resource should be returned.
1326 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1328 unsigned expected
[] = {999};
1329 EXPECT_RESOURCES(expected
, resources
);
1330 EXPECT_TRUE(TestAndResetAvailable());
1338 void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) override
{
1339 if (host_impl
->active_tree()->source_frame_number() != 1)
1342 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
1343 FakeDelegatedRendererLayerImpl
* delegated_impl
=
1344 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
1346 const ResourceProvider::ResourceIdMap
& map
=
1347 host_impl
->resource_provider()->GetChildToParentMap(
1348 delegated_impl
->ChildId());
1350 // The layer only held on to the resource that was used.
1351 EXPECT_EQ(1u, map
.size());
1352 EXPECT_EQ(1u, map
.count(555));
1354 EXPECT_EQ(1u, delegated_impl
->Resources().size());
1355 EXPECT_EQ(1u, delegated_impl
->Resources().count(555));
1359 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestUnnamedResource
);
1361 class LayerTreeHostDelegatedTestDontLeakResource
1362 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
1364 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
1366 void DidCommitAndDrawFrame() override
{
1367 scoped_ptr
<DelegatedFrameData
> frame
;
1368 ReturnedResourceArray resources
;
1370 int next_source_frame_number
= layer_tree_host()->source_frame_number();
1371 switch (next_source_frame_number
) {
1373 // This frame includes two resources in it.
1374 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1375 AddTextureQuad(frame
.get(), 999);
1376 AddTransferableResource(frame
.get(), 999);
1377 AddTextureQuad(frame
.get(), 555);
1378 AddTransferableResource(frame
.get(), 555);
1379 SetFrameData(frame
.Pass());
1381 // But then we immediately stop using 999.
1382 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1383 AddTextureQuad(frame
.get(), 555);
1384 AddTransferableResource(frame
.get(), 555);
1385 SetFrameData(frame
.Pass());
1388 // The unused resources should be returned. 555 is still used, but it's
1389 // returned once to account for the first frame.
1390 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1392 unsigned expected
[] = {555, 999};
1393 EXPECT_RESOURCES(expected
, resources
);
1394 EXPECT_TRUE(TestAndResetAvailable());
1396 // Send a frame with no resources in it.
1397 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1398 SetFrameData(frame
.Pass());
1401 // The now unused resource 555 should be returned.
1403 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1405 unsigned expected
[] = {555};
1406 EXPECT_RESOURCES(expected
, resources
);
1407 EXPECT_TRUE(TestAndResetAvailable());
1414 void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) override
{
1415 if (host_impl
->active_tree()->source_frame_number() != 1)
1418 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
1419 FakeDelegatedRendererLayerImpl
* delegated_impl
=
1420 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
1422 const ResourceProvider::ResourceIdMap
& map
=
1423 host_impl
->resource_provider()->GetChildToParentMap(
1424 delegated_impl
->ChildId());
1426 // The layer only held on to the resource that was used.
1427 EXPECT_EQ(1u, map
.size());
1428 EXPECT_EQ(1u, map
.count(555));
1430 EXPECT_EQ(1u, delegated_impl
->Resources().size());
1431 EXPECT_EQ(1u, delegated_impl
->Resources().count(555));
1434 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
1435 ReturnUnusedResourcesFromParent(host_impl
);
1439 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestDontLeakResource
);
1441 class LayerTreeHostDelegatedTestResourceSentToParent
1442 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
1444 void DidCommitAndDrawFrame() override
{
1445 scoped_ptr
<DelegatedFrameData
> frame
;
1446 ReturnedResourceArray resources
;
1448 int next_source_frame_number
= layer_tree_host()->source_frame_number();
1449 switch (next_source_frame_number
) {
1451 // This frame includes two resources in it.
1452 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1453 AddTextureQuad(frame
.get(), 999);
1454 AddTransferableResource(frame
.get(), 999);
1455 AddTextureQuad(frame
.get(), 555);
1456 AddTransferableResource(frame
.get(), 555);
1457 SetFrameData(frame
.Pass());
1460 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1461 EXPECT_EQ(0u, resources
.size());
1462 EXPECT_FALSE(TestAndResetAvailable());
1464 // 999 is in use in the grandparent compositor, generate a frame without
1466 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1467 AddTextureQuad(frame
.get(), 555);
1468 AddTransferableResource(frame
.get(), 555);
1469 SetFrameData(frame
.Pass());
1472 // Since 999 is in the grandparent it is not returned.
1473 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1474 EXPECT_EQ(0u, resources
.size());
1475 EXPECT_FALSE(TestAndResetAvailable());
1477 // The impl side will get back the resource at some point.
1478 ImplThreadTaskRunner()->PostTask(FROM_HERE
,
1479 receive_resource_on_thread_
);
1484 void ReceiveResourceOnThread(LayerTreeHostImpl
* host_impl
) {
1485 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
1486 FakeDelegatedRendererLayerImpl
* delegated_impl
=
1487 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
1489 const ResourceProvider::ResourceIdMap
& map
=
1490 host_impl
->resource_provider()->GetChildToParentMap(
1491 delegated_impl
->ChildId());
1493 // Receive 999 back from the grandparent.
1494 CompositorFrameAck ack
;
1495 output_surface()->ReturnResource(map
.find(999)->second
, &ack
);
1496 host_impl
->ReclaimResources(&ack
);
1499 void UnusedResourcesAreAvailable() override
{
1500 EXPECT_EQ(3, layer_tree_host()->source_frame_number());
1502 ReturnedResourceArray resources
;
1504 // 999 was returned from the grandparent and could be released.
1505 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1507 unsigned expected
[] = {999};
1508 EXPECT_RESOURCES(expected
, resources
);
1514 void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) override
{
1515 if (host_impl
->active_tree()->source_frame_number() < 1)
1518 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
1519 FakeDelegatedRendererLayerImpl
* delegated_impl
=
1520 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
1522 const ResourceProvider::ResourceIdMap
& map
=
1523 host_impl
->resource_provider()->GetChildToParentMap(
1524 delegated_impl
->ChildId());
1526 switch (host_impl
->active_tree()->source_frame_number()) {
1528 EXPECT_EQ(2u, map
.size());
1529 EXPECT_EQ(1u, map
.count(999));
1530 EXPECT_EQ(1u, map
.count(555));
1532 EXPECT_EQ(2u, delegated_impl
->Resources().size());
1533 EXPECT_EQ(1u, delegated_impl
->Resources().count(999));
1534 EXPECT_EQ(1u, delegated_impl
->Resources().count(555));
1536 // The 999 resource will be sent to a grandparent compositor.
1540 EXPECT_EQ(2u, map
.size());
1541 EXPECT_EQ(1u, map
.count(999));
1542 EXPECT_EQ(1u, map
.count(555));
1544 // 999 is in the parent, so not held by delegated renderer layer.
1545 EXPECT_EQ(1u, delegated_impl
->Resources().size());
1546 EXPECT_EQ(1u, delegated_impl
->Resources().count(555));
1548 receive_resource_on_thread_
=
1549 base::Bind(&LayerTreeHostDelegatedTestResourceSentToParent::
1550 ReceiveResourceOnThread
,
1551 base::Unretained(this),
1556 // 999 should be released.
1557 EXPECT_EQ(1u, map
.size());
1558 EXPECT_EQ(1u, map
.count(555));
1560 EXPECT_EQ(1u, delegated_impl
->Resources().size());
1561 EXPECT_EQ(1u, delegated_impl
->Resources().count(map
.find(555)->second
));
1566 base::Closure receive_resource_on_thread_
;
1569 SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(
1570 LayerTreeHostDelegatedTestResourceSentToParent
);
1572 class LayerTreeHostDelegatedTestCommitWithoutTake
1573 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
1575 void BeginTest() override
{
1576 // Prevent drawing with resources that are sent to the grandparent.
1577 layer_tree_host()->SetViewportSize(gfx::Size());
1578 PostSetNeedsCommitToMainThread();
1581 void DidCommit() override
{
1582 scoped_ptr
<DelegatedFrameData
> frame
;
1583 ReturnedResourceArray resources
;
1585 int next_source_frame_number
= layer_tree_host()->source_frame_number();
1586 switch (next_source_frame_number
) {
1588 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1589 AddTextureQuad(frame
.get(), 999);
1590 AddTransferableResource(frame
.get(), 999);
1591 AddTextureQuad(frame
.get(), 555);
1592 AddTransferableResource(frame
.get(), 555);
1593 AddTextureQuad(frame
.get(), 444);
1594 AddTransferableResource(frame
.get(), 444);
1595 SetFrameData(frame
.Pass());
1598 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1599 EXPECT_EQ(0u, resources
.size());
1600 EXPECT_FALSE(TestAndResetAvailable());
1602 // Stop using 999 and 444 in this frame and commit.
1603 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1604 AddTextureQuad(frame
.get(), 555);
1605 AddTransferableResource(frame
.get(), 555);
1606 SetFrameData(frame
.Pass());
1607 // 999 and 444 will be returned for frame 1, but not 555 since it's in
1608 // the current frame.
1611 // Don't take resources here, but set a new frame that uses 999 again.
1612 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1613 AddTextureQuad(frame
.get(), 999);
1614 AddTransferableResource(frame
.get(), 999);
1615 AddTextureQuad(frame
.get(), 555);
1616 AddTransferableResource(frame
.get(), 555);
1617 SetFrameData(frame
.Pass());
1620 // 555 from frame 1 and 2 isn't returned since it's still in use. 999
1621 // from frame 1 is returned though.
1622 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1624 unsigned expected
[] = {444, 999};
1625 EXPECT_RESOURCES(expected
, resources
);
1626 EXPECT_TRUE(TestAndResetAvailable());
1629 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1630 SetFrameData(frame
.Pass());
1631 // 555 will be returned 3 times for frames 1 2 and 3, and 999 will be
1632 // returned once for frame 3.
1635 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1637 unsigned expected
[] = {555, 555, 555, 999};
1638 EXPECT_RESOURCES(expected
, resources
);
1639 EXPECT_TRUE(TestAndResetAvailable());
1647 void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) override
{
1648 if (host_impl
->active_tree()->source_frame_number() < 1)
1651 LayerImpl
* root_impl
= host_impl
->active_tree()->root_layer();
1652 FakeDelegatedRendererLayerImpl
* delegated_impl
=
1653 static_cast<FakeDelegatedRendererLayerImpl
*>(root_impl
->children()[0]);
1655 const ResourceProvider::ResourceIdMap
& map
=
1656 host_impl
->resource_provider()->GetChildToParentMap(
1657 delegated_impl
->ChildId());
1659 switch (host_impl
->active_tree()->source_frame_number()) {
1661 EXPECT_EQ(3u, map
.size());
1662 EXPECT_EQ(1u, map
.count(999));
1663 EXPECT_EQ(1u, map
.count(555));
1664 EXPECT_EQ(1u, map
.count(444));
1666 EXPECT_EQ(3u, delegated_impl
->Resources().size());
1667 EXPECT_EQ(1u, delegated_impl
->Resources().count(999));
1668 EXPECT_EQ(1u, delegated_impl
->Resources().count(555));
1669 EXPECT_EQ(1u, delegated_impl
->Resources().count(444));
1672 EXPECT_EQ(1u, map
.size());
1673 EXPECT_EQ(1u, map
.count(555));
1675 EXPECT_EQ(1u, delegated_impl
->Resources().size());
1676 EXPECT_EQ(1u, delegated_impl
->Resources().count(555));
1679 EXPECT_EQ(2u, map
.size());
1680 EXPECT_EQ(1u, map
.count(999));
1681 EXPECT_EQ(1u, map
.count(555));
1683 EXPECT_EQ(2u, delegated_impl
->Resources().size());
1684 EXPECT_EQ(1u, delegated_impl
->Resources().count(999));
1685 EXPECT_EQ(1u, delegated_impl
->Resources().count(555));
1690 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestCommitWithoutTake
);
1692 class DelegatedFrameIsActivatedDuringCommit
1693 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
1695 DelegatedFrameIsActivatedDuringCommit() : returned_resource_count_(0) {}
1697 void BeginTest() override
{
1698 activate_count_
= 0;
1700 scoped_ptr
<DelegatedFrameData
> frame
=
1701 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1702 AddTextureQuad(frame
.get(), 999);
1703 AddTransferableResource(frame
.get(), 999);
1704 SetFrameData(frame
.Pass());
1706 PostSetNeedsCommitToMainThread();
1709 void WillActivateTreeOnThread(LayerTreeHostImpl
* impl
) override
{
1713 void DidCommit() override
{
1714 switch (layer_tree_host()->source_frame_number()) {
1716 // The first frame has been activated. Set a new frame, and
1717 // expect the next commit to finish *after* it is activated.
1718 scoped_ptr
<DelegatedFrameData
> frame
=
1719 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1720 AddTextureQuad(frame
.get(), 555);
1721 AddTransferableResource(frame
.get(), 555);
1722 SetFrameData(frame
.Pass());
1726 // The second frame has been activated. Remove the layer from
1727 // the tree to cause another commit/activation. The commit should
1728 // finish *after* the layer is removed from the active tree.
1729 delegated_
->RemoveFromParent();
1732 // Finish the test by releasing resources on the next frame.
1733 scoped_ptr
<DelegatedFrameData
> frame
=
1734 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1735 SetFrameData(frame
.Pass());
1740 void CommitCompleteOnThread(LayerTreeHostImpl
* host_impl
) override
{
1741 switch (host_impl
->active_tree()->source_frame_number()) {
1743 // The activate for the 1st frame should have happened before now.
1744 EXPECT_EQ(1, activate_count_
);
1748 // The activate for the 2nd frame should have happened before now.
1749 EXPECT_EQ(2, activate_count_
);
1753 // The activate to remove the layer should have happened before now.
1754 EXPECT_EQ(3, activate_count_
);
1764 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
1765 ReturnUnusedResourcesFromParent(host_impl
);
1768 void UnusedResourcesAreAvailable() override
{
1769 LayerTreeHostDelegatedTestCaseSingleDelegatedLayer::
1770 UnusedResourcesAreAvailable();
1771 ReturnedResourceArray resources
;
1772 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1773 EXPECT_TRUE(TestAndResetAvailable());
1774 returned_resource_count_
+= resources
.size();
1775 if (returned_resource_count_
== 2)
1779 int activate_count_
;
1780 size_t returned_resource_count_
;
1783 SINGLE_AND_MULTI_THREAD_TEST_F(
1784 DelegatedFrameIsActivatedDuringCommit
);
1786 class LayerTreeHostDelegatedTestTwoImplLayers
1787 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
1789 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
1791 void DidCommitAndDrawFrame() override
{
1792 scoped_ptr
<DelegatedFrameData
> frame
;
1793 ReturnedResourceArray resources
;
1795 int next_source_frame_number
= layer_tree_host()->source_frame_number();
1796 switch (next_source_frame_number
) {
1798 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1799 AddTextureQuad(frame
.get(), 999);
1800 AddTransferableResource(frame
.get(), 999);
1801 AddTextureQuad(frame
.get(), 555);
1802 AddTransferableResource(frame
.get(), 555);
1803 SetFrameData(frame
.Pass());
1806 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1807 EXPECT_EQ(0u, resources
.size());
1808 EXPECT_FALSE(TestAndResetAvailable());
1810 // Remove the delegated layer and replace it with a new one. Use the
1811 // same frame and resources for it.
1812 delegated_
->RemoveFromParent();
1813 delegated_
= CreateDelegatedLayer(frame_provider_
.get());
1816 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1817 EXPECT_EQ(0u, resources
.size());
1818 EXPECT_FALSE(TestAndResetAvailable());
1820 // Use a frame with no resources in it.
1821 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1822 SetFrameData(frame
.Pass());
1825 // We gave one frame to the frame provider, so we should get one
1826 // ref back for each resource.
1827 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1829 unsigned expected
[] = {555, 999};
1830 EXPECT_RESOURCES(expected
, resources
);
1831 EXPECT_TRUE(TestAndResetAvailable());
1838 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
1839 ReturnUnusedResourcesFromParent(host_impl
);
1843 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestTwoImplLayers
);
1845 class LayerTreeHostDelegatedTestTwoImplLayersTwoFrames
1846 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
1848 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
1850 void DidCommitAndDrawFrame() override
{
1851 scoped_ptr
<DelegatedFrameData
> frame
;
1852 ReturnedResourceArray resources
;
1854 int next_source_frame_number
= layer_tree_host()->source_frame_number();
1855 switch (next_source_frame_number
) {
1857 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1858 AddTextureQuad(frame
.get(), 999);
1859 AddTransferableResource(frame
.get(), 999);
1860 AddTextureQuad(frame
.get(), 555);
1861 AddTransferableResource(frame
.get(), 555);
1862 SetFrameData(frame
.Pass());
1865 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1866 EXPECT_EQ(0u, resources
.size());
1867 EXPECT_FALSE(TestAndResetAvailable());
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);
1875 // Remove the delegated layer and replace it with a new one. Make a new
1876 // frame but with the same resources for it.
1877 delegated_
->RemoveFromParent();
1880 frame_provider_
->SetFrameData(frame
.Pass());
1881 delegated_
= CreateDelegatedLayer(frame_provider_
.get());
1884 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1885 EXPECT_EQ(0u, resources
.size());
1886 EXPECT_FALSE(TestAndResetAvailable());
1888 // Use a frame with no resources in it.
1889 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1890 SetFrameData(frame
.Pass());
1893 // We gave two frames to the frame provider, so we should get two
1894 // refs back for each resource.
1895 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1897 unsigned expected
[] = {555, 555, 999, 999};
1898 EXPECT_RESOURCES(expected
, resources
);
1899 EXPECT_TRUE(TestAndResetAvailable());
1906 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
1907 ReturnUnusedResourcesFromParent(host_impl
);
1911 SINGLE_AND_MULTI_THREAD_TEST_F(
1912 LayerTreeHostDelegatedTestTwoImplLayersTwoFrames
);
1914 class LayerTreeHostDelegatedTestTwoLayers
1915 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
1917 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
1919 void DidCommitAndDrawFrame() override
{
1920 scoped_ptr
<DelegatedFrameData
> frame
;
1921 ReturnedResourceArray resources
;
1923 int next_source_frame_number
= layer_tree_host()->source_frame_number();
1924 switch (next_source_frame_number
) {
1926 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1927 AddTextureQuad(frame
.get(), 999);
1928 AddTransferableResource(frame
.get(), 999);
1929 AddTextureQuad(frame
.get(), 555);
1930 AddTransferableResource(frame
.get(), 555);
1932 // Create a DelegatedRendererLayer using the frame.
1933 SetFrameData(frame
.Pass());
1936 // Create a second DelegatedRendererLayer using the same frame provider.
1937 delegated_thief_
= CreateDelegatedLayer(frame_provider_
.get());
1938 root_
->AddChild(delegated_thief_
);
1940 // And drop our ref on the frame provider so only the layers keep it
1942 frame_provider_
= NULL
;
1945 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1946 EXPECT_EQ(0u, resources
.size());
1947 EXPECT_FALSE(TestAndResetAvailable());
1949 // Remove one delegated layer from the tree. No resources should be
1951 delegated_
->RemoveFromParent();
1954 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1955 EXPECT_EQ(0u, resources
.size());
1956 EXPECT_FALSE(TestAndResetAvailable());
1958 // Put the first layer back, and remove the other layer and destroy it.
1959 // No resources should be returned yet.
1960 root_
->AddChild(delegated_
);
1961 delegated_thief_
->RemoveFromParent();
1962 delegated_thief_
= NULL
;
1965 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1966 EXPECT_EQ(0u, resources
.size());
1967 EXPECT_FALSE(TestAndResetAvailable());
1969 // Remove the first layer from the tree again. The resources are still
1970 // held by the main thread layer.
1971 delegated_
->RemoveFromParent();
1974 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1975 EXPECT_EQ(0u, resources
.size());
1976 EXPECT_FALSE(TestAndResetAvailable());
1978 // Destroy the layer and the resources should be returned immediately.
1981 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
1983 unsigned expected
[] = {555, 999};
1984 EXPECT_RESOURCES(expected
, resources
);
1985 EXPECT_TRUE(TestAndResetAvailable());
1992 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
1993 ReturnUnusedResourcesFromParent(host_impl
);
1996 scoped_refptr
<DelegatedRendererLayer
> delegated_thief_
;
1999 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestTwoLayers
);
2001 class LayerTreeHostDelegatedTestRemoveAndAddToTree
2002 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
2004 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
2006 void DidCommitAndDrawFrame() override
{
2007 scoped_ptr
<DelegatedFrameData
> frame
;
2008 ReturnedResourceArray resources
;
2010 int next_source_frame_number
= layer_tree_host()->source_frame_number();
2011 switch (next_source_frame_number
) {
2013 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2014 AddTextureQuad(frame
.get(), 999);
2015 AddTransferableResource(frame
.get(), 999);
2016 AddTextureQuad(frame
.get(), 555);
2017 AddTransferableResource(frame
.get(), 555);
2019 // Create a DelegatedRendererLayer using the frame.
2020 SetFrameData(frame
.Pass());
2023 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
2024 EXPECT_EQ(0u, resources
.size());
2025 EXPECT_FALSE(TestAndResetAvailable());
2027 // Remove the layer from the tree. The resources should not be returned
2028 // since they are still on the main thread layer.
2029 delegated_
->RemoveFromParent();
2032 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
2033 EXPECT_EQ(0u, resources
.size());
2034 EXPECT_FALSE(TestAndResetAvailable());
2036 // Add the layer back to the tree.
2037 layer_tree_host()->root_layer()->AddChild(delegated_
);
2040 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
2041 EXPECT_EQ(0u, resources
.size());
2042 EXPECT_FALSE(TestAndResetAvailable());
2044 // Set a new frame. Resources should be returned.
2045 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2046 AddTextureQuad(frame
.get(), 888);
2047 AddTransferableResource(frame
.get(), 888);
2048 AddTextureQuad(frame
.get(), 777);
2049 AddTransferableResource(frame
.get(), 777);
2050 SetFrameData(frame
.Pass());
2053 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
2055 unsigned expected
[] = {555, 999};
2056 EXPECT_RESOURCES(expected
, resources
);
2057 EXPECT_TRUE(TestAndResetAvailable());
2060 // Destroy the layer.
2061 delegated_
->RemoveFromParent();
2065 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
2066 EXPECT_EQ(0u, resources
.size());
2067 EXPECT_FALSE(TestAndResetAvailable());
2069 // Destroy the frame provider. Resources should be returned.
2070 frame_provider_
= NULL
;
2072 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
2074 unsigned expected
[] = {777, 888};
2075 EXPECT_RESOURCES(expected
, resources
);
2076 EXPECT_TRUE(TestAndResetAvailable());
2083 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
2084 ReturnUnusedResourcesFromParent(host_impl
);
2087 scoped_refptr
<DelegatedRendererLayer
> delegated_thief_
;
2090 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestRemoveAndAddToTree
);
2092 class LayerTreeHostDelegatedTestRemoveAndChangeResources
2093 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
{
2095 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
2097 void DidCommitAndDrawFrame() override
{
2098 scoped_ptr
<DelegatedFrameData
> frame
;
2099 ReturnedResourceArray resources
;
2101 int next_source_frame_number
= layer_tree_host()->source_frame_number();
2102 switch (next_source_frame_number
) {
2104 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2105 AddTextureQuad(frame
.get(), 999);
2106 AddTransferableResource(frame
.get(), 999);
2107 AddTextureQuad(frame
.get(), 555);
2108 AddTransferableResource(frame
.get(), 555);
2110 // Create a DelegatedRendererLayer using the frame.
2111 SetFrameData(frame
.Pass());
2114 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
2115 EXPECT_EQ(0u, resources
.size());
2116 EXPECT_FALSE(TestAndResetAvailable());
2118 // Remove the layer from the tree. The resources should not be returned
2119 // since they are still on the main thread layer.
2120 delegated_
->RemoveFromParent();
2123 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
2124 EXPECT_EQ(0u, resources
.size());
2125 EXPECT_FALSE(TestAndResetAvailable());
2127 // Set a new frame. Resources should be returned immediately.
2128 frame
= CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2129 AddTextureQuad(frame
.get(), 888);
2130 AddTransferableResource(frame
.get(), 888);
2131 AddTextureQuad(frame
.get(), 777);
2132 AddTransferableResource(frame
.get(), 777);
2133 SetFrameData(frame
.Pass());
2135 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
2137 unsigned expected
[] = {555, 999};
2138 EXPECT_RESOURCES(expected
, resources
);
2139 EXPECT_TRUE(TestAndResetAvailable());
2143 // Destroy the frame provider.
2144 frame_provider_
= NULL
;
2146 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
2147 EXPECT_EQ(0u, resources
.size());
2148 EXPECT_FALSE(TestAndResetAvailable());
2150 // Destroy the layer. Resources should be returned.
2153 resource_collection_
->TakeUnusedResourcesForChildCompositor(&resources
);
2155 unsigned expected
[] = {777, 888};
2156 EXPECT_RESOURCES(expected
, resources
);
2157 EXPECT_TRUE(TestAndResetAvailable());
2164 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
2165 ReturnUnusedResourcesFromParent(host_impl
);
2168 scoped_refptr
<DelegatedRendererLayer
> delegated_thief_
;
2171 SINGLE_AND_MULTI_THREAD_TEST_F(
2172 LayerTreeHostDelegatedTestRemoveAndChangeResources
);