[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 | |
| 7 | #include "CCLayerTreeHostCommon.h" |
| 8 | |
| 9 | #include "CCLayerImpl.h" |
| 10 | #include "CCLayerIterator.h" |
| 11 | #include "CCLayerSorter.h" |
| 12 | #include "CCMathUtil.h" |
| 13 | #include "CCRenderSurface.h" |
| 14 | #include "FloatQuad.h" |
| 15 | #include "IntRect.h" |
[email protected] | a8461d8 | 2012-10-16 21:11:14 | [diff] [blame^] | 16 | #include "cc/layer.h" |
| 17 | #include "cc/render_surface.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] | 49306751 | 2012-09-19 23:34:10 | [diff] [blame] | 24 | CCScrollAndScaleSet::CCScrollAndScaleSet() |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | CCScrollAndScaleSet::~CCScrollAndScaleSet() |
| 29 | { |
| 30 | } |
| 31 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 32 | IntRect CCLayerTreeHostCommon::calculateVisibleRect(const IntRect& targetSurfaceRect, const IntRect& layerBoundRect, const WebTransformationMatrix& transform) |
| 33 | { |
| 34 | // Is this layer fully contained within the target surface? |
| 35 | IntRect layerInSurfaceSpace = CCMathUtil::mapClippedRect(transform, layerBoundRect); |
| 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(); |
| 50 | IntRect layerRect = enclosingIntRect(CCMathUtil::projectClippedRect(surfaceToLayer, FloatRect(minimalSurfaceRect))); |
| 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 | { |
| 114 | ASSERT(layer->renderTarget()); |
| 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. |
| 129 | targetSurfaceClipRect = enclosingIntRect(CCMathUtil::projectClippedRect(layer->renderTarget()->renderSurface()->drawTransform().inverse(), layer->renderTarget()->renderSurface()->clipRect())); |
| 130 | targetSurfaceClipRect.intersect(layer->drawableContentRect()); |
| 131 | } |
| 132 | |
| 133 | if (targetSurfaceClipRect.isEmpty()) |
| 134 | return IntRect(); |
| 135 | |
| 136 | return CCLayerTreeHostCommon::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 | |
| 147 | static inline bool transformToParentIsKnown(CCLayerImpl*) |
| 148 | { |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | static inline bool transformToParentIsKnown(LayerChromium* layer) |
| 153 | { |
| 154 | return !layer->transformIsAnimating(); |
| 155 | } |
| 156 | |
| 157 | static inline bool transformToScreenIsKnown(CCLayerImpl*) |
| 158 | { |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | static inline bool transformToScreenIsKnown(LayerChromium* layer) |
| 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()) { |
| 189 | ASSERT(layer->parent()); |
| 190 | ASSERT(!layer->parent()->useParentBackfaceVisibility()); |
| 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 | |
| 201 | static inline bool subtreeShouldBeSkipped(CCLayerImpl* layer) |
| 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 | |
| 209 | static inline bool subtreeShouldBeSkipped(LayerChromium* layer) |
| 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 | // |
| 229 | // A layer and its descendants should render onto a new RenderSurface if any of these rules hold: |
| 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. |
| 245 | if (!layer->filters().isEmpty() || !layer->backgroundFilters().isEmpty()) |
| 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 | |
| 264 | WebTransformationMatrix computeScrollCompensationForThisLayer(CCLayerImpl* scrollingLayer, const WebTransformationMatrix& parentMatrix) |
| 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 | |
| 292 | WebTransformationMatrix computeScrollCompensationMatrixForChildren(LayerChromium* currentLayer, const WebTransformationMatrix& currentParentMatrix, const WebTransformationMatrix& currentScrollCompensation) |
| 293 | { |
| 294 | // The main thread (i.e. LayerChromium) does not need to worry about scroll compensation. |
| 295 | // So we can just return an identity matrix here. |
| 296 | return WebTransformationMatrix(); |
| 297 | } |
| 298 | |
| 299 | WebTransformationMatrix computeScrollCompensationMatrixForChildren(CCLayerImpl* layer, const WebTransformationMatrix& parentMatrix, const WebTransformationMatrix& currentScrollCompensationMatrix) |
| 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] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 356 | ASSERT(renderSurfaceLayerList.empty()); |
| 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()) |
| 503 | CCMathUtil::flattenTransformTo2d(layerScreenSpaceTransform); |
| 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 | |
| 516 | // fullHierarchyMatrix is the matrix that transforms objects between screen space (except projection matrix) and the most recent RenderSurface's space. |
| 517 | // nextHierarchyMatrix will only change if this layer uses a new RenderSurface, otherwise remains the same. |
| 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 |
| 560 | // newly created RenderSurface. |
| 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 | |
| 579 | if (layer->filters().hasFilterThatMovesPixels()) |
| 580 | nearestAncestorThatMovesPixels = renderSurface; |
| 581 | |
[email protected] | 9bab0bb | 2012-10-08 19:11:58 | [diff] [blame] | 582 | // The render surface clipRect is expressed in the space where this surface draws, i.e. the same space as clipRectFromAncestor. |
| 583 | if (ancestorClipsSubtree) |
| 584 | renderSurface->setClipRect(clipRectFromAncestor); |
| 585 | else |
| 586 | renderSurface->setClipRect(IntRect()); |
| 587 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 588 | renderSurface->setNearestAncestorThatMovesPixels(nearestAncestorThatMovesPixels); |
| 589 | |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 590 | renderSurfaceLayerList.push_back(layer); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 591 | } else { |
| 592 | layer->setDrawTransform(drawTransform); |
| 593 | layer->setDrawTransformIsAnimating(animatingTransformToTarget); |
| 594 | layer->setScreenSpaceTransformIsAnimating(animatingTransformToScreen); |
| 595 | sublayerMatrix = combinedTransform; |
| 596 | |
| 597 | layer->setDrawOpacity(drawOpacity); |
| 598 | layer->setDrawOpacityIsAnimating(drawOpacityIsAnimating); |
| 599 | |
| 600 | if (layer != rootLayer) { |
| 601 | ASSERT(layer->parent()); |
| 602 | layer->clearRenderSurface(); |
| 603 | |
| 604 | // Layers without renderSurfaces directly inherit the ancestor's clip status. |
| 605 | subtreeShouldBeClipped = ancestorClipsSubtree; |
| 606 | if (ancestorClipsSubtree) |
| 607 | clipRectForSubtree = clipRectFromAncestor; |
| 608 | |
| 609 | // Layers that are not their own renderTarget will render into the target of their nearest ancestor. |
| 610 | layer->setRenderTarget(layer->parent()->renderTarget()); |
| 611 | } else { |
[email protected] | 23bbb41 | 2012-08-30 20:03:38 | [diff] [blame] | 612 | // FIXME: This root layer special case code should eventually go away. https://bugs.webkit.org/show_bug.cgi?id=92290 |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 613 | ASSERT(!layer->parent()); |
| 614 | ASSERT(layer->renderSurface()); |
| 615 | ASSERT(ancestorClipsSubtree); |
| 616 | layer->renderSurface()->setClipRect(clipRectFromAncestor); |
[email protected] | 23bbb41 | 2012-08-30 20:03:38 | [diff] [blame] | 617 | subtreeShouldBeClipped = false; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | |
| 621 | IntRect rectInTargetSpace = enclosingIntRect(CCMathUtil::mapClippedRect(layer->drawTransform(), contentRect)); |
| 622 | |
| 623 | if (layerClipsSubtree(layer)) { |
| 624 | subtreeShouldBeClipped = true; |
| 625 | if (ancestorClipsSubtree && !layer->renderSurface()) { |
| 626 | clipRectForSubtree = clipRectFromAncestor; |
| 627 | clipRectForSubtree.intersect(rectInTargetSpace); |
| 628 | } else |
| 629 | clipRectForSubtree = rectInTargetSpace; |
| 630 | } |
| 631 | |
| 632 | // Flatten to 2D if the layer doesn't preserve 3D. |
| 633 | if (!layer->preserves3D()) |
| 634 | CCMathUtil::flattenTransformTo2d(sublayerMatrix); |
| 635 | |
| 636 | // Apply the sublayer transform at the center of the layer. |
[email protected] | b8d3c02 | 2012-10-08 17:38:04 | [diff] [blame] | 637 | sublayerMatrix.translate(0.5 * bounds.width(), 0.5 * bounds.height()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 638 | sublayerMatrix.multiply(layer->sublayerTransform()); |
[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 | |
| 641 | LayerList& descendants = (layer->renderSurface() ? layer->renderSurface()->layerList() : layerList); |
| 642 | |
| 643 | // Any layers that are appended after this point are in the layer's subtree and should be included in the sorting process. |
| 644 | unsigned sortingStartIndex = descendants.size(); |
| 645 | |
| 646 | if (!layerShouldBeSkipped(layer)) |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 647 | descendants.push_back(layer); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 648 | |
| 649 | WebTransformationMatrix nextScrollCompensationMatrix = computeScrollCompensationMatrixForChildren(layer, parentMatrix, currentScrollCompensationMatrix);; |
| 650 | |
| 651 | IntRect accumulatedDrawableContentRectOfChildren; |
| 652 | for (size_t i = 0; i < layer->children().size(); ++i) { |
[email protected] | 0920e24f | 2012-09-20 03:34:03 | [diff] [blame] | 653 | LayerType* child = CCLayerTreeHostCommon::getChildAsRawPtr(layer->children(), i); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 654 | IntRect drawableContentRectOfChildSubtree; |
| 655 | calculateDrawTransformsInternal<LayerType, LayerList, RenderSurfaceType, LayerSorter>(child, rootLayer, sublayerMatrix, nextHierarchyMatrix, nextScrollCompensationMatrix, |
| 656 | clipRectForSubtree, subtreeShouldBeClipped, nearestAncestorThatMovesPixels, |
[email protected] | f3922f2 | 2012-10-12 09:20:38 | [diff] [blame] | 657 | renderSurfaceLayerList, descendants, layerSorter, maxTextureSize, deviceScaleFactor, drawableContentRectOfChildSubtree); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 658 | if (!drawableContentRectOfChildSubtree.isEmpty()) { |
| 659 | accumulatedDrawableContentRectOfChildren.unite(drawableContentRectOfChildSubtree); |
| 660 | if (child->renderSurface()) |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 661 | descendants.push_back(child); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
| 665 | // Compute the total drawableContentRect for this subtree (the rect is in targetSurface space) |
| 666 | IntRect localDrawableContentRectOfSubtree = accumulatedDrawableContentRectOfChildren; |
| 667 | if (layer->drawsContent()) |
| 668 | localDrawableContentRectOfSubtree.unite(rectInTargetSpace); |
| 669 | if (subtreeShouldBeClipped) |
| 670 | localDrawableContentRectOfSubtree.intersect(clipRectForSubtree); |
| 671 | |
| 672 | // Compute the layer's drawable content rect (the rect is in targetSurface space) |
| 673 | IntRect drawableContentRectOfLayer = rectInTargetSpace; |
| 674 | if (subtreeShouldBeClipped) |
| 675 | drawableContentRectOfLayer.intersect(clipRectForSubtree); |
| 676 | layer->setDrawableContentRect(drawableContentRectOfLayer); |
| 677 | |
[email protected] | 9bab0bb | 2012-10-08 19:11:58 | [diff] [blame] | 678 | // Compute the layer's visible content rect (the rect is in content space) |
| 679 | IntRect visibleContentRectOfLayer = calculateVisibleContentRect(layer); |
| 680 | layer->setVisibleContentRect(visibleContentRectOfLayer); |
| 681 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 682 | // Compute the remaining properties for the render surface, if the layer has one. |
| 683 | if (layer->renderSurface() && layer != rootLayer) { |
| 684 | RenderSurfaceType* renderSurface = layer->renderSurface(); |
| 685 | IntRect clippedContentRect = localDrawableContentRectOfSubtree; |
| 686 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 687 | // Don't clip if the layer is reflected as the reflection shouldn't be |
| 688 | // clipped. If the layer is animating, then the surface's transform to |
| 689 | // its target is not known on the main thread, and we should not use it |
| 690 | // to clip. |
| 691 | if (!layer->replicaLayer() && transformToParentIsKnown(layer)) { |
| 692 | // Note, it is correct to use ancestorClipsSubtree here, because we are looking at this layer's renderSurface, not the layer itself. |
| 693 | if (ancestorClipsSubtree && !clippedContentRect.isEmpty()) { |
| 694 | IntRect surfaceClipRect = CCLayerTreeHostCommon::calculateVisibleRect(renderSurface->clipRect(), clippedContentRect, renderSurface->drawTransform()); |
| 695 | clippedContentRect.intersect(surfaceClipRect); |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | // The RenderSurface backing texture cannot exceed the maximum supported |
| 700 | // texture size. |
| 701 | clippedContentRect.setWidth(std::min(clippedContentRect.width(), maxTextureSize)); |
| 702 | clippedContentRect.setHeight(std::min(clippedContentRect.height(), maxTextureSize)); |
| 703 | |
| 704 | if (clippedContentRect.isEmpty()) |
[email protected] | 7d929c0 | 2012-09-20 17:26:57 | [diff] [blame] | 705 | renderSurface->clearLayerLists(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 706 | |
| 707 | renderSurface->setContentRect(clippedContentRect); |
[email protected] | f3922f2 | 2012-10-12 09:20:38 | [diff] [blame] | 708 | renderSurface->setScreenSpaceTransform(layer->screenSpaceTransform()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 709 | |
| 710 | if (layer->replicaLayer()) { |
| 711 | WebTransformationMatrix surfaceOriginToReplicaOriginTransform; |
[email protected] | f3922f2 | 2012-10-12 09:20:38 | [diff] [blame] | 712 | surfaceOriginToReplicaOriginTransform.scale(deviceScaleFactor); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 713 | surfaceOriginToReplicaOriginTransform.translate(layer->replicaLayer()->position().x() + layer->replicaLayer()->anchorPoint().x() * bounds.width(), |
| 714 | layer->replicaLayer()->position().y() + layer->replicaLayer()->anchorPoint().y() * bounds.height()); |
| 715 | surfaceOriginToReplicaOriginTransform.multiply(layer->replicaLayer()->transform()); |
| 716 | 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] | 717 | surfaceOriginToReplicaOriginTransform.scale(1 / deviceScaleFactor); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 718 | |
| 719 | // Compute the replica's "originTransform" that maps from the replica's origin space to the target surface origin space. |
| 720 | WebTransformationMatrix replicaOriginTransform = layer->renderSurface()->drawTransform() * surfaceOriginToReplicaOriginTransform; |
| 721 | renderSurface->setReplicaDrawTransform(replicaOriginTransform); |
| 722 | |
| 723 | // Compute the replica's "screenSpaceTransform" that maps from the replica's origin space to the screen's origin space. |
| 724 | WebTransformationMatrix replicaScreenSpaceTransform = layer->renderSurface()->screenSpaceTransform() * surfaceOriginToReplicaOriginTransform; |
| 725 | renderSurface->setReplicaScreenSpaceTransform(replicaScreenSpaceTransform); |
| 726 | } |
| 727 | |
| 728 | // If a render surface has no layer list, then it and none of its children needed to get drawn. |
| 729 | if (!layer->renderSurface()->layerList().size()) { |
| 730 | // FIXME: Originally we asserted that this layer was already at the end of the |
| 731 | // list, and only needed to remove that layer. For now, we remove the |
| 732 | // entire subtree of surfaces to fix a crash bug. The root cause is |
| 733 | // https://bugs.webkit.org/show_bug.cgi?id=74147 and we should be able |
| 734 | // to put the original assert after fixing that. |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 735 | while (renderSurfaceLayerList.back() != layer) { |
| 736 | renderSurfaceLayerList.back()->clearRenderSurface(); |
| 737 | renderSurfaceLayerList.pop_back(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 738 | } |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 739 | ASSERT(renderSurfaceLayerList.back() == layer); |
| 740 | renderSurfaceLayerList.pop_back(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 741 | layer->clearRenderSurface(); |
| 742 | return; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | // If neither this layer nor any of its children were added, early out. |
| 747 | if (sortingStartIndex == descendants.size()) |
| 748 | return; |
| 749 | |
| 750 | // If preserves-3d then sort all the descendants in 3D so that they can be |
| 751 | // drawn from back to front. If the preserves-3d property is also set on the parent then |
| 752 | // skip the sorting as the parent will sort all the descendants anyway. |
| 753 | if (descendants.size() && layer->preserves3D() && (!layer->parent() || !layer->parent()->preserves3D())) |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 754 | sortLayers(descendants.begin() + sortingStartIndex, descendants.end(), layerSorter); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 755 | |
| 756 | if (layer->renderSurface()) |
| 757 | drawableContentRectOfSubtree = enclosingIntRect(layer->renderSurface()->drawableContentRect()); |
| 758 | else |
| 759 | drawableContentRectOfSubtree = localDrawableContentRectOfSubtree; |
| 760 | |
[email protected] | 7d929c0 | 2012-09-20 17:26:57 | [diff] [blame] | 761 | if (layer->hasContributingDelegatedRenderPasses()) |
| 762 | layer->renderTarget()->renderSurface()->addContributingDelegatedRenderPassLayer(layer); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 763 | } |
| 764 | |
[email protected] | f3922f2 | 2012-10-12 09:20:38 | [diff] [blame] | 765 | void CCLayerTreeHostCommon::calculateDrawTransforms(LayerChromium* rootLayer, const IntSize& deviceViewportSize, float deviceScaleFactor, int maxTextureSize, std::vector<scoped_refptr<LayerChromium> >& renderSurfaceLayerList) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 766 | { |
| 767 | IntRect totalDrawableContentRect; |
| 768 | WebTransformationMatrix identityMatrix; |
| 769 | WebTransformationMatrix deviceScaleTransform; |
| 770 | deviceScaleTransform.scale(deviceScaleFactor); |
| 771 | |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 772 | setupRootLayerAndSurfaceForRecursion<LayerChromium, std::vector<scoped_refptr<LayerChromium> > >(rootLayer, renderSurfaceLayerList, deviceViewportSize); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 773 | |
[email protected] | f3922f2 | 2012-10-12 09:20:38 | [diff] [blame] | 774 | cc::calculateDrawTransformsInternal<LayerChromium, std::vector<scoped_refptr<LayerChromium> >, RenderSurfaceChromium, void>(rootLayer, rootLayer, deviceScaleTransform, identityMatrix, identityMatrix, |
| 775 | rootLayer->renderSurface()->contentRect(), true, 0, renderSurfaceLayerList, |
| 776 | rootLayer->renderSurface()->layerList(), 0, maxTextureSize, deviceScaleFactor, totalDrawableContentRect); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 777 | } |
| 778 | |
[email protected] | f3922f2 | 2012-10-12 09:20:38 | [diff] [blame] | 779 | void CCLayerTreeHostCommon::calculateDrawTransforms(CCLayerImpl* rootLayer, const IntSize& deviceViewportSize, float deviceScaleFactor, CCLayerSorter* layerSorter, int maxTextureSize, std::vector<CCLayerImpl*>& renderSurfaceLayerList) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 780 | { |
| 781 | IntRect totalDrawableContentRect; |
| 782 | WebTransformationMatrix identityMatrix; |
| 783 | WebTransformationMatrix deviceScaleTransform; |
| 784 | deviceScaleTransform.scale(deviceScaleFactor); |
| 785 | |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 786 | setupRootLayerAndSurfaceForRecursion<CCLayerImpl, std::vector<CCLayerImpl*> >(rootLayer, renderSurfaceLayerList, deviceViewportSize); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 787 | |
[email protected] | f3922f2 | 2012-10-12 09:20:38 | [diff] [blame] | 788 | cc::calculateDrawTransformsInternal<CCLayerImpl, std::vector<CCLayerImpl*>, CCRenderSurface, CCLayerSorter>(rootLayer, rootLayer, deviceScaleTransform, identityMatrix, identityMatrix, |
| 789 | rootLayer->renderSurface()->contentRect(), true, 0, renderSurfaceLayerList, |
| 790 | rootLayer->renderSurface()->layerList(), layerSorter, maxTextureSize, deviceScaleFactor, totalDrawableContentRect); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 791 | } |
| 792 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 793 | static bool pointHitsRect(const IntPoint& viewportPoint, const WebTransformationMatrix& localSpaceToScreenSpaceTransform, FloatRect localSpaceRect) |
| 794 | { |
| 795 | // If the transform is not invertible, then assume that this point doesn't hit this rect. |
| 796 | if (!localSpaceToScreenSpaceTransform.isInvertible()) |
| 797 | return false; |
| 798 | |
| 799 | // Transform the hit test point from screen space to the local space of the given rect. |
| 800 | bool clipped = false; |
| 801 | FloatPoint hitTestPointInLocalSpace = CCMathUtil::projectPoint(localSpaceToScreenSpaceTransform.inverse(), FloatPoint(viewportPoint), clipped); |
| 802 | |
| 803 | // If projectPoint could not project to a valid value, then we assume that this point doesn't hit this rect. |
| 804 | if (clipped) |
| 805 | return false; |
| 806 | |
| 807 | return localSpaceRect.contains(hitTestPointInLocalSpace); |
| 808 | } |
| 809 | |
| 810 | static bool pointIsClippedBySurfaceOrClipRect(const IntPoint& viewportPoint, CCLayerImpl* layer) |
| 811 | { |
| 812 | CCLayerImpl* currentLayer = layer; |
| 813 | |
| 814 | // Walk up the layer tree and hit-test any renderSurfaces and any layer clipRects that are active. |
| 815 | while (currentLayer) { |
| 816 | if (currentLayer->renderSurface() && !pointHitsRect(viewportPoint, currentLayer->renderSurface()->screenSpaceTransform(), currentLayer->renderSurface()->contentRect())) |
| 817 | return true; |
| 818 | |
| 819 | // Note that drawableContentRects are actually in targetSurface space, so the transform we |
| 820 | // have to provide is the target surface's screenSpaceTransform. |
| 821 | CCLayerImpl* renderTarget = currentLayer->renderTarget(); |
| 822 | if (layerClipsSubtree(currentLayer) && !pointHitsRect(viewportPoint, renderTarget->renderSurface()->screenSpaceTransform(), currentLayer->drawableContentRect())) |
| 823 | return true; |
| 824 | |
| 825 | currentLayer = currentLayer->parent(); |
| 826 | } |
| 827 | |
| 828 | // If we have finished walking all ancestors without having already exited, then the point is not clipped by any ancestors. |
| 829 | return false; |
| 830 | } |
| 831 | |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 832 | CCLayerImpl* CCLayerTreeHostCommon::findLayerThatIsHitByPoint(const IntPoint& viewportPoint, std::vector<CCLayerImpl*>& renderSurfaceLayerList) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 833 | { |
| 834 | CCLayerImpl* foundLayer = 0; |
| 835 | |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 836 | typedef CCLayerIterator<CCLayerImpl, std::vector<CCLayerImpl*>, CCRenderSurface, CCLayerIteratorActions::FrontToBack> CCLayerIteratorType; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 837 | CCLayerIteratorType end = CCLayerIteratorType::end(&renderSurfaceLayerList); |
| 838 | |
| 839 | for (CCLayerIteratorType it = CCLayerIteratorType::begin(&renderSurfaceLayerList); it != end; ++it) { |
| 840 | // We don't want to consider renderSurfaces for hit testing. |
| 841 | if (!it.representsItself()) |
| 842 | continue; |
| 843 | |
| 844 | CCLayerImpl* currentLayer = (*it); |
| 845 | |
| 846 | FloatRect contentRect(FloatPoint::zero(), currentLayer->contentBounds()); |
| 847 | if (!pointHitsRect(viewportPoint, currentLayer->screenSpaceTransform(), contentRect)) |
| 848 | continue; |
| 849 | |
| 850 | // At this point, we think the point does hit the layer, but we need to walk up |
| 851 | // the parents to ensure that the layer was not clipped in such a way that the |
| 852 | // hit point actually should not hit the layer. |
| 853 | if (pointIsClippedBySurfaceOrClipRect(viewportPoint, currentLayer)) |
| 854 | continue; |
| 855 | |
| 856 | foundLayer = currentLayer; |
| 857 | break; |
| 858 | } |
| 859 | |
| 860 | // This can potentially return 0, which means the viewportPoint did not successfully hit test any layers, not even the root layer. |
| 861 | return foundLayer; |
| 862 | } |
| 863 | |
[email protected] | 9c88e56 | 2012-09-14 22:21:30 | [diff] [blame] | 864 | } // namespace cc |