Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / cc / quads / render_pass_unittest.cc
blobdea89cc9248d19d10d29cdfcca85a1a0d2572c8f
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 "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/skia/include/effects/SkBlurImageFilter.h"
15 #include "ui/gfx/transform.h"
17 namespace cc {
18 namespace {
20 struct RenderPassSize {
21 // If you add a new field to this class, make sure to add it to the
22 // Copy() tests.
23 RenderPassId id;
24 QuadList quad_list;
25 SharedQuadStateList shared_quad_state_list;
26 gfx::Transform transform_to_root_target;
27 gfx::Rect output_rect;
28 gfx::Rect damage_rect;
29 bool has_transparent_background;
30 std::vector<SurfaceId> referenced_surfaces;
31 ScopedPtrVector<CopyOutputRequest> copy_callbacks;
34 static void CompareRenderPassLists(const RenderPassList& expected_list,
35 const RenderPassList& actual_list) {
36 EXPECT_EQ(expected_list.size(), actual_list.size());
37 for (size_t i = 0; i < actual_list.size(); ++i) {
38 RenderPass* expected = expected_list[i];
39 RenderPass* actual = actual_list[i];
41 EXPECT_EQ(expected->id, actual->id);
42 EXPECT_EQ(expected->output_rect, actual->output_rect);
43 EXPECT_EQ(expected->transform_to_root_target,
44 actual->transform_to_root_target);
45 EXPECT_EQ(expected->damage_rect, actual->damage_rect);
46 EXPECT_EQ(expected->has_transparent_background,
47 actual->has_transparent_background);
49 EXPECT_EQ(expected->shared_quad_state_list.size(),
50 actual->shared_quad_state_list.size());
51 EXPECT_EQ(expected->quad_list.size(), actual->quad_list.size());
52 EXPECT_EQ(expected->referenced_surfaces, actual->referenced_surfaces);
54 for (auto exp_iter = expected->quad_list.cbegin(),
55 act_iter = actual->quad_list.cbegin();
56 exp_iter != expected->quad_list.cend();
57 ++exp_iter, ++act_iter) {
58 EXPECT_EQ(exp_iter->rect.ToString(), act_iter->rect.ToString());
59 EXPECT_EQ(exp_iter->shared_quad_state->quad_layer_bounds.ToString(),
60 act_iter->shared_quad_state->quad_layer_bounds.ToString());
65 TEST(RenderPassTest, CopyShouldBeIdenticalExceptIdAndQuads) {
66 RenderPassId 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<RenderPass> pass = RenderPass::Create();
74 pass->SetAll(id,
75 output_rect,
76 damage_rect,
77 transform_to_root,
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(),
84 gfx::Size(),
85 gfx::Rect(),
86 gfx::Rect(),
87 false,
89 SkXfermode::kSrcOver_Mode,
90 0);
92 SolidColorDrawQuad* color_quad =
93 pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
94 color_quad->SetNew(pass->shared_quad_state_list.back(), gfx::Rect(),
95 gfx::Rect(), SkColor(), false);
97 RenderPassId new_id(63, 4);
99 scoped_ptr<RenderPass> copy = pass->Copy(new_id);
100 EXPECT_EQ(new_id, copy->id);
101 EXPECT_EQ(pass->output_rect, copy->output_rect);
102 EXPECT_EQ(pass->transform_to_root_target, copy->transform_to_root_target);
103 EXPECT_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());
106 EXPECT_EQ(0u, copy->referenced_surfaces.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<RenderPass> pass = RenderPass::Create();
126 pass->SetAll(id,
127 output_rect,
128 damage_rect,
129 transform_to_root,
130 has_transparent_background);
132 // Two quads using one shared state.
133 SharedQuadState* shared_state1 = pass->CreateAndAppendSharedQuadState();
134 shared_state1->SetAll(gfx::Transform(),
135 gfx::Size(1, 1),
136 gfx::Rect(),
137 gfx::Rect(),
138 false,
140 SkXfermode::kSrcOver_Mode,
143 SolidColorDrawQuad* color_quad1 =
144 pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
145 color_quad1->SetNew(pass->shared_quad_state_list.back(),
146 gfx::Rect(1, 1, 1, 1), gfx::Rect(1, 1, 1, 1), SkColor(),
147 false);
149 SolidColorDrawQuad* color_quad2 =
150 pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
151 color_quad2->SetNew(pass->shared_quad_state_list.back(),
152 gfx::Rect(2, 2, 2, 2), gfx::Rect(2, 2, 2, 2), SkColor(),
153 false);
155 // And two quads using another shared state.
156 SharedQuadState* shared_state2 = pass->CreateAndAppendSharedQuadState();
157 shared_state2->SetAll(gfx::Transform(),
158 gfx::Size(2, 2),
159 gfx::Rect(),
160 gfx::Rect(),
161 false,
163 SkXfermode::kSrcOver_Mode,
166 SolidColorDrawQuad* color_quad3 =
167 pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
168 color_quad3->SetNew(pass->shared_quad_state_list.back(),
169 gfx::Rect(3, 3, 3, 3), gfx::Rect(3, 3, 3, 3), SkColor(),
170 false);
172 SolidColorDrawQuad* color_quad4 =
173 pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
174 color_quad4->SetNew(pass->shared_quad_state_list.back(),
175 gfx::Rect(4, 4, 4, 4), gfx::Rect(4, 4, 4, 4), SkColor(),
176 false);
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<RenderPass> contrib = RenderPass::Create();
187 contrib->SetAll(contrib_id,
188 contrib_output_rect,
189 contrib_damage_rect,
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(),
196 gfx::Size(2, 2),
197 gfx::Rect(),
198 gfx::Rect(),
199 false,
201 SkXfermode::kSrcOver_Mode,
204 SolidColorDrawQuad* contrib_quad =
205 contrib->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
206 contrib_quad->SetNew(contrib->shared_quad_state_list.back(),
207 gfx::Rect(3, 3, 3, 3), gfx::Rect(3, 3, 3, 3), SkColor(),
208 false);
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(),
214 contrib_output_rect,
215 contrib_output_rect,
216 contrib_id,
218 gfx::Vector2dF(),
219 gfx::Size(),
220 FilterOperations(),
221 gfx::Vector2dF(),
222 FilterOperations());
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, &copy_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<RenderPass> pass = RenderPass::Create();
245 pass->SetAll(id,
246 output_rect,
247 damage_rect,
248 transform_to_root,
249 has_transparent_background);
251 // A shared state with a quad.
252 SharedQuadState* shared_state1 = pass->CreateAndAppendSharedQuadState();
253 shared_state1->SetAll(gfx::Transform(),
254 gfx::Size(1, 1),
255 gfx::Rect(),
256 gfx::Rect(),
257 false,
259 SkXfermode::kSrcOver_Mode,
262 SolidColorDrawQuad* color_quad1 =
263 pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
264 color_quad1->SetNew(pass->shared_quad_state_list.back(),
265 gfx::Rect(1, 1, 1, 1), gfx::Rect(1, 1, 1, 1), SkColor(),
266 false);
268 // A shared state with no quads, they were culled.
269 SharedQuadState* shared_state2 = pass->CreateAndAppendSharedQuadState();
270 shared_state2->SetAll(gfx::Transform(),
271 gfx::Size(2, 2),
272 gfx::Rect(),
273 gfx::Rect(),
274 false,
276 SkXfermode::kSrcOver_Mode,
279 // A second shared state with no quads.
280 SharedQuadState* shared_state3 = pass->CreateAndAppendSharedQuadState();
281 shared_state3->SetAll(gfx::Transform(),
282 gfx::Size(2, 2),
283 gfx::Rect(),
284 gfx::Rect(),
285 false,
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(),
293 gfx::Size(2, 2),
294 gfx::Rect(),
295 gfx::Rect(),
296 false,
298 SkXfermode::kSrcOver_Mode,
301 SolidColorDrawQuad* color_quad2 =
302 pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
303 color_quad2->SetNew(pass->shared_quad_state_list.back(),
304 gfx::Rect(3, 3, 3, 3), gfx::Rect(3, 3, 3, 3), SkColor(),
305 false);
307 pass_list.push_back(pass.Pass());
309 // Make a copy with CopyAll().
310 RenderPassList copy_list;
311 RenderPass::CopyAll(pass_list, &copy_list);
313 CompareRenderPassLists(pass_list, copy_list);
316 } // namespace
317 } // namespace cc