1 // Copyright 2012 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/quads/render_pass.h"
7 #include "cc/base/math_util.h"
8 #include "cc/base/scoped_ptr_vector.h"
9 #include "cc/output/copy_output_request.h"
10 #include "cc/quads/checkerboard_draw_quad.h"
11 #include "cc/quads/render_pass_draw_quad.h"
12 #include "cc/test/geometry_test_utils.h"
13 #include "cc/test/render_pass_test_common.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/skia/include/effects/SkBlurImageFilter.h"
16 #include "ui/gfx/transform.h"
18 using cc::TestRenderPass
;
23 struct RenderPassSize
{
24 // If you add a new field to this class, make sure to add it to the
28 SharedQuadStateList shared_quad_state_list
;
29 gfx::Transform transform_to_root_target
;
30 gfx::Rect output_rect
;
31 gfx::Rect damage_rect
;
32 bool has_transparent_background
;
33 ScopedPtrVector
<CopyOutputRequest
> copy_callbacks
;
36 static void CompareRenderPassLists(const RenderPassList
& expected_list
,
37 const RenderPassList
& actual_list
) {
38 EXPECT_EQ(expected_list
.size(), actual_list
.size());
39 for (size_t i
= 0; i
< actual_list
.size(); ++i
) {
40 RenderPass
* expected
= expected_list
[i
];
41 RenderPass
* actual
= actual_list
[i
];
43 EXPECT_EQ(expected
->id
, actual
->id
);
44 EXPECT_RECT_EQ(expected
->output_rect
, actual
->output_rect
);
45 EXPECT_EQ(expected
->transform_to_root_target
,
46 actual
->transform_to_root_target
);
47 EXPECT_RECT_EQ(expected
->damage_rect
, actual
->damage_rect
);
48 EXPECT_EQ(expected
->has_transparent_background
,
49 actual
->has_transparent_background
);
51 EXPECT_EQ(expected
->shared_quad_state_list
.size(),
52 actual
->shared_quad_state_list
.size());
53 EXPECT_EQ(expected
->quad_list
.size(), actual
->quad_list
.size());
55 for (size_t i
= 0; i
< expected
->quad_list
.size(); ++i
) {
56 EXPECT_EQ(expected
->quad_list
[i
]->rect
.ToString(),
57 actual
->quad_list
[i
]->rect
.ToString());
59 expected
->quad_list
[i
]->shared_quad_state
->content_bounds
.ToString(),
60 actual
->quad_list
[i
]->shared_quad_state
->content_bounds
.ToString());
65 TEST(RenderPassTest
, CopyShouldBeIdenticalExceptIdAndQuads
) {
66 RenderPass::Id
id(3, 2);
67 gfx::Rect
output_rect(45, 22, 120, 13);
68 gfx::Transform transform_to_root
=
69 gfx::Transform(1.0, 0.5, 0.5, -0.5, -1.0, 0.0);
70 gfx::Rect
damage_rect(56, 123, 19, 43);
71 bool has_transparent_background
= true;
73 scoped_ptr
<TestRenderPass
> pass
= TestRenderPass::Create();
78 has_transparent_background
);
79 pass
->copy_requests
.push_back(CopyOutputRequest::CreateEmptyRequest());
81 // Stick a quad in the pass, this should not get copied.
82 SharedQuadState
* shared_state
= pass
->CreateAndAppendSharedQuadState();
83 shared_state
->SetAll(gfx::Transform(),
89 SkXfermode::kSrcOver_Mode
,
92 CheckerboardDrawQuad
* checkerboard_quad
=
93 pass
->CreateAndAppendDrawQuad
<CheckerboardDrawQuad
>();
94 checkerboard_quad
->SetNew(
95 pass
->shared_quad_state_list
.back(), gfx::Rect(), gfx::Rect(), SkColor());
97 RenderPass::Id
new_id(63, 4);
99 scoped_ptr
<RenderPass
> copy
= pass
->Copy(new_id
);
100 EXPECT_EQ(new_id
, copy
->id
);
101 EXPECT_RECT_EQ(pass
->output_rect
, copy
->output_rect
);
102 EXPECT_EQ(pass
->transform_to_root_target
, copy
->transform_to_root_target
);
103 EXPECT_RECT_EQ(pass
->damage_rect
, copy
->damage_rect
);
104 EXPECT_EQ(pass
->has_transparent_background
, copy
->has_transparent_background
);
105 EXPECT_EQ(0u, copy
->quad_list
.size());
107 // The copy request should not be copied/duplicated.
108 EXPECT_EQ(1u, pass
->copy_requests
.size());
109 EXPECT_EQ(0u, copy
->copy_requests
.size());
111 EXPECT_EQ(sizeof(RenderPassSize
), sizeof(RenderPass
));
114 TEST(RenderPassTest
, CopyAllShouldBeIdentical
) {
115 RenderPassList pass_list
;
117 RenderPass::Id
id(3, 2);
118 gfx::Rect
output_rect(45, 22, 120, 13);
119 gfx::Transform transform_to_root
=
120 gfx::Transform(1.0, 0.5, 0.5, -0.5, -1.0, 0.0);
121 gfx::Rect
damage_rect(56, 123, 19, 43);
122 bool has_transparent_background
= true;
124 scoped_ptr
<TestRenderPass
> pass
= TestRenderPass::Create();
129 has_transparent_background
);
131 // Two quads using one shared state.
132 SharedQuadState
* shared_state1
= pass
->CreateAndAppendSharedQuadState();
133 shared_state1
->SetAll(gfx::Transform(),
139 SkXfermode::kSrcOver_Mode
,
142 CheckerboardDrawQuad
* checkerboard_quad1
=
143 pass
->CreateAndAppendDrawQuad
<CheckerboardDrawQuad
>();
144 checkerboard_quad1
->SetNew(pass
->shared_quad_state_list
.back(),
145 gfx::Rect(1, 1, 1, 1),
146 gfx::Rect(1, 1, 1, 1),
149 CheckerboardDrawQuad
* checkerboard_quad2
=
150 pass
->CreateAndAppendDrawQuad
<CheckerboardDrawQuad
>();
151 checkerboard_quad2
->SetNew(pass
->shared_quad_state_list
.back(),
152 gfx::Rect(2, 2, 2, 2),
153 gfx::Rect(2, 2, 2, 2),
156 // And two quads using another shared state.
157 SharedQuadState
* shared_state2
= pass
->CreateAndAppendSharedQuadState();
158 shared_state2
->SetAll(gfx::Transform(),
164 SkXfermode::kSrcOver_Mode
,
167 CheckerboardDrawQuad
* checkerboard_quad3
=
168 pass
->CreateAndAppendDrawQuad
<CheckerboardDrawQuad
>();
169 checkerboard_quad3
->SetNew(pass
->shared_quad_state_list
.back(),
170 gfx::Rect(3, 3, 3, 3),
171 gfx::Rect(3, 3, 3, 3),
174 CheckerboardDrawQuad
* checkerboard_quad4
=
175 pass
->CreateAndAppendDrawQuad
<CheckerboardDrawQuad
>();
176 checkerboard_quad4
->SetNew(pass
->shared_quad_state_list
.back(),
177 gfx::Rect(4, 4, 4, 4),
178 gfx::Rect(4, 4, 4, 4),
181 // A second render pass with a quad.
182 RenderPass::Id
contrib_id(4, 1);
183 gfx::Rect
contrib_output_rect(10, 15, 12, 17);
184 gfx::Transform contrib_transform_to_root
=
185 gfx::Transform(1.0, 0.5, 0.5, -0.5, -1.0, 0.0);
186 gfx::Rect
contrib_damage_rect(11, 16, 10, 15);
187 bool contrib_has_transparent_background
= true;
189 scoped_ptr
<TestRenderPass
> contrib
= TestRenderPass::Create();
190 contrib
->SetAll(contrib_id
,
193 contrib_transform_to_root
,
194 contrib_has_transparent_background
);
196 SharedQuadState
* contrib_shared_state
=
197 contrib
->CreateAndAppendSharedQuadState();
198 contrib_shared_state
->SetAll(gfx::Transform(),
204 SkXfermode::kSrcOver_Mode
,
207 CheckerboardDrawQuad
* contrib_quad
=
208 contrib
->CreateAndAppendDrawQuad
<CheckerboardDrawQuad
>();
209 contrib_quad
->SetNew(contrib
->shared_quad_state_list
.back(),
210 gfx::Rect(3, 3, 3, 3),
211 gfx::Rect(3, 3, 3, 3),
214 // And a RenderPassDrawQuad for the contributing pass.
215 scoped_ptr
<RenderPassDrawQuad
> pass_quad
= RenderPassDrawQuad::Create();
216 pass_quad
->SetNew(pass
->shared_quad_state_list
.back(),
221 0, // mask_resource_id
223 gfx::RectF(), // mask_uv_rect
227 pass_list
.push_back(pass
.PassAs
<RenderPass
>());
228 pass_list
.push_back(contrib
.PassAs
<RenderPass
>());
230 // Make a copy with CopyAll().
231 RenderPassList copy_list
;
232 RenderPass::CopyAll(pass_list
, ©_list
);
234 CompareRenderPassLists(pass_list
, copy_list
);
237 TEST(RenderPassTest
, CopyAllWithCulledQuads
) {
238 RenderPassList pass_list
;
240 RenderPass::Id
id(3, 2);
241 gfx::Rect
output_rect(45, 22, 120, 13);
242 gfx::Transform transform_to_root
=
243 gfx::Transform(1.0, 0.5, 0.5, -0.5, -1.0, 0.0);
244 gfx::Rect
damage_rect(56, 123, 19, 43);
245 bool has_transparent_background
= true;
247 scoped_ptr
<TestRenderPass
> pass
= TestRenderPass::Create();
252 has_transparent_background
);
254 // A shared state with a quad.
255 SharedQuadState
* shared_state1
= pass
->CreateAndAppendSharedQuadState();
256 shared_state1
->SetAll(gfx::Transform(),
262 SkXfermode::kSrcOver_Mode
,
265 CheckerboardDrawQuad
* checkerboard_quad1
=
266 pass
->CreateAndAppendDrawQuad
<CheckerboardDrawQuad
>();
267 checkerboard_quad1
->SetNew(pass
->shared_quad_state_list
.back(),
268 gfx::Rect(1, 1, 1, 1),
269 gfx::Rect(1, 1, 1, 1),
272 // A shared state with no quads, they were culled.
273 SharedQuadState
* shared_state2
= pass
->CreateAndAppendSharedQuadState();
274 shared_state2
->SetAll(gfx::Transform(),
280 SkXfermode::kSrcOver_Mode
,
283 // A second shared state with no quads.
284 SharedQuadState
* shared_state3
= pass
->CreateAndAppendSharedQuadState();
285 shared_state3
->SetAll(gfx::Transform(),
291 SkXfermode::kSrcOver_Mode
,
294 // A last shared state with a quad again.
295 SharedQuadState
* shared_state4
= pass
->CreateAndAppendSharedQuadState();
296 shared_state4
->SetAll(gfx::Transform(),
302 SkXfermode::kSrcOver_Mode
,
305 CheckerboardDrawQuad
* checkerboard_quad2
=
306 pass
->CreateAndAppendDrawQuad
<CheckerboardDrawQuad
>();
307 checkerboard_quad2
->SetNew(pass
->shared_quad_state_list
.back(),
308 gfx::Rect(3, 3, 3, 3),
309 gfx::Rect(3, 3, 3, 3),
312 pass_list
.push_back(pass
.PassAs
<RenderPass
>());
314 // Make a copy with CopyAll().
315 RenderPassList copy_list
;
316 RenderPass::CopyAll(pass_list
, ©_list
);
318 CompareRenderPassLists(pass_list
, copy_list
);