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 RenderPass::Id 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 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 (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()); 58 EXPECT_EQ( 59 expected->quad_list[i]->shared_quad_state->content_bounds.ToString(), 60 actual->quad_list[i]->shared_quad_state->content_bounds.ToString()); 61 } 62 } 63 } 64 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; 72 73 scoped_ptr<TestRenderPass> pass = TestRenderPass::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()); 80 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, 88 1, 89 SkXfermode::kSrcOver_Mode, 90 0); 91 92 scoped_ptr<CheckerboardDrawQuad> checkerboard_quad = 93 CheckerboardDrawQuad::Create(); 94 checkerboard_quad->SetNew( 95 pass->shared_quad_state_list.back(), gfx::Rect(), gfx::Rect(), SkColor()); 96 pass->quad_list.push_back(checkerboard_quad.PassAs<DrawQuad>()); 97 98 RenderPass::Id 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 115 TEST(RenderPassTest, CopyAllShouldBeIdentical) { 116 RenderPassList pass_list; 117 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; 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 scoped_ptr<CheckerboardDrawQuad> checkerboard_quad1 = 144 CheckerboardDrawQuad::Create(); 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 pass->quad_list.push_back(checkerboard_quad1.PassAs<DrawQuad>()); 150 151 scoped_ptr<CheckerboardDrawQuad> checkerboard_quad2 = 152 CheckerboardDrawQuad::Create(); 153 checkerboard_quad2->SetNew(pass->shared_quad_state_list.back(), 154 gfx::Rect(2, 2, 2, 2), 155 gfx::Rect(2, 2, 2, 2), 156 SkColor()); 157 pass->quad_list.push_back(checkerboard_quad2.PassAs<DrawQuad>()); 158 159 // And two quads using another shared state. 160 SharedQuadState* shared_state2 = pass->CreateAndAppendSharedQuadState(); 161 shared_state2->SetAll(gfx::Transform(), 162 gfx::Size(2, 2), 163 gfx::Rect(), 164 gfx::Rect(), 165 false, 166 1, 167 SkXfermode::kSrcOver_Mode, 168 0); 169 170 scoped_ptr<CheckerboardDrawQuad> checkerboard_quad3 = 171 CheckerboardDrawQuad::Create(); 172 checkerboard_quad3->SetNew(pass->shared_quad_state_list.back(), 173 gfx::Rect(3, 3, 3, 3), 174 gfx::Rect(3, 3, 3, 3), 175 SkColor()); 176 pass->quad_list.push_back(checkerboard_quad3.PassAs<DrawQuad>()); 177 178 scoped_ptr<CheckerboardDrawQuad> checkerboard_quad4 = 179 CheckerboardDrawQuad::Create(); 180 checkerboard_quad4->SetNew(pass->shared_quad_state_list.back(), 181 gfx::Rect(4, 4, 4, 4), 182 gfx::Rect(4, 4, 4, 4), 183 SkColor()); 184 pass->quad_list.push_back(checkerboard_quad4.PassAs<DrawQuad>()); 185 186 // A second render pass with a quad. 187 RenderPass::Id contrib_id(4, 1); 188 gfx::Rect contrib_output_rect(10, 15, 12, 17); 189 gfx::Transform contrib_transform_to_root = 190 gfx::Transform(1.0, 0.5, 0.5, -0.5, -1.0, 0.0); 191 gfx::Rect contrib_damage_rect(11, 16, 10, 15); 192 bool contrib_has_transparent_background = true; 193 194 scoped_ptr<TestRenderPass> contrib = TestRenderPass::Create(); 195 contrib->SetAll(contrib_id, 196 contrib_output_rect, 197 contrib_damage_rect, 198 contrib_transform_to_root, 199 contrib_has_transparent_background); 200 201 SharedQuadState* contrib_shared_state = 202 contrib->CreateAndAppendSharedQuadState(); 203 contrib_shared_state->SetAll(gfx::Transform(), 204 gfx::Size(2, 2), 205 gfx::Rect(), 206 gfx::Rect(), 207 false, 208 1, 209 SkXfermode::kSrcOver_Mode, 210 0); 211 212 scoped_ptr<CheckerboardDrawQuad> contrib_quad = 213 CheckerboardDrawQuad::Create(); 214 contrib_quad->SetNew(contrib->shared_quad_state_list.back(), 215 gfx::Rect(3, 3, 3, 3), 216 gfx::Rect(3, 3, 3, 3), 217 SkColor()); 218 contrib->quad_list.push_back(contrib_quad.PassAs<DrawQuad>()); 219 220 // And a RenderPassDrawQuad for the contributing pass. 221 scoped_ptr<RenderPassDrawQuad> pass_quad = RenderPassDrawQuad::Create(); 222 pass_quad->SetNew(pass->shared_quad_state_list.back(), 223 contrib_output_rect, 224 contrib_output_rect, 225 contrib_id, 226 false, // is_replica 227 0, // mask_resource_id 228 contrib_damage_rect, 229 gfx::RectF(), // mask_uv_rect 230 FilterOperations(), 231 FilterOperations()); 232 pass->quad_list.push_back(pass_quad.PassAs<DrawQuad>()); 233 234 pass_list.push_back(pass.PassAs<RenderPass>()); 235 pass_list.push_back(contrib.PassAs<RenderPass>()); 236 237 // Make a copy with CopyAll(). 238 RenderPassList copy_list; 239 RenderPass::CopyAll(pass_list, ©_list); 240 241 CompareRenderPassLists(pass_list, copy_list); 242 } 243 244 TEST(RenderPassTest, CopyAllWithCulledQuads) { 245 RenderPassList pass_list; 246 247 RenderPass::Id id(3, 2); 248 gfx::Rect output_rect(45, 22, 120, 13); 249 gfx::Transform transform_to_root = 250 gfx::Transform(1.0, 0.5, 0.5, -0.5, -1.0, 0.0); 251 gfx::Rect damage_rect(56, 123, 19, 43); 252 bool has_transparent_background = true; 253 254 scoped_ptr<TestRenderPass> pass = TestRenderPass::Create(); 255 pass->SetAll(id, 256 output_rect, 257 damage_rect, 258 transform_to_root, 259 has_transparent_background); 260 261 // A shared state with a quad. 262 SharedQuadState* shared_state1 = pass->CreateAndAppendSharedQuadState(); 263 shared_state1->SetAll(gfx::Transform(), 264 gfx::Size(1, 1), 265 gfx::Rect(), 266 gfx::Rect(), 267 false, 268 1, 269 SkXfermode::kSrcOver_Mode, 270 0); 271 272 scoped_ptr<CheckerboardDrawQuad> checkerboard_quad1 = 273 CheckerboardDrawQuad::Create(); 274 checkerboard_quad1->SetNew(pass->shared_quad_state_list.back(), 275 gfx::Rect(1, 1, 1, 1), 276 gfx::Rect(1, 1, 1, 1), 277 SkColor()); 278 pass->quad_list.push_back(checkerboard_quad1.PassAs<DrawQuad>()); 279 280 // A shared state with no quads, they were culled. 281 SharedQuadState* shared_state2 = pass->CreateAndAppendSharedQuadState(); 282 shared_state2->SetAll(gfx::Transform(), 283 gfx::Size(2, 2), 284 gfx::Rect(), 285 gfx::Rect(), 286 false, 287 1, 288 SkXfermode::kSrcOver_Mode, 289 0); 290 291 // A second shared state with no quads. 292 SharedQuadState* shared_state3 = pass->CreateAndAppendSharedQuadState(); 293 shared_state3->SetAll(gfx::Transform(), 294 gfx::Size(2, 2), 295 gfx::Rect(), 296 gfx::Rect(), 297 false, 298 1, 299 SkXfermode::kSrcOver_Mode, 300 0); 301 302 // A last shared state with a quad again. 303 SharedQuadState* shared_state4 = pass->CreateAndAppendSharedQuadState(); 304 shared_state4->SetAll(gfx::Transform(), 305 gfx::Size(2, 2), 306 gfx::Rect(), 307 gfx::Rect(), 308 false, 309 1, 310 SkXfermode::kSrcOver_Mode, 311 0); 312 313 scoped_ptr<CheckerboardDrawQuad> checkerboard_quad2 = 314 CheckerboardDrawQuad::Create(); 315 checkerboard_quad2->SetNew(pass->shared_quad_state_list.back(), 316 gfx::Rect(3, 3, 3, 3), 317 gfx::Rect(3, 3, 3, 3), 318 SkColor()); 319 pass->quad_list.push_back(checkerboard_quad2.PassAs<DrawQuad>()); 320 321 pass_list.push_back(pass.PassAs<RenderPass>()); 322 323 // Make a copy with CopyAll(). 324 RenderPassList copy_list; 325 RenderPass::CopyAll(pass_list, ©_list); 326 327 CompareRenderPassLists(pass_list, copy_list); 328 } 329 330 } // namespace 331 } // namespace cc 332