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/render_pass_draw_quad.h"
11 #include "cc/quads/solid_color_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 std::vector
<SurfaceId
> referenced_surfaces
;
34 ScopedPtrVector
<CopyOutputRequest
> copy_callbacks
;
37 static void CompareRenderPassLists(const RenderPassList
& expected_list
,
38 const RenderPassList
& actual_list
) {
39 EXPECT_EQ(expected_list
.size(), actual_list
.size());
40 for (size_t i
= 0; i
< actual_list
.size(); ++i
) {
41 RenderPass
* expected
= expected_list
[i
];
42 RenderPass
* actual
= actual_list
[i
];
44 EXPECT_EQ(expected
->id
, actual
->id
);
45 EXPECT_EQ(expected
->output_rect
, actual
->output_rect
);
46 EXPECT_EQ(expected
->transform_to_root_target
,
47 actual
->transform_to_root_target
);
48 EXPECT_EQ(expected
->damage_rect
, actual
->damage_rect
);
49 EXPECT_EQ(expected
->has_transparent_background
,
50 actual
->has_transparent_background
);
52 EXPECT_EQ(expected
->shared_quad_state_list
.size(),
53 actual
->shared_quad_state_list
.size());
54 EXPECT_EQ(expected
->quad_list
.size(), actual
->quad_list
.size());
55 EXPECT_EQ(expected
->referenced_surfaces
, actual
->referenced_surfaces
);
57 for (auto exp_iter
= expected
->quad_list
.cbegin(),
58 act_iter
= actual
->quad_list
.cbegin();
59 exp_iter
!= expected
->quad_list
.cend();
60 ++exp_iter
, ++act_iter
) {
61 EXPECT_EQ(exp_iter
->rect
.ToString(), act_iter
->rect
.ToString());
62 EXPECT_EQ(exp_iter
->shared_quad_state
->quad_layer_bounds
.ToString(),
63 act_iter
->shared_quad_state
->quad_layer_bounds
.ToString());
68 TEST(RenderPassTest
, CopyShouldBeIdenticalExceptIdAndQuads
) {
69 RenderPassId
id(3, 2);
70 gfx::Rect
output_rect(45, 22, 120, 13);
71 gfx::Transform transform_to_root
=
72 gfx::Transform(1.0, 0.5, 0.5, -0.5, -1.0, 0.0);
73 gfx::Rect
damage_rect(56, 123, 19, 43);
74 bool has_transparent_background
= true;
76 scoped_ptr
<TestRenderPass
> pass
= TestRenderPass::Create();
81 has_transparent_background
);
82 pass
->copy_requests
.push_back(CopyOutputRequest::CreateEmptyRequest());
84 // Stick a quad in the pass, this should not get copied.
85 SharedQuadState
* shared_state
= pass
->CreateAndAppendSharedQuadState();
86 shared_state
->SetAll(gfx::Transform(),
92 SkXfermode::kSrcOver_Mode
,
95 SolidColorDrawQuad
* color_quad
=
96 pass
->CreateAndAppendDrawQuad
<SolidColorDrawQuad
>();
97 color_quad
->SetNew(pass
->shared_quad_state_list
.back(), gfx::Rect(),
98 gfx::Rect(), SkColor(), false);
100 RenderPassId
new_id(63, 4);
102 scoped_ptr
<RenderPass
> copy
= pass
->Copy(new_id
);
103 EXPECT_EQ(new_id
, copy
->id
);
104 EXPECT_EQ(pass
->output_rect
, copy
->output_rect
);
105 EXPECT_EQ(pass
->transform_to_root_target
, copy
->transform_to_root_target
);
106 EXPECT_EQ(pass
->damage_rect
, copy
->damage_rect
);
107 EXPECT_EQ(pass
->has_transparent_background
, copy
->has_transparent_background
);
108 EXPECT_EQ(0u, copy
->quad_list
.size());
109 EXPECT_EQ(0u, copy
->referenced_surfaces
.size());
111 // The copy request should not be copied/duplicated.
112 EXPECT_EQ(1u, pass
->copy_requests
.size());
113 EXPECT_EQ(0u, copy
->copy_requests
.size());
115 EXPECT_EQ(sizeof(RenderPassSize
), sizeof(RenderPass
));
118 TEST(RenderPassTest
, CopyAllShouldBeIdentical
) {
119 RenderPassList pass_list
;
121 RenderPassId
id(3, 2);
122 gfx::Rect
output_rect(45, 22, 120, 13);
123 gfx::Transform transform_to_root
=
124 gfx::Transform(1.0, 0.5, 0.5, -0.5, -1.0, 0.0);
125 gfx::Rect
damage_rect(56, 123, 19, 43);
126 bool has_transparent_background
= true;
128 scoped_ptr
<TestRenderPass
> pass
= TestRenderPass::Create();
133 has_transparent_background
);
135 // Two quads using one shared state.
136 SharedQuadState
* shared_state1
= pass
->CreateAndAppendSharedQuadState();
137 shared_state1
->SetAll(gfx::Transform(),
143 SkXfermode::kSrcOver_Mode
,
146 SolidColorDrawQuad
* color_quad1
=
147 pass
->CreateAndAppendDrawQuad
<SolidColorDrawQuad
>();
148 color_quad1
->SetNew(pass
->shared_quad_state_list
.back(),
149 gfx::Rect(1, 1, 1, 1), gfx::Rect(1, 1, 1, 1), SkColor(),
152 SolidColorDrawQuad
* color_quad2
=
153 pass
->CreateAndAppendDrawQuad
<SolidColorDrawQuad
>();
154 color_quad2
->SetNew(pass
->shared_quad_state_list
.back(),
155 gfx::Rect(2, 2, 2, 2), gfx::Rect(2, 2, 2, 2), SkColor(),
158 // And two quads using another shared state.
159 SharedQuadState
* shared_state2
= pass
->CreateAndAppendSharedQuadState();
160 shared_state2
->SetAll(gfx::Transform(),
166 SkXfermode::kSrcOver_Mode
,
169 SolidColorDrawQuad
* color_quad3
=
170 pass
->CreateAndAppendDrawQuad
<SolidColorDrawQuad
>();
171 color_quad3
->SetNew(pass
->shared_quad_state_list
.back(),
172 gfx::Rect(3, 3, 3, 3), gfx::Rect(3, 3, 3, 3), SkColor(),
175 SolidColorDrawQuad
* color_quad4
=
176 pass
->CreateAndAppendDrawQuad
<SolidColorDrawQuad
>();
177 color_quad4
->SetNew(pass
->shared_quad_state_list
.back(),
178 gfx::Rect(4, 4, 4, 4), gfx::Rect(4, 4, 4, 4), SkColor(),
181 // A second render pass with a quad.
182 RenderPassId
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 SolidColorDrawQuad
* contrib_quad
=
208 contrib
->CreateAndAppendDrawQuad
<SolidColorDrawQuad
>();
209 contrib_quad
->SetNew(contrib
->shared_quad_state_list
.back(),
210 gfx::Rect(3, 3, 3, 3), gfx::Rect(3, 3, 3, 3), SkColor(),
213 // And a RenderPassDrawQuad for the contributing pass.
214 scoped_ptr
<RenderPassDrawQuad
> pass_quad
=
215 make_scoped_ptr(new RenderPassDrawQuad
);
216 pass_quad
->SetNew(pass
->shared_quad_state_list
.back(),
227 pass_list
.push_back(pass
.Pass());
228 pass_list
.push_back(contrib
.Pass());
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 RenderPassId
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 SolidColorDrawQuad
* color_quad1
=
266 pass
->CreateAndAppendDrawQuad
<SolidColorDrawQuad
>();
267 color_quad1
->SetNew(pass
->shared_quad_state_list
.back(),
268 gfx::Rect(1, 1, 1, 1), gfx::Rect(1, 1, 1, 1), SkColor(),
271 // A shared state with no quads, they were culled.
272 SharedQuadState
* shared_state2
= pass
->CreateAndAppendSharedQuadState();
273 shared_state2
->SetAll(gfx::Transform(),
279 SkXfermode::kSrcOver_Mode
,
282 // A second shared state with no quads.
283 SharedQuadState
* shared_state3
= pass
->CreateAndAppendSharedQuadState();
284 shared_state3
->SetAll(gfx::Transform(),
290 SkXfermode::kSrcOver_Mode
,
293 // A last shared state with a quad again.
294 SharedQuadState
* shared_state4
= pass
->CreateAndAppendSharedQuadState();
295 shared_state4
->SetAll(gfx::Transform(),
301 SkXfermode::kSrcOver_Mode
,
304 SolidColorDrawQuad
* color_quad2
=
305 pass
->CreateAndAppendDrawQuad
<SolidColorDrawQuad
>();
306 color_quad2
->SetNew(pass
->shared_quad_state_list
.back(),
307 gfx::Rect(3, 3, 3, 3), gfx::Rect(3, 3, 3, 3), SkColor(),
310 pass_list
.push_back(pass
.Pass());
312 // Make a copy with CopyAll().
313 RenderPassList copy_list
;
314 RenderPass::CopyAll(pass_list
, ©_list
);
316 CompareRenderPassLists(pass_list
, copy_list
);