Provide core API permissions to extensions_unittests
[chromium-blink-merge.git] / cc / trees / layer_tree_host_unittest_delegated.cc
blob1607b71c886f16fbec9424f88ae0db44b7c00714
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 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 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 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,
193 0, // mask_resource_id
194 gfx::Rect(0, 0, 1, 1), // mask_uv_rect
195 filters,
196 gfx::Vector2dF(),
197 background_filters);
200 static ResourceProvider::ResourceId AppendResourceId(
201 std::vector<ResourceProvider::ResourceId>* resources_in_last_sent_frame,
202 ResourceProvider::ResourceId resource_id) {
203 resources_in_last_sent_frame->push_back(resource_id);
204 return resource_id;
207 void ReturnUnusedResourcesFromParent(LayerTreeHostImpl* host_impl) {
208 DelegatedFrameData* delegated_frame_data =
209 output_surface()->last_sent_frame().delegated_frame_data.get();
210 if (!delegated_frame_data)
211 return;
213 std::vector<ResourceProvider::ResourceId> resources_in_last_sent_frame;
214 for (size_t i = 0; i < delegated_frame_data->resource_list.size(); ++i) {
215 resources_in_last_sent_frame.push_back(
216 delegated_frame_data->resource_list[i].id);
219 std::vector<ResourceProvider::ResourceId> resources_to_return;
221 const TransferableResourceArray& resources_held_by_parent =
222 output_surface()->resources_held_by_parent();
223 for (size_t i = 0; i < resources_held_by_parent.size(); ++i) {
224 ResourceProvider::ResourceId resource_in_parent =
225 resources_held_by_parent[i].id;
226 bool resource_in_parent_is_not_part_of_frame =
227 std::find(resources_in_last_sent_frame.begin(),
228 resources_in_last_sent_frame.end(),
229 resource_in_parent) == resources_in_last_sent_frame.end();
230 if (resource_in_parent_is_not_part_of_frame)
231 resources_to_return.push_back(resource_in_parent);
234 if (resources_to_return.empty())
235 return;
237 CompositorFrameAck ack;
238 for (size_t i = 0; i < resources_to_return.size(); ++i)
239 output_surface()->ReturnResource(resources_to_return[i], &ack);
240 host_impl->ReclaimResources(&ack);
244 class LayerTreeHostDelegatedTestCaseSingleDelegatedLayer
245 : public LayerTreeHostDelegatedTest,
246 public DelegatedFrameResourceCollectionClient {
247 public:
248 LayerTreeHostDelegatedTestCaseSingleDelegatedLayer()
249 : resource_collection_(new DelegatedFrameResourceCollection),
250 available_(false) {
251 resource_collection_->SetClient(this);
254 virtual void SetupTree() OVERRIDE {
255 root_ = Layer::Create();
256 root_->SetBounds(gfx::Size(15, 15));
258 layer_tree_host()->SetRootLayer(root_);
259 LayerTreeHostDelegatedTest::SetupTree();
262 virtual void BeginTest() OVERRIDE {
263 resource_collection_->SetClient(this);
264 PostSetNeedsCommitToMainThread();
267 void SetFrameData(scoped_ptr<DelegatedFrameData> frame_data) {
268 RenderPass* root_pass = frame_data->render_pass_list.back();
269 gfx::Size frame_size = root_pass->output_rect.size();
271 if (frame_provider_.get() && frame_size == frame_provider_->frame_size()) {
272 frame_provider_->SetFrameData(frame_data.Pass());
273 return;
276 if (delegated_.get()) {
277 delegated_->RemoveFromParent();
278 delegated_ = NULL;
279 frame_provider_ = NULL;
282 frame_provider_ = new DelegatedFrameProvider(resource_collection_.get(),
283 frame_data.Pass());
285 delegated_ = CreateDelegatedLayer(frame_provider_.get());
288 scoped_refptr<DelegatedRendererLayer> CreateDelegatedLayer(
289 DelegatedFrameProvider* frame_provider) {
290 scoped_refptr<DelegatedRendererLayer> delegated =
291 FakeDelegatedRendererLayer::Create(frame_provider);
292 delegated->SetBounds(gfx::Size(10, 10));
293 delegated->SetIsDrawable(true);
295 root_->AddChild(delegated);
296 return delegated;
299 virtual void AfterTest() OVERRIDE { resource_collection_->SetClient(NULL); }
301 // DelegatedFrameProviderClient implementation.
302 virtual void UnusedResourcesAreAvailable() OVERRIDE { available_ = true; }
304 bool TestAndResetAvailable() {
305 bool available = available_;
306 available_ = false;
307 return available;
310 protected:
311 scoped_refptr<DelegatedFrameResourceCollection> resource_collection_;
312 scoped_refptr<DelegatedFrameProvider> frame_provider_;
313 scoped_refptr<Layer> root_;
314 scoped_refptr<DelegatedRendererLayer> delegated_;
315 bool available_;
318 class LayerTreeHostDelegatedTestCreateChildId
319 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
320 public:
321 LayerTreeHostDelegatedTestCreateChildId()
322 : LayerTreeHostDelegatedTestCaseSingleDelegatedLayer(),
323 num_activates_(0),
324 did_reset_child_id_(false) {}
326 virtual void DidCommit() OVERRIDE {
327 if (TestEnded())
328 return;
329 SetFrameData(CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1)));
332 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
333 if (host_impl->active_tree()->source_frame_number() < 1)
334 return;
336 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
337 FakeDelegatedRendererLayerImpl* delegated_impl =
338 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
340 TestContextProvider* context_provider = static_cast<TestContextProvider*>(
341 host_impl->output_surface()->context_provider().get());
343 ++num_activates_;
344 switch (num_activates_) {
345 case 2:
346 EXPECT_TRUE(delegated_impl->ChildId());
347 EXPECT_FALSE(did_reset_child_id_);
349 context_provider->ContextGL()->LoseContextCHROMIUM(
350 GL_GUILTY_CONTEXT_RESET_ARB,
351 GL_INNOCENT_CONTEXT_RESET_ARB);
352 context_provider->ContextGL()->Flush();
353 break;
354 case 3:
355 EXPECT_TRUE(delegated_impl->ChildId());
356 EXPECT_TRUE(did_reset_child_id_);
357 EndTest();
358 break;
362 virtual void InitializedRendererOnThread(LayerTreeHostImpl* host_impl,
363 bool success) OVERRIDE {
364 EXPECT_TRUE(success);
366 if (num_activates_ < 2)
367 return;
369 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
370 FakeDelegatedRendererLayerImpl* delegated_impl =
371 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
373 EXPECT_EQ(2, num_activates_);
374 EXPECT_FALSE(delegated_impl->ChildId());
375 did_reset_child_id_ = true;
378 protected:
379 int num_activates_;
380 bool did_reset_child_id_;
383 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestCreateChildId);
385 // Test that we can gracefully handle invalid frames after the context was lost.
386 // For example, we might be trying to use the previous frame in that case and
387 // have to make sure we don't crash because our resource accounting goes wrong.
388 class LayerTreeHostDelegatedTestInvalidFrameAfterContextLost
389 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
390 public:
391 LayerTreeHostDelegatedTestInvalidFrameAfterContextLost()
392 : num_activates_(0), num_output_surfaces_initialized_(0) {}
394 virtual void DidCommit() OVERRIDE {
395 if (TestEnded())
396 return;
397 scoped_ptr<DelegatedFrameData> frame1 =
398 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
399 AddTextureQuad(frame1.get(), 999);
400 AddTransferableResource(frame1.get(), 999);
401 SetFrameData(frame1.Pass());
404 virtual void DidInitializeOutputSurface() OVERRIDE {
405 if (!num_output_surfaces_initialized_++)
406 return;
408 scoped_refptr<DelegatedRendererLayer> old_delegated = delegated_;
409 SetFrameData(
410 CreateInvalidFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1)));
411 // Make sure we end up using the same layer, or we won't test the right
412 // thing, which is to make sure we can handle an invalid frame when using
413 // a stale layer from before the context was lost.
414 DCHECK(delegated_.get() == old_delegated.get());
417 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
418 if (host_impl->active_tree()->source_frame_number() < 1)
419 return;
421 TestContextProvider* context_provider = static_cast<TestContextProvider*>(
422 host_impl->output_surface()->context_provider().get());
424 ++num_activates_;
425 switch (num_activates_) {
426 case 2:
427 context_provider->ContextGL()->LoseContextCHROMIUM(
428 GL_GUILTY_CONTEXT_RESET_ARB,
429 GL_INNOCENT_CONTEXT_RESET_ARB);
430 break;
431 case 3:
432 EndTest();
433 break;
437 virtual void InitializedRendererOnThread(LayerTreeHostImpl* host_impl,
438 bool success) OVERRIDE {
439 EXPECT_TRUE(success);
441 if (num_activates_ < 2)
442 return;
444 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
445 FakeDelegatedRendererLayerImpl* delegated_impl =
446 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
448 EXPECT_EQ(2, num_activates_);
449 // Resources should have gotten cleared after the context was lost.
450 EXPECT_EQ(0U, delegated_impl->Resources().size());
453 virtual void AfterTest() OVERRIDE {
454 LayerTreeHostDelegatedTestCaseSingleDelegatedLayer::AfterTest();
455 EXPECT_EQ(2, num_output_surfaces_initialized_);
458 protected:
459 int num_activates_;
460 int num_output_surfaces_initialized_;
463 SINGLE_AND_MULTI_THREAD_TEST_F(
464 LayerTreeHostDelegatedTestInvalidFrameAfterContextLost);
466 class LayerTreeHostDelegatedTestLayerUsesFrameDamage
467 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
468 public:
469 LayerTreeHostDelegatedTestLayerUsesFrameDamage()
470 : LayerTreeHostDelegatedTestCaseSingleDelegatedLayer(),
471 first_draw_for_source_frame_(true) {}
473 virtual void DidCommit() OVERRIDE {
474 int next_source_frame_number = layer_tree_host()->source_frame_number();
475 switch (next_source_frame_number) {
476 case 1:
477 // The first time the layer gets a frame the whole layer should be
478 // damaged.
479 SetFrameData(
480 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1)));
481 break;
482 case 2:
483 // A different frame size will damage the whole layer.
484 SetFrameData(
485 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(0, 0, 0, 0)));
486 break;
487 case 3:
488 // Should create a total amount of gfx::Rect(2, 2, 8, 6) damage:
489 // (2, 2, 10, 6) clamped to the root output rect.
490 SetFrameData(
491 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(2, 2, 5, 5)));
492 SetFrameData(
493 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(7, 2, 5, 6)));
494 break;
495 case 4:
496 // Should create zero damage.
497 layer_tree_host()->SetNeedsCommit();
498 break;
499 case 5:
500 // Should damage the full viewport.
501 delegated_->SetBounds(gfx::Size(2, 2));
502 break;
503 case 6:
504 // Should create zero damage.
505 layer_tree_host()->SetNeedsCommit();
506 break;
507 case 7:
508 // Should damage the full layer, tho the frame size is not changing.
509 delegated_->SetBounds(gfx::Size(6, 6));
510 SetFrameData(
511 CreateFrameData(gfx::Rect(0, 0, 20, 20), gfx::Rect(1, 1, 2, 2)));
512 break;
513 case 8:
514 // Should create zero damage.
515 layer_tree_host()->SetNeedsCommit();
516 break;
517 case 9:
518 // Should create zero damage.
519 layer_tree_host()->SetNeedsCommit();
520 break;
521 case 10:
522 // Changing the frame size damages the full layer.
523 SetFrameData(
524 CreateFrameData(gfx::Rect(0, 0, 5, 5), gfx::Rect(4, 4, 1, 1)));
525 break;
526 case 11:
527 // An invalid frame isn't used, so it should not cause damage.
528 SetFrameData(CreateInvalidFrameData(gfx::Rect(0, 0, 5, 5),
529 gfx::Rect(4, 4, 1, 1)));
530 break;
531 case 12:
532 // Should create gfx::Rect(1, 1, 2, 2) of damage.
533 SetFrameData(
534 CreateFrameData(gfx::Rect(0, 0, 5, 5), gfx::Rect(1, 1, 2, 2)));
535 break;
536 case 13:
537 // Should create zero damage.
538 layer_tree_host()->SetNeedsCommit();
539 break;
540 case 14:
541 // Moving the layer out of the tree and back in will damage the whole
542 // impl layer.
543 delegated_->RemoveFromParent();
544 layer_tree_host()->root_layer()->AddChild(delegated_);
545 break;
546 case 15:
547 // Make a larger frame with lots of damage. Then a frame smaller than
548 // the first frame's damage. The entire layer should be damaged, but
549 // nothing more.
550 SetFrameData(
551 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(0, 0, 10, 10)));
552 SetFrameData(
553 CreateFrameData(gfx::Rect(0, 0, 5, 5), gfx::Rect(1, 1, 2, 2)));
554 break;
555 case 16:
556 // Make a frame with lots of damage. Then replace it with a frame with
557 // no damage. The entire layer should be damaged, but nothing more.
558 SetFrameData(
559 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(0, 0, 10, 10)));
560 SetFrameData(
561 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(0, 0, 0, 0)));
562 break;
563 case 17:
564 // Make another layer that uses the same frame provider. The new layer
565 // should be damaged.
566 delegated_copy_ = CreateDelegatedLayer(frame_provider_);
567 delegated_copy_->SetPosition(gfx::Point(5, 0));
569 // Also set a new frame.
570 SetFrameData(
571 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(4, 0, 1, 1)));
572 break;
573 case 18:
574 // Set another new frame, both layers should be damaged in the same
575 // ways.
576 SetFrameData(
577 CreateFrameData(gfx::Rect(0, 0, 10, 10), gfx::Rect(3, 3, 1, 1)));
578 break;
580 first_draw_for_source_frame_ = true;
583 virtual DrawResult PrepareToDrawOnThread(
584 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 virtual 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 virtual 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 virtual 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 virtual 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[0]);
772 EXPECT_EQ(parent_resource_id1, quad1->resource_id);
773 const TextureDrawQuad* quad2 = TextureDrawQuad::MaterialCast(
774 delegated_impl->RenderPassesInDrawOrder()[0]->quad_list[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 virtual void BeginTest() OVERRIDE {
787 PostSetNeedsCommitToMainThread();
790 virtual void DidCommitAndDrawFrame() OVERRIDE {
791 scoped_ptr<DelegatedFrameData> frame;
792 ReturnedResourceArray resources;
794 int next_source_frame_number = layer_tree_host()->source_frame_number();
795 switch (next_source_frame_number) {
796 case 1:
797 // Generate a frame with two resources in it.
798 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
799 AddTextureQuad(frame.get(), 999);
800 AddTransferableResource(frame.get(), 999);
801 AddTextureQuad(frame.get(), 555);
802 AddTransferableResource(frame.get(), 555);
803 SetFrameData(frame.Pass());
804 break;
805 case 2:
806 // All of the resources are in use.
807 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
808 EXPECT_EQ(0u, resources.size());
809 EXPECT_FALSE(TestAndResetAvailable());
811 // Keep using 999 but stop using 555.
812 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
813 AddTextureQuad(frame.get(), 999);
814 AddTransferableResource(frame.get(), 999);
815 AddTextureQuad(frame.get(), 444);
816 AddTransferableResource(frame.get(), 444);
817 SetFrameData(frame.Pass());
818 break;
819 case 3:
820 // 555 is no longer in use.
821 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
823 unsigned expected[] = {555};
824 EXPECT_RESOURCES(expected, resources);
825 EXPECT_TRUE(TestAndResetAvailable());
828 // Stop using any resources.
829 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
830 SetFrameData(frame.Pass());
831 break;
832 case 4:
833 // Postpone collecting resources for a frame. They should still be there
834 // the next frame.
835 layer_tree_host()->SetNeedsCommit();
836 return;
837 case 5:
838 // 444 and 999 are no longer in use. We sent two refs to 999, so we
839 // should get two back.
840 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
842 unsigned expected[] = {444, 999, 999};
843 EXPECT_RESOURCES(expected, resources);
844 EXPECT_TRUE(TestAndResetAvailable());
846 EndTest();
847 break;
850 // Resources are never immediately released.
851 ReturnedResourceArray empty_resources;
852 resource_collection_->TakeUnusedResourcesForChildCompositor(
853 &empty_resources);
854 EXPECT_EQ(0u, empty_resources.size());
855 EXPECT_FALSE(TestAndResetAvailable());
858 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
859 bool result) OVERRIDE {
860 ReturnUnusedResourcesFromParent(host_impl);
864 SINGLE_AND_MULTI_THREAD_TEST_F(
865 LayerTreeHostDelegatedTestReturnUnusedResources);
867 class LayerTreeHostDelegatedTestReusedResources
868 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
869 public:
870 virtual void BeginTest() OVERRIDE {
871 PostSetNeedsCommitToMainThread();
874 virtual void DidCommitAndDrawFrame() OVERRIDE {
875 scoped_ptr<DelegatedFrameData> frame;
876 ReturnedResourceArray resources;
878 int next_source_frame_number = layer_tree_host()->source_frame_number();
879 switch (next_source_frame_number) {
880 case 1:
881 // Generate a frame with some resources in it.
882 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
883 AddTextureQuad(frame.get(), 999);
884 AddTransferableResource(frame.get(), 999);
885 AddTextureQuad(frame.get(), 555);
886 AddTransferableResource(frame.get(), 555);
887 AddTextureQuad(frame.get(), 444);
888 AddTransferableResource(frame.get(), 444);
889 SetFrameData(frame.Pass());
890 break;
891 case 2:
892 // All of the resources are in use.
893 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
894 EXPECT_EQ(0u, resources.size());
895 EXPECT_FALSE(TestAndResetAvailable());
897 // Keep using 999 but stop using 555 and 444.
898 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
899 AddTextureQuad(frame.get(), 999);
900 AddTransferableResource(frame.get(), 999);
901 SetFrameData(frame.Pass());
903 // Resource are not immediately released.
904 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
905 EXPECT_EQ(0u, resources.size());
906 EXPECT_FALSE(TestAndResetAvailable());
908 // Now using 555 and 444 again, but not 999.
909 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
910 AddTextureQuad(frame.get(), 555);
911 AddTransferableResource(frame.get(), 555);
912 AddTextureQuad(frame.get(), 444);
913 AddTransferableResource(frame.get(), 444);
914 SetFrameData(frame.Pass());
915 break;
916 case 3:
917 // The 999 resource is the only unused one. Two references were sent, so
918 // two should be returned.
919 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
921 unsigned expected[] = {999, 999};
922 EXPECT_RESOURCES(expected, resources);
923 EXPECT_TRUE(TestAndResetAvailable());
925 EndTest();
926 break;
930 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
931 bool result) OVERRIDE {
932 ReturnUnusedResourcesFromParent(host_impl);
936 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestReusedResources);
938 class LayerTreeHostDelegatedTestFrameBeforeAck
939 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
940 public:
941 virtual void BeginTest() OVERRIDE {
942 PostSetNeedsCommitToMainThread();
945 virtual void DidCommitAndDrawFrame() OVERRIDE {
946 scoped_ptr<DelegatedFrameData> frame;
947 ReturnedResourceArray resources;
949 int next_source_frame_number = layer_tree_host()->source_frame_number();
950 switch (next_source_frame_number) {
951 case 1:
952 // Generate a frame with some resources in it.
953 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
954 AddTextureQuad(frame.get(), 999);
955 AddTransferableResource(frame.get(), 999);
956 AddTextureQuad(frame.get(), 555);
957 AddTransferableResource(frame.get(), 555);
958 AddTextureQuad(frame.get(), 444);
959 AddTransferableResource(frame.get(), 444);
960 SetFrameData(frame.Pass());
961 break;
962 case 2:
963 // All of the resources are in use.
964 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
965 EXPECT_EQ(0u, resources.size());
966 EXPECT_FALSE(TestAndResetAvailable());
968 // Keep using 999 but stop using 555 and 444.
969 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
970 AddTextureQuad(frame.get(), 999);
971 AddTransferableResource(frame.get(), 999);
972 SetFrameData(frame.Pass());
974 // Resource are not immediately released.
975 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
976 EXPECT_EQ(0u, resources.size());
977 EXPECT_FALSE(TestAndResetAvailable());
979 // The parent compositor (this one) does a commit.
980 break;
981 case 3:
982 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
984 unsigned expected[] = {444, 555};
985 EXPECT_RESOURCES(expected, resources);
986 EXPECT_TRUE(TestAndResetAvailable());
989 // The child compositor sends a frame referring to resources not in the
990 // frame.
991 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
992 AddTextureQuad(frame.get(), 999);
993 AddTextureQuad(frame.get(), 555);
994 AddTextureQuad(frame.get(), 444);
995 SetFrameData(frame.Pass());
996 break;
1000 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
1001 if (host_impl->active_tree()->source_frame_number() != 3)
1002 return;
1004 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1005 FakeDelegatedRendererLayerImpl* delegated_impl =
1006 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1008 const ResourceProvider::ResourceIdMap& map =
1009 host_impl->resource_provider()->GetChildToParentMap(
1010 delegated_impl->ChildId());
1012 // The bad frame should be dropped. So we should only have one quad (the
1013 // one with resource 999) on the impl tree. And only 999 will be present
1014 // in the parent's resource provider.
1015 EXPECT_EQ(1u, map.size());
1016 EXPECT_EQ(1u, map.count(999));
1018 EXPECT_EQ(1u, delegated_impl->Resources().size());
1019 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1021 const RenderPass* pass = delegated_impl->RenderPassesInDrawOrder()[0];
1022 EXPECT_EQ(1u, pass->quad_list.size());
1023 const TextureDrawQuad* quad = TextureDrawQuad::MaterialCast(
1024 pass->quad_list[0]);
1025 EXPECT_EQ(map.find(999)->second, quad->resource_id);
1027 EndTest();
1030 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
1031 bool result) OVERRIDE {
1032 ReturnUnusedResourcesFromParent(host_impl);
1036 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestFrameBeforeAck);
1038 class LayerTreeHostDelegatedTestFrameBeforeTakeResources
1039 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1040 public:
1041 virtual void BeginTest() OVERRIDE {
1042 PostSetNeedsCommitToMainThread();
1045 virtual void DidCommitAndDrawFrame() OVERRIDE {
1046 scoped_ptr<DelegatedFrameData> frame;
1047 ReturnedResourceArray resources;
1049 int next_source_frame_number = layer_tree_host()->source_frame_number();
1050 switch (next_source_frame_number) {
1051 case 1:
1052 // Generate a frame with some resources in it.
1053 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1054 AddTextureQuad(frame.get(), 999);
1055 AddTransferableResource(frame.get(), 999);
1056 AddTextureQuad(frame.get(), 555);
1057 AddTransferableResource(frame.get(), 555);
1058 AddTextureQuad(frame.get(), 444);
1059 AddTransferableResource(frame.get(), 444);
1060 SetFrameData(frame.Pass());
1061 break;
1062 case 2:
1063 // All of the resources are in use.
1064 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1065 EXPECT_EQ(0u, resources.size());
1066 EXPECT_FALSE(TestAndResetAvailable());
1068 // Keep using 999 but stop using 555 and 444.
1069 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1070 AddTextureQuad(frame.get(), 999);
1071 AddTransferableResource(frame.get(), 999);
1072 SetFrameData(frame.Pass());
1074 // Resource are not immediately released.
1075 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1076 EXPECT_EQ(0u, resources.size());
1077 EXPECT_FALSE(TestAndResetAvailable());
1079 // The parent compositor (this one) does a commit.
1080 break;
1081 case 3:
1082 // The child compositor sends a frame before taking resources back
1083 // from the previous commit. This frame makes use of the resources 555
1084 // and 444, which were just released during commit.
1085 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1086 AddTextureQuad(frame.get(), 999);
1087 AddTransferableResource(frame.get(), 999);
1088 AddTextureQuad(frame.get(), 555);
1089 AddTransferableResource(frame.get(), 555);
1090 AddTextureQuad(frame.get(), 444);
1091 AddTransferableResource(frame.get(), 444);
1092 SetFrameData(frame.Pass());
1094 // The resources are used by the new frame but are returned anyway since
1095 // we passed them again.
1096 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1098 unsigned expected[] = {444, 555};
1099 EXPECT_RESOURCES(expected, resources);
1100 EXPECT_TRUE(TestAndResetAvailable());
1102 break;
1103 case 4:
1104 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1105 EXPECT_EQ(0u, resources.size());
1106 EXPECT_FALSE(TestAndResetAvailable());
1107 EndTest();
1108 break;
1112 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
1113 if (host_impl->active_tree()->source_frame_number() != 3)
1114 return;
1116 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1117 FakeDelegatedRendererLayerImpl* delegated_impl =
1118 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1120 const ResourceProvider::ResourceIdMap& map =
1121 host_impl->resource_provider()->GetChildToParentMap(
1122 delegated_impl->ChildId());
1124 // The third frame has all of the resources in it again, the delegated
1125 // renderer layer should continue to own the resources for it.
1126 EXPECT_EQ(3u, map.size());
1127 EXPECT_EQ(1u, map.count(999));
1128 EXPECT_EQ(1u, map.count(555));
1129 EXPECT_EQ(1u, map.count(444));
1131 EXPECT_EQ(3u, delegated_impl->Resources().size());
1132 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1133 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1134 EXPECT_EQ(1u, delegated_impl->Resources().count(444));
1136 const RenderPass* pass = delegated_impl->RenderPassesInDrawOrder()[0];
1137 EXPECT_EQ(3u, pass->quad_list.size());
1138 const TextureDrawQuad* quad1 = TextureDrawQuad::MaterialCast(
1139 pass->quad_list[0]);
1140 EXPECT_EQ(map.find(999)->second, quad1->resource_id);
1141 const TextureDrawQuad* quad2 = TextureDrawQuad::MaterialCast(
1142 pass->quad_list[1]);
1143 EXPECT_EQ(map.find(555)->second, quad2->resource_id);
1144 const TextureDrawQuad* quad3 = TextureDrawQuad::MaterialCast(
1145 pass->quad_list[2]);
1146 EXPECT_EQ(map.find(444)->second, quad3->resource_id);
1149 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
1150 bool result) OVERRIDE {
1151 ReturnUnusedResourcesFromParent(host_impl);
1155 SINGLE_AND_MULTI_THREAD_TEST_F(
1156 LayerTreeHostDelegatedTestFrameBeforeTakeResources);
1158 class LayerTreeHostDelegatedTestBadFrame
1159 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1160 public:
1161 virtual void BeginTest() OVERRIDE {
1162 PostSetNeedsCommitToMainThread();
1165 virtual void DidCommitAndDrawFrame() OVERRIDE {
1166 scoped_ptr<DelegatedFrameData> frame;
1167 ReturnedResourceArray resources;
1169 int next_source_frame_number = layer_tree_host()->source_frame_number();
1170 switch (next_source_frame_number) {
1171 case 1:
1172 // Generate a frame with some resources in it.
1173 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1174 AddTextureQuad(frame.get(), 999);
1175 AddTransferableResource(frame.get(), 999);
1176 AddTextureQuad(frame.get(), 555);
1177 AddTransferableResource(frame.get(), 555);
1178 SetFrameData(frame.Pass());
1179 break;
1180 case 2:
1181 // All of the resources are in use.
1182 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1183 EXPECT_EQ(0u, resources.size());
1184 EXPECT_FALSE(TestAndResetAvailable());
1186 // Generate a bad frame with a resource the layer doesn't have. The
1187 // 885 and 775 resources are unknown, while ownership of the legit 444
1188 // resource is passed in here. The bad frame does not use any of the
1189 // previous resources, 999 or 555.
1190 // A bad quad is present both before and after the good quad.
1191 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1192 AddTextureQuad(frame.get(), 885);
1193 AddTextureQuad(frame.get(), 444);
1194 AddTransferableResource(frame.get(), 444);
1195 AddTextureQuad(frame.get(), 775);
1196 SetFrameData(frame.Pass());
1198 // The parent compositor (this one) does a commit.
1199 break;
1200 case 3:
1201 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1202 EXPECT_EQ(0u, resources.size());
1203 EXPECT_FALSE(TestAndResetAvailable());
1205 // Now send a good frame with 999 again.
1206 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1207 AddTextureQuad(frame.get(), 999);
1208 SetFrameData(frame.Pass());
1210 // The bad frame's resource is given back to the child compositor.
1211 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1213 unsigned expected[] = {444};
1214 EXPECT_RESOURCES(expected, resources);
1215 EXPECT_TRUE(TestAndResetAvailable());
1217 break;
1218 case 4:
1219 // The unused 555 from the last good frame is now released.
1220 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1222 unsigned expected[] = {555};
1223 EXPECT_RESOURCES(expected, resources);
1224 EXPECT_TRUE(TestAndResetAvailable());
1227 EndTest();
1228 break;
1232 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
1233 bool result) OVERRIDE {
1234 if (host_impl->active_tree()->source_frame_number() < 1)
1235 return;
1237 ReturnUnusedResourcesFromParent(host_impl);
1239 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1240 FakeDelegatedRendererLayerImpl* delegated_impl =
1241 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1243 const ResourceProvider::ResourceIdMap& map =
1244 host_impl->resource_provider()->GetChildToParentMap(
1245 delegated_impl->ChildId());
1247 switch (host_impl->active_tree()->source_frame_number()) {
1248 case 1: {
1249 // We have the first good frame with just 990 and 555 in it.
1250 // layer.
1251 EXPECT_EQ(2u, map.size());
1252 EXPECT_EQ(1u, map.count(999));
1253 EXPECT_EQ(1u, map.count(555));
1255 EXPECT_EQ(2u, delegated_impl->Resources().size());
1256 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1257 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1259 const RenderPass* pass = delegated_impl->RenderPassesInDrawOrder()[0];
1260 EXPECT_EQ(2u, pass->quad_list.size());
1261 const TextureDrawQuad* quad1 = TextureDrawQuad::MaterialCast(
1262 pass->quad_list[0]);
1263 EXPECT_EQ(map.find(999)->second, quad1->resource_id);
1264 const TextureDrawQuad* quad2 = TextureDrawQuad::MaterialCast(
1265 pass->quad_list[1]);
1266 EXPECT_EQ(map.find(555)->second, quad2->resource_id);
1267 break;
1269 case 2: {
1270 // We only keep resources from the last valid frame.
1271 EXPECT_EQ(2u, map.size());
1272 EXPECT_EQ(1u, map.count(999));
1273 EXPECT_EQ(1u, map.count(555));
1275 EXPECT_EQ(2u, delegated_impl->Resources().size());
1276 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1277 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1279 // The bad frame is dropped though, we still have the frame with 999 and
1280 // 555 in it.
1281 const RenderPass* pass = delegated_impl->RenderPassesInDrawOrder()[0];
1282 EXPECT_EQ(2u, pass->quad_list.size());
1283 const TextureDrawQuad* quad1 = TextureDrawQuad::MaterialCast(
1284 pass->quad_list[0]);
1285 EXPECT_EQ(map.find(999)->second, quad1->resource_id);
1286 const TextureDrawQuad* quad2 = TextureDrawQuad::MaterialCast(
1287 pass->quad_list[1]);
1288 EXPECT_EQ(map.find(555)->second, quad2->resource_id);
1289 break;
1291 case 3: {
1292 // We have the new good frame with just 999 in it.
1293 EXPECT_EQ(1u, map.size());
1294 EXPECT_EQ(1u, map.count(999));
1296 EXPECT_EQ(1u, delegated_impl->Resources().size());
1297 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1299 const RenderPass* pass = delegated_impl->RenderPassesInDrawOrder()[0];
1300 EXPECT_EQ(1u, pass->quad_list.size());
1301 const TextureDrawQuad* quad1 = TextureDrawQuad::MaterialCast(
1302 pass->quad_list[0]);
1303 EXPECT_EQ(map.find(999)->second, quad1->resource_id);
1304 break;
1310 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestBadFrame);
1312 class LayerTreeHostDelegatedTestUnnamedResource
1313 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1314 public:
1315 virtual void BeginTest() OVERRIDE {
1316 PostSetNeedsCommitToMainThread();
1319 virtual void DidCommit() OVERRIDE {
1320 scoped_ptr<DelegatedFrameData> frame;
1321 ReturnedResourceArray resources;
1323 int next_source_frame_number = layer_tree_host()->source_frame_number();
1324 switch (next_source_frame_number) {
1325 case 1:
1326 // This frame includes two resources in it, but only uses one.
1327 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1328 AddTransferableResource(frame.get(), 999);
1329 AddTextureQuad(frame.get(), 555);
1330 AddTransferableResource(frame.get(), 555);
1331 SetFrameData(frame.Pass());
1332 break;
1333 case 2:
1334 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1335 EXPECT_EQ(0u, resources.size());
1336 EXPECT_FALSE(TestAndResetAvailable());
1338 // Now send an empty frame.
1339 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1340 SetFrameData(frame.Pass());
1342 // The unused resource should be returned.
1343 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1345 unsigned expected[] = {999};
1346 EXPECT_RESOURCES(expected, resources);
1347 EXPECT_TRUE(TestAndResetAvailable());
1350 EndTest();
1351 break;
1355 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
1356 if (host_impl->active_tree()->source_frame_number() != 1)
1357 return;
1359 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1360 FakeDelegatedRendererLayerImpl* delegated_impl =
1361 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1363 const ResourceProvider::ResourceIdMap& map =
1364 host_impl->resource_provider()->GetChildToParentMap(
1365 delegated_impl->ChildId());
1367 // The layer only held on to the resource that was used.
1368 EXPECT_EQ(1u, map.size());
1369 EXPECT_EQ(1u, map.count(555));
1371 EXPECT_EQ(1u, delegated_impl->Resources().size());
1372 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1376 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestUnnamedResource);
1378 class LayerTreeHostDelegatedTestDontLeakResource
1379 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1380 public:
1381 virtual void BeginTest() OVERRIDE {
1382 PostSetNeedsCommitToMainThread();
1385 virtual void DidCommitAndDrawFrame() OVERRIDE {
1386 scoped_ptr<DelegatedFrameData> frame;
1387 ReturnedResourceArray resources;
1389 int next_source_frame_number = layer_tree_host()->source_frame_number();
1390 switch (next_source_frame_number) {
1391 case 1:
1392 // This frame includes two resources in it.
1393 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1394 AddTextureQuad(frame.get(), 999);
1395 AddTransferableResource(frame.get(), 999);
1396 AddTextureQuad(frame.get(), 555);
1397 AddTransferableResource(frame.get(), 555);
1398 SetFrameData(frame.Pass());
1400 // But then we immediately stop using 999.
1401 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1402 AddTextureQuad(frame.get(), 555);
1403 AddTransferableResource(frame.get(), 555);
1404 SetFrameData(frame.Pass());
1405 break;
1406 case 2:
1407 // The unused resources should be returned. 555 is still used, but it's
1408 // returned once to account for the first frame.
1409 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1411 unsigned expected[] = {555, 999};
1412 EXPECT_RESOURCES(expected, resources);
1413 EXPECT_TRUE(TestAndResetAvailable());
1415 // Send a frame with no resources in it.
1416 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1417 SetFrameData(frame.Pass());
1418 break;
1419 case 3:
1420 // The now unused resource 555 should be returned.
1421 resources.clear();
1422 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1424 unsigned expected[] = {555};
1425 EXPECT_RESOURCES(expected, resources);
1426 EXPECT_TRUE(TestAndResetAvailable());
1428 EndTest();
1429 break;
1433 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
1434 if (host_impl->active_tree()->source_frame_number() != 1)
1435 return;
1437 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1438 FakeDelegatedRendererLayerImpl* delegated_impl =
1439 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1441 const ResourceProvider::ResourceIdMap& map =
1442 host_impl->resource_provider()->GetChildToParentMap(
1443 delegated_impl->ChildId());
1445 // The layer only held on to the resource that was used.
1446 EXPECT_EQ(1u, map.size());
1447 EXPECT_EQ(1u, map.count(555));
1449 EXPECT_EQ(1u, delegated_impl->Resources().size());
1450 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1453 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
1454 bool result) OVERRIDE {
1455 ReturnUnusedResourcesFromParent(host_impl);
1459 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestDontLeakResource);
1461 class LayerTreeHostDelegatedTestResourceSentToParent
1462 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1463 public:
1464 virtual void DidCommitAndDrawFrame() OVERRIDE {
1465 scoped_ptr<DelegatedFrameData> frame;
1466 ReturnedResourceArray resources;
1468 int next_source_frame_number = layer_tree_host()->source_frame_number();
1469 switch (next_source_frame_number) {
1470 case 1:
1471 // This frame includes two resources in it.
1472 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1473 AddTextureQuad(frame.get(), 999);
1474 AddTransferableResource(frame.get(), 999);
1475 AddTextureQuad(frame.get(), 555);
1476 AddTransferableResource(frame.get(), 555);
1477 SetFrameData(frame.Pass());
1478 break;
1479 case 2:
1480 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1481 EXPECT_EQ(0u, resources.size());
1482 EXPECT_FALSE(TestAndResetAvailable());
1484 // 999 is in use in the grandparent compositor, generate a frame without
1485 // it present.
1486 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1487 AddTextureQuad(frame.get(), 555);
1488 AddTransferableResource(frame.get(), 555);
1489 SetFrameData(frame.Pass());
1490 break;
1491 case 3:
1492 // Since 999 is in the grandparent it is not returned.
1493 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1494 EXPECT_EQ(0u, resources.size());
1495 EXPECT_FALSE(TestAndResetAvailable());
1497 // The impl side will get back the resource at some point.
1498 ImplThreadTaskRunner()->PostTask(FROM_HERE,
1499 receive_resource_on_thread_);
1500 break;
1504 void ReceiveResourceOnThread(LayerTreeHostImpl* host_impl) {
1505 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1506 FakeDelegatedRendererLayerImpl* delegated_impl =
1507 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1509 const ResourceProvider::ResourceIdMap& map =
1510 host_impl->resource_provider()->GetChildToParentMap(
1511 delegated_impl->ChildId());
1513 // Receive 999 back from the grandparent.
1514 CompositorFrameAck ack;
1515 output_surface()->ReturnResource(map.find(999)->second, &ack);
1516 host_impl->ReclaimResources(&ack);
1519 virtual void UnusedResourcesAreAvailable() OVERRIDE {
1520 EXPECT_EQ(3, layer_tree_host()->source_frame_number());
1522 ReturnedResourceArray resources;
1524 // 999 was returned from the grandparent and could be released.
1525 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1527 unsigned expected[] = {999};
1528 EXPECT_RESOURCES(expected, resources);
1531 EndTest();
1534 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
1535 if (host_impl->active_tree()->source_frame_number() < 1)
1536 return;
1538 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1539 FakeDelegatedRendererLayerImpl* delegated_impl =
1540 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1542 const ResourceProvider::ResourceIdMap& map =
1543 host_impl->resource_provider()->GetChildToParentMap(
1544 delegated_impl->ChildId());
1546 switch (host_impl->active_tree()->source_frame_number()) {
1547 case 1: {
1548 EXPECT_EQ(2u, map.size());
1549 EXPECT_EQ(1u, map.count(999));
1550 EXPECT_EQ(1u, map.count(555));
1552 EXPECT_EQ(2u, delegated_impl->Resources().size());
1553 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1554 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1556 // The 999 resource will be sent to a grandparent compositor.
1557 break;
1559 case 2: {
1560 EXPECT_EQ(2u, map.size());
1561 EXPECT_EQ(1u, map.count(999));
1562 EXPECT_EQ(1u, map.count(555));
1564 // 999 is in the parent, so not held by delegated renderer layer.
1565 EXPECT_EQ(1u, delegated_impl->Resources().size());
1566 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1568 receive_resource_on_thread_ =
1569 base::Bind(&LayerTreeHostDelegatedTestResourceSentToParent::
1570 ReceiveResourceOnThread,
1571 base::Unretained(this),
1572 host_impl);
1573 break;
1575 case 3:
1576 // 999 should be released.
1577 EXPECT_EQ(1u, map.size());
1578 EXPECT_EQ(1u, map.count(555));
1580 EXPECT_EQ(1u, delegated_impl->Resources().size());
1581 EXPECT_EQ(1u, delegated_impl->Resources().count(map.find(555)->second));
1582 break;
1586 base::Closure receive_resource_on_thread_;
1589 SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(
1590 LayerTreeHostDelegatedTestResourceSentToParent);
1592 class LayerTreeHostDelegatedTestCommitWithoutTake
1593 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1594 public:
1595 virtual void BeginTest() OVERRIDE {
1596 // Prevent drawing with resources that are sent to the grandparent.
1597 layer_tree_host()->SetViewportSize(gfx::Size());
1598 PostSetNeedsCommitToMainThread();
1601 virtual void DidCommit() OVERRIDE {
1602 scoped_ptr<DelegatedFrameData> frame;
1603 ReturnedResourceArray resources;
1605 int next_source_frame_number = layer_tree_host()->source_frame_number();
1606 switch (next_source_frame_number) {
1607 case 1:
1608 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1609 AddTextureQuad(frame.get(), 999);
1610 AddTransferableResource(frame.get(), 999);
1611 AddTextureQuad(frame.get(), 555);
1612 AddTransferableResource(frame.get(), 555);
1613 AddTextureQuad(frame.get(), 444);
1614 AddTransferableResource(frame.get(), 444);
1615 SetFrameData(frame.Pass());
1616 break;
1617 case 2:
1618 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1619 EXPECT_EQ(0u, resources.size());
1620 EXPECT_FALSE(TestAndResetAvailable());
1622 // Stop using 999 and 444 in this frame and commit.
1623 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1624 AddTextureQuad(frame.get(), 555);
1625 AddTransferableResource(frame.get(), 555);
1626 SetFrameData(frame.Pass());
1627 // 999 and 444 will be returned for frame 1, but not 555 since it's in
1628 // the current frame.
1629 break;
1630 case 3:
1631 // Don't take resources here, but set a new frame that uses 999 again.
1632 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1633 AddTextureQuad(frame.get(), 999);
1634 AddTransferableResource(frame.get(), 999);
1635 AddTextureQuad(frame.get(), 555);
1636 AddTransferableResource(frame.get(), 555);
1637 SetFrameData(frame.Pass());
1638 break;
1639 case 4:
1640 // 555 from frame 1 and 2 isn't returned since it's still in use. 999
1641 // from frame 1 is returned though.
1642 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1644 unsigned expected[] = {444, 999};
1645 EXPECT_RESOURCES(expected, resources);
1646 EXPECT_TRUE(TestAndResetAvailable());
1649 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1650 SetFrameData(frame.Pass());
1651 // 555 will be returned 3 times for frames 1 2 and 3, and 999 will be
1652 // returned once for frame 3.
1653 break;
1654 case 5:
1655 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1657 unsigned expected[] = {555, 555, 555, 999};
1658 EXPECT_RESOURCES(expected, resources);
1659 EXPECT_TRUE(TestAndResetAvailable());
1662 EndTest();
1663 break;
1667 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
1668 if (host_impl->active_tree()->source_frame_number() < 1)
1669 return;
1671 LayerImpl* root_impl = host_impl->active_tree()->root_layer();
1672 FakeDelegatedRendererLayerImpl* delegated_impl =
1673 static_cast<FakeDelegatedRendererLayerImpl*>(root_impl->children()[0]);
1675 const ResourceProvider::ResourceIdMap& map =
1676 host_impl->resource_provider()->GetChildToParentMap(
1677 delegated_impl->ChildId());
1679 switch (host_impl->active_tree()->source_frame_number()) {
1680 case 1:
1681 EXPECT_EQ(3u, map.size());
1682 EXPECT_EQ(1u, map.count(999));
1683 EXPECT_EQ(1u, map.count(555));
1684 EXPECT_EQ(1u, map.count(444));
1686 EXPECT_EQ(3u, delegated_impl->Resources().size());
1687 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1688 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1689 EXPECT_EQ(1u, delegated_impl->Resources().count(444));
1690 break;
1691 case 2:
1692 EXPECT_EQ(1u, map.size());
1693 EXPECT_EQ(1u, map.count(555));
1695 EXPECT_EQ(1u, delegated_impl->Resources().size());
1696 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1697 break;
1698 case 3:
1699 EXPECT_EQ(2u, map.size());
1700 EXPECT_EQ(1u, map.count(999));
1701 EXPECT_EQ(1u, map.count(555));
1703 EXPECT_EQ(2u, delegated_impl->Resources().size());
1704 EXPECT_EQ(1u, delegated_impl->Resources().count(999));
1705 EXPECT_EQ(1u, delegated_impl->Resources().count(555));
1710 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestCommitWithoutTake);
1712 class DelegatedFrameIsActivatedDuringCommit
1713 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1714 protected:
1715 DelegatedFrameIsActivatedDuringCommit() : returned_resource_count_(0) {}
1717 virtual void BeginTest() OVERRIDE {
1718 activate_count_ = 0;
1720 scoped_ptr<DelegatedFrameData> frame =
1721 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1722 AddTextureQuad(frame.get(), 999);
1723 AddTransferableResource(frame.get(), 999);
1724 SetFrameData(frame.Pass());
1726 PostSetNeedsCommitToMainThread();
1729 virtual void WillActivateTreeOnThread(LayerTreeHostImpl* impl) OVERRIDE {
1730 ++activate_count_;
1733 virtual void DidCommit() OVERRIDE {
1734 switch (layer_tree_host()->source_frame_number()) {
1735 case 1: {
1736 // The first frame has been activated. Set a new frame, and
1737 // expect the next commit to finish *after* it is activated.
1738 scoped_ptr<DelegatedFrameData> frame =
1739 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1740 AddTextureQuad(frame.get(), 555);
1741 AddTransferableResource(frame.get(), 555);
1742 SetFrameData(frame.Pass());
1743 break;
1745 case 2:
1746 // The second frame has been activated. Remove the layer from
1747 // the tree to cause another commit/activation. The commit should
1748 // finish *after* the layer is removed from the active tree.
1749 delegated_->RemoveFromParent();
1750 break;
1751 case 3:
1752 // Finish the test by releasing resources on the next frame.
1753 scoped_ptr<DelegatedFrameData> frame =
1754 CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1755 SetFrameData(frame.Pass());
1756 break;
1760 virtual void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
1761 switch (host_impl->active_tree()->source_frame_number()) {
1762 case 0: {
1763 // The activate for the 1st frame should have happened before now.
1764 EXPECT_EQ(1, activate_count_);
1765 break;
1767 case 1: {
1768 // The activate for the 2nd frame should have happened before now.
1769 EXPECT_EQ(2, activate_count_);
1770 break;
1772 case 2: {
1773 // The activate to remove the layer should have happened before now.
1774 EXPECT_EQ(3, activate_count_);
1775 break;
1777 case 3: {
1778 NOTREACHED();
1779 break;
1784 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
1785 bool result) OVERRIDE {
1786 ReturnUnusedResourcesFromParent(host_impl);
1789 virtual void UnusedResourcesAreAvailable() OVERRIDE {
1790 LayerTreeHostDelegatedTestCaseSingleDelegatedLayer::
1791 UnusedResourcesAreAvailable();
1792 ReturnedResourceArray resources;
1793 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1794 EXPECT_TRUE(TestAndResetAvailable());
1795 returned_resource_count_ += resources.size();
1796 if (returned_resource_count_ == 2)
1797 EndTest();
1800 int activate_count_;
1801 size_t returned_resource_count_;
1804 SINGLE_AND_MULTI_THREAD_TEST_F(
1805 DelegatedFrameIsActivatedDuringCommit);
1807 class LayerTreeHostDelegatedTestTwoImplLayers
1808 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1809 public:
1810 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
1812 virtual void DidCommitAndDrawFrame() OVERRIDE {
1813 scoped_ptr<DelegatedFrameData> frame;
1814 ReturnedResourceArray resources;
1816 int next_source_frame_number = layer_tree_host()->source_frame_number();
1817 switch (next_source_frame_number) {
1818 case 1:
1819 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1820 AddTextureQuad(frame.get(), 999);
1821 AddTransferableResource(frame.get(), 999);
1822 AddTextureQuad(frame.get(), 555);
1823 AddTransferableResource(frame.get(), 555);
1824 SetFrameData(frame.Pass());
1825 break;
1826 case 2:
1827 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1828 EXPECT_EQ(0u, resources.size());
1829 EXPECT_FALSE(TestAndResetAvailable());
1831 // Remove the delegated layer and replace it with a new one. Use the
1832 // same frame and resources for it.
1833 delegated_->RemoveFromParent();
1834 delegated_ = CreateDelegatedLayer(frame_provider_.get());
1835 break;
1836 case 3:
1837 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1838 EXPECT_EQ(0u, resources.size());
1839 EXPECT_FALSE(TestAndResetAvailable());
1841 // Use a frame with no resources in it.
1842 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1843 SetFrameData(frame.Pass());
1844 break;
1845 case 4:
1846 // We gave one frame to the frame provider, so we should get one
1847 // ref back for each resource.
1848 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1850 unsigned expected[] = {555, 999};
1851 EXPECT_RESOURCES(expected, resources);
1852 EXPECT_TRUE(TestAndResetAvailable());
1854 EndTest();
1855 break;
1859 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
1860 bool result) OVERRIDE {
1861 ReturnUnusedResourcesFromParent(host_impl);
1865 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestTwoImplLayers);
1867 class LayerTreeHostDelegatedTestTwoImplLayersTwoFrames
1868 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1869 public:
1870 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
1872 virtual void DidCommitAndDrawFrame() OVERRIDE {
1873 scoped_ptr<DelegatedFrameData> frame;
1874 ReturnedResourceArray resources;
1876 int next_source_frame_number = layer_tree_host()->source_frame_number();
1877 switch (next_source_frame_number) {
1878 case 1:
1879 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1880 AddTextureQuad(frame.get(), 999);
1881 AddTransferableResource(frame.get(), 999);
1882 AddTextureQuad(frame.get(), 555);
1883 AddTransferableResource(frame.get(), 555);
1884 SetFrameData(frame.Pass());
1885 break;
1886 case 2:
1887 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1888 EXPECT_EQ(0u, resources.size());
1889 EXPECT_FALSE(TestAndResetAvailable());
1891 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1892 AddTextureQuad(frame.get(), 999);
1893 AddTransferableResource(frame.get(), 999);
1894 AddTextureQuad(frame.get(), 555);
1895 AddTransferableResource(frame.get(), 555);
1897 // Remove the delegated layer and replace it with a new one. Make a new
1898 // frame but with the same resources for it.
1899 delegated_->RemoveFromParent();
1900 delegated_ = NULL;
1902 frame_provider_->SetFrameData(frame.Pass());
1903 delegated_ = CreateDelegatedLayer(frame_provider_.get());
1904 break;
1905 case 3:
1906 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1907 EXPECT_EQ(0u, resources.size());
1908 EXPECT_FALSE(TestAndResetAvailable());
1910 // Use a frame with no resources in it.
1911 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1912 SetFrameData(frame.Pass());
1913 break;
1914 case 4:
1915 // We gave two frames to the frame provider, so we should get two
1916 // refs back for each resource.
1917 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1919 unsigned expected[] = {555, 555, 999, 999};
1920 EXPECT_RESOURCES(expected, resources);
1921 EXPECT_TRUE(TestAndResetAvailable());
1923 EndTest();
1924 break;
1928 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
1929 bool result) OVERRIDE {
1930 ReturnUnusedResourcesFromParent(host_impl);
1934 SINGLE_AND_MULTI_THREAD_TEST_F(
1935 LayerTreeHostDelegatedTestTwoImplLayersTwoFrames);
1937 class LayerTreeHostDelegatedTestTwoLayers
1938 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
1939 public:
1940 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
1942 virtual void DidCommitAndDrawFrame() OVERRIDE {
1943 scoped_ptr<DelegatedFrameData> frame;
1944 ReturnedResourceArray resources;
1946 int next_source_frame_number = layer_tree_host()->source_frame_number();
1947 switch (next_source_frame_number) {
1948 case 1:
1949 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
1950 AddTextureQuad(frame.get(), 999);
1951 AddTransferableResource(frame.get(), 999);
1952 AddTextureQuad(frame.get(), 555);
1953 AddTransferableResource(frame.get(), 555);
1955 // Create a DelegatedRendererLayer using the frame.
1956 SetFrameData(frame.Pass());
1957 break;
1958 case 2:
1959 // Create a second DelegatedRendererLayer using the same frame provider.
1960 delegated_thief_ = CreateDelegatedLayer(frame_provider_.get());
1961 root_->AddChild(delegated_thief_);
1963 // And drop our ref on the frame provider so only the layers keep it
1964 // alive.
1965 frame_provider_ = NULL;
1966 break;
1967 case 3:
1968 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1969 EXPECT_EQ(0u, resources.size());
1970 EXPECT_FALSE(TestAndResetAvailable());
1972 // Remove one delegated layer from the tree. No resources should be
1973 // returned yet.
1974 delegated_->RemoveFromParent();
1975 break;
1976 case 4:
1977 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1978 EXPECT_EQ(0u, resources.size());
1979 EXPECT_FALSE(TestAndResetAvailable());
1981 // Put the first layer back, and remove the other layer and destroy it.
1982 // No resources should be returned yet.
1983 root_->AddChild(delegated_);
1984 delegated_thief_->RemoveFromParent();
1985 delegated_thief_ = NULL;
1986 break;
1987 case 5:
1988 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1989 EXPECT_EQ(0u, resources.size());
1990 EXPECT_FALSE(TestAndResetAvailable());
1992 // Remove the first layer from the tree again. The resources are still
1993 // held by the main thread layer.
1994 delegated_->RemoveFromParent();
1995 break;
1996 case 6:
1997 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
1998 EXPECT_EQ(0u, resources.size());
1999 EXPECT_FALSE(TestAndResetAvailable());
2001 // Destroy the layer and the resources should be returned immediately.
2002 delegated_ = NULL;
2004 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2006 unsigned expected[] = {555, 999};
2007 EXPECT_RESOURCES(expected, resources);
2008 EXPECT_TRUE(TestAndResetAvailable());
2010 EndTest();
2011 break;
2015 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
2016 bool result) OVERRIDE {
2017 ReturnUnusedResourcesFromParent(host_impl);
2020 scoped_refptr<DelegatedRendererLayer> delegated_thief_;
2023 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestTwoLayers);
2025 class LayerTreeHostDelegatedTestRemoveAndAddToTree
2026 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
2027 public:
2028 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
2030 virtual void DidCommitAndDrawFrame() OVERRIDE {
2031 scoped_ptr<DelegatedFrameData> frame;
2032 ReturnedResourceArray resources;
2034 int next_source_frame_number = layer_tree_host()->source_frame_number();
2035 switch (next_source_frame_number) {
2036 case 1:
2037 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2038 AddTextureQuad(frame.get(), 999);
2039 AddTransferableResource(frame.get(), 999);
2040 AddTextureQuad(frame.get(), 555);
2041 AddTransferableResource(frame.get(), 555);
2043 // Create a DelegatedRendererLayer using the frame.
2044 SetFrameData(frame.Pass());
2045 break;
2046 case 2:
2047 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2048 EXPECT_EQ(0u, resources.size());
2049 EXPECT_FALSE(TestAndResetAvailable());
2051 // Remove the layer from the tree. The resources should not be returned
2052 // since they are still on the main thread layer.
2053 delegated_->RemoveFromParent();
2054 break;
2055 case 3:
2056 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2057 EXPECT_EQ(0u, resources.size());
2058 EXPECT_FALSE(TestAndResetAvailable());
2060 // Add the layer back to the tree.
2061 layer_tree_host()->root_layer()->AddChild(delegated_);
2062 break;
2063 case 4:
2064 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2065 EXPECT_EQ(0u, resources.size());
2066 EXPECT_FALSE(TestAndResetAvailable());
2068 // Set a new frame. Resources should be returned.
2069 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2070 AddTextureQuad(frame.get(), 888);
2071 AddTransferableResource(frame.get(), 888);
2072 AddTextureQuad(frame.get(), 777);
2073 AddTransferableResource(frame.get(), 777);
2074 SetFrameData(frame.Pass());
2075 break;
2076 case 5:
2077 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2079 unsigned expected[] = {555, 999};
2080 EXPECT_RESOURCES(expected, resources);
2081 EXPECT_TRUE(TestAndResetAvailable());
2084 // Destroy the layer.
2085 delegated_->RemoveFromParent();
2086 delegated_ = NULL;
2087 break;
2088 case 6:
2089 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2090 EXPECT_EQ(0u, resources.size());
2091 EXPECT_FALSE(TestAndResetAvailable());
2093 // Destroy the frame provider. Resources should be returned.
2094 frame_provider_ = NULL;
2096 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2098 unsigned expected[] = {777, 888};
2099 EXPECT_RESOURCES(expected, resources);
2100 EXPECT_TRUE(TestAndResetAvailable());
2102 EndTest();
2103 break;
2107 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
2108 bool result) OVERRIDE {
2109 ReturnUnusedResourcesFromParent(host_impl);
2112 scoped_refptr<DelegatedRendererLayer> delegated_thief_;
2115 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDelegatedTestRemoveAndAddToTree);
2117 class LayerTreeHostDelegatedTestRemoveAndChangeResources
2118 : public LayerTreeHostDelegatedTestCaseSingleDelegatedLayer {
2119 public:
2120 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
2122 virtual void DidCommitAndDrawFrame() OVERRIDE {
2123 scoped_ptr<DelegatedFrameData> frame;
2124 ReturnedResourceArray resources;
2126 int next_source_frame_number = layer_tree_host()->source_frame_number();
2127 switch (next_source_frame_number) {
2128 case 1:
2129 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2130 AddTextureQuad(frame.get(), 999);
2131 AddTransferableResource(frame.get(), 999);
2132 AddTextureQuad(frame.get(), 555);
2133 AddTransferableResource(frame.get(), 555);
2135 // Create a DelegatedRendererLayer using the frame.
2136 SetFrameData(frame.Pass());
2137 break;
2138 case 2:
2139 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2140 EXPECT_EQ(0u, resources.size());
2141 EXPECT_FALSE(TestAndResetAvailable());
2143 // Remove the layer from the tree. The resources should not be returned
2144 // since they are still on the main thread layer.
2145 delegated_->RemoveFromParent();
2146 break;
2147 case 3:
2148 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2149 EXPECT_EQ(0u, resources.size());
2150 EXPECT_FALSE(TestAndResetAvailable());
2152 // Set a new frame. Resources should be returned immediately.
2153 frame = CreateFrameData(gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1));
2154 AddTextureQuad(frame.get(), 888);
2155 AddTransferableResource(frame.get(), 888);
2156 AddTextureQuad(frame.get(), 777);
2157 AddTransferableResource(frame.get(), 777);
2158 SetFrameData(frame.Pass());
2160 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2162 unsigned expected[] = {555, 999};
2163 EXPECT_RESOURCES(expected, resources);
2164 EXPECT_TRUE(TestAndResetAvailable());
2165 resources.clear();
2168 // Destroy the frame provider.
2169 frame_provider_ = NULL;
2171 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2172 EXPECT_EQ(0u, resources.size());
2173 EXPECT_FALSE(TestAndResetAvailable());
2175 // Destroy the layer. Resources should be returned.
2176 delegated_ = NULL;
2178 resource_collection_->TakeUnusedResourcesForChildCompositor(&resources);
2180 unsigned expected[] = {777, 888};
2181 EXPECT_RESOURCES(expected, resources);
2182 EXPECT_TRUE(TestAndResetAvailable());
2184 EndTest();
2185 break;
2189 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
2190 bool result) OVERRIDE {
2191 ReturnUnusedResourcesFromParent(host_impl);
2194 scoped_refptr<DelegatedRendererLayer> delegated_thief_;
2197 SINGLE_AND_MULTI_THREAD_TEST_F(
2198 LayerTreeHostDelegatedTestRemoveAndChangeResources);
2200 } // namespace
2201 } // namespace cc