[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 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 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 5 | #include "config.h" |
| 6 | |
[email protected] | d50c686 | 2012-10-23 02:08:31 | [diff] [blame] | 7 | #include "cc/layer_tree_host_common.h" |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 8 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 9 | #include "FloatQuad.h" |
| 10 | #include "IntRect.h" |
[email protected] | a8461d8 | 2012-10-16 21:11:14 | [diff] [blame] | 11 | #include "cc/layer.h" |
[email protected] | d50c686 | 2012-10-23 02:08:31 | [diff] [blame] | 12 | #include "cc/layer_impl.h" |
| 13 | #include "cc/layer_iterator.h" |
| 14 | #include "cc/layer_sorter.h" |
[email protected] | 55a124d0 | 2012-10-22 03:07:13 | [diff] [blame] | 15 | #include "cc/math_util.h" |
[email protected] | a8461d8 | 2012-10-16 21:11:14 | [diff] [blame] | 16 | #include "cc/render_surface.h" |
[email protected] | d50c686 | 2012-10-23 02:08:31 | [diff] [blame] | 17 | #include "cc/render_surface_impl.h" |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 18 | #include <public/WebTransformationMatrix.h> |
| 19 | |
| 20 | using WebKit::WebTransformationMatrix; |
| 21 | |
[email protected] | 9c88e56 | 2012-09-14 22:21:30 | [diff] [blame] | 22 | namespace cc { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 23 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 24 | ScrollAndScaleSet::ScrollAndScaleSet() |
[email protected] | 49306751 | 2012-09-19 23:34:10 | [diff] [blame] | 25 | { |
| 26 | } |
| 27 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 28 | ScrollAndScaleSet::~ScrollAndScaleSet() |
[email protected] | 49306751 | 2012-09-19 23:34:10 | [diff] [blame] | 29 | { |
| 30 | } |
| 31 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 32 | IntRect LayerTreeHostCommon::calculateVisibleRect(const IntRect& targetSurfaceRect, const IntRect& layerBoundRect, const WebTransformationMatrix& transform) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 33 | { |
| 34 | // Is this layer fully contained within the target surface? |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 35 | IntRect layerInSurfaceSpace = MathUtil::mapClippedRect(transform, layerBoundRect); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 36 | if (targetSurfaceRect.contains(layerInSurfaceSpace)) |
| 37 | return layerBoundRect; |
| 38 | |
| 39 | // If the layer doesn't fill up the entire surface, then find the part of |
| 40 | // the surface rect where the layer could be visible. This avoids trying to |
| 41 | // project surface rect points that are behind the projection point. |
| 42 | IntRect minimalSurfaceRect = targetSurfaceRect; |
| 43 | minimalSurfaceRect.intersect(layerInSurfaceSpace); |
| 44 | |
| 45 | // Project the corners of the target surface rect into the layer space. |
| 46 | // This bounding rectangle may be larger than it needs to be (being |
| 47 | // axis-aligned), but is a reasonable filter on the space to consider. |
| 48 | // Non-invertible transforms will create an empty rect here. |
| 49 | const WebTransformationMatrix surfaceToLayer = transform.inverse(); |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 50 | IntRect layerRect = enclosingIntRect(MathUtil::projectClippedRect(surfaceToLayer, FloatRect(minimalSurfaceRect))); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 51 | layerRect.intersect(layerBoundRect); |
| 52 | return layerRect; |
| 53 | } |
| 54 | |
| 55 | template<typename LayerType> |
| 56 | static inline bool layerIsInExisting3DRenderingContext(LayerType* layer) |
| 57 | { |
| 58 | // According to current W3C spec on CSS transforms, a layer is part of an established |
| 59 | // 3d rendering context if its parent has transform-style of preserves-3d. |
| 60 | return layer->parent() && layer->parent()->preserves3D(); |
| 61 | } |
| 62 | |
| 63 | template<typename LayerType> |
| 64 | static bool layerIsRootOfNewRenderingContext(LayerType* layer) |
| 65 | { |
| 66 | // According to current W3C spec on CSS transforms (Section 6.1), a layer is the |
| 67 | // beginning of 3d rendering context if its parent does not have transform-style: |
| 68 | // preserve-3d, but this layer itself does. |
| 69 | if (layer->parent()) |
| 70 | return !layer->parent()->preserves3D() && layer->preserves3D(); |
| 71 | |
| 72 | return layer->preserves3D(); |
| 73 | } |
| 74 | |
| 75 | template<typename LayerType> |
| 76 | static bool isLayerBackFaceVisible(LayerType* layer) |
| 77 | { |
| 78 | // The current W3C spec on CSS transforms says that backface visibility should be |
| 79 | // determined differently depending on whether the layer is in a "3d rendering |
| 80 | // context" or not. For Chromium code, we can determine whether we are in a 3d |
| 81 | // rendering context by checking if the parent preserves 3d. |
| 82 | |
| 83 | if (layerIsInExisting3DRenderingContext(layer)) |
| 84 | return layer->drawTransform().isBackFaceVisible(); |
| 85 | |
| 86 | // In this case, either the layer establishes a new 3d rendering context, or is not in |
| 87 | // a 3d rendering context at all. |
| 88 | return layer->transform().isBackFaceVisible(); |
| 89 | } |
| 90 | |
| 91 | template<typename LayerType> |
| 92 | static bool isSurfaceBackFaceVisible(LayerType* layer, const WebTransformationMatrix& drawTransform) |
| 93 | { |
| 94 | if (layerIsInExisting3DRenderingContext(layer)) |
| 95 | return drawTransform.isBackFaceVisible(); |
| 96 | |
| 97 | if (layerIsRootOfNewRenderingContext(layer)) |
| 98 | return layer->transform().isBackFaceVisible(); |
| 99 | |
| 100 | // If the renderSurface is not part of a new or existing rendering context, then the |
| 101 | // layers that contribute to this surface will decide back-face visibility for themselves. |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | template<typename LayerType> |
| 106 | static inline bool layerClipsSubtree(LayerType* layer) |
| 107 | { |
| 108 | return layer->masksToBounds() || layer->maskLayer(); |
| 109 | } |
| 110 | |
| 111 | template<typename LayerType> |
| 112 | static IntRect calculateVisibleContentRect(LayerType* layer) |
| 113 | { |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 114 | DCHECK(layer->renderTarget()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 115 | |
[email protected] | 9bab0bb | 2012-10-08 19:11:58 | [diff] [blame] | 116 | // Nothing is visible if the layer bounds are empty. |
| 117 | if (!layer->drawsContent() || layer->contentBounds().isEmpty() || layer->drawableContentRect().isEmpty()) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 118 | return IntRect(); |
| 119 | |
[email protected] | 9bab0bb | 2012-10-08 19:11:58 | [diff] [blame] | 120 | IntRect targetSurfaceClipRect; |
| 121 | |
| 122 | // First, compute visible bounds in target surface space. |
| 123 | if (layer->renderTarget()->renderSurface()->clipRect().isEmpty()) |
| 124 | targetSurfaceClipRect = layer->drawableContentRect(); |
| 125 | else { |
| 126 | // In this case the target surface does clip layers that contribute to it. So, we |
| 127 | // have convert the current surface's clipRect from its ancestor surface space to |
| 128 | // the current surface space. |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 129 | targetSurfaceClipRect = enclosingIntRect(MathUtil::projectClippedRect(layer->renderTarget()->renderSurface()->drawTransform().inverse(), layer->renderTarget()->renderSurface()->clipRect())); |
[email protected] | 9bab0bb | 2012-10-08 19:11:58 | [diff] [blame] | 130 | targetSurfaceClipRect.intersect(layer->drawableContentRect()); |
| 131 | } |
| 132 | |
| 133 | if (targetSurfaceClipRect.isEmpty()) |
| 134 | return IntRect(); |
| 135 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 136 | return LayerTreeHostCommon::calculateVisibleRect(targetSurfaceClipRect, IntRect(IntPoint(), layer->contentBounds()), layer->drawTransform()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | static bool isScaleOrTranslation(const WebTransformationMatrix& m) |
| 140 | { |
| 141 | return !m.m12() && !m.m13() && !m.m14() |
| 142 | && !m.m21() && !m.m23() && !m.m24() |
| 143 | && !m.m31() && !m.m32() && !m.m43() |
| 144 | && m.m44(); |
| 145 | } |
| 146 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 147 | static inline bool transformToParentIsKnown(LayerImpl*) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 148 | { |
| 149 | return true; |
| 150 | } |
| 151 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 152 | static inline bool transformToParentIsKnown(Layer* layer) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 153 | { |
| 154 | return !layer->transformIsAnimating(); |
| 155 | } |
| 156 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 157 | static inline bool transformToScreenIsKnown(LayerImpl*) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 158 | { |
| 159 | return true; |
| 160 | } |
| 161 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 162 | static inline bool transformToScreenIsKnown(Layer* layer) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 163 | { |
| 164 | return !layer->screenSpaceTransformIsAnimating(); |
| 165 | } |
| 166 | |
| 167 | template<typename LayerType> |
| 168 | static bool layerShouldBeSkipped(LayerType* layer) |
| 169 | { |
| 170 | // Layers can be skipped if any of these conditions are met. |
| 171 | // - does not draw content. |
| 172 | // - is transparent |
| 173 | // - has empty bounds |
| 174 | // - the layer is not double-sided, but its back face is visible. |
| 175 | // |
| 176 | // Some additional conditions need to be computed at a later point after the recursion is finished. |
| 177 | // - the intersection of render surface content and layer clipRect is empty |
| 178 | // - the visibleContentRect is empty |
| 179 | // |
| 180 | // Note, if the layer should not have been drawn due to being fully transparent, |
| 181 | // we would have skipped the entire subtree and never made it into this function, |
| 182 | // so it is safe to omit this check here. |
| 183 | |
| 184 | if (!layer->drawsContent() || layer->bounds().isEmpty()) |
| 185 | return true; |
| 186 | |
| 187 | LayerType* backfaceTestLayer = layer; |
| 188 | if (layer->useParentBackfaceVisibility()) { |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 189 | DCHECK(layer->parent()); |
| 190 | DCHECK(!layer->parent()->useParentBackfaceVisibility()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 191 | backfaceTestLayer = layer->parent(); |
| 192 | } |
| 193 | |
| 194 | // The layer should not be drawn if (1) it is not double-sided and (2) the back of the layer is known to be facing the screen. |
| 195 | if (!backfaceTestLayer->doubleSided() && transformToScreenIsKnown(backfaceTestLayer) && isLayerBackFaceVisible(backfaceTestLayer)) |
| 196 | return true; |
| 197 | |
| 198 | return false; |
| 199 | } |
| 200 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 201 | static inline bool subtreeShouldBeSkipped(LayerImpl* layer) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 202 | { |
| 203 | // The opacity of a layer always applies to its children (either implicitly |
| 204 | // via a render surface or explicitly if the parent preserves 3D), so the |
| 205 | // entire subtree can be skipped if this layer is fully transparent. |
| 206 | return !layer->opacity(); |
| 207 | } |
| 208 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 209 | static inline bool subtreeShouldBeSkipped(Layer* layer) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 210 | { |
| 211 | // If the opacity is being animated then the opacity on the main thread is unreliable |
| 212 | // (since the impl thread may be using a different opacity), so it should not be trusted. |
| 213 | // In particular, it should not cause the subtree to be skipped. |
| 214 | return !layer->opacity() && !layer->opacityIsAnimating(); |
| 215 | } |
| 216 | |
| 217 | template<typename LayerType> |
| 218 | static bool subtreeShouldRenderToSeparateSurface(LayerType* layer, bool axisAlignedWithRespectToParent) |
| 219 | { |
| 220 | // The root layer has a special render surface that is set up externally, so |
| 221 | // it shouldn't be treated as a surface in this code. |
| 222 | if (!layer->parent()) |
| 223 | return false; |
| 224 | |
| 225 | // Cache this value, because otherwise it walks the entire subtree several times. |
| 226 | bool descendantDrawsContent = layer->descendantDrawsContent(); |
| 227 | |
| 228 | // |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 229 | // A layer and its descendants should render onto a new RenderSurfaceImpl if any of these rules hold: |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 230 | // |
| 231 | |
| 232 | // If we force it. |
| 233 | if (layer->forceRenderSurface()) |
| 234 | return true; |
| 235 | |
| 236 | // If the layer uses a mask. |
| 237 | if (layer->maskLayer()) |
| 238 | return true; |
| 239 | |
| 240 | // If the layer has a reflection. |
| 241 | if (layer->replicaLayer()) |
| 242 | return true; |
| 243 | |
| 244 | // If the layer uses a CSS filter. |
[email protected] | 4000abf | 2012-10-23 04:45:45 | [diff] [blame^] | 245 | if (!layer->filters().isEmpty() || !layer->backgroundFilters().isEmpty() || layer->filter()) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 246 | return true; |
| 247 | |
| 248 | // If the layer flattens its subtree (i.e. the layer doesn't preserve-3d), but it is |
| 249 | // treated as a 3D object by its parent (i.e. parent does preserve-3d). |
| 250 | if (layerIsInExisting3DRenderingContext(layer) && !layer->preserves3D() && descendantDrawsContent) |
| 251 | return true; |
| 252 | |
| 253 | // If the layer clips its descendants but it is not axis-aligned with respect to its parent. |
| 254 | if (layerClipsSubtree(layer) && !axisAlignedWithRespectToParent && descendantDrawsContent) |
| 255 | return true; |
| 256 | |
| 257 | // If the layer has opacity != 1 and does not have a preserves-3d transform style. |
| 258 | if (layer->opacity() != 1 && !layer->preserves3D() && descendantDrawsContent) |
| 259 | return true; |
| 260 | |
| 261 | return false; |
| 262 | } |
| 263 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 264 | WebTransformationMatrix computeScrollCompensationForThisLayer(LayerImpl* scrollingLayer, const WebTransformationMatrix& parentMatrix) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 265 | { |
| 266 | // For every layer that has non-zero scrollDelta, we have to compute a transform that can undo the |
| 267 | // scrollDelta translation. In particular, we want this matrix to premultiply a fixed-position layer's |
| 268 | // parentMatrix, so we design this transform in three steps as follows. The steps described here apply |
| 269 | // from right-to-left, so Step 1 would be the right-most matrix: |
| 270 | // |
| 271 | // Step 1. transform from target surface space to the exact space where scrollDelta is actually applied. |
| 272 | // -- this is inverse of the matrix in step 3 |
| 273 | // Step 2. undo the scrollDelta |
| 274 | // -- this is just a translation by scrollDelta. |
| 275 | // Step 3. transform back to target surface space. |
| 276 | // -- this transform is the "partialLayerOriginTransform" = (parentMatrix * scale(layer->pageScaleDelta())); |
| 277 | // |
| 278 | // These steps create a matrix that both start and end in targetSurfaceSpace. So this matrix can |
| 279 | // pre-multiply any fixed-position layer's drawTransform to undo the scrollDeltas -- as long as |
| 280 | // that fixed position layer is fixed onto the same renderTarget as this scrollingLayer. |
| 281 | // |
| 282 | |
| 283 | WebTransformationMatrix partialLayerOriginTransform = parentMatrix; |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 284 | partialLayerOriginTransform.multiply(scrollingLayer->implTransform()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 285 | |
| 286 | WebTransformationMatrix scrollCompensationForThisLayer = partialLayerOriginTransform; // Step 3 |
| 287 | scrollCompensationForThisLayer.translate(scrollingLayer->scrollDelta().width(), scrollingLayer->scrollDelta().height()); // Step 2 |
| 288 | scrollCompensationForThisLayer.multiply(partialLayerOriginTransform.inverse()); // Step 1 |
| 289 | return scrollCompensationForThisLayer; |
| 290 | } |
| 291 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 292 | WebTransformationMatrix computeScrollCompensationMatrixForChildren(Layer* currentLayer, const WebTransformationMatrix& currentParentMatrix, const WebTransformationMatrix& currentScrollCompensation) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 293 | { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 294 | // The main thread (i.e. Layer) does not need to worry about scroll compensation. |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 295 | // So we can just return an identity matrix here. |
| 296 | return WebTransformationMatrix(); |
| 297 | } |
| 298 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 299 | WebTransformationMatrix computeScrollCompensationMatrixForChildren(LayerImpl* layer, const WebTransformationMatrix& parentMatrix, const WebTransformationMatrix& currentScrollCompensationMatrix) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 300 | { |
| 301 | // "Total scroll compensation" is the transform needed to cancel out all scrollDelta translations that |
| 302 | // occurred since the nearest container layer, even if there are renderSurfaces in-between. |
| 303 | // |
| 304 | // There are some edge cases to be aware of, that are not explicit in the code: |
| 305 | // - A layer that is both a fixed-position and container should not be its own container, instead, that means |
| 306 | // it is fixed to an ancestor, and is a container for any fixed-position descendants. |
| 307 | // - A layer that is a fixed-position container and has a renderSurface should behave the same as a container |
| 308 | // without a renderSurface, the renderSurface is irrelevant in that case. |
| 309 | // - A layer that does not have an explicit container is simply fixed to the viewport |
| 310 | // (i.e. the root renderSurface, and it would still compensate for root layer's scrollDelta). |
| 311 | // - If the fixed-position layer has its own renderSurface, then the renderSurface is |
| 312 | // the one who gets fixed. |
| 313 | // |
| 314 | // This function needs to be called AFTER layers create their own renderSurfaces. |
| 315 | // |
| 316 | |
| 317 | // Avoid the overheads (including stack allocation and matrix initialization/copy) if we know that the scroll compensation doesn't need to be reset or adjusted. |
| 318 | if (!layer->isContainerForFixedPositionLayers() && layer->scrollDelta().isZero() && !layer->renderSurface()) |
| 319 | return currentScrollCompensationMatrix; |
| 320 | |
| 321 | // Start as identity matrix. |
| 322 | WebTransformationMatrix nextScrollCompensationMatrix; |
| 323 | |
| 324 | // If this layer is not a container, then it inherits the existing scroll compensations. |
| 325 | if (!layer->isContainerForFixedPositionLayers()) |
| 326 | nextScrollCompensationMatrix = currentScrollCompensationMatrix; |
| 327 | |
| 328 | // If the current layer has a non-zero scrollDelta, then we should compute its local scrollCompensation |
| 329 | // and accumulate it to the nextScrollCompensationMatrix. |
| 330 | if (!layer->scrollDelta().isZero()) { |
| 331 | WebTransformationMatrix scrollCompensationForThisLayer = computeScrollCompensationForThisLayer(layer, parentMatrix); |
| 332 | nextScrollCompensationMatrix.multiply(scrollCompensationForThisLayer); |
| 333 | } |
| 334 | |
| 335 | // If the layer created its own renderSurface, we have to adjust nextScrollCompensationMatrix. |
| 336 | // The adjustment allows us to continue using the scrollCompensation on the next surface. |
| 337 | // Step 1 (right-most in the math): transform from the new surface to the original ancestor surface |
| 338 | // Step 2: apply the scroll compensation |
| 339 | // Step 3: transform back to the new surface. |
| 340 | if (layer->renderSurface() && !nextScrollCompensationMatrix.isIdentity()) |
| 341 | nextScrollCompensationMatrix = layer->renderSurface()->drawTransform().inverse() * nextScrollCompensationMatrix * layer->renderSurface()->drawTransform(); |
| 342 | |
| 343 | return nextScrollCompensationMatrix; |
| 344 | } |
| 345 | |
| 346 | // Should be called just before the recursive calculateDrawTransformsInternal(). |
| 347 | template<typename LayerType, typename LayerList> |
| 348 | void setupRootLayerAndSurfaceForRecursion(LayerType* rootLayer, LayerList& renderSurfaceLayerList, const IntSize& deviceViewportSize) |
| 349 | { |
| 350 | if (!rootLayer->renderSurface()) |
| 351 | rootLayer->createRenderSurface(); |
| 352 | |
| 353 | rootLayer->renderSurface()->setContentRect(IntRect(IntPoint::zero(), deviceViewportSize)); |
[email protected] | 7d929c0 | 2012-09-20 17:26:57 | [diff] [blame] | 354 | rootLayer->renderSurface()->clearLayerLists(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 355 | |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 356 | DCHECK(renderSurfaceLayerList.empty()); |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 357 | renderSurfaceLayerList.push_back(rootLayer); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | // Recursively walks the layer tree starting at the given node and computes all the |
| 361 | // necessary transformations, clipRects, render surfaces, etc. |
| 362 | template<typename LayerType, typename LayerList, typename RenderSurfaceType, typename LayerSorter> |
| 363 | static void calculateDrawTransformsInternal(LayerType* layer, LayerType* rootLayer, const WebTransformationMatrix& parentMatrix, |
| 364 | const WebTransformationMatrix& fullHierarchyMatrix, const WebTransformationMatrix& currentScrollCompensationMatrix, |
| 365 | const IntRect& clipRectFromAncestor, bool ancestorClipsSubtree, |
| 366 | RenderSurfaceType* nearestAncestorThatMovesPixels, LayerList& renderSurfaceLayerList, LayerList& layerList, |
[email protected] | f3922f2 | 2012-10-12 09:20:38 | [diff] [blame] | 367 | LayerSorter* layerSorter, int maxTextureSize, float deviceScaleFactor, IntRect& drawableContentRectOfSubtree) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 368 | { |
| 369 | // This function computes the new matrix transformations recursively for this |
| 370 | // layer and all its descendants. It also computes the appropriate render surfaces. |
| 371 | // Some important points to remember: |
| 372 | // |
| 373 | // 0. Here, transforms are notated in Matrix x Vector order, and in words we describe what |
| 374 | // the transform does from left to right. |
| 375 | // |
| 376 | // 1. In our terminology, the "layer origin" refers to the top-left corner of a layer, and the |
| 377 | // positive Y-axis points downwards. This interpretation is valid because the orthographic |
| 378 | // projection applied at draw time flips the Y axis appropriately. |
| 379 | // |
| 380 | // 2. The anchor point, when given as a FloatPoint object, is specified in "unit layer space", |
| 381 | // where the bounds of the layer map to [0, 1]. However, as a WebTransformationMatrix object, |
[email protected] | b8d3c02 | 2012-10-08 17:38:04 | [diff] [blame] | 382 | // the transform to the anchor point is specified in "layer space", where the bounds |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 383 | // of the layer map to [bounds.width(), bounds.height()]. |
| 384 | // |
| 385 | // 3. Definition of various transforms used: |
| 386 | // M[parent] is the parent matrix, with respect to the nearest render surface, passed down recursively. |
| 387 | // M[root] is the full hierarchy, with respect to the root, passed down recursively. |
| 388 | // Tr[origin] is the translation matrix from the parent's origin to this layer's origin. |
| 389 | // Tr[origin2anchor] is the translation from the layer's origin to its anchor point |
| 390 | // Tr[origin2center] is the translation from the layer's origin to its center |
| 391 | // M[layer] is the layer's matrix (applied at the anchor point) |
| 392 | // M[sublayer] is the layer's sublayer transform (applied at the layer's center) |
[email protected] | b8d3c02 | 2012-10-08 17:38:04 | [diff] [blame] | 393 | // S[layer2content] is the ratio of a layer's contentBounds() to its bounds(). |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 394 | // |
| 395 | // Some composite transforms can help in understanding the sequence of transforms: |
| 396 | // compositeLayerTransform = Tr[origin2anchor] * M[layer] * Tr[origin2anchor].inverse() |
| 397 | // compositeSublayerTransform = Tr[origin2center] * M[sublayer] * Tr[origin2center].inverse() |
| 398 | // |
| 399 | // In words, the layer transform is applied about the anchor point, and the sublayer transform is |
| 400 | // applied about the center of the layer. |
| 401 | // |
| 402 | // 4. When a layer (or render surface) is drawn, it is drawn into a "target render surface". Therefore the draw |
| 403 | // transform does not necessarily transform from screen space to local layer space. Instead, the draw transform |
| 404 | // is the transform between the "target render surface space" and local layer space. Note that render surfaces, |
| 405 | // except for the root, also draw themselves into a different target render surface, and so their draw |
| 406 | // transform and origin transforms are also described with respect to the target. |
| 407 | // |
| 408 | // Using these definitions, then: |
| 409 | // |
| 410 | // The draw transform for the layer is: |
[email protected] | b8d3c02 | 2012-10-08 17:38:04 | [diff] [blame] | 411 | // M[draw] = M[parent] * Tr[origin] * compositeLayerTransform * S[layer2content] |
| 412 | // = M[parent] * Tr[layer->position() + anchor] * M[layer] * Tr[anchor2origin] * S[layer2content] |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 413 | // |
| 414 | // Interpreting the math left-to-right, this transforms from the layer's render surface to the origin of the layer in content space. |
| 415 | // |
| 416 | // The screen space transform is: |
[email protected] | b8d3c02 | 2012-10-08 17:38:04 | [diff] [blame] | 417 | // M[screenspace] = M[root] * Tr[origin] * compositeLayerTransform * S[layer2content] |
| 418 | // = M[root] * Tr[layer->position() + anchor] * M[layer] * Tr[anchor2origin] * S[layer2content] |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 419 | // |
| 420 | // Interpreting the math left-to-right, this transforms from the root render surface's content space to the local layer's origin in layer space. |
| 421 | // |
| 422 | // The transform hierarchy that is passed on to children (i.e. the child's parentMatrix) is: |
| 423 | // M[parent]_for_child = M[parent] * Tr[origin] * compositeLayerTransform * compositeSublayerTransform |
[email protected] | b8d3c02 | 2012-10-08 17:38:04 | [diff] [blame] | 424 | // = M[parent] * Tr[layer->position() + anchor] * M[layer] * Tr[anchor2origin] * compositeSublayerTransform |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 425 | // |
| 426 | // and a similar matrix for the full hierarchy with respect to the root. |
| 427 | // |
| 428 | // Finally, note that the final matrix used by the shader for the layer is P * M[draw] * S . This final product |
| 429 | // is computed in drawTexturedQuad(), where: |
| 430 | // P is the projection matrix |
[email protected] | b8d3c02 | 2012-10-08 17:38:04 | [diff] [blame] | 431 | // S is the scale adjustment (to scale up a canonical quad to the layer's size) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 432 | // |
| 433 | // When a render surface has a replica layer, that layer's transform is used to draw a second copy of the surface. |
| 434 | // Transforms named here are relative to the surface, unless they specify they are relative to the replica layer. |
| 435 | // |
| 436 | // We will denote a scale by device scale S[deviceScale] |
| 437 | // |
| 438 | // The render surface draw transform to its target surface origin is: |
| 439 | // M[surfaceDraw] = M[owningLayer->Draw] |
| 440 | // |
| 441 | // The render surface origin transform to its the root (screen space) origin is: |
| 442 | // M[surface2root] = M[owningLayer->screenspace] * S[deviceScale].inverse() |
| 443 | // |
| 444 | // The replica draw transform to its target surface origin is: |
| 445 | // M[replicaDraw] = S[deviceScale] * M[surfaceDraw] * Tr[replica->position() + replica->anchor()] * Tr[replica] * Tr[origin2anchor].inverse() * S[contentsScale].inverse() |
| 446 | // |
| 447 | // The replica draw transform to the root (screen space) origin is: |
| 448 | // M[replica2root] = M[surface2root] * Tr[replica->position()] * Tr[replica] * Tr[origin2anchor].inverse() |
| 449 | // |
| 450 | |
| 451 | // If we early-exit anywhere in this function, the drawableContentRect of this subtree should be considered empty. |
| 452 | drawableContentRectOfSubtree = IntRect(); |
| 453 | |
| 454 | if (subtreeShouldBeSkipped(layer)) |
| 455 | return; |
| 456 | |
| 457 | IntRect clipRectForSubtree; |
| 458 | bool subtreeShouldBeClipped = false; |
| 459 | |
| 460 | float drawOpacity = layer->opacity(); |
| 461 | bool drawOpacityIsAnimating = layer->opacityIsAnimating(); |
| 462 | if (layer->parent() && layer->parent()->preserves3D()) { |
| 463 | drawOpacity *= layer->parent()->drawOpacity(); |
| 464 | drawOpacityIsAnimating |= layer->parent()->drawOpacityIsAnimating(); |
| 465 | } |
| 466 | |
| 467 | IntSize bounds = layer->bounds(); |
| 468 | FloatPoint anchorPoint = layer->anchorPoint(); |
| 469 | FloatPoint position = layer->position() - layer->scrollDelta(); |
| 470 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 471 | WebTransformationMatrix layerLocalTransform; |
[email protected] | f3922f2 | 2012-10-12 09:20:38 | [diff] [blame] | 472 | // LT = M[impl transformation] |
| 473 | layerLocalTransform.multiply(layer->implTransform()); |
| 474 | // LT = M[impl transformation] * Tr[origin] * Tr[origin2anchor] |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 475 | layerLocalTransform.translate3d(position.x() + anchorPoint.x() * bounds.width(), position.y() + anchorPoint.y() * bounds.height(), layer->anchorPointZ()); |
[email protected] | f3922f2 | 2012-10-12 09:20:38 | [diff] [blame] | 476 | // LT = M[impl transformation] * Tr[origin] * Tr[origin2anchor] * M[layer] |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 477 | layerLocalTransform.multiply(layer->transform()); |
[email protected] | f3922f2 | 2012-10-12 09:20:38 | [diff] [blame] | 478 | // LT = S[impl transformation] * Tr[origin] * Tr[origin2anchor] * M[layer] * Tr[anchor2origin] |
[email protected] | b8d3c02 | 2012-10-08 17:38:04 | [diff] [blame] | 479 | layerLocalTransform.translate3d(-anchorPoint.x() * bounds.width(), -anchorPoint.y() * bounds.height(), -layer->anchorPointZ()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 480 | |
| 481 | WebTransformationMatrix combinedTransform = parentMatrix; |
| 482 | combinedTransform.multiply(layerLocalTransform); |
| 483 | |
| 484 | if (layer->fixedToContainerLayer()) { |
| 485 | // Special case: this layer is a composited fixed-position layer; we need to |
| 486 | // explicitly compensate for all ancestors' nonzero scrollDeltas to keep this layer |
| 487 | // fixed correctly. |
| 488 | combinedTransform = currentScrollCompensationMatrix * combinedTransform; |
| 489 | } |
| 490 | |
| 491 | // The drawTransform that gets computed below is effectively the layer's drawTransform, unless |
| 492 | // the layer itself creates a renderSurface. In that case, the renderSurface re-parents the transforms. |
| 493 | WebTransformationMatrix drawTransform = combinedTransform; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 494 | if (!layer->contentBounds().isEmpty() && !layer->bounds().isEmpty()) { |
[email protected] | b8d3c02 | 2012-10-08 17:38:04 | [diff] [blame] | 495 | // M[draw] = M[parent] * LT * S[layer2content] |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 496 | drawTransform.scaleNonUniform(layer->bounds().width() / static_cast<double>(layer->contentBounds().width()), |
| 497 | layer->bounds().height() / static_cast<double>(layer->contentBounds().height())); |
| 498 | } |
| 499 | |
| 500 | // layerScreenSpaceTransform represents the transform between root layer's "screen space" and local content space. |
| 501 | WebTransformationMatrix layerScreenSpaceTransform = fullHierarchyMatrix; |
| 502 | if (!layer->preserves3D()) |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 503 | MathUtil::flattenTransformTo2d(layerScreenSpaceTransform); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 504 | layerScreenSpaceTransform.multiply(drawTransform); |
| 505 | layer->setScreenSpaceTransform(layerScreenSpaceTransform); |
| 506 | |
| 507 | bool animatingTransformToTarget = layer->transformIsAnimating(); |
| 508 | bool animatingTransformToScreen = animatingTransformToTarget; |
| 509 | if (layer->parent()) { |
| 510 | animatingTransformToTarget |= layer->parent()->drawTransformIsAnimating(); |
| 511 | animatingTransformToScreen |= layer->parent()->screenSpaceTransformIsAnimating(); |
| 512 | } |
| 513 | |
| 514 | FloatRect contentRect(FloatPoint(), layer->contentBounds()); |
| 515 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 516 | // fullHierarchyMatrix is the matrix that transforms objects between screen space (except projection matrix) and the most recent RenderSurfaceImpl's space. |
| 517 | // nextHierarchyMatrix will only change if this layer uses a new RenderSurfaceImpl, otherwise remains the same. |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 518 | WebTransformationMatrix nextHierarchyMatrix = fullHierarchyMatrix; |
| 519 | WebTransformationMatrix sublayerMatrix; |
| 520 | |
| 521 | if (subtreeShouldRenderToSeparateSurface(layer, isScaleOrTranslation(combinedTransform))) { |
| 522 | // Check back-face visibility before continuing with this surface and its subtree |
| 523 | if (!layer->doubleSided() && transformToParentIsKnown(layer) && isSurfaceBackFaceVisible(layer, combinedTransform)) |
| 524 | return; |
| 525 | |
| 526 | if (!layer->renderSurface()) |
| 527 | layer->createRenderSurface(); |
| 528 | |
| 529 | RenderSurfaceType* renderSurface = layer->renderSurface(); |
[email protected] | 7d929c0 | 2012-09-20 17:26:57 | [diff] [blame] | 530 | renderSurface->clearLayerLists(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 531 | |
[email protected] | 8a000dd | 2012-10-12 01:41:18 | [diff] [blame] | 532 | // The origin of the new surface is the upper left corner of the layer. |
[email protected] | f3922f2 | 2012-10-12 09:20:38 | [diff] [blame] | 533 | renderSurface->setDrawTransform(drawTransform); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 534 | WebTransformationMatrix layerDrawTransform; |
[email protected] | f3922f2 | 2012-10-12 09:20:38 | [diff] [blame] | 535 | layerDrawTransform.scale(deviceScaleFactor); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 536 | if (!layer->contentBounds().isEmpty() && !layer->bounds().isEmpty()) { |
| 537 | layerDrawTransform.scaleNonUniform(layer->bounds().width() / static_cast<double>(layer->contentBounds().width()), |
| 538 | layer->bounds().height() / static_cast<double>(layer->contentBounds().height())); |
| 539 | } |
| 540 | layer->setDrawTransform(layerDrawTransform); |
| 541 | |
| 542 | // The sublayer matrix transforms centered layer rects into target |
| 543 | // surface content space. |
| 544 | sublayerMatrix.makeIdentity(); |
[email protected] | f3922f2 | 2012-10-12 09:20:38 | [diff] [blame] | 545 | sublayerMatrix.scale(deviceScaleFactor); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 546 | |
| 547 | // The opacity value is moved from the layer to its surface, so that the entire subtree properly inherits opacity. |
| 548 | renderSurface->setDrawOpacity(drawOpacity); |
| 549 | renderSurface->setDrawOpacityIsAnimating(drawOpacityIsAnimating); |
| 550 | layer->setDrawOpacity(1); |
| 551 | layer->setDrawOpacityIsAnimating(false); |
| 552 | |
| 553 | renderSurface->setTargetSurfaceTransformsAreAnimating(animatingTransformToTarget); |
| 554 | renderSurface->setScreenSpaceTransformsAreAnimating(animatingTransformToScreen); |
| 555 | animatingTransformToTarget = false; |
| 556 | layer->setDrawTransformIsAnimating(animatingTransformToTarget); |
| 557 | layer->setScreenSpaceTransformIsAnimating(animatingTransformToScreen); |
| 558 | |
| 559 | // Update the aggregate hierarchy matrix to include the transform of the |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 560 | // newly created RenderSurfaceImpl. |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 561 | nextHierarchyMatrix.multiply(renderSurface->drawTransform()); |
| 562 | |
| 563 | // The new renderSurface here will correctly clip the entire subtree. So, we do |
| 564 | // not need to continue propagating the clipping state further down the tree. This |
| 565 | // way, we can avoid transforming clipRects from ancestor target surface space to |
| 566 | // current target surface space that could cause more w < 0 headaches. |
| 567 | subtreeShouldBeClipped = false; |
| 568 | |
[email protected] | 9bab0bb | 2012-10-08 19:11:58 | [diff] [blame] | 569 | if (layer->maskLayer()) { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 570 | layer->maskLayer()->setRenderTarget(layer); |
[email protected] | 9bab0bb | 2012-10-08 19:11:58 | [diff] [blame] | 571 | layer->maskLayer()->setVisibleContentRect(IntRect(IntPoint(), layer->contentBounds())); |
| 572 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 573 | |
[email protected] | 9bab0bb | 2012-10-08 19:11:58 | [diff] [blame] | 574 | if (layer->replicaLayer() && layer->replicaLayer()->maskLayer()) { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 575 | layer->replicaLayer()->maskLayer()->setRenderTarget(layer); |
[email protected] | 9bab0bb | 2012-10-08 19:11:58 | [diff] [blame] | 576 | layer->replicaLayer()->maskLayer()->setVisibleContentRect(IntRect(IntPoint(), layer->contentBounds())); |
| 577 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 578 | |
[email protected] | 4000abf | 2012-10-23 04:45:45 | [diff] [blame^] | 579 | // FIXME: make this smarter for the SkImageFilter case (check for |
| 580 | // pixel-moving filters) |
| 581 | if (layer->filters().hasFilterThatMovesPixels() || layer->filter()) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 582 | nearestAncestorThatMovesPixels = renderSurface; |
| 583 | |
[email protected] | 9bab0bb | 2012-10-08 19:11:58 | [diff] [blame] | 584 | // The render surface clipRect is expressed in the space where this surface draws, i.e. the same space as clipRectFromAncestor. |
| 585 | if (ancestorClipsSubtree) |
| 586 | renderSurface->setClipRect(clipRectFromAncestor); |
| 587 | else |
| 588 | renderSurface->setClipRect(IntRect()); |
| 589 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 590 | renderSurface->setNearestAncestorThatMovesPixels(nearestAncestorThatMovesPixels); |
| 591 | |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 592 | renderSurfaceLayerList.push_back(layer); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 593 | } else { |
| 594 | layer->setDrawTransform(drawTransform); |
| 595 | layer->setDrawTransformIsAnimating(animatingTransformToTarget); |
| 596 | layer->setScreenSpaceTransformIsAnimating(animatingTransformToScreen); |
| 597 | sublayerMatrix = combinedTransform; |
| 598 | |
| 599 | layer->setDrawOpacity(drawOpacity); |
| 600 | layer->setDrawOpacityIsAnimating(drawOpacityIsAnimating); |
| 601 | |
| 602 | if (layer != rootLayer) { |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 603 | DCHECK(layer->parent()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 604 | layer->clearRenderSurface(); |
| 605 | |
| 606 | // Layers without renderSurfaces directly inherit the ancestor's clip status. |
| 607 | subtreeShouldBeClipped = ancestorClipsSubtree; |
| 608 | if (ancestorClipsSubtree) |
| 609 | clipRectForSubtree = clipRectFromAncestor; |
| 610 | |
| 611 | // Layers that are not their own renderTarget will render into the target of their nearest ancestor. |
| 612 | layer->setRenderTarget(layer->parent()->renderTarget()); |
| 613 | } else { |
[email protected] | 23bbb41 | 2012-08-30 20:03:38 | [diff] [blame] | 614 | // FIXME: This root layer special case code should eventually go away. https://bugs.webkit.org/show_bug.cgi?id=92290 |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 615 | DCHECK(!layer->parent()); |
| 616 | DCHECK(layer->renderSurface()); |
| 617 | DCHECK(ancestorClipsSubtree); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 618 | layer->renderSurface()->setClipRect(clipRectFromAncestor); |
[email protected] | 23bbb41 | 2012-08-30 20:03:38 | [diff] [blame] | 619 | subtreeShouldBeClipped = false; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 623 | IntRect rectInTargetSpace = enclosingIntRect(MathUtil::mapClippedRect(layer->drawTransform(), contentRect)); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 624 | |
| 625 | if (layerClipsSubtree(layer)) { |
| 626 | subtreeShouldBeClipped = true; |
| 627 | if (ancestorClipsSubtree && !layer->renderSurface()) { |
| 628 | clipRectForSubtree = clipRectFromAncestor; |
| 629 | clipRectForSubtree.intersect(rectInTargetSpace); |
| 630 | } else |
| 631 | clipRectForSubtree = rectInTargetSpace; |
| 632 | } |
| 633 | |
| 634 | // Flatten to 2D if the layer doesn't preserve 3D. |
| 635 | if (!layer->preserves3D()) |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 636 | MathUtil::flattenTransformTo2d(sublayerMatrix); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 637 | |
| 638 | // Apply the sublayer transform at the center of the layer. |
[email protected] | b8d3c02 | 2012-10-08 17:38:04 | [diff] [blame] | 639 | sublayerMatrix.translate(0.5 * bounds.width(), 0.5 * bounds.height()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 640 | sublayerMatrix.multiply(layer->sublayerTransform()); |
[email protected] | b8d3c02 | 2012-10-08 17:38:04 | [diff] [blame] | 641 | sublayerMatrix.translate(-0.5 * bounds.width(), -0.5 * bounds.height()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 642 | |
| 643 | LayerList& descendants = (layer->renderSurface() ? layer->renderSurface()->layerList() : layerList); |
| 644 | |
| 645 | // Any layers that are appended after this point are in the layer's subtree and should be included in the sorting process. |
| 646 | unsigned sortingStartIndex = descendants.size(); |
| 647 | |
| 648 | if (!layerShouldBeSkipped(layer)) |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 649 | descendants.push_back(layer); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 650 | |
| 651 | WebTransformationMatrix nextScrollCompensationMatrix = computeScrollCompensationMatrixForChildren(layer, parentMatrix, currentScrollCompensationMatrix);; |
| 652 | |
| 653 | IntRect accumulatedDrawableContentRectOfChildren; |
| 654 | for (size_t i = 0; i < layer->children().size(); ++i) { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 655 | LayerType* child = LayerTreeHostCommon::getChildAsRawPtr(layer->children(), i); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 656 | IntRect drawableContentRectOfChildSubtree; |
| 657 | calculateDrawTransformsInternal<LayerType, LayerList, RenderSurfaceType, LayerSorter>(child, rootLayer, sublayerMatrix, nextHierarchyMatrix, nextScrollCompensationMatrix, |
| 658 | clipRectForSubtree, subtreeShouldBeClipped, nearestAncestorThatMovesPixels, |
[email protected] | f3922f2 | 2012-10-12 09:20:38 | [diff] [blame] | 659 | renderSurfaceLayerList, descendants, layerSorter, maxTextureSize, deviceScaleFactor, drawableContentRectOfChildSubtree); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 660 | if (!drawableContentRectOfChildSubtree.isEmpty()) { |
| 661 | accumulatedDrawableContentRectOfChildren.unite(drawableContentRectOfChildSubtree); |
| 662 | if (child->renderSurface()) |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 663 | descendants.push_back(child); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
| 667 | // Compute the total drawableContentRect for this subtree (the rect is in targetSurface space) |
| 668 | IntRect localDrawableContentRectOfSubtree = accumulatedDrawableContentRectOfChildren; |
| 669 | if (layer->drawsContent()) |
| 670 | localDrawableContentRectOfSubtree.unite(rectInTargetSpace); |
| 671 | if (subtreeShouldBeClipped) |
| 672 | localDrawableContentRectOfSubtree.intersect(clipRectForSubtree); |
| 673 | |
| 674 | // Compute the layer's drawable content rect (the rect is in targetSurface space) |
| 675 | IntRect drawableContentRectOfLayer = rectInTargetSpace; |
| 676 | if (subtreeShouldBeClipped) |
| 677 | drawableContentRectOfLayer.intersect(clipRectForSubtree); |
| 678 | layer->setDrawableContentRect(drawableContentRectOfLayer); |
| 679 | |
[email protected] | 9bab0bb | 2012-10-08 19:11:58 | [diff] [blame] | 680 | // Compute the layer's visible content rect (the rect is in content space) |
| 681 | IntRect visibleContentRectOfLayer = calculateVisibleContentRect(layer); |
| 682 | layer->setVisibleContentRect(visibleContentRectOfLayer); |
| 683 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 684 | // Compute the remaining properties for the render surface, if the layer has one. |
| 685 | if (layer->renderSurface() && layer != rootLayer) { |
| 686 | RenderSurfaceType* renderSurface = layer->renderSurface(); |
| 687 | IntRect clippedContentRect = localDrawableContentRectOfSubtree; |
| 688 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 689 | // Don't clip if the layer is reflected as the reflection shouldn't be |
| 690 | // clipped. If the layer is animating, then the surface's transform to |
| 691 | // its target is not known on the main thread, and we should not use it |
| 692 | // to clip. |
| 693 | if (!layer->replicaLayer() && transformToParentIsKnown(layer)) { |
| 694 | // Note, it is correct to use ancestorClipsSubtree here, because we are looking at this layer's renderSurface, not the layer itself. |
| 695 | if (ancestorClipsSubtree && !clippedContentRect.isEmpty()) { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 696 | IntRect surfaceClipRect = LayerTreeHostCommon::calculateVisibleRect(renderSurface->clipRect(), clippedContentRect, renderSurface->drawTransform()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 697 | clippedContentRect.intersect(surfaceClipRect); |
| 698 | } |
| 699 | } |
| 700 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 701 | // The RenderSurfaceImpl backing texture cannot exceed the maximum supported |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 702 | // texture size. |
| 703 | clippedContentRect.setWidth(std::min(clippedContentRect.width(), maxTextureSize)); |
| 704 | clippedContentRect.setHeight(std::min(clippedContentRect.height(), maxTextureSize)); |
| 705 | |
| 706 | if (clippedContentRect.isEmpty()) |
[email protected] | 7d929c0 | 2012-09-20 17:26:57 | [diff] [blame] | 707 | renderSurface->clearLayerLists(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 708 | |
| 709 | renderSurface->setContentRect(clippedContentRect); |
[email protected] | f3922f2 | 2012-10-12 09:20:38 | [diff] [blame] | 710 | renderSurface->setScreenSpaceTransform(layer->screenSpaceTransform()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 711 | |
| 712 | if (layer->replicaLayer()) { |
| 713 | WebTransformationMatrix surfaceOriginToReplicaOriginTransform; |
[email protected] | f3922f2 | 2012-10-12 09:20:38 | [diff] [blame] | 714 | surfaceOriginToReplicaOriginTransform.scale(deviceScaleFactor); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 715 | surfaceOriginToReplicaOriginTransform.translate(layer->replicaLayer()->position().x() + layer->replicaLayer()->anchorPoint().x() * bounds.width(), |
| 716 | layer->replicaLayer()->position().y() + layer->replicaLayer()->anchorPoint().y() * bounds.height()); |
| 717 | surfaceOriginToReplicaOriginTransform.multiply(layer->replicaLayer()->transform()); |
| 718 | surfaceOriginToReplicaOriginTransform.translate(-layer->replicaLayer()->anchorPoint().x() * bounds.width(), -layer->replicaLayer()->anchorPoint().y() * bounds.height()); |
[email protected] | f3922f2 | 2012-10-12 09:20:38 | [diff] [blame] | 719 | surfaceOriginToReplicaOriginTransform.scale(1 / deviceScaleFactor); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 720 | |
| 721 | // Compute the replica's "originTransform" that maps from the replica's origin space to the target surface origin space. |
| 722 | WebTransformationMatrix replicaOriginTransform = layer->renderSurface()->drawTransform() * surfaceOriginToReplicaOriginTransform; |
| 723 | renderSurface->setReplicaDrawTransform(replicaOriginTransform); |
| 724 | |
| 725 | // Compute the replica's "screenSpaceTransform" that maps from the replica's origin space to the screen's origin space. |
| 726 | WebTransformationMatrix replicaScreenSpaceTransform = layer->renderSurface()->screenSpaceTransform() * surfaceOriginToReplicaOriginTransform; |
| 727 | renderSurface->setReplicaScreenSpaceTransform(replicaScreenSpaceTransform); |
| 728 | } |
| 729 | |
| 730 | // If a render surface has no layer list, then it and none of its children needed to get drawn. |
| 731 | if (!layer->renderSurface()->layerList().size()) { |
| 732 | // FIXME: Originally we asserted that this layer was already at the end of the |
| 733 | // list, and only needed to remove that layer. For now, we remove the |
| 734 | // entire subtree of surfaces to fix a crash bug. The root cause is |
| 735 | // https://bugs.webkit.org/show_bug.cgi?id=74147 and we should be able |
| 736 | // to put the original assert after fixing that. |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 737 | while (renderSurfaceLayerList.back() != layer) { |
| 738 | renderSurfaceLayerList.back()->clearRenderSurface(); |
| 739 | renderSurfaceLayerList.pop_back(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 740 | } |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 741 | DCHECK(renderSurfaceLayerList.back() == layer); |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 742 | renderSurfaceLayerList.pop_back(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 743 | layer->clearRenderSurface(); |
| 744 | return; |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | // If neither this layer nor any of its children were added, early out. |
| 749 | if (sortingStartIndex == descendants.size()) |
| 750 | return; |
| 751 | |
| 752 | // If preserves-3d then sort all the descendants in 3D so that they can be |
| 753 | // drawn from back to front. If the preserves-3d property is also set on the parent then |
| 754 | // skip the sorting as the parent will sort all the descendants anyway. |
| 755 | if (descendants.size() && layer->preserves3D() && (!layer->parent() || !layer->parent()->preserves3D())) |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 756 | sortLayers(descendants.begin() + sortingStartIndex, descendants.end(), layerSorter); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 757 | |
| 758 | if (layer->renderSurface()) |
| 759 | drawableContentRectOfSubtree = enclosingIntRect(layer->renderSurface()->drawableContentRect()); |
| 760 | else |
| 761 | drawableContentRectOfSubtree = localDrawableContentRectOfSubtree; |
| 762 | |
[email protected] | 7d929c0 | 2012-09-20 17:26:57 | [diff] [blame] | 763 | if (layer->hasContributingDelegatedRenderPasses()) |
| 764 | layer->renderTarget()->renderSurface()->addContributingDelegatedRenderPassLayer(layer); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 765 | } |
| 766 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 767 | void LayerTreeHostCommon::calculateDrawTransforms(Layer* rootLayer, const IntSize& deviceViewportSize, float deviceScaleFactor, int maxTextureSize, std::vector<scoped_refptr<Layer> >& renderSurfaceLayerList) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 768 | { |
| 769 | IntRect totalDrawableContentRect; |
| 770 | WebTransformationMatrix identityMatrix; |
| 771 | WebTransformationMatrix deviceScaleTransform; |
| 772 | deviceScaleTransform.scale(deviceScaleFactor); |
| 773 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 774 | setupRootLayerAndSurfaceForRecursion<Layer, std::vector<scoped_refptr<Layer> > >(rootLayer, renderSurfaceLayerList, deviceViewportSize); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 775 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 776 | cc::calculateDrawTransformsInternal<Layer, std::vector<scoped_refptr<Layer> >, RenderSurface, void>(rootLayer, rootLayer, deviceScaleTransform, identityMatrix, identityMatrix, |
[email protected] | f3922f2 | 2012-10-12 09:20:38 | [diff] [blame] | 777 | rootLayer->renderSurface()->contentRect(), true, 0, renderSurfaceLayerList, |
| 778 | rootLayer->renderSurface()->layerList(), 0, maxTextureSize, deviceScaleFactor, totalDrawableContentRect); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 779 | } |
| 780 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 781 | void LayerTreeHostCommon::calculateDrawTransforms(LayerImpl* rootLayer, const IntSize& deviceViewportSize, float deviceScaleFactor, LayerSorter* layerSorter, int maxTextureSize, std::vector<LayerImpl*>& renderSurfaceLayerList) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 782 | { |
| 783 | IntRect totalDrawableContentRect; |
| 784 | WebTransformationMatrix identityMatrix; |
| 785 | WebTransformationMatrix deviceScaleTransform; |
| 786 | deviceScaleTransform.scale(deviceScaleFactor); |
| 787 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 788 | setupRootLayerAndSurfaceForRecursion<LayerImpl, std::vector<LayerImpl*> >(rootLayer, renderSurfaceLayerList, deviceViewportSize); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 789 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 790 | cc::calculateDrawTransformsInternal<LayerImpl, std::vector<LayerImpl*>, RenderSurfaceImpl, LayerSorter>(rootLayer, rootLayer, deviceScaleTransform, identityMatrix, identityMatrix, |
[email protected] | f3922f2 | 2012-10-12 09:20:38 | [diff] [blame] | 791 | rootLayer->renderSurface()->contentRect(), true, 0, renderSurfaceLayerList, |
| 792 | rootLayer->renderSurface()->layerList(), layerSorter, maxTextureSize, deviceScaleFactor, totalDrawableContentRect); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 793 | } |
| 794 | |
[email protected] | 0152773 | 2012-10-19 18:16:19 | [diff] [blame] | 795 | static bool pointHitsRect(const IntPoint& screenSpacePoint, const WebTransformationMatrix& localSpaceToScreenSpaceTransform, FloatRect localSpaceRect) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 796 | { |
| 797 | // If the transform is not invertible, then assume that this point doesn't hit this rect. |
| 798 | if (!localSpaceToScreenSpaceTransform.isInvertible()) |
| 799 | return false; |
| 800 | |
| 801 | // Transform the hit test point from screen space to the local space of the given rect. |
| 802 | bool clipped = false; |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 803 | FloatPoint hitTestPointInLocalSpace = MathUtil::projectPoint(localSpaceToScreenSpaceTransform.inverse(), FloatPoint(screenSpacePoint), clipped); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 804 | |
| 805 | // If projectPoint could not project to a valid value, then we assume that this point doesn't hit this rect. |
| 806 | if (clipped) |
| 807 | return false; |
| 808 | |
| 809 | return localSpaceRect.contains(hitTestPointInLocalSpace); |
| 810 | } |
| 811 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 812 | static bool pointIsClippedBySurfaceOrClipRect(const IntPoint& screenSpacePoint, LayerImpl* layer) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 813 | { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 814 | LayerImpl* currentLayer = layer; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 815 | |
| 816 | // Walk up the layer tree and hit-test any renderSurfaces and any layer clipRects that are active. |
| 817 | while (currentLayer) { |
[email protected] | 0152773 | 2012-10-19 18:16:19 | [diff] [blame] | 818 | if (currentLayer->renderSurface() && !pointHitsRect(screenSpacePoint, currentLayer->renderSurface()->screenSpaceTransform(), currentLayer->renderSurface()->contentRect())) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 819 | return true; |
| 820 | |
| 821 | // Note that drawableContentRects are actually in targetSurface space, so the transform we |
| 822 | // have to provide is the target surface's screenSpaceTransform. |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 823 | LayerImpl* renderTarget = currentLayer->renderTarget(); |
[email protected] | 0152773 | 2012-10-19 18:16:19 | [diff] [blame] | 824 | if (layerClipsSubtree(currentLayer) && !pointHitsRect(screenSpacePoint, renderTarget->renderSurface()->screenSpaceTransform(), currentLayer->drawableContentRect())) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 825 | return true; |
| 826 | |
| 827 | currentLayer = currentLayer->parent(); |
| 828 | } |
| 829 | |
| 830 | // If we have finished walking all ancestors without having already exited, then the point is not clipped by any ancestors. |
| 831 | return false; |
| 832 | } |
| 833 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 834 | LayerImpl* LayerTreeHostCommon::findLayerThatIsHitByPoint(const IntPoint& screenSpacePoint, std::vector<LayerImpl*>& renderSurfaceLayerList) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 835 | { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 836 | LayerImpl* foundLayer = 0; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 837 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 838 | typedef LayerIterator<LayerImpl, std::vector<LayerImpl*>, RenderSurfaceImpl, LayerIteratorActions::FrontToBack> LayerIteratorType; |
| 839 | LayerIteratorType end = LayerIteratorType::end(&renderSurfaceLayerList); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 840 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 841 | for (LayerIteratorType it = LayerIteratorType::begin(&renderSurfaceLayerList); it != end; ++it) { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 842 | // We don't want to consider renderSurfaces for hit testing. |
| 843 | if (!it.representsItself()) |
| 844 | continue; |
| 845 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 846 | LayerImpl* currentLayer = (*it); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 847 | |
| 848 | FloatRect contentRect(FloatPoint::zero(), currentLayer->contentBounds()); |
[email protected] | 0152773 | 2012-10-19 18:16:19 | [diff] [blame] | 849 | if (!pointHitsRect(screenSpacePoint, currentLayer->screenSpaceTransform(), contentRect)) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 850 | continue; |
| 851 | |
| 852 | // At this point, we think the point does hit the layer, but we need to walk up |
| 853 | // the parents to ensure that the layer was not clipped in such a way that the |
| 854 | // hit point actually should not hit the layer. |
[email protected] | 0152773 | 2012-10-19 18:16:19 | [diff] [blame] | 855 | if (pointIsClippedBySurfaceOrClipRect(screenSpacePoint, currentLayer)) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 856 | continue; |
| 857 | |
| 858 | foundLayer = currentLayer; |
| 859 | break; |
| 860 | } |
| 861 | |
[email protected] | 0152773 | 2012-10-19 18:16:19 | [diff] [blame] | 862 | // This can potentially return 0, which means the screenSpacePoint did not successfully hit test any layers, not even the root layer. |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 863 | return foundLayer; |
| 864 | } |
| 865 | |
[email protected] | 9c88e56 | 2012-09-14 22:21:30 | [diff] [blame] | 866 | } // namespace cc |