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_EQ(expected
->output_rect
, actual
->output_rect
);
45 EXPECT_EQ(expected
->transform_to_root_target
,
46 actual
->transform_to_root_target
);
47 EXPECT_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 (auto exp_iter
= expected
->quad_list
.cbegin(),
56 act_iter
= actual
->quad_list
.cbegin();
57 exp_iter
!= expected
->quad_list
.cend();
58 ++exp_iter
, ++act_iter
) {
59 EXPECT_EQ(exp_iter
->rect
.ToString(), act_iter
->rect
.ToString());
60 EXPECT_EQ(exp_iter
->shared_quad_state
->content_bounds
.ToString(),
61 act_iter
->shared_quad_state
->content_bounds
.ToString());
66 TEST(RenderPassTest
, CopyShouldBeIdenticalExceptIdAndQuads
) {
67 RenderPassId
id(3, 2);
68 gfx::Rect
output_rect(45, 22, 120, 13);
69 gfx::Transform transform_to_root
=
70 gfx::Transform(1.0, 0.5, 0.5, -0.5, -1.0, 0.0);
71 gfx::Rect
damage_rect(56, 123, 19, 43);
72 bool has_transparent_background
= true;
74 scoped_ptr
<TestRenderPass
> pass
= TestRenderPass::Create();
79 has_transparent_background
);
80 pass
->copy_requests
.push_back(CopyOutputRequest::CreateEmptyRequest());
82 // Stick a quad in the pass, this should not get copied.
83 SharedQuadState
* shared_state
= pass
->CreateAndAppendSharedQuadState();
84 shared_state
->SetAll(gfx::Transform(),
90 SkXfermode::kSrcOver_Mode
,
93 CheckerboardDrawQuad
* checkerboard_quad
=
94 pass
->CreateAndAppendDrawQuad
<CheckerboardDrawQuad
>();
95 checkerboard_quad
->SetNew(pass
->shared_quad_state_list
.back(), gfx::Rect(),
96 gfx::Rect(), SkColor(), 1.f
);
98 RenderPassId
new_id(63, 4);
100 scoped_ptr
<RenderPass
> copy
= pass
->Copy(new_id
);
101 EXPECT_EQ(new_id
, copy
->id
);
102 EXPECT_EQ(pass
->output_rect
, copy
->output_rect
);
103 EXPECT_EQ(pass
->transform_to_root_target
, copy
->transform_to_root_target
);
104 EXPECT_EQ(pass
->damage_rect
, copy
->damage_rect
);
105 EXPECT_EQ(pass
->has_transparent_background
, copy
->has_transparent_background
);
106 EXPECT_EQ(0u, copy
->quad_list
.size());
108 // The copy request should not be copied/duplicated.
109 EXPECT_EQ(1u, pass
->copy_requests
.size());
110 EXPECT_EQ(0u, copy
->copy_requests
.size());
112 EXPECT_EQ(sizeof(RenderPassSize
), sizeof(RenderPass
));
115 TEST(RenderPassTest
, CopyAllShouldBeIdentical
) {
116 RenderPassList pass_list
;
118 RenderPassId
id(3, 2);
119 gfx::Rect
output_rect(45, 22, 120, 13);
120 gfx::Transform transform_to_root
=
121 gfx::Transform(1.0, 0.5, 0.5, -0.5, -1.0, 0.0);
122 gfx::Rect
damage_rect(56, 123, 19, 43);
123 bool has_transparent_background
= true;
125 scoped_ptr
<TestRenderPass
> pass
= TestRenderPass::Create();
130 has_transparent_background
);
132 // Two quads using one shared state.
133 SharedQuadState
* shared_state1
= pass
->CreateAndAppendSharedQuadState();
134 shared_state1
->SetAll(gfx::Transform(),
140 SkXfermode::kSrcOver_Mode
,
143 CheckerboardDrawQuad
* checkerboard_quad1
=
144 pass
->CreateAndAppendDrawQuad
<CheckerboardDrawQuad
>();
145 checkerboard_quad1
->SetNew(pass
->shared_quad_state_list
.back(),
146 gfx::Rect(1, 1, 1, 1), 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), gfx::Rect(2, 2, 2, 2),
155 // And two quads using another shared state.
156 SharedQuadState
* shared_state2
= pass
->CreateAndAppendSharedQuadState();
157 shared_state2
->SetAll(gfx::Transform(),
163 SkXfermode::kSrcOver_Mode
,
166 CheckerboardDrawQuad
* checkerboard_quad3
=
167 pass
->CreateAndAppendDrawQuad
<CheckerboardDrawQuad
>();
168 checkerboard_quad3
->SetNew(pass
->shared_quad_state_list
.back(),
169 gfx::Rect(3, 3, 3, 3), gfx::Rect(3, 3, 3, 3),
172 CheckerboardDrawQuad
* checkerboard_quad4
=
173 pass
->CreateAndAppendDrawQuad
<CheckerboardDrawQuad
>();
174 checkerboard_quad4
->SetNew(pass
->shared_quad_state_list
.back(),
175 gfx::Rect(4, 4, 4, 4), gfx::Rect(4, 4, 4, 4),
178 // A second render pass with a quad.
179 RenderPassId
contrib_id(4, 1);
180 gfx::Rect
contrib_output_rect(10, 15, 12, 17);
181 gfx::Transform contrib_transform_to_root
=
182 gfx::Transform(1.0, 0.5, 0.5, -0.5, -1.0, 0.0);
183 gfx::Rect
contrib_damage_rect(11, 16, 10, 15);
184 bool contrib_has_transparent_background
= true;
186 scoped_ptr
<TestRenderPass
> contrib
= TestRenderPass::Create();
187 contrib
->SetAll(contrib_id
,
190 contrib_transform_to_root
,
191 contrib_has_transparent_background
);
193 SharedQuadState
* contrib_shared_state
=
194 contrib
->CreateAndAppendSharedQuadState();
195 contrib_shared_state
->SetAll(gfx::Transform(),
201 SkXfermode::kSrcOver_Mode
,
204 CheckerboardDrawQuad
* contrib_quad
=
205 contrib
->CreateAndAppendDrawQuad
<CheckerboardDrawQuad
>();
206 contrib_quad
->SetNew(contrib
->shared_quad_state_list
.back(),
207 gfx::Rect(3, 3, 3, 3), gfx::Rect(3, 3, 3, 3), SkColor(),
210 // And a RenderPassDrawQuad for the contributing pass.
211 scoped_ptr
<RenderPassDrawQuad
> pass_quad
=
212 make_scoped_ptr(new RenderPassDrawQuad
);
213 pass_quad
->SetNew(pass
->shared_quad_state_list
.back(),
224 pass_list
.push_back(pass
.Pass());
225 pass_list
.push_back(contrib
.Pass());
227 // Make a copy with CopyAll().
228 RenderPassList copy_list
;
229 RenderPass::CopyAll(pass_list
, ©_list
);
231 CompareRenderPassLists(pass_list
, copy_list
);
234 TEST(RenderPassTest
, CopyAllWithCulledQuads
) {
235 RenderPassList pass_list
;
237 RenderPassId
id(3, 2);
238 gfx::Rect
output_rect(45, 22, 120, 13);
239 gfx::Transform transform_to_root
=
240 gfx::Transform(1.0, 0.5, 0.5, -0.5, -1.0, 0.0);
241 gfx::Rect
damage_rect(56, 123, 19, 43);
242 bool has_transparent_background
= true;
244 scoped_ptr
<TestRenderPass
> pass
= TestRenderPass::Create();
249 has_transparent_background
);
251 // A shared state with a quad.
252 SharedQuadState
* shared_state1
= pass
->CreateAndAppendSharedQuadState();
253 shared_state1
->SetAll(gfx::Transform(),
259 SkXfermode::kSrcOver_Mode
,
262 CheckerboardDrawQuad
* checkerboard_quad1
=
263 pass
->CreateAndAppendDrawQuad
<CheckerboardDrawQuad
>();
264 checkerboard_quad1
->SetNew(pass
->shared_quad_state_list
.back(),
265 gfx::Rect(1, 1, 1, 1), gfx::Rect(1, 1, 1, 1),
268 // A shared state with no quads, they were culled.
269 SharedQuadState
* shared_state2
= pass
->CreateAndAppendSharedQuadState();
270 shared_state2
->SetAll(gfx::Transform(),
276 SkXfermode::kSrcOver_Mode
,
279 // A second shared state with no quads.
280 SharedQuadState
* shared_state3
= pass
->CreateAndAppendSharedQuadState();
281 shared_state3
->SetAll(gfx::Transform(),
287 SkXfermode::kSrcOver_Mode
,
290 // A last shared state with a quad again.
291 SharedQuadState
* shared_state4
= pass
->CreateAndAppendSharedQuadState();
292 shared_state4
->SetAll(gfx::Transform(),
298 SkXfermode::kSrcOver_Mode
,
301 CheckerboardDrawQuad
* checkerboard_quad2
=
302 pass
->CreateAndAppendDrawQuad
<CheckerboardDrawQuad
>();
303 checkerboard_quad2
->SetNew(pass
->shared_quad_state_list
.back(),
304 gfx::Rect(3, 3, 3, 3), gfx::Rect(3, 3, 3, 3),
307 pass_list
.push_back(pass
.Pass());
309 // Make a copy with CopyAll().
310 RenderPassList copy_list
;
311 RenderPass::CopyAll(pass_list
, ©_list
);
313 CompareRenderPassLists(pass_list
, copy_list
);