1 // Copyright 2011 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/trees/layer_tree_host_common.h" 6 7 #include <algorithm> 8 9 #include "base/debug/trace_event.h" 10 #include "cc/base/math_util.h" 11 #include "cc/layers/heads_up_display_layer_impl.h" 12 #include "cc/layers/layer.h" 13 #include "cc/layers/layer_impl.h" 14 #include "cc/layers/layer_iterator.h" 15 #include "cc/layers/render_surface.h" 16 #include "cc/layers/render_surface_impl.h" 17 #include "cc/trees/layer_sorter.h" 18 #include "cc/trees/layer_tree_impl.h" 19 #include "ui/gfx/point_conversions.h" 20 #include "ui/gfx/rect_conversions.h" 21 #include "ui/gfx/transform.h" 22 23 namespace cc { 24 25 ScrollAndScaleSet::ScrollAndScaleSet() {} 26 27 ScrollAndScaleSet::~ScrollAndScaleSet() {} 28 29 static void SortLayers(LayerList::iterator forst, 30 LayerList::iterator end, 31 void* layer_sorter) { 32 NOTREACHED(); 33 } 34 35 static void SortLayers(LayerImplList::iterator first, 36 LayerImplList::iterator end, 37 LayerSorter* layer_sorter) { 38 DCHECK(layer_sorter); 39 TRACE_EVENT0("cc", "LayerTreeHostCommon::SortLayers"); 40 layer_sorter->Sort(first, end); 41 } 42 43 inline gfx::Rect CalculateVisibleRectWithCachedLayerRect( 44 gfx::Rect target_surface_rect, 45 gfx::Rect layer_bound_rect, 46 gfx::Rect layer_rect_in_target_space, 47 const gfx::Transform& transform) { 48 if (layer_rect_in_target_space.IsEmpty()) 49 return gfx::Rect(); 50 51 // Is this layer fully contained within the target surface? 52 if (target_surface_rect.Contains(layer_rect_in_target_space)) 53 return layer_bound_rect; 54 55 // If the layer doesn't fill up the entire surface, then find the part of 56 // the surface rect where the layer could be visible. This avoids trying to 57 // project surface rect points that are behind the projection point. 58 gfx::Rect minimal_surface_rect = target_surface_rect; 59 minimal_surface_rect.Intersect(layer_rect_in_target_space); 60 61 // Project the corners of the target surface rect into the layer space. 62 // This bounding rectangle may be larger than it needs to be (being 63 // axis-aligned), but is a reasonable filter on the space to consider. 64 // Non-invertible transforms will create an empty rect here. 65 66 gfx::Transform surface_to_layer(gfx::Transform::kSkipInitialization); 67 if (!transform.GetInverse(&surface_to_layer)) { 68 // TODO(shawnsingh): Some uninvertible transforms may be visible, but 69 // their behaviour is undefined thoughout the compositor. Make their 70 // behaviour well-defined and allow the visible content rect to be non- 71 // empty when needed. 72 return gfx::Rect(); 73 } 74 75 gfx::Rect layer_rect = gfx::ToEnclosingRect(MathUtil::ProjectClippedRect( 76 surface_to_layer, gfx::RectF(minimal_surface_rect))); 77 layer_rect.Intersect(layer_bound_rect); 78 return layer_rect; 79 } 80 81 gfx::Rect LayerTreeHostCommon::CalculateVisibleRect( 82 gfx::Rect target_surface_rect, 83 gfx::Rect layer_bound_rect, 84 const gfx::Transform& transform) { 85 gfx::Rect layer_in_surface_space = 86 MathUtil::MapClippedRect(transform, layer_bound_rect); 87 return CalculateVisibleRectWithCachedLayerRect( 88 target_surface_rect, layer_bound_rect, layer_in_surface_space, transform); 89 } 90 91 template <typename LayerType> static inline bool IsRootLayer(LayerType* layer) { 92 return !layer->parent(); 93 } 94 95 template <typename LayerType> 96 static inline bool LayerIsInExisting3DRenderingContext(LayerType* layer) { 97 // According to current W3C spec on CSS transforms, a layer is part of an 98 // established 3d rendering context if its parent has transform-style of 99 // preserves-3d. 100 return layer->parent() && layer->parent()->preserves_3d(); 101 } 102 103 template <typename LayerType> 104 static bool IsRootLayerOfNewRenderingContext(LayerType* layer) { 105 // According to current W3C spec on CSS transforms (Section 6.1), a layer is 106 // the beginning of 3d rendering context if its parent does not have 107 // transform-style: preserve-3d, but this layer itself does. 108 if (layer->parent()) 109 return !layer->parent()->preserves_3d() && layer->preserves_3d(); 110 111 return layer->preserves_3d(); 112 } 113 114 template <typename LayerType> 115 static bool IsLayerBackFaceVisible(LayerType* layer) { 116 // The current W3C spec on CSS transforms says that backface visibility should 117 // be determined differently depending on whether the layer is in a "3d 118 // rendering context" or not. For Chromium code, we can determine whether we 119 // are in a 3d rendering context by checking if the parent preserves 3d. 120 121 if (LayerIsInExisting3DRenderingContext(layer)) 122 return layer->draw_transform().IsBackFaceVisible(); 123 124 // In this case, either the layer establishes a new 3d rendering context, or 125 // is not in a 3d rendering context at all. 126 return layer->transform().IsBackFaceVisible(); 127 } 128 129 template <typename LayerType> 130 static bool IsSurfaceBackFaceVisible(LayerType* layer, 131 const gfx::Transform& draw_transform) { 132 if (LayerIsInExisting3DRenderingContext(layer)) 133 return draw_transform.IsBackFaceVisible(); 134 135 if (IsRootLayerOfNewRenderingContext(layer)) 136 return layer->transform().IsBackFaceVisible(); 137 138 // If the render_surface is not part of a new or existing rendering context, 139 // then the layers that contribute to this surface will decide back-face 140 // visibility for themselves. 141 return false; 142 } 143 144 template <typename LayerType> 145 static inline bool LayerClipsSubtree(LayerType* layer) { 146 return layer->masks_to_bounds() || layer->mask_layer(); 147 } 148 149 template <typename LayerType> 150 static gfx::Rect CalculateVisibleContentRect( 151 LayerType* layer, 152 gfx::Rect clip_rect_of_target_surface_in_target_space, 153 gfx::Rect layer_rect_in_target_space) { 154 DCHECK(layer->render_target()); 155 156 // Nothing is visible if the layer bounds are empty. 157 if (!layer->DrawsContent() || layer->content_bounds().IsEmpty() || 158 layer->drawable_content_rect().IsEmpty()) 159 return gfx::Rect(); 160 161 // Compute visible bounds in target surface space. 162 gfx::Rect visible_rect_in_target_surface_space = 163 layer->drawable_content_rect(); 164 165 if (!layer->render_target()->render_surface()->clip_rect().IsEmpty()) { 166 // The |layer| L has a target T which owns a surface Ts. The surface Ts 167 // has a target TsT. 168 // 169 // In this case the target surface Ts does clip the layer L that contributes 170 // to it. So, we have to convert the clip rect of Ts from the target space 171 // of Ts (that is the space of TsT), to the current render target's space 172 // (that is the space of T). This conversion is done outside this function 173 // so that it can be cached instead of computing it redundantly for every 174 // layer. 175 visible_rect_in_target_surface_space.Intersect( 176 clip_rect_of_target_surface_in_target_space); 177 } 178 179 if (visible_rect_in_target_surface_space.IsEmpty()) 180 return gfx::Rect(); 181 182 return CalculateVisibleRectWithCachedLayerRect( 183 visible_rect_in_target_surface_space, 184 gfx::Rect(layer->content_bounds()), 185 layer_rect_in_target_space, 186 layer->draw_transform()); 187 } 188 189 static inline bool TransformToParentIsKnown(LayerImpl* layer) { return true; } 190 191 static inline bool TransformToParentIsKnown(Layer* layer) { 192 return !layer->TransformIsAnimating(); 193 } 194 195 static inline bool TransformToScreenIsKnown(LayerImpl* layer) { return true; } 196 197 static inline bool TransformToScreenIsKnown(Layer* layer) { 198 return !layer->screen_space_transform_is_animating(); 199 } 200 201 template <typename LayerType> 202 static bool LayerShouldBeSkipped(LayerType* layer, 203 bool layer_is_visible) { 204 // Layers can be skipped if any of these conditions are met. 205 // - is not visible due to it or one of its ancestors being hidden. 206 // - does not draw content. 207 // - is transparent 208 // - has empty bounds 209 // - the layer is not double-sided, but its back face is visible. 210 // 211 // Some additional conditions need to be computed at a later point after the 212 // recursion is finished. 213 // - the intersection of render_surface content and layer clip_rect is empty 214 // - the visible_content_rect is empty 215 // 216 // Note, if the layer should not have been drawn due to being fully 217 // transparent, we would have skipped the entire subtree and never made it 218 // into this function, so it is safe to omit this check here. 219 220 if (!layer_is_visible) 221 return true; 222 223 if (!layer->DrawsContent() || layer->bounds().IsEmpty()) 224 return true; 225 226 LayerType* backface_test_layer = layer; 227 if (layer->use_parent_backface_visibility()) { 228 DCHECK(layer->parent()); 229 DCHECK(!layer->parent()->use_parent_backface_visibility()); 230 backface_test_layer = layer->parent(); 231 } 232 233 // The layer should not be drawn if (1) it is not double-sided and (2) the 234 // back of the layer is known to be facing the screen. 235 if (!backface_test_layer->double_sided() && 236 TransformToScreenIsKnown(backface_test_layer) && 237 IsLayerBackFaceVisible(backface_test_layer)) 238 return true; 239 240 return false; 241 } 242 243 static inline bool SubtreeShouldBeSkipped(LayerImpl* layer, 244 bool layer_is_visible) { 245 // When we need to do a readback/copy of a layer's output, we can not skip 246 // it or any of its ancestors. 247 if (layer->draw_properties().layer_or_descendant_has_copy_request) 248 return false; 249 250 // If the layer is not visible, then skip it and its subtree. 251 if (!layer_is_visible) 252 return true; 253 254 // If layer is on the pending tree and opacity is being animated then 255 // this subtree can't be skipped as we need to create, prioritize and 256 // include tiles for this layer when deciding if tree can be activated. 257 if (layer->layer_tree_impl()->IsPendingTree() && layer->OpacityIsAnimating()) 258 return false; 259 260 // The opacity of a layer always applies to its children (either implicitly 261 // via a render surface or explicitly if the parent preserves 3D), so the 262 // entire subtree can be skipped if this layer is fully transparent. 263 return !layer->opacity(); 264 } 265 266 static inline bool SubtreeShouldBeSkipped(Layer* layer, 267 bool layer_is_visible) { 268 // When we need to do a readback/copy of a layer's output, we can not skip 269 // it or any of its ancestors. 270 if (layer->draw_properties().layer_or_descendant_has_copy_request) 271 return false; 272 273 // If the layer is not visible, then skip it and its subtree. 274 if (!layer_is_visible) 275 return true; 276 277 // If the opacity is being animated then the opacity on the main thread is 278 // unreliable (since the impl thread may be using a different opacity), so it 279 // should not be trusted. 280 // In particular, it should not cause the subtree to be skipped. 281 // Similarly, for layers that might animate opacity using an impl-only 282 // animation, their subtree should also not be skipped. 283 return !layer->opacity() && !layer->OpacityIsAnimating() && 284 !layer->OpacityCanAnimateOnImplThread(); 285 } 286 287 // Called on each layer that could be drawn after all information from 288 // CalcDrawProperties has been updated on that layer. May have some false 289 // positives (e.g. layers get this called on them but don't actually get drawn). 290 static inline void UpdateTilePrioritiesForLayer(LayerImpl* layer) { 291 layer->UpdateTilePriorities(); 292 293 // Mask layers don't get this call, so explicitly update them so they can 294 // kick off tile rasterization. 295 if (layer->mask_layer()) 296 layer->mask_layer()->UpdateTilePriorities(); 297 if (layer->replica_layer() && layer->replica_layer()->mask_layer()) 298 layer->replica_layer()->mask_layer()->UpdateTilePriorities(); 299 } 300 301 static inline void UpdateTilePrioritiesForLayer(Layer* layer) {} 302 303 static inline void SavePaintPropertiesLayer(LayerImpl* layer) {} 304 305 static inline void SavePaintPropertiesLayer(Layer* layer) { 306 layer->SavePaintProperties(); 307 308 if (layer->mask_layer()) 309 layer->mask_layer()->SavePaintProperties(); 310 if (layer->replica_layer() && layer->replica_layer()->mask_layer()) 311 layer->replica_layer()->mask_layer()->SavePaintProperties(); 312 } 313 314 template <typename LayerType> 315 static bool SubtreeShouldRenderToSeparateSurface( 316 LayerType* layer, 317 bool axis_aligned_with_respect_to_parent) { 318 // 319 // A layer and its descendants should render onto a new RenderSurfaceImpl if 320 // any of these rules hold: 321 // 322 323 // The root layer owns a render surface, but it never acts as a contributing 324 // surface to another render target. Compositor features that are applied via 325 // a contributing surface can not be applied to the root layer. In order to 326 // use these effects, another child of the root would need to be introduced 327 // in order to act as a contributing surface to the root layer's surface. 328 bool is_root = IsRootLayer(layer); 329 330 // If the layer uses a mask. 331 if (layer->mask_layer()) { 332 DCHECK(!is_root); 333 return true; 334 } 335 336 // If the layer has a reflection. 337 if (layer->replica_layer()) { 338 DCHECK(!is_root); 339 return true; 340 } 341 342 // If the layer uses a CSS filter. 343 if (!layer->filters().IsEmpty() || !layer->background_filters().IsEmpty() || 344 layer->filter()) { 345 DCHECK(!is_root); 346 return true; 347 } 348 349 int num_descendants_that_draw_content = 350 layer->draw_properties().num_descendants_that_draw_content; 351 352 // If the layer flattens its subtree (i.e. the layer doesn't preserve-3d), but 353 // it is treated as a 3D object by its parent (i.e. parent does preserve-3d). 354 if (LayerIsInExisting3DRenderingContext(layer) && !layer->preserves_3d() && 355 num_descendants_that_draw_content > 0) { 356 TRACE_EVENT_INSTANT0( 357 "cc", 358 "LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface flattening", 359 TRACE_EVENT_SCOPE_THREAD); 360 DCHECK(!is_root); 361 return true; 362 } 363 364 // If the layer clips its descendants but it is not axis-aligned with respect 365 // to its parent. 366 bool layer_clips_external_content = 367 LayerClipsSubtree(layer) || layer->HasDelegatedContent(); 368 if (layer_clips_external_content && !axis_aligned_with_respect_to_parent && 369 !layer->draw_properties().descendants_can_clip_selves) { 370 TRACE_EVENT_INSTANT0( 371 "cc", 372 "LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface clipping", 373 TRACE_EVENT_SCOPE_THREAD); 374 DCHECK(!is_root); 375 return true; 376 } 377 378 // If the layer has some translucency and does not have a preserves-3d 379 // transform style. This condition only needs a render surface if two or more 380 // layers in the subtree overlap. But checking layer overlaps is unnecessarily 381 // costly so instead we conservatively create a surface whenever at least two 382 // layers draw content for this subtree. 383 bool at_least_two_layers_in_subtree_draw_content = 384 num_descendants_that_draw_content > 0 && 385 (layer->DrawsContent() || num_descendants_that_draw_content > 1); 386 387 if (layer->opacity() != 1.f && !layer->preserves_3d() && 388 at_least_two_layers_in_subtree_draw_content) { 389 TRACE_EVENT_INSTANT0( 390 "cc", 391 "LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface opacity", 392 TRACE_EVENT_SCOPE_THREAD); 393 DCHECK(!is_root); 394 return true; 395 } 396 397 // The root layer should always have a render_surface. 398 if (is_root) 399 return true; 400 401 // 402 // These are allowed on the root surface, as they don't require the surface to 403 // be used as a contributing surface in order to apply correctly. 404 // 405 406 // If we force it. 407 if (layer->force_render_surface()) 408 return true; 409 410 // If we'll make a copy of the layer's contents. 411 if (layer->HasCopyRequest()) 412 return true; 413 414 return false; 415 } 416 417 static LayerImpl* NextTargetSurface(LayerImpl* layer) { 418 return layer->parent() ? layer->parent()->render_target() : 0; 419 } 420 421 // This function returns a translation matrix that can be applied on a vector 422 // that's in the layer's target surface coordinate, while the position offset is 423 // specified in some ancestor layer's coordinate. 424 gfx::Transform ComputeSizeDeltaCompensation( 425 LayerImpl* layer, 426 LayerImpl* container, 427 gfx::Vector2dF position_offset) { 428 gfx::Transform result_transform; 429 430 // To apply a translate in the container's layer space, 431 // the following steps need to be done: 432 // Step 1a. transform from target surface space to the container's target 433 // surface space 434 // Step 1b. transform from container's target surface space to the 435 // container's layer space 436 // Step 2. apply the compensation 437 // Step 3. transform back to target surface space 438 439 gfx::Transform target_surface_space_to_container_layer_space; 440 // Calculate step 1a 441 LayerImpl* container_target_surface = container->render_target(); 442 for (LayerImpl* current_target_surface = NextTargetSurface(layer); 443 current_target_surface && 444 current_target_surface != container_target_surface; 445 current_target_surface = NextTargetSurface(current_target_surface)) { 446 // Note: Concat is used here to convert the result coordinate space from 447 // current render surface to the next render surface. 448 target_surface_space_to_container_layer_space.ConcatTransform( 449 current_target_surface->render_surface()->draw_transform()); 450 } 451 // Calculate step 1b 452 gfx::Transform container_layer_space_to_container_target_surface_space = 453 container->draw_transform(); 454 container_layer_space_to_container_target_surface_space.Scale( 455 container->contents_scale_x(), container->contents_scale_y()); 456 457 gfx::Transform container_target_surface_space_to_container_layer_space; 458 if (container_layer_space_to_container_target_surface_space.GetInverse( 459 &container_target_surface_space_to_container_layer_space)) { 460 // Note: Again, Concat is used to conver the result coordinate space from 461 // the container render surface to the container layer. 462 target_surface_space_to_container_layer_space.ConcatTransform( 463 container_target_surface_space_to_container_layer_space); 464 } 465 466 // Apply step 3 467 gfx::Transform container_layer_space_to_target_surface_space; 468 if (target_surface_space_to_container_layer_space.GetInverse( 469 &container_layer_space_to_target_surface_space)) { 470 result_transform.PreconcatTransform( 471 container_layer_space_to_target_surface_space); 472 } else { 473 // TODO(shawnsingh): A non-invertible matrix could still make meaningful 474 // projection. For example ScaleZ(0) is non-invertible but the layer is 475 // still visible. 476 return gfx::Transform(); 477 } 478 479 // Apply step 2 480 result_transform.Translate(position_offset.x(), position_offset.y()); 481 482 // Apply step 1 483 result_transform.PreconcatTransform( 484 target_surface_space_to_container_layer_space); 485 486 return result_transform; 487 } 488 489 void ApplyPositionAdjustment( 490 Layer* layer, 491 Layer* container, 492 const gfx::Transform& scroll_compensation, 493 gfx::Transform* combined_transform) {} 494 void ApplyPositionAdjustment( 495 LayerImpl* layer, 496 LayerImpl* container, 497 const gfx::Transform& scroll_compensation, 498 gfx::Transform* combined_transform) { 499 if (!layer->position_constraint().is_fixed_position()) 500 return; 501 502 // Special case: this layer is a composited fixed-position layer; we need to 503 // explicitly compensate for all ancestors' nonzero scroll_deltas to keep 504 // this layer fixed correctly. 505 // Note carefully: this is Concat, not Preconcat 506 // (current_scroll_compensation * combined_transform). 507 combined_transform->ConcatTransform(scroll_compensation); 508 509 // For right-edge or bottom-edge anchored fixed position layers, 510 // the layer should relocate itself if the container changes its size. 511 bool fixed_to_right_edge = 512 layer->position_constraint().is_fixed_to_right_edge(); 513 bool fixed_to_bottom_edge = 514 layer->position_constraint().is_fixed_to_bottom_edge(); 515 gfx::Vector2dF position_offset = container->fixed_container_size_delta(); 516 position_offset.set_x(fixed_to_right_edge ? position_offset.x() : 0); 517 position_offset.set_y(fixed_to_bottom_edge ? position_offset.y() : 0); 518 if (position_offset.IsZero()) 519 return; 520 521 // Note: Again, this is Concat. The compensation matrix will be applied on 522 // the vector in target surface space. 523 combined_transform->ConcatTransform( 524 ComputeSizeDeltaCompensation(layer, container, position_offset)); 525 } 526 527 gfx::Transform ComputeScrollCompensationForThisLayer( 528 LayerImpl* scrolling_layer, 529 const gfx::Transform& parent_matrix) { 530 // For every layer that has non-zero scroll_delta, we have to compute a 531 // transform that can undo the scroll_delta translation. In particular, we 532 // want this matrix to premultiply a fixed-position layer's parent_matrix, so 533 // we design this transform in three steps as follows. The steps described 534 // here apply from right-to-left, so Step 1 would be the right-most matrix: 535 // 536 // Step 1. transform from target surface space to the exact space where 537 // scroll_delta is actually applied. 538 // -- this is inverse of parent_matrix 539 // Step 2. undo the scroll_delta 540 // -- this is just a translation by scroll_delta. 541 // Step 3. transform back to target surface space. 542 // -- this transform is the parent_matrix 543 // 544 // These steps create a matrix that both start and end in target surface 545 // space. So this matrix can pre-multiply any fixed-position layer's 546 // draw_transform to undo the scroll_deltas -- as long as that fixed position 547 // layer is fixed onto the same render_target as this scrolling_layer. 548 // 549 550 gfx::Transform scroll_compensation_for_this_layer = parent_matrix; // Step 3 551 scroll_compensation_for_this_layer.Translate( 552 scrolling_layer->ScrollDelta().x(), 553 scrolling_layer->ScrollDelta().y()); // Step 2 554 555 gfx::Transform inverse_parent_matrix(gfx::Transform::kSkipInitialization); 556 if (!parent_matrix.GetInverse(&inverse_parent_matrix)) { 557 // TODO(shawnsingh): Either we need to handle uninvertible transforms 558 // here, or DCHECK that the transform is invertible. 559 } 560 scroll_compensation_for_this_layer.PreconcatTransform( 561 inverse_parent_matrix); // Step 1 562 return scroll_compensation_for_this_layer; 563 } 564 565 gfx::Transform ComputeScrollCompensationMatrixForChildren( 566 Layer* current_layer, 567 const gfx::Transform& current_parent_matrix, 568 const gfx::Transform& current_scroll_compensation) { 569 // The main thread (i.e. Layer) does not need to worry about scroll 570 // compensation. So we can just return an identity matrix here. 571 return gfx::Transform(); 572 } 573 574 gfx::Transform ComputeScrollCompensationMatrixForChildren( 575 LayerImpl* layer, 576 const gfx::Transform& parent_matrix, 577 const gfx::Transform& current_scroll_compensation_matrix) { 578 // "Total scroll compensation" is the transform needed to cancel out all 579 // scroll_delta translations that occurred since the nearest container layer, 580 // even if there are render_surfaces in-between. 581 // 582 // There are some edge cases to be aware of, that are not explicit in the 583 // code: 584 // - A layer that is both a fixed-position and container should not be its 585 // own container, instead, that means it is fixed to an ancestor, and is a 586 // container for any fixed-position descendants. 587 // - A layer that is a fixed-position container and has a render_surface 588 // should behave the same as a container without a render_surface, the 589 // render_surface is irrelevant in that case. 590 // - A layer that does not have an explicit container is simply fixed to the 591 // viewport. (i.e. the root render_surface.) 592 // - If the fixed-position layer has its own render_surface, then the 593 // render_surface is the one who gets fixed. 594 // 595 // This function needs to be called AFTER layers create their own 596 // render_surfaces. 597 // 598 599 // Avoid the overheads (including stack allocation and matrix 600 // initialization/copy) if we know that the scroll compensation doesn't need 601 // to be reset or adjusted. 602 if (!layer->IsContainerForFixedPositionLayers() && 603 layer->ScrollDelta().IsZero() && !layer->render_surface()) 604 return current_scroll_compensation_matrix; 605 606 // Start as identity matrix. 607 gfx::Transform next_scroll_compensation_matrix; 608 609 // If this layer is not a container, then it inherits the existing scroll 610 // compensations. 611 if (!layer->IsContainerForFixedPositionLayers()) 612 next_scroll_compensation_matrix = current_scroll_compensation_matrix; 613 614 // If the current layer has a non-zero scroll_delta, then we should compute 615 // its local scroll compensation and accumulate it to the 616 // next_scroll_compensation_matrix. 617 if (!layer->ScrollDelta().IsZero()) { 618 gfx::Transform scroll_compensation_for_this_layer = 619 ComputeScrollCompensationForThisLayer( 620 layer, parent_matrix); 621 next_scroll_compensation_matrix.PreconcatTransform( 622 scroll_compensation_for_this_layer); 623 } 624 625 // If the layer created its own render_surface, we have to adjust 626 // next_scroll_compensation_matrix. The adjustment allows us to continue 627 // using the scroll compensation on the next surface. 628 // Step 1 (right-most in the math): transform from the new surface to the 629 // original ancestor surface 630 // Step 2: apply the scroll compensation 631 // Step 3: transform back to the new surface. 632 if (layer->render_surface() && 633 !next_scroll_compensation_matrix.IsIdentity()) { 634 gfx::Transform inverse_surface_draw_transform( 635 gfx::Transform::kSkipInitialization); 636 if (!layer->render_surface()->draw_transform().GetInverse( 637 &inverse_surface_draw_transform)) { 638 // TODO(shawnsingh): Either we need to handle uninvertible transforms 639 // here, or DCHECK that the transform is invertible. 640 } 641 next_scroll_compensation_matrix = 642 inverse_surface_draw_transform * next_scroll_compensation_matrix * 643 layer->render_surface()->draw_transform(); 644 } 645 646 return next_scroll_compensation_matrix; 647 } 648 649 template <typename LayerType> 650 static inline void CalculateContentsScale(LayerType* layer, 651 float contents_scale, 652 float device_scale_factor, 653 float page_scale_factor, 654 bool animating_transform_to_screen) { 655 layer->CalculateContentsScale(contents_scale, 656 device_scale_factor, 657 page_scale_factor, 658 animating_transform_to_screen, 659 &layer->draw_properties().contents_scale_x, 660 &layer->draw_properties().contents_scale_y, 661 &layer->draw_properties().content_bounds); 662 663 LayerType* mask_layer = layer->mask_layer(); 664 if (mask_layer) { 665 mask_layer->CalculateContentsScale( 666 contents_scale, 667 device_scale_factor, 668 page_scale_factor, 669 animating_transform_to_screen, 670 &mask_layer->draw_properties().contents_scale_x, 671 &mask_layer->draw_properties().contents_scale_y, 672 &mask_layer->draw_properties().content_bounds); 673 } 674 675 LayerType* replica_mask_layer = 676 layer->replica_layer() ? layer->replica_layer()->mask_layer() : NULL; 677 if (replica_mask_layer) { 678 replica_mask_layer->CalculateContentsScale( 679 contents_scale, 680 device_scale_factor, 681 page_scale_factor, 682 animating_transform_to_screen, 683 &replica_mask_layer->draw_properties().contents_scale_x, 684 &replica_mask_layer->draw_properties().contents_scale_y, 685 &replica_mask_layer->draw_properties().content_bounds); 686 } 687 } 688 689 static inline void UpdateLayerContentsScale( 690 LayerImpl* layer, 691 bool can_adjust_raster_scale, 692 float ideal_contents_scale, 693 float device_scale_factor, 694 float page_scale_factor, 695 bool animating_transform_to_screen) { 696 CalculateContentsScale(layer, 697 ideal_contents_scale, 698 device_scale_factor, 699 page_scale_factor, 700 animating_transform_to_screen); 701 } 702 703 static inline void UpdateLayerContentsScale( 704 Layer* layer, 705 bool can_adjust_raster_scale, 706 float ideal_contents_scale, 707 float device_scale_factor, 708 float page_scale_factor, 709 bool animating_transform_to_screen) { 710 if (can_adjust_raster_scale) { 711 float ideal_raster_scale = 712 ideal_contents_scale / (device_scale_factor * page_scale_factor); 713 714 bool need_to_set_raster_scale = layer->raster_scale_is_unknown(); 715 716 // If we've previously saved a raster_scale but the ideal changes, things 717 // are unpredictable and we should just use 1. 718 if (!need_to_set_raster_scale && layer->raster_scale() != 1.f && 719 ideal_raster_scale != layer->raster_scale()) { 720 ideal_raster_scale = 1.f; 721 need_to_set_raster_scale = true; 722 } 723 724 if (need_to_set_raster_scale) { 725 bool use_and_save_ideal_scale = 726 ideal_raster_scale >= 1.f && !animating_transform_to_screen; 727 if (use_and_save_ideal_scale) 728 layer->set_raster_scale(ideal_raster_scale); 729 } 730 } 731 732 float raster_scale = 1.f; 733 if (!layer->raster_scale_is_unknown()) 734 raster_scale = layer->raster_scale(); 735 736 737 float contents_scale = raster_scale * device_scale_factor * page_scale_factor; 738 CalculateContentsScale(layer, 739 contents_scale, 740 device_scale_factor, 741 page_scale_factor, 742 animating_transform_to_screen); 743 } 744 745 static inline RenderSurface* CreateOrReuseRenderSurface(Layer* layer) { 746 // The render surface should always be new on the main thread, as the 747 // RenderSurfaceLayerList should be a new empty list when given to 748 // CalculateDrawProperties. 749 DCHECK(!layer->render_surface()); 750 layer->CreateRenderSurface(); 751 return layer->render_surface(); 752 } 753 754 static inline RenderSurfaceImpl* CreateOrReuseRenderSurface(LayerImpl* layer) { 755 if (!layer->render_surface()) { 756 layer->CreateRenderSurface(); 757 return layer->render_surface(); 758 } 759 760 layer->render_surface()->ClearLayerLists(); 761 return layer->render_surface(); 762 } 763 764 template <typename LayerType, typename LayerList> 765 static inline void RemoveSurfaceForEarlyExit( 766 LayerType* layer_to_remove, 767 LayerList* render_surface_layer_list) { 768 DCHECK(layer_to_remove->render_surface()); 769 // Technically, we know that the layer we want to remove should be 770 // at the back of the render_surface_layer_list. However, we have had 771 // bugs before that added unnecessary layers here 772 // (https://bugs.webkit.org/show_bug.cgi?id=74147), but that causes 773 // things to crash. So here we proactively remove any additional 774 // layers from the end of the list. 775 while (render_surface_layer_list->back() != layer_to_remove) { 776 render_surface_layer_list->back()->ClearRenderSurface(); 777 render_surface_layer_list->pop_back(); 778 } 779 DCHECK_EQ(render_surface_layer_list->back(), layer_to_remove); 780 render_surface_layer_list->pop_back(); 781 layer_to_remove->ClearRenderSurface(); 782 } 783 784 struct PreCalculateMetaInformationRecursiveData { 785 bool layer_or_descendant_has_copy_request; 786 787 PreCalculateMetaInformationRecursiveData() 788 : layer_or_descendant_has_copy_request(false) {} 789 790 void Merge(const PreCalculateMetaInformationRecursiveData& data) { 791 layer_or_descendant_has_copy_request |= 792 data.layer_or_descendant_has_copy_request; 793 } 794 }; 795 796 // Recursively walks the layer tree to compute any information that is needed 797 // before doing the main recursion. 798 template <typename LayerType> 799 static void PreCalculateMetaInformation( 800 LayerType* layer, 801 PreCalculateMetaInformationRecursiveData* recursive_data) { 802 bool has_delegated_content = layer->HasDelegatedContent(); 803 int num_descendants_that_draw_content = 0; 804 bool descendants_can_clip_selves = true; 805 806 if (has_delegated_content) { 807 // Layers with delegated content need to be treated as if they have as 808 // many children as the number of layers they own delegated quads for. 809 // Since we don't know this number right now, we choose one that acts like 810 // infinity for our purposes. 811 num_descendants_that_draw_content = 1000; 812 descendants_can_clip_selves = false; 813 } 814 815 for (size_t i = 0; i < layer->children().size(); ++i) { 816 LayerType* child_layer = 817 LayerTreeHostCommon::get_child_as_raw_ptr(layer->children(), i); 818 819 PreCalculateMetaInformationRecursiveData data_for_child; 820 PreCalculateMetaInformation(child_layer, &data_for_child); 821 822 if (!has_delegated_content) { 823 bool sublayer_transform_prevents_clip = 824 !layer->sublayer_transform().IsPositiveScaleOrTranslation(); 825 826 num_descendants_that_draw_content += child_layer->DrawsContent() ? 1 : 0; 827 num_descendants_that_draw_content += 828 child_layer->draw_properties().num_descendants_that_draw_content; 829 830 if ((child_layer->DrawsContent() && !child_layer->CanClipSelf()) || 831 !child_layer->draw_properties().descendants_can_clip_selves || 832 sublayer_transform_prevents_clip || 833 !child_layer->transform().IsPositiveScaleOrTranslation()) 834 descendants_can_clip_selves = false; 835 } 836 837 recursive_data->Merge(data_for_child); 838 } 839 840 if (layer->HasCopyRequest()) 841 recursive_data->layer_or_descendant_has_copy_request = true; 842 843 layer->draw_properties().num_descendants_that_draw_content = 844 num_descendants_that_draw_content; 845 layer->draw_properties().descendants_can_clip_selves = 846 descendants_can_clip_selves; 847 layer->draw_properties().layer_or_descendant_has_copy_request = 848 recursive_data->layer_or_descendant_has_copy_request; 849 } 850 851 static void RoundTranslationComponents(gfx::Transform* transform) { 852 transform->matrix(). 853 setDouble(0, 3, MathUtil::Round(transform->matrix().getDouble(0, 3))); 854 transform->matrix(). 855 setDouble(1, 3, MathUtil::Round(transform->matrix().getDouble(1, 3))); 856 } 857 858 template <typename LayerType> 859 struct SubtreeGlobals { 860 LayerSorter* layer_sorter; 861 int max_texture_size; 862 float device_scale_factor; 863 float page_scale_factor; 864 LayerType* page_scale_application_layer; 865 bool can_adjust_raster_scales; 866 bool can_update_tile_priorities; 867 bool can_render_to_separate_surface; 868 }; 869 870 template<typename LayerType, typename RenderSurfaceType> 871 struct DataForRecursion { 872 // The accumulated sequence of transforms a layer will use to determine its 873 // own draw transform. 874 gfx::Transform parent_matrix; 875 876 // The accumulated sequence of transforms a layer will use to determine its 877 // own screen-space transform. 878 gfx::Transform full_hierarchy_matrix; 879 880 // The transform that removes all scrolling that may have occurred between a 881 // fixed-position layer and its container, so that the layer actually does 882 // remain fixed. 883 gfx::Transform scroll_compensation_matrix; 884 885 // The ancestor that would be the container for any fixed-position / sticky 886 // layers. 887 LayerType* fixed_container; 888 889 // This is the normal clip rect that is propagated from parent to child. 890 gfx::Rect clip_rect_in_target_space; 891 892 // When the layer's children want to compute their visible content rect, they 893 // want to know what their target surface's clip rect will be. BUT - they 894 // want to know this clip rect represented in their own target space. This 895 // requires inverse-projecting the surface's clip rect from the surface's 896 // render target space down to the surface's own space. Instead of computing 897 // this value redundantly for each child layer, it is computed only once 898 // while dealing with the parent layer, and then this precomputed value is 899 // passed down the recursion to the children that actually use it. 900 gfx::Rect clip_rect_of_target_surface_in_target_space; 901 902 bool ancestor_clips_subtree; 903 RenderSurfaceType* nearest_ancestor_surface_that_moves_pixels; 904 bool in_subtree_of_page_scale_application_layer; 905 bool subtree_can_use_lcd_text; 906 bool subtree_is_visible_from_ancestor; 907 }; 908 909 // Recursively walks the layer tree starting at the given node and computes all 910 // the necessary transformations, clip rects, render surfaces, etc. 911 template <typename LayerType, 912 typename LayerListType, 913 typename RenderSurfaceType> 914 static void CalculateDrawPropertiesInternal( 915 LayerType* layer, 916 const SubtreeGlobals<LayerType>& globals, 917 const DataForRecursion<LayerType, RenderSurfaceType>& data_from_ancestor, 918 LayerListType* render_surface_layer_list, 919 LayerListType* layer_list, 920 gfx::Rect* drawable_content_rect_of_subtree) { 921 // This function computes the new matrix transformations recursively for this 922 // layer and all its descendants. It also computes the appropriate render 923 // surfaces. 924 // Some important points to remember: 925 // 926 // 0. Here, transforms are notated in Matrix x Vector order, and in words we 927 // describe what the transform does from left to right. 928 // 929 // 1. In our terminology, the "layer origin" refers to the top-left corner of 930 // a layer, and the positive Y-axis points downwards. This interpretation is 931 // valid because the orthographic projection applied at draw time flips the Y 932 // axis appropriately. 933 // 934 // 2. The anchor point, when given as a PointF object, is specified in "unit 935 // layer space", where the bounds of the layer map to [0, 1]. However, as a 936 // Transform object, the transform to the anchor point is specified in "layer 937 // space", where the bounds of the layer map to [bounds.width(), 938 // bounds.height()]. 939 // 940 // 3. Definition of various transforms used: 941 // M[parent] is the parent matrix, with respect to the nearest render 942 // surface, passed down recursively. 943 // 944 // M[root] is the full hierarchy, with respect to the root, passed down 945 // recursively. 946 // 947 // Tr[origin] is the translation matrix from the parent's origin to 948 // this layer's origin. 949 // 950 // Tr[origin2anchor] is the translation from the layer's origin to its 951 // anchor point 952 // 953 // Tr[origin2center] is the translation from the layer's origin to its 954 // center 955 // 956 // M[layer] is the layer's matrix (applied at the anchor point) 957 // 958 // M[sublayer] is the layer's sublayer transform (also applied at the 959 // layer's anchor point) 960 // 961 // S[layer2content] is the ratio of a layer's content_bounds() to its 962 // Bounds(). 963 // 964 // Some composite transforms can help in understanding the sequence of 965 // transforms: 966 // composite_layer_transform = Tr[origin2anchor] * M[layer] * 967 // Tr[origin2anchor].inverse() 968 // 969 // composite_sublayer_transform = Tr[origin2anchor] * M[sublayer] * 970 // Tr[origin2anchor].inverse() 971 // 972 // 4. When a layer (or render surface) is drawn, it is drawn into a "target 973 // render surface". Therefore the draw transform does not necessarily 974 // transform from screen space to local layer space. Instead, the draw 975 // transform is the transform between the "target render surface space" and 976 // local layer space. Note that render surfaces, except for the root, also 977 // draw themselves into a different target render surface, and so their draw 978 // transform and origin transforms are also described with respect to the 979 // target. 980 // 981 // Using these definitions, then: 982 // 983 // The draw transform for the layer is: 984 // M[draw] = M[parent] * Tr[origin] * composite_layer_transform * 985 // S[layer2content] = M[parent] * Tr[layer->position() + anchor] * 986 // M[layer] * Tr[anchor2origin] * S[layer2content] 987 // 988 // Interpreting the math left-to-right, this transforms from the 989 // layer's render surface to the origin of the layer in content space. 990 // 991 // The screen space transform is: 992 // M[screenspace] = M[root] * Tr[origin] * composite_layer_transform * 993 // S[layer2content] 994 // = M[root] * Tr[layer->position() + anchor] * M[layer] 995 // * Tr[anchor2origin] * S[layer2content] 996 // 997 // Interpreting the math left-to-right, this transforms from the root 998 // render surface's content space to the origin of the layer in content 999 // space. 1000 // 1001 // The transform hierarchy that is passed on to children (i.e. the child's 1002 // parent_matrix) is: 1003 // M[parent]_for_child = M[parent] * Tr[origin] * 1004 // composite_layer_transform * composite_sublayer_transform 1005 // = M[parent] * Tr[layer->position() + anchor] * 1006 // M[layer] * Tr[anchor2origin] * 1007 // composite_sublayer_transform 1008 // 1009 // and a similar matrix for the full hierarchy with respect to the 1010 // root. 1011 // 1012 // Finally, note that the final matrix used by the shader for the layer is P * 1013 // M[draw] * S . This final product is computed in drawTexturedQuad(), where: 1014 // P is the projection matrix 1015 // S is the scale adjustment (to scale up a canonical quad to the 1016 // layer's size) 1017 // 1018 // When a render surface has a replica layer, that layer's transform is used 1019 // to draw a second copy of the surface. gfx::Transforms named here are 1020 // relative to the surface, unless they specify they are relative to the 1021 // replica layer. 1022 // 1023 // We will denote a scale by device scale S[deviceScale] 1024 // 1025 // The render surface draw transform to its target surface origin is: 1026 // M[surfaceDraw] = M[owningLayer->Draw] 1027 // 1028 // The render surface origin transform to its the root (screen space) origin 1029 // is: 1030 // M[surface2root] = M[owningLayer->screenspace] * 1031 // S[deviceScale].inverse() 1032 // 1033 // The replica draw transform to its target surface origin is: 1034 // M[replicaDraw] = S[deviceScale] * M[surfaceDraw] * 1035 // Tr[replica->position() + replica->anchor()] * Tr[replica] * 1036 // Tr[origin2anchor].inverse() * S[contents_scale].inverse() 1037 // 1038 // The replica draw transform to the root (screen space) origin is: 1039 // M[replica2root] = M[surface2root] * Tr[replica->position()] * 1040 // Tr[replica] * Tr[origin2anchor].inverse() 1041 // 1042 1043 // It makes no sense to have a non-unit page_scale_factor without specifying 1044 // which layer roots the subtree the scale is applied to. 1045 DCHECK(globals.page_scale_application_layer || 1046 (globals.page_scale_factor == 1.f)); 1047 1048 // If we early-exit anywhere in this function, the drawable_content_rect of 1049 // this subtree should be considered empty. 1050 *drawable_content_rect_of_subtree = gfx::Rect(); 1051 1052 DataForRecursion<LayerType, RenderSurfaceType> data_for_children; 1053 RenderSurfaceType* nearest_ancestor_surface_that_moves_pixels = 1054 data_from_ancestor.nearest_ancestor_surface_that_moves_pixels; 1055 data_for_children.in_subtree_of_page_scale_application_layer = 1056 data_from_ancestor.in_subtree_of_page_scale_application_layer; 1057 data_for_children.subtree_can_use_lcd_text = 1058 data_from_ancestor.subtree_can_use_lcd_text; 1059 1060 // Layers with a copy request are always visible, as well as un-hiding their 1061 // subtree. Otherise, layers that are marked as hidden will hide themselves 1062 // and their subtree. 1063 bool layer_is_visible = 1064 data_from_ancestor.subtree_is_visible_from_ancestor && 1065 !layer->hide_layer_and_subtree(); 1066 if (layer->HasCopyRequest()) 1067 layer_is_visible = true; 1068 1069 // The root layer cannot skip CalcDrawProperties. 1070 if (!IsRootLayer(layer) && SubtreeShouldBeSkipped(layer, layer_is_visible)) 1071 return; 1072 1073 // As this function proceeds, these are the properties for the current 1074 // layer that actually get computed. To avoid unnecessary copies 1075 // (particularly for matrices), we do computations directly on these values 1076 // when possible. 1077 DrawProperties<LayerType, RenderSurfaceType>& layer_draw_properties = 1078 layer->draw_properties(); 1079 1080 gfx::Rect clip_rect_in_target_space; 1081 bool layer_or_ancestor_clips_descendants = false; 1082 1083 // This value is cached on the stack so that we don't have to inverse-project 1084 // the surface's clip rect redundantly for every layer. This value is the 1085 // same as the target surface's clip rect, except that instead of being 1086 // described in the target surface's target's space, it is described in the 1087 // current render target's space. 1088 gfx::Rect clip_rect_of_target_surface_in_target_space; 1089 1090 float accumulated_draw_opacity = layer->opacity(); 1091 bool animating_opacity_to_target = layer->OpacityIsAnimating(); 1092 bool animating_opacity_to_screen = animating_opacity_to_target; 1093 if (layer->parent()) { 1094 accumulated_draw_opacity *= layer->parent()->draw_opacity(); 1095 animating_opacity_to_target |= layer->parent()->draw_opacity_is_animating(); 1096 animating_opacity_to_screen |= 1097 layer->parent()->screen_space_opacity_is_animating(); 1098 } 1099 1100 bool animating_transform_to_target = layer->TransformIsAnimating(); 1101 bool animating_transform_to_screen = animating_transform_to_target; 1102 if (layer->parent()) { 1103 animating_transform_to_target |= 1104 layer->parent()->draw_transform_is_animating(); 1105 animating_transform_to_screen |= 1106 layer->parent()->screen_space_transform_is_animating(); 1107 } 1108 1109 gfx::Size bounds = layer->bounds(); 1110 gfx::PointF anchor_point = layer->anchor_point(); 1111 gfx::PointF position = layer->position() - layer->TotalScrollOffset(); 1112 1113 gfx::Transform combined_transform = data_from_ancestor.parent_matrix; 1114 if (!layer->transform().IsIdentity()) { 1115 // LT = Tr[origin] * Tr[origin2anchor] 1116 combined_transform.Translate3d( 1117 position.x() + anchor_point.x() * bounds.width(), 1118 position.y() + anchor_point.y() * bounds.height(), 1119 layer->anchor_point_z()); 1120 // LT = Tr[origin] * Tr[origin2anchor] * M[layer] 1121 combined_transform.PreconcatTransform(layer->transform()); 1122 // LT = Tr[origin] * Tr[origin2anchor] * M[layer] * Tr[anchor2origin] 1123 combined_transform.Translate3d(-anchor_point.x() * bounds.width(), 1124 -anchor_point.y() * bounds.height(), 1125 -layer->anchor_point_z()); 1126 } else { 1127 combined_transform.Translate(position.x(), position.y()); 1128 } 1129 1130 if (!animating_transform_to_target && layer->scrollable() && 1131 combined_transform.IsScaleOrTranslation()) { 1132 // Align the scrollable layer's position to screen space pixels to avoid 1133 // blurriness. To avoid side-effects, do this only if the transform is 1134 // simple. 1135 RoundTranslationComponents(&combined_transform); 1136 } 1137 1138 // Apply adjustment from position constraints. 1139 ApplyPositionAdjustment(layer, data_from_ancestor.fixed_container, 1140 data_from_ancestor.scroll_compensation_matrix, &combined_transform); 1141 1142 // Compute the 2d scale components of the transform hierarchy up to the target 1143 // surface. From there, we can decide on a contents scale for the layer. 1144 float layer_scale_factors = globals.device_scale_factor; 1145 if (data_from_ancestor.in_subtree_of_page_scale_application_layer) 1146 layer_scale_factors *= globals.page_scale_factor; 1147 gfx::Vector2dF combined_transform_scales = 1148 MathUtil::ComputeTransform2dScaleComponents( 1149 combined_transform, 1150 layer_scale_factors); 1151 1152 float ideal_contents_scale = 1153 globals.can_adjust_raster_scales 1154 ? std::max(combined_transform_scales.x(), 1155 combined_transform_scales.y()) 1156 : layer_scale_factors; 1157 UpdateLayerContentsScale( 1158 layer, 1159 globals.can_adjust_raster_scales, 1160 ideal_contents_scale, 1161 globals.device_scale_factor, 1162 data_from_ancestor.in_subtree_of_page_scale_application_layer ? 1163 globals.page_scale_factor : 1.f, 1164 animating_transform_to_screen); 1165 1166 // The draw_transform that gets computed below is effectively the layer's 1167 // draw_transform, unless the layer itself creates a render_surface. In that 1168 // case, the render_surface re-parents the transforms. 1169 layer_draw_properties.target_space_transform = combined_transform; 1170 // M[draw] = M[parent] * LT * S[layer2content] 1171 layer_draw_properties.target_space_transform.Scale 1172 (1.f / layer->contents_scale_x(), 1.f / layer->contents_scale_y()); 1173 1174 // The layer's screen_space_transform represents the transform between root 1175 // layer's "screen space" and local content space. 1176 layer_draw_properties.screen_space_transform = 1177 data_from_ancestor.full_hierarchy_matrix; 1178 if (!layer->preserves_3d()) 1179 layer_draw_properties.screen_space_transform.FlattenTo2d(); 1180 layer_draw_properties.screen_space_transform.PreconcatTransform 1181 (layer_draw_properties.target_space_transform); 1182 1183 // Adjusting text AA method during animation may cause repaints, which in-turn 1184 // causes jank. 1185 bool adjust_text_aa = 1186 !animating_opacity_to_screen && !animating_transform_to_screen; 1187 // To avoid color fringing, LCD text should only be used on opaque layers with 1188 // just integral translation. 1189 bool layer_can_use_lcd_text = 1190 data_from_ancestor.subtree_can_use_lcd_text && 1191 accumulated_draw_opacity == 1.f && 1192 layer_draw_properties.target_space_transform. 1193 IsIdentityOrIntegerTranslation(); 1194 1195 gfx::RectF content_rect(layer->content_bounds()); 1196 1197 // full_hierarchy_matrix is the matrix that transforms objects between screen 1198 // space (except projection matrix) and the most recent RenderSurfaceImpl's 1199 // space. next_hierarchy_matrix will only change if this layer uses a new 1200 // RenderSurfaceImpl, otherwise remains the same. 1201 data_for_children.full_hierarchy_matrix = 1202 data_from_ancestor.full_hierarchy_matrix; 1203 1204 // If the subtree will scale layer contents by the transform hierarchy, then 1205 // we should scale things into the render surface by the transform hierarchy 1206 // to take advantage of that. 1207 gfx::Vector2dF render_surface_sublayer_scale = 1208 globals.can_adjust_raster_scales 1209 ? combined_transform_scales 1210 : gfx::Vector2dF(layer_scale_factors, layer_scale_factors); 1211 1212 bool render_to_separate_surface; 1213 if (globals.can_render_to_separate_surface) { 1214 render_to_separate_surface = SubtreeShouldRenderToSeparateSurface( 1215 layer, combined_transform.Preserves2dAxisAlignment()); 1216 } else { 1217 render_to_separate_surface = IsRootLayer(layer); 1218 } 1219 if (render_to_separate_surface) { 1220 // Check back-face visibility before continuing with this surface and its 1221 // subtree 1222 if (!layer->double_sided() && TransformToParentIsKnown(layer) && 1223 IsSurfaceBackFaceVisible(layer, combined_transform)) 1224 return; 1225 1226 RenderSurfaceType* render_surface = CreateOrReuseRenderSurface(layer); 1227 1228 if (IsRootLayer(layer)) { 1229 // The root layer's render surface size is predetermined and so the root 1230 // layer can't directly support non-identity transforms. It should just 1231 // forward top-level transforms to the rest of the tree. 1232 data_for_children.parent_matrix = combined_transform; 1233 1234 // The root surface does not contribute to any other surface, it has no 1235 // target. 1236 layer->render_surface()->set_contributes_to_drawn_surface(false); 1237 } else { 1238 // The owning layer's draw transform has a scale from content to layer 1239 // space which we do not want; so here we use the combined_transform 1240 // instead of the draw_transform. However, we do need to add a different 1241 // scale factor that accounts for the surface's pixel dimensions. 1242 combined_transform.Scale(1.0 / render_surface_sublayer_scale.x(), 1243 1.0 / render_surface_sublayer_scale.y()); 1244 render_surface->SetDrawTransform(combined_transform); 1245 1246 // The owning layer's transform was re-parented by the surface, so the 1247 // layer's new draw_transform only needs to scale the layer to surface 1248 // space. 1249 layer_draw_properties.target_space_transform.MakeIdentity(); 1250 layer_draw_properties.target_space_transform. 1251 Scale(render_surface_sublayer_scale.x() / layer->contents_scale_x(), 1252 render_surface_sublayer_scale.y() / layer->contents_scale_y()); 1253 1254 // Inside the surface's subtree, we scale everything to the owning layer's 1255 // scale. The sublayer matrix transforms layer rects into target surface 1256 // content space. Conceptually, all layers in the subtree inherit the 1257 // scale at the point of the render surface in the transform hierarchy, 1258 // but we apply it explicitly to the owning layer and the remainder of the 1259 // subtree independently. 1260 DCHECK(data_for_children.parent_matrix.IsIdentity()); 1261 data_for_children.parent_matrix.Scale(render_surface_sublayer_scale.x(), 1262 render_surface_sublayer_scale.y()); 1263 1264 layer->render_surface()->set_contributes_to_drawn_surface( 1265 data_from_ancestor.subtree_is_visible_from_ancestor && 1266 layer_is_visible); 1267 } 1268 1269 // The opacity value is moved from the layer to its surface, so that the 1270 // entire subtree properly inherits opacity. 1271 render_surface->SetDrawOpacity(accumulated_draw_opacity); 1272 render_surface->SetDrawOpacityIsAnimating(animating_opacity_to_target); 1273 animating_opacity_to_target = false; 1274 layer_draw_properties.opacity = 1.f; 1275 layer_draw_properties.opacity_is_animating = animating_opacity_to_target; 1276 layer_draw_properties.screen_space_opacity_is_animating = 1277 animating_opacity_to_screen; 1278 1279 render_surface->SetTargetSurfaceTransformsAreAnimating( 1280 animating_transform_to_target); 1281 render_surface->SetScreenSpaceTransformsAreAnimating( 1282 animating_transform_to_screen); 1283 animating_transform_to_target = false; 1284 layer_draw_properties.target_space_transform_is_animating = 1285 animating_transform_to_target; 1286 layer_draw_properties.screen_space_transform_is_animating = 1287 animating_transform_to_screen; 1288 1289 // Update the aggregate hierarchy matrix to include the transform of the 1290 // newly created RenderSurfaceImpl. 1291 data_for_children.full_hierarchy_matrix.PreconcatTransform( 1292 render_surface->draw_transform()); 1293 1294 // The new render_surface here will correctly clip the entire subtree. So, 1295 // we do not need to continue propagating the clipping state further down 1296 // the tree. This way, we can avoid transforming clip rects from ancestor 1297 // target surface space to current target surface space that could cause 1298 // more w < 0 headaches. 1299 layer_or_ancestor_clips_descendants = false; 1300 1301 if (layer->mask_layer()) { 1302 DrawProperties<LayerType, RenderSurfaceType>& mask_layer_draw_properties = 1303 layer->mask_layer()->draw_properties(); 1304 mask_layer_draw_properties.render_target = layer; 1305 mask_layer_draw_properties.visible_content_rect = 1306 gfx::Rect(layer->content_bounds()); 1307 } 1308 1309 if (layer->replica_layer() && layer->replica_layer()->mask_layer()) { 1310 DrawProperties<LayerType, RenderSurfaceType>& 1311 replica_mask_draw_properties = 1312 layer->replica_layer()->mask_layer()->draw_properties(); 1313 replica_mask_draw_properties.render_target = layer; 1314 replica_mask_draw_properties.visible_content_rect = 1315 gfx::Rect(layer->content_bounds()); 1316 } 1317 1318 // TODO(senorblanco): make this smarter for the SkImageFilter case (check 1319 // for pixel-moving filters) 1320 if (layer->filters().HasFilterThatMovesPixels() || layer->filter()) 1321 nearest_ancestor_surface_that_moves_pixels = render_surface; 1322 1323 // The render surface clip rect is expressed in the space where this surface 1324 // draws, i.e. the same space as 1325 // data_from_ancestor.clip_rect_in_target_space. 1326 render_surface->SetIsClipped(data_from_ancestor.ancestor_clips_subtree); 1327 if (data_from_ancestor.ancestor_clips_subtree) { 1328 render_surface->SetClipRect( 1329 data_from_ancestor.clip_rect_in_target_space); 1330 1331 gfx::Transform inverse_surface_draw_transform( 1332 gfx::Transform::kSkipInitialization); 1333 if (!render_surface->draw_transform().GetInverse( 1334 &inverse_surface_draw_transform)) { 1335 // TODO(shawnsingh): Either we need to handle uninvertible transforms 1336 // here, or DCHECK that the transform is invertible. 1337 } 1338 clip_rect_of_target_surface_in_target_space = 1339 gfx::ToEnclosingRect(MathUtil::ProjectClippedRect( 1340 inverse_surface_draw_transform, render_surface->clip_rect())); 1341 } else { 1342 render_surface->SetClipRect(gfx::Rect()); 1343 clip_rect_of_target_surface_in_target_space = 1344 data_from_ancestor.clip_rect_of_target_surface_in_target_space; 1345 } 1346 1347 render_surface->SetNearestAncestorThatMovesPixels( 1348 nearest_ancestor_surface_that_moves_pixels); 1349 1350 // If the new render surface is drawn translucent or with a non-integral 1351 // translation then the subtree that gets drawn on this render surface 1352 // cannot use LCD text. 1353 data_for_children.subtree_can_use_lcd_text = layer_can_use_lcd_text; 1354 1355 render_surface_layer_list->push_back(layer); 1356 } else { 1357 DCHECK(layer->parent()); 1358 1359 // Note: layer_draw_properties.target_space_transform is computed above, 1360 // before this if-else statement. 1361 layer_draw_properties.target_space_transform_is_animating = 1362 animating_transform_to_target; 1363 layer_draw_properties.screen_space_transform_is_animating = 1364 animating_transform_to_screen; 1365 layer_draw_properties.opacity = accumulated_draw_opacity; 1366 layer_draw_properties.opacity_is_animating = animating_opacity_to_target; 1367 layer_draw_properties.screen_space_opacity_is_animating = 1368 animating_opacity_to_screen; 1369 data_for_children.parent_matrix = combined_transform; 1370 1371 layer->ClearRenderSurface(); 1372 1373 // Layers without render_surfaces directly inherit the ancestor's clip 1374 // status. 1375 layer_or_ancestor_clips_descendants = 1376 data_from_ancestor.ancestor_clips_subtree; 1377 if (data_from_ancestor.ancestor_clips_subtree) { 1378 clip_rect_in_target_space = 1379 data_from_ancestor.clip_rect_in_target_space; 1380 } 1381 1382 // The surface's cached clip rect value propagates regardless of what 1383 // clipping goes on between layers here. 1384 clip_rect_of_target_surface_in_target_space = 1385 data_from_ancestor.clip_rect_of_target_surface_in_target_space; 1386 1387 // Layers that are not their own render_target will render into the target 1388 // of their nearest ancestor. 1389 layer_draw_properties.render_target = layer->parent()->render_target(); 1390 } 1391 1392 // Mark whether a layer could be drawn directly to the back buffer, for 1393 // example when it could use LCD text even though it's in a non-contents 1394 // opaque layer. This means that it can't be drawn to an intermediate 1395 // render target and also that no blending is applied to the layer as a whole 1396 // (meaning that its contents don't have to be pre-composited into a bitmap or 1397 // a render target). 1398 // 1399 // Ignoring animations is an optimization, 1400 // as it means that we're going to need some retained resources for this 1401 // layer in the near future even if its opacity is 1 now. 1402 layer_draw_properties.can_draw_directly_to_backbuffer = 1403 IsRootLayer(layer_draw_properties.render_target) && 1404 layer->draw_properties().opacity == 1.f && 1405 !animating_opacity_to_screen; 1406 1407 if (adjust_text_aa) 1408 layer_draw_properties.can_use_lcd_text = layer_can_use_lcd_text; 1409 1410 gfx::Rect rect_in_target_space = ToEnclosingRect( 1411 MathUtil::MapClippedRect(layer->draw_transform(), content_rect)); 1412 1413 if (LayerClipsSubtree(layer)) { 1414 layer_or_ancestor_clips_descendants = true; 1415 if (data_from_ancestor.ancestor_clips_subtree && !layer->render_surface()) { 1416 // A layer without render surface shares the same target as its ancestor. 1417 clip_rect_in_target_space = 1418 data_from_ancestor.clip_rect_in_target_space; 1419 clip_rect_in_target_space.Intersect(rect_in_target_space); 1420 } else { 1421 clip_rect_in_target_space = rect_in_target_space; 1422 } 1423 } 1424 1425 if (layer == globals.page_scale_application_layer) { 1426 data_for_children.parent_matrix.Scale( 1427 globals.page_scale_factor, 1428 globals.page_scale_factor); 1429 data_for_children.in_subtree_of_page_scale_application_layer = true; 1430 } 1431 1432 // Flatten to 2D if the layer doesn't preserve 3D. 1433 if (!layer->preserves_3d()) 1434 data_for_children.parent_matrix.FlattenTo2d(); 1435 1436 // Apply the sublayer transform at the anchor point of the layer. 1437 if (!layer->sublayer_transform().IsIdentity()) { 1438 data_for_children.parent_matrix.Translate( 1439 layer->anchor_point().x() * bounds.width(), 1440 layer->anchor_point().y() * bounds.height()); 1441 data_for_children.parent_matrix.PreconcatTransform( 1442 layer->sublayer_transform()); 1443 data_for_children.parent_matrix.Translate( 1444 -layer->anchor_point().x() * bounds.width(), 1445 -layer->anchor_point().y() * bounds.height()); 1446 } 1447 1448 LayerListType& descendants = 1449 (layer->render_surface() ? layer->render_surface()->layer_list() 1450 : *layer_list); 1451 1452 // Any layers that are appended after this point are in the layer's subtree 1453 // and should be included in the sorting process. 1454 size_t sorting_start_index = descendants.size(); 1455 1456 if (!LayerShouldBeSkipped(layer, layer_is_visible)) 1457 descendants.push_back(layer); 1458 1459 data_for_children.scroll_compensation_matrix = 1460 ComputeScrollCompensationMatrixForChildren( 1461 layer, 1462 data_from_ancestor.parent_matrix, 1463 data_from_ancestor.scroll_compensation_matrix); 1464 data_for_children.fixed_container = 1465 layer->IsContainerForFixedPositionLayers() ? 1466 layer : data_from_ancestor.fixed_container; 1467 1468 data_for_children.clip_rect_in_target_space = clip_rect_in_target_space; 1469 data_for_children.clip_rect_of_target_surface_in_target_space = 1470 clip_rect_of_target_surface_in_target_space; 1471 data_for_children.ancestor_clips_subtree = 1472 layer_or_ancestor_clips_descendants; 1473 data_for_children.nearest_ancestor_surface_that_moves_pixels = 1474 nearest_ancestor_surface_that_moves_pixels; 1475 data_for_children.subtree_is_visible_from_ancestor = layer_is_visible; 1476 1477 gfx::Rect accumulated_drawable_content_rect_of_children; 1478 for (size_t i = 0; i < layer->children().size(); ++i) { 1479 LayerType* child = 1480 LayerTreeHostCommon::get_child_as_raw_ptr(layer->children(), i); 1481 gfx::Rect drawable_content_rect_of_child_subtree; 1482 gfx::Transform identity_matrix; 1483 CalculateDrawPropertiesInternal<LayerType, 1484 LayerListType, 1485 RenderSurfaceType>( 1486 child, 1487 globals, 1488 data_for_children, 1489 render_surface_layer_list, 1490 &descendants, 1491 &drawable_content_rect_of_child_subtree); 1492 if (!drawable_content_rect_of_child_subtree.IsEmpty()) { 1493 accumulated_drawable_content_rect_of_children.Union( 1494 drawable_content_rect_of_child_subtree); 1495 if (child->render_surface()) 1496 descendants.push_back(child); 1497 } 1498 } 1499 1500 if (layer->render_surface() && !IsRootLayer(layer) && 1501 layer->render_surface()->layer_list().empty()) { 1502 RemoveSurfaceForEarlyExit(layer, render_surface_layer_list); 1503 return; 1504 } 1505 1506 // Compute the total drawable_content_rect for this subtree (the rect is in 1507 // target surface space). 1508 gfx::Rect local_drawable_content_rect_of_subtree = 1509 accumulated_drawable_content_rect_of_children; 1510 if (layer->DrawsContent()) 1511 local_drawable_content_rect_of_subtree.Union(rect_in_target_space); 1512 if (layer_or_ancestor_clips_descendants) 1513 local_drawable_content_rect_of_subtree.Intersect(clip_rect_in_target_space); 1514 1515 // Compute the layer's drawable content rect (the rect is in target surface 1516 // space). 1517 layer_draw_properties.drawable_content_rect = rect_in_target_space; 1518 if (layer_or_ancestor_clips_descendants) { 1519 layer_draw_properties.drawable_content_rect. 1520 Intersect(clip_rect_in_target_space); 1521 } 1522 1523 // Tell the layer the rect that is clipped by. In theory we could use a 1524 // tighter clip rect here (drawable_content_rect), but that actually does not 1525 // reduce how much would be drawn, and instead it would create unnecessary 1526 // changes to scissor state affecting GPU performance. 1527 layer_draw_properties.is_clipped = layer_or_ancestor_clips_descendants; 1528 if (layer_or_ancestor_clips_descendants) { 1529 layer_draw_properties.clip_rect = clip_rect_in_target_space; 1530 } else { 1531 // Initialize the clip rect to a safe value that will not clip the 1532 // layer, just in case clipping is still accidentally used. 1533 layer_draw_properties.clip_rect = rect_in_target_space; 1534 } 1535 1536 // Compute the layer's visible content rect (the rect is in content space). 1537 layer_draw_properties.visible_content_rect = CalculateVisibleContentRect( 1538 layer, clip_rect_of_target_surface_in_target_space, rect_in_target_space); 1539 1540 // Compute the remaining properties for the render surface, if the layer has 1541 // one. 1542 if (IsRootLayer(layer)) { 1543 // The root layer's surface's content_rect is always the entire viewport. 1544 DCHECK(layer->render_surface()); 1545 layer->render_surface()->SetContentRect( 1546 data_from_ancestor.clip_rect_in_target_space); 1547 } else if (layer->render_surface() && !IsRootLayer(layer)) { 1548 RenderSurfaceType* render_surface = layer->render_surface(); 1549 gfx::Rect clipped_content_rect = local_drawable_content_rect_of_subtree; 1550 1551 // Don't clip if the layer is reflected as the reflection shouldn't be 1552 // clipped. If the layer is animating, then the surface's transform to 1553 // its target is not known on the main thread, and we should not use it 1554 // to clip. 1555 if (!layer->replica_layer() && TransformToParentIsKnown(layer)) { 1556 // Note, it is correct to use data_from_ancestor.ancestor_clips_subtree 1557 // here, because we are looking at this layer's render_surface, not the 1558 // layer itself. 1559 if (data_from_ancestor.ancestor_clips_subtree && 1560 !clipped_content_rect.IsEmpty()) { 1561 gfx::Rect surface_clip_rect = LayerTreeHostCommon::CalculateVisibleRect( 1562 render_surface->clip_rect(), 1563 clipped_content_rect, 1564 render_surface->draw_transform()); 1565 clipped_content_rect.Intersect(surface_clip_rect); 1566 } 1567 } 1568 1569 // The RenderSurfaceImpl backing texture cannot exceed the maximum supported 1570 // texture size. 1571 clipped_content_rect.set_width( 1572 std::min(clipped_content_rect.width(), globals.max_texture_size)); 1573 clipped_content_rect.set_height( 1574 std::min(clipped_content_rect.height(), globals.max_texture_size)); 1575 1576 if (clipped_content_rect.IsEmpty()) { 1577 RemoveSurfaceForEarlyExit(layer, render_surface_layer_list); 1578 return; 1579 } 1580 1581 render_surface->SetContentRect(clipped_content_rect); 1582 1583 // The owning layer's screen_space_transform has a scale from content to 1584 // layer space which we need to undo and replace with a scale from the 1585 // surface's subtree into layer space. 1586 gfx::Transform screen_space_transform = layer->screen_space_transform(); 1587 screen_space_transform.Scale( 1588 layer->contents_scale_x() / render_surface_sublayer_scale.x(), 1589 layer->contents_scale_y() / render_surface_sublayer_scale.y()); 1590 render_surface->SetScreenSpaceTransform(screen_space_transform); 1591 1592 if (layer->replica_layer()) { 1593 gfx::Transform surface_origin_to_replica_origin_transform; 1594 surface_origin_to_replica_origin_transform.Scale( 1595 render_surface_sublayer_scale.x(), render_surface_sublayer_scale.y()); 1596 surface_origin_to_replica_origin_transform.Translate( 1597 layer->replica_layer()->position().x() + 1598 layer->replica_layer()->anchor_point().x() * bounds.width(), 1599 layer->replica_layer()->position().y() + 1600 layer->replica_layer()->anchor_point().y() * bounds.height()); 1601 surface_origin_to_replica_origin_transform.PreconcatTransform( 1602 layer->replica_layer()->transform()); 1603 surface_origin_to_replica_origin_transform.Translate( 1604 -layer->replica_layer()->anchor_point().x() * bounds.width(), 1605 -layer->replica_layer()->anchor_point().y() * bounds.height()); 1606 surface_origin_to_replica_origin_transform.Scale( 1607 1.0 / render_surface_sublayer_scale.x(), 1608 1.0 / render_surface_sublayer_scale.y()); 1609 1610 // Compute the replica's "originTransform" that maps from the replica's 1611 // origin space to the target surface origin space. 1612 gfx::Transform replica_origin_transform = 1613 layer->render_surface()->draw_transform() * 1614 surface_origin_to_replica_origin_transform; 1615 render_surface->SetReplicaDrawTransform(replica_origin_transform); 1616 1617 // Compute the replica's "screen_space_transform" that maps from the 1618 // replica's origin space to the screen's origin space. 1619 gfx::Transform replica_screen_space_transform = 1620 layer->render_surface()->screen_space_transform() * 1621 surface_origin_to_replica_origin_transform; 1622 render_surface->SetReplicaScreenSpaceTransform( 1623 replica_screen_space_transform); 1624 } 1625 } 1626 1627 if (globals.can_update_tile_priorities) 1628 UpdateTilePrioritiesForLayer(layer); 1629 SavePaintPropertiesLayer(layer); 1630 1631 // If neither this layer nor any of its children were added, early out. 1632 if (sorting_start_index == descendants.size()) 1633 return; 1634 1635 // If preserves-3d then sort all the descendants in 3D so that they can be 1636 // drawn from back to front. If the preserves-3d property is also set on the 1637 // parent then skip the sorting as the parent will sort all the descendants 1638 // anyway. 1639 if (globals.layer_sorter && descendants.size() && layer->preserves_3d() && 1640 (!layer->parent() || !layer->parent()->preserves_3d())) { 1641 SortLayers(descendants.begin() + sorting_start_index, 1642 descendants.end(), 1643 globals.layer_sorter); 1644 } 1645 1646 if (layer->render_surface()) { 1647 *drawable_content_rect_of_subtree = 1648 gfx::ToEnclosingRect(layer->render_surface()->DrawableContentRect()); 1649 } else { 1650 *drawable_content_rect_of_subtree = local_drawable_content_rect_of_subtree; 1651 } 1652 1653 if (layer->HasContributingDelegatedRenderPasses()) { 1654 layer->render_target()->render_surface()-> 1655 AddContributingDelegatedRenderPassLayer(layer); 1656 } 1657 } 1658 1659 void LayerTreeHostCommon::CalculateDrawProperties( 1660 CalcDrawPropsMainInputs* inputs) { 1661 DCHECK(inputs->root_layer); 1662 DCHECK(IsRootLayer(inputs->root_layer)); 1663 DCHECK(inputs->render_surface_layer_list); 1664 gfx::Rect total_drawable_content_rect; 1665 gfx::Transform identity_matrix; 1666 gfx::Transform scaled_device_transform = inputs->device_transform; 1667 scaled_device_transform.Scale(inputs->device_scale_factor, 1668 inputs->device_scale_factor); 1669 RenderSurfaceLayerList dummy_layer_list; 1670 1671 // The root layer's render_surface should receive the device viewport as the 1672 // initial clip rect. 1673 gfx::Rect device_viewport_rect(inputs->device_viewport_size); 1674 1675 SubtreeGlobals<Layer> globals; 1676 globals.layer_sorter = NULL; 1677 globals.max_texture_size = inputs->max_texture_size; 1678 globals.device_scale_factor = inputs->device_scale_factor; 1679 globals.page_scale_factor = inputs->page_scale_factor; 1680 globals.page_scale_application_layer = inputs->page_scale_application_layer; 1681 globals.can_render_to_separate_surface = 1682 inputs->can_render_to_separate_surface; 1683 globals.can_adjust_raster_scales = inputs->can_adjust_raster_scales; 1684 globals.can_update_tile_priorities = inputs->can_update_tile_priorities; 1685 1686 DataForRecursion<Layer, RenderSurface> data_for_recursion; 1687 data_for_recursion.parent_matrix = scaled_device_transform; 1688 data_for_recursion.full_hierarchy_matrix = identity_matrix; 1689 data_for_recursion.scroll_compensation_matrix = identity_matrix; 1690 data_for_recursion.fixed_container = inputs->root_layer; 1691 data_for_recursion.clip_rect_in_target_space = device_viewport_rect; 1692 data_for_recursion.clip_rect_of_target_surface_in_target_space = 1693 device_viewport_rect; 1694 data_for_recursion.ancestor_clips_subtree = true; 1695 data_for_recursion.nearest_ancestor_surface_that_moves_pixels = NULL; 1696 data_for_recursion.in_subtree_of_page_scale_application_layer = false; 1697 data_for_recursion.subtree_can_use_lcd_text = inputs->can_use_lcd_text; 1698 data_for_recursion.subtree_is_visible_from_ancestor = true; 1699 1700 PreCalculateMetaInformationRecursiveData recursive_data; 1701 PreCalculateMetaInformation(inputs->root_layer, &recursive_data); 1702 1703 CalculateDrawPropertiesInternal<Layer, RenderSurfaceLayerList, RenderSurface>( 1704 inputs->root_layer, 1705 globals, 1706 data_for_recursion, 1707 inputs->render_surface_layer_list, 1708 &dummy_layer_list, 1709 &total_drawable_content_rect); 1710 1711 // The dummy layer list should not have been used. 1712 DCHECK_EQ(0u, dummy_layer_list.size()); 1713 // A root layer render_surface should always exist after 1714 // CalculateDrawProperties. 1715 DCHECK(inputs->root_layer->render_surface()); 1716 } 1717 1718 void LayerTreeHostCommon::CalculateDrawProperties( 1719 CalcDrawPropsImplInputs* inputs) { 1720 DCHECK(inputs->root_layer); 1721 DCHECK(IsRootLayer(inputs->root_layer)); 1722 DCHECK(inputs->render_surface_layer_list); 1723 1724 gfx::Rect total_drawable_content_rect; 1725 gfx::Transform identity_matrix; 1726 gfx::Transform scaled_device_transform = inputs->device_transform; 1727 scaled_device_transform.Scale(inputs->device_scale_factor, 1728 inputs->device_scale_factor); 1729 LayerImplList dummy_layer_list; 1730 LayerSorter layer_sorter; 1731 1732 // The root layer's render_surface should receive the device viewport as the 1733 // initial clip rect. 1734 gfx::Rect device_viewport_rect(inputs->device_viewport_size); 1735 1736 SubtreeGlobals<LayerImpl> globals; 1737 globals.layer_sorter = &layer_sorter; 1738 globals.max_texture_size = inputs->max_texture_size; 1739 globals.device_scale_factor = inputs->device_scale_factor; 1740 globals.page_scale_factor = inputs->page_scale_factor; 1741 globals.page_scale_application_layer = inputs->page_scale_application_layer; 1742 globals.can_render_to_separate_surface = 1743 inputs->can_render_to_separate_surface; 1744 globals.can_adjust_raster_scales = inputs->can_adjust_raster_scales; 1745 globals.can_update_tile_priorities = inputs->can_update_tile_priorities; 1746 1747 DataForRecursion<LayerImpl, RenderSurfaceImpl> data_for_recursion; 1748 data_for_recursion.parent_matrix = scaled_device_transform; 1749 data_for_recursion.full_hierarchy_matrix = identity_matrix; 1750 data_for_recursion.scroll_compensation_matrix = identity_matrix; 1751 data_for_recursion.fixed_container = inputs->root_layer; 1752 data_for_recursion.clip_rect_in_target_space = device_viewport_rect; 1753 data_for_recursion.clip_rect_of_target_surface_in_target_space = 1754 device_viewport_rect; 1755 data_for_recursion.ancestor_clips_subtree = true; 1756 data_for_recursion.nearest_ancestor_surface_that_moves_pixels = NULL; 1757 data_for_recursion.in_subtree_of_page_scale_application_layer = false; 1758 data_for_recursion.subtree_can_use_lcd_text = inputs->can_use_lcd_text; 1759 data_for_recursion.subtree_is_visible_from_ancestor = true; 1760 1761 PreCalculateMetaInformationRecursiveData recursive_data; 1762 PreCalculateMetaInformation(inputs->root_layer, &recursive_data); 1763 1764 CalculateDrawPropertiesInternal<LayerImpl, LayerImplList, RenderSurfaceImpl>( 1765 inputs->root_layer, 1766 globals, 1767 data_for_recursion, 1768 inputs->render_surface_layer_list, 1769 &dummy_layer_list, 1770 &total_drawable_content_rect); 1771 1772 // The dummy layer list should not have been used. 1773 DCHECK_EQ(0u, dummy_layer_list.size()); 1774 // A root layer render_surface should always exist after 1775 // CalculateDrawProperties. 1776 DCHECK(inputs->root_layer->render_surface()); 1777 } 1778 1779 static bool PointHitsRect( 1780 gfx::PointF screen_space_point, 1781 const gfx::Transform& local_space_to_screen_space_transform, 1782 gfx::RectF local_space_rect) { 1783 // If the transform is not invertible, then assume that this point doesn't hit 1784 // this rect. 1785 gfx::Transform inverse_local_space_to_screen_space( 1786 gfx::Transform::kSkipInitialization); 1787 if (!local_space_to_screen_space_transform.GetInverse( 1788 &inverse_local_space_to_screen_space)) 1789 return false; 1790 1791 // Transform the hit test point from screen space to the local space of the 1792 // given rect. 1793 bool clipped = false; 1794 gfx::PointF hit_test_point_in_local_space = MathUtil::ProjectPoint( 1795 inverse_local_space_to_screen_space, screen_space_point, &clipped); 1796 1797 // If ProjectPoint could not project to a valid value, then we assume that 1798 // this point doesn't hit this rect. 1799 if (clipped) 1800 return false; 1801 1802 return local_space_rect.Contains(hit_test_point_in_local_space); 1803 } 1804 1805 static bool PointHitsRegion(gfx::PointF screen_space_point, 1806 const gfx::Transform& screen_space_transform, 1807 const Region& layer_space_region, 1808 float layer_content_scale_x, 1809 float layer_content_scale_y) { 1810 // If the transform is not invertible, then assume that this point doesn't hit 1811 // this region. 1812 gfx::Transform inverse_screen_space_transform( 1813 gfx::Transform::kSkipInitialization); 1814 if (!screen_space_transform.GetInverse(&inverse_screen_space_transform)) 1815 return false; 1816 1817 // Transform the hit test point from screen space to the local space of the 1818 // given region. 1819 bool clipped = false; 1820 gfx::PointF hit_test_point_in_content_space = MathUtil::ProjectPoint( 1821 inverse_screen_space_transform, screen_space_point, &clipped); 1822 gfx::PointF hit_test_point_in_layer_space = 1823 gfx::ScalePoint(hit_test_point_in_content_space, 1824 1.f / layer_content_scale_x, 1825 1.f / layer_content_scale_y); 1826 1827 // If ProjectPoint could not project to a valid value, then we assume that 1828 // this point doesn't hit this region. 1829 if (clipped) 1830 return false; 1831 1832 return layer_space_region.Contains( 1833 gfx::ToRoundedPoint(hit_test_point_in_layer_space)); 1834 } 1835 1836 static bool PointIsClippedBySurfaceOrClipRect(gfx::PointF screen_space_point, 1837 LayerImpl* layer) { 1838 LayerImpl* current_layer = layer; 1839 1840 // Walk up the layer tree and hit-test any render_surfaces and any layer 1841 // clip rects that are active. 1842 while (current_layer) { 1843 if (current_layer->render_surface() && 1844 !PointHitsRect( 1845 screen_space_point, 1846 current_layer->render_surface()->screen_space_transform(), 1847 current_layer->render_surface()->content_rect())) 1848 return true; 1849 1850 // Note that drawable content rects are actually in target surface space, so 1851 // the transform we have to provide is the target surface's 1852 // screen_space_transform. 1853 LayerImpl* render_target = current_layer->render_target(); 1854 if (LayerClipsSubtree(current_layer) && 1855 !PointHitsRect( 1856 screen_space_point, 1857 render_target->render_surface()->screen_space_transform(), 1858 current_layer->drawable_content_rect())) 1859 return true; 1860 1861 current_layer = current_layer->parent(); 1862 } 1863 1864 // If we have finished walking all ancestors without having already exited, 1865 // then the point is not clipped by any ancestors. 1866 return false; 1867 } 1868 1869 LayerImpl* LayerTreeHostCommon::FindLayerThatIsHitByPoint( 1870 gfx::PointF screen_space_point, 1871 const LayerImplList& render_surface_layer_list) { 1872 LayerImpl* found_layer = NULL; 1873 1874 typedef LayerIterator<LayerImpl, 1875 LayerImplList, 1876 RenderSurfaceImpl, 1877 LayerIteratorActions::FrontToBack> LayerIteratorType; 1878 LayerIteratorType end = LayerIteratorType::End(&render_surface_layer_list); 1879 1880 for (LayerIteratorType 1881 it = LayerIteratorType::Begin(&render_surface_layer_list); 1882 it != end; 1883 ++it) { 1884 // We don't want to consider render_surfaces for hit testing. 1885 if (!it.represents_itself()) 1886 continue; 1887 1888 LayerImpl* current_layer = (*it); 1889 1890 gfx::RectF content_rect(current_layer->content_bounds()); 1891 if (!PointHitsRect(screen_space_point, 1892 current_layer->screen_space_transform(), 1893 content_rect)) 1894 continue; 1895 1896 // At this point, we think the point does hit the layer, but we need to walk 1897 // up the parents to ensure that the layer was not clipped in such a way 1898 // that the hit point actually should not hit the layer. 1899 if (PointIsClippedBySurfaceOrClipRect(screen_space_point, current_layer)) 1900 continue; 1901 1902 // Skip the HUD layer. 1903 if (current_layer == current_layer->layer_tree_impl()->hud_layer()) 1904 continue; 1905 1906 found_layer = current_layer; 1907 break; 1908 } 1909 1910 // This can potentially return NULL, which means the screen_space_point did 1911 // not successfully hit test any layers, not even the root layer. 1912 return found_layer; 1913 } 1914 1915 LayerImpl* LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( 1916 gfx::PointF screen_space_point, 1917 const LayerImplList& render_surface_layer_list) { 1918 LayerImpl* found_layer = NULL; 1919 1920 typedef LayerIterator<LayerImpl, 1921 LayerImplList, 1922 RenderSurfaceImpl, 1923 LayerIteratorActions::FrontToBack> LayerIteratorType; 1924 LayerIteratorType end = LayerIteratorType::End(&render_surface_layer_list); 1925 1926 for (LayerIteratorType 1927 it = LayerIteratorType::Begin(&render_surface_layer_list); 1928 it != end; 1929 ++it) { 1930 // We don't want to consider render_surfaces for hit testing. 1931 if (!it.represents_itself()) 1932 continue; 1933 1934 LayerImpl* current_layer = (*it); 1935 1936 if (!LayerHasTouchEventHandlersAt(screen_space_point, current_layer)) 1937 continue; 1938 1939 found_layer = current_layer; 1940 break; 1941 } 1942 1943 // This can potentially return NULL, which means the screen_space_point did 1944 // not successfully hit test any layers, not even the root layer. 1945 return found_layer; 1946 } 1947 1948 bool LayerTreeHostCommon::LayerHasTouchEventHandlersAt( 1949 gfx::PointF screen_space_point, 1950 LayerImpl* layer_impl) { 1951 if (layer_impl->touch_event_handler_region().IsEmpty()) 1952 return false; 1953 1954 if (!PointHitsRegion(screen_space_point, 1955 layer_impl->screen_space_transform(), 1956 layer_impl->touch_event_handler_region(), 1957 layer_impl->contents_scale_x(), 1958 layer_impl->contents_scale_y())) 1959 return false; 1960 1961 // At this point, we think the point does hit the touch event handler region 1962 // on the layer, but we need to walk up the parents to ensure that the layer 1963 // was not clipped in such a way that the hit point actually should not hit 1964 // the layer. 1965 if (PointIsClippedBySurfaceOrClipRect(screen_space_point, layer_impl)) 1966 return false; 1967 1968 return true; 1969 } 1970 } // namespace cc 1971