Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / cc / surfaces / surface_aggregator_unittest.cc
blob1fb5163dd3779a45fb1f2980f673c69cfe4f66fc
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_common.h"
24 #include "cc/test/render_pass_test_utils.h"
25 #include "cc/test/test_shared_bitmap_manager.h"
26 #include "testing/gmock/include/gmock/gmock.h"
27 #include "testing/gtest/include/gtest/gtest.h"
28 #include "third_party/skia/include/core/SkColor.h"
30 namespace cc {
31 namespace {
33 SurfaceId InvalidSurfaceId() {
34 static SurfaceId invalid;
35 invalid.id = static_cast<uint64_t>(-1);
36 return invalid;
39 gfx::Size SurfaceSize() {
40 static gfx::Size size(5, 5);
41 return size;
44 class EmptySurfaceFactoryClient : public SurfaceFactoryClient {
45 public:
46 void ReturnResources(const ReturnedResourceArray& resources) override {}
49 class SurfaceAggregatorTest : public testing::Test {
50 public:
51 SurfaceAggregatorTest()
52 : factory_(&manager_, &empty_client_), aggregator_(&manager_, NULL) {}
54 protected:
55 SurfaceManager manager_;
56 EmptySurfaceFactoryClient empty_client_;
57 SurfaceFactory factory_;
58 SurfaceAggregator aggregator_;
61 TEST_F(SurfaceAggregatorTest, ValidSurfaceNoFrame) {
62 SurfaceId one_id(7);
63 factory_.Create(one_id);
64 scoped_ptr<CompositorFrame> frame = aggregator_.Aggregate(one_id);
65 EXPECT_FALSE(frame);
66 factory_.Destroy(one_id);
69 class SurfaceAggregatorValidSurfaceTest : public SurfaceAggregatorTest {
70 public:
71 SurfaceAggregatorValidSurfaceTest() : allocator_(1u), child_allocator_(2u) {}
73 void SetUp() override {
74 SurfaceAggregatorTest::SetUp();
75 root_surface_id_ = allocator_.GenerateId();
76 factory_.Create(root_surface_id_);
79 void TearDown() override {
80 factory_.Destroy(root_surface_id_);
81 SurfaceAggregatorTest::TearDown();
84 void AggregateAndVerify(test::Pass* expected_passes,
85 size_t expected_pass_count,
86 SurfaceId* surface_ids,
87 size_t expected_surface_count) {
88 scoped_ptr<CompositorFrame> aggregated_frame =
89 aggregator_.Aggregate(root_surface_id_);
91 ASSERT_TRUE(aggregated_frame);
92 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
94 DelegatedFrameData* frame_data =
95 aggregated_frame->delegated_frame_data.get();
97 TestPassesMatchExpectations(
98 expected_passes, expected_pass_count, &frame_data->render_pass_list);
100 // Ensure no duplicate pass ids output.
101 std::set<RenderPassId> used_passes;
102 for (auto* pass : frame_data->render_pass_list) {
103 EXPECT_TRUE(used_passes.insert(pass->id).second);
106 EXPECT_EQ(expected_surface_count,
107 aggregator_.previous_contained_surfaces().size());
108 for (size_t i = 0; i < expected_surface_count; i++) {
109 EXPECT_TRUE(
110 aggregator_.previous_contained_surfaces().find(surface_ids[i]) !=
111 aggregator_.previous_contained_surfaces().end());
115 void SubmitFrame(test::Pass* passes,
116 size_t pass_count,
117 SurfaceId surface_id) {
118 RenderPassList pass_list;
119 AddPasses(&pass_list, gfx::Rect(SurfaceSize()), passes, pass_count);
121 scoped_ptr<DelegatedFrameData> frame_data(new DelegatedFrameData);
122 pass_list.swap(frame_data->render_pass_list);
124 scoped_ptr<CompositorFrame> frame(new CompositorFrame);
125 frame->delegated_frame_data = frame_data.Pass();
127 factory_.SubmitFrame(surface_id, frame.Pass(),
128 SurfaceFactory::DrawCallback());
131 void QueuePassAsFrame(scoped_ptr<RenderPass> pass, SurfaceId surface_id) {
132 scoped_ptr<DelegatedFrameData> delegated_frame_data(new DelegatedFrameData);
133 delegated_frame_data->render_pass_list.push_back(pass.Pass());
135 scoped_ptr<CompositorFrame> child_frame(new CompositorFrame);
136 child_frame->delegated_frame_data = delegated_frame_data.Pass();
138 factory_.SubmitFrame(surface_id, child_frame.Pass(),
139 SurfaceFactory::DrawCallback());
142 protected:
143 SurfaceId root_surface_id_;
144 SurfaceIdAllocator allocator_;
145 SurfaceIdAllocator child_allocator_;
148 // Tests that a very simple frame containing only two solid color quads makes it
149 // through the aggregator correctly.
150 TEST_F(SurfaceAggregatorValidSurfaceTest, SimpleFrame) {
151 test::Quad quads[] = {test::Quad::SolidColorQuad(SK_ColorRED),
152 test::Quad::SolidColorQuad(SK_ColorBLUE)};
153 test::Pass passes[] = {test::Pass(quads, arraysize(quads))};
155 SubmitFrame(passes, arraysize(passes), root_surface_id_);
157 SurfaceId ids[] = {root_surface_id_};
158 AggregateAndVerify(passes, arraysize(passes), ids, arraysize(ids));
161 TEST_F(SurfaceAggregatorValidSurfaceTest, OpacityCopied) {
162 SurfaceId embedded_surface_id = allocator_.GenerateId();
163 factory_.Create(embedded_surface_id);
165 test::Quad embedded_quads[] = {test::Quad::SolidColorQuad(SK_ColorGREEN),
166 test::Quad::SolidColorQuad(SK_ColorBLUE)};
167 test::Pass embedded_passes[] = {
168 test::Pass(embedded_quads, arraysize(embedded_quads))};
170 SubmitFrame(embedded_passes, arraysize(embedded_passes), embedded_surface_id);
172 test::Quad quads[] = {test::Quad::SurfaceQuad(embedded_surface_id, .5f)};
173 test::Pass passes[] = {test::Pass(quads, arraysize(quads))};
175 SubmitFrame(passes, arraysize(passes), root_surface_id_);
177 scoped_ptr<CompositorFrame> aggregated_frame =
178 aggregator_.Aggregate(root_surface_id_);
180 ASSERT_TRUE(aggregated_frame);
181 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
183 DelegatedFrameData* frame_data = aggregated_frame->delegated_frame_data.get();
185 RenderPassList& render_pass_list(frame_data->render_pass_list);
186 ASSERT_EQ(2u, render_pass_list.size());
187 SharedQuadStateList& shared_quad_state_list(
188 render_pass_list[0]->shared_quad_state_list);
189 ASSERT_EQ(2u, shared_quad_state_list.size());
190 EXPECT_EQ(1.f, shared_quad_state_list.ElementAt(0)->opacity);
191 EXPECT_EQ(1.f, shared_quad_state_list.ElementAt(1)->opacity);
193 SharedQuadStateList& shared_quad_state_list2(
194 render_pass_list[1]->shared_quad_state_list);
195 ASSERT_EQ(1u, shared_quad_state_list2.size());
196 EXPECT_EQ(.5f, shared_quad_state_list2.ElementAt(0)->opacity);
198 factory_.Destroy(embedded_surface_id);
201 TEST_F(SurfaceAggregatorValidSurfaceTest, MultiPassSimpleFrame) {
202 test::Quad quads[][2] = {{test::Quad::SolidColorQuad(SK_ColorWHITE),
203 test::Quad::SolidColorQuad(SK_ColorLTGRAY)},
204 {test::Quad::SolidColorQuad(SK_ColorGRAY),
205 test::Quad::SolidColorQuad(SK_ColorDKGRAY)}};
206 test::Pass passes[] = {
207 test::Pass(quads[0], arraysize(quads[0]), RenderPassId(1, 1)),
208 test::Pass(quads[1], arraysize(quads[1]), RenderPassId(1, 2))};
210 SubmitFrame(passes, arraysize(passes), root_surface_id_);
212 SurfaceId ids[] = {root_surface_id_};
213 AggregateAndVerify(passes, arraysize(passes), ids, arraysize(ids));
216 // This tests very simple embedding. root_surface has a frame containing a few
217 // solid color quads and a surface quad referencing embedded_surface.
218 // embedded_surface has a frame containing only a solid color quad. The solid
219 // color quad should be aggregated into the final frame.
220 TEST_F(SurfaceAggregatorValidSurfaceTest, SimpleSurfaceReference) {
221 SurfaceId embedded_surface_id = allocator_.GenerateId();
222 factory_.Create(embedded_surface_id);
224 test::Quad embedded_quads[] = {test::Quad::SolidColorQuad(SK_ColorGREEN)};
225 test::Pass embedded_passes[] = {
226 test::Pass(embedded_quads, arraysize(embedded_quads))};
228 SubmitFrame(embedded_passes, arraysize(embedded_passes), embedded_surface_id);
230 test::Quad root_quads[] = {test::Quad::SolidColorQuad(SK_ColorWHITE),
231 test::Quad::SurfaceQuad(embedded_surface_id, 1.f),
232 test::Quad::SolidColorQuad(SK_ColorBLACK)};
233 test::Pass root_passes[] = {test::Pass(root_quads, arraysize(root_quads))};
235 SubmitFrame(root_passes, arraysize(root_passes), root_surface_id_);
237 test::Quad expected_quads[] = {test::Quad::SolidColorQuad(SK_ColorWHITE),
238 test::Quad::SolidColorQuad(SK_ColorGREEN),
239 test::Quad::SolidColorQuad(SK_ColorBLACK)};
240 test::Pass expected_passes[] = {
241 test::Pass(expected_quads, arraysize(expected_quads))};
242 SurfaceId ids[] = {root_surface_id_, embedded_surface_id};
243 AggregateAndVerify(
244 expected_passes, arraysize(expected_passes), ids, arraysize(ids));
246 factory_.Destroy(embedded_surface_id);
249 TEST_F(SurfaceAggregatorValidSurfaceTest, CopyRequest) {
250 SurfaceId embedded_surface_id = allocator_.GenerateId();
251 factory_.Create(embedded_surface_id);
253 test::Quad embedded_quads[] = {test::Quad::SolidColorQuad(SK_ColorGREEN)};
254 test::Pass embedded_passes[] = {
255 test::Pass(embedded_quads, arraysize(embedded_quads))};
257 SubmitFrame(embedded_passes, arraysize(embedded_passes), embedded_surface_id);
258 scoped_ptr<CopyOutputRequest> copy_request(
259 CopyOutputRequest::CreateEmptyRequest());
260 CopyOutputRequest* copy_request_ptr = copy_request.get();
261 factory_.RequestCopyOfSurface(embedded_surface_id, copy_request.Pass());
263 test::Quad root_quads[] = {test::Quad::SolidColorQuad(SK_ColorWHITE),
264 test::Quad::SurfaceQuad(embedded_surface_id, 1.f),
265 test::Quad::SolidColorQuad(SK_ColorBLACK)};
266 test::Pass root_passes[] = {test::Pass(root_quads, arraysize(root_quads))};
268 SubmitFrame(root_passes, arraysize(root_passes), root_surface_id_);
270 scoped_ptr<CompositorFrame> aggregated_frame =
271 aggregator_.Aggregate(root_surface_id_);
273 ASSERT_TRUE(aggregated_frame);
274 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
276 DelegatedFrameData* frame_data = aggregated_frame->delegated_frame_data.get();
278 test::Quad expected_quads[] = {
279 test::Quad::SolidColorQuad(SK_ColorWHITE),
280 test::Quad::RenderPassQuad(frame_data->render_pass_list[0]->id),
281 test::Quad::SolidColorQuad(SK_ColorBLACK)};
282 test::Pass expected_passes[] = {
283 test::Pass(embedded_quads, arraysize(embedded_quads)),
284 test::Pass(expected_quads, arraysize(expected_quads))};
285 TestPassesMatchExpectations(expected_passes,
286 arraysize(expected_passes),
287 &frame_data->render_pass_list);
288 ASSERT_EQ(2u, frame_data->render_pass_list.size());
289 ASSERT_EQ(1u, frame_data->render_pass_list[0]->copy_requests.size());
290 DCHECK_EQ(copy_request_ptr,
291 frame_data->render_pass_list[0]->copy_requests[0]);
293 SurfaceId surface_ids[] = {root_surface_id_, embedded_surface_id};
294 EXPECT_EQ(arraysize(surface_ids),
295 aggregator_.previous_contained_surfaces().size());
296 for (size_t i = 0; i < arraysize(surface_ids); i++) {
297 EXPECT_TRUE(
298 aggregator_.previous_contained_surfaces().find(surface_ids[i]) !=
299 aggregator_.previous_contained_surfaces().end());
302 factory_.Destroy(embedded_surface_id);
305 // Root surface may contain copy requests.
306 TEST_F(SurfaceAggregatorValidSurfaceTest, RootCopyRequest) {
307 SurfaceId embedded_surface_id = allocator_.GenerateId();
308 factory_.Create(embedded_surface_id);
310 test::Quad embedded_quads[] = {test::Quad::SolidColorQuad(SK_ColorGREEN)};
311 test::Pass embedded_passes[] = {
312 test::Pass(embedded_quads, arraysize(embedded_quads))};
314 SubmitFrame(embedded_passes, arraysize(embedded_passes), embedded_surface_id);
315 scoped_ptr<CopyOutputRequest> copy_request(
316 CopyOutputRequest::CreateEmptyRequest());
317 CopyOutputRequest* copy_request_ptr = copy_request.get();
318 scoped_ptr<CopyOutputRequest> copy_request2(
319 CopyOutputRequest::CreateEmptyRequest());
320 CopyOutputRequest* copy_request2_ptr = copy_request2.get();
322 test::Quad root_quads[] = {test::Quad::SolidColorQuad(SK_ColorWHITE),
323 test::Quad::SurfaceQuad(embedded_surface_id, 1.f),
324 test::Quad::SolidColorQuad(SK_ColorBLACK)};
325 test::Quad root_quads2[] = {test::Quad::SolidColorQuad(SK_ColorRED)};
326 test::Pass root_passes[] = {
327 test::Pass(root_quads, arraysize(root_quads), RenderPassId(1, 1)),
328 test::Pass(root_quads2, arraysize(root_quads2), RenderPassId(1, 2))};
330 RenderPassList pass_list;
331 AddPasses(&pass_list,
332 gfx::Rect(SurfaceSize()),
333 root_passes,
334 arraysize(root_passes));
335 pass_list[0]->copy_requests.push_back(copy_request.Pass());
336 pass_list[1]->copy_requests.push_back(copy_request2.Pass());
338 scoped_ptr<DelegatedFrameData> frame_data(new DelegatedFrameData);
339 pass_list.swap(frame_data->render_pass_list);
341 scoped_ptr<CompositorFrame> frame(new CompositorFrame);
342 frame->delegated_frame_data = frame_data.Pass();
344 factory_.SubmitFrame(root_surface_id_, frame.Pass(),
345 SurfaceFactory::DrawCallback());
348 scoped_ptr<CompositorFrame> aggregated_frame =
349 aggregator_.Aggregate(root_surface_id_);
351 ASSERT_TRUE(aggregated_frame);
352 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
354 DelegatedFrameData* frame_data = aggregated_frame->delegated_frame_data.get();
356 test::Quad expected_quads[] = {test::Quad::SolidColorQuad(SK_ColorWHITE),
357 test::Quad::SolidColorQuad(SK_ColorGREEN),
358 test::Quad::SolidColorQuad(SK_ColorBLACK)};
359 test::Pass expected_passes[] = {
360 test::Pass(expected_quads, arraysize(expected_quads)),
361 test::Pass(root_quads2, arraysize(root_quads2))};
362 TestPassesMatchExpectations(expected_passes,
363 arraysize(expected_passes),
364 &frame_data->render_pass_list);
365 ASSERT_EQ(2u, frame_data->render_pass_list.size());
366 ASSERT_EQ(1u, frame_data->render_pass_list[0]->copy_requests.size());
367 DCHECK_EQ(copy_request_ptr,
368 frame_data->render_pass_list[0]->copy_requests[0]);
369 ASSERT_EQ(1u, frame_data->render_pass_list[1]->copy_requests.size());
370 DCHECK_EQ(copy_request2_ptr,
371 frame_data->render_pass_list[1]->copy_requests[0]);
373 SurfaceId surface_ids[] = {root_surface_id_, embedded_surface_id};
374 EXPECT_EQ(arraysize(surface_ids),
375 aggregator_.previous_contained_surfaces().size());
376 for (size_t i = 0; i < arraysize(surface_ids); i++) {
377 EXPECT_TRUE(
378 aggregator_.previous_contained_surfaces().find(surface_ids[i]) !=
379 aggregator_.previous_contained_surfaces().end());
382 // Ensure copy requests have been removed from root surface.
383 const CompositorFrame* original_frame =
384 manager_.GetSurfaceForId(root_surface_id_)->GetEligibleFrame();
385 RenderPassList& original_pass_list =
386 original_frame->delegated_frame_data->render_pass_list;
387 ASSERT_EQ(2u, original_pass_list.size());
388 DCHECK(original_pass_list[0]->copy_requests.empty());
389 DCHECK(original_pass_list[1]->copy_requests.empty());
391 factory_.Destroy(embedded_surface_id);
394 // This tests referencing a surface that has multiple render passes.
395 TEST_F(SurfaceAggregatorValidSurfaceTest, MultiPassSurfaceReference) {
396 SurfaceId embedded_surface_id = child_allocator_.GenerateId();
397 factory_.Create(embedded_surface_id);
399 RenderPassId pass_ids[] = {RenderPassId(1, 1), RenderPassId(1, 2),
400 RenderPassId(1, 3)};
402 test::Quad embedded_quads[][2] = {
403 {test::Quad::SolidColorQuad(1), test::Quad::SolidColorQuad(2)},
404 {test::Quad::SolidColorQuad(3), test::Quad::RenderPassQuad(pass_ids[0])},
405 {test::Quad::SolidColorQuad(4), test::Quad::RenderPassQuad(pass_ids[1])}};
406 test::Pass embedded_passes[] = {
407 test::Pass(embedded_quads[0], arraysize(embedded_quads[0]), pass_ids[0]),
408 test::Pass(embedded_quads[1], arraysize(embedded_quads[1]), pass_ids[1]),
409 test::Pass(embedded_quads[2], arraysize(embedded_quads[2]), pass_ids[2])};
411 SubmitFrame(embedded_passes, arraysize(embedded_passes), embedded_surface_id);
413 test::Quad root_quads[][2] = {
414 {test::Quad::SolidColorQuad(5), test::Quad::SolidColorQuad(6)},
415 {test::Quad::SurfaceQuad(embedded_surface_id, 1.f),
416 test::Quad::RenderPassQuad(pass_ids[0])},
417 {test::Quad::SolidColorQuad(7), test::Quad::RenderPassQuad(pass_ids[1])}};
418 test::Pass root_passes[] = {
419 test::Pass(root_quads[0], arraysize(root_quads[0]), pass_ids[0]),
420 test::Pass(root_quads[1], arraysize(root_quads[1]), pass_ids[1]),
421 test::Pass(root_quads[2], arraysize(root_quads[2]), pass_ids[2])};
423 SubmitFrame(root_passes, arraysize(root_passes), root_surface_id_);
425 scoped_ptr<CompositorFrame> aggregated_frame =
426 aggregator_.Aggregate(root_surface_id_);
428 ASSERT_TRUE(aggregated_frame);
429 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
431 DelegatedFrameData* frame_data = aggregated_frame->delegated_frame_data.get();
433 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
435 ASSERT_EQ(5u, aggregated_pass_list.size());
436 RenderPassId actual_pass_ids[] = {
437 aggregated_pass_list[0]->id, aggregated_pass_list[1]->id,
438 aggregated_pass_list[2]->id, aggregated_pass_list[3]->id,
439 aggregated_pass_list[4]->id};
440 for (size_t i = 0; i < 5; ++i) {
441 for (size_t j = 0; j < i; ++j) {
442 EXPECT_NE(actual_pass_ids[i], actual_pass_ids[j]);
447 SCOPED_TRACE("First pass");
448 // The first pass will just be the first pass from the root surfaces quad
449 // with no render pass quads to remap.
450 TestPassMatchesExpectations(root_passes[0], aggregated_pass_list[0]);
454 SCOPED_TRACE("Second pass");
455 // The next two passes will be from the embedded surface since we have to
456 // draw those passes before they are referenced from the render pass draw
457 // quad embedded into the root surface's second pass.
458 // First, there's the first embedded pass which doesn't reference anything
459 // else.
460 TestPassMatchesExpectations(embedded_passes[0], aggregated_pass_list[1]);
464 SCOPED_TRACE("Third pass");
465 const QuadList& third_pass_quad_list = aggregated_pass_list[2]->quad_list;
466 ASSERT_EQ(2u, third_pass_quad_list.size());
467 TestQuadMatchesExpectations(embedded_quads[1][0],
468 third_pass_quad_list.ElementAt(0));
470 // This render pass pass quad will reference the first pass from the
471 // embedded surface, which is the second pass in the aggregated frame.
472 ASSERT_EQ(DrawQuad::RENDER_PASS,
473 third_pass_quad_list.ElementAt(1)->material);
474 const RenderPassDrawQuad* third_pass_render_pass_draw_quad =
475 RenderPassDrawQuad::MaterialCast(third_pass_quad_list.ElementAt(1));
476 EXPECT_EQ(actual_pass_ids[1],
477 third_pass_render_pass_draw_quad->render_pass_id);
481 SCOPED_TRACE("Fourth pass");
482 // The fourth pass will have aggregated quads from the root surface's second
483 // pass and the embedded surface's first pass.
484 const QuadList& fourth_pass_quad_list = aggregated_pass_list[3]->quad_list;
485 ASSERT_EQ(3u, fourth_pass_quad_list.size());
487 // The first quad will be the yellow quad from the embedded surface's last
488 // pass.
489 TestQuadMatchesExpectations(embedded_quads[2][0],
490 fourth_pass_quad_list.ElementAt(0));
492 // The next quad will be a render pass quad referencing the second pass from
493 // the embedded surface, which is the third pass in the aggregated frame.
494 ASSERT_EQ(DrawQuad::RENDER_PASS,
495 fourth_pass_quad_list.ElementAt(1)->material);
496 const RenderPassDrawQuad* fourth_pass_first_render_pass_draw_quad =
497 RenderPassDrawQuad::MaterialCast(fourth_pass_quad_list.ElementAt(1));
498 EXPECT_EQ(actual_pass_ids[2],
499 fourth_pass_first_render_pass_draw_quad->render_pass_id);
501 // The last quad will be a render pass quad referencing the first pass from
502 // the root surface, which is the first pass overall.
503 ASSERT_EQ(DrawQuad::RENDER_PASS,
504 fourth_pass_quad_list.ElementAt(2)->material);
505 const RenderPassDrawQuad* fourth_pass_second_render_pass_draw_quad =
506 RenderPassDrawQuad::MaterialCast(fourth_pass_quad_list.ElementAt(2));
507 EXPECT_EQ(actual_pass_ids[0],
508 fourth_pass_second_render_pass_draw_quad->render_pass_id);
512 SCOPED_TRACE("Fifth pass");
513 const QuadList& fifth_pass_quad_list = aggregated_pass_list[4]->quad_list;
514 ASSERT_EQ(2u, fifth_pass_quad_list.size());
516 TestQuadMatchesExpectations(root_quads[2][0],
517 fifth_pass_quad_list.ElementAt(0));
519 // The last quad in the last pass will reference the second pass from the
520 // root surface, which after aggregating is the fourth pass in the overall
521 // list.
522 ASSERT_EQ(DrawQuad::RENDER_PASS,
523 fifth_pass_quad_list.ElementAt(1)->material);
524 const RenderPassDrawQuad* fifth_pass_render_pass_draw_quad =
525 RenderPassDrawQuad::MaterialCast(fifth_pass_quad_list.ElementAt(1));
526 EXPECT_EQ(actual_pass_ids[3],
527 fifth_pass_render_pass_draw_quad->render_pass_id);
529 factory_.Destroy(embedded_surface_id);
532 // Tests an invalid surface reference in a frame. The surface quad should just
533 // be dropped.
534 TEST_F(SurfaceAggregatorValidSurfaceTest, InvalidSurfaceReference) {
535 test::Quad quads[] = {test::Quad::SolidColorQuad(SK_ColorGREEN),
536 test::Quad::SurfaceQuad(InvalidSurfaceId(), 1.f),
537 test::Quad::SolidColorQuad(SK_ColorBLUE)};
538 test::Pass passes[] = {test::Pass(quads, arraysize(quads))};
540 SubmitFrame(passes, arraysize(passes), root_surface_id_);
542 test::Quad expected_quads[] = {test::Quad::SolidColorQuad(SK_ColorGREEN),
543 test::Quad::SolidColorQuad(SK_ColorBLUE)};
544 test::Pass expected_passes[] = {
545 test::Pass(expected_quads, arraysize(expected_quads))};
546 SurfaceId ids[] = {root_surface_id_, InvalidSurfaceId()};
547 AggregateAndVerify(
548 expected_passes, arraysize(expected_passes), ids, arraysize(ids));
551 // Tests a reference to a valid surface with no submitted frame. This quad
552 // should also just be dropped.
553 TEST_F(SurfaceAggregatorValidSurfaceTest, ValidSurfaceReferenceWithNoFrame) {
554 SurfaceId surface_with_no_frame_id = allocator_.GenerateId();
555 factory_.Create(surface_with_no_frame_id);
556 test::Quad quads[] = {test::Quad::SolidColorQuad(SK_ColorGREEN),
557 test::Quad::SurfaceQuad(surface_with_no_frame_id, 1.f),
558 test::Quad::SolidColorQuad(SK_ColorBLUE)};
559 test::Pass passes[] = {test::Pass(quads, arraysize(quads))};
561 SubmitFrame(passes, arraysize(passes), root_surface_id_);
563 test::Quad expected_quads[] = {test::Quad::SolidColorQuad(SK_ColorGREEN),
564 test::Quad::SolidColorQuad(SK_ColorBLUE)};
565 test::Pass expected_passes[] = {
566 test::Pass(expected_quads, arraysize(expected_quads))};
567 SurfaceId ids[] = {root_surface_id_, surface_with_no_frame_id};
568 AggregateAndVerify(
569 expected_passes, arraysize(expected_passes), ids, arraysize(ids));
570 factory_.Destroy(surface_with_no_frame_id);
573 // Tests a surface quad referencing itself, generating a trivial cycle.
574 // The quad creating the cycle should be dropped from the final frame.
575 TEST_F(SurfaceAggregatorValidSurfaceTest, SimpleCyclicalReference) {
576 test::Quad quads[] = {test::Quad::SurfaceQuad(root_surface_id_, 1.f),
577 test::Quad::SolidColorQuad(SK_ColorYELLOW)};
578 test::Pass passes[] = {test::Pass(quads, arraysize(quads))};
580 SubmitFrame(passes, arraysize(passes), root_surface_id_);
582 test::Quad expected_quads[] = {test::Quad::SolidColorQuad(SK_ColorYELLOW)};
583 test::Pass expected_passes[] = {
584 test::Pass(expected_quads, arraysize(expected_quads))};
585 SurfaceId ids[] = {root_surface_id_};
586 AggregateAndVerify(
587 expected_passes, arraysize(expected_passes), ids, arraysize(ids));
590 // Tests a more complex cycle with one intermediate surface.
591 TEST_F(SurfaceAggregatorValidSurfaceTest, TwoSurfaceCyclicalReference) {
592 SurfaceId child_surface_id = allocator_.GenerateId();
593 factory_.Create(child_surface_id);
595 test::Quad parent_quads[] = {test::Quad::SolidColorQuad(SK_ColorBLUE),
596 test::Quad::SurfaceQuad(child_surface_id, 1.f),
597 test::Quad::SolidColorQuad(SK_ColorCYAN)};
598 test::Pass parent_passes[] = {
599 test::Pass(parent_quads, arraysize(parent_quads))};
601 SubmitFrame(parent_passes, arraysize(parent_passes), root_surface_id_);
603 test::Quad child_quads[] = {test::Quad::SolidColorQuad(SK_ColorGREEN),
604 test::Quad::SurfaceQuad(root_surface_id_, 1.f),
605 test::Quad::SolidColorQuad(SK_ColorMAGENTA)};
606 test::Pass child_passes[] = {test::Pass(child_quads, arraysize(child_quads))};
608 SubmitFrame(child_passes, arraysize(child_passes), child_surface_id);
610 // The child surface's reference to the root_surface_ will be dropped, so
611 // we'll end up with:
612 // SK_ColorBLUE from the parent
613 // SK_ColorGREEN from the child
614 // SK_ColorMAGENTA from the child
615 // SK_ColorCYAN from the parent
616 test::Quad expected_quads[] = {test::Quad::SolidColorQuad(SK_ColorBLUE),
617 test::Quad::SolidColorQuad(SK_ColorGREEN),
618 test::Quad::SolidColorQuad(SK_ColorMAGENTA),
619 test::Quad::SolidColorQuad(SK_ColorCYAN)};
620 test::Pass expected_passes[] = {
621 test::Pass(expected_quads, arraysize(expected_quads))};
622 SurfaceId ids[] = {root_surface_id_, child_surface_id};
623 AggregateAndVerify(
624 expected_passes, arraysize(expected_passes), ids, arraysize(ids));
625 factory_.Destroy(child_surface_id);
628 // Tests that we map render pass IDs from different surfaces into a unified
629 // namespace and update RenderPassDrawQuad's id references to match.
630 TEST_F(SurfaceAggregatorValidSurfaceTest, RenderPassIdMapping) {
631 SurfaceId child_surface_id = allocator_.GenerateId();
632 factory_.Create(child_surface_id);
634 RenderPassId child_pass_id[] = {RenderPassId(1, 1), RenderPassId(1, 2)};
635 test::Quad child_quad[][1] = {{test::Quad::SolidColorQuad(SK_ColorGREEN)},
636 {test::Quad::RenderPassQuad(child_pass_id[0])}};
637 test::Pass surface_passes[] = {
638 test::Pass(child_quad[0], arraysize(child_quad[0]), child_pass_id[0]),
639 test::Pass(child_quad[1], arraysize(child_quad[1]), child_pass_id[1])};
641 SubmitFrame(surface_passes, arraysize(surface_passes), child_surface_id);
643 // Pass IDs from the parent surface may collide with ones from the child.
644 RenderPassId parent_pass_id[] = {RenderPassId(2, 1), RenderPassId(1, 2)};
645 test::Quad parent_quad[][1] = {
646 {test::Quad::SurfaceQuad(child_surface_id, 1.f)},
647 {test::Quad::RenderPassQuad(parent_pass_id[0])}};
648 test::Pass parent_passes[] = {
649 test::Pass(parent_quad[0], arraysize(parent_quad[0]), parent_pass_id[0]),
650 test::Pass(parent_quad[1], arraysize(parent_quad[1]), parent_pass_id[1])};
652 SubmitFrame(parent_passes, arraysize(parent_passes), root_surface_id_);
653 scoped_ptr<CompositorFrame> aggregated_frame =
654 aggregator_.Aggregate(root_surface_id_);
656 ASSERT_TRUE(aggregated_frame);
657 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
659 DelegatedFrameData* frame_data = aggregated_frame->delegated_frame_data.get();
661 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
663 ASSERT_EQ(3u, aggregated_pass_list.size());
664 RenderPassId actual_pass_ids[] = {aggregated_pass_list[0]->id,
665 aggregated_pass_list[1]->id,
666 aggregated_pass_list[2]->id};
667 // Make sure the aggregated frame's pass IDs are all unique.
668 for (size_t i = 0; i < 3; ++i) {
669 for (size_t j = 0; j < i; ++j) {
670 EXPECT_NE(actual_pass_ids[j], actual_pass_ids[i]) << "pass ids " << i
671 << " and " << j;
675 // Make sure the render pass quads reference the remapped pass IDs.
676 DrawQuad* render_pass_quads[] = {aggregated_pass_list[1]->quad_list.front(),
677 aggregated_pass_list[2]->quad_list.front()};
678 ASSERT_EQ(render_pass_quads[0]->material, DrawQuad::RENDER_PASS);
679 EXPECT_EQ(
680 actual_pass_ids[0],
681 RenderPassDrawQuad::MaterialCast(render_pass_quads[0])->render_pass_id);
683 ASSERT_EQ(render_pass_quads[1]->material, DrawQuad::RENDER_PASS);
684 EXPECT_EQ(
685 actual_pass_ids[1],
686 RenderPassDrawQuad::MaterialCast(render_pass_quads[1])->render_pass_id);
687 factory_.Destroy(child_surface_id);
690 void AddSolidColorQuadWithBlendMode(const gfx::Size& size,
691 RenderPass* pass,
692 const SkXfermode::Mode blend_mode) {
693 const gfx::Transform content_to_target_transform;
694 const gfx::Size content_bounds(size);
695 const gfx::Rect visible_content_rect(size);
696 const gfx::Rect clip_rect(size);
698 bool is_clipped = false;
699 float opacity = 1.f;
701 bool force_anti_aliasing_off = false;
702 SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState();
703 sqs->SetAll(content_to_target_transform,
704 content_bounds,
705 visible_content_rect,
706 clip_rect,
707 is_clipped,
708 opacity,
709 blend_mode,
712 SolidColorDrawQuad* color_quad =
713 pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
714 color_quad->SetNew(pass->shared_quad_state_list.back(),
715 visible_content_rect,
716 visible_content_rect,
717 SK_ColorGREEN,
718 force_anti_aliasing_off);
721 // This tests that we update shared quad state pointers correctly within
722 // aggregated passes. The shared quad state list on the aggregated pass will
723 // include the shared quad states from each pass in one list so the quads will
724 // end up pointed to shared quad state objects at different offsets. This test
725 // uses the blend_mode value stored on the shared quad state to track the shared
726 // quad state, but anything saved on the shared quad state would work.
728 // This test has 4 surfaces in the following structure:
729 // root_surface -> quad with kClear_Mode,
730 // [child_one_surface],
731 // quad with kDstOver_Mode,
732 // [child_two_surface],
733 // quad with kDstIn_Mode
734 // child_one_surface -> quad with kSrc_Mode,
735 // [grandchild_surface],
736 // quad with kSrcOver_Mode
737 // child_two_surface -> quad with kSrcIn_Mode
738 // grandchild_surface -> quad with kDst_Mode
740 // Resulting in the following aggregated pass:
741 // quad_root_0 - blend_mode kClear_Mode
742 // quad_child_one_0 - blend_mode kSrc_Mode
743 // quad_grandchild_0 - blend_mode kDst_Mode
744 // quad_child_one_1 - blend_mode kSrcOver_Mode
745 // quad_root_1 - blend_mode kDstOver_Mode
746 // quad_child_two_0 - blend_mode kSrcIn_Mode
747 // quad_root_2 - blend_mode kDstIn_Mode
748 TEST_F(SurfaceAggregatorValidSurfaceTest, AggregateSharedQuadStateProperties) {
749 const SkXfermode::Mode blend_modes[] = {SkXfermode::kClear_Mode, // 0
750 SkXfermode::kSrc_Mode, // 1
751 SkXfermode::kDst_Mode, // 2
752 SkXfermode::kSrcOver_Mode, // 3
753 SkXfermode::kDstOver_Mode, // 4
754 SkXfermode::kSrcIn_Mode, // 5
755 SkXfermode::kDstIn_Mode, // 6
758 RenderPassId pass_id(1, 1);
759 SurfaceId grandchild_surface_id = allocator_.GenerateId();
760 factory_.Create(grandchild_surface_id);
761 scoped_ptr<RenderPass> grandchild_pass = RenderPass::Create();
762 gfx::Rect output_rect(SurfaceSize());
763 gfx::Rect damage_rect(SurfaceSize());
764 gfx::Transform transform_to_root_target;
765 grandchild_pass->SetNew(
766 pass_id, output_rect, damage_rect, transform_to_root_target);
767 AddSolidColorQuadWithBlendMode(
768 SurfaceSize(), grandchild_pass.get(), blend_modes[2]);
769 QueuePassAsFrame(grandchild_pass.Pass(), grandchild_surface_id);
771 SurfaceId child_one_surface_id = allocator_.GenerateId();
772 factory_.Create(child_one_surface_id);
774 scoped_ptr<RenderPass> child_one_pass = RenderPass::Create();
775 child_one_pass->SetNew(
776 pass_id, output_rect, damage_rect, transform_to_root_target);
777 AddSolidColorQuadWithBlendMode(
778 SurfaceSize(), child_one_pass.get(), blend_modes[1]);
779 SurfaceDrawQuad* grandchild_surface_quad =
780 child_one_pass->CreateAndAppendDrawQuad<SurfaceDrawQuad>();
781 grandchild_surface_quad->SetNew(child_one_pass->shared_quad_state_list.back(),
782 gfx::Rect(SurfaceSize()),
783 gfx::Rect(SurfaceSize()),
784 grandchild_surface_id);
785 AddSolidColorQuadWithBlendMode(
786 SurfaceSize(), child_one_pass.get(), blend_modes[3]);
787 QueuePassAsFrame(child_one_pass.Pass(), child_one_surface_id);
789 SurfaceId child_two_surface_id = allocator_.GenerateId();
790 factory_.Create(child_two_surface_id);
792 scoped_ptr<RenderPass> child_two_pass = RenderPass::Create();
793 child_two_pass->SetNew(
794 pass_id, output_rect, damage_rect, transform_to_root_target);
795 AddSolidColorQuadWithBlendMode(
796 SurfaceSize(), child_two_pass.get(), blend_modes[5]);
797 QueuePassAsFrame(child_two_pass.Pass(), child_two_surface_id);
799 scoped_ptr<RenderPass> root_pass = RenderPass::Create();
800 root_pass->SetNew(
801 pass_id, output_rect, damage_rect, transform_to_root_target);
803 AddSolidColorQuadWithBlendMode(
804 SurfaceSize(), root_pass.get(), blend_modes[0]);
805 SurfaceDrawQuad* child_one_surface_quad =
806 root_pass->CreateAndAppendDrawQuad<SurfaceDrawQuad>();
807 child_one_surface_quad->SetNew(root_pass->shared_quad_state_list.back(),
808 gfx::Rect(SurfaceSize()),
809 gfx::Rect(SurfaceSize()),
810 child_one_surface_id);
811 AddSolidColorQuadWithBlendMode(
812 SurfaceSize(), root_pass.get(), blend_modes[4]);
813 SurfaceDrawQuad* child_two_surface_quad =
814 root_pass->CreateAndAppendDrawQuad<SurfaceDrawQuad>();
815 child_two_surface_quad->SetNew(root_pass->shared_quad_state_list.back(),
816 gfx::Rect(SurfaceSize()),
817 gfx::Rect(SurfaceSize()),
818 child_two_surface_id);
819 AddSolidColorQuadWithBlendMode(
820 SurfaceSize(), root_pass.get(), blend_modes[6]);
822 QueuePassAsFrame(root_pass.Pass(), root_surface_id_);
824 scoped_ptr<CompositorFrame> aggregated_frame =
825 aggregator_.Aggregate(root_surface_id_);
827 ASSERT_TRUE(aggregated_frame);
828 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
830 DelegatedFrameData* frame_data = aggregated_frame->delegated_frame_data.get();
832 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
834 ASSERT_EQ(1u, aggregated_pass_list.size());
836 const QuadList& aggregated_quad_list = aggregated_pass_list[0]->quad_list;
838 ASSERT_EQ(7u, aggregated_quad_list.size());
840 for (auto iter = aggregated_quad_list.cbegin();
841 iter != aggregated_quad_list.cend();
842 ++iter) {
843 EXPECT_EQ(blend_modes[iter.index()], iter->shared_quad_state->blend_mode)
844 << iter.index();
846 factory_.Destroy(child_one_surface_id);
847 factory_.Destroy(child_two_surface_id);
848 factory_.Destroy(grandchild_surface_id);
851 // This tests that when aggregating a frame with multiple render passes that we
852 // map the transforms for the root pass but do not modify the transform on child
853 // passes.
855 // The root surface has one pass with a surface quad transformed by +10 in the y
856 // direction.
858 // The middle surface has one pass with a surface quad scaled by 2 in the x
859 // and 3 in the y directions.
861 // The child surface has two passes. The first pass has a quad with a transform
862 // of +5 in the x direction. The second pass has a reference to the first pass'
863 // pass id and a transform of +8 in the x direction.
865 // After aggregation, the child surface's root pass quad should have all
866 // transforms concatenated for a total transform of +23 x, +10 y. The
867 // contributing render pass' transform in the aggregate frame should not be
868 // affected.
869 TEST_F(SurfaceAggregatorValidSurfaceTest, AggregateMultiplePassWithTransform) {
870 // Innermost child surface.
871 SurfaceId child_surface_id = allocator_.GenerateId();
873 factory_.Create(child_surface_id);
874 RenderPassId child_pass_id[] = {RenderPassId(1, 1), RenderPassId(1, 2)};
875 test::Quad child_quads[][1] = {
876 {test::Quad::SolidColorQuad(SK_ColorGREEN)},
877 {test::Quad::RenderPassQuad(child_pass_id[0])},
879 test::Pass child_passes[] = {
880 test::Pass(child_quads[0], arraysize(child_quads[0]), child_pass_id[0]),
881 test::Pass(child_quads[1], arraysize(child_quads[1]),
882 child_pass_id[1])};
884 RenderPassList child_pass_list;
885 AddPasses(&child_pass_list, gfx::Rect(SurfaceSize()), child_passes,
886 arraysize(child_passes));
888 RenderPass* child_nonroot_pass = child_pass_list.at(0u);
889 child_nonroot_pass->transform_to_root_target.Translate(8, 0);
890 SharedQuadState* child_nonroot_pass_sqs =
891 child_nonroot_pass->shared_quad_state_list.front();
892 child_nonroot_pass_sqs->content_to_target_transform.Translate(5, 0);
894 RenderPass* child_root_pass = child_pass_list.at(1u);
895 SharedQuadState* child_root_pass_sqs =
896 child_root_pass->shared_quad_state_list.front();
897 child_root_pass_sqs->content_to_target_transform.Translate(8, 0);
898 child_root_pass_sqs->is_clipped = true;
899 child_root_pass_sqs->clip_rect = gfx::Rect(0, 0, 5, 5);
901 scoped_ptr<DelegatedFrameData> child_frame_data(new DelegatedFrameData);
902 child_pass_list.swap(child_frame_data->render_pass_list);
904 scoped_ptr<CompositorFrame> child_frame(new CompositorFrame);
905 child_frame->delegated_frame_data = child_frame_data.Pass();
907 factory_.SubmitFrame(child_surface_id, child_frame.Pass(),
908 SurfaceFactory::DrawCallback());
911 // Middle child surface.
912 SurfaceId middle_surface_id = allocator_.GenerateId();
914 factory_.Create(middle_surface_id);
915 test::Quad middle_quads[] = {
916 test::Quad::SurfaceQuad(child_surface_id, 1.f)};
917 test::Pass middle_passes[] = {
918 test::Pass(middle_quads, arraysize(middle_quads)),
921 RenderPassList middle_pass_list;
922 AddPasses(&middle_pass_list, gfx::Rect(SurfaceSize()), middle_passes,
923 arraysize(middle_passes));
925 RenderPass* middle_root_pass = middle_pass_list.at(0u);
926 middle_root_pass->quad_list.ElementAt(0)->visible_rect =
927 gfx::Rect(0, 1, 100, 7);
928 SharedQuadState* middle_root_pass_sqs =
929 middle_root_pass->shared_quad_state_list.front();
930 middle_root_pass_sqs->content_to_target_transform.Scale(2, 3);
932 scoped_ptr<DelegatedFrameData> middle_frame_data(new DelegatedFrameData);
933 middle_pass_list.swap(middle_frame_data->render_pass_list);
935 scoped_ptr<CompositorFrame> middle_frame(new CompositorFrame);
936 middle_frame->delegated_frame_data = middle_frame_data.Pass();
938 factory_.SubmitFrame(middle_surface_id, middle_frame.Pass(),
939 SurfaceFactory::DrawCallback());
942 // Root surface.
943 test::Quad secondary_quads[] = {
944 test::Quad::SolidColorQuad(1),
945 test::Quad::SurfaceQuad(middle_surface_id, 1.f)};
946 test::Quad root_quads[] = {test::Quad::SolidColorQuad(1)};
947 test::Pass root_passes[] = {
948 test::Pass(secondary_quads, arraysize(secondary_quads)),
949 test::Pass(root_quads, arraysize(root_quads))};
951 RenderPassList root_pass_list;
952 AddPasses(&root_pass_list,
953 gfx::Rect(SurfaceSize()),
954 root_passes,
955 arraysize(root_passes));
957 root_pass_list.at(0)
958 ->shared_quad_state_list.front()
959 ->content_to_target_transform.Translate(0, 7);
960 root_pass_list.at(0)
961 ->shared_quad_state_list.ElementAt(1)
962 ->content_to_target_transform.Translate(0, 10);
963 root_pass_list.at(0)->quad_list.ElementAt(1)->visible_rect =
964 gfx::Rect(0, 0, 8, 100);
966 root_pass_list[0]->transform_to_root_target.Translate(10, 5);
968 scoped_ptr<DelegatedFrameData> root_frame_data(new DelegatedFrameData);
969 root_pass_list.swap(root_frame_data->render_pass_list);
971 scoped_ptr<CompositorFrame> root_frame(new CompositorFrame);
972 root_frame->delegated_frame_data = root_frame_data.Pass();
974 factory_.SubmitFrame(root_surface_id_, root_frame.Pass(),
975 SurfaceFactory::DrawCallback());
977 scoped_ptr<CompositorFrame> aggregated_frame =
978 aggregator_.Aggregate(root_surface_id_);
980 ASSERT_TRUE(aggregated_frame);
981 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
983 DelegatedFrameData* frame_data = aggregated_frame->delegated_frame_data.get();
985 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
987 ASSERT_EQ(3u, aggregated_pass_list.size());
989 ASSERT_EQ(1u, aggregated_pass_list[0]->shared_quad_state_list.size());
991 // The first pass should have one shared quad state for the one solid color
992 // quad.
993 EXPECT_EQ(1u, aggregated_pass_list[0]->shared_quad_state_list.size());
994 // The second pass should have just two shared quad states. We'll
995 // verify the properties through the quads.
996 EXPECT_EQ(2u, aggregated_pass_list[1]->shared_quad_state_list.size());
998 EXPECT_EQ(1u, aggregated_pass_list[2]->shared_quad_state_list.size());
1000 SharedQuadState* aggregated_first_pass_sqs =
1001 aggregated_pass_list[0]->shared_quad_state_list.front();
1003 // The first pass's transform should be unaffected by the embedding and still
1004 // be a translation by +5 in the x direction.
1005 gfx::Transform expected_aggregated_first_pass_sqs_transform;
1006 expected_aggregated_first_pass_sqs_transform.Translate(5, 0);
1007 EXPECT_EQ(expected_aggregated_first_pass_sqs_transform.ToString(),
1008 aggregated_first_pass_sqs->content_to_target_transform.ToString());
1010 // The first pass's transform to the root target should include the aggregated
1011 // transform, including the transform from the child pass to the root.
1012 gfx::Transform expected_first_pass_transform_to_root_target;
1013 expected_first_pass_transform_to_root_target.Translate(10, 5);
1014 expected_first_pass_transform_to_root_target.Translate(0, 10);
1015 expected_first_pass_transform_to_root_target.Scale(2, 3);
1016 expected_first_pass_transform_to_root_target.Translate(8, 0);
1017 EXPECT_EQ(expected_first_pass_transform_to_root_target.ToString(),
1018 aggregated_pass_list[0]->transform_to_root_target.ToString());
1020 ASSERT_EQ(2u, aggregated_pass_list[1]->quad_list.size());
1022 gfx::Transform expected_root_pass_quad_transforms[2];
1023 // The first quad in the root pass is the solid color quad from the original
1024 // root surface. Its transform should be unaffected by the aggregation and
1025 // still be +7 in the y direction.
1026 expected_root_pass_quad_transforms[0].Translate(0, 7);
1027 // The second quad in the root pass is aggregated from the child surface so
1028 // its transform should be the combination of its original translation
1029 // (0, 10), the middle surface draw quad's scale of (2, 3), and the
1030 // child surface draw quad's translation (8, 0).
1031 expected_root_pass_quad_transforms[1].Translate(0, 10);
1032 expected_root_pass_quad_transforms[1].Scale(2, 3);
1033 expected_root_pass_quad_transforms[1].Translate(8, 0);
1035 for (auto iter = aggregated_pass_list[1]->quad_list.cbegin();
1036 iter != aggregated_pass_list[1]->quad_list.cend();
1037 ++iter) {
1038 EXPECT_EQ(expected_root_pass_quad_transforms[iter.index()].ToString(),
1039 iter->quadTransform().ToString())
1040 << iter.index();
1043 EXPECT_TRUE(
1044 aggregated_pass_list[1]->shared_quad_state_list.ElementAt(1)->is_clipped);
1046 // The second quad in the root pass is aggregated from the child, so its
1047 // clip rect must be transformed by the child's translation/scale and
1048 // clipped be the visible_rects for both children.
1049 EXPECT_EQ(gfx::Rect(0, 13, 8, 12).ToString(),
1050 aggregated_pass_list[1]
1051 ->shared_quad_state_list.ElementAt(1)
1052 ->clip_rect.ToString());
1054 factory_.Destroy(middle_surface_id);
1055 factory_.Destroy(child_surface_id);
1058 // Tests that damage rects are aggregated correctly when surfaces change.
1059 TEST_F(SurfaceAggregatorValidSurfaceTest, AggregateDamageRect) {
1060 SurfaceId child_surface_id = allocator_.GenerateId();
1061 factory_.Create(child_surface_id);
1062 RenderPassId child_pass_id = RenderPassId(1, 1);
1063 test::Quad child_quads[] = {test::Quad::RenderPassQuad(child_pass_id)};
1064 test::Pass child_passes[] = {
1065 test::Pass(child_quads, arraysize(child_quads), child_pass_id)};
1067 RenderPassList child_pass_list;
1068 AddPasses(&child_pass_list,
1069 gfx::Rect(SurfaceSize()),
1070 child_passes,
1071 arraysize(child_passes));
1073 RenderPass* child_root_pass = child_pass_list.at(0u);
1074 SharedQuadState* child_root_pass_sqs =
1075 child_root_pass->shared_quad_state_list.front();
1076 child_root_pass_sqs->content_to_target_transform.Translate(8, 0);
1078 scoped_ptr<DelegatedFrameData> child_frame_data(new DelegatedFrameData);
1079 child_pass_list.swap(child_frame_data->render_pass_list);
1081 scoped_ptr<CompositorFrame> child_frame(new CompositorFrame);
1082 child_frame->delegated_frame_data = child_frame_data.Pass();
1084 factory_.SubmitFrame(child_surface_id, child_frame.Pass(),
1085 SurfaceFactory::DrawCallback());
1087 RenderPassId pass_id(5, 10);
1088 test::Quad first_quads[] = {test::Quad::SurfaceQuad(child_surface_id, 1.f)};
1089 test::Quad root_quads[] = {test::Quad::RenderPassQuad(pass_id)};
1091 test::Pass root_passes[] = {
1092 test::Pass(first_quads, arraysize(first_quads), pass_id),
1093 test::Pass(root_quads, arraysize(root_quads))};
1095 RenderPassList root_pass_list;
1096 AddPasses(&root_pass_list,
1097 gfx::Rect(SurfaceSize()),
1098 root_passes,
1099 arraysize(root_passes));
1101 root_pass_list.at(0)
1102 ->shared_quad_state_list.front()
1103 ->content_to_target_transform.Translate(0, 10);
1104 root_pass_list.at(0)->damage_rect = gfx::Rect(5, 5, 10, 10);
1105 root_pass_list.at(1)->damage_rect = gfx::Rect(5, 5, 100, 100);
1107 scoped_ptr<DelegatedFrameData> root_frame_data(new DelegatedFrameData);
1108 root_pass_list.swap(root_frame_data->render_pass_list);
1110 scoped_ptr<CompositorFrame> root_frame(new CompositorFrame);
1111 root_frame->delegated_frame_data = root_frame_data.Pass();
1113 factory_.SubmitFrame(root_surface_id_, root_frame.Pass(),
1114 SurfaceFactory::DrawCallback());
1116 scoped_ptr<CompositorFrame> aggregated_frame =
1117 aggregator_.Aggregate(root_surface_id_);
1119 ASSERT_TRUE(aggregated_frame);
1120 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
1122 DelegatedFrameData* frame_data = aggregated_frame->delegated_frame_data.get();
1124 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
1126 ASSERT_EQ(2u, aggregated_pass_list.size());
1128 // Damage rect for first aggregation should contain entire root surface.
1129 EXPECT_TRUE(
1130 aggregated_pass_list[1]->damage_rect.Contains(gfx::Rect(SurfaceSize())));
1133 AddPasses(&child_pass_list,
1134 gfx::Rect(SurfaceSize()),
1135 child_passes,
1136 arraysize(child_passes));
1138 RenderPass* child_root_pass = child_pass_list.at(0u);
1139 SharedQuadState* child_root_pass_sqs =
1140 child_root_pass->shared_quad_state_list.front();
1141 child_root_pass_sqs->content_to_target_transform.Translate(8, 0);
1142 child_root_pass->damage_rect = gfx::Rect(10, 10, 10, 10);
1144 scoped_ptr<DelegatedFrameData> child_frame_data(new DelegatedFrameData);
1145 child_pass_list.swap(child_frame_data->render_pass_list);
1147 scoped_ptr<CompositorFrame> child_frame(new CompositorFrame);
1148 child_frame->delegated_frame_data = child_frame_data.Pass();
1150 factory_.SubmitFrame(child_surface_id, child_frame.Pass(),
1151 SurfaceFactory::DrawCallback());
1153 scoped_ptr<CompositorFrame> aggregated_frame =
1154 aggregator_.Aggregate(root_surface_id_);
1156 ASSERT_TRUE(aggregated_frame);
1157 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
1159 DelegatedFrameData* frame_data =
1160 aggregated_frame->delegated_frame_data.get();
1162 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
1164 ASSERT_EQ(2u, aggregated_pass_list.size());
1166 // Outer surface didn't change, so transformed inner damage rect should be
1167 // used.
1168 EXPECT_EQ(gfx::Rect(10, 20, 10, 10).ToString(),
1169 aggregated_pass_list[1]->damage_rect.ToString());
1173 RenderPassList root_pass_list;
1174 AddPasses(&root_pass_list,
1175 gfx::Rect(SurfaceSize()),
1176 root_passes,
1177 arraysize(root_passes));
1179 root_pass_list.at(0)
1180 ->shared_quad_state_list.front()
1181 ->content_to_target_transform.Translate(0, 10);
1182 root_pass_list.at(0)->damage_rect = gfx::Rect(0, 0, 1, 1);
1184 scoped_ptr<DelegatedFrameData> root_frame_data(new DelegatedFrameData);
1185 root_pass_list.swap(root_frame_data->render_pass_list);
1187 scoped_ptr<CompositorFrame> root_frame(new CompositorFrame);
1188 root_frame->delegated_frame_data = root_frame_data.Pass();
1190 factory_.SubmitFrame(root_surface_id_, root_frame.Pass(),
1191 SurfaceFactory::DrawCallback());
1195 RenderPassList root_pass_list;
1196 AddPasses(&root_pass_list,
1197 gfx::Rect(SurfaceSize()),
1198 root_passes,
1199 arraysize(root_passes));
1201 root_pass_list.at(0)
1202 ->shared_quad_state_list.front()
1203 ->content_to_target_transform.Translate(0, 10);
1204 root_pass_list.at(0)->damage_rect = gfx::Rect(1, 1, 1, 1);
1206 scoped_ptr<DelegatedFrameData> root_frame_data(new DelegatedFrameData);
1207 root_pass_list.swap(root_frame_data->render_pass_list);
1209 scoped_ptr<CompositorFrame> root_frame(new CompositorFrame);
1210 root_frame->delegated_frame_data = root_frame_data.Pass();
1212 factory_.SubmitFrame(root_surface_id_, root_frame.Pass(),
1213 SurfaceFactory::DrawCallback());
1215 scoped_ptr<CompositorFrame> aggregated_frame =
1216 aggregator_.Aggregate(root_surface_id_);
1218 ASSERT_TRUE(aggregated_frame);
1219 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
1221 DelegatedFrameData* frame_data =
1222 aggregated_frame->delegated_frame_data.get();
1224 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
1226 ASSERT_EQ(2u, aggregated_pass_list.size());
1228 // The root surface was enqueued without being aggregated once, so it should
1229 // be treated as completely damaged.
1230 EXPECT_TRUE(aggregated_pass_list[1]->damage_rect.Contains(
1231 gfx::Rect(SurfaceSize())));
1234 // No Surface changed, so no damage should be given.
1236 scoped_ptr<CompositorFrame> aggregated_frame =
1237 aggregator_.Aggregate(root_surface_id_);
1239 ASSERT_TRUE(aggregated_frame);
1240 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
1242 DelegatedFrameData* frame_data =
1243 aggregated_frame->delegated_frame_data.get();
1245 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
1247 ASSERT_EQ(2u, aggregated_pass_list.size());
1249 EXPECT_TRUE(aggregated_pass_list[1]->damage_rect.IsEmpty());
1252 // SetFullDamageRectForSurface should cause the entire output to be
1253 // marked as damaged.
1255 aggregator_.SetFullDamageForSurface(root_surface_id_);
1256 scoped_ptr<CompositorFrame> aggregated_frame =
1257 aggregator_.Aggregate(root_surface_id_);
1259 ASSERT_TRUE(aggregated_frame);
1260 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
1262 DelegatedFrameData* frame_data =
1263 aggregated_frame->delegated_frame_data.get();
1265 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
1267 ASSERT_EQ(2u, aggregated_pass_list.size());
1269 EXPECT_TRUE(aggregated_pass_list[1]->damage_rect.Contains(
1270 gfx::Rect(SurfaceSize())));
1273 factory_.Destroy(child_surface_id);
1276 class SurfaceAggregatorWithResourcesTest : public testing::Test {
1277 public:
1278 void SetUp() override {
1279 output_surface_ = FakeOutputSurface::CreateSoftware(
1280 make_scoped_ptr(new SoftwareOutputDevice));
1281 output_surface_->BindToClient(&output_surface_client_);
1282 shared_bitmap_manager_.reset(new TestSharedBitmapManager);
1284 resource_provider_ = FakeResourceProvider::Create(
1285 output_surface_.get(), shared_bitmap_manager_.get());
1287 aggregator_.reset(
1288 new SurfaceAggregator(&manager_, resource_provider_.get()));
1291 protected:
1292 SurfaceManager manager_;
1293 FakeOutputSurfaceClient output_surface_client_;
1294 scoped_ptr<OutputSurface> output_surface_;
1295 scoped_ptr<SharedBitmapManager> shared_bitmap_manager_;
1296 scoped_ptr<ResourceProvider> resource_provider_;
1297 scoped_ptr<SurfaceAggregator> aggregator_;
1300 class ResourceTrackingSurfaceFactoryClient : public SurfaceFactoryClient {
1301 public:
1302 ResourceTrackingSurfaceFactoryClient() {}
1303 ~ResourceTrackingSurfaceFactoryClient() override {}
1305 void ReturnResources(const ReturnedResourceArray& resources) override {
1306 returned_resources_ = resources;
1309 ReturnedResourceArray returned_resources() const {
1310 return returned_resources_;
1313 private:
1314 ReturnedResourceArray returned_resources_;
1316 DISALLOW_COPY_AND_ASSIGN(ResourceTrackingSurfaceFactoryClient);
1319 void SubmitFrameWithResources(ResourceId* resource_ids,
1320 size_t num_resource_ids,
1321 bool valid,
1322 SurfaceId child_id,
1323 SurfaceFactory* factory,
1324 SurfaceId surface_id) {
1325 scoped_ptr<DelegatedFrameData> frame_data(new DelegatedFrameData);
1326 scoped_ptr<RenderPass> pass = RenderPass::Create();
1327 pass->id = RenderPassId(1, 1);
1328 SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState();
1329 sqs->opacity = 1.f;
1330 if (!child_id.is_null()) {
1331 SurfaceDrawQuad* surface_quad =
1332 pass->CreateAndAppendDrawQuad<SurfaceDrawQuad>();
1333 surface_quad->SetNew(sqs, gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1),
1334 child_id);
1337 for (size_t i = 0u; i < num_resource_ids; ++i) {
1338 TransferableResource resource;
1339 resource.id = resource_ids[i];
1340 // ResourceProvider is software, so only software resources are valid.
1341 resource.is_software = valid;
1342 frame_data->resource_list.push_back(resource);
1343 TextureDrawQuad* quad = pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
1344 const gfx::Rect rect;
1345 const gfx::Rect opaque_rect;
1346 const gfx::Rect visible_rect;
1347 bool needs_blending = false;
1348 bool premultiplied_alpha = false;
1349 const gfx::PointF uv_top_left;
1350 const gfx::PointF uv_bottom_right;
1351 SkColor background_color = SK_ColorGREEN;
1352 const float vertex_opacity[4] = {0.f, 0.f, 1.f, 1.f};
1353 bool flipped = false;
1354 bool nearest_neighbor = false;
1355 quad->SetAll(sqs,
1356 rect,
1357 opaque_rect,
1358 visible_rect,
1359 needs_blending,
1360 resource_ids[i],
1361 premultiplied_alpha,
1362 uv_top_left,
1363 uv_bottom_right,
1364 background_color,
1365 vertex_opacity,
1366 flipped,
1367 nearest_neighbor);
1369 frame_data->render_pass_list.push_back(pass.Pass());
1370 scoped_ptr<CompositorFrame> frame(new CompositorFrame);
1371 frame->delegated_frame_data = frame_data.Pass();
1372 factory->SubmitFrame(surface_id, frame.Pass(),
1373 SurfaceFactory::DrawCallback());
1376 TEST_F(SurfaceAggregatorWithResourcesTest, TakeResourcesOneSurface) {
1377 ResourceTrackingSurfaceFactoryClient client;
1378 SurfaceFactory factory(&manager_, &client);
1379 SurfaceId surface_id(7u);
1380 factory.Create(surface_id);
1382 ResourceId ids[] = {11, 12, 13};
1383 SubmitFrameWithResources(ids, arraysize(ids), true, SurfaceId(), &factory,
1384 surface_id);
1386 scoped_ptr<CompositorFrame> frame = aggregator_->Aggregate(surface_id);
1388 // Nothing should be available to be returned yet.
1389 EXPECT_TRUE(client.returned_resources().empty());
1391 SubmitFrameWithResources(NULL, 0u, true, SurfaceId(), &factory, surface_id);
1393 frame = aggregator_->Aggregate(surface_id);
1395 ASSERT_EQ(3u, client.returned_resources().size());
1396 ResourceId returned_ids[3];
1397 for (size_t i = 0; i < 3; ++i) {
1398 returned_ids[i] = client.returned_resources()[i].id;
1400 EXPECT_THAT(returned_ids,
1401 testing::WhenSorted(testing::ElementsAreArray(ids)));
1402 factory.Destroy(surface_id);
1405 TEST_F(SurfaceAggregatorWithResourcesTest, TakeInvalidResources) {
1406 ResourceTrackingSurfaceFactoryClient client;
1407 SurfaceFactory factory(&manager_, &client);
1408 SurfaceId surface_id(7u);
1409 factory.Create(surface_id);
1411 scoped_ptr<DelegatedFrameData> frame_data(new DelegatedFrameData);
1412 scoped_ptr<RenderPass> pass = RenderPass::Create();
1413 pass->id = RenderPassId(1, 1);
1414 TransferableResource resource;
1415 resource.id = 11;
1416 // ResourceProvider is software but resource is not, so it should be
1417 // ignored.
1418 resource.is_software = false;
1419 frame_data->resource_list.push_back(resource);
1420 frame_data->render_pass_list.push_back(pass.Pass());
1421 scoped_ptr<CompositorFrame> frame(new CompositorFrame);
1422 frame->delegated_frame_data = frame_data.Pass();
1423 factory.SubmitFrame(surface_id, frame.Pass(), SurfaceFactory::DrawCallback());
1425 scoped_ptr<CompositorFrame> returned_frame =
1426 aggregator_->Aggregate(surface_id);
1428 // Nothing should be available to be returned yet.
1429 EXPECT_TRUE(client.returned_resources().empty());
1431 SubmitFrameWithResources(NULL, 0, true, SurfaceId(), &factory, surface_id);
1432 ASSERT_EQ(1u, client.returned_resources().size());
1433 EXPECT_EQ(11u, client.returned_resources()[0].id);
1435 factory.Destroy(surface_id);
1438 TEST_F(SurfaceAggregatorWithResourcesTest, TwoSurfaces) {
1439 ResourceTrackingSurfaceFactoryClient client;
1440 SurfaceFactory factory(&manager_, &client);
1441 SurfaceId surface_id(7u);
1442 factory.Create(surface_id);
1443 SurfaceId surface_id2(8u);
1444 factory.Create(surface_id2);
1446 ResourceId ids[] = {11, 12, 13};
1447 SubmitFrameWithResources(ids, arraysize(ids), true, SurfaceId(), &factory,
1448 surface_id);
1449 ResourceId ids2[] = {14, 15, 16};
1450 SubmitFrameWithResources(ids2, arraysize(ids2), true, SurfaceId(), &factory,
1451 surface_id2);
1453 scoped_ptr<CompositorFrame> frame = aggregator_->Aggregate(surface_id);
1455 SubmitFrameWithResources(NULL, 0, true, SurfaceId(), &factory, surface_id);
1457 // Nothing should be available to be returned yet.
1458 EXPECT_TRUE(client.returned_resources().empty());
1460 frame = aggregator_->Aggregate(surface_id2);
1462 // surface_id wasn't referenced, so its resources should be returned.
1463 ASSERT_EQ(3u, client.returned_resources().size());
1464 ResourceId returned_ids[3];
1465 for (size_t i = 0; i < 3; ++i) {
1466 returned_ids[i] = client.returned_resources()[i].id;
1468 EXPECT_THAT(returned_ids,
1469 testing::WhenSorted(testing::ElementsAreArray(ids)));
1470 EXPECT_EQ(3u, resource_provider_->num_resources());
1471 factory.Destroy(surface_id);
1472 factory.Destroy(surface_id2);
1475 // Ensure that aggregator completely ignores Surfaces that reference invalid
1476 // resources.
1477 TEST_F(SurfaceAggregatorWithResourcesTest, InvalidChildSurface) {
1478 ResourceTrackingSurfaceFactoryClient client;
1479 SurfaceFactory factory(&manager_, &client);
1480 SurfaceId root_surface_id(7u);
1481 factory.Create(root_surface_id);
1482 SurfaceId middle_surface_id(8u);
1483 factory.Create(middle_surface_id);
1484 SurfaceId child_surface_id(9u);
1485 factory.Create(child_surface_id);
1487 ResourceId ids[] = {14, 15, 16};
1488 SubmitFrameWithResources(ids, arraysize(ids), true, SurfaceId(), &factory,
1489 child_surface_id);
1491 ResourceId ids2[] = {17, 18, 19};
1492 SubmitFrameWithResources(ids2, arraysize(ids2), false, child_surface_id,
1493 &factory, middle_surface_id);
1495 ResourceId ids3[] = {20, 21, 22};
1496 SubmitFrameWithResources(ids3, arraysize(ids3), true, middle_surface_id,
1497 &factory, root_surface_id);
1499 scoped_ptr<CompositorFrame> frame;
1500 frame = aggregator_->Aggregate(root_surface_id);
1502 RenderPassList* pass_list = &frame->delegated_frame_data->render_pass_list;
1503 ASSERT_EQ(1u, pass_list->size());
1504 EXPECT_EQ(1u, pass_list->back()->shared_quad_state_list.size());
1505 EXPECT_EQ(3u, pass_list->back()->quad_list.size());
1507 SubmitFrameWithResources(ids2, arraysize(ids), true, child_surface_id,
1508 &factory, middle_surface_id);
1510 frame = aggregator_->Aggregate(root_surface_id);
1512 pass_list = &frame->delegated_frame_data->render_pass_list;
1513 ASSERT_EQ(1u, pass_list->size());
1514 EXPECT_EQ(3u, pass_list->back()->shared_quad_state_list.size());
1515 EXPECT_EQ(9u, pass_list->back()->quad_list.size());
1517 factory.Destroy(root_surface_id);
1518 factory.Destroy(child_surface_id);
1519 factory.Destroy(middle_surface_id);
1522 } // namespace
1523 } // namespace cc