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::RectF 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 scoped_ptr
<SharedQuadState
> shared_state
= SharedQuadState::Create();
83 shared_state
->SetAll(gfx::Transform(),
89 SkXfermode::kSrcOver_Mode
);
90 pass
->AppendSharedQuadState(shared_state
.Pass());
92 scoped_ptr
<CheckerboardDrawQuad
> checkerboard_quad
=
93 CheckerboardDrawQuad::Create();
94 checkerboard_quad
->SetNew(
95 pass
->shared_quad_state_list
.back(), gfx::Rect(), SkColor());
96 pass
->quad_list
.push_back(checkerboard_quad
.PassAs
<DrawQuad
>());
98 RenderPass::Id
new_id(63, 4);
100 scoped_ptr
<RenderPass
> copy
= pass
->Copy(new_id
);
101 EXPECT_EQ(new_id
, copy
->id
);
102 EXPECT_RECT_EQ(pass
->output_rect
, copy
->output_rect
);
103 EXPECT_EQ(pass
->transform_to_root_target
, copy
->transform_to_root_target
);
104 EXPECT_RECT_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 RenderPass::Id
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 scoped_ptr
<SharedQuadState
> shared_state1
= SharedQuadState::Create();
134 shared_state1
->SetAll(gfx::Transform(),
140 SkXfermode::kSrcOver_Mode
);
141 pass
->AppendSharedQuadState(shared_state1
.Pass());
143 scoped_ptr
<CheckerboardDrawQuad
> checkerboard_quad1
=
144 CheckerboardDrawQuad::Create();
145 checkerboard_quad1
->SetNew(
146 pass
->shared_quad_state_list
.back(), gfx::Rect(1, 1, 1, 1), SkColor());
147 pass
->quad_list
.push_back(checkerboard_quad1
.PassAs
<DrawQuad
>());
149 scoped_ptr
<CheckerboardDrawQuad
> checkerboard_quad2
=
150 CheckerboardDrawQuad::Create();
151 checkerboard_quad2
->SetNew(
152 pass
->shared_quad_state_list
.back(), gfx::Rect(2, 2, 2, 2), SkColor());
153 pass
->quad_list
.push_back(checkerboard_quad2
.PassAs
<DrawQuad
>());
155 // And two quads using another shared state.
156 scoped_ptr
<SharedQuadState
> shared_state2
= SharedQuadState::Create();
157 shared_state2
->SetAll(gfx::Transform(),
163 SkXfermode::kSrcOver_Mode
);
164 pass
->AppendSharedQuadState(shared_state2
.Pass());
166 scoped_ptr
<CheckerboardDrawQuad
> checkerboard_quad3
=
167 CheckerboardDrawQuad::Create();
168 checkerboard_quad3
->SetNew(
169 pass
->shared_quad_state_list
.back(), gfx::Rect(3, 3, 3, 3), SkColor());
170 pass
->quad_list
.push_back(checkerboard_quad3
.PassAs
<DrawQuad
>());
172 scoped_ptr
<CheckerboardDrawQuad
> checkerboard_quad4
=
173 CheckerboardDrawQuad::Create();
174 checkerboard_quad4
->SetNew(
175 pass
->shared_quad_state_list
.back(), gfx::Rect(4, 4, 4, 4), SkColor());
176 pass
->quad_list
.push_back(checkerboard_quad4
.PassAs
<DrawQuad
>());
178 // A second render pass with a quad.
179 RenderPass::Id
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 scoped_ptr
<SharedQuadState
> contrib_shared_state
= SharedQuadState::Create();
194 contrib_shared_state
->SetAll(gfx::Transform(),
200 SkXfermode::kSrcOver_Mode
);
201 contrib
->AppendSharedQuadState(contrib_shared_state
.Pass());
203 scoped_ptr
<CheckerboardDrawQuad
> contrib_quad
=
204 CheckerboardDrawQuad::Create();
205 contrib_quad
->SetNew(
206 contrib
->shared_quad_state_list
.back(), gfx::Rect(3, 3, 3, 3), SkColor());
207 contrib
->quad_list
.push_back(contrib_quad
.PassAs
<DrawQuad
>());
209 // And a RenderPassDrawQuad for the contributing pass.
210 scoped_ptr
<RenderPassDrawQuad
> pass_quad
= RenderPassDrawQuad::Create();
211 pass_quad
->SetNew(pass
->shared_quad_state_list
.back(),
215 0, // mask_resource_id
217 gfx::RectF(), // mask_uv_rect
220 pass
->quad_list
.push_back(pass_quad
.PassAs
<DrawQuad
>());
222 pass_list
.push_back(pass
.PassAs
<RenderPass
>());
223 pass_list
.push_back(contrib
.PassAs
<RenderPass
>());
225 // Make a copy with CopyAll().
226 RenderPassList copy_list
;
227 RenderPass::CopyAll(pass_list
, ©_list
);
229 CompareRenderPassLists(pass_list
, copy_list
);
232 TEST(RenderPassTest
, CopyAllWithCulledQuads
) {
233 RenderPassList pass_list
;
235 RenderPass::Id
id(3, 2);
236 gfx::Rect
output_rect(45, 22, 120, 13);
237 gfx::Transform transform_to_root
=
238 gfx::Transform(1.0, 0.5, 0.5, -0.5, -1.0, 0.0);
239 gfx::Rect
damage_rect(56, 123, 19, 43);
240 bool has_transparent_background
= true;
242 scoped_ptr
<TestRenderPass
> pass
= TestRenderPass::Create();
247 has_transparent_background
);
249 // A shared state with a quad.
250 scoped_ptr
<SharedQuadState
> shared_state1
= SharedQuadState::Create();
251 shared_state1
->SetAll(gfx::Transform(),
257 SkXfermode::kSrcOver_Mode
);
258 pass
->AppendSharedQuadState(shared_state1
.Pass());
260 scoped_ptr
<CheckerboardDrawQuad
> checkerboard_quad1
=
261 CheckerboardDrawQuad::Create();
262 checkerboard_quad1
->SetNew(
263 pass
->shared_quad_state_list
.back(), gfx::Rect(1, 1, 1, 1), SkColor());
264 pass
->quad_list
.push_back(checkerboard_quad1
.PassAs
<DrawQuad
>());
266 // A shared state with no quads, they were culled.
267 scoped_ptr
<SharedQuadState
> shared_state2
= SharedQuadState::Create();
268 shared_state2
->SetAll(gfx::Transform(),
274 SkXfermode::kSrcOver_Mode
);
275 pass
->AppendSharedQuadState(shared_state2
.Pass());
277 // A second shared state with no quads.
278 scoped_ptr
<SharedQuadState
> shared_state3
= SharedQuadState::Create();
279 shared_state3
->SetAll(gfx::Transform(),
285 SkXfermode::kSrcOver_Mode
);
286 pass
->AppendSharedQuadState(shared_state3
.Pass());
288 // A last shared state with a quad again.
289 scoped_ptr
<SharedQuadState
> shared_state4
= SharedQuadState::Create();
290 shared_state4
->SetAll(gfx::Transform(),
296 SkXfermode::kSrcOver_Mode
);
297 pass
->AppendSharedQuadState(shared_state4
.Pass());
299 scoped_ptr
<CheckerboardDrawQuad
> checkerboard_quad2
=
300 CheckerboardDrawQuad::Create();
301 checkerboard_quad2
->SetNew(
302 pass
->shared_quad_state_list
.back(), gfx::Rect(3, 3, 3, 3), SkColor());
303 pass
->quad_list
.push_back(checkerboard_quad2
.PassAs
<DrawQuad
>());
305 pass_list
.push_back(pass
.PassAs
<RenderPass
>());
307 // Make a copy with CopyAll().
308 RenderPassList copy_list
;
309 RenderPass::CopyAll(pass_list
, ©_list
);
311 CompareRenderPassLists(pass_list
, copy_list
);