blob: 9147ecddcc625ea6701ed6738b85b59d5d257471 [file] [log] [blame]
[email protected]94f206c12012-08-25 00:09:141// 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]d50c6862012-10-23 02:08:315#include "cc/layer_tree_host_common.h"
[email protected]94f206c12012-08-25 00:09:146
[email protected]a8461d82012-10-16 21:11:147#include "cc/layer.h"
[email protected]d50c6862012-10-23 02:08:318#include "cc/layer_impl.h"
9#include "cc/layer_iterator.h"
10#include "cc/layer_sorter.h"
[email protected]55a124d02012-10-22 03:07:1311#include "cc/math_util.h"
[email protected]a8461d82012-10-16 21:11:1412#include "cc/render_surface.h"
[email protected]d50c6862012-10-23 02:08:3113#include "cc/render_surface_impl.h"
[email protected]aad0a0072012-11-01 18:15:5814#include "ui/gfx/rect_conversions.h"
[email protected]518ee582012-10-24 18:29:4415#include <algorithm>
[email protected]94f206c12012-08-25 00:09:1416#include <public/WebTransformationMatrix.h>
17
18using WebKit::WebTransformationMatrix;
19
[email protected]9c88e562012-09-14 22:21:3020namespace cc {
[email protected]94f206c12012-08-25 00:09:1421
[email protected]96baf3e2012-10-22 23:09:5522ScrollAndScaleSet::ScrollAndScaleSet()
[email protected]493067512012-09-19 23:34:1023{
24}
25
[email protected]96baf3e2012-10-22 23:09:5526ScrollAndScaleSet::~ScrollAndScaleSet()
[email protected]493067512012-09-19 23:34:1027{
28}
29
[email protected]aad0a0072012-11-01 18:15:5830gfx::Rect LayerTreeHostCommon::calculateVisibleRect(const gfx::Rect& targetSurfaceRect, const gfx::Rect& layerBoundRect, const WebTransformationMatrix& transform)
[email protected]94f206c12012-08-25 00:09:1431{
32 // Is this layer fully contained within the target surface?
[email protected]aad0a0072012-11-01 18:15:5833 gfx::Rect layerInSurfaceSpace = MathUtil::mapClippedRect(transform, layerBoundRect);
34 if (targetSurfaceRect.Contains(layerInSurfaceSpace))
[email protected]94f206c12012-08-25 00:09:1435 return layerBoundRect;
36
37 // If the layer doesn't fill up the entire surface, then find the part of
38 // the surface rect where the layer could be visible. This avoids trying to
39 // project surface rect points that are behind the projection point.
[email protected]aad0a0072012-11-01 18:15:5840 gfx::Rect minimalSurfaceRect = targetSurfaceRect;
41 minimalSurfaceRect.Intersect(layerInSurfaceSpace);
[email protected]94f206c12012-08-25 00:09:1442
43 // Project the corners of the target surface rect into the layer space.
44 // This bounding rectangle may be larger than it needs to be (being
45 // axis-aligned), but is a reasonable filter on the space to consider.
46 // Non-invertible transforms will create an empty rect here.
47 const WebTransformationMatrix surfaceToLayer = transform.inverse();
[email protected]aad0a0072012-11-01 18:15:5848 gfx::Rect layerRect = gfx::ToEnclosingRect(MathUtil::projectClippedRect(surfaceToLayer, gfx::RectF(minimalSurfaceRect)));
49 layerRect.Intersect(layerBoundRect);
[email protected]94f206c12012-08-25 00:09:1450 return layerRect;
51}
52
[email protected]ecc12622012-10-30 20:45:4253template <typename LayerType>
54static inline bool isRootLayer(LayerType* layer)
55{
56 return !layer->parent();
57}
58
[email protected]94f206c12012-08-25 00:09:1459template<typename LayerType>
60static inline bool layerIsInExisting3DRenderingContext(LayerType* layer)
61{
62 // According to current W3C spec on CSS transforms, a layer is part of an established
63 // 3d rendering context if its parent has transform-style of preserves-3d.
64 return layer->parent() && layer->parent()->preserves3D();
65}
66
67template<typename LayerType>
[email protected]ecc12622012-10-30 20:45:4268static bool isRootLayerOfNewRenderingContext(LayerType* layer)
[email protected]94f206c12012-08-25 00:09:1469{
70 // According to current W3C spec on CSS transforms (Section 6.1), a layer is the
71 // beginning of 3d rendering context if its parent does not have transform-style:
72 // preserve-3d, but this layer itself does.
73 if (layer->parent())
74 return !layer->parent()->preserves3D() && layer->preserves3D();
75
76 return layer->preserves3D();
77}
78
79template<typename LayerType>
80static bool isLayerBackFaceVisible(LayerType* layer)
81{
82 // The current W3C spec on CSS transforms says that backface visibility should be
83 // determined differently depending on whether the layer is in a "3d rendering
84 // context" or not. For Chromium code, we can determine whether we are in a 3d
85 // rendering context by checking if the parent preserves 3d.
86
87 if (layerIsInExisting3DRenderingContext(layer))
88 return layer->drawTransform().isBackFaceVisible();
89
90 // In this case, either the layer establishes a new 3d rendering context, or is not in
91 // a 3d rendering context at all.
92 return layer->transform().isBackFaceVisible();
93}
94
95template<typename LayerType>
96static bool isSurfaceBackFaceVisible(LayerType* layer, const WebTransformationMatrix& drawTransform)
97{
98 if (layerIsInExisting3DRenderingContext(layer))
99 return drawTransform.isBackFaceVisible();
100
[email protected]ecc12622012-10-30 20:45:42101 if (isRootLayerOfNewRenderingContext(layer))
[email protected]94f206c12012-08-25 00:09:14102 return layer->transform().isBackFaceVisible();
103
104 // If the renderSurface is not part of a new or existing rendering context, then the
105 // layers that contribute to this surface will decide back-face visibility for themselves.
106 return false;
107}
108
109template<typename LayerType>
110static inline bool layerClipsSubtree(LayerType* layer)
111{
112 return layer->masksToBounds() || layer->maskLayer();
113}
114
115template<typename LayerType>
[email protected]aad0a0072012-11-01 18:15:58116static gfx::Rect calculateVisibleContentRect(LayerType* layer)
[email protected]94f206c12012-08-25 00:09:14117{
[email protected]1d993172012-10-18 18:15:04118 DCHECK(layer->renderTarget());
[email protected]94f206c12012-08-25 00:09:14119
[email protected]9bab0bb2012-10-08 19:11:58120 // Nothing is visible if the layer bounds are empty.
[email protected]aad0a0072012-11-01 18:15:58121 if (!layer->drawsContent() || layer->contentBounds().IsEmpty() || layer->drawableContentRect().IsEmpty())
122 return gfx::Rect();
[email protected]94f206c12012-08-25 00:09:14123
[email protected]aad0a0072012-11-01 18:15:58124 gfx::Rect targetSurfaceClipRect;
[email protected]9bab0bb2012-10-08 19:11:58125
126 // First, compute visible bounds in target surface space.
[email protected]aad0a0072012-11-01 18:15:58127 if (layer->renderTarget()->renderSurface()->clipRect().IsEmpty())
[email protected]9bab0bb2012-10-08 19:11:58128 targetSurfaceClipRect = layer->drawableContentRect();
129 else {
130 // In this case the target surface does clip layers that contribute to it. So, we
131 // have convert the current surface's clipRect from its ancestor surface space to
132 // the current surface space.
[email protected]aad0a0072012-11-01 18:15:58133 targetSurfaceClipRect = gfx::ToEnclosingRect(MathUtil::projectClippedRect(layer->renderTarget()->renderSurface()->drawTransform().inverse(), layer->renderTarget()->renderSurface()->clipRect()));
134 targetSurfaceClipRect.Intersect(layer->drawableContentRect());
[email protected]9bab0bb2012-10-08 19:11:58135 }
136
[email protected]aad0a0072012-11-01 18:15:58137 if (targetSurfaceClipRect.IsEmpty())
138 return gfx::Rect();
[email protected]9bab0bb2012-10-08 19:11:58139
[email protected]aad0a0072012-11-01 18:15:58140 return LayerTreeHostCommon::calculateVisibleRect(targetSurfaceClipRect, gfx::Rect(gfx::Point(), layer->contentBounds()), layer->drawTransform());
[email protected]94f206c12012-08-25 00:09:14141}
142
143static bool isScaleOrTranslation(const WebTransformationMatrix& m)
144{
145 return !m.m12() && !m.m13() && !m.m14()
146 && !m.m21() && !m.m23() && !m.m24()
147 && !m.m31() && !m.m32() && !m.m43()
148 && m.m44();
149}
150
[email protected]96baf3e2012-10-22 23:09:55151static inline bool transformToParentIsKnown(LayerImpl*)
[email protected]94f206c12012-08-25 00:09:14152{
153 return true;
154}
155
[email protected]96baf3e2012-10-22 23:09:55156static inline bool transformToParentIsKnown(Layer* layer)
[email protected]94f206c12012-08-25 00:09:14157{
158 return !layer->transformIsAnimating();
159}
160
[email protected]96baf3e2012-10-22 23:09:55161static inline bool transformToScreenIsKnown(LayerImpl*)
[email protected]94f206c12012-08-25 00:09:14162{
163 return true;
164}
165
[email protected]96baf3e2012-10-22 23:09:55166static inline bool transformToScreenIsKnown(Layer* layer)
[email protected]94f206c12012-08-25 00:09:14167{
168 return !layer->screenSpaceTransformIsAnimating();
169}
170
171template<typename LayerType>
172static bool layerShouldBeSkipped(LayerType* layer)
173{
174 // Layers can be skipped if any of these conditions are met.
175 // - does not draw content.
176 // - is transparent
177 // - has empty bounds
178 // - the layer is not double-sided, but its back face is visible.
179 //
180 // Some additional conditions need to be computed at a later point after the recursion is finished.
181 // - the intersection of render surface content and layer clipRect is empty
182 // - the visibleContentRect is empty
183 //
184 // Note, if the layer should not have been drawn due to being fully transparent,
185 // we would have skipped the entire subtree and never made it into this function,
186 // so it is safe to omit this check here.
187
[email protected]aad0a0072012-11-01 18:15:58188 if (!layer->drawsContent() || layer->bounds().IsEmpty())
[email protected]94f206c12012-08-25 00:09:14189 return true;
190
191 LayerType* backfaceTestLayer = layer;
192 if (layer->useParentBackfaceVisibility()) {
[email protected]1d993172012-10-18 18:15:04193 DCHECK(layer->parent());
194 DCHECK(!layer->parent()->useParentBackfaceVisibility());
[email protected]94f206c12012-08-25 00:09:14195 backfaceTestLayer = layer->parent();
196 }
197
198 // 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.
199 if (!backfaceTestLayer->doubleSided() && transformToScreenIsKnown(backfaceTestLayer) && isLayerBackFaceVisible(backfaceTestLayer))
200 return true;
201
202 return false;
203}
204
[email protected]96baf3e2012-10-22 23:09:55205static inline bool subtreeShouldBeSkipped(LayerImpl* layer)
[email protected]94f206c12012-08-25 00:09:14206{
207 // The opacity of a layer always applies to its children (either implicitly
208 // via a render surface or explicitly if the parent preserves 3D), so the
209 // entire subtree can be skipped if this layer is fully transparent.
210 return !layer->opacity();
211}
212
[email protected]96baf3e2012-10-22 23:09:55213static inline bool subtreeShouldBeSkipped(Layer* layer)
[email protected]94f206c12012-08-25 00:09:14214{
215 // If the opacity is being animated then the opacity on the main thread is unreliable
216 // (since the impl thread may be using a different opacity), so it should not be trusted.
217 // In particular, it should not cause the subtree to be skipped.
218 return !layer->opacity() && !layer->opacityIsAnimating();
219}
220
221template<typename LayerType>
222static bool subtreeShouldRenderToSeparateSurface(LayerType* layer, bool axisAlignedWithRespectToParent)
223{
[email protected]94f206c12012-08-25 00:09:14224 //
[email protected]96baf3e2012-10-22 23:09:55225 // A layer and its descendants should render onto a new RenderSurfaceImpl if any of these rules hold:
[email protected]94f206c12012-08-25 00:09:14226 //
227
[email protected]ecc12622012-10-30 20:45:42228 // The root layer should always have a renderSurface.
229 if (isRootLayer(layer))
230 return true;
231
[email protected]94f206c12012-08-25 00:09:14232 // 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]4000abf2012-10-23 04:45:45245 if (!layer->filters().isEmpty() || !layer->backgroundFilters().isEmpty() || layer->filter())
[email protected]94f206c12012-08-25 00:09:14246 return true;
247
[email protected]ecc12622012-10-30 20:45:42248 // Cache this value, because otherwise it walks the entire subtree several times.
249 bool descendantDrawsContent = layer->descendantDrawsContent();
250
[email protected]94f206c12012-08-25 00:09:14251 // If the layer flattens its subtree (i.e. the layer doesn't preserve-3d), but it is
252 // treated as a 3D object by its parent (i.e. parent does preserve-3d).
253 if (layerIsInExisting3DRenderingContext(layer) && !layer->preserves3D() && descendantDrawsContent)
254 return true;
255
256 // If the layer clips its descendants but it is not axis-aligned with respect to its parent.
257 if (layerClipsSubtree(layer) && !axisAlignedWithRespectToParent && descendantDrawsContent)
258 return true;
259
260 // If the layer has opacity != 1 and does not have a preserves-3d transform style.
261 if (layer->opacity() != 1 && !layer->preserves3D() && descendantDrawsContent)
262 return true;
263
264 return false;
265}
266
[email protected]96baf3e2012-10-22 23:09:55267WebTransformationMatrix computeScrollCompensationForThisLayer(LayerImpl* scrollingLayer, const WebTransformationMatrix& parentMatrix)
[email protected]94f206c12012-08-25 00:09:14268{
269 // For every layer that has non-zero scrollDelta, we have to compute a transform that can undo the
270 // scrollDelta translation. In particular, we want this matrix to premultiply a fixed-position layer's
271 // parentMatrix, so we design this transform in three steps as follows. The steps described here apply
272 // from right-to-left, so Step 1 would be the right-most matrix:
273 //
274 // Step 1. transform from target surface space to the exact space where scrollDelta is actually applied.
275 // -- this is inverse of the matrix in step 3
276 // Step 2. undo the scrollDelta
277 // -- this is just a translation by scrollDelta.
278 // Step 3. transform back to target surface space.
279 // -- this transform is the "partialLayerOriginTransform" = (parentMatrix * scale(layer->pageScaleDelta()));
280 //
281 // These steps create a matrix that both start and end in targetSurfaceSpace. So this matrix can
282 // pre-multiply any fixed-position layer's drawTransform to undo the scrollDeltas -- as long as
283 // that fixed position layer is fixed onto the same renderTarget as this scrollingLayer.
284 //
285
286 WebTransformationMatrix partialLayerOriginTransform = parentMatrix;
[email protected]1c0c9bc2012-10-08 22:41:48287 partialLayerOriginTransform.multiply(scrollingLayer->implTransform());
[email protected]94f206c12012-08-25 00:09:14288
289 WebTransformationMatrix scrollCompensationForThisLayer = partialLayerOriginTransform; // Step 3
[email protected]c9c1ebe2012-11-05 20:46:13290 scrollCompensationForThisLayer.translate(scrollingLayer->scrollDelta().x(), scrollingLayer->scrollDelta().y()); // Step 2
[email protected]94f206c12012-08-25 00:09:14291 scrollCompensationForThisLayer.multiply(partialLayerOriginTransform.inverse()); // Step 1
292 return scrollCompensationForThisLayer;
293}
294
[email protected]96baf3e2012-10-22 23:09:55295WebTransformationMatrix computeScrollCompensationMatrixForChildren(Layer* currentLayer, const WebTransformationMatrix& currentParentMatrix, const WebTransformationMatrix& currentScrollCompensation)
[email protected]94f206c12012-08-25 00:09:14296{
[email protected]96baf3e2012-10-22 23:09:55297 // The main thread (i.e. Layer) does not need to worry about scroll compensation.
[email protected]94f206c12012-08-25 00:09:14298 // So we can just return an identity matrix here.
299 return WebTransformationMatrix();
300}
301
[email protected]96baf3e2012-10-22 23:09:55302WebTransformationMatrix computeScrollCompensationMatrixForChildren(LayerImpl* layer, const WebTransformationMatrix& parentMatrix, const WebTransformationMatrix& currentScrollCompensationMatrix)
[email protected]94f206c12012-08-25 00:09:14303{
304 // "Total scroll compensation" is the transform needed to cancel out all scrollDelta translations that
305 // occurred since the nearest container layer, even if there are renderSurfaces in-between.
306 //
307 // There are some edge cases to be aware of, that are not explicit in the code:
308 // - A layer that is both a fixed-position and container should not be its own container, instead, that means
309 // it is fixed to an ancestor, and is a container for any fixed-position descendants.
310 // - A layer that is a fixed-position container and has a renderSurface should behave the same as a container
311 // without a renderSurface, the renderSurface is irrelevant in that case.
[email protected]ecc12622012-10-30 20:45:42312 // - A layer that does not have an explicit container is simply fixed to the viewport.
313 // (i.e. the root renderSurface.)
[email protected]94f206c12012-08-25 00:09:14314 // - If the fixed-position layer has its own renderSurface, then the renderSurface is
315 // the one who gets fixed.
316 //
317 // This function needs to be called AFTER layers create their own renderSurfaces.
318 //
319
320 // 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.
[email protected]c9c1ebe2012-11-05 20:46:13321 if (!layer->isContainerForFixedPositionLayers() && layer->scrollDelta().IsZero() && !layer->renderSurface())
[email protected]94f206c12012-08-25 00:09:14322 return currentScrollCompensationMatrix;
323
324 // Start as identity matrix.
325 WebTransformationMatrix nextScrollCompensationMatrix;
326
327 // If this layer is not a container, then it inherits the existing scroll compensations.
328 if (!layer->isContainerForFixedPositionLayers())
329 nextScrollCompensationMatrix = currentScrollCompensationMatrix;
330
331 // If the current layer has a non-zero scrollDelta, then we should compute its local scrollCompensation
332 // and accumulate it to the nextScrollCompensationMatrix.
[email protected]c9c1ebe2012-11-05 20:46:13333 if (!layer->scrollDelta().IsZero()) {
[email protected]94f206c12012-08-25 00:09:14334 WebTransformationMatrix scrollCompensationForThisLayer = computeScrollCompensationForThisLayer(layer, parentMatrix);
335 nextScrollCompensationMatrix.multiply(scrollCompensationForThisLayer);
336 }
337
338 // If the layer created its own renderSurface, we have to adjust nextScrollCompensationMatrix.
339 // The adjustment allows us to continue using the scrollCompensation on the next surface.
340 // Step 1 (right-most in the math): transform from the new surface to the original ancestor surface
341 // Step 2: apply the scroll compensation
342 // Step 3: transform back to the new surface.
343 if (layer->renderSurface() && !nextScrollCompensationMatrix.isIdentity())
344 nextScrollCompensationMatrix = layer->renderSurface()->drawTransform().inverse() * nextScrollCompensationMatrix * layer->renderSurface()->drawTransform();
345
346 return nextScrollCompensationMatrix;
347}
348
[email protected]518ee582012-10-24 18:29:44349// There is no contentsScale on impl thread.
[email protected]6a9cff92012-11-08 11:53:26350static inline void updateLayerContentsScale(LayerImpl*, const WebTransformationMatrix&, float, float, bool) { }
[email protected]518ee582012-10-24 18:29:44351
[email protected]6a9cff92012-11-08 11:53:26352static inline void updateLayerContentsScale(Layer* layer, const WebTransformationMatrix& combinedTransform, float deviceScaleFactor, float pageScaleFactor, bool animatingTransformToScreen)
[email protected]518ee582012-10-24 18:29:44353{
354 float rasterScale = layer->rasterScale();
355 if (!rasterScale) {
356 rasterScale = 1;
357
[email protected]6a9cff92012-11-08 11:53:26358 if (!animatingTransformToScreen && layer->automaticallyComputeRasterScale()) {
[email protected]aad0a0072012-11-01 18:15:58359 gfx::Vector2dF transformScale = MathUtil::computeTransform2dScaleComponents(combinedTransform);
[email protected]518ee582012-10-24 18:29:44360 float combinedScale = std::max(transformScale.x(), transformScale.y());
361 rasterScale = combinedScale / deviceScaleFactor;
362 if (!layer->boundsContainPageScale())
363 rasterScale /= pageScaleFactor;
[email protected]11ec92972012-11-10 03:06:21364 // Prevent scale factors below 1 from being used or saved.
365 if (rasterScale < 1)
366 rasterScale = 1;
367 else
368 layer->setRasterScale(rasterScale);
[email protected]518ee582012-10-24 18:29:44369 }
370 }
371
372 float contentsScale = rasterScale * deviceScaleFactor;
373 if (!layer->boundsContainPageScale())
374 contentsScale *= pageScaleFactor;
375 layer->setContentsScale(contentsScale);
376
377 Layer* maskLayer = layer->maskLayer();
378 if (maskLayer)
379 maskLayer->setContentsScale(contentsScale);
380
381 Layer* replicaMaskLayer = layer->replicaLayer() ? layer->replicaLayer()->maskLayer() : 0;
382 if (replicaMaskLayer)
383 replicaMaskLayer->setContentsScale(contentsScale);
384}
385
[email protected]94f206c12012-08-25 00:09:14386// Recursively walks the layer tree starting at the given node and computes all the
387// necessary transformations, clipRects, render surfaces, etc.
388template<typename LayerType, typename LayerList, typename RenderSurfaceType, typename LayerSorter>
[email protected]ecc12622012-10-30 20:45:42389static void calculateDrawTransformsInternal(LayerType* layer, const WebTransformationMatrix& parentMatrix,
[email protected]94f206c12012-08-25 00:09:14390 const WebTransformationMatrix& fullHierarchyMatrix, const WebTransformationMatrix& currentScrollCompensationMatrix,
[email protected]aad0a0072012-11-01 18:15:58391 const gfx::Rect& clipRectFromAncestor, bool ancestorClipsSubtree,
[email protected]94f206c12012-08-25 00:09:14392 RenderSurfaceType* nearestAncestorThatMovesPixels, LayerList& renderSurfaceLayerList, LayerList& layerList,
[email protected]aad0a0072012-11-01 18:15:58393 LayerSorter* layerSorter, int maxTextureSize, float deviceScaleFactor, float pageScaleFactor, gfx::Rect& drawableContentRectOfSubtree)
[email protected]94f206c12012-08-25 00:09:14394{
395 // This function computes the new matrix transformations recursively for this
396 // layer and all its descendants. It also computes the appropriate render surfaces.
397 // Some important points to remember:
398 //
399 // 0. Here, transforms are notated in Matrix x Vector order, and in words we describe what
400 // the transform does from left to right.
401 //
402 // 1. In our terminology, the "layer origin" refers to the top-left corner of a layer, and the
403 // positive Y-axis points downwards. This interpretation is valid because the orthographic
404 // projection applied at draw time flips the Y axis appropriately.
405 //
[email protected]aad0a0072012-11-01 18:15:58406 // 2. The anchor point, when given as a PointF object, is specified in "unit layer space",
[email protected]94f206c12012-08-25 00:09:14407 // where the bounds of the layer map to [0, 1]. However, as a WebTransformationMatrix object,
[email protected]b8d3c022012-10-08 17:38:04408 // the transform to the anchor point is specified in "layer space", where the bounds
[email protected]94f206c12012-08-25 00:09:14409 // of the layer map to [bounds.width(), bounds.height()].
410 //
411 // 3. Definition of various transforms used:
412 // M[parent] is the parent matrix, with respect to the nearest render surface, passed down recursively.
413 // M[root] is the full hierarchy, with respect to the root, passed down recursively.
414 // Tr[origin] is the translation matrix from the parent's origin to this layer's origin.
415 // Tr[origin2anchor] is the translation from the layer's origin to its anchor point
416 // Tr[origin2center] is the translation from the layer's origin to its center
417 // M[layer] is the layer's matrix (applied at the anchor point)
418 // M[sublayer] is the layer's sublayer transform (applied at the layer's center)
[email protected]b8d3c022012-10-08 17:38:04419 // S[layer2content] is the ratio of a layer's contentBounds() to its bounds().
[email protected]94f206c12012-08-25 00:09:14420 //
421 // Some composite transforms can help in understanding the sequence of transforms:
422 // compositeLayerTransform = Tr[origin2anchor] * M[layer] * Tr[origin2anchor].inverse()
423 // compositeSublayerTransform = Tr[origin2center] * M[sublayer] * Tr[origin2center].inverse()
424 //
425 // In words, the layer transform is applied about the anchor point, and the sublayer transform is
426 // applied about the center of the layer.
427 //
428 // 4. When a layer (or render surface) is drawn, it is drawn into a "target render surface". Therefore the draw
429 // transform does not necessarily transform from screen space to local layer space. Instead, the draw transform
430 // is the transform between the "target render surface space" and local layer space. Note that render surfaces,
431 // except for the root, also draw themselves into a different target render surface, and so their draw
432 // transform and origin transforms are also described with respect to the target.
433 //
434 // Using these definitions, then:
435 //
436 // The draw transform for the layer is:
[email protected]b8d3c022012-10-08 17:38:04437 // M[draw] = M[parent] * Tr[origin] * compositeLayerTransform * S[layer2content]
438 // = M[parent] * Tr[layer->position() + anchor] * M[layer] * Tr[anchor2origin] * S[layer2content]
[email protected]94f206c12012-08-25 00:09:14439 //
440 // Interpreting the math left-to-right, this transforms from the layer's render surface to the origin of the layer in content space.
441 //
442 // The screen space transform is:
[email protected]b8d3c022012-10-08 17:38:04443 // M[screenspace] = M[root] * Tr[origin] * compositeLayerTransform * S[layer2content]
444 // = M[root] * Tr[layer->position() + anchor] * M[layer] * Tr[anchor2origin] * S[layer2content]
[email protected]94f206c12012-08-25 00:09:14445 //
446 // 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.
447 //
448 // The transform hierarchy that is passed on to children (i.e. the child's parentMatrix) is:
449 // M[parent]_for_child = M[parent] * Tr[origin] * compositeLayerTransform * compositeSublayerTransform
[email protected]b8d3c022012-10-08 17:38:04450 // = M[parent] * Tr[layer->position() + anchor] * M[layer] * Tr[anchor2origin] * compositeSublayerTransform
[email protected]94f206c12012-08-25 00:09:14451 //
452 // and a similar matrix for the full hierarchy with respect to the root.
453 //
454 // Finally, note that the final matrix used by the shader for the layer is P * M[draw] * S . This final product
455 // is computed in drawTexturedQuad(), where:
456 // P is the projection matrix
[email protected]b8d3c022012-10-08 17:38:04457 // S is the scale adjustment (to scale up a canonical quad to the layer's size)
[email protected]94f206c12012-08-25 00:09:14458 //
459 // When a render surface has a replica layer, that layer's transform is used to draw a second copy of the surface.
460 // Transforms named here are relative to the surface, unless they specify they are relative to the replica layer.
461 //
462 // We will denote a scale by device scale S[deviceScale]
463 //
464 // The render surface draw transform to its target surface origin is:
465 // M[surfaceDraw] = M[owningLayer->Draw]
466 //
467 // The render surface origin transform to its the root (screen space) origin is:
468 // M[surface2root] = M[owningLayer->screenspace] * S[deviceScale].inverse()
469 //
470 // The replica draw transform to its target surface origin is:
471 // M[replicaDraw] = S[deviceScale] * M[surfaceDraw] * Tr[replica->position() + replica->anchor()] * Tr[replica] * Tr[origin2anchor].inverse() * S[contentsScale].inverse()
472 //
473 // The replica draw transform to the root (screen space) origin is:
474 // M[replica2root] = M[surface2root] * Tr[replica->position()] * Tr[replica] * Tr[origin2anchor].inverse()
475 //
476
477 // If we early-exit anywhere in this function, the drawableContentRect of this subtree should be considered empty.
[email protected]aad0a0072012-11-01 18:15:58478 drawableContentRectOfSubtree = gfx::Rect();
[email protected]94f206c12012-08-25 00:09:14479
[email protected]ecc12622012-10-30 20:45:42480 // The root layer cannot skip calcDrawTransforms.
481 if (!isRootLayer(layer) && subtreeShouldBeSkipped(layer))
[email protected]94f206c12012-08-25 00:09:14482 return;
483
[email protected]aad0a0072012-11-01 18:15:58484 gfx::Rect clipRectForSubtree;
[email protected]94f206c12012-08-25 00:09:14485 bool subtreeShouldBeClipped = false;
486
487 float drawOpacity = layer->opacity();
488 bool drawOpacityIsAnimating = layer->opacityIsAnimating();
489 if (layer->parent() && layer->parent()->preserves3D()) {
490 drawOpacity *= layer->parent()->drawOpacity();
491 drawOpacityIsAnimating |= layer->parent()->drawOpacityIsAnimating();
492 }
493
[email protected]6a9cff92012-11-08 11:53:26494 bool animatingTransformToTarget = layer->transformIsAnimating();
495 bool animatingTransformToScreen = animatingTransformToTarget;
496 if (layer->parent()) {
497 animatingTransformToTarget |= layer->parent()->drawTransformIsAnimating();
498 animatingTransformToScreen |= layer->parent()->screenSpaceTransformIsAnimating();
499 }
500
[email protected]aad0a0072012-11-01 18:15:58501 gfx::Size bounds = layer->bounds();
502 gfx::PointF anchorPoint = layer->anchorPoint();
[email protected]c9c1ebe2012-11-05 20:46:13503 gfx::PointF position = layer->position() - layer->scrollDelta();
[email protected]94f206c12012-08-25 00:09:14504
[email protected]94f206c12012-08-25 00:09:14505 WebTransformationMatrix layerLocalTransform;
[email protected]518ee582012-10-24 18:29:44506 // LT = Tr[origin] * Tr[origin2anchor]
[email protected]94f206c12012-08-25 00:09:14507 layerLocalTransform.translate3d(position.x() + anchorPoint.x() * bounds.width(), position.y() + anchorPoint.y() * bounds.height(), layer->anchorPointZ());
[email protected]518ee582012-10-24 18:29:44508 // LT = Tr[origin] * Tr[origin2anchor] * M[layer]
[email protected]94f206c12012-08-25 00:09:14509 layerLocalTransform.multiply(layer->transform());
[email protected]518ee582012-10-24 18:29:44510 // LT = Tr[origin] * Tr[origin2anchor] * M[layer] * Tr[anchor2origin]
[email protected]b8d3c022012-10-08 17:38:04511 layerLocalTransform.translate3d(-anchorPoint.x() * bounds.width(), -anchorPoint.y() * bounds.height(), -layer->anchorPointZ());
[email protected]94f206c12012-08-25 00:09:14512
513 WebTransformationMatrix combinedTransform = parentMatrix;
514 combinedTransform.multiply(layerLocalTransform);
515
[email protected]518ee582012-10-24 18:29:44516 // The layer's contentsSize is determined from the combinedTransform, which then informs the
517 // layer's drawTransform.
[email protected]6a9cff92012-11-08 11:53:26518 updateLayerContentsScale(layer, combinedTransform, deviceScaleFactor, pageScaleFactor, animatingTransformToScreen);
[email protected]518ee582012-10-24 18:29:44519
520 // If there is a tranformation from the impl thread then it should be at the
521 // start of the combinedTransform, but we don't want it to affect the contentsScale.
522 combinedTransform = layer->implTransform() * combinedTransform;
523
[email protected]94f206c12012-08-25 00:09:14524 if (layer->fixedToContainerLayer()) {
525 // Special case: this layer is a composited fixed-position layer; we need to
526 // explicitly compensate for all ancestors' nonzero scrollDeltas to keep this layer
527 // fixed correctly.
528 combinedTransform = currentScrollCompensationMatrix * combinedTransform;
529 }
530
531 // The drawTransform that gets computed below is effectively the layer's drawTransform, unless
532 // the layer itself creates a renderSurface. In that case, the renderSurface re-parents the transforms.
533 WebTransformationMatrix drawTransform = combinedTransform;
[email protected]f89f5632012-11-14 23:34:45534 // M[draw] = M[parent] * LT * S[layer2content]
535 drawTransform.scaleNonUniform(1.0 / layer->contentsScaleX(), 1.0 / layer->contentsScaleY());
[email protected]94f206c12012-08-25 00:09:14536
537 // layerScreenSpaceTransform represents the transform between root layer's "screen space" and local content space.
538 WebTransformationMatrix layerScreenSpaceTransform = fullHierarchyMatrix;
539 if (!layer->preserves3D())
[email protected]96baf3e2012-10-22 23:09:55540 MathUtil::flattenTransformTo2d(layerScreenSpaceTransform);
[email protected]94f206c12012-08-25 00:09:14541 layerScreenSpaceTransform.multiply(drawTransform);
542 layer->setScreenSpaceTransform(layerScreenSpaceTransform);
543
[email protected]aad0a0072012-11-01 18:15:58544 gfx::RectF contentRect(gfx::PointF(), layer->contentBounds());
[email protected]94f206c12012-08-25 00:09:14545
[email protected]96baf3e2012-10-22 23:09:55546 // fullHierarchyMatrix is the matrix that transforms objects between screen space (except projection matrix) and the most recent RenderSurfaceImpl's space.
547 // nextHierarchyMatrix will only change if this layer uses a new RenderSurfaceImpl, otherwise remains the same.
[email protected]94f206c12012-08-25 00:09:14548 WebTransformationMatrix nextHierarchyMatrix = fullHierarchyMatrix;
549 WebTransformationMatrix sublayerMatrix;
550
[email protected]aad0a0072012-11-01 18:15:58551 gfx::Vector2dF renderSurfaceSublayerScale = MathUtil::computeTransform2dScaleComponents(combinedTransform);
[email protected]518ee582012-10-24 18:29:44552
[email protected]94f206c12012-08-25 00:09:14553 if (subtreeShouldRenderToSeparateSurface(layer, isScaleOrTranslation(combinedTransform))) {
554 // Check back-face visibility before continuing with this surface and its subtree
555 if (!layer->doubleSided() && transformToParentIsKnown(layer) && isSurfaceBackFaceVisible(layer, combinedTransform))
556 return;
557
558 if (!layer->renderSurface())
559 layer->createRenderSurface();
560
561 RenderSurfaceType* renderSurface = layer->renderSurface();
[email protected]7d929c02012-09-20 17:26:57562 renderSurface->clearLayerLists();
[email protected]94f206c12012-08-25 00:09:14563
[email protected]518ee582012-10-24 18:29:44564 // The owning layer's draw transform has a scale from content to layer space which we need to undo and
565 // replace with a scale from the surface's subtree into layer space.
[email protected]f89f5632012-11-14 23:34:45566 drawTransform.scaleNonUniform(layer->contentsScaleX(), layer->contentsScaleY());
[email protected]518ee582012-10-24 18:29:44567 drawTransform.scaleNonUniform(1 / renderSurfaceSublayerScale.x(), 1 / renderSurfaceSublayerScale.y());
[email protected]f3922f22012-10-12 09:20:38568 renderSurface->setDrawTransform(drawTransform);
[email protected]518ee582012-10-24 18:29:44569
570 // The origin of the new surface is the upper left corner of the layer.
[email protected]94f206c12012-08-25 00:09:14571 WebTransformationMatrix layerDrawTransform;
[email protected]518ee582012-10-24 18:29:44572 layerDrawTransform.scaleNonUniform(renderSurfaceSublayerScale.x(), renderSurfaceSublayerScale.y());
[email protected]f89f5632012-11-14 23:34:45573 layerDrawTransform.scaleNonUniform(1.0 / layer->contentsScaleX(), 1.0 / layer->contentsScaleY());
[email protected]94f206c12012-08-25 00:09:14574 layer->setDrawTransform(layerDrawTransform);
575
[email protected]518ee582012-10-24 18:29:44576 // Inside the surface's subtree, we scale everything to the owning layer's scale.
[email protected]94f206c12012-08-25 00:09:14577 // The sublayer matrix transforms centered layer rects into target
578 // surface content space.
579 sublayerMatrix.makeIdentity();
[email protected]518ee582012-10-24 18:29:44580 sublayerMatrix.scaleNonUniform(renderSurfaceSublayerScale.x(), renderSurfaceSublayerScale.y());
[email protected]94f206c12012-08-25 00:09:14581
582 // The opacity value is moved from the layer to its surface, so that the entire subtree properly inherits opacity.
583 renderSurface->setDrawOpacity(drawOpacity);
584 renderSurface->setDrawOpacityIsAnimating(drawOpacityIsAnimating);
585 layer->setDrawOpacity(1);
586 layer->setDrawOpacityIsAnimating(false);
587
588 renderSurface->setTargetSurfaceTransformsAreAnimating(animatingTransformToTarget);
589 renderSurface->setScreenSpaceTransformsAreAnimating(animatingTransformToScreen);
590 animatingTransformToTarget = false;
591 layer->setDrawTransformIsAnimating(animatingTransformToTarget);
592 layer->setScreenSpaceTransformIsAnimating(animatingTransformToScreen);
593
594 // Update the aggregate hierarchy matrix to include the transform of the
[email protected]96baf3e2012-10-22 23:09:55595 // newly created RenderSurfaceImpl.
[email protected]94f206c12012-08-25 00:09:14596 nextHierarchyMatrix.multiply(renderSurface->drawTransform());
597
598 // The new renderSurface here will correctly clip the entire subtree. So, we do
599 // not need to continue propagating the clipping state further down the tree. This
600 // way, we can avoid transforming clipRects from ancestor target surface space to
601 // current target surface space that could cause more w < 0 headaches.
602 subtreeShouldBeClipped = false;
603
[email protected]9bab0bb2012-10-08 19:11:58604 if (layer->maskLayer()) {
[email protected]94f206c12012-08-25 00:09:14605 layer->maskLayer()->setRenderTarget(layer);
[email protected]aad0a0072012-11-01 18:15:58606 layer->maskLayer()->setVisibleContentRect(gfx::Rect(gfx::Point(), layer->contentBounds()));
[email protected]9bab0bb2012-10-08 19:11:58607 }
[email protected]94f206c12012-08-25 00:09:14608
[email protected]9bab0bb2012-10-08 19:11:58609 if (layer->replicaLayer() && layer->replicaLayer()->maskLayer()) {
[email protected]94f206c12012-08-25 00:09:14610 layer->replicaLayer()->maskLayer()->setRenderTarget(layer);
[email protected]aad0a0072012-11-01 18:15:58611 layer->replicaLayer()->maskLayer()->setVisibleContentRect(gfx::Rect(gfx::Point(), layer->contentBounds()));
[email protected]9bab0bb2012-10-08 19:11:58612 }
[email protected]94f206c12012-08-25 00:09:14613
[email protected]4000abf2012-10-23 04:45:45614 // FIXME: make this smarter for the SkImageFilter case (check for
615 // pixel-moving filters)
616 if (layer->filters().hasFilterThatMovesPixels() || layer->filter())
[email protected]94f206c12012-08-25 00:09:14617 nearestAncestorThatMovesPixels = renderSurface;
618
[email protected]9bab0bb2012-10-08 19:11:58619 // The render surface clipRect is expressed in the space where this surface draws, i.e. the same space as clipRectFromAncestor.
620 if (ancestorClipsSubtree)
621 renderSurface->setClipRect(clipRectFromAncestor);
622 else
[email protected]aad0a0072012-11-01 18:15:58623 renderSurface->setClipRect(gfx::Rect());
[email protected]9bab0bb2012-10-08 19:11:58624
[email protected]94f206c12012-08-25 00:09:14625 renderSurface->setNearestAncestorThatMovesPixels(nearestAncestorThatMovesPixels);
626
[email protected]d58499a2012-10-09 22:27:47627 renderSurfaceLayerList.push_back(layer);
[email protected]94f206c12012-08-25 00:09:14628 } else {
[email protected]ecc12622012-10-30 20:45:42629 DCHECK(layer->parent());
630
[email protected]94f206c12012-08-25 00:09:14631 layer->setDrawTransform(drawTransform);
632 layer->setDrawTransformIsAnimating(animatingTransformToTarget);
633 layer->setScreenSpaceTransformIsAnimating(animatingTransformToScreen);
634 sublayerMatrix = combinedTransform;
635
636 layer->setDrawOpacity(drawOpacity);
637 layer->setDrawOpacityIsAnimating(drawOpacityIsAnimating);
638
[email protected]ecc12622012-10-30 20:45:42639 layer->clearRenderSurface();
[email protected]94f206c12012-08-25 00:09:14640
[email protected]ecc12622012-10-30 20:45:42641 // Layers without renderSurfaces directly inherit the ancestor's clip status.
642 subtreeShouldBeClipped = ancestorClipsSubtree;
643 if (ancestorClipsSubtree)
644 clipRectForSubtree = clipRectFromAncestor;
[email protected]94f206c12012-08-25 00:09:14645
[email protected]ecc12622012-10-30 20:45:42646 // Layers that are not their own renderTarget will render into the target of their nearest ancestor.
647 layer->setRenderTarget(layer->parent()->renderTarget());
[email protected]94f206c12012-08-25 00:09:14648 }
649
[email protected]aad0a0072012-11-01 18:15:58650 gfx::Rect rectInTargetSpace = ToEnclosingRect(MathUtil::mapClippedRect(layer->drawTransform(), contentRect));
[email protected]94f206c12012-08-25 00:09:14651
652 if (layerClipsSubtree(layer)) {
653 subtreeShouldBeClipped = true;
654 if (ancestorClipsSubtree && !layer->renderSurface()) {
655 clipRectForSubtree = clipRectFromAncestor;
[email protected]aad0a0072012-11-01 18:15:58656 clipRectForSubtree.Intersect(rectInTargetSpace);
[email protected]94f206c12012-08-25 00:09:14657 } else
658 clipRectForSubtree = rectInTargetSpace;
659 }
660
661 // Flatten to 2D if the layer doesn't preserve 3D.
662 if (!layer->preserves3D())
[email protected]96baf3e2012-10-22 23:09:55663 MathUtil::flattenTransformTo2d(sublayerMatrix);
[email protected]94f206c12012-08-25 00:09:14664
665 // Apply the sublayer transform at the center of the layer.
[email protected]b8d3c022012-10-08 17:38:04666 sublayerMatrix.translate(0.5 * bounds.width(), 0.5 * bounds.height());
[email protected]94f206c12012-08-25 00:09:14667 sublayerMatrix.multiply(layer->sublayerTransform());
[email protected]b8d3c022012-10-08 17:38:04668 sublayerMatrix.translate(-0.5 * bounds.width(), -0.5 * bounds.height());
[email protected]94f206c12012-08-25 00:09:14669
670 LayerList& descendants = (layer->renderSurface() ? layer->renderSurface()->layerList() : layerList);
671
672 // Any layers that are appended after this point are in the layer's subtree and should be included in the sorting process.
673 unsigned sortingStartIndex = descendants.size();
674
675 if (!layerShouldBeSkipped(layer))
[email protected]d58499a2012-10-09 22:27:47676 descendants.push_back(layer);
[email protected]94f206c12012-08-25 00:09:14677
678 WebTransformationMatrix nextScrollCompensationMatrix = computeScrollCompensationMatrixForChildren(layer, parentMatrix, currentScrollCompensationMatrix);;
679
[email protected]aad0a0072012-11-01 18:15:58680 gfx::Rect accumulatedDrawableContentRectOfChildren;
[email protected]94f206c12012-08-25 00:09:14681 for (size_t i = 0; i < layer->children().size(); ++i) {
[email protected]96baf3e2012-10-22 23:09:55682 LayerType* child = LayerTreeHostCommon::getChildAsRawPtr(layer->children(), i);
[email protected]aad0a0072012-11-01 18:15:58683 gfx::Rect drawableContentRectOfChildSubtree;
[email protected]ecc12622012-10-30 20:45:42684 calculateDrawTransformsInternal<LayerType, LayerList, RenderSurfaceType, LayerSorter>(child, sublayerMatrix, nextHierarchyMatrix, nextScrollCompensationMatrix,
[email protected]94f206c12012-08-25 00:09:14685 clipRectForSubtree, subtreeShouldBeClipped, nearestAncestorThatMovesPixels,
[email protected]518ee582012-10-24 18:29:44686 renderSurfaceLayerList, descendants, layerSorter, maxTextureSize, deviceScaleFactor, pageScaleFactor, drawableContentRectOfChildSubtree);
[email protected]aad0a0072012-11-01 18:15:58687 if (!drawableContentRectOfChildSubtree.IsEmpty()) {
688 accumulatedDrawableContentRectOfChildren.Union(drawableContentRectOfChildSubtree);
[email protected]94f206c12012-08-25 00:09:14689 if (child->renderSurface())
[email protected]d58499a2012-10-09 22:27:47690 descendants.push_back(child);
[email protected]94f206c12012-08-25 00:09:14691 }
692 }
693
694 // Compute the total drawableContentRect for this subtree (the rect is in targetSurface space)
[email protected]aad0a0072012-11-01 18:15:58695 gfx::Rect localDrawableContentRectOfSubtree = accumulatedDrawableContentRectOfChildren;
[email protected]94f206c12012-08-25 00:09:14696 if (layer->drawsContent())
[email protected]aad0a0072012-11-01 18:15:58697 localDrawableContentRectOfSubtree.Union(rectInTargetSpace);
[email protected]94f206c12012-08-25 00:09:14698 if (subtreeShouldBeClipped)
[email protected]aad0a0072012-11-01 18:15:58699 localDrawableContentRectOfSubtree.Intersect(clipRectForSubtree);
[email protected]94f206c12012-08-25 00:09:14700
701 // Compute the layer's drawable content rect (the rect is in targetSurface space)
[email protected]aad0a0072012-11-01 18:15:58702 gfx::Rect drawableContentRectOfLayer = rectInTargetSpace;
[email protected]94f206c12012-08-25 00:09:14703 if (subtreeShouldBeClipped)
[email protected]aad0a0072012-11-01 18:15:58704 drawableContentRectOfLayer.Intersect(clipRectForSubtree);
[email protected]94f206c12012-08-25 00:09:14705 layer->setDrawableContentRect(drawableContentRectOfLayer);
706
[email protected]9bab0bb2012-10-08 19:11:58707 // Compute the layer's visible content rect (the rect is in content space)
[email protected]aad0a0072012-11-01 18:15:58708 gfx::Rect visibleContentRectOfLayer = calculateVisibleContentRect(layer);
[email protected]9bab0bb2012-10-08 19:11:58709 layer->setVisibleContentRect(visibleContentRectOfLayer);
710
[email protected]94f206c12012-08-25 00:09:14711 // Compute the remaining properties for the render surface, if the layer has one.
[email protected]ecc12622012-10-30 20:45:42712 if (isRootLayer(layer)) {
713 // The root layer's surface's contentRect is always the entire viewport.
714 DCHECK(layer->renderSurface());
715 layer->renderSurface()->setContentRect(clipRectFromAncestor);
716 } else if (layer->renderSurface() && !isRootLayer(layer)) {
[email protected]94f206c12012-08-25 00:09:14717 RenderSurfaceType* renderSurface = layer->renderSurface();
[email protected]aad0a0072012-11-01 18:15:58718 gfx::Rect clippedContentRect = localDrawableContentRectOfSubtree;
[email protected]94f206c12012-08-25 00:09:14719
[email protected]94f206c12012-08-25 00:09:14720 // Don't clip if the layer is reflected as the reflection shouldn't be
721 // clipped. If the layer is animating, then the surface's transform to
722 // its target is not known on the main thread, and we should not use it
723 // to clip.
724 if (!layer->replicaLayer() && transformToParentIsKnown(layer)) {
725 // Note, it is correct to use ancestorClipsSubtree here, because we are looking at this layer's renderSurface, not the layer itself.
[email protected]aad0a0072012-11-01 18:15:58726 if (ancestorClipsSubtree && !clippedContentRect.IsEmpty()) {
727 gfx::Rect surfaceClipRect = LayerTreeHostCommon::calculateVisibleRect(renderSurface->clipRect(), clippedContentRect, renderSurface->drawTransform());
728 clippedContentRect.Intersect(surfaceClipRect);
[email protected]94f206c12012-08-25 00:09:14729 }
730 }
731
[email protected]96baf3e2012-10-22 23:09:55732 // The RenderSurfaceImpl backing texture cannot exceed the maximum supported
[email protected]94f206c12012-08-25 00:09:14733 // texture size.
[email protected]aad0a0072012-11-01 18:15:58734 clippedContentRect.set_width(std::min(clippedContentRect.width(), maxTextureSize));
735 clippedContentRect.set_height(std::min(clippedContentRect.height(), maxTextureSize));
[email protected]94f206c12012-08-25 00:09:14736
[email protected]aad0a0072012-11-01 18:15:58737 if (clippedContentRect.IsEmpty())
[email protected]7d929c02012-09-20 17:26:57738 renderSurface->clearLayerLists();
[email protected]94f206c12012-08-25 00:09:14739
740 renderSurface->setContentRect(clippedContentRect);
[email protected]518ee582012-10-24 18:29:44741
742 // The owning layer's screenSpaceTransform has a scale from content to layer space which we need to undo and
743 // replace with a scale from the surface's subtree into layer space.
744 WebTransformationMatrix screenSpaceTransform = layer->screenSpaceTransform();
[email protected]f89f5632012-11-14 23:34:45745 screenSpaceTransform.scaleNonUniform(layer->contentsScaleX(), layer->contentsScaleY());
[email protected]518ee582012-10-24 18:29:44746 screenSpaceTransform.scaleNonUniform(1 / renderSurfaceSublayerScale.x(), 1 / renderSurfaceSublayerScale.y());
747 renderSurface->setScreenSpaceTransform(screenSpaceTransform);
[email protected]94f206c12012-08-25 00:09:14748
749 if (layer->replicaLayer()) {
750 WebTransformationMatrix surfaceOriginToReplicaOriginTransform;
[email protected]518ee582012-10-24 18:29:44751 surfaceOriginToReplicaOriginTransform.scaleNonUniform(renderSurfaceSublayerScale.x(), renderSurfaceSublayerScale.y());
[email protected]94f206c12012-08-25 00:09:14752 surfaceOriginToReplicaOriginTransform.translate(layer->replicaLayer()->position().x() + layer->replicaLayer()->anchorPoint().x() * bounds.width(),
753 layer->replicaLayer()->position().y() + layer->replicaLayer()->anchorPoint().y() * bounds.height());
754 surfaceOriginToReplicaOriginTransform.multiply(layer->replicaLayer()->transform());
755 surfaceOriginToReplicaOriginTransform.translate(-layer->replicaLayer()->anchorPoint().x() * bounds.width(), -layer->replicaLayer()->anchorPoint().y() * bounds.height());
[email protected]518ee582012-10-24 18:29:44756 surfaceOriginToReplicaOriginTransform.scaleNonUniform(1 / renderSurfaceSublayerScale.x(), 1 / renderSurfaceSublayerScale.y());
[email protected]94f206c12012-08-25 00:09:14757
758 // Compute the replica's "originTransform" that maps from the replica's origin space to the target surface origin space.
759 WebTransformationMatrix replicaOriginTransform = layer->renderSurface()->drawTransform() * surfaceOriginToReplicaOriginTransform;
760 renderSurface->setReplicaDrawTransform(replicaOriginTransform);
761
762 // Compute the replica's "screenSpaceTransform" that maps from the replica's origin space to the screen's origin space.
763 WebTransformationMatrix replicaScreenSpaceTransform = layer->renderSurface()->screenSpaceTransform() * surfaceOriginToReplicaOriginTransform;
764 renderSurface->setReplicaScreenSpaceTransform(replicaScreenSpaceTransform);
765 }
766
767 // If a render surface has no layer list, then it and none of its children needed to get drawn.
768 if (!layer->renderSurface()->layerList().size()) {
769 // FIXME: Originally we asserted that this layer was already at the end of the
770 // list, and only needed to remove that layer. For now, we remove the
771 // entire subtree of surfaces to fix a crash bug. The root cause is
772 // https://bugs.webkit.org/show_bug.cgi?id=74147 and we should be able
773 // to put the original assert after fixing that.
[email protected]d58499a2012-10-09 22:27:47774 while (renderSurfaceLayerList.back() != layer) {
775 renderSurfaceLayerList.back()->clearRenderSurface();
776 renderSurfaceLayerList.pop_back();
[email protected]94f206c12012-08-25 00:09:14777 }
[email protected]1d993172012-10-18 18:15:04778 DCHECK(renderSurfaceLayerList.back() == layer);
[email protected]d58499a2012-10-09 22:27:47779 renderSurfaceLayerList.pop_back();
[email protected]94f206c12012-08-25 00:09:14780 layer->clearRenderSurface();
781 return;
782 }
783 }
784
785 // If neither this layer nor any of its children were added, early out.
786 if (sortingStartIndex == descendants.size())
787 return;
788
789 // If preserves-3d then sort all the descendants in 3D so that they can be
790 // drawn from back to front. If the preserves-3d property is also set on the parent then
791 // skip the sorting as the parent will sort all the descendants anyway.
792 if (descendants.size() && layer->preserves3D() && (!layer->parent() || !layer->parent()->preserves3D()))
[email protected]d58499a2012-10-09 22:27:47793 sortLayers(descendants.begin() + sortingStartIndex, descendants.end(), layerSorter);
[email protected]94f206c12012-08-25 00:09:14794
795 if (layer->renderSurface())
[email protected]aad0a0072012-11-01 18:15:58796 drawableContentRectOfSubtree = gfx::ToEnclosingRect(layer->renderSurface()->drawableContentRect());
[email protected]94f206c12012-08-25 00:09:14797 else
798 drawableContentRectOfSubtree = localDrawableContentRectOfSubtree;
799
[email protected]7d929c02012-09-20 17:26:57800 if (layer->hasContributingDelegatedRenderPasses())
801 layer->renderTarget()->renderSurface()->addContributingDelegatedRenderPassLayer(layer);
[email protected]94f206c12012-08-25 00:09:14802}
803
[email protected]aad0a0072012-11-01 18:15:58804void LayerTreeHostCommon::calculateDrawTransforms(Layer* rootLayer, const gfx::Size& deviceViewportSize, float deviceScaleFactor, float pageScaleFactor, int maxTextureSize, std::vector<scoped_refptr<Layer> >& renderSurfaceLayerList)
[email protected]94f206c12012-08-25 00:09:14805{
[email protected]aad0a0072012-11-01 18:15:58806 gfx::Rect totalDrawableContentRect;
[email protected]94f206c12012-08-25 00:09:14807 WebTransformationMatrix identityMatrix;
808 WebTransformationMatrix deviceScaleTransform;
809 deviceScaleTransform.scale(deviceScaleFactor);
[email protected]ecc12622012-10-30 20:45:42810 std::vector<scoped_refptr<Layer> > dummyLayerList;
[email protected]94f206c12012-08-25 00:09:14811
[email protected]ecc12622012-10-30 20:45:42812 // The root layer's renderSurface should receive the deviceViewport as the initial clipRect.
813 bool subtreeShouldBeClipped = true;
[email protected]aad0a0072012-11-01 18:15:58814 gfx::Rect deviceViewportRect(gfx::Point(), deviceViewportSize);
[email protected]ecc12622012-10-30 20:45:42815
816 // This function should have received a root layer.
817 DCHECK(isRootLayer(rootLayer));
[email protected]94f206c12012-08-25 00:09:14818
[email protected]518ee582012-10-24 18:29:44819 cc::calculateDrawTransformsInternal<Layer, std::vector<scoped_refptr<Layer> >, RenderSurface, void>(
[email protected]ecc12622012-10-30 20:45:42820 rootLayer, deviceScaleTransform, identityMatrix, identityMatrix,
821 deviceViewportRect, subtreeShouldBeClipped, 0, renderSurfaceLayerList,
822 dummyLayerList, 0, maxTextureSize,
[email protected]518ee582012-10-24 18:29:44823 deviceScaleFactor, pageScaleFactor, totalDrawableContentRect);
[email protected]ecc12622012-10-30 20:45:42824
825 // The dummy layer list should not have been used.
826 DCHECK(dummyLayerList.size() == 0);
827 // A root layer renderSurface should always exist after calculateDrawTransforms.
828 DCHECK(rootLayer->renderSurface());
[email protected]94f206c12012-08-25 00:09:14829}
830
[email protected]aad0a0072012-11-01 18:15:58831void LayerTreeHostCommon::calculateDrawTransforms(LayerImpl* rootLayer, const gfx::Size& deviceViewportSize, float deviceScaleFactor, float pageScaleFactor, LayerSorter* layerSorter, int maxTextureSize, std::vector<LayerImpl*>& renderSurfaceLayerList)
[email protected]94f206c12012-08-25 00:09:14832{
[email protected]aad0a0072012-11-01 18:15:58833 gfx::Rect totalDrawableContentRect;
[email protected]94f206c12012-08-25 00:09:14834 WebTransformationMatrix identityMatrix;
835 WebTransformationMatrix deviceScaleTransform;
836 deviceScaleTransform.scale(deviceScaleFactor);
[email protected]ecc12622012-10-30 20:45:42837 std::vector<LayerImpl*> dummyLayerList;
[email protected]94f206c12012-08-25 00:09:14838
[email protected]ecc12622012-10-30 20:45:42839 // The root layer's renderSurface should receive the deviceViewport as the initial clipRect.
840 bool subtreeShouldBeClipped = true;
[email protected]aad0a0072012-11-01 18:15:58841 gfx::Rect deviceViewportRect(gfx::Point(), deviceViewportSize);
[email protected]ecc12622012-10-30 20:45:42842
843 // This function should have received a root layer.
844 DCHECK(isRootLayer(rootLayer));
[email protected]94f206c12012-08-25 00:09:14845
[email protected]518ee582012-10-24 18:29:44846 cc::calculateDrawTransformsInternal<LayerImpl, std::vector<LayerImpl*>, RenderSurfaceImpl, LayerSorter>(
[email protected]ecc12622012-10-30 20:45:42847 rootLayer, deviceScaleTransform, identityMatrix, identityMatrix,
848 deviceViewportRect, subtreeShouldBeClipped, 0, renderSurfaceLayerList,
849 dummyLayerList, layerSorter, maxTextureSize,
[email protected]518ee582012-10-24 18:29:44850 deviceScaleFactor, pageScaleFactor, totalDrawableContentRect);
[email protected]ecc12622012-10-30 20:45:42851
852 // The dummy layer list should not have been used.
853 DCHECK(dummyLayerList.size() == 0);
854 // A root layer renderSurface should always exist after calculateDrawTransforms.
855 DCHECK(rootLayer->renderSurface());
[email protected]94f206c12012-08-25 00:09:14856}
857
[email protected]d455d552012-11-02 00:19:06858static bool pointHitsRect(const gfx::PointF& screenSpacePoint, const WebTransformationMatrix& localSpaceToScreenSpaceTransform, gfx::RectF localSpaceRect)
[email protected]94f206c12012-08-25 00:09:14859{
860 // If the transform is not invertible, then assume that this point doesn't hit this rect.
861 if (!localSpaceToScreenSpaceTransform.isInvertible())
862 return false;
863
864 // Transform the hit test point from screen space to the local space of the given rect.
865 bool clipped = false;
[email protected]d455d552012-11-02 00:19:06866 gfx::PointF hitTestPointInLocalSpace = MathUtil::projectPoint(localSpaceToScreenSpaceTransform.inverse(), screenSpacePoint, clipped);
[email protected]94f206c12012-08-25 00:09:14867
868 // If projectPoint could not project to a valid value, then we assume that this point doesn't hit this rect.
869 if (clipped)
870 return false;
871
[email protected]aad0a0072012-11-01 18:15:58872 return localSpaceRect.Contains(hitTestPointInLocalSpace);
[email protected]94f206c12012-08-25 00:09:14873}
874
[email protected]d455d552012-11-02 00:19:06875static bool pointIsClippedBySurfaceOrClipRect(const gfx::PointF& screenSpacePoint, LayerImpl* layer)
[email protected]94f206c12012-08-25 00:09:14876{
[email protected]96baf3e2012-10-22 23:09:55877 LayerImpl* currentLayer = layer;
[email protected]94f206c12012-08-25 00:09:14878
879 // Walk up the layer tree and hit-test any renderSurfaces and any layer clipRects that are active.
880 while (currentLayer) {
[email protected]01527732012-10-19 18:16:19881 if (currentLayer->renderSurface() && !pointHitsRect(screenSpacePoint, currentLayer->renderSurface()->screenSpaceTransform(), currentLayer->renderSurface()->contentRect()))
[email protected]94f206c12012-08-25 00:09:14882 return true;
883
884 // Note that drawableContentRects are actually in targetSurface space, so the transform we
885 // have to provide is the target surface's screenSpaceTransform.
[email protected]96baf3e2012-10-22 23:09:55886 LayerImpl* renderTarget = currentLayer->renderTarget();
[email protected]01527732012-10-19 18:16:19887 if (layerClipsSubtree(currentLayer) && !pointHitsRect(screenSpacePoint, renderTarget->renderSurface()->screenSpaceTransform(), currentLayer->drawableContentRect()))
[email protected]94f206c12012-08-25 00:09:14888 return true;
889
890 currentLayer = currentLayer->parent();
891 }
892
893 // If we have finished walking all ancestors without having already exited, then the point is not clipped by any ancestors.
894 return false;
895}
896
[email protected]d455d552012-11-02 00:19:06897LayerImpl* LayerTreeHostCommon::findLayerThatIsHitByPoint(const gfx::PointF& screenSpacePoint, std::vector<LayerImpl*>& renderSurfaceLayerList)
[email protected]94f206c12012-08-25 00:09:14898{
[email protected]96baf3e2012-10-22 23:09:55899 LayerImpl* foundLayer = 0;
[email protected]94f206c12012-08-25 00:09:14900
[email protected]96baf3e2012-10-22 23:09:55901 typedef LayerIterator<LayerImpl, std::vector<LayerImpl*>, RenderSurfaceImpl, LayerIteratorActions::FrontToBack> LayerIteratorType;
902 LayerIteratorType end = LayerIteratorType::end(&renderSurfaceLayerList);
[email protected]94f206c12012-08-25 00:09:14903
[email protected]96baf3e2012-10-22 23:09:55904 for (LayerIteratorType it = LayerIteratorType::begin(&renderSurfaceLayerList); it != end; ++it) {
[email protected]94f206c12012-08-25 00:09:14905 // We don't want to consider renderSurfaces for hit testing.
906 if (!it.representsItself())
907 continue;
908
[email protected]96baf3e2012-10-22 23:09:55909 LayerImpl* currentLayer = (*it);
[email protected]94f206c12012-08-25 00:09:14910
[email protected]aad0a0072012-11-01 18:15:58911 gfx::RectF contentRect(gfx::PointF(), currentLayer->contentBounds());
[email protected]01527732012-10-19 18:16:19912 if (!pointHitsRect(screenSpacePoint, currentLayer->screenSpaceTransform(), contentRect))
[email protected]94f206c12012-08-25 00:09:14913 continue;
914
915 // At this point, we think the point does hit the layer, but we need to walk up
916 // the parents to ensure that the layer was not clipped in such a way that the
917 // hit point actually should not hit the layer.
[email protected]01527732012-10-19 18:16:19918 if (pointIsClippedBySurfaceOrClipRect(screenSpacePoint, currentLayer))
[email protected]94f206c12012-08-25 00:09:14919 continue;
920
921 foundLayer = currentLayer;
922 break;
923 }
924
[email protected]01527732012-10-19 18:16:19925 // This can potentially return 0, which means the screenSpacePoint did not successfully hit test any layers, not even the root layer.
[email protected]94f206c12012-08-25 00:09:14926 return foundLayer;
927}
928
[email protected]bc5e77c2012-11-05 20:00:49929} // namespace cc