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::RectF 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 scoped_ptr<SharedQuadState> shared_state = SharedQuadState::Create(); 83 shared_state->SetAll(gfx::Transform(), 84 gfx::Size(), 85 gfx::Rect(), 86 gfx::Rect(), 87 false, 88 1, 89 SkXfermode::kSrcOver_Mode); 90 pass->AppendSharedQuadState(shared_state.Pass()); 91 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>()); 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 scoped_ptr<SharedQuadState> shared_state1 = SharedQuadState::Create(); 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 pass->AppendSharedQuadState(shared_state1.Pass()); 142 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>()); 148 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>()); 154 155 // And two quads using another shared state. 156 scoped_ptr<SharedQuadState> shared_state2 = SharedQuadState::Create(); 157 shared_state2->SetAll(gfx::Transform(), 158 gfx::Size(2, 2), 159 gfx::Rect(), 160 gfx::Rect(), 161 false, 162 1, 163 SkXfermode::kSrcOver_Mode); 164 pass->AppendSharedQuadState(shared_state2.Pass()); 165 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>()); 171 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>()); 177 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; 185 186 scoped_ptr<TestRenderPass> contrib = TestRenderPass::Create(); 187 contrib->SetAll(contrib_id, 188 contrib_output_rect, 189 contrib_damage_rect, 190 contrib_transform_to_root, 191 contrib_has_transparent_background); 192 193 scoped_ptr<SharedQuadState> contrib_shared_state = SharedQuadState::Create(); 194 contrib_shared_state->SetAll(gfx::Transform(), 195 gfx::Size(2, 2), 196 gfx::Rect(), 197 gfx::Rect(), 198 false, 199 1, 200 SkXfermode::kSrcOver_Mode); 201 contrib->AppendSharedQuadState(contrib_shared_state.Pass()); 202 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>()); 208 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(), 212 contrib_output_rect, 213 contrib_id, 214 false, // is_replica 215 0, // mask_resource_id 216 contrib_damage_rect, 217 gfx::RectF(), // mask_uv_rect 218 FilterOperations(), 219 FilterOperations()); 220 pass->quad_list.push_back(pass_quad.PassAs<DrawQuad>()); 221 222 pass_list.push_back(pass.PassAs<RenderPass>()); 223 pass_list.push_back(contrib.PassAs<RenderPass>()); 224 225 // Make a copy with CopyAll(). 226 RenderPassList copy_list; 227 RenderPass::CopyAll(pass_list, ©_list); 228 229 CompareRenderPassLists(pass_list, copy_list); 230 } 231 232 TEST(RenderPassTest, CopyAllWithCulledQuads) { 233 RenderPassList pass_list; 234 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; 241 242 scoped_ptr<TestRenderPass> pass = TestRenderPass::Create(); 243 pass->SetAll(id, 244 output_rect, 245 damage_rect, 246 transform_to_root, 247 has_transparent_background); 248 249 // A shared state with a quad. 250 scoped_ptr<SharedQuadState> shared_state1 = SharedQuadState::Create(); 251 shared_state1->SetAll(gfx::Transform(), 252 gfx::Size(1, 1), 253 gfx::Rect(), 254 gfx::Rect(), 255 false, 256 1, 257 SkXfermode::kSrcOver_Mode); 258 pass->AppendSharedQuadState(shared_state1.Pass()); 259 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>()); 265 266 // A shared state with no quads, they were culled. 267 scoped_ptr<SharedQuadState> shared_state2 = SharedQuadState::Create(); 268 shared_state2->SetAll(gfx::Transform(), 269 gfx::Size(2, 2), 270 gfx::Rect(), 271 gfx::Rect(), 272 false, 273 1, 274 SkXfermode::kSrcOver_Mode); 275 pass->AppendSharedQuadState(shared_state2.Pass()); 276 277 // A second shared state with no quads. 278 scoped_ptr<SharedQuadState> shared_state3 = SharedQuadState::Create(); 279 shared_state3->SetAll(gfx::Transform(), 280 gfx::Size(2, 2), 281 gfx::Rect(), 282 gfx::Rect(), 283 false, 284 1, 285 SkXfermode::kSrcOver_Mode); 286 pass->AppendSharedQuadState(shared_state3.Pass()); 287 288 // A last shared state with a quad again. 289 scoped_ptr<SharedQuadState> shared_state4 = SharedQuadState::Create(); 290 shared_state4->SetAll(gfx::Transform(), 291 gfx::Size(2, 2), 292 gfx::Rect(), 293 gfx::Rect(), 294 false, 295 1, 296 SkXfermode::kSrcOver_Mode); 297 pass->AppendSharedQuadState(shared_state4.Pass()); 298 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>()); 304 305 pass_list.push_back(pass.PassAs<RenderPass>()); 306 307 // Make a copy with CopyAll(). 308 RenderPassList copy_list; 309 RenderPass::CopyAll(pass_list, ©_list); 310 311 CompareRenderPassLists(pass_list, copy_list); 312 } 313 314 } // namespace 315 } // namespace cc 316