Compare selection models in Textfield::ExecuteCommand.
[chromium-blink-merge.git] / cc / trees / layer_tree_host_unittest_delegated.cc
blob7ef3c1f81b85dd8741cce0f8e2b83d6c67b03944
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(RenderPass::Id(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(RenderPass::Id(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 scoped_ptr<TextureDrawQuad> invalid_draw_quad = TextureDrawQuad::Create();
118 invalid_draw_quad->SetNew(shared_quad_state,
119 rect,
120 opaque_rect,
121 visible_rect,
122 resource_id,
123 premultiplied_alpha,
124 uv_top_left,
125 uv_bottom_right,
126 background_color,
127 vertex_opacity,
128 flipped);
129 root_pass->quad_list.push_back(invalid_draw_quad.PassAs<DrawQuad>());
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 SharedQuadState* sqs =
151 frame->render_pass_list[0]->CreateAndAppendSharedQuadState();
152 scoped_ptr<TextureDrawQuad> quad = TextureDrawQuad::Create();
153 float vertex_opacity[4] = { 1.f, 1.f, 1.f, 1.f };
154 quad->SetNew(sqs,
155 gfx::Rect(0, 0, 10, 10),
156 gfx::Rect(0, 0, 10, 10),
157 gfx::Rect(0, 0, 10, 10),
158 resource_id,
159 false,
160 gfx::PointF(0.f, 0.f),
161 gfx::PointF(1.f, 1.f),
162 SK_ColorTRANSPARENT,
163 vertex_opacity,
164 false);
165 frame->render_pass_list[0]->quad_list.push_back(quad.PassAs<DrawQuad>());
168 void AddRenderPass(DelegatedFrameData* frame,
169 RenderPass::Id 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 SharedQuadState* sqs =
185 frame->render_pass_list[0]->CreateAndAppendSharedQuadState();
186 scoped_ptr<RenderPassDrawQuad> quad = RenderPassDrawQuad::Create();
188 quad->SetNew(sqs,
189 output_rect,
190 output_rect,
192 false, // is_replica
193 0, // mask_resource_id
194 damage_rect,
195 gfx::Rect(0, 0, 1, 1), // mask_uv_rect
196 filters,
197 background_filters);
198 frame->render_pass_list[0]->quad_list.push_back(quad.PassAs<DrawQuad>());
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 virtual void SetupTree() OVERRIDE {
256 root_ = Layer::Create();
257 root_->SetAnchorPoint(gfx::PointF());
258 root_->SetBounds(gfx::Size(15, 15));
260 layer_tree_host()->SetRootLayer(root_);
261 LayerTreeHostDelegatedTest::SetupTree();
264 virtual void BeginTest() OVERRIDE {
265 resource_collection_->SetClient(this);
266 PostSetNeedsCommitToMainThread();
269 void SetFrameData(scoped_ptr<DelegatedFrameData> frame_data) {
270 RenderPass* root_pass = frame_data->render_pass_list.back();
271 gfx::Size frame_size = root_pass->output_rect.size();
273 if (frame_provider_.get() && frame_size == frame_provider_->frame_size()) {
274 frame_provider_->SetFrameData(frame_data.Pass());
275 return;
278 if (delegated_.get()) {
279 delegated_->RemoveFromParent();
280 delegated_ = NULL;
281 frame_provider_ = NULL;
284 frame_provider_ = new DelegatedFrameProvider(resource_collection_.get(),
285 frame_data.Pass());
287 delegated_ = CreateDelegatedLayer(frame_provider_.get());
290 scoped_refptr<DelegatedRendererLayer> CreateDelegatedLayer(
291 DelegatedFrameProvider* frame_provider) {
292 scoped_refptr<DelegatedRendererLayer> delegated =
293 FakeDelegatedRendererLayer::Create(frame_provider);
294 delegated->SetAnchorPoint(gfx::PointF());
295 delegated->SetBounds(gfx::Size(10, 10));
296 delegated->SetIsDrawable(true);
298 root_->AddChild(delegated);
299 return delegated;
302 virtual void AfterTest() OVERRIDE { resource_collection_->SetClient(NULL); }
304 // DelegatedFrameProviderClient implementation.
305 virtual void UnusedResourcesAreAvailable() OVERRIDE { available_ = true; }
307 bool TestAndResetAvailable() {
308 bool available = available_;
309 available_ = false;
310 return available;
313 protected:
314 scoped_refptr<DelegatedFrameResourceCollection> resource_collection_;
315 scoped_refptr<DelegatedFrameProvider> frame_provider_;
316 scoped_refptr<Layer> root_;
317 scoped_refptr<DelegatedRendererLayer> delegated_;
318 bool available_;
321 class LayerTreeHostDelegatedTestCreateChildId
322 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
323 public:
324 LayerTreeHostDelegatedTestCreateChildId()
325 : LayerTreeHostDelegatedTestCaseSingleDelegatedLayer(),
326 num_activates_(0),
327 did_reset_child_id_(false) {}
329 virtual void DidCommit() OVERRIDE {
330 if (TestEnded())
331 return;
332 SetFrameData(CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1)));
335 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
336 if (host_impl->active_tree()->source_frame_number() < 1)
337 return;
339 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
340 FakeDelegatedRendererLayerImpl* delegated_impl =
341 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
343 TestContextProvider* context_provider = static_cast<TestContextProvider*>(
344 host_impl->output_surface()->context_provider().get());
346 ++num_activates_;
347 switch (num_activates_) {
348 case 2:
349 EXPECT_TRUE(delegated_impl->ChildId());
350 EXPECT_FALSE(did_reset_child_id_);
352 context_provider->ContextGL()->LoseContextCHROMIUM(
353 GL_GUILTY_CONTEXT_RESET_ARB,
354 GL_INNOCENT_CONTEXT_RESET_ARB);
355 context_provider->ContextGL()->Flush();
356 break;
357 case 3:
358 EXPECT_TRUE(delegated_impl->ChildId());
359 EXPECT_TRUE(did_reset_child_id_);
360 EndTest();
361 break;
365 virtual void InitializedRendererOnThread(LayerTreeHostImpl* host_impl,
366 bool success) OVERRIDE {
367 EXPECT_TRUE(success);
369 if (num_activates_ < 2)
370 return;
372 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
373 FakeDelegatedRendererLayerImpl* delegated_impl =
374 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
376 EXPECT_EQ(2, num_activates_);
377 EXPECT_FALSE(delegated_impl->ChildId());
378 did_reset_child_id_ = true;
381 protected:
382 int num_activates_;
383 bool did_reset_child_id_;
386 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestCreateChildId);
388 // Test that we can gracefully handle invalid frames after the context was lost.
389 // For example, we might be trying to use the previous frame in that case and
390 // have to make sure we don't crash because our resource accounting goes wrong.
391 class LayerTreeHostDelegatedTestInvalidFrameAfterContextLost
392 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
393 public:
394 LayerTreeHostDelegatedTestInvalidFrameAfterContextLost()
395 : num_activates_(0), num_output_surfaces_initialized_(0) {}
397 virtual void DidCommit() OVERRIDE {
398 if (TestEnded())
399 return;
400 scoped_ptr<DelegatedFrameData> frame1 =
401 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
402 AddTextureQuad(frame1.get(), 999);
403 AddTransferableResource(frame1.get(), 999);
404 SetFrameData(frame1.Pass());
407 virtual void DidInitializeOutputSurface() OVERRIDE {
408 if (!num_output_surfaces_initialized_++)
409 return;
411 scoped_refptr<DelegatedRendererLayer> old_delegated = delegated_;
412 SetFrameData(
413 CreateInvalidFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1)));
414 // Make sure we end up using the same layer, or we won't test the right
415 // thing, which is to make sure we can handle an invalid frame when using
416 // a stale layer from before the context was lost.
417 DCHECK(delegated_.get() == old_delegated.get());
420 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
421 if (host_impl->active_tree()->source_frame_number() < 1)
422 return;
424 TestContextProvider* context_provider = static_cast<TestContextProvider*>(
425 host_impl->output_surface()->context_provider().get());
427 ++num_activates_;
428 switch (num_activates_) {
429 case 2:
430 context_provider->ContextGL()->LoseContextCHROMIUM(
431 GL_GUILTY_CONTEXT_RESET_ARB,
432 GL_INNOCENT_CONTEXT_RESET_ARB);
433 break;
434 case 3:
435 EndTest();
436 break;
440 virtual void InitializedRendererOnThread(LayerTreeHostImpl* host_impl,
441 bool success) OVERRIDE {
442 EXPECT_TRUE(success);
444 if (num_activates_ < 2)
445 return;
447 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
448 FakeDelegatedRendererLayerImpl* delegated_impl =
449 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
451 EXPECT_EQ(2, num_activates_);
452 // Resources should have gotten cleared after the context was lost.
453 EXPECT_EQ(0U, delegated_impl->Resources().size());
456 virtual void AfterTest() OVERRIDE {
457 LayerTreeHostDelegatedTestCaseSingleDelegatedLayer::AfterTest();
458 EXPECT_EQ(2, num_output_surfaces_initialized_);
461 protected:
462 int num_activates_;
463 int num_output_surfaces_initialized_;
466 SINGLE_AND_MULTI_THREAD_TEST_F(
467 LayerTreeHostDelegatedTestInvalidFrameAfterContextLost);
469 class LayerTreeHostDelegatedTestLayerUsesFrameDamage
470 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
471 public:
472 LayerTreeHostDelegatedTestLayerUsesFrameDamage()
473 : LayerTreeHostDelegatedTestCaseSingleDelegatedLayer(),
474 first_draw_for_source_frame_(true) {}
476 virtual void DidCommit() OVERRIDE {
477 int next_source_frame_number = layer_tree_host()->source_frame_number();
478 switch (next_source_frame_number) {
479 case 1:
480 // The first time the layer gets a frame the whole layer should be
481 // damaged.
482 SetFrameData(
483 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1)));
484 break;
485 case 2:
486 // A different frame size will damage the whole layer.
487 SetFrameData(
488 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(0, 0, 0, 0)));
489 break;
490 case 3:
491 // Should create a total amount of gfx::Rect(2, 2, 10, 6) damage.
492 // The frame size is 20x20 while the layer is 10x10, so this should
493 // produce a gfx::Rect(1, 1, 5, 3) damage rect.
494 SetFrameData(
495 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(2, 2, 5, 5)));
496 SetFrameData(
497 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(7, 2, 5, 6)));
498 break;
499 case 4:
500 // Should create zero damage.
501 layer_tree_host()->SetNeedsCommit();
502 break;
503 case 5:
504 // Should damage the full viewport.
505 delegated_->SetBounds(gfx::Size(2, 2));
506 break;
507 case 6:
508 // Should create zero damage.
509 layer_tree_host()->SetNeedsCommit();
510 break;
511 case 7:
512 // Should damage the full layer, tho the frame size is not changing.
513 delegated_->SetBounds(gfx::Size(6, 6));
514 SetFrameData(
515 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(1, 1, 2, 2)));
516 break;
517 case 8:
518 // Should create zero damage.
519 layer_tree_host()->SetNeedsCommit();
520 break;
521 case 9:
522 // Should damage the full layer.
523 delegated_->SetDisplaySize(gfx::Size(10, 10));
524 break;
525 case 10:
526 // Should create zero damage.
527 layer_tree_host()->SetNeedsCommit();
528 break;
529 case 11:
530 // Changing the frame size damages the full layer.
531 SetFrameData(
532 CreateFrameData(gfx::Rect(0, 0, 5, 5), gfx::Rect(4, 4, 1, 1)));
533 break;
534 case 12:
535 // An invalid frame isn't used, so it should not cause damage.
536 SetFrameData(CreateInvalidFrameData(gfx::Rect(0, 0, 5, 5),
537 gfx::Rect(4, 4, 1, 1)));
538 break;
539 case 13:
540 // Should create gfx::Rect(1, 1, 2, 2) of damage. The frame size is
541 // 5x5 and the display size is now set to 10x10, so this should result
542 // in a gfx::Rect(2, 2, 4, 4) damage rect.
543 SetFrameData(
544 CreateFrameData(gfx::Rect(0, 0, 5, 5), gfx::Rect(1, 1, 2, 2)));
545 break;
546 case 14:
547 // Should create zero damage.
548 layer_tree_host()->SetNeedsCommit();
549 break;
550 case 15:
551 // Moving the layer out of the tree and back in will damage the whole
552 // impl layer.
553 delegated_->RemoveFromParent();
554 layer_tree_host()->root_layer()->AddChild(delegated_);
555 break;
556 case 16:
557 // Make a larger frame with lots of damage. Then a frame smaller than
558 // the first frame's damage. The entire layer should be damaged, but
559 // nothing more.
560 SetFrameData(
561 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(0, 0, 10, 10)));
562 SetFrameData(
563 CreateFrameData(gfx::Rect(0, 0, 5, 5), gfx::Rect(1, 1, 2, 2)));
564 break;
565 case 17:
566 // Make a frame with lots of damage. Then replace it with a frame with
567 // no damage. The entire layer should be damaged, but nothing more.
568 SetFrameData(
569 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(0, 0, 10, 10)));
570 SetFrameData(
571 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(0, 0, 0, 0)));
572 break;
573 case 18:
574 // Make another layer that uses the same frame provider. The new layer
575 // should be damaged.
576 delegated_copy_ = CreateDelegatedLayer(frame_provider_);
577 delegated_copy_->SetPosition(gfx::Point(5, 0));
579 // Also set a new frame.
580 SetFrameData(
581 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(4, 0, 1, 1)));
582 break;
583 case 19:
584 // Set another new frame, both layers should be damaged in the same
585 // ways.
586 SetFrameData(
587 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(3, 3, 1, 1)));
589 first_draw_for_source_frame_ = true;
592 virtual DrawResult PrepareToDrawOnThread(
593 LayerTreeHostImpl* host_impl,
594 LayerTreeHostImpl::FrameData* frame,
595 DrawResult draw_result) OVERRIDE {
596 EXPECT_EQ(DRAW_SUCCESS, draw_result);
598 if (!first_draw_for_source_frame_)
599 return draw_result;
601 gfx::Rect damage_rect;
602 if (!frame->has_no_damage) {
603 damage_rect = frame->render_passes.back()->damage_rect;
604 } else {
605 // If there is no damage, then we have no render passes to send.
606 EXPECT_TRUE(frame->render_passes.empty());
609 switch (host_impl->active_tree()->source_frame_number()) {
610 case 0:
611 // First frame is damaged because of viewport resize.
612 EXPECT_EQ(gfx::Rect(15, 15).ToString(), damage_rect.ToString());
613 break;
614 case 1:
615 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect.ToString());
616 break;
617 case 2:
618 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect.ToString());
619 break;
620 case 3:
621 EXPECT_EQ(gfx::Rect(1, 1, 5, 3).ToString(), damage_rect.ToString());
622 break;
623 case 4:
624 EXPECT_EQ(gfx::Rect().ToString(), damage_rect.ToString());
625 break;
626 case 5:
627 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect.ToString());
628 break;
629 case 6:
630 EXPECT_EQ(gfx::Rect().ToString(), damage_rect.ToString());
631 break;
632 case 7:
633 EXPECT_EQ(gfx::Rect(6, 6).ToString(), damage_rect.ToString());
634 break;
635 case 8:
636 EXPECT_EQ(gfx::Rect().ToString(), damage_rect.ToString());
637 break;
638 case 9:
639 EXPECT_EQ(gfx::Rect(6, 6).ToString(), damage_rect.ToString());
640 break;
641 case 10:
642 EXPECT_EQ(gfx::Rect().ToString(), damage_rect.ToString());
643 break;
644 case 11:
645 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect.ToString());
646 break;
647 case 12:
648 EXPECT_EQ(gfx::Rect().ToString(), damage_rect.ToString());
649 break;
650 case 13:
651 EXPECT_EQ(gfx::Rect(2, 2, 4, 4).ToString(), damage_rect.ToString());
652 break;
653 case 14:
654 EXPECT_EQ(gfx::Rect().ToString(), damage_rect.ToString());
655 break;
656 case 15:
657 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect.ToString());
658 break;
659 case 16:
660 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect.ToString());
661 break;
662 case 17:
663 EXPECT_EQ(gfx::Rect(10, 10).ToString(), damage_rect.ToString());
664 break;
665 case 18:
666 EXPECT_EQ(gfx::UnionRects(gfx::Rect(5, 0, 10, 10),
667 gfx::Rect(4, 0, 1, 1)).ToString(),
668 damage_rect.ToString());
669 break;
670 case 19:
671 EXPECT_EQ(gfx::Rect(3, 3, 6, 1).ToString(), damage_rect.ToString());
672 EndTest();
673 break;
676 return draw_result;
679 protected:
680 scoped_refptr<DelegatedRendererLayer> delegated_copy_;
681 bool first_draw_for_source_frame_;
684 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestLayerUsesFrameDamage);
686 class LayerTreeHostDelegatedTestMergeResources
687 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
688 public:
689 virtual void BeginTest() OVERRIDE {
690 // Push two frames to the delegated renderer layer with no commit between.
692 // The first frame has resource 999.
693 scoped_ptr<DelegatedFrameData> frame1 =
694 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
695 AddTextureQuad(frame1.get(), 999);
696 AddTransferableResource(frame1.get(), 999);
697 SetFrameData(frame1.Pass());
699 // The second frame uses resource 999 still, but also adds 555.
700 scoped_ptr<DelegatedFrameData> frame2 =
701 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
702 AddTextureQuad(frame2.get(), 999);
703 AddTransferableResource(frame2.get(), 999);
704 AddTextureQuad(frame2.get(), 555);
705 AddTransferableResource(frame2.get(), 555);
706 SetFrameData(frame2.Pass());
708 // The resource 999 from frame1 is returned since it is still on the main
709 // thread.
710 ReturnedResourceArray returned_resources;
711 resource_collection_->TakeUnusedResourcesForChildCompositor(
712 &returned_resources);
714 unsigned expected[] = {999};
715 EXPECT_RESOURCES(expected, returned_resources);
716 EXPECT_TRUE(TestAndResetAvailable());
719 PostSetNeedsCommitToMainThread();
722 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
723 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
724 FakeDelegatedRendererLayerImpl* delegated_impl =
725 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
727 const ResourceProvider::ResourceIdMap& map =
728 host_impl->resource_provider()->GetChildToParentMap(
729 delegated_impl->ChildId());
731 // Both frames' resources should be in the parent's resource provider.
732 EXPECT_EQ(2u, map.size());
733 EXPECT_EQ(1u, map.count(999));
734 EXPECT_EQ(1u, map.count(555));
736 EXPECT_EQ(2u, delegated_impl->Resources().size());
737 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
738 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
740 EndTest();
744 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestMergeResources);
746 class LayerTreeHostDelegatedTestRemapResourcesInQuads
747 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
748 public:
749 virtual void BeginTest() OVERRIDE {
750 // Generate a frame with two resources in it.
751 scoped_ptr<DelegatedFrameData> frame =
752 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
753 AddTextureQuad(frame.get(), 999);
754 AddTransferableResource(frame.get(), 999);
755 AddTextureQuad(frame.get(), 555);
756 AddTransferableResource(frame.get(), 555);
757 SetFrameData(frame.Pass());
759 PostSetNeedsCommitToMainThread();
762 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
763 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
764 FakeDelegatedRendererLayerImpl* delegated_impl =
765 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
767 const ResourceProvider::ResourceIdMap& map =
768 host_impl->resource_provider()->GetChildToParentMap(
769 delegated_impl->ChildId());
771 // The frame's resource should be in the parent's resource provider.
772 EXPECT_EQ(2u, map.size());
773 EXPECT_EQ(1u, map.count(999));
774 EXPECT_EQ(1u, map.count(555));
776 ResourceProvider::ResourceId parent_resource_id1 = map.find(999)->second;
777 EXPECT_NE(parent_resource_id1, 999u);
778 ResourceProvider::ResourceId parent_resource_id2 = map.find(555)->second;
779 EXPECT_NE(parent_resource_id2, 555u);
781 // The resources in the quads should be remapped to the parent's namespace.
782 const TextureDrawQuad* quad1 = TextureDrawQuad::MaterialCast(
783 delegated_impl->RenderPassesInDrawOrder()[0]->quad_list[0]);
784 EXPECT_EQ(parent_resource_id1, quad1->resource_id);
785 const TextureDrawQuad* quad2 = TextureDrawQuad::MaterialCast(
786 delegated_impl->RenderPassesInDrawOrder()[0]->quad_list[1]);
787 EXPECT_EQ(parent_resource_id2, quad2->resource_id);
789 EndTest();
793 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestRemapResourcesInQuads);
795 class LayerTreeHostDelegatedTestReturnUnusedResources
796 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
797 public:
798 virtual void BeginTest() OVERRIDE {
799 PostSetNeedsCommitToMainThread();
802 virtual void DidCommitAndDrawFrame() OVERRIDE {
803 scoped_ptr<DelegatedFrameData> frame;
804 ReturnedResourceArray resources;
806 int next_source_frame_number = layer_tree_host()->source_frame_number();
807 switch (next_source_frame_number) {
808 case 1:
809 // Generate a frame with two resources in it.
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(), 555);
814 AddTransferableResource(frame.get(), 555);
815 SetFrameData(frame.Pass());
816 break;
817 case 2:
818 // All of the resources are in use.
819 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
820 EXPECT_EQ(0u, resources.size());
821 EXPECT_FALSE(TestAndResetAvailable());
823 // Keep using 999 but stop using 555.
824 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
825 AddTextureQuad(frame.get(), 999);
826 AddTransferableResource(frame.get(), 999);
827 AddTextureQuad(frame.get(), 444);
828 AddTransferableResource(frame.get(), 444);
829 SetFrameData(frame.Pass());
830 break;
831 case 3:
832 // 555 is no longer in use.
833 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
835 unsigned expected[] = {555};
836 EXPECT_RESOURCES(expected, resources);
837 EXPECT_TRUE(TestAndResetAvailable());
840 // Stop using any resources.
841 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
842 SetFrameData(frame.Pass());
843 break;
844 case 4:
845 // Postpone collecting resources for a frame. They should still be there
846 // the next frame.
847 layer_tree_host()->SetNeedsCommit();
848 return;
849 case 5:
850 // 444 and 999 are no longer in use. We sent two refs to 999, so we
851 // should get two back.
852 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
854 unsigned expected[] = {444, 999, 999};
855 EXPECT_RESOURCES(expected, resources);
856 EXPECT_TRUE(TestAndResetAvailable());
858 EndTest();
859 break;
862 // Resources are never immediately released.
863 ReturnedResourceArray empty_resources;
864 resource_collection_->TakeUnusedResourcesForChildCompositor(
865 &empty_resources);
866 EXPECT_EQ(0u, empty_resources.size());
867 EXPECT_FALSE(TestAndResetAvailable());
870 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
871 bool result) OVERRIDE {
872 ReturnUnusedResourcesFromParent(host_impl);
876 SINGLE_AND_MULTI_THREAD_TEST_F(
877 LayerTreeHostDelegatedTestReturnUnusedResources);
879 class LayerTreeHostDelegatedTestReusedResources
880 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
881 public:
882 virtual void BeginTest() OVERRIDE {
883 PostSetNeedsCommitToMainThread();
886 virtual void DidCommitAndDrawFrame() OVERRIDE {
887 scoped_ptr<DelegatedFrameData> frame;
888 ReturnedResourceArray resources;
890 int next_source_frame_number = layer_tree_host()->source_frame_number();
891 switch (next_source_frame_number) {
892 case 1:
893 // Generate a frame with some resources in it.
894 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
895 AddTextureQuad(frame.get(), 999);
896 AddTransferableResource(frame.get(), 999);
897 AddTextureQuad(frame.get(), 555);
898 AddTransferableResource(frame.get(), 555);
899 AddTextureQuad(frame.get(), 444);
900 AddTransferableResource(frame.get(), 444);
901 SetFrameData(frame.Pass());
902 break;
903 case 2:
904 // All of the resources are in use.
905 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
906 EXPECT_EQ(0u, resources.size());
907 EXPECT_FALSE(TestAndResetAvailable());
909 // Keep using 999 but stop using 555 and 444.
910 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
911 AddTextureQuad(frame.get(), 999);
912 AddTransferableResource(frame.get(), 999);
913 SetFrameData(frame.Pass());
915 // Resource are not immediately released.
916 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
917 EXPECT_EQ(0u, resources.size());
918 EXPECT_FALSE(TestAndResetAvailable());
920 // Now using 555 and 444 again, but not 999.
921 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
922 AddTextureQuad(frame.get(), 555);
923 AddTransferableResource(frame.get(), 555);
924 AddTextureQuad(frame.get(), 444);
925 AddTransferableResource(frame.get(), 444);
926 SetFrameData(frame.Pass());
927 break;
928 case 3:
929 // The 999 resource is the only unused one. Two references were sent, so
930 // two should be returned.
931 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
933 unsigned expected[] = {999, 999};
934 EXPECT_RESOURCES(expected, resources);
935 EXPECT_TRUE(TestAndResetAvailable());
937 EndTest();
938 break;
942 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
943 bool result) OVERRIDE {
944 ReturnUnusedResourcesFromParent(host_impl);
948 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestReusedResources);
950 class LayerTreeHostDelegatedTestFrameBeforeAck
951 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
952 public:
953 virtual void BeginTest() OVERRIDE {
954 PostSetNeedsCommitToMainThread();
957 virtual void DidCommitAndDrawFrame() OVERRIDE {
958 scoped_ptr<DelegatedFrameData> frame;
959 ReturnedResourceArray resources;
961 int next_source_frame_number = layer_tree_host()->source_frame_number();
962 switch (next_source_frame_number) {
963 case 1:
964 // Generate a frame with some resources in it.
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(), 555);
969 AddTransferableResource(frame.get(), 555);
970 AddTextureQuad(frame.get(), 444);
971 AddTransferableResource(frame.get(), 444);
972 SetFrameData(frame.Pass());
973 break;
974 case 2:
975 // All of the resources are in use.
976 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
977 EXPECT_EQ(0u, resources.size());
978 EXPECT_FALSE(TestAndResetAvailable());
980 // Keep using 999 but stop using 555 and 444.
981 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
982 AddTextureQuad(frame.get(), 999);
983 AddTransferableResource(frame.get(), 999);
984 SetFrameData(frame.Pass());
986 // Resource are not immediately released.
987 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
988 EXPECT_EQ(0u, resources.size());
989 EXPECT_FALSE(TestAndResetAvailable());
991 // The parent compositor (this one) does a commit.
992 break;
993 case 3:
994 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
996 unsigned expected[] = {444, 555};
997 EXPECT_RESOURCES(expected, resources);
998 EXPECT_TRUE(TestAndResetAvailable());
1001 // The child compositor sends a frame referring to resources not in the
1002 // frame.
1003 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1004 AddTextureQuad(frame.get(), 999);
1005 AddTextureQuad(frame.get(), 555);
1006 AddTextureQuad(frame.get(), 444);
1007 SetFrameData(frame.Pass());
1008 break;
1012 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
1013 if (host_impl->active_tree()->source_frame_number() != 3)
1014 return;
1016 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1017 FakeDelegatedRendererLayerImpl* delegated_impl =
1018 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1020 const ResourceProvider::ResourceIdMap& map =
1021 host_impl->resource_provider()->GetChildToParentMap(
1022 delegated_impl->ChildId());
1024 // The bad frame should be dropped. So we should only have one quad (the
1025 // one with resource 999) on the impl tree. And only 999 will be present
1026 // in the parent's resource provider.
1027 EXPECT_EQ(1u, map.size());
1028 EXPECT_EQ(1u, map.count(999));
1030 EXPECT_EQ(1u, delegated_impl->Resources().size());
1031 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1033 const RenderPass* pass = delegated_impl->RenderPassesInDrawOrder()[0];
1034 EXPECT_EQ(1u, pass->quad_list.size());
1035 const TextureDrawQuad* quad = TextureDrawQuad::MaterialCast(
1036 pass->quad_list[0]);
1037 EXPECT_EQ(map.find(999)->second, quad->resource_id);
1039 EndTest();
1042 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
1043 bool result) OVERRIDE {
1044 ReturnUnusedResourcesFromParent(host_impl);
1048 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestFrameBeforeAck);
1050 class LayerTreeHostDelegatedTestFrameBeforeTakeResources
1051 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1052 public:
1053 virtual void BeginTest() OVERRIDE {
1054 PostSetNeedsCommitToMainThread();
1057 virtual void DidCommitAndDrawFrame() OVERRIDE {
1058 scoped_ptr<DelegatedFrameData> frame;
1059 ReturnedResourceArray resources;
1061 int next_source_frame_number = layer_tree_host()->source_frame_number();
1062 switch (next_source_frame_number) {
1063 case 1:
1064 // Generate a frame with some resources in it.
1065 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1066 AddTextureQuad(frame.get(), 999);
1067 AddTransferableResource(frame.get(), 999);
1068 AddTextureQuad(frame.get(), 555);
1069 AddTransferableResource(frame.get(), 555);
1070 AddTextureQuad(frame.get(), 444);
1071 AddTransferableResource(frame.get(), 444);
1072 SetFrameData(frame.Pass());
1073 break;
1074 case 2:
1075 // All of the resources are in use.
1076 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1077 EXPECT_EQ(0u, resources.size());
1078 EXPECT_FALSE(TestAndResetAvailable());
1080 // Keep using 999 but stop using 555 and 444.
1081 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1082 AddTextureQuad(frame.get(), 999);
1083 AddTransferableResource(frame.get(), 999);
1084 SetFrameData(frame.Pass());
1086 // Resource are not immediately released.
1087 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1088 EXPECT_EQ(0u, resources.size());
1089 EXPECT_FALSE(TestAndResetAvailable());
1091 // The parent compositor (this one) does a commit.
1092 break;
1093 case 3:
1094 // The child compositor sends a frame before taking resources back
1095 // from the previous commit. This frame makes use of the resources 555
1096 // and 444, which were just released during commit.
1097 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1098 AddTextureQuad(frame.get(), 999);
1099 AddTransferableResource(frame.get(), 999);
1100 AddTextureQuad(frame.get(), 555);
1101 AddTransferableResource(frame.get(), 555);
1102 AddTextureQuad(frame.get(), 444);
1103 AddTransferableResource(frame.get(), 444);
1104 SetFrameData(frame.Pass());
1106 // The resources are used by the new frame but are returned anyway since
1107 // we passed them again.
1108 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1110 unsigned expected[] = {444, 555};
1111 EXPECT_RESOURCES(expected, resources);
1112 EXPECT_TRUE(TestAndResetAvailable());
1114 break;
1115 case 4:
1116 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1117 EXPECT_EQ(0u, resources.size());
1118 EXPECT_FALSE(TestAndResetAvailable());
1119 EndTest();
1120 break;
1124 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
1125 if (host_impl->active_tree()->source_frame_number() != 3)
1126 return;
1128 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1129 FakeDelegatedRendererLayerImpl* delegated_impl =
1130 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1132 const ResourceProvider::ResourceIdMap& map =
1133 host_impl->resource_provider()->GetChildToParentMap(
1134 delegated_impl->ChildId());
1136 // The third frame has all of the resources in it again, the delegated
1137 // renderer layer should continue to own the resources for it.
1138 EXPECT_EQ(3u, map.size());
1139 EXPECT_EQ(1u, map.count(999));
1140 EXPECT_EQ(1u, map.count(555));
1141 EXPECT_EQ(1u, map.count(444));
1143 EXPECT_EQ(3u, delegated_impl->Resources().size());
1144 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1145 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1146 EXPECT_EQ(1u, delegated_impl->Resources().count(444));
1148 const RenderPass* pass = delegated_impl->RenderPassesInDrawOrder()[0];
1149 EXPECT_EQ(3u, pass->quad_list.size());
1150 const TextureDrawQuad* quad1 = TextureDrawQuad::MaterialCast(
1151 pass->quad_list[0]);
1152 EXPECT_EQ(map.find(999)->second, quad1->resource_id);
1153 const TextureDrawQuad* quad2 = TextureDrawQuad::MaterialCast(
1154 pass->quad_list[1]);
1155 EXPECT_EQ(map.find(555)->second, quad2->resource_id);
1156 const TextureDrawQuad* quad3 = TextureDrawQuad::MaterialCast(
1157 pass->quad_list[2]);
1158 EXPECT_EQ(map.find(444)->second, quad3->resource_id);
1161 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
1162 bool result) OVERRIDE {
1163 ReturnUnusedResourcesFromParent(host_impl);
1167 SINGLE_AND_MULTI_THREAD_TEST_F(
1168 LayerTreeHostDelegatedTestFrameBeforeTakeResources);
1170 class LayerTreeHostDelegatedTestBadFrame
1171 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1172 public:
1173 virtual void BeginTest() OVERRIDE {
1174 PostSetNeedsCommitToMainThread();
1177 virtual void DidCommitAndDrawFrame() OVERRIDE {
1178 scoped_ptr<DelegatedFrameData> frame;
1179 ReturnedResourceArray resources;
1181 int next_source_frame_number = layer_tree_host()->source_frame_number();
1182 switch (next_source_frame_number) {
1183 case 1:
1184 // Generate a frame with some resources in it.
1185 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1186 AddTextureQuad(frame.get(), 999);
1187 AddTransferableResource(frame.get(), 999);
1188 AddTextureQuad(frame.get(), 555);
1189 AddTransferableResource(frame.get(), 555);
1190 SetFrameData(frame.Pass());
1191 break;
1192 case 2:
1193 // All of the resources are in use.
1194 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1195 EXPECT_EQ(0u, resources.size());
1196 EXPECT_FALSE(TestAndResetAvailable());
1198 // Generate a bad frame with a resource the layer doesn't have. The
1199 // 885 and 775 resources are unknown, while ownership of the legit 444
1200 // resource is passed in here. The bad frame does not use any of the
1201 // previous resources, 999 or 555.
1202 // A bad quad is present both before and after the good quad.
1203 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1204 AddTextureQuad(frame.get(), 885);
1205 AddTextureQuad(frame.get(), 444);
1206 AddTransferableResource(frame.get(), 444);
1207 AddTextureQuad(frame.get(), 775);
1208 SetFrameData(frame.Pass());
1210 // The parent compositor (this one) does a commit.
1211 break;
1212 case 3:
1213 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1214 EXPECT_EQ(0u, resources.size());
1215 EXPECT_FALSE(TestAndResetAvailable());
1217 // Now send a good frame with 999 again.
1218 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1219 AddTextureQuad(frame.get(), 999);
1220 SetFrameData(frame.Pass());
1222 // The bad frame's resource is given back to the child compositor.
1223 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1225 unsigned expected[] = {444};
1226 EXPECT_RESOURCES(expected, resources);
1227 EXPECT_TRUE(TestAndResetAvailable());
1229 break;
1230 case 4:
1231 // The unused 555 from the last good frame is now released.
1232 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1234 unsigned expected[] = {555};
1235 EXPECT_RESOURCES(expected, resources);
1236 EXPECT_TRUE(TestAndResetAvailable());
1239 EndTest();
1240 break;
1244 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
1245 bool result) OVERRIDE {
1246 if (host_impl->active_tree()->source_frame_number() < 1)
1247 return;
1249 ReturnUnusedResourcesFromParent(host_impl);
1251 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1252 FakeDelegatedRendererLayerImpl* delegated_impl =
1253 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1255 const ResourceProvider::ResourceIdMap& map =
1256 host_impl->resource_provider()->GetChildToParentMap(
1257 delegated_impl->ChildId());
1259 switch (host_impl->active_tree()->source_frame_number()) {
1260 case 1: {
1261 // We have the first good frame with just 990 and 555 in it.
1262 // layer.
1263 EXPECT_EQ(2u, map.size());
1264 EXPECT_EQ(1u, map.count(999));
1265 EXPECT_EQ(1u, map.count(555));
1267 EXPECT_EQ(2u, delegated_impl->Resources().size());
1268 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1269 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1271 const RenderPass* pass = delegated_impl->RenderPassesInDrawOrder()[0];
1272 EXPECT_EQ(2u, pass->quad_list.size());
1273 const TextureDrawQuad* quad1 = TextureDrawQuad::MaterialCast(
1274 pass->quad_list[0]);
1275 EXPECT_EQ(map.find(999)->second, quad1->resource_id);
1276 const TextureDrawQuad* quad2 = TextureDrawQuad::MaterialCast(
1277 pass->quad_list[1]);
1278 EXPECT_EQ(map.find(555)->second, quad2->resource_id);
1279 break;
1281 case 2: {
1282 // We only keep resources from the last valid frame.
1283 EXPECT_EQ(2u, map.size());
1284 EXPECT_EQ(1u, map.count(999));
1285 EXPECT_EQ(1u, map.count(555));
1287 EXPECT_EQ(2u, delegated_impl->Resources().size());
1288 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1289 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1291 // The bad frame is dropped though, we still have the frame with 999 and
1292 // 555 in it.
1293 const RenderPass* pass = delegated_impl->RenderPassesInDrawOrder()[0];
1294 EXPECT_EQ(2u, pass->quad_list.size());
1295 const TextureDrawQuad* quad1 = TextureDrawQuad::MaterialCast(
1296 pass->quad_list[0]);
1297 EXPECT_EQ(map.find(999)->second, quad1->resource_id);
1298 const TextureDrawQuad* quad2 = TextureDrawQuad::MaterialCast(
1299 pass->quad_list[1]);
1300 EXPECT_EQ(map.find(555)->second, quad2->resource_id);
1301 break;
1303 case 3: {
1304 // We have the new good frame with just 999 in it.
1305 EXPECT_EQ(1u, map.size());
1306 EXPECT_EQ(1u, map.count(999));
1308 EXPECT_EQ(1u, delegated_impl->Resources().size());
1309 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1311 const RenderPass* pass = delegated_impl->RenderPassesInDrawOrder()[0];
1312 EXPECT_EQ(1u, pass->quad_list.size());
1313 const TextureDrawQuad* quad1 = TextureDrawQuad::MaterialCast(
1314 pass->quad_list[0]);
1315 EXPECT_EQ(map.find(999)->second, quad1->resource_id);
1316 break;
1322 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestBadFrame);
1324 class LayerTreeHostDelegatedTestUnnamedResource
1325 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1326 public:
1327 virtual void BeginTest() OVERRIDE {
1328 PostSetNeedsCommitToMainThread();
1331 virtual void DidCommit() OVERRIDE {
1332 scoped_ptr<DelegatedFrameData> frame;
1333 ReturnedResourceArray resources;
1335 int next_source_frame_number = layer_tree_host()->source_frame_number();
1336 switch (next_source_frame_number) {
1337 case 1:
1338 // This frame includes two resources in it, but only uses one.
1339 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1340 AddTransferableResource(frame.get(), 999);
1341 AddTextureQuad(frame.get(), 555);
1342 AddTransferableResource(frame.get(), 555);
1343 SetFrameData(frame.Pass());
1344 break;
1345 case 2:
1346 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1347 EXPECT_EQ(0u, resources.size());
1348 EXPECT_FALSE(TestAndResetAvailable());
1350 // Now send an empty frame.
1351 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1352 SetFrameData(frame.Pass());
1354 // The unused resource should be returned.
1355 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1357 unsigned expected[] = {999};
1358 EXPECT_RESOURCES(expected, resources);
1359 EXPECT_TRUE(TestAndResetAvailable());
1362 EndTest();
1363 break;
1367 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
1368 if (host_impl->active_tree()->source_frame_number() != 1)
1369 return;
1371 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1372 FakeDelegatedRendererLayerImpl* delegated_impl =
1373 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1375 const ResourceProvider::ResourceIdMap& map =
1376 host_impl->resource_provider()->GetChildToParentMap(
1377 delegated_impl->ChildId());
1379 // The layer only held on to the resource that was used.
1380 EXPECT_EQ(1u, map.size());
1381 EXPECT_EQ(1u, map.count(555));
1383 EXPECT_EQ(1u, delegated_impl->Resources().size());
1384 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1388 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestUnnamedResource);
1390 class LayerTreeHostDelegatedTestDontLeakResource
1391 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1392 public:
1393 virtual void BeginTest() OVERRIDE {
1394 PostSetNeedsCommitToMainThread();
1397 virtual void DidCommitAndDrawFrame() OVERRIDE {
1398 scoped_ptr<DelegatedFrameData> frame;
1399 ReturnedResourceArray resources;
1401 int next_source_frame_number = layer_tree_host()->source_frame_number();
1402 switch (next_source_frame_number) {
1403 case 1:
1404 // This frame includes two resources in it.
1405 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1406 AddTextureQuad(frame.get(), 999);
1407 AddTransferableResource(frame.get(), 999);
1408 AddTextureQuad(frame.get(), 555);
1409 AddTransferableResource(frame.get(), 555);
1410 SetFrameData(frame.Pass());
1412 // But then we immediately stop using 999.
1413 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1414 AddTextureQuad(frame.get(), 555);
1415 AddTransferableResource(frame.get(), 555);
1416 SetFrameData(frame.Pass());
1417 break;
1418 case 2:
1419 // The unused resources should be returned. 555 is still used, but it's
1420 // returned once to account for the first frame.
1421 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1423 unsigned expected[] = {555, 999};
1424 EXPECT_RESOURCES(expected, resources);
1425 EXPECT_TRUE(TestAndResetAvailable());
1427 // Send a frame with no resources in it.
1428 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1429 SetFrameData(frame.Pass());
1430 break;
1431 case 3:
1432 // The now unused resource 555 should be returned.
1433 resources.clear();
1434 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1436 unsigned expected[] = {555};
1437 EXPECT_RESOURCES(expected, resources);
1438 EXPECT_TRUE(TestAndResetAvailable());
1440 EndTest();
1441 break;
1445 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
1446 if (host_impl->active_tree()->source_frame_number() != 1)
1447 return;
1449 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1450 FakeDelegatedRendererLayerImpl* delegated_impl =
1451 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1453 const ResourceProvider::ResourceIdMap& map =
1454 host_impl->resource_provider()->GetChildToParentMap(
1455 delegated_impl->ChildId());
1457 // The layer only held on to the resource that was used.
1458 EXPECT_EQ(1u, map.size());
1459 EXPECT_EQ(1u, map.count(555));
1461 EXPECT_EQ(1u, delegated_impl->Resources().size());
1462 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1465 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
1466 bool result) OVERRIDE {
1467 ReturnUnusedResourcesFromParent(host_impl);
1471 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestDontLeakResource);
1473 class LayerTreeHostDelegatedTestResourceSentToParent
1474 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1475 public:
1476 virtual void DidCommitAndDrawFrame() OVERRIDE {
1477 scoped_ptr<DelegatedFrameData> frame;
1478 ReturnedResourceArray resources;
1480 int next_source_frame_number = layer_tree_host()->source_frame_number();
1481 switch (next_source_frame_number) {
1482 case 1:
1483 // This frame includes two resources in it.
1484 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1485 AddTextureQuad(frame.get(), 999);
1486 AddTransferableResource(frame.get(), 999);
1487 AddTextureQuad(frame.get(), 555);
1488 AddTransferableResource(frame.get(), 555);
1489 SetFrameData(frame.Pass());
1490 break;
1491 case 2:
1492 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1493 EXPECT_EQ(0u, resources.size());
1494 EXPECT_FALSE(TestAndResetAvailable());
1496 // 999 is in use in the grandparent compositor, generate a frame without
1497 // it present.
1498 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1499 AddTextureQuad(frame.get(), 555);
1500 AddTransferableResource(frame.get(), 555);
1501 SetFrameData(frame.Pass());
1502 break;
1503 case 3:
1504 // Since 999 is in the grandparent it is not returned.
1505 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1506 EXPECT_EQ(0u, resources.size());
1507 EXPECT_FALSE(TestAndResetAvailable());
1509 // The impl side will get back the resource at some point.
1510 ImplThreadTaskRunner()->PostTask(FROM_HERE,
1511 receive_resource_on_thread_);
1512 break;
1516 void ReceiveResourceOnThread(LayerTreeHostImpl* host_impl) {
1517 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1518 FakeDelegatedRendererLayerImpl* delegated_impl =
1519 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1521 const ResourceProvider::ResourceIdMap& map =
1522 host_impl->resource_provider()->GetChildToParentMap(
1523 delegated_impl->ChildId());
1525 // Receive 999 back from the grandparent.
1526 CompositorFrameAck ack;
1527 output_surface()->ReturnResource(map.find(999)->second, &ack);
1528 host_impl->ReclaimResources(&ack);
1531 virtual void UnusedResourcesAreAvailable() OVERRIDE {
1532 EXPECT_EQ(3, layer_tree_host()->source_frame_number());
1534 ReturnedResourceArray resources;
1536 // 999 was returned from the grandparent and could be released.
1537 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1539 unsigned expected[] = {999};
1540 EXPECT_RESOURCES(expected, resources);
1543 EndTest();
1546 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
1547 if (host_impl->active_tree()->source_frame_number() < 1)
1548 return;
1550 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1551 FakeDelegatedRendererLayerImpl* delegated_impl =
1552 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1554 const ResourceProvider::ResourceIdMap& map =
1555 host_impl->resource_provider()->GetChildToParentMap(
1556 delegated_impl->ChildId());
1558 switch (host_impl->active_tree()->source_frame_number()) {
1559 case 1: {
1560 EXPECT_EQ(2u, map.size());
1561 EXPECT_EQ(1u, map.count(999));
1562 EXPECT_EQ(1u, map.count(555));
1564 EXPECT_EQ(2u, delegated_impl->Resources().size());
1565 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1566 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1568 // The 999 resource will be sent to a grandparent compositor.
1569 break;
1571 case 2: {
1572 EXPECT_EQ(2u, map.size());
1573 EXPECT_EQ(1u, map.count(999));
1574 EXPECT_EQ(1u, map.count(555));
1576 // 999 is in the parent, so not held by delegated renderer layer.
1577 EXPECT_EQ(1u, delegated_impl->Resources().size());
1578 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1580 receive_resource_on_thread_ =
1581 base::Bind(&LayerTreeHostDelegatedTestResourceSentToParent::
1582 ReceiveResourceOnThread,
1583 base::Unretained(this),
1584 host_impl);
1585 break;
1587 case 3:
1588 // 999 should be released.
1589 EXPECT_EQ(1u, map.size());
1590 EXPECT_EQ(1u, map.count(555));
1592 EXPECT_EQ(1u, delegated_impl->Resources().size());
1593 EXPECT_EQ(1u, delegated_impl->Resources().count(map.find(555)->second));
1594 break;
1598 base::Closure receive_resource_on_thread_;
1601 SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(
1602 LayerTreeHostDelegatedTestResourceSentToParent);
1604 class LayerTreeHostDelegatedTestCommitWithoutTake
1605 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1606 public:
1607 virtual void BeginTest() OVERRIDE {
1608 // Prevent drawing with resources that are sent to the grandparent.
1609 layer_tree_host()->SetViewportSize(gfx::Size());
1610 PostSetNeedsCommitToMainThread();
1613 virtual void DidCommit() OVERRIDE {
1614 scoped_ptr<DelegatedFrameData> frame;
1615 ReturnedResourceArray resources;
1617 int next_source_frame_number = layer_tree_host()->source_frame_number();
1618 switch (next_source_frame_number) {
1619 case 1:
1620 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1621 AddTextureQuad(frame.get(), 999);
1622 AddTransferableResource(frame.get(), 999);
1623 AddTextureQuad(frame.get(), 555);
1624 AddTransferableResource(frame.get(), 555);
1625 AddTextureQuad(frame.get(), 444);
1626 AddTransferableResource(frame.get(), 444);
1627 SetFrameData(frame.Pass());
1628 break;
1629 case 2:
1630 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1631 EXPECT_EQ(0u, resources.size());
1632 EXPECT_FALSE(TestAndResetAvailable());
1634 // Stop using 999 and 444 in this frame and commit.
1635 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1636 AddTextureQuad(frame.get(), 555);
1637 AddTransferableResource(frame.get(), 555);
1638 SetFrameData(frame.Pass());
1639 // 999 and 444 will be returned for frame 1, but not 555 since it's in
1640 // the current frame.
1641 break;
1642 case 3:
1643 // Don't take resources here, but set a new frame that uses 999 again.
1644 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1645 AddTextureQuad(frame.get(), 999);
1646 AddTransferableResource(frame.get(), 999);
1647 AddTextureQuad(frame.get(), 555);
1648 AddTransferableResource(frame.get(), 555);
1649 SetFrameData(frame.Pass());
1650 break;
1651 case 4:
1652 // 555 from frame 1 and 2 isn't returned since it's still in use. 999
1653 // from frame 1 is returned though.
1654 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1656 unsigned expected[] = {444, 999};
1657 EXPECT_RESOURCES(expected, resources);
1658 EXPECT_TRUE(TestAndResetAvailable());
1661 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1662 SetFrameData(frame.Pass());
1663 // 555 will be returned 3 times for frames 1 2 and 3, and 999 will be
1664 // returned once for frame 3.
1665 break;
1666 case 5:
1667 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1669 unsigned expected[] = {555, 555, 555, 999};
1670 EXPECT_RESOURCES(expected, resources);
1671 EXPECT_TRUE(TestAndResetAvailable());
1674 EndTest();
1675 break;
1679 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
1680 if (host_impl->active_tree()->source_frame_number() < 1)
1681 return;
1683 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1684 FakeDelegatedRendererLayerImpl* delegated_impl =
1685 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1687 const ResourceProvider::ResourceIdMap& map =
1688 host_impl->resource_provider()->GetChildToParentMap(
1689 delegated_impl->ChildId());
1691 switch (host_impl->active_tree()->source_frame_number()) {
1692 case 1:
1693 EXPECT_EQ(3u, map.size());
1694 EXPECT_EQ(1u, map.count(999));
1695 EXPECT_EQ(1u, map.count(555));
1696 EXPECT_EQ(1u, map.count(444));
1698 EXPECT_EQ(3u, delegated_impl->Resources().size());
1699 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1700 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1701 EXPECT_EQ(1u, delegated_impl->Resources().count(444));
1702 break;
1703 case 2:
1704 EXPECT_EQ(1u, map.size());
1705 EXPECT_EQ(1u, map.count(555));
1707 EXPECT_EQ(1u, delegated_impl->Resources().size());
1708 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1709 break;
1710 case 3:
1711 EXPECT_EQ(2u, map.size());
1712 EXPECT_EQ(1u, map.count(999));
1713 EXPECT_EQ(1u, map.count(555));
1715 EXPECT_EQ(2u, delegated_impl->Resources().size());
1716 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1717 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1722 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestCommitWithoutTake);
1724 class DelegatedFrameIsActivatedDuringCommit
1725 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1726 protected:
1727 DelegatedFrameIsActivatedDuringCommit() : returned_resource_count_(0) {}
1729 virtual void BeginTest() OVERRIDE {
1730 activate_count_ = 0;
1732 scoped_ptr<DelegatedFrameData> frame =
1733 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1734 AddTextureQuad(frame.get(), 999);
1735 AddTransferableResource(frame.get(), 999);
1736 SetFrameData(frame.Pass());
1738 PostSetNeedsCommitToMainThread();
1741 virtual void WillActivateTreeOnThread(LayerTreeHostImpl* impl) OVERRIDE {
1742 ++activate_count_;
1745 virtual void DidCommit() OVERRIDE {
1746 switch (layer_tree_host()->source_frame_number()) {
1747 case 1: {
1748 // The first frame has been activated. Set a new frame, and
1749 // expect the next commit to finish *after* it is activated.
1750 scoped_ptr<DelegatedFrameData> frame =
1751 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1752 AddTextureQuad(frame.get(), 555);
1753 AddTransferableResource(frame.get(), 555);
1754 SetFrameData(frame.Pass());
1755 break;
1757 case 2:
1758 // The second frame has been activated. Remove the layer from
1759 // the tree to cause another commit/activation. The commit should
1760 // finish *after* the layer is removed from the active tree.
1761 delegated_->RemoveFromParent();
1762 break;
1763 case 3:
1764 // Finish the test by releasing resources on the next frame.
1765 scoped_ptr<DelegatedFrameData> frame =
1766 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1767 SetFrameData(frame.Pass());
1768 break;
1772 virtual void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
1773 switch (host_impl->active_tree()->source_frame_number()) {
1774 case 2: {
1775 // The activate for the 2nd frame should have happened before now.
1776 EXPECT_EQ(2, activate_count_);
1777 break;
1779 case 3: {
1780 // The activate to remove the layer should have happened before now.
1781 EXPECT_EQ(3, activate_count_);
1782 break;
1787 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
1788 bool result) OVERRIDE {
1789 ReturnUnusedResourcesFromParent(host_impl);
1792 virtual void UnusedResourcesAreAvailable() OVERRIDE {
1793 LayerTreeHostDelegatedTestCaseSingleDelegatedLayer::
1794 UnusedResourcesAreAvailable();
1795 ReturnedResourceArray resources;
1796 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1797 EXPECT_TRUE(TestAndResetAvailable());
1798 returned_resource_count_ += resources.size();
1799 if (returned_resource_count_ == 2)
1800 EndTest();
1803 int activate_count_;
1804 size_t returned_resource_count_;
1807 SINGLE_AND_MULTI_THREAD_TEST_F(
1808 DelegatedFrameIsActivatedDuringCommit);
1810 class LayerTreeHostDelegatedTestTwoImplLayers
1811 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1812 public:
1813 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
1815 virtual void DidCommitAndDrawFrame() OVERRIDE {
1816 scoped_ptr<DelegatedFrameData> frame;
1817 ReturnedResourceArray resources;
1819 int next_source_frame_number = layer_tree_host()->source_frame_number();
1820 switch (next_source_frame_number) {
1821 case 1:
1822 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1823 AddTextureQuad(frame.get(), 999);
1824 AddTransferableResource(frame.get(), 999);
1825 AddTextureQuad(frame.get(), 555);
1826 AddTransferableResource(frame.get(), 555);
1827 SetFrameData(frame.Pass());
1828 break;
1829 case 2:
1830 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1831 EXPECT_EQ(0u, resources.size());
1832 EXPECT_FALSE(TestAndResetAvailable());
1834 // Remove the delegated layer and replace it with a new one. Use the
1835 // same frame and resources for it.
1836 delegated_->RemoveFromParent();
1837 delegated_ = CreateDelegatedLayer(frame_provider_.get());
1838 break;
1839 case 3:
1840 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1841 EXPECT_EQ(0u, resources.size());
1842 EXPECT_FALSE(TestAndResetAvailable());
1844 // Use a frame with no resources in it.
1845 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1846 SetFrameData(frame.Pass());
1847 break;
1848 case 4:
1849 // We gave one frame to the frame provider, so we should get one
1850 // ref back for each resource.
1851 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1853 unsigned expected[] = {555, 999};
1854 EXPECT_RESOURCES(expected, resources);
1855 EXPECT_TRUE(TestAndResetAvailable());
1857 EndTest();
1858 break;
1862 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
1863 bool result) OVERRIDE {
1864 ReturnUnusedResourcesFromParent(host_impl);
1868 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestTwoImplLayers);
1870 class LayerTreeHostDelegatedTestTwoImplLayersTwoFrames
1871 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1872 public:
1873 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
1875 virtual void DidCommitAndDrawFrame() OVERRIDE {
1876 scoped_ptr<DelegatedFrameData> frame;
1877 ReturnedResourceArray resources;
1879 int next_source_frame_number = layer_tree_host()->source_frame_number();
1880 switch (next_source_frame_number) {
1881 case 1:
1882 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1883 AddTextureQuad(frame.get(), 999);
1884 AddTransferableResource(frame.get(), 999);
1885 AddTextureQuad(frame.get(), 555);
1886 AddTransferableResource(frame.get(), 555);
1887 SetFrameData(frame.Pass());
1888 break;
1889 case 2:
1890 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1891 EXPECT_EQ(0u, resources.size());
1892 EXPECT_FALSE(TestAndResetAvailable());
1894 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1895 AddTextureQuad(frame.get(), 999);
1896 AddTransferableResource(frame.get(), 999);
1897 AddTextureQuad(frame.get(), 555);
1898 AddTransferableResource(frame.get(), 555);
1900 // Remove the delegated layer and replace it with a new one. Make a new
1901 // frame but with the same resources for it.
1902 delegated_->RemoveFromParent();
1903 delegated_ = NULL;
1905 frame_provider_->SetFrameData(frame.Pass());
1906 delegated_ = CreateDelegatedLayer(frame_provider_.get());
1907 break;
1908 case 3:
1909 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1910 EXPECT_EQ(0u, resources.size());
1911 EXPECT_FALSE(TestAndResetAvailable());
1913 // Use a frame with no resources in it.
1914 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1915 SetFrameData(frame.Pass());
1916 break;
1917 case 4:
1918 // We gave two frames to the frame provider, so we should get two
1919 // refs back for each resource.
1920 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1922 unsigned expected[] = {555, 555, 999, 999};
1923 EXPECT_RESOURCES(expected, resources);
1924 EXPECT_TRUE(TestAndResetAvailable());
1926 EndTest();
1927 break;
1931 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
1932 bool result) OVERRIDE {
1933 ReturnUnusedResourcesFromParent(host_impl);
1937 SINGLE_AND_MULTI_THREAD_TEST_F(
1938 LayerTreeHostDelegatedTestTwoImplLayersTwoFrames);
1940 class LayerTreeHostDelegatedTestTwoLayers
1941 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1942 public:
1943 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
1945 virtual void DidCommitAndDrawFrame() OVERRIDE {
1946 scoped_ptr<DelegatedFrameData> frame;
1947 ReturnedResourceArray resources;
1949 int next_source_frame_number = layer_tree_host()->source_frame_number();
1950 switch (next_source_frame_number) {
1951 case 1:
1952 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1953 AddTextureQuad(frame.get(), 999);
1954 AddTransferableResource(frame.get(), 999);
1955 AddTextureQuad(frame.get(), 555);
1956 AddTransferableResource(frame.get(), 555);
1958 // Create a DelegatedRendererLayer using the frame.
1959 SetFrameData(frame.Pass());
1960 break;
1961 case 2:
1962 // Create a second DelegatedRendererLayer using the same frame provider.
1963 delegated_thief_ = CreateDelegatedLayer(frame_provider_.get());
1964 root_->AddChild(delegated_thief_);
1966 // And drop our ref on the frame provider so only the layers keep it
1967 // alive.
1968 frame_provider_ = NULL;
1969 break;
1970 case 3:
1971 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1972 EXPECT_EQ(0u, resources.size());
1973 EXPECT_FALSE(TestAndResetAvailable());
1975 // Remove one delegated layer from the tree. No resources should be
1976 // returned yet.
1977 delegated_->RemoveFromParent();
1978 break;
1979 case 4:
1980 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1981 EXPECT_EQ(0u, resources.size());
1982 EXPECT_FALSE(TestAndResetAvailable());
1984 // Put the first layer back, and remove the other layer and destroy it.
1985 // No resources should be returned yet.
1986 root_->AddChild(delegated_);
1987 delegated_thief_->RemoveFromParent();
1988 delegated_thief_ = NULL;
1989 break;
1990 case 5:
1991 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1992 EXPECT_EQ(0u, resources.size());
1993 EXPECT_FALSE(TestAndResetAvailable());
1995 // Remove the first layer from the tree again. The resources are still
1996 // held by the main thread layer.
1997 delegated_->RemoveFromParent();
1998 break;
1999 case 6:
2000 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2001 EXPECT_EQ(0u, resources.size());
2002 EXPECT_FALSE(TestAndResetAvailable());
2004 // Destroy the layer and the resources should be returned immediately.
2005 delegated_ = NULL;
2007 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2009 unsigned expected[] = {555, 999};
2010 EXPECT_RESOURCES(expected, resources);
2011 EXPECT_TRUE(TestAndResetAvailable());
2013 EndTest();
2014 break;
2018 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
2019 bool result) OVERRIDE {
2020 ReturnUnusedResourcesFromParent(host_impl);
2023 scoped_refptr<DelegatedRendererLayer> delegated_thief_;
2026 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestTwoLayers);
2028 class LayerTreeHostDelegatedTestRemoveAndAddToTree
2029 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
2030 public:
2031 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
2033 virtual void DidCommitAndDrawFrame() OVERRIDE {
2034 scoped_ptr<DelegatedFrameData> frame;
2035 ReturnedResourceArray resources;
2037 int next_source_frame_number = layer_tree_host()->source_frame_number();
2038 switch (next_source_frame_number) {
2039 case 1:
2040 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2041 AddTextureQuad(frame.get(), 999);
2042 AddTransferableResource(frame.get(), 999);
2043 AddTextureQuad(frame.get(), 555);
2044 AddTransferableResource(frame.get(), 555);
2046 // Create a DelegatedRendererLayer using the frame.
2047 SetFrameData(frame.Pass());
2048 break;
2049 case 2:
2050 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2051 EXPECT_EQ(0u, resources.size());
2052 EXPECT_FALSE(TestAndResetAvailable());
2054 // Remove the layer from the tree. The resources should not be returned
2055 // since they are still on the main thread layer.
2056 delegated_->RemoveFromParent();
2057 break;
2058 case 3:
2059 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2060 EXPECT_EQ(0u, resources.size());
2061 EXPECT_FALSE(TestAndResetAvailable());
2063 // Add the layer back to the tree.
2064 layer_tree_host()->root_layer()->AddChild(delegated_);
2065 break;
2066 case 4:
2067 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2068 EXPECT_EQ(0u, resources.size());
2069 EXPECT_FALSE(TestAndResetAvailable());
2071 // Set a new frame. Resources should be returned.
2072 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2073 AddTextureQuad(frame.get(), 888);
2074 AddTransferableResource(frame.get(), 888);
2075 AddTextureQuad(frame.get(), 777);
2076 AddTransferableResource(frame.get(), 777);
2077 SetFrameData(frame.Pass());
2078 break;
2079 case 5:
2080 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2082 unsigned expected[] = {555, 999};
2083 EXPECT_RESOURCES(expected, resources);
2084 EXPECT_TRUE(TestAndResetAvailable());
2087 // Destroy the layer.
2088 delegated_->RemoveFromParent();
2089 delegated_ = NULL;
2090 break;
2091 case 6:
2092 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2093 EXPECT_EQ(0u, resources.size());
2094 EXPECT_FALSE(TestAndResetAvailable());
2096 // Destroy the frame provider. Resources should be returned.
2097 frame_provider_ = NULL;
2099 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2101 unsigned expected[] = {777, 888};
2102 EXPECT_RESOURCES(expected, resources);
2103 EXPECT_TRUE(TestAndResetAvailable());
2105 EndTest();
2106 break;
2110 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
2111 bool result) OVERRIDE {
2112 ReturnUnusedResourcesFromParent(host_impl);
2115 scoped_refptr<DelegatedRendererLayer> delegated_thief_;
2118 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestRemoveAndAddToTree);
2120 class LayerTreeHostDelegatedTestRemoveAndChangeResources
2121 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
2122 public:
2123 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
2125 virtual void DidCommitAndDrawFrame() OVERRIDE {
2126 scoped_ptr<DelegatedFrameData> frame;
2127 ReturnedResourceArray resources;
2129 int next_source_frame_number = layer_tree_host()->source_frame_number();
2130 switch (next_source_frame_number) {
2131 case 1:
2132 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2133 AddTextureQuad(frame.get(), 999);
2134 AddTransferableResource(frame.get(), 999);
2135 AddTextureQuad(frame.get(), 555);
2136 AddTransferableResource(frame.get(), 555);
2138 // Create a DelegatedRendererLayer using the frame.
2139 SetFrameData(frame.Pass());
2140 break;
2141 case 2:
2142 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2143 EXPECT_EQ(0u, resources.size());
2144 EXPECT_FALSE(TestAndResetAvailable());
2146 // Remove the layer from the tree. The resources should not be returned
2147 // since they are still on the main thread layer.
2148 delegated_->RemoveFromParent();
2149 break;
2150 case 3:
2151 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2152 EXPECT_EQ(0u, resources.size());
2153 EXPECT_FALSE(TestAndResetAvailable());
2155 // Set a new frame. Resources should be returned immediately.
2156 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2157 AddTextureQuad(frame.get(), 888);
2158 AddTransferableResource(frame.get(), 888);
2159 AddTextureQuad(frame.get(), 777);
2160 AddTransferableResource(frame.get(), 777);
2161 SetFrameData(frame.Pass());
2163 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2165 unsigned expected[] = {555, 999};
2166 EXPECT_RESOURCES(expected, resources);
2167 EXPECT_TRUE(TestAndResetAvailable());
2168 resources.clear();
2171 // Destroy the frame provider.
2172 frame_provider_ = NULL;
2174 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2175 EXPECT_EQ(0u, resources.size());
2176 EXPECT_FALSE(TestAndResetAvailable());
2178 // Destroy the layer. Resources should be returned.
2179 delegated_ = NULL;
2181 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2183 unsigned expected[] = {777, 888};
2184 EXPECT_RESOURCES(expected, resources);
2185 EXPECT_TRUE(TestAndResetAvailable());
2187 EndTest();
2188 break;
2192 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
2193 bool result) OVERRIDE {
2194 ReturnUnusedResourcesFromParent(host_impl);
2197 scoped_refptr<DelegatedRendererLayer> delegated_thief_;
2200 SINGLE_AND_MULTI_THREAD_TEST_F(
2201 LayerTreeHostDelegatedTestRemoveAndChangeResources);
2203 } // namespace
2204 } // namespace cc