Android NTP: Most visited tile clicks should have PageTransition.AUTO_BOOKMARK
[chromium-blink-merge.git] / cc / surfaces / surface_aggregator_unittest.cc
blobb2476d9df57081d916758de740b4559a9dff9321
1 // Copyright 2014 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/output/compositor_frame.h"
6 #include "cc/output/delegated_frame_data.h"
7 #include "cc/quads/render_pass.h"
8 #include "cc/quads/render_pass_draw_quad.h"
9 #include "cc/quads/solid_color_draw_quad.h"
10 #include "cc/quads/surface_draw_quad.h"
11 #include "cc/quads/texture_draw_quad.h"
12 #include "cc/resources/shared_bitmap_manager.h"
13 #include "cc/surfaces/surface.h"
14 #include "cc/surfaces/surface_aggregator.h"
15 #include "cc/surfaces/surface_aggregator_test_helpers.h"
16 #include "cc/surfaces/surface_factory.h"
17 #include "cc/surfaces/surface_factory_client.h"
18 #include "cc/surfaces/surface_id_allocator.h"
19 #include "cc/surfaces/surface_manager.h"
20 #include "cc/test/fake_output_surface.h"
21 #include "cc/test/fake_output_surface_client.h"
22 #include "cc/test/fake_resource_provider.h"
23 #include "cc/test/render_pass_test_utils.h"
24 #include "cc/test/test_shared_bitmap_manager.h"
25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27 #include "third_party/skia/include/core/SkColor.h"
29 namespace cc {
30 namespace {
32 SurfaceId InvalidSurfaceId() {
33 static SurfaceId invalid;
34 invalid.id = static_cast<uint64_t>(-1);
35 return invalid;
38 gfx::Size SurfaceSize() {
39 static gfx::Size size(5, 5);
40 return size;
43 class EmptySurfaceFactoryClient : public SurfaceFactoryClient {
44 public:
45 void ReturnResources(const ReturnedResourceArray& resources) override {}
48 class SurfaceAggregatorTest : public testing::Test {
49 public:
50 explicit SurfaceAggregatorTest(bool use_damage_rect)
51 : factory_(&manager_, &empty_client_),
52 aggregator_(&manager_, NULL, use_damage_rect) {}
54 SurfaceAggregatorTest() : SurfaceAggregatorTest(false) {}
56 protected:
57 SurfaceManager manager_;
58 EmptySurfaceFactoryClient empty_client_;
59 SurfaceFactory factory_;
60 SurfaceAggregator aggregator_;
63 TEST_F(SurfaceAggregatorTest, ValidSurfaceNoFrame) {
64 SurfaceId one_id(7);
65 factory_.Create(one_id);
66 scoped_ptr<CompositorFrame> frame = aggregator_.Aggregate(one_id);
67 EXPECT_FALSE(frame);
68 factory_.Destroy(one_id);
71 class SurfaceAggregatorValidSurfaceTest : public SurfaceAggregatorTest {
72 public:
73 explicit SurfaceAggregatorValidSurfaceTest(bool use_damage_rect)
74 : SurfaceAggregatorTest(use_damage_rect),
75 allocator_(1u),
76 child_allocator_(2u) {}
77 SurfaceAggregatorValidSurfaceTest()
78 : SurfaceAggregatorValidSurfaceTest(false) {}
80 void SetUp() override {
81 SurfaceAggregatorTest::SetUp();
82 root_surface_id_ = allocator_.GenerateId();
83 factory_.Create(root_surface_id_);
86 void TearDown() override {
87 factory_.Destroy(root_surface_id_);
88 SurfaceAggregatorTest::TearDown();
91 void AggregateAndVerify(test::Pass* expected_passes,
92 size_t expected_pass_count,
93 SurfaceId* surface_ids,
94 size_t expected_surface_count) {
95 scoped_ptr<CompositorFrame> aggregated_frame =
96 aggregator_.Aggregate(root_surface_id_);
98 ASSERT_TRUE(aggregated_frame);
99 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
101 DelegatedFrameData* frame_data =
102 aggregated_frame->delegated_frame_data.get();
104 TestPassesMatchExpectations(
105 expected_passes, expected_pass_count, &frame_data->render_pass_list);
107 // Ensure no duplicate pass ids output.
108 std::set<RenderPassId> used_passes;
109 for (auto* pass : frame_data->render_pass_list) {
110 EXPECT_TRUE(used_passes.insert(pass->id).second);
113 EXPECT_EQ(expected_surface_count,
114 aggregator_.previous_contained_surfaces().size());
115 for (size_t i = 0; i < expected_surface_count; i++) {
116 EXPECT_TRUE(
117 aggregator_.previous_contained_surfaces().find(surface_ids[i]) !=
118 aggregator_.previous_contained_surfaces().end());
122 void SubmitPassListAsFrame(SurfaceId surface_id, RenderPassList* pass_list) {
123 scoped_ptr<DelegatedFrameData> frame_data(new DelegatedFrameData);
124 pass_list->swap(frame_data->render_pass_list);
126 scoped_ptr<CompositorFrame> frame(new CompositorFrame);
127 frame->delegated_frame_data = frame_data.Pass();
129 factory_.SubmitCompositorFrame(surface_id, frame.Pass(),
130 SurfaceFactory::DrawCallback());
133 void SubmitCompositorFrame(test::Pass* passes,
134 size_t pass_count,
135 SurfaceId surface_id) {
136 RenderPassList pass_list;
137 AddPasses(&pass_list, gfx::Rect(SurfaceSize()), passes, pass_count);
138 SubmitPassListAsFrame(surface_id, &pass_list);
141 void QueuePassAsFrame(scoped_ptr<RenderPass> pass, SurfaceId surface_id) {
142 scoped_ptr<DelegatedFrameData> delegated_frame_data(new DelegatedFrameData);
143 delegated_frame_data->render_pass_list.push_back(pass.Pass());
145 scoped_ptr<CompositorFrame> child_frame(new CompositorFrame);
146 child_frame->delegated_frame_data = delegated_frame_data.Pass();
148 factory_.SubmitCompositorFrame(surface_id, child_frame.Pass(),
149 SurfaceFactory::DrawCallback());
152 protected:
153 SurfaceId root_surface_id_;
154 SurfaceIdAllocator allocator_;
155 SurfaceIdAllocator child_allocator_;
158 // Tests that a very simple frame containing only two solid color quads makes it
159 // through the aggregator correctly.
160 TEST_F(SurfaceAggregatorValidSurfaceTest, SimpleFrame) {
161 test::Quad quads[] = {test::Quad::SolidColorQuad(SK_ColorRED),
162 test::Quad::SolidColorQuad(SK_ColorBLUE)};
163 test::Pass passes[] = {test::Pass(quads, arraysize(quads))};
165 SubmitCompositorFrame(passes, arraysize(passes), root_surface_id_);
167 SurfaceId ids[] = {root_surface_id_};
168 AggregateAndVerify(passes, arraysize(passes), ids, arraysize(ids));
171 TEST_F(SurfaceAggregatorValidSurfaceTest, OpacityCopied) {
172 SurfaceId embedded_surface_id = allocator_.GenerateId();
173 factory_.Create(embedded_surface_id);
175 test::Quad embedded_quads[] = {test::Quad::SolidColorQuad(SK_ColorGREEN),
176 test::Quad::SolidColorQuad(SK_ColorBLUE)};
177 test::Pass embedded_passes[] = {
178 test::Pass(embedded_quads, arraysize(embedded_quads))};
180 SubmitCompositorFrame(embedded_passes, arraysize(embedded_passes),
181 embedded_surface_id);
183 test::Quad quads[] = {test::Quad::SurfaceQuad(embedded_surface_id, .5f)};
184 test::Pass passes[] = {test::Pass(quads, arraysize(quads))};
186 SubmitCompositorFrame(passes, arraysize(passes), root_surface_id_);
188 scoped_ptr<CompositorFrame> aggregated_frame =
189 aggregator_.Aggregate(root_surface_id_);
191 ASSERT_TRUE(aggregated_frame);
192 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
194 DelegatedFrameData* frame_data = aggregated_frame->delegated_frame_data.get();
196 RenderPassList& render_pass_list(frame_data->render_pass_list);
197 ASSERT_EQ(2u, render_pass_list.size());
198 SharedQuadStateList& shared_quad_state_list(
199 render_pass_list[0]->shared_quad_state_list);
200 ASSERT_EQ(2u, shared_quad_state_list.size());
201 EXPECT_EQ(1.f, shared_quad_state_list.ElementAt(0)->opacity);
202 EXPECT_EQ(1.f, shared_quad_state_list.ElementAt(1)->opacity);
204 SharedQuadStateList& shared_quad_state_list2(
205 render_pass_list[1]->shared_quad_state_list);
206 ASSERT_EQ(1u, shared_quad_state_list2.size());
207 EXPECT_EQ(.5f, shared_quad_state_list2.ElementAt(0)->opacity);
209 factory_.Destroy(embedded_surface_id);
212 TEST_F(SurfaceAggregatorValidSurfaceTest, MultiPassSimpleFrame) {
213 test::Quad quads[][2] = {{test::Quad::SolidColorQuad(SK_ColorWHITE),
214 test::Quad::SolidColorQuad(SK_ColorLTGRAY)},
215 {test::Quad::SolidColorQuad(SK_ColorGRAY),
216 test::Quad::SolidColorQuad(SK_ColorDKGRAY)}};
217 test::Pass passes[] = {
218 test::Pass(quads[0], arraysize(quads[0]), RenderPassId(1, 1)),
219 test::Pass(quads[1], arraysize(quads[1]), RenderPassId(1, 2))};
221 SubmitCompositorFrame(passes, arraysize(passes), root_surface_id_);
223 SurfaceId ids[] = {root_surface_id_};
224 AggregateAndVerify(passes, arraysize(passes), ids, arraysize(ids));
227 // This tests very simple embedding. root_surface has a frame containing a few
228 // solid color quads and a surface quad referencing embedded_surface.
229 // embedded_surface has a frame containing only a solid color quad. The solid
230 // color quad should be aggregated into the final frame.
231 TEST_F(SurfaceAggregatorValidSurfaceTest, SimpleSurfaceReference) {
232 SurfaceId embedded_surface_id = allocator_.GenerateId();
233 factory_.Create(embedded_surface_id);
235 test::Quad embedded_quads[] = {test::Quad::SolidColorQuad(SK_ColorGREEN)};
236 test::Pass embedded_passes[] = {
237 test::Pass(embedded_quads, arraysize(embedded_quads))};
239 SubmitCompositorFrame(embedded_passes, arraysize(embedded_passes),
240 embedded_surface_id);
242 test::Quad root_quads[] = {test::Quad::SolidColorQuad(SK_ColorWHITE),
243 test::Quad::SurfaceQuad(embedded_surface_id, 1.f),
244 test::Quad::SolidColorQuad(SK_ColorBLACK)};
245 test::Pass root_passes[] = {test::Pass(root_quads, arraysize(root_quads))};
247 SubmitCompositorFrame(root_passes, arraysize(root_passes), root_surface_id_);
249 test::Quad expected_quads[] = {test::Quad::SolidColorQuad(SK_ColorWHITE),
250 test::Quad::SolidColorQuad(SK_ColorGREEN),
251 test::Quad::SolidColorQuad(SK_ColorBLACK)};
252 test::Pass expected_passes[] = {
253 test::Pass(expected_quads, arraysize(expected_quads))};
254 SurfaceId ids[] = {root_surface_id_, embedded_surface_id};
255 AggregateAndVerify(
256 expected_passes, arraysize(expected_passes), ids, arraysize(ids));
258 factory_.Destroy(embedded_surface_id);
261 TEST_F(SurfaceAggregatorValidSurfaceTest, CopyRequest) {
262 SurfaceId embedded_surface_id = allocator_.GenerateId();
263 factory_.Create(embedded_surface_id);
265 test::Quad embedded_quads[] = {test::Quad::SolidColorQuad(SK_ColorGREEN)};
266 test::Pass embedded_passes[] = {
267 test::Pass(embedded_quads, arraysize(embedded_quads))};
269 SubmitCompositorFrame(embedded_passes, arraysize(embedded_passes),
270 embedded_surface_id);
271 scoped_ptr<CopyOutputRequest> copy_request(
272 CopyOutputRequest::CreateEmptyRequest());
273 CopyOutputRequest* copy_request_ptr = copy_request.get();
274 factory_.RequestCopyOfSurface(embedded_surface_id, copy_request.Pass());
276 test::Quad root_quads[] = {test::Quad::SolidColorQuad(SK_ColorWHITE),
277 test::Quad::SurfaceQuad(embedded_surface_id, 1.f),
278 test::Quad::SolidColorQuad(SK_ColorBLACK)};
279 test::Pass root_passes[] = {test::Pass(root_quads, arraysize(root_quads))};
281 SubmitCompositorFrame(root_passes, arraysize(root_passes), root_surface_id_);
283 scoped_ptr<CompositorFrame> aggregated_frame =
284 aggregator_.Aggregate(root_surface_id_);
286 ASSERT_TRUE(aggregated_frame);
287 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
289 DelegatedFrameData* frame_data = aggregated_frame->delegated_frame_data.get();
291 test::Quad expected_quads[] = {
292 test::Quad::SolidColorQuad(SK_ColorWHITE),
293 test::Quad::RenderPassQuad(frame_data->render_pass_list[0]->id),
294 test::Quad::SolidColorQuad(SK_ColorBLACK)};
295 test::Pass expected_passes[] = {
296 test::Pass(embedded_quads, arraysize(embedded_quads)),
297 test::Pass(expected_quads, arraysize(expected_quads))};
298 TestPassesMatchExpectations(expected_passes,
299 arraysize(expected_passes),
300 &frame_data->render_pass_list);
301 ASSERT_EQ(2u, frame_data->render_pass_list.size());
302 ASSERT_EQ(1u, frame_data->render_pass_list[0]->copy_requests.size());
303 DCHECK_EQ(copy_request_ptr,
304 frame_data->render_pass_list[0]->copy_requests[0]);
306 SurfaceId surface_ids[] = {root_surface_id_, embedded_surface_id};
307 EXPECT_EQ(arraysize(surface_ids),
308 aggregator_.previous_contained_surfaces().size());
309 for (size_t i = 0; i < arraysize(surface_ids); i++) {
310 EXPECT_TRUE(
311 aggregator_.previous_contained_surfaces().find(surface_ids[i]) !=
312 aggregator_.previous_contained_surfaces().end());
315 factory_.Destroy(embedded_surface_id);
318 // Root surface may contain copy requests.
319 TEST_F(SurfaceAggregatorValidSurfaceTest, RootCopyRequest) {
320 SurfaceId embedded_surface_id = allocator_.GenerateId();
321 factory_.Create(embedded_surface_id);
323 test::Quad embedded_quads[] = {test::Quad::SolidColorQuad(SK_ColorGREEN)};
324 test::Pass embedded_passes[] = {
325 test::Pass(embedded_quads, arraysize(embedded_quads))};
327 SubmitCompositorFrame(embedded_passes, arraysize(embedded_passes),
328 embedded_surface_id);
329 scoped_ptr<CopyOutputRequest> copy_request(
330 CopyOutputRequest::CreateEmptyRequest());
331 CopyOutputRequest* copy_request_ptr = copy_request.get();
332 scoped_ptr<CopyOutputRequest> copy_request2(
333 CopyOutputRequest::CreateEmptyRequest());
334 CopyOutputRequest* copy_request2_ptr = copy_request2.get();
336 test::Quad root_quads[] = {test::Quad::SolidColorQuad(SK_ColorWHITE),
337 test::Quad::SurfaceQuad(embedded_surface_id, 1.f),
338 test::Quad::SolidColorQuad(SK_ColorBLACK)};
339 test::Quad root_quads2[] = {test::Quad::SolidColorQuad(SK_ColorRED)};
340 test::Pass root_passes[] = {
341 test::Pass(root_quads, arraysize(root_quads), RenderPassId(1, 1)),
342 test::Pass(root_quads2, arraysize(root_quads2), RenderPassId(1, 2))};
344 RenderPassList pass_list;
345 AddPasses(&pass_list,
346 gfx::Rect(SurfaceSize()),
347 root_passes,
348 arraysize(root_passes));
349 pass_list[0]->copy_requests.push_back(copy_request.Pass());
350 pass_list[1]->copy_requests.push_back(copy_request2.Pass());
352 scoped_ptr<DelegatedFrameData> frame_data(new DelegatedFrameData);
353 pass_list.swap(frame_data->render_pass_list);
355 scoped_ptr<CompositorFrame> frame(new CompositorFrame);
356 frame->delegated_frame_data = frame_data.Pass();
358 factory_.SubmitCompositorFrame(root_surface_id_, frame.Pass(),
359 SurfaceFactory::DrawCallback());
362 scoped_ptr<CompositorFrame> aggregated_frame =
363 aggregator_.Aggregate(root_surface_id_);
365 ASSERT_TRUE(aggregated_frame);
366 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
368 DelegatedFrameData* frame_data = aggregated_frame->delegated_frame_data.get();
370 test::Quad expected_quads[] = {test::Quad::SolidColorQuad(SK_ColorWHITE),
371 test::Quad::SolidColorQuad(SK_ColorGREEN),
372 test::Quad::SolidColorQuad(SK_ColorBLACK)};
373 test::Pass expected_passes[] = {
374 test::Pass(expected_quads, arraysize(expected_quads)),
375 test::Pass(root_quads2, arraysize(root_quads2))};
376 TestPassesMatchExpectations(expected_passes,
377 arraysize(expected_passes),
378 &frame_data->render_pass_list);
379 ASSERT_EQ(2u, frame_data->render_pass_list.size());
380 ASSERT_EQ(1u, frame_data->render_pass_list[0]->copy_requests.size());
381 DCHECK_EQ(copy_request_ptr,
382 frame_data->render_pass_list[0]->copy_requests[0]);
383 ASSERT_EQ(1u, frame_data->render_pass_list[1]->copy_requests.size());
384 DCHECK_EQ(copy_request2_ptr,
385 frame_data->render_pass_list[1]->copy_requests[0]);
387 SurfaceId surface_ids[] = {root_surface_id_, embedded_surface_id};
388 EXPECT_EQ(arraysize(surface_ids),
389 aggregator_.previous_contained_surfaces().size());
390 for (size_t i = 0; i < arraysize(surface_ids); i++) {
391 EXPECT_TRUE(
392 aggregator_.previous_contained_surfaces().find(surface_ids[i]) !=
393 aggregator_.previous_contained_surfaces().end());
396 // Ensure copy requests have been removed from root surface.
397 const CompositorFrame* original_frame =
398 manager_.GetSurfaceForId(root_surface_id_)->GetEligibleFrame();
399 RenderPassList& original_pass_list =
400 original_frame->delegated_frame_data->render_pass_list;
401 ASSERT_EQ(2u, original_pass_list.size());
402 DCHECK(original_pass_list[0]->copy_requests.empty());
403 DCHECK(original_pass_list[1]->copy_requests.empty());
405 factory_.Destroy(embedded_surface_id);
408 // This tests referencing a surface that has multiple render passes.
409 TEST_F(SurfaceAggregatorValidSurfaceTest, MultiPassSurfaceReference) {
410 SurfaceId embedded_surface_id = child_allocator_.GenerateId();
411 factory_.Create(embedded_surface_id);
413 RenderPassId pass_ids[] = {RenderPassId(1, 1), RenderPassId(1, 2),
414 RenderPassId(1, 3)};
416 test::Quad embedded_quads[][2] = {
417 {test::Quad::SolidColorQuad(1), test::Quad::SolidColorQuad(2)},
418 {test::Quad::SolidColorQuad(3), test::Quad::RenderPassQuad(pass_ids[0])},
419 {test::Quad::SolidColorQuad(4), test::Quad::RenderPassQuad(pass_ids[1])}};
420 test::Pass embedded_passes[] = {
421 test::Pass(embedded_quads[0], arraysize(embedded_quads[0]), pass_ids[0]),
422 test::Pass(embedded_quads[1], arraysize(embedded_quads[1]), pass_ids[1]),
423 test::Pass(embedded_quads[2], arraysize(embedded_quads[2]), pass_ids[2])};
425 SubmitCompositorFrame(embedded_passes, arraysize(embedded_passes),
426 embedded_surface_id);
428 test::Quad root_quads[][2] = {
429 {test::Quad::SolidColorQuad(5), test::Quad::SolidColorQuad(6)},
430 {test::Quad::SurfaceQuad(embedded_surface_id, 1.f),
431 test::Quad::RenderPassQuad(pass_ids[0])},
432 {test::Quad::SolidColorQuad(7), test::Quad::RenderPassQuad(pass_ids[1])}};
433 test::Pass root_passes[] = {
434 test::Pass(root_quads[0], arraysize(root_quads[0]), pass_ids[0]),
435 test::Pass(root_quads[1], arraysize(root_quads[1]), pass_ids[1]),
436 test::Pass(root_quads[2], arraysize(root_quads[2]), pass_ids[2])};
438 SubmitCompositorFrame(root_passes, arraysize(root_passes), root_surface_id_);
440 scoped_ptr<CompositorFrame> aggregated_frame =
441 aggregator_.Aggregate(root_surface_id_);
443 ASSERT_TRUE(aggregated_frame);
444 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
446 DelegatedFrameData* frame_data = aggregated_frame->delegated_frame_data.get();
448 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
450 ASSERT_EQ(5u, aggregated_pass_list.size());
451 RenderPassId actual_pass_ids[] = {
452 aggregated_pass_list[0]->id, aggregated_pass_list[1]->id,
453 aggregated_pass_list[2]->id, aggregated_pass_list[3]->id,
454 aggregated_pass_list[4]->id};
455 for (size_t i = 0; i < 5; ++i) {
456 for (size_t j = 0; j < i; ++j) {
457 EXPECT_NE(actual_pass_ids[i], actual_pass_ids[j]);
462 SCOPED_TRACE("First pass");
463 // The first pass will just be the first pass from the root surfaces quad
464 // with no render pass quads to remap.
465 TestPassMatchesExpectations(root_passes[0], aggregated_pass_list[0]);
469 SCOPED_TRACE("Second pass");
470 // The next two passes will be from the embedded surface since we have to
471 // draw those passes before they are referenced from the render pass draw
472 // quad embedded into the root surface's second pass.
473 // First, there's the first embedded pass which doesn't reference anything
474 // else.
475 TestPassMatchesExpectations(embedded_passes[0], aggregated_pass_list[1]);
479 SCOPED_TRACE("Third pass");
480 const QuadList& third_pass_quad_list = aggregated_pass_list[2]->quad_list;
481 ASSERT_EQ(2u, third_pass_quad_list.size());
482 TestQuadMatchesExpectations(embedded_quads[1][0],
483 third_pass_quad_list.ElementAt(0));
485 // This render pass pass quad will reference the first pass from the
486 // embedded surface, which is the second pass in the aggregated frame.
487 ASSERT_EQ(DrawQuad::RENDER_PASS,
488 third_pass_quad_list.ElementAt(1)->material);
489 const RenderPassDrawQuad* third_pass_render_pass_draw_quad =
490 RenderPassDrawQuad::MaterialCast(third_pass_quad_list.ElementAt(1));
491 EXPECT_EQ(actual_pass_ids[1],
492 third_pass_render_pass_draw_quad->render_pass_id);
496 SCOPED_TRACE("Fourth pass");
497 // The fourth pass will have aggregated quads from the root surface's second
498 // pass and the embedded surface's first pass.
499 const QuadList& fourth_pass_quad_list = aggregated_pass_list[3]->quad_list;
500 ASSERT_EQ(3u, fourth_pass_quad_list.size());
502 // The first quad will be the yellow quad from the embedded surface's last
503 // pass.
504 TestQuadMatchesExpectations(embedded_quads[2][0],
505 fourth_pass_quad_list.ElementAt(0));
507 // The next quad will be a render pass quad referencing the second pass from
508 // the embedded surface, which is the third pass in the aggregated frame.
509 ASSERT_EQ(DrawQuad::RENDER_PASS,
510 fourth_pass_quad_list.ElementAt(1)->material);
511 const RenderPassDrawQuad* fourth_pass_first_render_pass_draw_quad =
512 RenderPassDrawQuad::MaterialCast(fourth_pass_quad_list.ElementAt(1));
513 EXPECT_EQ(actual_pass_ids[2],
514 fourth_pass_first_render_pass_draw_quad->render_pass_id);
516 // The last quad will be a render pass quad referencing the first pass from
517 // the root surface, which is the first pass overall.
518 ASSERT_EQ(DrawQuad::RENDER_PASS,
519 fourth_pass_quad_list.ElementAt(2)->material);
520 const RenderPassDrawQuad* fourth_pass_second_render_pass_draw_quad =
521 RenderPassDrawQuad::MaterialCast(fourth_pass_quad_list.ElementAt(2));
522 EXPECT_EQ(actual_pass_ids[0],
523 fourth_pass_second_render_pass_draw_quad->render_pass_id);
527 SCOPED_TRACE("Fifth pass");
528 const QuadList& fifth_pass_quad_list = aggregated_pass_list[4]->quad_list;
529 ASSERT_EQ(2u, fifth_pass_quad_list.size());
531 TestQuadMatchesExpectations(root_quads[2][0],
532 fifth_pass_quad_list.ElementAt(0));
534 // The last quad in the last pass will reference the second pass from the
535 // root surface, which after aggregating is the fourth pass in the overall
536 // list.
537 ASSERT_EQ(DrawQuad::RENDER_PASS,
538 fifth_pass_quad_list.ElementAt(1)->material);
539 const RenderPassDrawQuad* fifth_pass_render_pass_draw_quad =
540 RenderPassDrawQuad::MaterialCast(fifth_pass_quad_list.ElementAt(1));
541 EXPECT_EQ(actual_pass_ids[3],
542 fifth_pass_render_pass_draw_quad->render_pass_id);
544 factory_.Destroy(embedded_surface_id);
547 // Tests an invalid surface reference in a frame. The surface quad should just
548 // be dropped.
549 TEST_F(SurfaceAggregatorValidSurfaceTest, InvalidSurfaceReference) {
550 test::Quad quads[] = {test::Quad::SolidColorQuad(SK_ColorGREEN),
551 test::Quad::SurfaceQuad(InvalidSurfaceId(), 1.f),
552 test::Quad::SolidColorQuad(SK_ColorBLUE)};
553 test::Pass passes[] = {test::Pass(quads, arraysize(quads))};
555 SubmitCompositorFrame(passes, arraysize(passes), root_surface_id_);
557 test::Quad expected_quads[] = {test::Quad::SolidColorQuad(SK_ColorGREEN),
558 test::Quad::SolidColorQuad(SK_ColorBLUE)};
559 test::Pass expected_passes[] = {
560 test::Pass(expected_quads, arraysize(expected_quads))};
561 SurfaceId ids[] = {root_surface_id_, InvalidSurfaceId()};
562 AggregateAndVerify(
563 expected_passes, arraysize(expected_passes), ids, arraysize(ids));
566 // Tests a reference to a valid surface with no submitted frame. This quad
567 // should also just be dropped.
568 TEST_F(SurfaceAggregatorValidSurfaceTest, ValidSurfaceReferenceWithNoFrame) {
569 SurfaceId surface_with_no_frame_id = allocator_.GenerateId();
570 factory_.Create(surface_with_no_frame_id);
571 test::Quad quads[] = {test::Quad::SolidColorQuad(SK_ColorGREEN),
572 test::Quad::SurfaceQuad(surface_with_no_frame_id, 1.f),
573 test::Quad::SolidColorQuad(SK_ColorBLUE)};
574 test::Pass passes[] = {test::Pass(quads, arraysize(quads))};
576 SubmitCompositorFrame(passes, arraysize(passes), root_surface_id_);
578 test::Quad expected_quads[] = {test::Quad::SolidColorQuad(SK_ColorGREEN),
579 test::Quad::SolidColorQuad(SK_ColorBLUE)};
580 test::Pass expected_passes[] = {
581 test::Pass(expected_quads, arraysize(expected_quads))};
582 SurfaceId ids[] = {root_surface_id_, surface_with_no_frame_id};
583 AggregateAndVerify(
584 expected_passes, arraysize(expected_passes), ids, arraysize(ids));
585 factory_.Destroy(surface_with_no_frame_id);
588 // Tests a surface quad referencing itself, generating a trivial cycle.
589 // The quad creating the cycle should be dropped from the final frame.
590 TEST_F(SurfaceAggregatorValidSurfaceTest, SimpleCyclicalReference) {
591 test::Quad quads[] = {test::Quad::SurfaceQuad(root_surface_id_, 1.f),
592 test::Quad::SolidColorQuad(SK_ColorYELLOW)};
593 test::Pass passes[] = {test::Pass(quads, arraysize(quads))};
595 SubmitCompositorFrame(passes, arraysize(passes), root_surface_id_);
597 test::Quad expected_quads[] = {test::Quad::SolidColorQuad(SK_ColorYELLOW)};
598 test::Pass expected_passes[] = {
599 test::Pass(expected_quads, arraysize(expected_quads))};
600 SurfaceId ids[] = {root_surface_id_};
601 AggregateAndVerify(
602 expected_passes, arraysize(expected_passes), ids, arraysize(ids));
605 // Tests a more complex cycle with one intermediate surface.
606 TEST_F(SurfaceAggregatorValidSurfaceTest, TwoSurfaceCyclicalReference) {
607 SurfaceId child_surface_id = allocator_.GenerateId();
608 factory_.Create(child_surface_id);
610 test::Quad parent_quads[] = {test::Quad::SolidColorQuad(SK_ColorBLUE),
611 test::Quad::SurfaceQuad(child_surface_id, 1.f),
612 test::Quad::SolidColorQuad(SK_ColorCYAN)};
613 test::Pass parent_passes[] = {
614 test::Pass(parent_quads, arraysize(parent_quads))};
616 SubmitCompositorFrame(parent_passes, arraysize(parent_passes),
617 root_surface_id_);
619 test::Quad child_quads[] = {test::Quad::SolidColorQuad(SK_ColorGREEN),
620 test::Quad::SurfaceQuad(root_surface_id_, 1.f),
621 test::Quad::SolidColorQuad(SK_ColorMAGENTA)};
622 test::Pass child_passes[] = {test::Pass(child_quads, arraysize(child_quads))};
624 SubmitCompositorFrame(child_passes, arraysize(child_passes),
625 child_surface_id);
627 // The child surface's reference to the root_surface_ will be dropped, so
628 // we'll end up with:
629 // SK_ColorBLUE from the parent
630 // SK_ColorGREEN from the child
631 // SK_ColorMAGENTA from the child
632 // SK_ColorCYAN from the parent
633 test::Quad expected_quads[] = {test::Quad::SolidColorQuad(SK_ColorBLUE),
634 test::Quad::SolidColorQuad(SK_ColorGREEN),
635 test::Quad::SolidColorQuad(SK_ColorMAGENTA),
636 test::Quad::SolidColorQuad(SK_ColorCYAN)};
637 test::Pass expected_passes[] = {
638 test::Pass(expected_quads, arraysize(expected_quads))};
639 SurfaceId ids[] = {root_surface_id_, child_surface_id};
640 AggregateAndVerify(
641 expected_passes, arraysize(expected_passes), ids, arraysize(ids));
642 factory_.Destroy(child_surface_id);
645 // Tests that we map render pass IDs from different surfaces into a unified
646 // namespace and update RenderPassDrawQuad's id references to match.
647 TEST_F(SurfaceAggregatorValidSurfaceTest, RenderPassIdMapping) {
648 SurfaceId child_surface_id = allocator_.GenerateId();
649 factory_.Create(child_surface_id);
651 RenderPassId child_pass_id[] = {RenderPassId(1, 1), RenderPassId(1, 2)};
652 test::Quad child_quad[][1] = {{test::Quad::SolidColorQuad(SK_ColorGREEN)},
653 {test::Quad::RenderPassQuad(child_pass_id[0])}};
654 test::Pass surface_passes[] = {
655 test::Pass(child_quad[0], arraysize(child_quad[0]), child_pass_id[0]),
656 test::Pass(child_quad[1], arraysize(child_quad[1]), child_pass_id[1])};
658 SubmitCompositorFrame(surface_passes, arraysize(surface_passes),
659 child_surface_id);
661 // Pass IDs from the parent surface may collide with ones from the child.
662 RenderPassId parent_pass_id[] = {RenderPassId(2, 1), RenderPassId(1, 2)};
663 test::Quad parent_quad[][1] = {
664 {test::Quad::SurfaceQuad(child_surface_id, 1.f)},
665 {test::Quad::RenderPassQuad(parent_pass_id[0])}};
666 test::Pass parent_passes[] = {
667 test::Pass(parent_quad[0], arraysize(parent_quad[0]), parent_pass_id[0]),
668 test::Pass(parent_quad[1], arraysize(parent_quad[1]), parent_pass_id[1])};
670 SubmitCompositorFrame(parent_passes, arraysize(parent_passes),
671 root_surface_id_);
672 scoped_ptr<CompositorFrame> aggregated_frame =
673 aggregator_.Aggregate(root_surface_id_);
675 ASSERT_TRUE(aggregated_frame);
676 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
678 DelegatedFrameData* frame_data = aggregated_frame->delegated_frame_data.get();
680 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
682 ASSERT_EQ(3u, aggregated_pass_list.size());
683 RenderPassId actual_pass_ids[] = {aggregated_pass_list[0]->id,
684 aggregated_pass_list[1]->id,
685 aggregated_pass_list[2]->id};
686 // Make sure the aggregated frame's pass IDs are all unique.
687 for (size_t i = 0; i < 3; ++i) {
688 for (size_t j = 0; j < i; ++j) {
689 EXPECT_NE(actual_pass_ids[j], actual_pass_ids[i]) << "pass ids " << i
690 << " and " << j;
694 // Make sure the render pass quads reference the remapped pass IDs.
695 DrawQuad* render_pass_quads[] = {aggregated_pass_list[1]->quad_list.front(),
696 aggregated_pass_list[2]->quad_list.front()};
697 ASSERT_EQ(render_pass_quads[0]->material, DrawQuad::RENDER_PASS);
698 EXPECT_EQ(
699 actual_pass_ids[0],
700 RenderPassDrawQuad::MaterialCast(render_pass_quads[0])->render_pass_id);
702 ASSERT_EQ(render_pass_quads[1]->material, DrawQuad::RENDER_PASS);
703 EXPECT_EQ(
704 actual_pass_ids[1],
705 RenderPassDrawQuad::MaterialCast(render_pass_quads[1])->render_pass_id);
706 factory_.Destroy(child_surface_id);
709 void AddSolidColorQuadWithBlendMode(const gfx::Size& size,
710 RenderPass* pass,
711 const SkXfermode::Mode blend_mode) {
712 const gfx::Transform layer_to_target_transform;
713 const gfx::Size layer_bounds(size);
714 const gfx::Rect visible_layer_rect(size);
715 const gfx::Rect clip_rect(size);
717 bool is_clipped = false;
718 float opacity = 1.f;
720 bool force_anti_aliasing_off = false;
721 SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState();
722 sqs->SetAll(layer_to_target_transform, layer_bounds, visible_layer_rect,
723 clip_rect, is_clipped, opacity, blend_mode, 0);
725 SolidColorDrawQuad* color_quad =
726 pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
727 color_quad->SetNew(pass->shared_quad_state_list.back(), visible_layer_rect,
728 visible_layer_rect, SK_ColorGREEN,
729 force_anti_aliasing_off);
732 // This tests that we update shared quad state pointers correctly within
733 // aggregated passes. The shared quad state list on the aggregated pass will
734 // include the shared quad states from each pass in one list so the quads will
735 // end up pointed to shared quad state objects at different offsets. This test
736 // uses the blend_mode value stored on the shared quad state to track the shared
737 // quad state, but anything saved on the shared quad state would work.
739 // This test has 4 surfaces in the following structure:
740 // root_surface -> quad with kClear_Mode,
741 // [child_one_surface],
742 // quad with kDstOver_Mode,
743 // [child_two_surface],
744 // quad with kDstIn_Mode
745 // child_one_surface -> quad with kSrc_Mode,
746 // [grandchild_surface],
747 // quad with kSrcOver_Mode
748 // child_two_surface -> quad with kSrcIn_Mode
749 // grandchild_surface -> quad with kDst_Mode
751 // Resulting in the following aggregated pass:
752 // quad_root_0 - blend_mode kClear_Mode
753 // quad_child_one_0 - blend_mode kSrc_Mode
754 // quad_grandchild_0 - blend_mode kDst_Mode
755 // quad_child_one_1 - blend_mode kSrcOver_Mode
756 // quad_root_1 - blend_mode kDstOver_Mode
757 // quad_child_two_0 - blend_mode kSrcIn_Mode
758 // quad_root_2 - blend_mode kDstIn_Mode
759 TEST_F(SurfaceAggregatorValidSurfaceTest, AggregateSharedQuadStateProperties) {
760 const SkXfermode::Mode blend_modes[] = {SkXfermode::kClear_Mode, // 0
761 SkXfermode::kSrc_Mode, // 1
762 SkXfermode::kDst_Mode, // 2
763 SkXfermode::kSrcOver_Mode, // 3
764 SkXfermode::kDstOver_Mode, // 4
765 SkXfermode::kSrcIn_Mode, // 5
766 SkXfermode::kDstIn_Mode, // 6
769 RenderPassId pass_id(1, 1);
770 SurfaceId grandchild_surface_id = allocator_.GenerateId();
771 factory_.Create(grandchild_surface_id);
772 scoped_ptr<RenderPass> grandchild_pass = RenderPass::Create();
773 gfx::Rect output_rect(SurfaceSize());
774 gfx::Rect damage_rect(SurfaceSize());
775 gfx::Transform transform_to_root_target;
776 grandchild_pass->SetNew(
777 pass_id, output_rect, damage_rect, transform_to_root_target);
778 AddSolidColorQuadWithBlendMode(
779 SurfaceSize(), grandchild_pass.get(), blend_modes[2]);
780 QueuePassAsFrame(grandchild_pass.Pass(), grandchild_surface_id);
782 SurfaceId child_one_surface_id = allocator_.GenerateId();
783 factory_.Create(child_one_surface_id);
785 scoped_ptr<RenderPass> child_one_pass = RenderPass::Create();
786 child_one_pass->SetNew(
787 pass_id, output_rect, damage_rect, transform_to_root_target);
788 AddSolidColorQuadWithBlendMode(
789 SurfaceSize(), child_one_pass.get(), blend_modes[1]);
790 SurfaceDrawQuad* grandchild_surface_quad =
791 child_one_pass->CreateAndAppendDrawQuad<SurfaceDrawQuad>();
792 grandchild_surface_quad->SetNew(child_one_pass->shared_quad_state_list.back(),
793 gfx::Rect(SurfaceSize()),
794 gfx::Rect(SurfaceSize()),
795 grandchild_surface_id);
796 AddSolidColorQuadWithBlendMode(
797 SurfaceSize(), child_one_pass.get(), blend_modes[3]);
798 QueuePassAsFrame(child_one_pass.Pass(), child_one_surface_id);
800 SurfaceId child_two_surface_id = allocator_.GenerateId();
801 factory_.Create(child_two_surface_id);
803 scoped_ptr<RenderPass> child_two_pass = RenderPass::Create();
804 child_two_pass->SetNew(
805 pass_id, output_rect, damage_rect, transform_to_root_target);
806 AddSolidColorQuadWithBlendMode(
807 SurfaceSize(), child_two_pass.get(), blend_modes[5]);
808 QueuePassAsFrame(child_two_pass.Pass(), child_two_surface_id);
810 scoped_ptr<RenderPass> root_pass = RenderPass::Create();
811 root_pass->SetNew(
812 pass_id, output_rect, damage_rect, transform_to_root_target);
814 AddSolidColorQuadWithBlendMode(
815 SurfaceSize(), root_pass.get(), blend_modes[0]);
816 SurfaceDrawQuad* child_one_surface_quad =
817 root_pass->CreateAndAppendDrawQuad<SurfaceDrawQuad>();
818 child_one_surface_quad->SetNew(root_pass->shared_quad_state_list.back(),
819 gfx::Rect(SurfaceSize()),
820 gfx::Rect(SurfaceSize()),
821 child_one_surface_id);
822 AddSolidColorQuadWithBlendMode(
823 SurfaceSize(), root_pass.get(), blend_modes[4]);
824 SurfaceDrawQuad* child_two_surface_quad =
825 root_pass->CreateAndAppendDrawQuad<SurfaceDrawQuad>();
826 child_two_surface_quad->SetNew(root_pass->shared_quad_state_list.back(),
827 gfx::Rect(SurfaceSize()),
828 gfx::Rect(SurfaceSize()),
829 child_two_surface_id);
830 AddSolidColorQuadWithBlendMode(
831 SurfaceSize(), root_pass.get(), blend_modes[6]);
833 QueuePassAsFrame(root_pass.Pass(), root_surface_id_);
835 scoped_ptr<CompositorFrame> aggregated_frame =
836 aggregator_.Aggregate(root_surface_id_);
838 ASSERT_TRUE(aggregated_frame);
839 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
841 DelegatedFrameData* frame_data = aggregated_frame->delegated_frame_data.get();
843 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
845 ASSERT_EQ(1u, aggregated_pass_list.size());
847 const QuadList& aggregated_quad_list = aggregated_pass_list[0]->quad_list;
849 ASSERT_EQ(7u, aggregated_quad_list.size());
851 for (auto iter = aggregated_quad_list.cbegin();
852 iter != aggregated_quad_list.cend();
853 ++iter) {
854 EXPECT_EQ(blend_modes[iter.index()], iter->shared_quad_state->blend_mode)
855 << iter.index();
857 factory_.Destroy(child_one_surface_id);
858 factory_.Destroy(child_two_surface_id);
859 factory_.Destroy(grandchild_surface_id);
862 // This tests that when aggregating a frame with multiple render passes that we
863 // map the transforms for the root pass but do not modify the transform on child
864 // passes.
866 // The root surface has one pass with a surface quad transformed by +10 in the y
867 // direction.
869 // The middle surface has one pass with a surface quad scaled by 2 in the x
870 // and 3 in the y directions.
872 // The child surface has two passes. The first pass has a quad with a transform
873 // of +5 in the x direction. The second pass has a reference to the first pass'
874 // pass id and a transform of +8 in the x direction.
876 // After aggregation, the child surface's root pass quad should have all
877 // transforms concatenated for a total transform of +23 x, +10 y. The
878 // contributing render pass' transform in the aggregate frame should not be
879 // affected.
880 TEST_F(SurfaceAggregatorValidSurfaceTest, AggregateMultiplePassWithTransform) {
881 // Innermost child surface.
882 SurfaceId child_surface_id = allocator_.GenerateId();
884 factory_.Create(child_surface_id);
885 RenderPassId child_pass_id[] = {RenderPassId(1, 1), RenderPassId(1, 2)};
886 test::Quad child_quads[][1] = {
887 {test::Quad::SolidColorQuad(SK_ColorGREEN)},
888 {test::Quad::RenderPassQuad(child_pass_id[0])},
890 test::Pass child_passes[] = {
891 test::Pass(child_quads[0], arraysize(child_quads[0]), child_pass_id[0]),
892 test::Pass(child_quads[1], arraysize(child_quads[1]),
893 child_pass_id[1])};
895 RenderPassList child_pass_list;
896 AddPasses(&child_pass_list, gfx::Rect(SurfaceSize()), child_passes,
897 arraysize(child_passes));
899 RenderPass* child_nonroot_pass = child_pass_list.at(0u);
900 child_nonroot_pass->transform_to_root_target.Translate(8, 0);
901 SharedQuadState* child_nonroot_pass_sqs =
902 child_nonroot_pass->shared_quad_state_list.front();
903 child_nonroot_pass_sqs->quad_to_target_transform.Translate(5, 0);
905 RenderPass* child_root_pass = child_pass_list.at(1u);
906 SharedQuadState* child_root_pass_sqs =
907 child_root_pass->shared_quad_state_list.front();
908 child_root_pass_sqs->quad_to_target_transform.Translate(8, 0);
909 child_root_pass_sqs->is_clipped = true;
910 child_root_pass_sqs->clip_rect = gfx::Rect(0, 0, 5, 5);
912 scoped_ptr<DelegatedFrameData> child_frame_data(new DelegatedFrameData);
913 child_pass_list.swap(child_frame_data->render_pass_list);
915 scoped_ptr<CompositorFrame> child_frame(new CompositorFrame);
916 child_frame->delegated_frame_data = child_frame_data.Pass();
918 factory_.SubmitCompositorFrame(child_surface_id, child_frame.Pass(),
919 SurfaceFactory::DrawCallback());
922 // Middle child surface.
923 SurfaceId middle_surface_id = allocator_.GenerateId();
925 factory_.Create(middle_surface_id);
926 test::Quad middle_quads[] = {
927 test::Quad::SurfaceQuad(child_surface_id, 1.f)};
928 test::Pass middle_passes[] = {
929 test::Pass(middle_quads, arraysize(middle_quads)),
932 RenderPassList middle_pass_list;
933 AddPasses(&middle_pass_list, gfx::Rect(SurfaceSize()), middle_passes,
934 arraysize(middle_passes));
936 RenderPass* middle_root_pass = middle_pass_list.at(0u);
937 middle_root_pass->quad_list.ElementAt(0)->visible_rect =
938 gfx::Rect(0, 1, 100, 7);
939 SharedQuadState* middle_root_pass_sqs =
940 middle_root_pass->shared_quad_state_list.front();
941 middle_root_pass_sqs->quad_to_target_transform.Scale(2, 3);
943 scoped_ptr<DelegatedFrameData> middle_frame_data(new DelegatedFrameData);
944 middle_pass_list.swap(middle_frame_data->render_pass_list);
946 scoped_ptr<CompositorFrame> middle_frame(new CompositorFrame);
947 middle_frame->delegated_frame_data = middle_frame_data.Pass();
949 factory_.SubmitCompositorFrame(middle_surface_id, middle_frame.Pass(),
950 SurfaceFactory::DrawCallback());
953 // Root surface.
954 test::Quad secondary_quads[] = {
955 test::Quad::SolidColorQuad(1),
956 test::Quad::SurfaceQuad(middle_surface_id, 1.f)};
957 test::Quad root_quads[] = {test::Quad::SolidColorQuad(1)};
958 test::Pass root_passes[] = {
959 test::Pass(secondary_quads, arraysize(secondary_quads)),
960 test::Pass(root_quads, arraysize(root_quads))};
962 RenderPassList root_pass_list;
963 AddPasses(&root_pass_list,
964 gfx::Rect(SurfaceSize()),
965 root_passes,
966 arraysize(root_passes));
968 root_pass_list.at(0)
969 ->shared_quad_state_list.front()
970 ->quad_to_target_transform.Translate(0, 7);
971 root_pass_list.at(0)
972 ->shared_quad_state_list.ElementAt(1)
973 ->quad_to_target_transform.Translate(0, 10);
974 root_pass_list.at(0)->quad_list.ElementAt(1)->visible_rect =
975 gfx::Rect(0, 0, 8, 100);
977 root_pass_list[0]->transform_to_root_target.Translate(10, 5);
979 scoped_ptr<DelegatedFrameData> root_frame_data(new DelegatedFrameData);
980 root_pass_list.swap(root_frame_data->render_pass_list);
982 scoped_ptr<CompositorFrame> root_frame(new CompositorFrame);
983 root_frame->delegated_frame_data = root_frame_data.Pass();
985 factory_.SubmitCompositorFrame(root_surface_id_, root_frame.Pass(),
986 SurfaceFactory::DrawCallback());
988 scoped_ptr<CompositorFrame> aggregated_frame =
989 aggregator_.Aggregate(root_surface_id_);
991 ASSERT_TRUE(aggregated_frame);
992 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
994 DelegatedFrameData* frame_data = aggregated_frame->delegated_frame_data.get();
996 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
998 ASSERT_EQ(3u, aggregated_pass_list.size());
1000 ASSERT_EQ(1u, aggregated_pass_list[0]->shared_quad_state_list.size());
1002 // The first pass should have one shared quad state for the one solid color
1003 // quad.
1004 EXPECT_EQ(1u, aggregated_pass_list[0]->shared_quad_state_list.size());
1005 // The second pass should have just two shared quad states. We'll
1006 // verify the properties through the quads.
1007 EXPECT_EQ(2u, aggregated_pass_list[1]->shared_quad_state_list.size());
1009 EXPECT_EQ(1u, aggregated_pass_list[2]->shared_quad_state_list.size());
1011 SharedQuadState* aggregated_first_pass_sqs =
1012 aggregated_pass_list[0]->shared_quad_state_list.front();
1014 // The first pass's transform should be unaffected by the embedding and still
1015 // be a translation by +5 in the x direction.
1016 gfx::Transform expected_aggregated_first_pass_sqs_transform;
1017 expected_aggregated_first_pass_sqs_transform.Translate(5, 0);
1018 EXPECT_EQ(expected_aggregated_first_pass_sqs_transform.ToString(),
1019 aggregated_first_pass_sqs->quad_to_target_transform.ToString());
1021 // The first pass's transform to the root target should include the aggregated
1022 // transform, including the transform from the child pass to the root.
1023 gfx::Transform expected_first_pass_transform_to_root_target;
1024 expected_first_pass_transform_to_root_target.Translate(10, 5);
1025 expected_first_pass_transform_to_root_target.Translate(0, 10);
1026 expected_first_pass_transform_to_root_target.Scale(2, 3);
1027 expected_first_pass_transform_to_root_target.Translate(8, 0);
1028 EXPECT_EQ(expected_first_pass_transform_to_root_target.ToString(),
1029 aggregated_pass_list[0]->transform_to_root_target.ToString());
1031 ASSERT_EQ(2u, aggregated_pass_list[1]->quad_list.size());
1033 gfx::Transform expected_root_pass_quad_transforms[2];
1034 // The first quad in the root pass is the solid color quad from the original
1035 // root surface. Its transform should be unaffected by the aggregation and
1036 // still be +7 in the y direction.
1037 expected_root_pass_quad_transforms[0].Translate(0, 7);
1038 // The second quad in the root pass is aggregated from the child surface so
1039 // its transform should be the combination of its original translation
1040 // (0, 10), the middle surface draw quad's scale of (2, 3), and the
1041 // child surface draw quad's translation (8, 0).
1042 expected_root_pass_quad_transforms[1].Translate(0, 10);
1043 expected_root_pass_quad_transforms[1].Scale(2, 3);
1044 expected_root_pass_quad_transforms[1].Translate(8, 0);
1046 for (auto iter = aggregated_pass_list[1]->quad_list.cbegin();
1047 iter != aggregated_pass_list[1]->quad_list.cend();
1048 ++iter) {
1049 EXPECT_EQ(expected_root_pass_quad_transforms[iter.index()].ToString(),
1050 iter->shared_quad_state->quad_to_target_transform.ToString())
1051 << iter.index();
1054 EXPECT_TRUE(
1055 aggregated_pass_list[1]->shared_quad_state_list.ElementAt(1)->is_clipped);
1057 // The second quad in the root pass is aggregated from the child, so its
1058 // clip rect must be transformed by the child's translation/scale and
1059 // clipped be the visible_rects for both children.
1060 EXPECT_EQ(gfx::Rect(0, 13, 8, 12).ToString(),
1061 aggregated_pass_list[1]
1062 ->shared_quad_state_list.ElementAt(1)
1063 ->clip_rect.ToString());
1065 factory_.Destroy(middle_surface_id);
1066 factory_.Destroy(child_surface_id);
1069 // Tests that damage rects are aggregated correctly when surfaces change.
1070 TEST_F(SurfaceAggregatorValidSurfaceTest, AggregateDamageRect) {
1071 test::Quad child_quads[] = {test::Quad::RenderPassQuad(RenderPassId(1, 1))};
1072 test::Pass child_passes[] = {
1073 test::Pass(child_quads, arraysize(child_quads), RenderPassId(1, 1))};
1075 RenderPassList child_pass_list;
1076 AddPasses(&child_pass_list,
1077 gfx::Rect(SurfaceSize()),
1078 child_passes,
1079 arraysize(child_passes));
1081 RenderPass* child_root_pass = child_pass_list.at(0u);
1082 SharedQuadState* child_root_pass_sqs =
1083 child_root_pass->shared_quad_state_list.front();
1084 child_root_pass_sqs->quad_to_target_transform.Translate(8, 0);
1086 scoped_ptr<DelegatedFrameData> child_frame_data(new DelegatedFrameData);
1087 child_pass_list.swap(child_frame_data->render_pass_list);
1089 scoped_ptr<CompositorFrame> child_frame(new CompositorFrame);
1090 child_frame->delegated_frame_data = child_frame_data.Pass();
1092 SurfaceId child_surface_id = allocator_.GenerateId();
1093 factory_.Create(child_surface_id);
1094 factory_.SubmitCompositorFrame(child_surface_id, child_frame.Pass(),
1095 SurfaceFactory::DrawCallback());
1097 test::Quad parent_surface_quads[] = {
1098 test::Quad::SurfaceQuad(child_surface_id, 1.f)};
1099 test::Pass parent_surface_passes[] = {
1100 test::Pass(parent_surface_quads, arraysize(parent_surface_quads),
1101 RenderPassId(1, 1))};
1103 RenderPassList parent_surface_pass_list;
1104 AddPasses(&parent_surface_pass_list,
1105 gfx::Rect(SurfaceSize()),
1106 parent_surface_passes,
1107 arraysize(parent_surface_passes));
1109 // Parent surface is only used to test if the transform is applied correctly
1110 // to the child surface's damage.
1111 scoped_ptr<DelegatedFrameData> parent_surface_frame_data(
1112 new DelegatedFrameData);
1113 parent_surface_pass_list.swap(parent_surface_frame_data->render_pass_list);
1115 scoped_ptr<CompositorFrame> parent_surface_frame(new CompositorFrame);
1116 parent_surface_frame->delegated_frame_data = parent_surface_frame_data.Pass();
1118 SurfaceId parent_surface_id = allocator_.GenerateId();
1119 factory_.Create(parent_surface_id);
1120 factory_.SubmitCompositorFrame(parent_surface_id, parent_surface_frame.Pass(),
1121 SurfaceFactory::DrawCallback());
1123 test::Quad root_surface_quads[] = {
1124 test::Quad::SurfaceQuad(parent_surface_id, 1.f)};
1125 test::Quad root_render_pass_quads[] = {
1126 test::Quad::RenderPassQuad(RenderPassId(1, 1))};
1128 test::Pass root_passes[] = {
1129 test::Pass(root_surface_quads, arraysize(root_surface_quads),
1130 RenderPassId(1, 1)),
1131 test::Pass(root_render_pass_quads, arraysize(root_render_pass_quads),
1132 RenderPassId(2, 1))};
1134 RenderPassList root_pass_list;
1135 AddPasses(&root_pass_list,
1136 gfx::Rect(SurfaceSize()),
1137 root_passes,
1138 arraysize(root_passes));
1140 root_pass_list.at(0)
1141 ->shared_quad_state_list.front()
1142 ->quad_to_target_transform.Translate(0, 10);
1143 root_pass_list.at(0)->damage_rect = gfx::Rect(5, 5, 10, 10);
1144 root_pass_list.at(1)->damage_rect = gfx::Rect(5, 5, 100, 100);
1146 scoped_ptr<DelegatedFrameData> root_frame_data(new DelegatedFrameData);
1147 root_pass_list.swap(root_frame_data->render_pass_list);
1149 scoped_ptr<CompositorFrame> root_frame(new CompositorFrame);
1150 root_frame->delegated_frame_data = root_frame_data.Pass();
1152 factory_.SubmitCompositorFrame(root_surface_id_, root_frame.Pass(),
1153 SurfaceFactory::DrawCallback());
1155 scoped_ptr<CompositorFrame> aggregated_frame =
1156 aggregator_.Aggregate(root_surface_id_);
1158 ASSERT_TRUE(aggregated_frame);
1159 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
1161 DelegatedFrameData* frame_data = aggregated_frame->delegated_frame_data.get();
1163 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
1165 ASSERT_EQ(2u, aggregated_pass_list.size());
1167 // Damage rect for first aggregation should contain entire root surface.
1168 EXPECT_TRUE(
1169 aggregated_pass_list[1]->damage_rect.Contains(gfx::Rect(SurfaceSize())));
1172 AddPasses(&child_pass_list,
1173 gfx::Rect(SurfaceSize()),
1174 child_passes,
1175 arraysize(child_passes));
1177 RenderPass* child_root_pass = child_pass_list.at(0u);
1178 SharedQuadState* child_root_pass_sqs =
1179 child_root_pass->shared_quad_state_list.front();
1180 child_root_pass_sqs->quad_to_target_transform.Translate(8, 0);
1181 child_root_pass->damage_rect = gfx::Rect(10, 10, 10, 10);
1183 scoped_ptr<DelegatedFrameData> child_frame_data(new DelegatedFrameData);
1184 child_pass_list.swap(child_frame_data->render_pass_list);
1186 scoped_ptr<CompositorFrame> child_frame(new CompositorFrame);
1187 child_frame->delegated_frame_data = child_frame_data.Pass();
1189 factory_.SubmitCompositorFrame(child_surface_id, child_frame.Pass(),
1190 SurfaceFactory::DrawCallback());
1192 scoped_ptr<CompositorFrame> aggregated_frame =
1193 aggregator_.Aggregate(root_surface_id_);
1195 ASSERT_TRUE(aggregated_frame);
1196 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
1198 DelegatedFrameData* frame_data =
1199 aggregated_frame->delegated_frame_data.get();
1201 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
1203 ASSERT_EQ(2u, aggregated_pass_list.size());
1205 // Outer surface didn't change, so transformed inner damage rect should be
1206 // used.
1207 EXPECT_EQ(gfx::Rect(10, 20, 10, 10).ToString(),
1208 aggregated_pass_list[1]->damage_rect.ToString());
1212 RenderPassList root_pass_list;
1213 AddPasses(&root_pass_list,
1214 gfx::Rect(SurfaceSize()),
1215 root_passes,
1216 arraysize(root_passes));
1218 root_pass_list.at(0)
1219 ->shared_quad_state_list.front()
1220 ->quad_to_target_transform.Translate(0, 10);
1221 root_pass_list.at(0)->damage_rect = gfx::Rect(0, 0, 1, 1);
1223 scoped_ptr<DelegatedFrameData> root_frame_data(new DelegatedFrameData);
1224 root_pass_list.swap(root_frame_data->render_pass_list);
1226 scoped_ptr<CompositorFrame> root_frame(new CompositorFrame);
1227 root_frame->delegated_frame_data = root_frame_data.Pass();
1229 factory_.SubmitCompositorFrame(root_surface_id_, root_frame.Pass(),
1230 SurfaceFactory::DrawCallback());
1234 RenderPassList root_pass_list;
1235 AddPasses(&root_pass_list,
1236 gfx::Rect(SurfaceSize()),
1237 root_passes,
1238 arraysize(root_passes));
1240 root_pass_list.at(0)
1241 ->shared_quad_state_list.front()
1242 ->quad_to_target_transform.Translate(0, 10);
1243 root_pass_list.at(0)->damage_rect = gfx::Rect(1, 1, 1, 1);
1245 scoped_ptr<DelegatedFrameData> root_frame_data(new DelegatedFrameData);
1246 root_pass_list.swap(root_frame_data->render_pass_list);
1248 scoped_ptr<CompositorFrame> root_frame(new CompositorFrame);
1249 root_frame->delegated_frame_data = root_frame_data.Pass();
1251 factory_.SubmitCompositorFrame(root_surface_id_, root_frame.Pass(),
1252 SurfaceFactory::DrawCallback());
1254 scoped_ptr<CompositorFrame> aggregated_frame =
1255 aggregator_.Aggregate(root_surface_id_);
1257 ASSERT_TRUE(aggregated_frame);
1258 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
1260 DelegatedFrameData* frame_data =
1261 aggregated_frame->delegated_frame_data.get();
1263 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
1265 ASSERT_EQ(2u, aggregated_pass_list.size());
1267 // The root surface was enqueued without being aggregated once, so it should
1268 // be treated as completely damaged.
1269 EXPECT_TRUE(aggregated_pass_list[1]->damage_rect.Contains(
1270 gfx::Rect(SurfaceSize())));
1273 // No Surface changed, so no damage should be given.
1275 scoped_ptr<CompositorFrame> aggregated_frame =
1276 aggregator_.Aggregate(root_surface_id_);
1278 ASSERT_TRUE(aggregated_frame);
1279 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
1281 DelegatedFrameData* frame_data =
1282 aggregated_frame->delegated_frame_data.get();
1284 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
1286 ASSERT_EQ(2u, aggregated_pass_list.size());
1288 EXPECT_TRUE(aggregated_pass_list[1]->damage_rect.IsEmpty());
1291 // SetFullDamageRectForSurface should cause the entire output to be
1292 // marked as damaged.
1294 aggregator_.SetFullDamageForSurface(root_surface_id_);
1295 scoped_ptr<CompositorFrame> aggregated_frame =
1296 aggregator_.Aggregate(root_surface_id_);
1298 ASSERT_TRUE(aggregated_frame);
1299 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
1301 DelegatedFrameData* frame_data =
1302 aggregated_frame->delegated_frame_data.get();
1304 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
1306 ASSERT_EQ(2u, aggregated_pass_list.size());
1308 EXPECT_TRUE(aggregated_pass_list[1]->damage_rect.Contains(
1309 gfx::Rect(SurfaceSize())));
1312 factory_.Destroy(child_surface_id);
1315 class SurfaceAggregatorPartialSwapTest
1316 : public SurfaceAggregatorValidSurfaceTest {
1317 public:
1318 SurfaceAggregatorPartialSwapTest()
1319 : SurfaceAggregatorValidSurfaceTest(true) {}
1322 // Tests that quads outside the damage rect are ignored.
1323 TEST_F(SurfaceAggregatorPartialSwapTest, IgnoreOutside) {
1324 SurfaceId child_surface_id = allocator_.GenerateId();
1325 factory_.Create(child_surface_id);
1326 // The child surface has two quads, one with a visible rect of 13,13 4x4 and
1327 // the other other with a visible rect of 10,10 2x2 (relative to root target
1328 // space).
1330 RenderPassId child_pass_id = RenderPassId(1, 1);
1331 test::Quad child_quads1[] = {test::Quad::RenderPassQuad(child_pass_id)};
1332 test::Quad child_quads2[] = {test::Quad::RenderPassQuad(child_pass_id)};
1333 test::Pass child_passes[] = {
1334 test::Pass(child_quads1, arraysize(child_quads1), child_pass_id),
1335 test::Pass(child_quads2, arraysize(child_quads2), child_pass_id)};
1337 RenderPassList child_pass_list;
1338 AddPasses(&child_pass_list, gfx::Rect(SurfaceSize()), child_passes,
1339 arraysize(child_passes));
1341 child_pass_list.at(0u)->quad_list.ElementAt(0)->visible_rect =
1342 gfx::Rect(1, 1, 2, 2);
1343 SharedQuadState* child_sqs =
1344 child_pass_list.at(0u)->shared_quad_state_list.ElementAt(0u);
1345 child_sqs->quad_to_target_transform.Translate(1, 1);
1346 child_sqs->quad_to_target_transform.Scale(2, 2);
1348 child_pass_list.at(1u)->quad_list.ElementAt(0)->visible_rect =
1349 gfx::Rect(0, 0, 2, 2);
1351 SubmitPassListAsFrame(child_surface_id, &child_pass_list);
1355 test::Quad root_quads[] = {test::Quad::SurfaceQuad(child_surface_id, 1.f)};
1357 test::Pass root_passes[] = {test::Pass(root_quads, arraysize(root_quads))};
1359 RenderPassList root_pass_list;
1360 AddPasses(&root_pass_list, gfx::Rect(SurfaceSize()), root_passes,
1361 arraysize(root_passes));
1363 RenderPass* root_pass = root_pass_list.at(0u);
1364 root_pass->shared_quad_state_list.front()
1365 ->quad_to_target_transform.Translate(10, 10);
1366 root_pass->damage_rect = gfx::Rect(0, 0, 1, 1);
1368 SubmitPassListAsFrame(root_surface_id_, &root_pass_list);
1371 scoped_ptr<CompositorFrame> aggregated_frame =
1372 aggregator_.Aggregate(root_surface_id_);
1374 ASSERT_TRUE(aggregated_frame);
1375 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
1377 DelegatedFrameData* frame_data = aggregated_frame->delegated_frame_data.get();
1379 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
1381 ASSERT_EQ(2u, aggregated_pass_list.size());
1383 // Damage rect for first aggregation should contain entire root surface.
1384 EXPECT_EQ(gfx::Rect(0, 0, 15, 15), aggregated_pass_list[1]->damage_rect);
1385 EXPECT_EQ(1u, aggregated_pass_list[0]->quad_list.size());
1386 EXPECT_EQ(1u, aggregated_pass_list[1]->quad_list.size());
1388 // Create a root surface with a smaller damage rect.
1390 test::Quad root_quads[] = {test::Quad::SurfaceQuad(child_surface_id, 1.f)};
1392 test::Pass root_passes[] = {test::Pass(root_quads, arraysize(root_quads))};
1394 RenderPassList root_pass_list;
1395 AddPasses(&root_pass_list, gfx::Rect(SurfaceSize()), root_passes,
1396 arraysize(root_passes));
1398 RenderPass* root_pass = root_pass_list.at(0u);
1399 root_pass->shared_quad_state_list.front()
1400 ->quad_to_target_transform.Translate(10, 10);
1401 root_pass->damage_rect = gfx::Rect(10, 10, 2, 2);
1402 SubmitPassListAsFrame(root_surface_id_, &root_pass_list);
1406 scoped_ptr<CompositorFrame> aggregated_frame =
1407 aggregator_.Aggregate(root_surface_id_);
1409 ASSERT_TRUE(aggregated_frame);
1410 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
1412 DelegatedFrameData* frame_data =
1413 aggregated_frame->delegated_frame_data.get();
1415 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
1417 ASSERT_EQ(2u, aggregated_pass_list.size());
1419 // Only first quad from surface is inside damage rect and should be
1420 // included.
1421 EXPECT_EQ(gfx::Rect(10, 10, 2, 2), aggregated_pass_list[1]->damage_rect);
1422 EXPECT_EQ(0u, aggregated_pass_list[0]->quad_list.size());
1423 EXPECT_EQ(1u, aggregated_pass_list[1]->quad_list.size());
1424 EXPECT_EQ(gfx::Rect(0, 0, 2, 2),
1425 aggregated_pass_list[1]->quad_list.back()->visible_rect);
1428 // New child frame has same content and no damage, but has a
1429 // CopyOutputRequest.
1431 RenderPassId child_pass_id = RenderPassId(1, 1);
1432 test::Quad child_quads1[] = {test::Quad::RenderPassQuad(child_pass_id)};
1433 test::Quad child_quads2[] = {test::Quad::RenderPassQuad(child_pass_id)};
1434 test::Pass child_passes[] = {
1435 test::Pass(child_quads1, arraysize(child_quads1), child_pass_id),
1436 test::Pass(child_quads2, arraysize(child_quads2), child_pass_id)};
1438 RenderPassList child_pass_list;
1439 AddPasses(&child_pass_list, gfx::Rect(SurfaceSize()), child_passes,
1440 arraysize(child_passes));
1442 child_pass_list.at(0u)->quad_list.ElementAt(0)->visible_rect =
1443 gfx::Rect(1, 1, 2, 2);
1444 SharedQuadState* child_sqs =
1445 child_pass_list.at(0u)->shared_quad_state_list.ElementAt(0u);
1446 child_sqs->quad_to_target_transform.Translate(1, 1);
1447 child_sqs->quad_to_target_transform.Scale(2, 2);
1449 child_pass_list.at(1u)->quad_list.ElementAt(0)->visible_rect =
1450 gfx::Rect(0, 0, 2, 2);
1452 RenderPass* child_root_pass = child_pass_list.at(1u);
1454 child_root_pass->copy_requests.push_back(
1455 CopyOutputRequest::CreateEmptyRequest());
1456 child_root_pass->damage_rect = gfx::Rect();
1457 SubmitPassListAsFrame(child_surface_id, &child_pass_list);
1461 scoped_ptr<CompositorFrame> aggregated_frame =
1462 aggregator_.Aggregate(root_surface_id_);
1464 ASSERT_TRUE(aggregated_frame);
1465 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
1467 DelegatedFrameData* frame_data =
1468 aggregated_frame->delegated_frame_data.get();
1470 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
1472 // Output frame should have no damage, but all quads included.
1473 ASSERT_EQ(3u, aggregated_pass_list.size());
1475 EXPECT_TRUE(aggregated_pass_list[1]->damage_rect.IsEmpty());
1476 ASSERT_EQ(1u, aggregated_pass_list[0]->quad_list.size());
1477 ASSERT_EQ(1u, aggregated_pass_list[1]->quad_list.size());
1478 EXPECT_EQ(gfx::Rect(1, 1, 2, 2),
1479 aggregated_pass_list[0]->quad_list.ElementAt(0)->visible_rect);
1480 EXPECT_EQ(gfx::Rect(0, 0, 2, 2),
1481 aggregated_pass_list[1]->quad_list.ElementAt(0)->visible_rect);
1485 scoped_ptr<CompositorFrame> aggregated_frame =
1486 aggregator_.Aggregate(root_surface_id_);
1488 ASSERT_TRUE(aggregated_frame);
1489 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
1491 DelegatedFrameData* frame_data =
1492 aggregated_frame->delegated_frame_data.get();
1494 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
1495 // There were no changes since last aggregation, so output should be empty
1496 // and have no damage.
1497 ASSERT_EQ(1u, aggregated_pass_list.size());
1498 EXPECT_TRUE(aggregated_pass_list[0]->damage_rect.IsEmpty());
1499 ASSERT_EQ(0u, aggregated_pass_list[0]->quad_list.size());
1502 factory_.Destroy(child_surface_id);
1505 class SurfaceAggregatorWithResourcesTest : public testing::Test {
1506 public:
1507 void SetUp() override {
1508 output_surface_ = FakeOutputSurface::CreateSoftware(
1509 make_scoped_ptr(new SoftwareOutputDevice));
1510 output_surface_->BindToClient(&output_surface_client_);
1511 shared_bitmap_manager_.reset(new TestSharedBitmapManager);
1513 resource_provider_ = FakeResourceProvider::Create(
1514 output_surface_.get(), shared_bitmap_manager_.get());
1516 aggregator_.reset(
1517 new SurfaceAggregator(&manager_, resource_provider_.get(), false));
1520 protected:
1521 SurfaceManager manager_;
1522 FakeOutputSurfaceClient output_surface_client_;
1523 scoped_ptr<OutputSurface> output_surface_;
1524 scoped_ptr<SharedBitmapManager> shared_bitmap_manager_;
1525 scoped_ptr<ResourceProvider> resource_provider_;
1526 scoped_ptr<SurfaceAggregator> aggregator_;
1529 class ResourceTrackingSurfaceFactoryClient : public SurfaceFactoryClient {
1530 public:
1531 ResourceTrackingSurfaceFactoryClient() {}
1532 ~ResourceTrackingSurfaceFactoryClient() override {}
1534 void ReturnResources(const ReturnedResourceArray& resources) override {
1535 returned_resources_ = resources;
1538 ReturnedResourceArray returned_resources() const {
1539 return returned_resources_;
1542 private:
1543 ReturnedResourceArray returned_resources_;
1545 DISALLOW_COPY_AND_ASSIGN(ResourceTrackingSurfaceFactoryClient);
1548 void SubmitCompositorFrameWithResources(ResourceId* resource_ids,
1549 size_t num_resource_ids,
1550 bool valid,
1551 SurfaceId child_id,
1552 SurfaceFactory* factory,
1553 SurfaceId surface_id) {
1554 scoped_ptr<DelegatedFrameData> frame_data(new DelegatedFrameData);
1555 scoped_ptr<RenderPass> pass = RenderPass::Create();
1556 pass->id = RenderPassId(1, 1);
1557 SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState();
1558 sqs->opacity = 1.f;
1559 if (!child_id.is_null()) {
1560 SurfaceDrawQuad* surface_quad =
1561 pass->CreateAndAppendDrawQuad<SurfaceDrawQuad>();
1562 surface_quad->SetNew(sqs, gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1),
1563 child_id);
1566 for (size_t i = 0u; i < num_resource_ids; ++i) {
1567 TransferableResource resource;
1568 resource.id = resource_ids[i];
1569 // ResourceProvider is software, so only software resources are valid.
1570 resource.is_software = valid;
1571 frame_data->resource_list.push_back(resource);
1572 TextureDrawQuad* quad = pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
1573 const gfx::Rect rect;
1574 const gfx::Rect opaque_rect;
1575 const gfx::Rect visible_rect;
1576 bool needs_blending = false;
1577 bool premultiplied_alpha = false;
1578 const gfx::PointF uv_top_left;
1579 const gfx::PointF uv_bottom_right;
1580 SkColor background_color = SK_ColorGREEN;
1581 const float vertex_opacity[4] = {0.f, 0.f, 1.f, 1.f};
1582 bool flipped = false;
1583 bool nearest_neighbor = false;
1584 quad->SetAll(sqs, rect, opaque_rect, visible_rect, needs_blending,
1585 resource_ids[i], gfx::Size(), false, premultiplied_alpha,
1586 uv_top_left, uv_bottom_right, background_color, vertex_opacity,
1587 flipped, nearest_neighbor);
1589 frame_data->render_pass_list.push_back(pass.Pass());
1590 scoped_ptr<CompositorFrame> frame(new CompositorFrame);
1591 frame->delegated_frame_data = frame_data.Pass();
1592 factory->SubmitCompositorFrame(surface_id, frame.Pass(),
1593 SurfaceFactory::DrawCallback());
1596 TEST_F(SurfaceAggregatorWithResourcesTest, TakeResourcesOneSurface) {
1597 ResourceTrackingSurfaceFactoryClient client;
1598 SurfaceFactory factory(&manager_, &client);
1599 SurfaceId surface_id(7u);
1600 factory.Create(surface_id);
1602 ResourceId ids[] = {11, 12, 13};
1603 SubmitCompositorFrameWithResources(ids, arraysize(ids), true, SurfaceId(),
1604 &factory, surface_id);
1606 scoped_ptr<CompositorFrame> frame = aggregator_->Aggregate(surface_id);
1608 // Nothing should be available to be returned yet.
1609 EXPECT_TRUE(client.returned_resources().empty());
1611 SubmitCompositorFrameWithResources(NULL, 0u, true, SurfaceId(), &factory,
1612 surface_id);
1614 frame = aggregator_->Aggregate(surface_id);
1616 ASSERT_EQ(3u, client.returned_resources().size());
1617 ResourceId returned_ids[3];
1618 for (size_t i = 0; i < 3; ++i) {
1619 returned_ids[i] = client.returned_resources()[i].id;
1621 EXPECT_THAT(returned_ids,
1622 testing::WhenSorted(testing::ElementsAreArray(ids)));
1623 factory.Destroy(surface_id);
1626 TEST_F(SurfaceAggregatorWithResourcesTest, TakeInvalidResources) {
1627 ResourceTrackingSurfaceFactoryClient client;
1628 SurfaceFactory factory(&manager_, &client);
1629 SurfaceId surface_id(7u);
1630 factory.Create(surface_id);
1632 scoped_ptr<DelegatedFrameData> frame_data(new DelegatedFrameData);
1633 scoped_ptr<RenderPass> pass = RenderPass::Create();
1634 pass->id = RenderPassId(1, 1);
1635 TransferableResource resource;
1636 resource.id = 11;
1637 // ResourceProvider is software but resource is not, so it should be
1638 // ignored.
1639 resource.is_software = false;
1640 frame_data->resource_list.push_back(resource);
1641 frame_data->render_pass_list.push_back(pass.Pass());
1642 scoped_ptr<CompositorFrame> frame(new CompositorFrame);
1643 frame->delegated_frame_data = frame_data.Pass();
1644 factory.SubmitCompositorFrame(surface_id, frame.Pass(),
1645 SurfaceFactory::DrawCallback());
1647 scoped_ptr<CompositorFrame> returned_frame =
1648 aggregator_->Aggregate(surface_id);
1650 // Nothing should be available to be returned yet.
1651 EXPECT_TRUE(client.returned_resources().empty());
1653 SubmitCompositorFrameWithResources(NULL, 0, true, SurfaceId(), &factory,
1654 surface_id);
1655 ASSERT_EQ(1u, client.returned_resources().size());
1656 EXPECT_EQ(11u, client.returned_resources()[0].id);
1658 factory.Destroy(surface_id);
1661 TEST_F(SurfaceAggregatorWithResourcesTest, TwoSurfaces) {
1662 ResourceTrackingSurfaceFactoryClient client;
1663 SurfaceFactory factory(&manager_, &client);
1664 SurfaceId surface_id(7u);
1665 factory.Create(surface_id);
1666 SurfaceId surface_id2(8u);
1667 factory.Create(surface_id2);
1669 ResourceId ids[] = {11, 12, 13};
1670 SubmitCompositorFrameWithResources(ids, arraysize(ids), true, SurfaceId(),
1671 &factory, surface_id);
1672 ResourceId ids2[] = {14, 15, 16};
1673 SubmitCompositorFrameWithResources(ids2, arraysize(ids2), true, SurfaceId(),
1674 &factory, surface_id2);
1676 scoped_ptr<CompositorFrame> frame = aggregator_->Aggregate(surface_id);
1678 SubmitCompositorFrameWithResources(NULL, 0, true, SurfaceId(), &factory,
1679 surface_id);
1681 // Nothing should be available to be returned yet.
1682 EXPECT_TRUE(client.returned_resources().empty());
1684 frame = aggregator_->Aggregate(surface_id2);
1686 // surface_id wasn't referenced, so its resources should be returned.
1687 ASSERT_EQ(3u, client.returned_resources().size());
1688 ResourceId returned_ids[3];
1689 for (size_t i = 0; i < 3; ++i) {
1690 returned_ids[i] = client.returned_resources()[i].id;
1692 EXPECT_THAT(returned_ids,
1693 testing::WhenSorted(testing::ElementsAreArray(ids)));
1694 EXPECT_EQ(3u, resource_provider_->num_resources());
1695 factory.Destroy(surface_id);
1696 factory.Destroy(surface_id2);
1699 // Ensure that aggregator completely ignores Surfaces that reference invalid
1700 // resources.
1701 TEST_F(SurfaceAggregatorWithResourcesTest, InvalidChildSurface) {
1702 ResourceTrackingSurfaceFactoryClient client;
1703 SurfaceFactory factory(&manager_, &client);
1704 SurfaceId root_surface_id(7u);
1705 factory.Create(root_surface_id);
1706 SurfaceId middle_surface_id(8u);
1707 factory.Create(middle_surface_id);
1708 SurfaceId child_surface_id(9u);
1709 factory.Create(child_surface_id);
1711 ResourceId ids[] = {14, 15, 16};
1712 SubmitCompositorFrameWithResources(ids, arraysize(ids), true, SurfaceId(),
1713 &factory, child_surface_id);
1715 ResourceId ids2[] = {17, 18, 19};
1716 SubmitCompositorFrameWithResources(ids2, arraysize(ids2), false,
1717 child_surface_id, &factory,
1718 middle_surface_id);
1720 ResourceId ids3[] = {20, 21, 22};
1721 SubmitCompositorFrameWithResources(ids3, arraysize(ids3), true,
1722 middle_surface_id, &factory,
1723 root_surface_id);
1725 scoped_ptr<CompositorFrame> frame;
1726 frame = aggregator_->Aggregate(root_surface_id);
1728 RenderPassList* pass_list = &frame->delegated_frame_data->render_pass_list;
1729 ASSERT_EQ(1u, pass_list->size());
1730 EXPECT_EQ(1u, pass_list->back()->shared_quad_state_list.size());
1731 EXPECT_EQ(3u, pass_list->back()->quad_list.size());
1733 SubmitCompositorFrameWithResources(ids2, arraysize(ids), true,
1734 child_surface_id, &factory,
1735 middle_surface_id);
1737 frame = aggregator_->Aggregate(root_surface_id);
1739 pass_list = &frame->delegated_frame_data->render_pass_list;
1740 ASSERT_EQ(1u, pass_list->size());
1741 EXPECT_EQ(3u, pass_list->back()->shared_quad_state_list.size());
1742 EXPECT_EQ(9u, pass_list->back()->quad_list.size());
1744 factory.Destroy(root_surface_id);
1745 factory.Destroy(child_surface_id);
1746 factory.Destroy(middle_surface_id);
1749 } // namespace
1750 } // namespace cc