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(RenderPass::Id(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(RenderPass::Id(1, 1),
102 scoped_ptr<SharedQuadState> shared_quad_state = SharedQuadState::Create();
104 gfx::Rect rect = root_output_rect;
105 gfx::Rect opaque_rect = root_output_rect;
106 gfx::Rect visible_rect = root_output_rect;
107 // An invalid resource id! The resource isn't part of the frame.
108 unsigned resource_id = 5;
109 bool premultiplied_alpha = false;
110 gfx::PointF uv_top_left = gfx::PointF(0.f, 0.f);
111 gfx::PointF uv_bottom_right = gfx::PointF(1.f, 1.f);
112 SkColor background_color = 0;
113 float vertex_opacity[4] = {1.f, 1.f, 1.f, 1.f};
114 bool flipped = false;
116 scoped_ptr<TextureDrawQuad> invalid_draw_quad = TextureDrawQuad::Create();
117 invalid_draw_quad->SetNew(shared_quad_state.get(),
128 root_pass->quad_list.push_back(invalid_draw_quad.PassAs<DrawQuad>());
130 root_pass->shared_quad_state_list.push_back(shared_quad_state.Pass());
132 frame->render_pass_list.push_back(root_pass.Pass());
136 void AddTransferableResource(DelegatedFrameData* frame,
137 ResourceProvider::ResourceId resource_id) {
138 TransferableResource resource;
139 resource.id = resource_id;
140 resource.mailbox_holder.texture_target = GL_TEXTURE_2D;
141 GLbyte arbitrary_mailbox[GL_MAILBOX_SIZE_CHROMIUM] = {
142 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2,
143 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4,
144 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4};
145 resource.mailbox_holder.mailbox.SetName(arbitrary_mailbox);
146 frame->resource_list.push_back(resource);
149 void AddTextureQuad(DelegatedFrameData* frame,
150 ResourceProvider::ResourceId resource_id) {
151 scoped_ptr<SharedQuadState> sqs = SharedQuadState::Create();
152 scoped_ptr<TextureDrawQuad> quad = TextureDrawQuad::Create();
153 float vertex_opacity[4] = { 1.f, 1.f, 1.f, 1.f };
154 quad->SetNew(sqs.get(),
155 gfx::Rect(0, 0, 10, 10),
156 gfx::Rect(0, 0, 10, 10),
157 gfx::Rect(0, 0, 10, 10),
160 gfx::PointF(0.f, 0.f),
161 gfx::PointF(1.f, 1.f),
165 frame->render_pass_list[0]->shared_quad_state_list.push_back(sqs.Pass());
166 frame->render_pass_list[0]->quad_list.push_back(quad.PassAs<DrawQuad>());
169 void AddRenderPass(DelegatedFrameData* frame,
171 const gfx::Rect& output_rect,
172 const gfx::Rect& damage_rect,
173 const FilterOperations& filters,
174 const FilterOperations& background_filters) {
175 for (size_t i = 0; i < frame->render_pass_list.size(); ++i)
176 DCHECK(id != frame->render_pass_list[i]->id);
178 scoped_ptr<RenderPass> pass(RenderPass::Create());
183 frame->render_pass_list.push_back(pass.Pass());
185 scoped_ptr<SharedQuadState> sqs = SharedQuadState::Create();
186 scoped_ptr<RenderPassDrawQuad> quad = RenderPassDrawQuad::Create();
188 quad->SetNew(sqs.get(),
193 0, // mask_resource_id
195 gfx::Rect(0, 0, 1, 1), // mask_uv_rect
198 frame->render_pass_list[0]->shared_quad_state_list.push_back(sqs.Pass());
199 frame->render_pass_list[0]->quad_list.push_back(quad.PassAs<DrawQuad>());
202 static ResourceProvider::ResourceId AppendResourceId(
203 std::vector<ResourceProvider::ResourceId>* resources_in_last_sent_frame,
204 ResourceProvider::ResourceId resource_id) {
205 resources_in_last_sent_frame->push_back(resource_id);
209 void ReturnUnusedResourcesFromParent(LayerTreeHostImpl* host_impl) {
210 DelegatedFrameData* delegated_frame_data =
211 output_surface()->last_sent_frame().delegated_frame_data.get();
212 if (!delegated_frame_data)
215 std::vector<ResourceProvider::ResourceId> resources_in_last_sent_frame;
216 for (size_t i = 0; i < delegated_frame_data->resource_list.size(); ++i) {
217 resources_in_last_sent_frame.push_back(
218 delegated_frame_data->resource_list[i].id);
221 std::vector<ResourceProvider::ResourceId> resources_to_return;
223 const TransferableResourceArray& resources_held_by_parent =
224 output_surface()->resources_held_by_parent();
225 for (size_t i = 0; i < resources_held_by_parent.size(); ++i) {
226 ResourceProvider::ResourceId resource_in_parent =
227 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);
243 host_impl->OnSwapBuffersComplete();
247 class LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
248 : public LayerTreeHostDelegatedTest,
249 public DelegatedFrameResourceCollectionClient {
251 LayerTreeHostDelegatedTestCaseSingleDelegatedLayer()
252 : resource_collection_(new DelegatedFrameResourceCollection),
254 resource_collection_->SetClient(this);
257 virtual void SetupTree() OVERRIDE {
258 root_ = Layer::Create();
259 root_->SetAnchorPoint(gfx::PointF());
260 root_->SetBounds(gfx::Size(10, 10));
262 layer_tree_host()->SetRootLayer(root_);
263 LayerTreeHostDelegatedTest::SetupTree();
266 virtual void BeginTest() OVERRIDE {
267 resource_collection_->SetClient(this);
268 PostSetNeedsCommitToMainThread();
271 void SetFrameData(scoped_ptr<DelegatedFrameData> frame_data) {
272 RenderPass* root_pass = frame_data->render_pass_list.back();
273 gfx::Size frame_size = root_pass->output_rect.size();
275 if (frame_provider_.get() && frame_size == frame_provider_->frame_size()) {
276 frame_provider_->SetFrameData(frame_data.Pass());
280 if (delegated_.get()) {
281 delegated_->RemoveFromParent();
283 frame_provider_ = NULL;
286 frame_provider_ = new DelegatedFrameProvider(resource_collection_.get(),
289 delegated_ = CreateDelegatedLayer(frame_provider_.get());
292 scoped_refptr<DelegatedRendererLayer> CreateDelegatedLayer(
293 DelegatedFrameProvider* frame_provider) {
294 scoped_refptr<DelegatedRendererLayer> delegated =
295 FakeDelegatedRendererLayer::Create(frame_provider);
296 delegated->SetAnchorPoint(gfx::PointF());
297 delegated->SetBounds(gfx::Size(10, 10));
298 delegated->SetIsDrawable(true);
300 root_->AddChild(delegated);
304 virtual void AfterTest() OVERRIDE { resource_collection_->SetClient(NULL); }
306 // DelegatedFrameProviderClient implementation.
307 virtual void UnusedResourcesAreAvailable() OVERRIDE { available_ = true; }
309 bool TestAndResetAvailable() {
310 bool available = available_;
316 scoped_refptr<DelegatedFrameResourceCollection> resource_collection_;
317 scoped_refptr<DelegatedFrameProvider> frame_provider_;
318 scoped_refptr<Layer> root_;
319 scoped_refptr<DelegatedRendererLayer> delegated_;
323 class LayerTreeHostDelegatedTestCreateChildId
324 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
326 LayerTreeHostDelegatedTestCreateChildId()
327 : LayerTreeHostDelegatedTestCaseSingleDelegatedLayer(),
329 did_reset_child_id_(false) {}
331 virtual void DidCommit() OVERRIDE {
334 SetFrameData(CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1)));
337 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
338 if (host_impl->active_tree()->source_frame_number() < 1)
341 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
342 FakeDelegatedRendererLayerImpl* delegated_impl =
343 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
345 TestContextProvider* context_provider = static_cast<TestContextProvider*>(
346 host_impl->output_surface()->context_provider().get());
349 switch (num_activates_) {
351 EXPECT_TRUE(delegated_impl->ChildId());
352 EXPECT_FALSE(did_reset_child_id_);
354 context_provider->ContextGL()->LoseContextCHROMIUM(
355 GL_GUILTY_CONTEXT_RESET_ARB,
356 GL_INNOCENT_CONTEXT_RESET_ARB);
357 context_provider->ContextGL()->Flush();
360 EXPECT_TRUE(delegated_impl->ChildId());
361 EXPECT_TRUE(did_reset_child_id_);
367 virtual void InitializedRendererOnThread(LayerTreeHostImpl* host_impl,
368 bool success) OVERRIDE {
369 EXPECT_TRUE(success);
371 if (num_activates_ < 2)
374 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
375 FakeDelegatedRendererLayerImpl* delegated_impl =
376 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
378 EXPECT_EQ(2, num_activates_);
379 EXPECT_FALSE(delegated_impl->ChildId());
380 did_reset_child_id_ = true;
385 bool did_reset_child_id_;
388 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestCreateChildId);
390 // Test that we can gracefully handle invalid frames after the context was lost.
391 // For example, we might be trying to use the previous frame in that case and
392 // have to make sure we don't crash because our resource accounting goes wrong.
393 class LayerTreeHostDelegatedTestInvalidFrameAfterContextLost
394 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
396 LayerTreeHostDelegatedTestInvalidFrameAfterContextLost()
397 : num_activates_(0), num_output_surfaces_initialized_(0) {}
399 virtual void DidCommit() OVERRIDE {
402 scoped_ptr<DelegatedFrameData> frame1 =
403 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
404 AddTextureQuad(frame1.get(), 999);
405 AddTransferableResource(frame1.get(), 999);
406 SetFrameData(frame1.Pass());
409 virtual void DidInitializeOutputSurface(bool succeeded) OVERRIDE {
410 if (!num_output_surfaces_initialized_++)
413 scoped_refptr<DelegatedRendererLayer> old_delegated = delegated_;
415 CreateInvalidFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1)));
416 // Make sure we end up using the same layer, or we won't test the right
417 // thing, which is to make sure we can handle an invalid frame when using
418 // a stale layer from before the context was lost.
419 DCHECK(delegated_.get() == old_delegated.get());
422 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
423 if (host_impl->active_tree()->source_frame_number() < 1)
426 TestContextProvider* context_provider = static_cast<TestContextProvider*>(
427 host_impl->output_surface()->context_provider().get());
430 switch (num_activates_) {
432 context_provider->ContextGL()->LoseContextCHROMIUM(
433 GL_GUILTY_CONTEXT_RESET_ARB,
434 GL_INNOCENT_CONTEXT_RESET_ARB);
442 virtual void InitializedRendererOnThread(LayerTreeHostImpl* host_impl,
443 bool success) OVERRIDE {
444 EXPECT_TRUE(success);
446 if (num_activates_ < 2)
449 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
450 FakeDelegatedRendererLayerImpl* delegated_impl =
451 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
453 EXPECT_EQ(2, num_activates_);
454 // Resources should have gotten cleared after the context was lost.
455 EXPECT_EQ(0U, delegated_impl->Resources().size());
458 virtual void AfterTest() OVERRIDE {
459 LayerTreeHostDelegatedTestCaseSingleDelegatedLayer::AfterTest();
460 EXPECT_EQ(2, num_output_surfaces_initialized_);
465 int num_output_surfaces_initialized_;
468 SINGLE_AND_MULTI_THREAD_TEST_F(
469 LayerTreeHostDelegatedTestInvalidFrameAfterContextLost);
471 class LayerTreeHostDelegatedTestOffscreenContext_NoFilters
472 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
474 virtual void BeginTest() OVERRIDE {
475 scoped_ptr<DelegatedFrameData> frame =
476 CreateFrameData(gfx::Rect(0, 0, 1, 1),
477 gfx::Rect(0, 0, 1, 1));
478 SetFrameData(frame.Pass());
480 PostSetNeedsCommitToMainThread();
483 virtual void DrawLayersOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
484 EXPECT_FALSE(host_impl->offscreen_context_provider());
489 SINGLE_AND_MULTI_THREAD_TEST_F(
490 LayerTreeHostDelegatedTestOffscreenContext_NoFilters);
492 class LayerTreeHostDelegatedTestOffscreenContext_Filters
493 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
495 virtual void BeginTest() OVERRIDE {
496 scoped_ptr<DelegatedFrameData> frame =
497 CreateFrameData(gfx::Rect(0, 0, 1, 1),
498 gfx::Rect(0, 0, 1, 1));
500 FilterOperations filters;
501 filters.Append(FilterOperation::CreateGrayscaleFilter(0.5f));
502 AddRenderPass(frame.get(),
503 RenderPass::Id(2, 1),
504 gfx::Rect(0, 0, 1, 1),
505 gfx::Rect(0, 0, 1, 1),
508 SetFrameData(frame.Pass());
510 PostSetNeedsCommitToMainThread();
513 virtual void DrawLayersOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
514 bool expect_context = !delegating_renderer();
515 EXPECT_EQ(expect_context, !!host_impl->offscreen_context_provider());
520 SINGLE_AND_MULTI_THREAD_TEST_F(
521 LayerTreeHostDelegatedTestOffscreenContext_Filters);
523 class LayerTreeHostDelegatedTestOffscreenContext_BackgroundFilters
524 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
526 virtual void BeginTest() OVERRIDE {
527 scoped_ptr<DelegatedFrameData> frame =
528 CreateFrameData(gfx::Rect(0, 0, 1, 1),
529 gfx::Rect(0, 0, 1, 1));
531 FilterOperations filters;
532 filters.Append(FilterOperation::CreateGrayscaleFilter(0.5f));
533 AddRenderPass(frame.get(),
534 RenderPass::Id(2, 1),
535 gfx::Rect(0, 0, 1, 1),
536 gfx::Rect(0, 0, 1, 1),
539 SetFrameData(frame.Pass());
541 PostSetNeedsCommitToMainThread();
544 virtual void DrawLayersOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
545 bool expect_context = !delegating_renderer();
546 EXPECT_EQ(expect_context, !!host_impl->offscreen_context_provider());
551 SINGLE_AND_MULTI_THREAD_TEST_F(
552 LayerTreeHostDelegatedTestOffscreenContext_BackgroundFilters);
554 class LayerTreeHostDelegatedTestOffscreenContext_Filters_AddedToTree
555 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
557 virtual void BeginTest() OVERRIDE {
558 scoped_ptr<DelegatedFrameData> frame_no_filters =
559 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
561 scoped_ptr<DelegatedFrameData> frame_with_filters =
562 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
564 FilterOperations filters;
565 filters.Append(FilterOperation::CreateGrayscaleFilter(0.5f));
566 AddRenderPass(frame_with_filters.get(),
567 RenderPass::Id(2, 1),
568 gfx::Rect(0, 0, 1, 1),
569 gfx::Rect(0, 0, 1, 1),
573 SetFrameData(frame_no_filters.Pass());
574 delegated_->RemoveFromParent();
575 SetFrameData(frame_with_filters.Pass());
576 layer_tree_host()->root_layer()->AddChild(delegated_);
578 PostSetNeedsCommitToMainThread();
581 virtual void DrawLayersOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
582 bool expect_context = !delegating_renderer();
583 EXPECT_EQ(expect_context, !!host_impl->offscreen_context_provider());
588 SINGLE_AND_MULTI_THREAD_TEST_F(
589 LayerTreeHostDelegatedTestOffscreenContext_Filters_AddedToTree);
591 class LayerTreeHostDelegatedTestLayerUsesFrameDamage
592 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
594 LayerTreeHostDelegatedTestLayerUsesFrameDamage()
595 : LayerTreeHostDelegatedTestCaseSingleDelegatedLayer(),
596 first_draw_for_source_frame_(true) {}
598 virtual void DidCommit() OVERRIDE {
599 int next_source_frame_number = layer_tree_host()->source_frame_number();
600 switch (next_source_frame_number) {
602 // The first time the layer gets a frame the whole layer should be
605 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1)));
608 // A different frame size will damage the whole layer.
610 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(0, 0, 0, 0)));
613 // Should create a total amount of gfx::Rect(2, 2, 10, 6) damage.
614 // The frame size is 20x20 while the layer is 10x10, so this should
615 // produce a gfx::Rect(1, 1, 5, 3) damage rect.
617 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(2, 2, 5, 5)));
619 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(7, 2, 5, 6)));
622 // Should create zero damage.
623 layer_tree_host()->SetNeedsCommit();
626 // Should damage the full viewport.
627 delegated_->SetBounds(gfx::Size(2, 2));
630 // Should create zero damage.
631 layer_tree_host()->SetNeedsCommit();
634 // Should damage the full layer, tho the frame size is not changing.
635 delegated_->SetBounds(gfx::Size(6, 6));
637 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(1, 1, 2, 2)));
640 // Should create zero damage.
641 layer_tree_host()->SetNeedsCommit();
644 // Should damage the full layer.
645 delegated_->SetDisplaySize(gfx::Size(10, 10));
648 // Should create zero damage.
649 layer_tree_host()->SetNeedsCommit();
652 // Changing the frame size damages the full layer.
654 CreateFrameData(gfx::Rect(0, 0, 5, 5), gfx::Rect(4, 4, 1, 1)));
657 // An invalid frame isn't used, so it should not cause damage.
658 SetFrameData(CreateInvalidFrameData(gfx::Rect(0, 0, 5, 5),
659 gfx::Rect(4, 4, 1, 1)));
662 // Should create gfx::Rect(1, 1, 2, 2) of damage. The frame size is
663 // 5x5 and the display size is now set to 10x10, so this should result
664 // in a gfx::Rect(2, 2, 4, 4) damage rect.
666 CreateFrameData(gfx::Rect(0, 0, 5, 5), gfx::Rect(1, 1, 2, 2)));
669 // Should create zero damage.
670 layer_tree_host()->SetNeedsCommit();
673 // Moving the layer out of the tree and back in will damage the whole
675 delegated_->RemoveFromParent();
676 layer_tree_host()->root_layer()->AddChild(delegated_);
679 // Make a larger frame with lots of damage. Then a frame smaller than
680 // the first frame's damage. The entire layer should be damaged, but
683 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(0, 0, 10, 10)));
685 CreateFrameData(gfx::Rect(0, 0, 5, 5), gfx::Rect(1, 1, 2, 2)));
688 // Make a frame with lots of damage. Then replace it with a frame with
689 // no damage. The entire layer should be damaged, but nothing more.
691 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(0, 0, 10, 10)));
693 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(0, 0, 0, 0)));
696 // Make another layer that uses the same frame provider. The new layer
697 // should be damaged.
698 delegated_copy_ = CreateDelegatedLayer(frame_provider_);
699 delegated_copy_->SetPosition(gfx::Point(5, 0));
701 // Also set a new frame.
703 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(4, 0, 1, 1)));
706 // Set another new frame, both layers should be damaged in the same
709 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(3, 3, 1, 1)));
711 first_draw_for_source_frame_ = true;
714 virtual DrawSwapReadbackResult::DrawResult PrepareToDrawOnThread(
715 LayerTreeHostImpl* host_impl,
716 LayerTreeHostImpl::FrameData* frame,
717 DrawSwapReadbackResult::DrawResult draw_result) OVERRIDE {
718 EXPECT_EQ(DrawSwapReadbackResult::DRAW_SUCCESS, draw_result);
720 if (!first_draw_for_source_frame_)
723 gfx::RectF damage_rect;
724 if (!frame->has_no_damage) {
725 damage_rect = frame->render_passes.back()->damage_rect;
727 // If there is no damage, then we have no render passes to send.
728 EXPECT_TRUE(frame->render_passes.empty());
731 switch (host_impl->active_tree()->source_frame_number()) {
733 // First frame is damaged because of viewport resize.
734 EXPECT_EQ(gfx::RectF(0.f, 0.f, 10.f, 10.f).ToString(),
735 damage_rect.ToString());
738 EXPECT_EQ(gfx::RectF(0.f, 0.f, 10.f, 10.f).ToString(),
739 damage_rect.ToString());
742 EXPECT_EQ(gfx::RectF(0.f, 0.f, 10.f, 10.f).ToString(),
743 damage_rect.ToString());
746 EXPECT_EQ(gfx::RectF(1.f, 1.f, 5.f, 3.f).ToString(),
747 damage_rect.ToString());
750 EXPECT_EQ(gfx::RectF(0.f, 0.f, 0.f, 0.f).ToString(),
751 damage_rect.ToString());
754 EXPECT_EQ(gfx::RectF(0.f, 0.f, 10.f, 10.f).ToString(),
755 damage_rect.ToString());
758 EXPECT_EQ(gfx::RectF(0.f, 0.f, 0.f, 0.f).ToString(),
759 damage_rect.ToString());
762 EXPECT_EQ(gfx::RectF(0.f, 0.f, 6.f, 6.f).ToString(),
763 damage_rect.ToString());
766 EXPECT_EQ(gfx::RectF(0.f, 0.f, 0.f, 0.f).ToString(),
767 damage_rect.ToString());
770 EXPECT_EQ(gfx::RectF(0.f, 0.f, 6.f, 6.f).ToString(),
771 damage_rect.ToString());
774 EXPECT_EQ(gfx::RectF(0.f, 0.f, 0.f, 0.f).ToString(),
775 damage_rect.ToString());
778 EXPECT_EQ(gfx::RectF(0.f, 0.f, 10.f, 10.f).ToString(),
779 damage_rect.ToString());
782 EXPECT_EQ(gfx::RectF(0.f, 0.f, 0.f, 0.f).ToString(),
783 damage_rect.ToString());
786 EXPECT_EQ(gfx::RectF(2.f, 2.f, 4.f, 4.f).ToString(),
787 damage_rect.ToString());
790 EXPECT_EQ(gfx::RectF(0.f, 0.f, 0.f, 0.f).ToString(),
791 damage_rect.ToString());
794 EXPECT_EQ(gfx::RectF(0.f, 0.f, 10.f, 10.f).ToString(),
795 damage_rect.ToString());
798 EXPECT_EQ(gfx::RectF(0.f, 0.f, 10.f, 10.f).ToString(),
799 damage_rect.ToString());
802 EXPECT_EQ(gfx::RectF(0.f, 0.f, 10.f, 10.f).ToString(),
803 damage_rect.ToString());
806 EXPECT_EQ(gfx::UnionRects(gfx::RectF(5.f, 0.f, 10.f, 10.f),
807 gfx::RectF(4.f, 0.f, 1.f, 1.f)).ToString(),
808 damage_rect.ToString());
811 EXPECT_EQ(gfx::RectF(3.f, 3.f, 6.f, 1.f).ToString(),
812 damage_rect.ToString());
821 scoped_refptr<DelegatedRendererLayer> delegated_copy_;
822 bool first_draw_for_source_frame_;
825 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestLayerUsesFrameDamage);
827 class LayerTreeHostDelegatedTestMergeResources
828 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
830 virtual void BeginTest() OVERRIDE {
831 // Push two frames to the delegated renderer layer with no commit between.
833 // The first frame has resource 999.
834 scoped_ptr<DelegatedFrameData> frame1 =
835 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
836 AddTextureQuad(frame1.get(), 999);
837 AddTransferableResource(frame1.get(), 999);
838 SetFrameData(frame1.Pass());
840 // The second frame uses resource 999 still, but also adds 555.
841 scoped_ptr<DelegatedFrameData> frame2 =
842 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
843 AddTextureQuad(frame2.get(), 999);
844 AddTransferableResource(frame2.get(), 999);
845 AddTextureQuad(frame2.get(), 555);
846 AddTransferableResource(frame2.get(), 555);
847 SetFrameData(frame2.Pass());
849 // The resource 999 from frame1 is returned since it is still on the main
851 ReturnedResourceArray returned_resources;
852 resource_collection_->TakeUnusedResourcesForChildCompositor(
853 &returned_resources);
855 unsigned expected[] = {999};
856 EXPECT_RESOURCES(expected, returned_resources);
857 EXPECT_TRUE(TestAndResetAvailable());
860 PostSetNeedsCommitToMainThread();
863 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
864 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
865 FakeDelegatedRendererLayerImpl* delegated_impl =
866 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
868 const ResourceProvider::ResourceIdMap& map =
869 host_impl->resource_provider()->GetChildToParentMap(
870 delegated_impl->ChildId());
872 // Both frames' resources should be in the parent's resource provider.
873 EXPECT_EQ(2u, map.size());
874 EXPECT_EQ(1u, map.count(999));
875 EXPECT_EQ(1u, map.count(555));
877 EXPECT_EQ(2u, delegated_impl->Resources().size());
878 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
879 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
885 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestMergeResources);
887 class LayerTreeHostDelegatedTestRemapResourcesInQuads
888 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
890 virtual void BeginTest() OVERRIDE {
891 // Generate a frame with two resources in it.
892 scoped_ptr<DelegatedFrameData> frame =
893 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
894 AddTextureQuad(frame.get(), 999);
895 AddTransferableResource(frame.get(), 999);
896 AddTextureQuad(frame.get(), 555);
897 AddTransferableResource(frame.get(), 555);
898 SetFrameData(frame.Pass());
900 PostSetNeedsCommitToMainThread();
903 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
904 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
905 FakeDelegatedRendererLayerImpl* delegated_impl =
906 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
908 const ResourceProvider::ResourceIdMap& map =
909 host_impl->resource_provider()->GetChildToParentMap(
910 delegated_impl->ChildId());
912 // The frame's resource should be in the parent's resource provider.
913 EXPECT_EQ(2u, map.size());
914 EXPECT_EQ(1u, map.count(999));
915 EXPECT_EQ(1u, map.count(555));
917 ResourceProvider::ResourceId parent_resource_id1 = map.find(999)->second;
918 EXPECT_NE(parent_resource_id1, 999u);
919 ResourceProvider::ResourceId parent_resource_id2 = map.find(555)->second;
920 EXPECT_NE(parent_resource_id2, 555u);
922 // The resources in the quads should be remapped to the parent's namespace.
923 const TextureDrawQuad* quad1 = TextureDrawQuad::MaterialCast(
924 delegated_impl->RenderPassesInDrawOrder()[0]->quad_list[0]);
925 EXPECT_EQ(parent_resource_id1, quad1->resource_id);
926 const TextureDrawQuad* quad2 = TextureDrawQuad::MaterialCast(
927 delegated_impl->RenderPassesInDrawOrder()[0]->quad_list[1]);
928 EXPECT_EQ(parent_resource_id2, quad2->resource_id);
934 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestRemapResourcesInQuads);
936 class LayerTreeHostDelegatedTestReturnUnusedResources
937 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
939 virtual void BeginTest() OVERRIDE {
940 PostSetNeedsCommitToMainThread();
943 virtual void DidCommitAndDrawFrame() OVERRIDE {
944 scoped_ptr<DelegatedFrameData> frame;
945 ReturnedResourceArray resources;
947 int next_source_frame_number = layer_tree_host()->source_frame_number();
948 switch (next_source_frame_number) {
950 // Generate a frame with two resources in it.
951 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
952 AddTextureQuad(frame.get(), 999);
953 AddTransferableResource(frame.get(), 999);
954 AddTextureQuad(frame.get(), 555);
955 AddTransferableResource(frame.get(), 555);
956 SetFrameData(frame.Pass());
959 // All of the resources are in use.
960 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
961 EXPECT_EQ(0u, resources.size());
962 EXPECT_FALSE(TestAndResetAvailable());
964 // Keep using 999 but stop using 555.
965 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
966 AddTextureQuad(frame.get(), 999);
967 AddTransferableResource(frame.get(), 999);
968 AddTextureQuad(frame.get(), 444);
969 AddTransferableResource(frame.get(), 444);
970 SetFrameData(frame.Pass());
973 // 555 is no longer in use.
974 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
976 unsigned expected[] = {555};
977 EXPECT_RESOURCES(expected, resources);
978 EXPECT_TRUE(TestAndResetAvailable());
981 // Stop using any resources.
982 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
983 SetFrameData(frame.Pass());
986 // Postpone collecting resources for a frame. They should still be there
988 layer_tree_host()->SetNeedsCommit();
991 // 444 and 999 are no longer in use. We sent two refs to 999, so we
992 // should get two back.
993 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
995 unsigned expected[] = {444, 999, 999};
996 EXPECT_RESOURCES(expected, resources);
997 EXPECT_TRUE(TestAndResetAvailable());
1003 // Resources are never immediately released.
1004 ReturnedResourceArray empty_resources;
1005 resource_collection_->TakeUnusedResourcesForChildCompositor(
1007 EXPECT_EQ(0u, empty_resources.size());
1008 EXPECT_FALSE(TestAndResetAvailable());
1011 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
1012 bool result) OVERRIDE {
1013 ReturnUnusedResourcesFromParent(host_impl);
1017 SINGLE_AND_MULTI_THREAD_TEST_F(
1018 LayerTreeHostDelegatedTestReturnUnusedResources);
1020 class LayerTreeHostDelegatedTestReusedResources
1021 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1023 virtual void BeginTest() OVERRIDE {
1024 PostSetNeedsCommitToMainThread();
1027 virtual void DidCommitAndDrawFrame() OVERRIDE {
1028 scoped_ptr<DelegatedFrameData> frame;
1029 ReturnedResourceArray resources;
1031 int next_source_frame_number = layer_tree_host()->source_frame_number();
1032 switch (next_source_frame_number) {
1034 // Generate a frame with some resources in it.
1035 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1036 AddTextureQuad(frame.get(), 999);
1037 AddTransferableResource(frame.get(), 999);
1038 AddTextureQuad(frame.get(), 555);
1039 AddTransferableResource(frame.get(), 555);
1040 AddTextureQuad(frame.get(), 444);
1041 AddTransferableResource(frame.get(), 444);
1042 SetFrameData(frame.Pass());
1045 // All of the resources are in use.
1046 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1047 EXPECT_EQ(0u, resources.size());
1048 EXPECT_FALSE(TestAndResetAvailable());
1050 // Keep using 999 but stop using 555 and 444.
1051 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1052 AddTextureQuad(frame.get(), 999);
1053 AddTransferableResource(frame.get(), 999);
1054 SetFrameData(frame.Pass());
1056 // Resource are not immediately released.
1057 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1058 EXPECT_EQ(0u, resources.size());
1059 EXPECT_FALSE(TestAndResetAvailable());
1061 // Now using 555 and 444 again, but not 999.
1062 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1063 AddTextureQuad(frame.get(), 555);
1064 AddTransferableResource(frame.get(), 555);
1065 AddTextureQuad(frame.get(), 444);
1066 AddTransferableResource(frame.get(), 444);
1067 SetFrameData(frame.Pass());
1070 // The 999 resource is the only unused one. Two references were sent, so
1071 // two should be returned.
1072 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1074 unsigned expected[] = {999, 999};
1075 EXPECT_RESOURCES(expected, resources);
1076 EXPECT_TRUE(TestAndResetAvailable());
1083 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
1084 bool result) OVERRIDE {
1085 ReturnUnusedResourcesFromParent(host_impl);
1089 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestReusedResources);
1091 class LayerTreeHostDelegatedTestFrameBeforeAck
1092 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1094 virtual void BeginTest() OVERRIDE {
1095 PostSetNeedsCommitToMainThread();
1098 virtual void DidCommitAndDrawFrame() OVERRIDE {
1099 scoped_ptr<DelegatedFrameData> frame;
1100 ReturnedResourceArray resources;
1102 int next_source_frame_number = layer_tree_host()->source_frame_number();
1103 switch (next_source_frame_number) {
1105 // Generate a frame with some resources in it.
1106 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1107 AddTextureQuad(frame.get(), 999);
1108 AddTransferableResource(frame.get(), 999);
1109 AddTextureQuad(frame.get(), 555);
1110 AddTransferableResource(frame.get(), 555);
1111 AddTextureQuad(frame.get(), 444);
1112 AddTransferableResource(frame.get(), 444);
1113 SetFrameData(frame.Pass());
1116 // All of the resources are in use.
1117 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1118 EXPECT_EQ(0u, resources.size());
1119 EXPECT_FALSE(TestAndResetAvailable());
1121 // Keep using 999 but stop using 555 and 444.
1122 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1123 AddTextureQuad(frame.get(), 999);
1124 AddTransferableResource(frame.get(), 999);
1125 SetFrameData(frame.Pass());
1127 // Resource are not immediately released.
1128 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1129 EXPECT_EQ(0u, resources.size());
1130 EXPECT_FALSE(TestAndResetAvailable());
1132 // The parent compositor (this one) does a commit.
1135 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1137 unsigned expected[] = {444, 555};
1138 EXPECT_RESOURCES(expected, resources);
1139 EXPECT_TRUE(TestAndResetAvailable());
1142 // The child compositor sends a frame referring to resources not in the
1144 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1145 AddTextureQuad(frame.get(), 999);
1146 AddTextureQuad(frame.get(), 555);
1147 AddTextureQuad(frame.get(), 444);
1148 SetFrameData(frame.Pass());
1153 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
1154 if (host_impl->active_tree()->source_frame_number() != 3)
1157 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1158 FakeDelegatedRendererLayerImpl* delegated_impl =
1159 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1161 const ResourceProvider::ResourceIdMap& map =
1162 host_impl->resource_provider()->GetChildToParentMap(
1163 delegated_impl->ChildId());
1165 // The bad frame should be dropped. So we should only have one quad (the
1166 // one with resource 999) on the impl tree. And only 999 will be present
1167 // in the parent's resource provider.
1168 EXPECT_EQ(1u, map.size());
1169 EXPECT_EQ(1u, map.count(999));
1171 EXPECT_EQ(1u, delegated_impl->Resources().size());
1172 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1174 const RenderPass* pass = delegated_impl->RenderPassesInDrawOrder()[0];
1175 EXPECT_EQ(1u, pass->quad_list.size());
1176 const TextureDrawQuad* quad = TextureDrawQuad::MaterialCast(
1177 pass->quad_list[0]);
1178 EXPECT_EQ(map.find(999)->second, quad->resource_id);
1183 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
1184 bool result) OVERRIDE {
1185 ReturnUnusedResourcesFromParent(host_impl);
1189 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestFrameBeforeAck);
1191 class LayerTreeHostDelegatedTestFrameBeforeTakeResources
1192 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1194 virtual void BeginTest() OVERRIDE {
1195 PostSetNeedsCommitToMainThread();
1198 virtual void DidCommitAndDrawFrame() OVERRIDE {
1199 scoped_ptr<DelegatedFrameData> frame;
1200 ReturnedResourceArray resources;
1202 int next_source_frame_number = layer_tree_host()->source_frame_number();
1203 switch (next_source_frame_number) {
1205 // Generate a frame with some resources in it.
1206 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1207 AddTextureQuad(frame.get(), 999);
1208 AddTransferableResource(frame.get(), 999);
1209 AddTextureQuad(frame.get(), 555);
1210 AddTransferableResource(frame.get(), 555);
1211 AddTextureQuad(frame.get(), 444);
1212 AddTransferableResource(frame.get(), 444);
1213 SetFrameData(frame.Pass());
1216 // All of the resources are in use.
1217 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1218 EXPECT_EQ(0u, resources.size());
1219 EXPECT_FALSE(TestAndResetAvailable());
1221 // Keep using 999 but stop using 555 and 444.
1222 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1223 AddTextureQuad(frame.get(), 999);
1224 AddTransferableResource(frame.get(), 999);
1225 SetFrameData(frame.Pass());
1227 // Resource are not immediately released.
1228 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1229 EXPECT_EQ(0u, resources.size());
1230 EXPECT_FALSE(TestAndResetAvailable());
1232 // The parent compositor (this one) does a commit.
1235 // The child compositor sends a frame before taking resources back
1236 // from the previous commit. This frame makes use of the resources 555
1237 // and 444, which were just released during commit.
1238 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1239 AddTextureQuad(frame.get(), 999);
1240 AddTransferableResource(frame.get(), 999);
1241 AddTextureQuad(frame.get(), 555);
1242 AddTransferableResource(frame.get(), 555);
1243 AddTextureQuad(frame.get(), 444);
1244 AddTransferableResource(frame.get(), 444);
1245 SetFrameData(frame.Pass());
1247 // The resources are used by the new frame but are returned anyway since
1248 // we passed them again.
1249 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1251 unsigned expected[] = {444, 555};
1252 EXPECT_RESOURCES(expected, resources);
1253 EXPECT_TRUE(TestAndResetAvailable());
1257 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1258 EXPECT_EQ(0u, resources.size());
1259 EXPECT_FALSE(TestAndResetAvailable());
1265 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
1266 if (host_impl->active_tree()->source_frame_number() != 3)
1269 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1270 FakeDelegatedRendererLayerImpl* delegated_impl =
1271 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1273 const ResourceProvider::ResourceIdMap& map =
1274 host_impl->resource_provider()->GetChildToParentMap(
1275 delegated_impl->ChildId());
1277 // The third frame has all of the resources in it again, the delegated
1278 // renderer layer should continue to own the resources for it.
1279 EXPECT_EQ(3u, map.size());
1280 EXPECT_EQ(1u, map.count(999));
1281 EXPECT_EQ(1u, map.count(555));
1282 EXPECT_EQ(1u, map.count(444));
1284 EXPECT_EQ(3u, delegated_impl->Resources().size());
1285 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1286 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1287 EXPECT_EQ(1u, delegated_impl->Resources().count(444));
1289 const RenderPass* pass = delegated_impl->RenderPassesInDrawOrder()[0];
1290 EXPECT_EQ(3u, pass->quad_list.size());
1291 const TextureDrawQuad* quad1 = TextureDrawQuad::MaterialCast(
1292 pass->quad_list[0]);
1293 EXPECT_EQ(map.find(999)->second, quad1->resource_id);
1294 const TextureDrawQuad* quad2 = TextureDrawQuad::MaterialCast(
1295 pass->quad_list[1]);
1296 EXPECT_EQ(map.find(555)->second, quad2->resource_id);
1297 const TextureDrawQuad* quad3 = TextureDrawQuad::MaterialCast(
1298 pass->quad_list[2]);
1299 EXPECT_EQ(map.find(444)->second, quad3->resource_id);
1302 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
1303 bool result) OVERRIDE {
1304 ReturnUnusedResourcesFromParent(host_impl);
1308 SINGLE_AND_MULTI_THREAD_TEST_F(
1309 LayerTreeHostDelegatedTestFrameBeforeTakeResources);
1311 class LayerTreeHostDelegatedTestBadFrame
1312 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1314 virtual void BeginTest() OVERRIDE {
1315 PostSetNeedsCommitToMainThread();
1318 virtual void DidCommitAndDrawFrame() OVERRIDE {
1319 scoped_ptr<DelegatedFrameData> frame;
1320 ReturnedResourceArray resources;
1322 int next_source_frame_number = layer_tree_host()->source_frame_number();
1323 switch (next_source_frame_number) {
1325 // Generate a frame with some resources in it.
1326 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1327 AddTextureQuad(frame.get(), 999);
1328 AddTransferableResource(frame.get(), 999);
1329 AddTextureQuad(frame.get(), 555);
1330 AddTransferableResource(frame.get(), 555);
1331 SetFrameData(frame.Pass());
1334 // All of the resources are in use.
1335 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1336 EXPECT_EQ(0u, resources.size());
1337 EXPECT_FALSE(TestAndResetAvailable());
1339 // Generate a bad frame with a resource the layer doesn't have. The
1340 // 885 and 775 resources are unknown, while ownership of the legit 444
1341 // resource is passed in here. The bad frame does not use any of the
1342 // previous resources, 999 or 555.
1343 // A bad quad is present both before and after the good quad.
1344 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1345 AddTextureQuad(frame.get(), 885);
1346 AddTextureQuad(frame.get(), 444);
1347 AddTransferableResource(frame.get(), 444);
1348 AddTextureQuad(frame.get(), 775);
1349 SetFrameData(frame.Pass());
1351 // The parent compositor (this one) does a commit.
1354 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1355 EXPECT_EQ(0u, resources.size());
1356 EXPECT_FALSE(TestAndResetAvailable());
1358 // Now send a good frame with 999 again.
1359 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1360 AddTextureQuad(frame.get(), 999);
1361 SetFrameData(frame.Pass());
1363 // The bad frame's resource is given back to the child compositor.
1364 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1366 unsigned expected[] = {444};
1367 EXPECT_RESOURCES(expected, resources);
1368 EXPECT_TRUE(TestAndResetAvailable());
1372 // The unused 555 from the last good frame is now released.
1373 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1375 unsigned expected[] = {555};
1376 EXPECT_RESOURCES(expected, resources);
1377 EXPECT_TRUE(TestAndResetAvailable());
1385 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
1386 bool result) OVERRIDE {
1387 if (host_impl->active_tree()->source_frame_number() < 1)
1390 ReturnUnusedResourcesFromParent(host_impl);
1392 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1393 FakeDelegatedRendererLayerImpl* delegated_impl =
1394 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1396 const ResourceProvider::ResourceIdMap& map =
1397 host_impl->resource_provider()->GetChildToParentMap(
1398 delegated_impl->ChildId());
1400 switch (host_impl->active_tree()->source_frame_number()) {
1402 // We have the first good frame with just 990 and 555 in it.
1404 EXPECT_EQ(2u, map.size());
1405 EXPECT_EQ(1u, map.count(999));
1406 EXPECT_EQ(1u, map.count(555));
1408 EXPECT_EQ(2u, delegated_impl->Resources().size());
1409 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1410 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1412 const RenderPass* pass = delegated_impl->RenderPassesInDrawOrder()[0];
1413 EXPECT_EQ(2u, pass->quad_list.size());
1414 const TextureDrawQuad* quad1 = TextureDrawQuad::MaterialCast(
1415 pass->quad_list[0]);
1416 EXPECT_EQ(map.find(999)->second, quad1->resource_id);
1417 const TextureDrawQuad* quad2 = TextureDrawQuad::MaterialCast(
1418 pass->quad_list[1]);
1419 EXPECT_EQ(map.find(555)->second, quad2->resource_id);
1423 // We only keep resources from the last valid frame.
1424 EXPECT_EQ(2u, map.size());
1425 EXPECT_EQ(1u, map.count(999));
1426 EXPECT_EQ(1u, map.count(555));
1428 EXPECT_EQ(2u, delegated_impl->Resources().size());
1429 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1430 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1432 // The bad frame is dropped though, we still have the frame with 999 and
1434 const RenderPass* pass = delegated_impl->RenderPassesInDrawOrder()[0];
1435 EXPECT_EQ(2u, pass->quad_list.size());
1436 const TextureDrawQuad* quad1 = TextureDrawQuad::MaterialCast(
1437 pass->quad_list[0]);
1438 EXPECT_EQ(map.find(999)->second, quad1->resource_id);
1439 const TextureDrawQuad* quad2 = TextureDrawQuad::MaterialCast(
1440 pass->quad_list[1]);
1441 EXPECT_EQ(map.find(555)->second, quad2->resource_id);
1445 // We have the new good frame with just 999 in it.
1446 EXPECT_EQ(1u, map.size());
1447 EXPECT_EQ(1u, map.count(999));
1449 EXPECT_EQ(1u, delegated_impl->Resources().size());
1450 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1452 const RenderPass* pass = delegated_impl->RenderPassesInDrawOrder()[0];
1453 EXPECT_EQ(1u, pass->quad_list.size());
1454 const TextureDrawQuad* quad1 = TextureDrawQuad::MaterialCast(
1455 pass->quad_list[0]);
1456 EXPECT_EQ(map.find(999)->second, quad1->resource_id);
1463 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestBadFrame);
1465 class LayerTreeHostDelegatedTestUnnamedResource
1466 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1468 virtual void BeginTest() OVERRIDE {
1469 PostSetNeedsCommitToMainThread();
1472 virtual void DidCommit() OVERRIDE {
1473 scoped_ptr<DelegatedFrameData> frame;
1474 ReturnedResourceArray resources;
1476 int next_source_frame_number = layer_tree_host()->source_frame_number();
1477 switch (next_source_frame_number) {
1479 // This frame includes two resources in it, but only uses one.
1480 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1481 AddTransferableResource(frame.get(), 999);
1482 AddTextureQuad(frame.get(), 555);
1483 AddTransferableResource(frame.get(), 555);
1484 SetFrameData(frame.Pass());
1487 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1488 EXPECT_EQ(0u, resources.size());
1489 EXPECT_FALSE(TestAndResetAvailable());
1491 // Now send an empty frame.
1492 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1493 SetFrameData(frame.Pass());
1495 // The unused resource should be returned.
1496 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1498 unsigned expected[] = {999};
1499 EXPECT_RESOURCES(expected, resources);
1500 EXPECT_TRUE(TestAndResetAvailable());
1508 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
1509 if (host_impl->active_tree()->source_frame_number() != 1)
1512 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1513 FakeDelegatedRendererLayerImpl* delegated_impl =
1514 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1516 const ResourceProvider::ResourceIdMap& map =
1517 host_impl->resource_provider()->GetChildToParentMap(
1518 delegated_impl->ChildId());
1520 // The layer only held on to the resource that was used.
1521 EXPECT_EQ(1u, map.size());
1522 EXPECT_EQ(1u, map.count(555));
1524 EXPECT_EQ(1u, delegated_impl->Resources().size());
1525 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1529 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestUnnamedResource);
1531 class LayerTreeHostDelegatedTestDontLeakResource
1532 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1534 virtual void BeginTest() OVERRIDE {
1535 PostSetNeedsCommitToMainThread();
1538 virtual void DidCommitAndDrawFrame() OVERRIDE {
1539 scoped_ptr<DelegatedFrameData> frame;
1540 ReturnedResourceArray resources;
1542 int next_source_frame_number = layer_tree_host()->source_frame_number();
1543 switch (next_source_frame_number) {
1545 // This frame includes two resources in it.
1546 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1547 AddTextureQuad(frame.get(), 999);
1548 AddTransferableResource(frame.get(), 999);
1549 AddTextureQuad(frame.get(), 555);
1550 AddTransferableResource(frame.get(), 555);
1551 SetFrameData(frame.Pass());
1553 // But then we immediately stop using 999.
1554 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1555 AddTextureQuad(frame.get(), 555);
1556 AddTransferableResource(frame.get(), 555);
1557 SetFrameData(frame.Pass());
1560 // The unused resources should be returned. 555 is still used, but it's
1561 // returned once to account for the first frame.
1562 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1564 unsigned expected[] = {555, 999};
1565 EXPECT_RESOURCES(expected, resources);
1566 EXPECT_TRUE(TestAndResetAvailable());
1568 // Send a frame with no resources in it.
1569 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1570 SetFrameData(frame.Pass());
1573 // The now unused resource 555 should be returned.
1575 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1577 unsigned expected[] = {555};
1578 EXPECT_RESOURCES(expected, resources);
1579 EXPECT_TRUE(TestAndResetAvailable());
1586 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
1587 if (host_impl->active_tree()->source_frame_number() != 1)
1590 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1591 FakeDelegatedRendererLayerImpl* delegated_impl =
1592 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1594 const ResourceProvider::ResourceIdMap& map =
1595 host_impl->resource_provider()->GetChildToParentMap(
1596 delegated_impl->ChildId());
1598 // The layer only held on to the resource that was used.
1599 EXPECT_EQ(1u, map.size());
1600 EXPECT_EQ(1u, map.count(555));
1602 EXPECT_EQ(1u, delegated_impl->Resources().size());
1603 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1606 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
1607 bool result) OVERRIDE {
1608 ReturnUnusedResourcesFromParent(host_impl);
1612 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestDontLeakResource);
1614 class LayerTreeHostDelegatedTestResourceSentToParent
1615 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1617 virtual void DidCommitAndDrawFrame() OVERRIDE {
1618 scoped_ptr<DelegatedFrameData> frame;
1619 ReturnedResourceArray resources;
1621 int next_source_frame_number = layer_tree_host()->source_frame_number();
1622 switch (next_source_frame_number) {
1624 // This frame includes two resources in it.
1625 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1626 AddTextureQuad(frame.get(), 999);
1627 AddTransferableResource(frame.get(), 999);
1628 AddTextureQuad(frame.get(), 555);
1629 AddTransferableResource(frame.get(), 555);
1630 SetFrameData(frame.Pass());
1633 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1634 EXPECT_EQ(0u, resources.size());
1635 EXPECT_FALSE(TestAndResetAvailable());
1637 // 999 is in use in the grandparent compositor, generate a frame without
1639 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1640 AddTextureQuad(frame.get(), 555);
1641 AddTransferableResource(frame.get(), 555);
1642 SetFrameData(frame.Pass());
1645 // Since 999 is in the grandparent it is not returned.
1646 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1647 EXPECT_EQ(0u, resources.size());
1648 EXPECT_FALSE(TestAndResetAvailable());
1650 // The impl side will get back the resource at some point.
1651 ImplThreadTaskRunner()->PostTask(FROM_HERE,
1652 receive_resource_on_thread_);
1657 void ReceiveResourceOnThread(LayerTreeHostImpl* host_impl) {
1658 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1659 FakeDelegatedRendererLayerImpl* delegated_impl =
1660 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1662 const ResourceProvider::ResourceIdMap& map =
1663 host_impl->resource_provider()->GetChildToParentMap(
1664 delegated_impl->ChildId());
1666 // Receive 999 back from the grandparent.
1667 CompositorFrameAck ack;
1668 output_surface()->ReturnResource(map.find(999)->second, &ack);
1669 host_impl->ReclaimResources(&ack);
1670 host_impl->OnSwapBuffersComplete();
1673 virtual void UnusedResourcesAreAvailable() OVERRIDE {
1674 EXPECT_EQ(3, layer_tree_host()->source_frame_number());
1676 ReturnedResourceArray resources;
1678 // 999 was returned from the grandparent and could be released.
1679 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1681 unsigned expected[] = {999};
1682 EXPECT_RESOURCES(expected, resources);
1688 virtual 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(2u, map.size());
1703 EXPECT_EQ(1u, map.count(999));
1704 EXPECT_EQ(1u, map.count(555));
1706 EXPECT_EQ(2u, delegated_impl->Resources().size());
1707 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1708 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1710 // The 999 resource will be sent to a grandparent compositor.
1714 EXPECT_EQ(2u, map.size());
1715 EXPECT_EQ(1u, map.count(999));
1716 EXPECT_EQ(1u, map.count(555));
1718 // 999 is in the parent, so not held by delegated renderer layer.
1719 EXPECT_EQ(1u, delegated_impl->Resources().size());
1720 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1722 receive_resource_on_thread_ =
1723 base::Bind(&LayerTreeHostDelegatedTestResourceSentToParent::
1724 ReceiveResourceOnThread,
1725 base::Unretained(this),
1730 // 999 should be released.
1731 EXPECT_EQ(1u, map.size());
1732 EXPECT_EQ(1u, map.count(555));
1734 EXPECT_EQ(1u, delegated_impl->Resources().size());
1735 EXPECT_EQ(1u, delegated_impl->Resources().count(map.find(555)->second));
1740 base::Closure receive_resource_on_thread_;
1743 SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(
1744 LayerTreeHostDelegatedTestResourceSentToParent);
1746 class LayerTreeHostDelegatedTestCommitWithoutTake
1747 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1749 virtual void BeginTest() OVERRIDE {
1750 // Prevent drawing with resources that are sent to the grandparent.
1751 layer_tree_host()->SetViewportSize(gfx::Size());
1752 PostSetNeedsCommitToMainThread();
1755 virtual void DidCommit() OVERRIDE {
1756 scoped_ptr<DelegatedFrameData> frame;
1757 ReturnedResourceArray resources;
1759 int next_source_frame_number = layer_tree_host()->source_frame_number();
1760 switch (next_source_frame_number) {
1762 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1763 AddTextureQuad(frame.get(), 999);
1764 AddTransferableResource(frame.get(), 999);
1765 AddTextureQuad(frame.get(), 555);
1766 AddTransferableResource(frame.get(), 555);
1767 AddTextureQuad(frame.get(), 444);
1768 AddTransferableResource(frame.get(), 444);
1769 SetFrameData(frame.Pass());
1772 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1773 EXPECT_EQ(0u, resources.size());
1774 EXPECT_FALSE(TestAndResetAvailable());
1776 // Stop using 999 and 444 in this frame and commit.
1777 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1778 AddTextureQuad(frame.get(), 555);
1779 AddTransferableResource(frame.get(), 555);
1780 SetFrameData(frame.Pass());
1781 // 999 and 444 will be returned for frame 1, but not 555 since it's in
1782 // the current frame.
1785 // Don't take resources here, but set a new frame that uses 999 again.
1786 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1787 AddTextureQuad(frame.get(), 999);
1788 AddTransferableResource(frame.get(), 999);
1789 AddTextureQuad(frame.get(), 555);
1790 AddTransferableResource(frame.get(), 555);
1791 SetFrameData(frame.Pass());
1794 // 555 from frame 1 and 2 isn't returned since it's still in use. 999
1795 // from frame 1 is returned though.
1796 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1798 unsigned expected[] = {444, 999};
1799 EXPECT_RESOURCES(expected, resources);
1800 EXPECT_TRUE(TestAndResetAvailable());
1803 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1804 SetFrameData(frame.Pass());
1805 // 555 will be returned 3 times for frames 1 2 and 3, and 999 will be
1806 // returned once for frame 3.
1809 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1811 unsigned expected[] = {555, 555, 555, 999};
1812 EXPECT_RESOURCES(expected, resources);
1813 EXPECT_TRUE(TestAndResetAvailable());
1821 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
1822 if (host_impl->active_tree()->source_frame_number() < 1)
1825 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1826 FakeDelegatedRendererLayerImpl* delegated_impl =
1827 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1829 const ResourceProvider::ResourceIdMap& map =
1830 host_impl->resource_provider()->GetChildToParentMap(
1831 delegated_impl->ChildId());
1833 switch (host_impl->active_tree()->source_frame_number()) {
1835 EXPECT_EQ(3u, map.size());
1836 EXPECT_EQ(1u, map.count(999));
1837 EXPECT_EQ(1u, map.count(555));
1838 EXPECT_EQ(1u, map.count(444));
1840 EXPECT_EQ(3u, delegated_impl->Resources().size());
1841 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1842 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1843 EXPECT_EQ(1u, delegated_impl->Resources().count(444));
1846 EXPECT_EQ(1u, map.size());
1847 EXPECT_EQ(1u, map.count(555));
1849 EXPECT_EQ(1u, delegated_impl->Resources().size());
1850 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1853 EXPECT_EQ(2u, map.size());
1854 EXPECT_EQ(1u, map.count(999));
1855 EXPECT_EQ(1u, map.count(555));
1857 EXPECT_EQ(2u, delegated_impl->Resources().size());
1858 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1859 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1864 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestCommitWithoutTake);
1866 class DelegatedFrameIsActivatedDuringCommit
1867 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1869 DelegatedFrameIsActivatedDuringCommit() : returned_resource_count_(0) {}
1871 virtual void BeginTest() OVERRIDE {
1872 activate_count_ = 0;
1874 scoped_ptr<DelegatedFrameData> frame =
1875 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1876 AddTextureQuad(frame.get(), 999);
1877 AddTransferableResource(frame.get(), 999);
1878 SetFrameData(frame.Pass());
1880 PostSetNeedsCommitToMainThread();
1883 virtual void WillActivateTreeOnThread(LayerTreeHostImpl* impl) OVERRIDE {
1887 virtual void DidCommit() OVERRIDE {
1888 switch (layer_tree_host()->source_frame_number()) {
1890 // The first frame has been activated. Set a new frame, and
1891 // expect the next commit to finish *after* it is activated.
1892 scoped_ptr<DelegatedFrameData> frame =
1893 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1894 AddTextureQuad(frame.get(), 555);
1895 AddTransferableResource(frame.get(), 555);
1896 SetFrameData(frame.Pass());
1900 // The second frame has been activated. Remove the layer from
1901 // the tree to cause another commit/activation. The commit should
1902 // finish *after* the layer is removed from the active tree.
1903 delegated_->RemoveFromParent();
1906 // Finish the test by releasing resources on the next frame.
1907 scoped_ptr<DelegatedFrameData> frame =
1908 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1909 SetFrameData(frame.Pass());
1914 virtual void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
1915 switch (host_impl->active_tree()->source_frame_number()) {
1917 // The activate for the 2nd frame should have happened before now.
1918 EXPECT_EQ(2, activate_count_);
1922 // The activate to remove the layer should have happened before now.
1923 EXPECT_EQ(3, activate_count_);
1929 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
1930 bool result) OVERRIDE {
1931 ReturnUnusedResourcesFromParent(host_impl);
1934 virtual void UnusedResourcesAreAvailable() OVERRIDE {
1935 LayerTreeHostDelegatedTestCaseSingleDelegatedLayer::
1936 UnusedResourcesAreAvailable();
1937 ReturnedResourceArray resources;
1938 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1939 EXPECT_TRUE(TestAndResetAvailable());
1940 returned_resource_count_ += resources.size();
1941 if (returned_resource_count_ == 2)
1945 int activate_count_;
1946 size_t returned_resource_count_;
1949 SINGLE_AND_MULTI_THREAD_TEST_F(
1950 DelegatedFrameIsActivatedDuringCommit);
1952 class LayerTreeHostDelegatedTestTwoImplLayers
1953 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1955 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
1957 virtual void DidCommitAndDrawFrame() OVERRIDE {
1958 scoped_ptr<DelegatedFrameData> frame;
1959 ReturnedResourceArray resources;
1961 int next_source_frame_number = layer_tree_host()->source_frame_number();
1962 switch (next_source_frame_number) {
1964 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1965 AddTextureQuad(frame.get(), 999);
1966 AddTransferableResource(frame.get(), 999);
1967 AddTextureQuad(frame.get(), 555);
1968 AddTransferableResource(frame.get(), 555);
1969 SetFrameData(frame.Pass());
1972 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1973 EXPECT_EQ(0u, resources.size());
1974 EXPECT_FALSE(TestAndResetAvailable());
1976 // Remove the delegated layer and replace it with a new one. Use the
1977 // same frame and resources for it.
1978 delegated_->RemoveFromParent();
1979 delegated_ = CreateDelegatedLayer(frame_provider_.get());
1982 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1983 EXPECT_EQ(0u, resources.size());
1984 EXPECT_FALSE(TestAndResetAvailable());
1986 // Use a frame with no resources in it.
1987 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1988 SetFrameData(frame.Pass());
1991 // We gave one frame to the frame provider, so we should get one
1992 // ref back for each resource.
1993 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1995 unsigned expected[] = {555, 999};
1996 EXPECT_RESOURCES(expected, resources);
1997 EXPECT_TRUE(TestAndResetAvailable());
2004 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
2005 bool result) OVERRIDE {
2006 ReturnUnusedResourcesFromParent(host_impl);
2010 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestTwoImplLayers);
2012 class LayerTreeHostDelegatedTestTwoImplLayersTwoFrames
2013 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
2015 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
2017 virtual void DidCommitAndDrawFrame() OVERRIDE {
2018 scoped_ptr<DelegatedFrameData> frame;
2019 ReturnedResourceArray resources;
2021 int next_source_frame_number = layer_tree_host()->source_frame_number();
2022 switch (next_source_frame_number) {
2024 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2025 AddTextureQuad(frame.get(), 999);
2026 AddTransferableResource(frame.get(), 999);
2027 AddTextureQuad(frame.get(), 555);
2028 AddTransferableResource(frame.get(), 555);
2029 SetFrameData(frame.Pass());
2032 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2033 EXPECT_EQ(0u, resources.size());
2034 EXPECT_FALSE(TestAndResetAvailable());
2036 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2037 AddTextureQuad(frame.get(), 999);
2038 AddTransferableResource(frame.get(), 999);
2039 AddTextureQuad(frame.get(), 555);
2040 AddTransferableResource(frame.get(), 555);
2042 // Remove the delegated layer and replace it with a new one. Make a new
2043 // frame but with the same resources for it.
2044 delegated_->RemoveFromParent();
2047 frame_provider_->SetFrameData(frame.Pass());
2048 delegated_ = CreateDelegatedLayer(frame_provider_.get());
2051 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2052 EXPECT_EQ(0u, resources.size());
2053 EXPECT_FALSE(TestAndResetAvailable());
2055 // Use a frame with no resources in it.
2056 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2057 SetFrameData(frame.Pass());
2060 // We gave two frames to the frame provider, so we should get two
2061 // refs back for each resource.
2062 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2064 unsigned expected[] = {555, 555, 999, 999};
2065 EXPECT_RESOURCES(expected, resources);
2066 EXPECT_TRUE(TestAndResetAvailable());
2073 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
2074 bool result) OVERRIDE {
2075 ReturnUnusedResourcesFromParent(host_impl);
2079 SINGLE_AND_MULTI_THREAD_TEST_F(
2080 LayerTreeHostDelegatedTestTwoImplLayersTwoFrames);
2082 class LayerTreeHostDelegatedTestTwoLayers
2083 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
2085 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
2087 virtual void DidCommitAndDrawFrame() OVERRIDE {
2088 scoped_ptr<DelegatedFrameData> frame;
2089 ReturnedResourceArray resources;
2091 int next_source_frame_number = layer_tree_host()->source_frame_number();
2092 switch (next_source_frame_number) {
2094 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2095 AddTextureQuad(frame.get(), 999);
2096 AddTransferableResource(frame.get(), 999);
2097 AddTextureQuad(frame.get(), 555);
2098 AddTransferableResource(frame.get(), 555);
2100 // Create a DelegatedRendererLayer using the frame.
2101 SetFrameData(frame.Pass());
2104 // Create a second DelegatedRendererLayer using the same frame provider.
2105 delegated_thief_ = CreateDelegatedLayer(frame_provider_.get());
2106 root_->AddChild(delegated_thief_);
2108 // And drop our ref on the frame provider so only the layers keep it
2110 frame_provider_ = NULL;
2113 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2114 EXPECT_EQ(0u, resources.size());
2115 EXPECT_FALSE(TestAndResetAvailable());
2117 // Remove one delegated layer from the tree. No resources should be
2119 delegated_->RemoveFromParent();
2122 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2123 EXPECT_EQ(0u, resources.size());
2124 EXPECT_FALSE(TestAndResetAvailable());
2126 // Put the first layer back, and remove the other layer and destroy it.
2127 // No resources should be returned yet.
2128 root_->AddChild(delegated_);
2129 delegated_thief_->RemoveFromParent();
2130 delegated_thief_ = NULL;
2133 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2134 EXPECT_EQ(0u, resources.size());
2135 EXPECT_FALSE(TestAndResetAvailable());
2137 // Remove the first layer from the tree again. The resources are still
2138 // held by the main thread layer.
2139 delegated_->RemoveFromParent();
2142 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2143 EXPECT_EQ(0u, resources.size());
2144 EXPECT_FALSE(TestAndResetAvailable());
2146 // Destroy the layer and the resources should be returned immediately.
2149 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2151 unsigned expected[] = {555, 999};
2152 EXPECT_RESOURCES(expected, resources);
2153 EXPECT_TRUE(TestAndResetAvailable());
2160 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
2161 bool result) OVERRIDE {
2162 ReturnUnusedResourcesFromParent(host_impl);
2165 scoped_refptr<DelegatedRendererLayer> delegated_thief_;
2168 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestTwoLayers);
2170 class LayerTreeHostDelegatedTestRemoveAndAddToTree
2171 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
2173 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
2175 virtual void DidCommitAndDrawFrame() OVERRIDE {
2176 scoped_ptr<DelegatedFrameData> frame;
2177 ReturnedResourceArray resources;
2179 int next_source_frame_number = layer_tree_host()->source_frame_number();
2180 switch (next_source_frame_number) {
2182 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2183 AddTextureQuad(frame.get(), 999);
2184 AddTransferableResource(frame.get(), 999);
2185 AddTextureQuad(frame.get(), 555);
2186 AddTransferableResource(frame.get(), 555);
2188 // Create a DelegatedRendererLayer using the frame.
2189 SetFrameData(frame.Pass());
2192 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2193 EXPECT_EQ(0u, resources.size());
2194 EXPECT_FALSE(TestAndResetAvailable());
2196 // Remove the layer from the tree. The resources should not be returned
2197 // since they are still on the main thread layer.
2198 delegated_->RemoveFromParent();
2201 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2202 EXPECT_EQ(0u, resources.size());
2203 EXPECT_FALSE(TestAndResetAvailable());
2205 // Add the layer back to the tree.
2206 layer_tree_host()->root_layer()->AddChild(delegated_);
2209 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2210 EXPECT_EQ(0u, resources.size());
2211 EXPECT_FALSE(TestAndResetAvailable());
2213 // Set a new frame. Resources should be returned.
2214 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2215 AddTextureQuad(frame.get(), 888);
2216 AddTransferableResource(frame.get(), 888);
2217 AddTextureQuad(frame.get(), 777);
2218 AddTransferableResource(frame.get(), 777);
2219 SetFrameData(frame.Pass());
2222 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2224 unsigned expected[] = {555, 999};
2225 EXPECT_RESOURCES(expected, resources);
2226 EXPECT_TRUE(TestAndResetAvailable());
2229 // Destroy the layer.
2230 delegated_->RemoveFromParent();
2234 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2235 EXPECT_EQ(0u, resources.size());
2236 EXPECT_FALSE(TestAndResetAvailable());
2238 // Destroy the frame provider. Resources should be returned.
2239 frame_provider_ = NULL;
2241 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2243 unsigned expected[] = {777, 888};
2244 EXPECT_RESOURCES(expected, resources);
2245 EXPECT_TRUE(TestAndResetAvailable());
2252 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
2253 bool result) OVERRIDE {
2254 ReturnUnusedResourcesFromParent(host_impl);
2257 scoped_refptr<DelegatedRendererLayer> delegated_thief_;
2260 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestRemoveAndAddToTree);
2262 class LayerTreeHostDelegatedTestRemoveAndChangeResources
2263 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
2265 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
2267 virtual void DidCommitAndDrawFrame() OVERRIDE {
2268 scoped_ptr<DelegatedFrameData> frame;
2269 ReturnedResourceArray resources;
2271 int next_source_frame_number = layer_tree_host()->source_frame_number();
2272 switch (next_source_frame_number) {
2274 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2275 AddTextureQuad(frame.get(), 999);
2276 AddTransferableResource(frame.get(), 999);
2277 AddTextureQuad(frame.get(), 555);
2278 AddTransferableResource(frame.get(), 555);
2280 // Create a DelegatedRendererLayer using the frame.
2281 SetFrameData(frame.Pass());
2284 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2285 EXPECT_EQ(0u, resources.size());
2286 EXPECT_FALSE(TestAndResetAvailable());
2288 // Remove the layer from the tree. The resources should not be returned
2289 // since they are still on the main thread layer.
2290 delegated_->RemoveFromParent();
2293 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2294 EXPECT_EQ(0u, resources.size());
2295 EXPECT_FALSE(TestAndResetAvailable());
2297 // Set a new frame. Resources should be returned immediately.
2298 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2299 AddTextureQuad(frame.get(), 888);
2300 AddTransferableResource(frame.get(), 888);
2301 AddTextureQuad(frame.get(), 777);
2302 AddTransferableResource(frame.get(), 777);
2303 SetFrameData(frame.Pass());
2305 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2307 unsigned expected[] = {555, 999};
2308 EXPECT_RESOURCES(expected, resources);
2309 EXPECT_TRUE(TestAndResetAvailable());
2313 // Destroy the frame provider.
2314 frame_provider_ = NULL;
2316 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2317 EXPECT_EQ(0u, resources.size());
2318 EXPECT_FALSE(TestAndResetAvailable());
2320 // Destroy the layer. Resources should be returned.
2323 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2325 unsigned expected[] = {777, 888};
2326 EXPECT_RESOURCES(expected, resources);
2327 EXPECT_TRUE(TestAndResetAvailable());
2334 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
2335 bool result) OVERRIDE {
2336 ReturnUnusedResourcesFromParent(host_impl);
2339 scoped_refptr<DelegatedRendererLayer> delegated_thief_;
2342 SINGLE_AND_MULTI_THREAD_TEST_F(
2343 LayerTreeHostDelegatedTestRemoveAndChangeResources);