Change DtmfSenderHandler to handle events on the signaling thread.
[chromium-blink-merge.git] / cc / trees / layer_tree_host_unittest_delegated.cc
blob3aabeffd142388a4de581b2bd84d027bb9838576
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/trees/layer_tree_host.h"
7 #include <algorithm>
9 #include "base/bind.h"
10 #include "base/location.h"
11 #include "base/synchronization/waitable_event.h"
12 #include "base/threading/thread.h"
13 #include "base/time/time.h"
14 #include "cc/layers/delegated_frame_provider.h"
15 #include "cc/layers/delegated_frame_resource_collection.h"
16 #include "cc/layers/delegated_renderer_layer.h"
17 #include "cc/layers/delegated_renderer_layer_impl.h"
18 #include "cc/output/compositor_frame.h"
19 #include "cc/output/compositor_frame_ack.h"
20 #include "cc/output/delegated_frame_data.h"
21 #include "cc/quads/render_pass_draw_quad.h"
22 #include "cc/quads/shared_quad_state.h"
23 #include "cc/quads/texture_draw_quad.h"
24 #include "cc/resources/returned_resource.h"
25 #include "cc/test/fake_delegated_renderer_layer.h"
26 #include "cc/test/fake_delegated_renderer_layer_impl.h"
27 #include "cc/test/fake_output_surface.h"
28 #include "cc/test/layer_tree_test.h"
29 #include "cc/trees/layer_tree_impl.h"
30 #include "gpu/GLES2/gl2extchromium.h"
32 namespace cc {
33 namespace {
35 bool ReturnedResourceLower(const ReturnedResource& a,
36 const ReturnedResource& b) {
37 return a.id < b.id;
40 // Tests if the list of resources matches an expectation, modulo the order.
41 bool ResourcesMatch(ReturnedResourceArray actual,
42 unsigned* expected,
43 size_t expected_count) {
44 std::sort(actual.begin(), actual.end(), ReturnedResourceLower);
45 std::sort(expected, expected + expected_count);
46 size_t actual_index = 0;
48 // for each element of the expected array, count off one of the actual array
49 // (after checking it matches).
50 for (size_t expected_index = 0; expected_index < expected_count;
51 ++expected_index) {
52 EXPECT_LT(actual_index, actual.size());
53 if (actual_index >= actual.size())
54 return false;
55 EXPECT_EQ(actual[actual_index].id, expected[expected_index]);
56 if (actual[actual_index].id != expected[expected_index])
57 return false;
58 EXPECT_GT(actual[actual_index].count, 0);
59 if (actual[actual_index].count <= 0) {
60 return false;
61 } else {
62 --actual[actual_index].count;
63 if (actual[actual_index].count == 0)
64 ++actual_index;
67 EXPECT_EQ(actual_index, actual.size());
68 return actual_index == actual.size();
71 #define EXPECT_RESOURCES(expected, actual) \
72 EXPECT_TRUE(ResourcesMatch(actual, expected, arraysize(expected)));
74 // These tests deal with delegated renderer layers.
75 class LayerTreeHostDelegatedTest : public LayerTreeTest {
76 protected:
77 scoped_ptr<DelegatedFrameData> CreateFrameData(
78 const gfx::Rect& root_output_rect,
79 const gfx::Rect& root_damage_rect) {
80 scoped_ptr<DelegatedFrameData> frame(new DelegatedFrameData);
82 scoped_ptr<RenderPass> root_pass(RenderPass::Create());
83 root_pass->SetNew(RenderPassId(1, 1),
84 root_output_rect,
85 root_damage_rect,
86 gfx::Transform());
87 frame->render_pass_list.push_back(root_pass.Pass());
88 return frame.Pass();
91 scoped_ptr<DelegatedFrameData> CreateInvalidFrameData(
92 const gfx::Rect& root_output_rect,
93 const gfx::Rect& root_damage_rect) {
94 scoped_ptr<DelegatedFrameData> frame(new DelegatedFrameData);
96 scoped_ptr<RenderPass> root_pass(RenderPass::Create());
97 root_pass->SetNew(RenderPassId(1, 1),
98 root_output_rect,
99 root_damage_rect,
100 gfx::Transform());
102 SharedQuadState* shared_quad_state =
103 root_pass->CreateAndAppendSharedQuadState();
105 gfx::Rect rect = root_output_rect;
106 gfx::Rect opaque_rect = root_output_rect;
107 gfx::Rect visible_rect = root_output_rect;
108 // An invalid resource id! The resource isn't part of the frame.
109 unsigned resource_id = 5;
110 bool premultiplied_alpha = false;
111 gfx::PointF uv_top_left = gfx::PointF(0.f, 0.f);
112 gfx::PointF uv_bottom_right = gfx::PointF(1.f, 1.f);
113 SkColor background_color = 0;
114 float vertex_opacity[4] = {1.f, 1.f, 1.f, 1.f};
115 bool flipped = false;
117 TextureDrawQuad* invalid_draw_quad =
118 root_pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
119 invalid_draw_quad->SetNew(shared_quad_state,
120 rect,
121 opaque_rect,
122 visible_rect,
123 resource_id,
124 premultiplied_alpha,
125 uv_top_left,
126 uv_bottom_right,
127 background_color,
128 vertex_opacity,
129 flipped);
131 frame->render_pass_list.push_back(root_pass.Pass());
132 return frame.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 };
155 quad->SetNew(sqs,
156 gfx::Rect(0, 0, 10, 10),
157 gfx::Rect(0, 0, 10, 10),
158 gfx::Rect(0, 0, 10, 10),
159 resource_id,
160 false,
161 gfx::PointF(0.f, 0.f),
162 gfx::PointF(1.f, 1.f),
163 SK_ColorTRANSPARENT,
164 vertex_opacity,
165 false);
168 void AddRenderPass(DelegatedFrameData* frame,
169 RenderPassId id,
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());
178 pass->SetNew(id,
179 output_rect,
180 damage_rect,
181 gfx::Transform());
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>();
189 quad->SetNew(sqs,
190 output_rect,
191 output_rect,
194 gfx::Vector2dF(),
195 gfx::Size(),
196 filters,
197 gfx::Vector2dF(),
198 background_filters);
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);
205 return 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)
212 return;
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())
236 return;
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 {
248 public:
249 LayerTreeHostDelegatedTestCaseSingleDelegatedLayer()
250 : resource_collection_(new DelegatedFrameResourceCollection),
251 available_(false) {
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());
274 return;
277 if (delegated_.get()) {
278 delegated_->RemoveFromParent();
279 delegated_ = NULL;
280 frame_provider_ = NULL;
283 frame_provider_ = new DelegatedFrameProvider(resource_collection_.get(),
284 frame_data.Pass());
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);
297 return 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_;
307 available_ = false;
308 return available;
311 protected:
312 scoped_refptr<DelegatedFrameResourceCollection> resource_collection_;
313 scoped_refptr<DelegatedFrameProvider> frame_provider_;
314 scoped_refptr<Layer> root_;
315 scoped_refptr<DelegatedRendererLayer> delegated_;
316 bool available_;
319 class LayerTreeHostDelegatedTestCreateChildId
320 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
321 public:
322 LayerTreeHostDelegatedTestCreateChildId()
323 : LayerTreeHostDelegatedTestCaseSingleDelegatedLayer(),
324 num_activates_(0),
325 did_reset_child_id_(false) {}
327 void DidCommit() override {
328 if (TestEnded())
329 return;
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)
335 return;
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());
344 ++num_activates_;
345 switch (num_activates_) {
346 case 2:
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();
354 break;
355 case 3:
356 EXPECT_TRUE(delegated_impl->ChildId());
357 EXPECT_TRUE(did_reset_child_id_);
358 EndTest();
359 break;
363 void InitializedRendererOnThread(LayerTreeHostImpl* host_impl,
364 bool success) override {
365 EXPECT_TRUE(success);
367 if (num_activates_ < 2)
368 return;
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;
379 protected:
380 int num_activates_;
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 {
391 public:
392 LayerTreeHostDelegatedTestInvalidFrameAfterContextLost()
393 : num_activates_(0), num_output_surfaces_initialized_(0) {}
395 void DidCommit() override {
396 if (TestEnded())
397 return;
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_++)
407 return;
409 scoped_refptr<DelegatedRendererLayer> old_delegated = delegated_;
410 SetFrameData(
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)
420 return;
422 TestContextProvider* context_provider = static_cast<TestContextProvider*>(
423 host_impl->output_surface()->context_provider());
425 ++num_activates_;
426 switch (num_activates_) {
427 case 2:
428 context_provider->ContextGL()->LoseContextCHROMIUM(
429 GL_GUILTY_CONTEXT_RESET_ARB,
430 GL_INNOCENT_CONTEXT_RESET_ARB);
431 break;
432 case 3:
433 EndTest();
434 break;
438 void InitializedRendererOnThread(LayerTreeHostImpl* host_impl,
439 bool success) override {
440 EXPECT_TRUE(success);
442 if (num_activates_ < 2)
443 return;
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_);
459 protected:
460 int num_activates_;
461 int num_output_surfaces_initialized_;
464 SINGLE_AND_MULTI_THREAD_TEST_F(
465 LayerTreeHostDelegatedTestInvalidFrameAfterContextLost);
467 class LayerTreeHostDelegatedTestLayerUsesFrameDamage
468 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
469 public:
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) {
477 case 1:
478 // The first time the layer gets a frame the whole layer should be
479 // damaged.
480 SetFrameData(
481 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1)));
482 break;
483 case 2:
484 // A different frame size will damage the whole layer.
485 SetFrameData(
486 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(0, 0, 0, 0)));
487 break;
488 case 3:
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.
491 SetFrameData(
492 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(2, 2, 5, 5)));
493 SetFrameData(
494 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(7, 2, 5, 6)));
495 break;
496 case 4:
497 // Should create zero damage.
498 layer_tree_host()->SetNeedsCommit();
499 break;
500 case 5:
501 // Should damage the full viewport.
502 delegated_->SetBounds(gfx::Size(2, 2));
503 break;
504 case 6:
505 // Should create zero damage.
506 layer_tree_host()->SetNeedsCommit();
507 break;
508 case 7:
509 // Should damage the full layer, tho the frame size is not changing.
510 delegated_->SetBounds(gfx::Size(6, 6));
511 SetFrameData(
512 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(1, 1, 2, 2)));
513 break;
514 case 8:
515 // Should create zero damage.
516 layer_tree_host()->SetNeedsCommit();
517 break;
518 case 9:
519 // Should create zero damage.
520 layer_tree_host()->SetNeedsCommit();
521 break;
522 case 10:
523 // Changing the frame size damages the full layer.
524 SetFrameData(
525 CreateFrameData(gfx::Rect(0, 0, 5, 5), gfx::Rect(4, 4, 1, 1)));
526 break;
527 case 11:
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)));
531 break;
532 case 12:
533 // Should create gfx::Rect(1, 1, 2, 2) of damage.
534 SetFrameData(
535 CreateFrameData(gfx::Rect(0, 0, 5, 5), gfx::Rect(1, 1, 2, 2)));
536 break;
537 case 13:
538 // Should create zero damage.
539 layer_tree_host()->SetNeedsCommit();
540 break;
541 case 14:
542 // Moving the layer out of the tree and back in will damage the whole
543 // impl layer.
544 delegated_->RemoveFromParent();
545 layer_tree_host()->root_layer()->AddChild(delegated_);
546 break;
547 case 15:
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
550 // nothing more.
551 SetFrameData(
552 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(0, 0, 10, 10)));
553 SetFrameData(
554 CreateFrameData(gfx::Rect(0, 0, 5, 5), gfx::Rect(1, 1, 2, 2)));
555 break;
556 case 16:
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.
559 SetFrameData(
560 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(0, 0, 10, 10)));
561 SetFrameData(
562 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(0, 0, 0, 0)));
563 break;
564 case 17:
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.
571 SetFrameData(
572 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(4, 0, 1, 1)));
573 break;
574 case 18:
575 // Set another new frame, both layers should be damaged in the same
576 // ways.
577 SetFrameData(
578 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(3, 3, 1, 1)));
579 break;
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_)
590 return draw_result;
592 gfx::Rect damage_rect;
593 if (!frame->has_no_damage) {
594 damage_rect = frame->render_passes.back()->damage_rect;
595 } else {
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()) {
601 case 0:
602 // First frame is damaged because of viewport resize.
603 EXPECT_EQ(gfx::Rect(15, 15).ToString(), damage_rect.ToString());
604 break;
605 case 1:
606 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect.ToString());
607 break;
608 case 2:
609 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect.ToString());
610 break;
611 case 3:
612 EXPECT_EQ(gfx::Rect(2, 2, 8, 6).ToString(), damage_rect.ToString());
613 break;
614 case 4:
615 EXPECT_EQ(gfx::Rect().ToString(), damage_rect.ToString());
616 break;
617 case 5:
618 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect.ToString());
619 break;
620 case 6:
621 EXPECT_EQ(gfx::Rect().ToString(), damage_rect.ToString());
622 break;
623 case 7:
624 EXPECT_EQ(gfx::Rect(6, 6).ToString(), damage_rect.ToString());
625 break;
626 case 8:
627 EXPECT_EQ(gfx::Rect().ToString(), damage_rect.ToString());
628 break;
629 case 9:
630 EXPECT_EQ(gfx::Rect().ToString(), damage_rect.ToString());
631 break;
632 case 10:
633 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect.ToString());
634 break;
635 case 11:
636 EXPECT_EQ(gfx::Rect().ToString(), damage_rect.ToString());
637 break;
638 case 12:
639 EXPECT_EQ(gfx::Rect(1, 1, 2, 2).ToString(), damage_rect.ToString());
640 break;
641 case 13:
642 EXPECT_EQ(gfx::Rect().ToString(), damage_rect.ToString());
643 break;
644 case 14:
645 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect.ToString());
646 break;
647 case 15:
648 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect.ToString());
649 break;
650 case 16:
651 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect.ToString());
652 break;
653 case 17:
654 EXPECT_EQ(gfx::UnionRects(gfx::Rect(5, 0, 10, 10),
655 gfx::Rect(4, 0, 1, 1)).ToString(),
656 damage_rect.ToString());
657 break;
658 case 18:
659 EXPECT_EQ(gfx::Rect(3, 3, 6, 1).ToString(), damage_rect.ToString());
660 EndTest();
661 break;
664 return draw_result;
667 protected:
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 {
676 public:
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
697 // thread.
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));
728 EndTest();
732 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestMergeResources);
734 class LayerTreeHostDelegatedTestRemapResourcesInQuads
735 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
736 public:
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);
777 EndTest();
781 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestRemapResourcesInQuads);
783 class LayerTreeHostDelegatedTestReturnUnusedResources
784 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
785 public:
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) {
794 case 1:
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());
802 break;
803 case 2:
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());
816 break;
817 case 3:
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());
829 break;
830 case 4:
831 // Postpone collecting resources for a frame. They should still be there
832 // the next frame.
833 layer_tree_host()->SetNeedsCommit();
834 return;
835 case 5:
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());
844 EndTest();
845 break;
848 // Resources are never immediately released.
849 ReturnedResourceArray empty_resources;
850 resource_collection_->TakeUnusedResourcesForChildCompositor(
851 &empty_resources);
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 {
866 public:
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) {
875 case 1:
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());
885 break;
886 case 2:
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());
910 break;
911 case 3:
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());
920 EndTest();
921 break;
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 {
934 public:
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) {
943 case 1:
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());
953 break;
954 case 2:
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.
972 break;
973 case 3:
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
982 // frame.
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());
988 break;
992 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
993 if (host_impl->active_tree()->source_frame_number() != 3)
994 return;
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);
1019 EndTest();
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 {
1031 public:
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) {
1040 case 1:
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());
1050 break;
1051 case 2:
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.
1069 break;
1070 case 3:
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());
1091 break;
1092 case 4:
1093 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1094 EXPECT_EQ(0u, resources.size());
1095 EXPECT_FALSE(TestAndResetAvailable());
1096 EndTest();
1097 break;
1101 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
1102 if (host_impl->active_tree()->source_frame_number() != 3)
1103 return;
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 {
1148 public:
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) {
1157 case 1:
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());
1165 break;
1166 case 2:
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.
1185 break;
1186 case 3:
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());
1203 break;
1204 case 4:
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());
1213 EndTest();
1214 break;
1218 void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, bool result) override {
1219 if (host_impl->active_tree()->source_frame_number() < 1)
1220 return;
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()) {
1233 case 1: {
1234 // We have the first good frame with just 990 and 555 in it.
1235 // layer.
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);
1252 break;
1254 case 2: {
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
1265 // 555 in it.
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);
1274 break;
1276 case 3: {
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);
1289 break;
1295 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestBadFrame);
1297 class LayerTreeHostDelegatedTestUnnamedResource
1298 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1299 public:
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) {
1308 case 1:
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());
1315 break;
1316 case 2:
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());
1333 EndTest();
1334 break;
1338 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
1339 if (host_impl->active_tree()->source_frame_number() != 1)
1340 return;
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 {
1363 public:
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) {
1372 case 1:
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());
1386 break;
1387 case 2:
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());
1399 break;
1400 case 3:
1401 // The now unused resource 555 should be returned.
1402 resources.clear();
1403 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1405 unsigned expected[] = {555};
1406 EXPECT_RESOURCES(expected, resources);
1407 EXPECT_TRUE(TestAndResetAvailable());
1409 EndTest();
1410 break;
1414 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
1415 if (host_impl->active_tree()->source_frame_number() != 1)
1416 return;
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 {
1443 public:
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) {
1450 case 1:
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());
1458 break;
1459 case 2:
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
1465 // it present.
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());
1470 break;
1471 case 3:
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_);
1480 break;
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);
1511 EndTest();
1514 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
1515 if (host_impl->active_tree()->source_frame_number() < 1)
1516 return;
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()) {
1527 case 1: {
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.
1537 break;
1539 case 2: {
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),
1552 host_impl);
1553 break;
1555 case 3:
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));
1562 break;
1566 base::Closure receive_resource_on_thread_;
1569 SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(
1570 LayerTreeHostDelegatedTestResourceSentToParent);
1572 class LayerTreeHostDelegatedTestCommitWithoutTake
1573 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1574 public:
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) {
1587 case 1:
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());
1596 break;
1597 case 2:
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.
1609 break;
1610 case 3:
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());
1618 break;
1619 case 4:
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.
1633 break;
1634 case 5:
1635 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1637 unsigned expected[] = {555, 555, 555, 999};
1638 EXPECT_RESOURCES(expected, resources);
1639 EXPECT_TRUE(TestAndResetAvailable());
1642 EndTest();
1643 break;
1647 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
1648 if (host_impl->active_tree()->source_frame_number() < 1)
1649 return;
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()) {
1660 case 1:
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));
1670 break;
1671 case 2:
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));
1677 break;
1678 case 3:
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 {
1694 protected:
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 {
1710 ++activate_count_;
1713 void DidCommit() override {
1714 switch (layer_tree_host()->source_frame_number()) {
1715 case 1: {
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());
1723 break;
1725 case 2:
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();
1730 break;
1731 case 3:
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());
1736 break;
1740 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
1741 switch (host_impl->active_tree()->source_frame_number()) {
1742 case 0: {
1743 // The activate for the 1st frame should have happened before now.
1744 EXPECT_EQ(1, activate_count_);
1745 break;
1747 case 1: {
1748 // The activate for the 2nd frame should have happened before now.
1749 EXPECT_EQ(2, activate_count_);
1750 break;
1752 case 2: {
1753 // The activate to remove the layer should have happened before now.
1754 EXPECT_EQ(3, activate_count_);
1755 break;
1757 case 3: {
1758 NOTREACHED();
1759 break;
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)
1776 EndTest();
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 {
1788 public:
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) {
1797 case 1:
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());
1804 break;
1805 case 2:
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());
1814 break;
1815 case 3:
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());
1823 break;
1824 case 4:
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());
1833 EndTest();
1834 break;
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 {
1847 public:
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) {
1856 case 1:
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());
1863 break;
1864 case 2:
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();
1878 delegated_ = NULL;
1880 frame_provider_->SetFrameData(frame.Pass());
1881 delegated_ = CreateDelegatedLayer(frame_provider_.get());
1882 break;
1883 case 3:
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());
1891 break;
1892 case 4:
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());
1901 EndTest();
1902 break;
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 {
1916 public:
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) {
1925 case 1:
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());
1934 break;
1935 case 2:
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
1941 // alive.
1942 frame_provider_ = NULL;
1943 break;
1944 case 3:
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
1950 // returned yet.
1951 delegated_->RemoveFromParent();
1952 break;
1953 case 4:
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;
1963 break;
1964 case 5:
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();
1972 break;
1973 case 6:
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.
1979 delegated_ = NULL;
1981 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1983 unsigned expected[] = {555, 999};
1984 EXPECT_RESOURCES(expected, resources);
1985 EXPECT_TRUE(TestAndResetAvailable());
1987 EndTest();
1988 break;
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 {
2003 public:
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) {
2012 case 1:
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());
2021 break;
2022 case 2:
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();
2030 break;
2031 case 3:
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_);
2038 break;
2039 case 4:
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());
2051 break;
2052 case 5:
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();
2062 delegated_ = NULL;
2063 break;
2064 case 6:
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());
2078 EndTest();
2079 break;
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 {
2094 public:
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) {
2103 case 1:
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());
2112 break;
2113 case 2:
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();
2121 break;
2122 case 3:
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());
2140 resources.clear();
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.
2151 delegated_ = NULL;
2153 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2155 unsigned expected[] = {777, 888};
2156 EXPECT_RESOURCES(expected, resources);
2157 EXPECT_TRUE(TestAndResetAvailable());
2159 EndTest();
2160 break;
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);
2174 } // namespace
2175 } // namespace cc