• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "cc/quads/render_pass.h"
6 
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"
17 
18 using cc::TestRenderPass;
19 
20 namespace cc {
21 namespace {
22 
23 struct RenderPassSize {
24   // If you add a new field to this class, make sure to add it to the
25   // Copy() tests.
26   RenderPassId id;
27   QuadList quad_list;
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;
34 };
35 
CompareRenderPassLists(const RenderPassList & expected_list,const RenderPassList & actual_list)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];
42 
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);
50 
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());
54 
55     for (QuadList::Iterator exp_iter = expected->quad_list.begin(),
56                             act_iter = actual->quad_list.begin();
57          exp_iter != expected->quad_list.end();
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());
62     }
63   }
64 }
65 
TEST(RenderPassTest,CopyShouldBeIdenticalExceptIdAndQuads)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;
73 
74   scoped_ptr<TestRenderPass> pass = TestRenderPass::Create();
75   pass->SetAll(id,
76                output_rect,
77                damage_rect,
78                transform_to_root,
79                has_transparent_background);
80   pass->copy_requests.push_back(CopyOutputRequest::CreateEmptyRequest());
81 
82   // Stick a quad in the pass, this should not get copied.
83   SharedQuadState* shared_state = pass->CreateAndAppendSharedQuadState();
84   shared_state->SetAll(gfx::Transform(),
85                        gfx::Size(),
86                        gfx::Rect(),
87                        gfx::Rect(),
88                        false,
89                        1,
90                        SkXfermode::kSrcOver_Mode,
91                        0);
92 
93   CheckerboardDrawQuad* checkerboard_quad =
94       pass->CreateAndAppendDrawQuad<CheckerboardDrawQuad>();
95   checkerboard_quad->SetNew(
96       pass->shared_quad_state_list.back(), gfx::Rect(), gfx::Rect(), SkColor());
97 
98   RenderPassId new_id(63, 4);
99 
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());
107 
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());
111 
112   EXPECT_EQ(sizeof(RenderPassSize), sizeof(RenderPass));
113 }
114 
TEST(RenderPassTest,CopyAllShouldBeIdentical)115 TEST(RenderPassTest, CopyAllShouldBeIdentical) {
116   RenderPassList pass_list;
117 
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;
124 
125   scoped_ptr<TestRenderPass> pass = TestRenderPass::Create();
126   pass->SetAll(id,
127                output_rect,
128                damage_rect,
129                transform_to_root,
130                has_transparent_background);
131 
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,
139                         1,
140                         SkXfermode::kSrcOver_Mode,
141                         0);
142 
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),
147                              gfx::Rect(1, 1, 1, 1),
148                              SkColor());
149 
150   CheckerboardDrawQuad* checkerboard_quad2 =
151       pass->CreateAndAppendDrawQuad<CheckerboardDrawQuad>();
152   checkerboard_quad2->SetNew(pass->shared_quad_state_list.back(),
153                              gfx::Rect(2, 2, 2, 2),
154                              gfx::Rect(2, 2, 2, 2),
155                              SkColor());
156 
157   // And two quads using another shared state.
158   SharedQuadState* shared_state2 = pass->CreateAndAppendSharedQuadState();
159   shared_state2->SetAll(gfx::Transform(),
160                         gfx::Size(2, 2),
161                         gfx::Rect(),
162                         gfx::Rect(),
163                         false,
164                         1,
165                         SkXfermode::kSrcOver_Mode,
166                         0);
167 
168   CheckerboardDrawQuad* checkerboard_quad3 =
169       pass->CreateAndAppendDrawQuad<CheckerboardDrawQuad>();
170   checkerboard_quad3->SetNew(pass->shared_quad_state_list.back(),
171                              gfx::Rect(3, 3, 3, 3),
172                              gfx::Rect(3, 3, 3, 3),
173                              SkColor());
174 
175   CheckerboardDrawQuad* checkerboard_quad4 =
176       pass->CreateAndAppendDrawQuad<CheckerboardDrawQuad>();
177   checkerboard_quad4->SetNew(pass->shared_quad_state_list.back(),
178                              gfx::Rect(4, 4, 4, 4),
179                              gfx::Rect(4, 4, 4, 4),
180                              SkColor());
181 
182   // A second render pass with a quad.
183   RenderPassId contrib_id(4, 1);
184   gfx::Rect contrib_output_rect(10, 15, 12, 17);
185   gfx::Transform contrib_transform_to_root =
186       gfx::Transform(1.0, 0.5, 0.5, -0.5, -1.0, 0.0);
187   gfx::Rect contrib_damage_rect(11, 16, 10, 15);
188   bool contrib_has_transparent_background = true;
189 
190   scoped_ptr<TestRenderPass> contrib = TestRenderPass::Create();
191   contrib->SetAll(contrib_id,
192                   contrib_output_rect,
193                   contrib_damage_rect,
194                   contrib_transform_to_root,
195                   contrib_has_transparent_background);
196 
197   SharedQuadState* contrib_shared_state =
198       contrib->CreateAndAppendSharedQuadState();
199   contrib_shared_state->SetAll(gfx::Transform(),
200                                gfx::Size(2, 2),
201                                gfx::Rect(),
202                                gfx::Rect(),
203                                false,
204                                1,
205                                SkXfermode::kSrcOver_Mode,
206                                0);
207 
208   CheckerboardDrawQuad* contrib_quad =
209       contrib->CreateAndAppendDrawQuad<CheckerboardDrawQuad>();
210   contrib_quad->SetNew(contrib->shared_quad_state_list.back(),
211                        gfx::Rect(3, 3, 3, 3),
212                        gfx::Rect(3, 3, 3, 3),
213                        SkColor());
214 
215   // And a RenderPassDrawQuad for the contributing pass.
216   scoped_ptr<RenderPassDrawQuad> pass_quad =
217       make_scoped_ptr(new RenderPassDrawQuad);
218   pass_quad->SetNew(pass->shared_quad_state_list.back(),
219                     contrib_output_rect,
220                     contrib_output_rect,
221                     contrib_id,
222                     0,      // mask_resource_id
223                     gfx::RectF(),  // mask_uv_rect
224                     FilterOperations(),
225                     gfx::Vector2dF(),  // filters_scale
226                     FilterOperations());
227 
228   pass_list.push_back(pass.PassAs<RenderPass>());
229   pass_list.push_back(contrib.PassAs<RenderPass>());
230 
231   // Make a copy with CopyAll().
232   RenderPassList copy_list;
233   RenderPass::CopyAll(pass_list, &copy_list);
234 
235   CompareRenderPassLists(pass_list, copy_list);
236 }
237 
TEST(RenderPassTest,CopyAllWithCulledQuads)238 TEST(RenderPassTest, CopyAllWithCulledQuads) {
239   RenderPassList pass_list;
240 
241   RenderPassId id(3, 2);
242   gfx::Rect output_rect(45, 22, 120, 13);
243   gfx::Transform transform_to_root =
244       gfx::Transform(1.0, 0.5, 0.5, -0.5, -1.0, 0.0);
245   gfx::Rect damage_rect(56, 123, 19, 43);
246   bool has_transparent_background = true;
247 
248   scoped_ptr<TestRenderPass> pass = TestRenderPass::Create();
249   pass->SetAll(id,
250                output_rect,
251                damage_rect,
252                transform_to_root,
253                has_transparent_background);
254 
255   // A shared state with a quad.
256   SharedQuadState* shared_state1 = pass->CreateAndAppendSharedQuadState();
257   shared_state1->SetAll(gfx::Transform(),
258                         gfx::Size(1, 1),
259                         gfx::Rect(),
260                         gfx::Rect(),
261                         false,
262                         1,
263                         SkXfermode::kSrcOver_Mode,
264                         0);
265 
266   CheckerboardDrawQuad* checkerboard_quad1 =
267       pass->CreateAndAppendDrawQuad<CheckerboardDrawQuad>();
268   checkerboard_quad1->SetNew(pass->shared_quad_state_list.back(),
269                              gfx::Rect(1, 1, 1, 1),
270                              gfx::Rect(1, 1, 1, 1),
271                              SkColor());
272 
273   // A shared state with no quads, they were culled.
274   SharedQuadState* shared_state2 = pass->CreateAndAppendSharedQuadState();
275   shared_state2->SetAll(gfx::Transform(),
276                         gfx::Size(2, 2),
277                         gfx::Rect(),
278                         gfx::Rect(),
279                         false,
280                         1,
281                         SkXfermode::kSrcOver_Mode,
282                         0);
283 
284   // A second shared state with no quads.
285   SharedQuadState* shared_state3 = pass->CreateAndAppendSharedQuadState();
286   shared_state3->SetAll(gfx::Transform(),
287                         gfx::Size(2, 2),
288                         gfx::Rect(),
289                         gfx::Rect(),
290                         false,
291                         1,
292                         SkXfermode::kSrcOver_Mode,
293                         0);
294 
295   // A last shared state with a quad again.
296   SharedQuadState* shared_state4 = pass->CreateAndAppendSharedQuadState();
297   shared_state4->SetAll(gfx::Transform(),
298                         gfx::Size(2, 2),
299                         gfx::Rect(),
300                         gfx::Rect(),
301                         false,
302                         1,
303                         SkXfermode::kSrcOver_Mode,
304                         0);
305 
306   CheckerboardDrawQuad* checkerboard_quad2 =
307       pass->CreateAndAppendDrawQuad<CheckerboardDrawQuad>();
308   checkerboard_quad2->SetNew(pass->shared_quad_state_list.back(),
309                              gfx::Rect(3, 3, 3, 3),
310                              gfx::Rect(3, 3, 3, 3),
311                              SkColor());
312 
313   pass_list.push_back(pass.PassAs<RenderPass>());
314 
315   // Make a copy with CopyAll().
316   RenderPassList copy_list;
317   RenderPass::CopyAll(pass_list, &copy_list);
318 
319   CompareRenderPassLists(pass_list, copy_list);
320 }
321 
322 }  // namespace
323 }  // namespace cc
324