[Telemetry] Remove adb_commands usage from android_device.
[chromium-blink-merge.git] / cc / surfaces / surface_aggregator_unittest.cc
blobe4ade82116e073ea3fecbfb9f1072bb0ee2c0ad2
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 layer_to_target_transform;
694 const gfx::Size layer_bounds(size);
695 const gfx::Rect visible_layer_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(layer_to_target_transform, layer_bounds, visible_layer_rect,
704 clip_rect, is_clipped, opacity, blend_mode, 0);
706 SolidColorDrawQuad* color_quad =
707 pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
708 color_quad->SetNew(pass->shared_quad_state_list.back(), visible_layer_rect,
709 visible_layer_rect, SK_ColorGREEN,
710 force_anti_aliasing_off);
713 // This tests that we update shared quad state pointers correctly within
714 // aggregated passes. The shared quad state list on the aggregated pass will
715 // include the shared quad states from each pass in one list so the quads will
716 // end up pointed to shared quad state objects at different offsets. This test
717 // uses the blend_mode value stored on the shared quad state to track the shared
718 // quad state, but anything saved on the shared quad state would work.
720 // This test has 4 surfaces in the following structure:
721 // root_surface -> quad with kClear_Mode,
722 // [child_one_surface],
723 // quad with kDstOver_Mode,
724 // [child_two_surface],
725 // quad with kDstIn_Mode
726 // child_one_surface -> quad with kSrc_Mode,
727 // [grandchild_surface],
728 // quad with kSrcOver_Mode
729 // child_two_surface -> quad with kSrcIn_Mode
730 // grandchild_surface -> quad with kDst_Mode
732 // Resulting in the following aggregated pass:
733 // quad_root_0 - blend_mode kClear_Mode
734 // quad_child_one_0 - blend_mode kSrc_Mode
735 // quad_grandchild_0 - blend_mode kDst_Mode
736 // quad_child_one_1 - blend_mode kSrcOver_Mode
737 // quad_root_1 - blend_mode kDstOver_Mode
738 // quad_child_two_0 - blend_mode kSrcIn_Mode
739 // quad_root_2 - blend_mode kDstIn_Mode
740 TEST_F(SurfaceAggregatorValidSurfaceTest, AggregateSharedQuadStateProperties) {
741 const SkXfermode::Mode blend_modes[] = {SkXfermode::kClear_Mode, // 0
742 SkXfermode::kSrc_Mode, // 1
743 SkXfermode::kDst_Mode, // 2
744 SkXfermode::kSrcOver_Mode, // 3
745 SkXfermode::kDstOver_Mode, // 4
746 SkXfermode::kSrcIn_Mode, // 5
747 SkXfermode::kDstIn_Mode, // 6
750 RenderPassId pass_id(1, 1);
751 SurfaceId grandchild_surface_id = allocator_.GenerateId();
752 factory_.Create(grandchild_surface_id);
753 scoped_ptr<RenderPass> grandchild_pass = RenderPass::Create();
754 gfx::Rect output_rect(SurfaceSize());
755 gfx::Rect damage_rect(SurfaceSize());
756 gfx::Transform transform_to_root_target;
757 grandchild_pass->SetNew(
758 pass_id, output_rect, damage_rect, transform_to_root_target);
759 AddSolidColorQuadWithBlendMode(
760 SurfaceSize(), grandchild_pass.get(), blend_modes[2]);
761 QueuePassAsFrame(grandchild_pass.Pass(), grandchild_surface_id);
763 SurfaceId child_one_surface_id = allocator_.GenerateId();
764 factory_.Create(child_one_surface_id);
766 scoped_ptr<RenderPass> child_one_pass = RenderPass::Create();
767 child_one_pass->SetNew(
768 pass_id, output_rect, damage_rect, transform_to_root_target);
769 AddSolidColorQuadWithBlendMode(
770 SurfaceSize(), child_one_pass.get(), blend_modes[1]);
771 SurfaceDrawQuad* grandchild_surface_quad =
772 child_one_pass->CreateAndAppendDrawQuad<SurfaceDrawQuad>();
773 grandchild_surface_quad->SetNew(child_one_pass->shared_quad_state_list.back(),
774 gfx::Rect(SurfaceSize()),
775 gfx::Rect(SurfaceSize()),
776 grandchild_surface_id);
777 AddSolidColorQuadWithBlendMode(
778 SurfaceSize(), child_one_pass.get(), blend_modes[3]);
779 QueuePassAsFrame(child_one_pass.Pass(), child_one_surface_id);
781 SurfaceId child_two_surface_id = allocator_.GenerateId();
782 factory_.Create(child_two_surface_id);
784 scoped_ptr<RenderPass> child_two_pass = RenderPass::Create();
785 child_two_pass->SetNew(
786 pass_id, output_rect, damage_rect, transform_to_root_target);
787 AddSolidColorQuadWithBlendMode(
788 SurfaceSize(), child_two_pass.get(), blend_modes[5]);
789 QueuePassAsFrame(child_two_pass.Pass(), child_two_surface_id);
791 scoped_ptr<RenderPass> root_pass = RenderPass::Create();
792 root_pass->SetNew(
793 pass_id, output_rect, damage_rect, transform_to_root_target);
795 AddSolidColorQuadWithBlendMode(
796 SurfaceSize(), root_pass.get(), blend_modes[0]);
797 SurfaceDrawQuad* child_one_surface_quad =
798 root_pass->CreateAndAppendDrawQuad<SurfaceDrawQuad>();
799 child_one_surface_quad->SetNew(root_pass->shared_quad_state_list.back(),
800 gfx::Rect(SurfaceSize()),
801 gfx::Rect(SurfaceSize()),
802 child_one_surface_id);
803 AddSolidColorQuadWithBlendMode(
804 SurfaceSize(), root_pass.get(), blend_modes[4]);
805 SurfaceDrawQuad* child_two_surface_quad =
806 root_pass->CreateAndAppendDrawQuad<SurfaceDrawQuad>();
807 child_two_surface_quad->SetNew(root_pass->shared_quad_state_list.back(),
808 gfx::Rect(SurfaceSize()),
809 gfx::Rect(SurfaceSize()),
810 child_two_surface_id);
811 AddSolidColorQuadWithBlendMode(
812 SurfaceSize(), root_pass.get(), blend_modes[6]);
814 QueuePassAsFrame(root_pass.Pass(), root_surface_id_);
816 scoped_ptr<CompositorFrame> aggregated_frame =
817 aggregator_.Aggregate(root_surface_id_);
819 ASSERT_TRUE(aggregated_frame);
820 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
822 DelegatedFrameData* frame_data = aggregated_frame->delegated_frame_data.get();
824 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
826 ASSERT_EQ(1u, aggregated_pass_list.size());
828 const QuadList& aggregated_quad_list = aggregated_pass_list[0]->quad_list;
830 ASSERT_EQ(7u, aggregated_quad_list.size());
832 for (auto iter = aggregated_quad_list.cbegin();
833 iter != aggregated_quad_list.cend();
834 ++iter) {
835 EXPECT_EQ(blend_modes[iter.index()], iter->shared_quad_state->blend_mode)
836 << iter.index();
838 factory_.Destroy(child_one_surface_id);
839 factory_.Destroy(child_two_surface_id);
840 factory_.Destroy(grandchild_surface_id);
843 // This tests that when aggregating a frame with multiple render passes that we
844 // map the transforms for the root pass but do not modify the transform on child
845 // passes.
847 // The root surface has one pass with a surface quad transformed by +10 in the y
848 // direction.
850 // The middle surface has one pass with a surface quad scaled by 2 in the x
851 // and 3 in the y directions.
853 // The child surface has two passes. The first pass has a quad with a transform
854 // of +5 in the x direction. The second pass has a reference to the first pass'
855 // pass id and a transform of +8 in the x direction.
857 // After aggregation, the child surface's root pass quad should have all
858 // transforms concatenated for a total transform of +23 x, +10 y. The
859 // contributing render pass' transform in the aggregate frame should not be
860 // affected.
861 TEST_F(SurfaceAggregatorValidSurfaceTest, AggregateMultiplePassWithTransform) {
862 // Innermost child surface.
863 SurfaceId child_surface_id = allocator_.GenerateId();
865 factory_.Create(child_surface_id);
866 RenderPassId child_pass_id[] = {RenderPassId(1, 1), RenderPassId(1, 2)};
867 test::Quad child_quads[][1] = {
868 {test::Quad::SolidColorQuad(SK_ColorGREEN)},
869 {test::Quad::RenderPassQuad(child_pass_id[0])},
871 test::Pass child_passes[] = {
872 test::Pass(child_quads[0], arraysize(child_quads[0]), child_pass_id[0]),
873 test::Pass(child_quads[1], arraysize(child_quads[1]),
874 child_pass_id[1])};
876 RenderPassList child_pass_list;
877 AddPasses(&child_pass_list, gfx::Rect(SurfaceSize()), child_passes,
878 arraysize(child_passes));
880 RenderPass* child_nonroot_pass = child_pass_list.at(0u);
881 child_nonroot_pass->transform_to_root_target.Translate(8, 0);
882 SharedQuadState* child_nonroot_pass_sqs =
883 child_nonroot_pass->shared_quad_state_list.front();
884 child_nonroot_pass_sqs->quad_to_target_transform.Translate(5, 0);
886 RenderPass* child_root_pass = child_pass_list.at(1u);
887 SharedQuadState* child_root_pass_sqs =
888 child_root_pass->shared_quad_state_list.front();
889 child_root_pass_sqs->quad_to_target_transform.Translate(8, 0);
890 child_root_pass_sqs->is_clipped = true;
891 child_root_pass_sqs->clip_rect = gfx::Rect(0, 0, 5, 5);
893 scoped_ptr<DelegatedFrameData> child_frame_data(new DelegatedFrameData);
894 child_pass_list.swap(child_frame_data->render_pass_list);
896 scoped_ptr<CompositorFrame> child_frame(new CompositorFrame);
897 child_frame->delegated_frame_data = child_frame_data.Pass();
899 factory_.SubmitFrame(child_surface_id, child_frame.Pass(),
900 SurfaceFactory::DrawCallback());
903 // Middle child surface.
904 SurfaceId middle_surface_id = allocator_.GenerateId();
906 factory_.Create(middle_surface_id);
907 test::Quad middle_quads[] = {
908 test::Quad::SurfaceQuad(child_surface_id, 1.f)};
909 test::Pass middle_passes[] = {
910 test::Pass(middle_quads, arraysize(middle_quads)),
913 RenderPassList middle_pass_list;
914 AddPasses(&middle_pass_list, gfx::Rect(SurfaceSize()), middle_passes,
915 arraysize(middle_passes));
917 RenderPass* middle_root_pass = middle_pass_list.at(0u);
918 middle_root_pass->quad_list.ElementAt(0)->visible_rect =
919 gfx::Rect(0, 1, 100, 7);
920 SharedQuadState* middle_root_pass_sqs =
921 middle_root_pass->shared_quad_state_list.front();
922 middle_root_pass_sqs->quad_to_target_transform.Scale(2, 3);
924 scoped_ptr<DelegatedFrameData> middle_frame_data(new DelegatedFrameData);
925 middle_pass_list.swap(middle_frame_data->render_pass_list);
927 scoped_ptr<CompositorFrame> middle_frame(new CompositorFrame);
928 middle_frame->delegated_frame_data = middle_frame_data.Pass();
930 factory_.SubmitFrame(middle_surface_id, middle_frame.Pass(),
931 SurfaceFactory::DrawCallback());
934 // Root surface.
935 test::Quad secondary_quads[] = {
936 test::Quad::SolidColorQuad(1),
937 test::Quad::SurfaceQuad(middle_surface_id, 1.f)};
938 test::Quad root_quads[] = {test::Quad::SolidColorQuad(1)};
939 test::Pass root_passes[] = {
940 test::Pass(secondary_quads, arraysize(secondary_quads)),
941 test::Pass(root_quads, arraysize(root_quads))};
943 RenderPassList root_pass_list;
944 AddPasses(&root_pass_list,
945 gfx::Rect(SurfaceSize()),
946 root_passes,
947 arraysize(root_passes));
949 root_pass_list.at(0)
950 ->shared_quad_state_list.front()
951 ->quad_to_target_transform.Translate(0, 7);
952 root_pass_list.at(0)
953 ->shared_quad_state_list.ElementAt(1)
954 ->quad_to_target_transform.Translate(0, 10);
955 root_pass_list.at(0)->quad_list.ElementAt(1)->visible_rect =
956 gfx::Rect(0, 0, 8, 100);
958 root_pass_list[0]->transform_to_root_target.Translate(10, 5);
960 scoped_ptr<DelegatedFrameData> root_frame_data(new DelegatedFrameData);
961 root_pass_list.swap(root_frame_data->render_pass_list);
963 scoped_ptr<CompositorFrame> root_frame(new CompositorFrame);
964 root_frame->delegated_frame_data = root_frame_data.Pass();
966 factory_.SubmitFrame(root_surface_id_, root_frame.Pass(),
967 SurfaceFactory::DrawCallback());
969 scoped_ptr<CompositorFrame> aggregated_frame =
970 aggregator_.Aggregate(root_surface_id_);
972 ASSERT_TRUE(aggregated_frame);
973 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
975 DelegatedFrameData* frame_data = aggregated_frame->delegated_frame_data.get();
977 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
979 ASSERT_EQ(3u, aggregated_pass_list.size());
981 ASSERT_EQ(1u, aggregated_pass_list[0]->shared_quad_state_list.size());
983 // The first pass should have one shared quad state for the one solid color
984 // quad.
985 EXPECT_EQ(1u, aggregated_pass_list[0]->shared_quad_state_list.size());
986 // The second pass should have just two shared quad states. We'll
987 // verify the properties through the quads.
988 EXPECT_EQ(2u, aggregated_pass_list[1]->shared_quad_state_list.size());
990 EXPECT_EQ(1u, aggregated_pass_list[2]->shared_quad_state_list.size());
992 SharedQuadState* aggregated_first_pass_sqs =
993 aggregated_pass_list[0]->shared_quad_state_list.front();
995 // The first pass's transform should be unaffected by the embedding and still
996 // be a translation by +5 in the x direction.
997 gfx::Transform expected_aggregated_first_pass_sqs_transform;
998 expected_aggregated_first_pass_sqs_transform.Translate(5, 0);
999 EXPECT_EQ(expected_aggregated_first_pass_sqs_transform.ToString(),
1000 aggregated_first_pass_sqs->quad_to_target_transform.ToString());
1002 // The first pass's transform to the root target should include the aggregated
1003 // transform, including the transform from the child pass to the root.
1004 gfx::Transform expected_first_pass_transform_to_root_target;
1005 expected_first_pass_transform_to_root_target.Translate(10, 5);
1006 expected_first_pass_transform_to_root_target.Translate(0, 10);
1007 expected_first_pass_transform_to_root_target.Scale(2, 3);
1008 expected_first_pass_transform_to_root_target.Translate(8, 0);
1009 EXPECT_EQ(expected_first_pass_transform_to_root_target.ToString(),
1010 aggregated_pass_list[0]->transform_to_root_target.ToString());
1012 ASSERT_EQ(2u, aggregated_pass_list[1]->quad_list.size());
1014 gfx::Transform expected_root_pass_quad_transforms[2];
1015 // The first quad in the root pass is the solid color quad from the original
1016 // root surface. Its transform should be unaffected by the aggregation and
1017 // still be +7 in the y direction.
1018 expected_root_pass_quad_transforms[0].Translate(0, 7);
1019 // The second quad in the root pass is aggregated from the child surface so
1020 // its transform should be the combination of its original translation
1021 // (0, 10), the middle surface draw quad's scale of (2, 3), and the
1022 // child surface draw quad's translation (8, 0).
1023 expected_root_pass_quad_transforms[1].Translate(0, 10);
1024 expected_root_pass_quad_transforms[1].Scale(2, 3);
1025 expected_root_pass_quad_transforms[1].Translate(8, 0);
1027 for (auto iter = aggregated_pass_list[1]->quad_list.cbegin();
1028 iter != aggregated_pass_list[1]->quad_list.cend();
1029 ++iter) {
1030 EXPECT_EQ(expected_root_pass_quad_transforms[iter.index()].ToString(),
1031 iter->shared_quad_state->quad_to_target_transform.ToString())
1032 << iter.index();
1035 EXPECT_TRUE(
1036 aggregated_pass_list[1]->shared_quad_state_list.ElementAt(1)->is_clipped);
1038 // The second quad in the root pass is aggregated from the child, so its
1039 // clip rect must be transformed by the child's translation/scale and
1040 // clipped be the visible_rects for both children.
1041 EXPECT_EQ(gfx::Rect(0, 13, 8, 12).ToString(),
1042 aggregated_pass_list[1]
1043 ->shared_quad_state_list.ElementAt(1)
1044 ->clip_rect.ToString());
1046 factory_.Destroy(middle_surface_id);
1047 factory_.Destroy(child_surface_id);
1050 // Tests that damage rects are aggregated correctly when surfaces change.
1051 TEST_F(SurfaceAggregatorValidSurfaceTest, AggregateDamageRect) {
1052 test::Quad child_quads[] = {test::Quad::RenderPassQuad(RenderPassId(1, 1))};
1053 test::Pass child_passes[] = {
1054 test::Pass(child_quads, arraysize(child_quads), RenderPassId(1, 1))};
1056 RenderPassList child_pass_list;
1057 AddPasses(&child_pass_list,
1058 gfx::Rect(SurfaceSize()),
1059 child_passes,
1060 arraysize(child_passes));
1062 RenderPass* child_root_pass = child_pass_list.at(0u);
1063 SharedQuadState* child_root_pass_sqs =
1064 child_root_pass->shared_quad_state_list.front();
1065 child_root_pass_sqs->quad_to_target_transform.Translate(8, 0);
1067 scoped_ptr<DelegatedFrameData> child_frame_data(new DelegatedFrameData);
1068 child_pass_list.swap(child_frame_data->render_pass_list);
1070 scoped_ptr<CompositorFrame> child_frame(new CompositorFrame);
1071 child_frame->delegated_frame_data = child_frame_data.Pass();
1073 SurfaceId child_surface_id = allocator_.GenerateId();
1074 factory_.Create(child_surface_id);
1075 factory_.SubmitFrame(child_surface_id, child_frame.Pass(),
1076 SurfaceFactory::DrawCallback());
1078 test::Quad parent_surface_quads[] = {
1079 test::Quad::SurfaceQuad(child_surface_id, 1.f)};
1080 test::Pass parent_surface_passes[] = {
1081 test::Pass(parent_surface_quads, arraysize(parent_surface_quads),
1082 RenderPassId(1, 1))};
1084 RenderPassList parent_surface_pass_list;
1085 AddPasses(&parent_surface_pass_list,
1086 gfx::Rect(SurfaceSize()),
1087 parent_surface_passes,
1088 arraysize(parent_surface_passes));
1090 // Parent surface is only used to test if the transform is applied correctly
1091 // to the child surface's damage.
1092 scoped_ptr<DelegatedFrameData> parent_surface_frame_data(
1093 new DelegatedFrameData);
1094 parent_surface_pass_list.swap(parent_surface_frame_data->render_pass_list);
1096 scoped_ptr<CompositorFrame> parent_surface_frame(new CompositorFrame);
1097 parent_surface_frame->delegated_frame_data = parent_surface_frame_data.Pass();
1099 SurfaceId parent_surface_id = allocator_.GenerateId();
1100 factory_.Create(parent_surface_id);
1101 factory_.SubmitFrame(parent_surface_id, parent_surface_frame.Pass(),
1102 SurfaceFactory::DrawCallback());
1104 test::Quad root_surface_quads[] = {
1105 test::Quad::SurfaceQuad(parent_surface_id, 1.f)};
1106 test::Quad root_render_pass_quads[] = {
1107 test::Quad::RenderPassQuad(RenderPassId(1, 1))};
1109 test::Pass root_passes[] = {
1110 test::Pass(root_surface_quads, arraysize(root_surface_quads),
1111 RenderPassId(1, 1)),
1112 test::Pass(root_render_pass_quads, arraysize(root_render_pass_quads),
1113 RenderPassId(2, 1))};
1115 RenderPassList root_pass_list;
1116 AddPasses(&root_pass_list,
1117 gfx::Rect(SurfaceSize()),
1118 root_passes,
1119 arraysize(root_passes));
1121 root_pass_list.at(0)
1122 ->shared_quad_state_list.front()
1123 ->quad_to_target_transform.Translate(0, 10);
1124 root_pass_list.at(0)->damage_rect = gfx::Rect(5, 5, 10, 10);
1125 root_pass_list.at(1)->damage_rect = gfx::Rect(5, 5, 100, 100);
1127 scoped_ptr<DelegatedFrameData> root_frame_data(new DelegatedFrameData);
1128 root_pass_list.swap(root_frame_data->render_pass_list);
1130 scoped_ptr<CompositorFrame> root_frame(new CompositorFrame);
1131 root_frame->delegated_frame_data = root_frame_data.Pass();
1133 factory_.SubmitFrame(root_surface_id_, root_frame.Pass(),
1134 SurfaceFactory::DrawCallback());
1136 scoped_ptr<CompositorFrame> aggregated_frame =
1137 aggregator_.Aggregate(root_surface_id_);
1139 ASSERT_TRUE(aggregated_frame);
1140 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
1142 DelegatedFrameData* frame_data = aggregated_frame->delegated_frame_data.get();
1144 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
1146 ASSERT_EQ(2u, aggregated_pass_list.size());
1148 // Damage rect for first aggregation should contain entire root surface.
1149 EXPECT_TRUE(
1150 aggregated_pass_list[1]->damage_rect.Contains(gfx::Rect(SurfaceSize())));
1153 AddPasses(&child_pass_list,
1154 gfx::Rect(SurfaceSize()),
1155 child_passes,
1156 arraysize(child_passes));
1158 RenderPass* child_root_pass = child_pass_list.at(0u);
1159 SharedQuadState* child_root_pass_sqs =
1160 child_root_pass->shared_quad_state_list.front();
1161 child_root_pass_sqs->quad_to_target_transform.Translate(8, 0);
1162 child_root_pass->damage_rect = gfx::Rect(10, 10, 10, 10);
1164 scoped_ptr<DelegatedFrameData> child_frame_data(new DelegatedFrameData);
1165 child_pass_list.swap(child_frame_data->render_pass_list);
1167 scoped_ptr<CompositorFrame> child_frame(new CompositorFrame);
1168 child_frame->delegated_frame_data = child_frame_data.Pass();
1170 factory_.SubmitFrame(child_surface_id, child_frame.Pass(),
1171 SurfaceFactory::DrawCallback());
1173 scoped_ptr<CompositorFrame> aggregated_frame =
1174 aggregator_.Aggregate(root_surface_id_);
1176 ASSERT_TRUE(aggregated_frame);
1177 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
1179 DelegatedFrameData* frame_data =
1180 aggregated_frame->delegated_frame_data.get();
1182 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
1184 ASSERT_EQ(2u, aggregated_pass_list.size());
1186 // Outer surface didn't change, so transformed inner damage rect should be
1187 // used.
1188 EXPECT_EQ(gfx::Rect(10, 20, 10, 10).ToString(),
1189 aggregated_pass_list[1]->damage_rect.ToString());
1193 RenderPassList root_pass_list;
1194 AddPasses(&root_pass_list,
1195 gfx::Rect(SurfaceSize()),
1196 root_passes,
1197 arraysize(root_passes));
1199 root_pass_list.at(0)
1200 ->shared_quad_state_list.front()
1201 ->quad_to_target_transform.Translate(0, 10);
1202 root_pass_list.at(0)->damage_rect = gfx::Rect(0, 0, 1, 1);
1204 scoped_ptr<DelegatedFrameData> root_frame_data(new DelegatedFrameData);
1205 root_pass_list.swap(root_frame_data->render_pass_list);
1207 scoped_ptr<CompositorFrame> root_frame(new CompositorFrame);
1208 root_frame->delegated_frame_data = root_frame_data.Pass();
1210 factory_.SubmitFrame(root_surface_id_, root_frame.Pass(),
1211 SurfaceFactory::DrawCallback());
1215 RenderPassList root_pass_list;
1216 AddPasses(&root_pass_list,
1217 gfx::Rect(SurfaceSize()),
1218 root_passes,
1219 arraysize(root_passes));
1221 root_pass_list.at(0)
1222 ->shared_quad_state_list.front()
1223 ->quad_to_target_transform.Translate(0, 10);
1224 root_pass_list.at(0)->damage_rect = gfx::Rect(1, 1, 1, 1);
1226 scoped_ptr<DelegatedFrameData> root_frame_data(new DelegatedFrameData);
1227 root_pass_list.swap(root_frame_data->render_pass_list);
1229 scoped_ptr<CompositorFrame> root_frame(new CompositorFrame);
1230 root_frame->delegated_frame_data = root_frame_data.Pass();
1232 factory_.SubmitFrame(root_surface_id_, root_frame.Pass(),
1233 SurfaceFactory::DrawCallback());
1235 scoped_ptr<CompositorFrame> aggregated_frame =
1236 aggregator_.Aggregate(root_surface_id_);
1238 ASSERT_TRUE(aggregated_frame);
1239 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
1241 DelegatedFrameData* frame_data =
1242 aggregated_frame->delegated_frame_data.get();
1244 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
1246 ASSERT_EQ(2u, aggregated_pass_list.size());
1248 // The root surface was enqueued without being aggregated once, so it should
1249 // be treated as completely damaged.
1250 EXPECT_TRUE(aggregated_pass_list[1]->damage_rect.Contains(
1251 gfx::Rect(SurfaceSize())));
1254 // No Surface changed, so no damage should be given.
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.IsEmpty());
1272 // SetFullDamageRectForSurface should cause the entire output to be
1273 // marked as damaged.
1275 aggregator_.SetFullDamageForSurface(root_surface_id_);
1276 scoped_ptr<CompositorFrame> aggregated_frame =
1277 aggregator_.Aggregate(root_surface_id_);
1279 ASSERT_TRUE(aggregated_frame);
1280 ASSERT_TRUE(aggregated_frame->delegated_frame_data);
1282 DelegatedFrameData* frame_data =
1283 aggregated_frame->delegated_frame_data.get();
1285 const RenderPassList& aggregated_pass_list = frame_data->render_pass_list;
1287 ASSERT_EQ(2u, aggregated_pass_list.size());
1289 EXPECT_TRUE(aggregated_pass_list[1]->damage_rect.Contains(
1290 gfx::Rect(SurfaceSize())));
1293 factory_.Destroy(child_surface_id);
1296 class SurfaceAggregatorWithResourcesTest : public testing::Test {
1297 public:
1298 void SetUp() override {
1299 output_surface_ = FakeOutputSurface::CreateSoftware(
1300 make_scoped_ptr(new SoftwareOutputDevice));
1301 output_surface_->BindToClient(&output_surface_client_);
1302 shared_bitmap_manager_.reset(new TestSharedBitmapManager);
1304 resource_provider_ = FakeResourceProvider::Create(
1305 output_surface_.get(), shared_bitmap_manager_.get());
1307 aggregator_.reset(
1308 new SurfaceAggregator(&manager_, resource_provider_.get()));
1311 protected:
1312 SurfaceManager manager_;
1313 FakeOutputSurfaceClient output_surface_client_;
1314 scoped_ptr<OutputSurface> output_surface_;
1315 scoped_ptr<SharedBitmapManager> shared_bitmap_manager_;
1316 scoped_ptr<ResourceProvider> resource_provider_;
1317 scoped_ptr<SurfaceAggregator> aggregator_;
1320 class ResourceTrackingSurfaceFactoryClient : public SurfaceFactoryClient {
1321 public:
1322 ResourceTrackingSurfaceFactoryClient() {}
1323 ~ResourceTrackingSurfaceFactoryClient() override {}
1325 void ReturnResources(const ReturnedResourceArray& resources) override {
1326 returned_resources_ = resources;
1329 ReturnedResourceArray returned_resources() const {
1330 return returned_resources_;
1333 private:
1334 ReturnedResourceArray returned_resources_;
1336 DISALLOW_COPY_AND_ASSIGN(ResourceTrackingSurfaceFactoryClient);
1339 void SubmitFrameWithResources(ResourceId* resource_ids,
1340 size_t num_resource_ids,
1341 bool valid,
1342 SurfaceId child_id,
1343 SurfaceFactory* factory,
1344 SurfaceId surface_id) {
1345 scoped_ptr<DelegatedFrameData> frame_data(new DelegatedFrameData);
1346 scoped_ptr<RenderPass> pass = RenderPass::Create();
1347 pass->id = RenderPassId(1, 1);
1348 SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState();
1349 sqs->opacity = 1.f;
1350 if (!child_id.is_null()) {
1351 SurfaceDrawQuad* surface_quad =
1352 pass->CreateAndAppendDrawQuad<SurfaceDrawQuad>();
1353 surface_quad->SetNew(sqs, gfx::Rect(0, 0, 1, 1), gfx::Rect(0, 0, 1, 1),
1354 child_id);
1357 for (size_t i = 0u; i < num_resource_ids; ++i) {
1358 TransferableResource resource;
1359 resource.id = resource_ids[i];
1360 // ResourceProvider is software, so only software resources are valid.
1361 resource.is_software = valid;
1362 frame_data->resource_list.push_back(resource);
1363 TextureDrawQuad* quad = pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
1364 const gfx::Rect rect;
1365 const gfx::Rect opaque_rect;
1366 const gfx::Rect visible_rect;
1367 bool needs_blending = false;
1368 bool premultiplied_alpha = false;
1369 const gfx::PointF uv_top_left;
1370 const gfx::PointF uv_bottom_right;
1371 SkColor background_color = SK_ColorGREEN;
1372 const float vertex_opacity[4] = {0.f, 0.f, 1.f, 1.f};
1373 bool flipped = false;
1374 bool nearest_neighbor = false;
1375 quad->SetAll(sqs, rect, opaque_rect, visible_rect, needs_blending,
1376 resource_ids[i], gfx::Size(), false, premultiplied_alpha,
1377 uv_top_left, uv_bottom_right, background_color, vertex_opacity,
1378 flipped, nearest_neighbor);
1380 frame_data->render_pass_list.push_back(pass.Pass());
1381 scoped_ptr<CompositorFrame> frame(new CompositorFrame);
1382 frame->delegated_frame_data = frame_data.Pass();
1383 factory->SubmitFrame(surface_id, frame.Pass(),
1384 SurfaceFactory::DrawCallback());
1387 TEST_F(SurfaceAggregatorWithResourcesTest, TakeResourcesOneSurface) {
1388 ResourceTrackingSurfaceFactoryClient client;
1389 SurfaceFactory factory(&manager_, &client);
1390 SurfaceId surface_id(7u);
1391 factory.Create(surface_id);
1393 ResourceId ids[] = {11, 12, 13};
1394 SubmitFrameWithResources(ids, arraysize(ids), true, SurfaceId(), &factory,
1395 surface_id);
1397 scoped_ptr<CompositorFrame> frame = aggregator_->Aggregate(surface_id);
1399 // Nothing should be available to be returned yet.
1400 EXPECT_TRUE(client.returned_resources().empty());
1402 SubmitFrameWithResources(NULL, 0u, true, SurfaceId(), &factory, surface_id);
1404 frame = aggregator_->Aggregate(surface_id);
1406 ASSERT_EQ(3u, client.returned_resources().size());
1407 ResourceId returned_ids[3];
1408 for (size_t i = 0; i < 3; ++i) {
1409 returned_ids[i] = client.returned_resources()[i].id;
1411 EXPECT_THAT(returned_ids,
1412 testing::WhenSorted(testing::ElementsAreArray(ids)));
1413 factory.Destroy(surface_id);
1416 TEST_F(SurfaceAggregatorWithResourcesTest, TakeInvalidResources) {
1417 ResourceTrackingSurfaceFactoryClient client;
1418 SurfaceFactory factory(&manager_, &client);
1419 SurfaceId surface_id(7u);
1420 factory.Create(surface_id);
1422 scoped_ptr<DelegatedFrameData> frame_data(new DelegatedFrameData);
1423 scoped_ptr<RenderPass> pass = RenderPass::Create();
1424 pass->id = RenderPassId(1, 1);
1425 TransferableResource resource;
1426 resource.id = 11;
1427 // ResourceProvider is software but resource is not, so it should be
1428 // ignored.
1429 resource.is_software = false;
1430 frame_data->resource_list.push_back(resource);
1431 frame_data->render_pass_list.push_back(pass.Pass());
1432 scoped_ptr<CompositorFrame> frame(new CompositorFrame);
1433 frame->delegated_frame_data = frame_data.Pass();
1434 factory.SubmitFrame(surface_id, frame.Pass(), SurfaceFactory::DrawCallback());
1436 scoped_ptr<CompositorFrame> returned_frame =
1437 aggregator_->Aggregate(surface_id);
1439 // Nothing should be available to be returned yet.
1440 EXPECT_TRUE(client.returned_resources().empty());
1442 SubmitFrameWithResources(NULL, 0, true, SurfaceId(), &factory, surface_id);
1443 ASSERT_EQ(1u, client.returned_resources().size());
1444 EXPECT_EQ(11u, client.returned_resources()[0].id);
1446 factory.Destroy(surface_id);
1449 TEST_F(SurfaceAggregatorWithResourcesTest, TwoSurfaces) {
1450 ResourceTrackingSurfaceFactoryClient client;
1451 SurfaceFactory factory(&manager_, &client);
1452 SurfaceId surface_id(7u);
1453 factory.Create(surface_id);
1454 SurfaceId surface_id2(8u);
1455 factory.Create(surface_id2);
1457 ResourceId ids[] = {11, 12, 13};
1458 SubmitFrameWithResources(ids, arraysize(ids), true, SurfaceId(), &factory,
1459 surface_id);
1460 ResourceId ids2[] = {14, 15, 16};
1461 SubmitFrameWithResources(ids2, arraysize(ids2), true, SurfaceId(), &factory,
1462 surface_id2);
1464 scoped_ptr<CompositorFrame> frame = aggregator_->Aggregate(surface_id);
1466 SubmitFrameWithResources(NULL, 0, true, SurfaceId(), &factory, surface_id);
1468 // Nothing should be available to be returned yet.
1469 EXPECT_TRUE(client.returned_resources().empty());
1471 frame = aggregator_->Aggregate(surface_id2);
1473 // surface_id wasn't referenced, so its resources should be returned.
1474 ASSERT_EQ(3u, client.returned_resources().size());
1475 ResourceId returned_ids[3];
1476 for (size_t i = 0; i < 3; ++i) {
1477 returned_ids[i] = client.returned_resources()[i].id;
1479 EXPECT_THAT(returned_ids,
1480 testing::WhenSorted(testing::ElementsAreArray(ids)));
1481 EXPECT_EQ(3u, resource_provider_->num_resources());
1482 factory.Destroy(surface_id);
1483 factory.Destroy(surface_id2);
1486 // Ensure that aggregator completely ignores Surfaces that reference invalid
1487 // resources.
1488 TEST_F(SurfaceAggregatorWithResourcesTest, InvalidChildSurface) {
1489 ResourceTrackingSurfaceFactoryClient client;
1490 SurfaceFactory factory(&manager_, &client);
1491 SurfaceId root_surface_id(7u);
1492 factory.Create(root_surface_id);
1493 SurfaceId middle_surface_id(8u);
1494 factory.Create(middle_surface_id);
1495 SurfaceId child_surface_id(9u);
1496 factory.Create(child_surface_id);
1498 ResourceId ids[] = {14, 15, 16};
1499 SubmitFrameWithResources(ids, arraysize(ids), true, SurfaceId(), &factory,
1500 child_surface_id);
1502 ResourceId ids2[] = {17, 18, 19};
1503 SubmitFrameWithResources(ids2, arraysize(ids2), false, child_surface_id,
1504 &factory, middle_surface_id);
1506 ResourceId ids3[] = {20, 21, 22};
1507 SubmitFrameWithResources(ids3, arraysize(ids3), true, middle_surface_id,
1508 &factory, root_surface_id);
1510 scoped_ptr<CompositorFrame> frame;
1511 frame = aggregator_->Aggregate(root_surface_id);
1513 RenderPassList* pass_list = &frame->delegated_frame_data->render_pass_list;
1514 ASSERT_EQ(1u, pass_list->size());
1515 EXPECT_EQ(1u, pass_list->back()->shared_quad_state_list.size());
1516 EXPECT_EQ(3u, pass_list->back()->quad_list.size());
1518 SubmitFrameWithResources(ids2, arraysize(ids), true, child_surface_id,
1519 &factory, middle_surface_id);
1521 frame = aggregator_->Aggregate(root_surface_id);
1523 pass_list = &frame->delegated_frame_data->render_pass_list;
1524 ASSERT_EQ(1u, pass_list->size());
1525 EXPECT_EQ(3u, pass_list->back()->shared_quad_state_list.size());
1526 EXPECT_EQ(9u, pass_list->back()->quad_list.size());
1528 factory.Destroy(root_surface_id);
1529 factory.Destroy(child_surface_id);
1530 factory.Destroy(middle_surface_id);
1533 } // namespace
1534 } // namespace cc