blob: d3bee7262d42fd3cf47c89612152fc876d73996c [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]c8686a02012-11-27 08:29:007#include <algorithm>
8
[email protected]a8461d82012-10-16 21:11:149#include "cc/layer.h"
[email protected]d50c6862012-10-23 02:08:3110#include "cc/layer_impl.h"
11#include "cc/layer_iterator.h"
12#include "cc/layer_sorter.h"
[email protected]55a124d02012-10-22 03:07:1313#include "cc/math_util.h"
[email protected]a8461d82012-10-16 21:11:1414#include "cc/render_surface.h"
[email protected]d50c6862012-10-23 02:08:3115#include "cc/render_surface_impl.h"
[email protected]2f1acc262012-11-16 21:42:2216#include "ui/gfx/point_conversions.h"
[email protected]aad0a0072012-11-01 18:15:5817#include "ui/gfx/rect_conversions.h"
[email protected]c8686a02012-11-27 08:29:0018#include "ui/gfx/transform.h"
[email protected]94f206c12012-08-25 00:09:1419
[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]c8686a02012-11-27 08:29:0030gfx::Rect LayerTreeHostCommon::calculateVisibleRect(const gfx::Rect& targetSurfaceRect, const gfx::Rect& layerBoundRect, const gfx::Transform& 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.
[email protected]c8686a02012-11-27 08:29:0047 const gfx::Transform surfaceToLayer = MathUtil::inverse(transform);
[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))
[email protected]2c7cd6d2012-11-28 23:49:2688 return layer->drawTransform().IsBackFaceVisible();
[email protected]94f206c12012-08-25 00:09:1489
90 // In this case, either the layer establishes a new 3d rendering context, or is not in
91 // a 3d rendering context at all.
[email protected]2c7cd6d2012-11-28 23:49:2692 return layer->transform().IsBackFaceVisible();
[email protected]94f206c12012-08-25 00:09:1493}
94
95template<typename LayerType>
[email protected]c8686a02012-11-27 08:29:0096static bool isSurfaceBackFaceVisible(LayerType* layer, const gfx::Transform& drawTransform)
[email protected]94f206c12012-08-25 00:09:1497{
98 if (layerIsInExisting3DRenderingContext(layer))
[email protected]2c7cd6d2012-11-28 23:49:2699 return drawTransform.IsBackFaceVisible();
[email protected]94f206c12012-08-25 00:09:14100
[email protected]ecc12622012-10-30 20:45:42101 if (isRootLayerOfNewRenderingContext(layer))
[email protected]2c7cd6d2012-11-28 23:49:26102 return layer->transform().IsBackFaceVisible();
[email protected]94f206c12012-08-25 00:09:14103
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]c8686a02012-11-27 08:29:00133 targetSurfaceClipRect = gfx::ToEnclosingRect(MathUtil::projectClippedRect(MathUtil::inverse(layer->renderTarget()->renderSurface()->drawTransform()), layer->renderTarget()->renderSurface()->clipRect()));
[email protected]aad0a0072012-11-01 18:15:58134 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
[email protected]96baf3e2012-10-22 23:09:55143static inline bool transformToParentIsKnown(LayerImpl*)
[email protected]94f206c12012-08-25 00:09:14144{
145 return true;
146}
147
[email protected]96baf3e2012-10-22 23:09:55148static inline bool transformToParentIsKnown(Layer* layer)
[email protected]94f206c12012-08-25 00:09:14149{
150 return !layer->transformIsAnimating();
151}
152
[email protected]96baf3e2012-10-22 23:09:55153static inline bool transformToScreenIsKnown(LayerImpl*)
[email protected]94f206c12012-08-25 00:09:14154{
155 return true;
156}
157
[email protected]96baf3e2012-10-22 23:09:55158static inline bool transformToScreenIsKnown(Layer* layer)
[email protected]94f206c12012-08-25 00:09:14159{
160 return !layer->screenSpaceTransformIsAnimating();
161}
162
163template<typename LayerType>
164static bool layerShouldBeSkipped(LayerType* layer)
165{
166 // Layers can be skipped if any of these conditions are met.
167 // - does not draw content.
168 // - is transparent
169 // - has empty bounds
170 // - the layer is not double-sided, but its back face is visible.
171 //
172 // Some additional conditions need to be computed at a later point after the recursion is finished.
173 // - the intersection of render surface content and layer clipRect is empty
174 // - the visibleContentRect is empty
175 //
176 // Note, if the layer should not have been drawn due to being fully transparent,
177 // we would have skipped the entire subtree and never made it into this function,
178 // so it is safe to omit this check here.
179
[email protected]aad0a0072012-11-01 18:15:58180 if (!layer->drawsContent() || layer->bounds().IsEmpty())
[email protected]94f206c12012-08-25 00:09:14181 return true;
182
183 LayerType* backfaceTestLayer = layer;
184 if (layer->useParentBackfaceVisibility()) {
[email protected]1d993172012-10-18 18:15:04185 DCHECK(layer->parent());
186 DCHECK(!layer->parent()->useParentBackfaceVisibility());
[email protected]94f206c12012-08-25 00:09:14187 backfaceTestLayer = layer->parent();
188 }
189
190 // 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.
191 if (!backfaceTestLayer->doubleSided() && transformToScreenIsKnown(backfaceTestLayer) && isLayerBackFaceVisible(backfaceTestLayer))
192 return true;
193
194 return false;
195}
196
[email protected]96baf3e2012-10-22 23:09:55197static inline bool subtreeShouldBeSkipped(LayerImpl* layer)
[email protected]94f206c12012-08-25 00:09:14198{
199 // The opacity of a layer always applies to its children (either implicitly
200 // via a render surface or explicitly if the parent preserves 3D), so the
201 // entire subtree can be skipped if this layer is fully transparent.
202 return !layer->opacity();
203}
204
[email protected]96baf3e2012-10-22 23:09:55205static inline bool subtreeShouldBeSkipped(Layer* layer)
[email protected]94f206c12012-08-25 00:09:14206{
207 // If the opacity is being animated then the opacity on the main thread is unreliable
208 // (since the impl thread may be using a different opacity), so it should not be trusted.
209 // In particular, it should not cause the subtree to be skipped.
210 return !layer->opacity() && !layer->opacityIsAnimating();
211}
212
[email protected]6ef21ffc2012-11-28 05:58:54213// Called on each layer that could be drawn after all information from
[email protected]d76806f82012-12-05 21:41:50214// calcDrawProperties has been updated on that layer. May have some false
[email protected]6ef21ffc2012-11-28 05:58:54215// positives (e.g. layers get this called on them but don't actually get drawn).
216static inline void markLayerAsUpdated(LayerImpl* layer)
217{
218 layer->didUpdateTransforms();
219}
220
221static inline void markLayerAsUpdated(Layer* layer)
222{
223}
224
[email protected]94f206c12012-08-25 00:09:14225template<typename LayerType>
226static bool subtreeShouldRenderToSeparateSurface(LayerType* layer, bool axisAlignedWithRespectToParent)
227{
[email protected]94f206c12012-08-25 00:09:14228 //
[email protected]96baf3e2012-10-22 23:09:55229 // A layer and its descendants should render onto a new RenderSurfaceImpl if any of these rules hold:
[email protected]94f206c12012-08-25 00:09:14230 //
231
[email protected]ecc12622012-10-30 20:45:42232 // The root layer should always have a renderSurface.
233 if (isRootLayer(layer))
234 return true;
235
[email protected]94f206c12012-08-25 00:09:14236 // If we force it.
237 if (layer->forceRenderSurface())
238 return true;
239
240 // If the layer uses a mask.
241 if (layer->maskLayer())
242 return true;
243
244 // If the layer has a reflection.
245 if (layer->replicaLayer())
246 return true;
247
248 // If the layer uses a CSS filter.
[email protected]4000abf2012-10-23 04:45:45249 if (!layer->filters().isEmpty() || !layer->backgroundFilters().isEmpty() || layer->filter())
[email protected]94f206c12012-08-25 00:09:14250 return true;
251
[email protected]ecc12622012-10-30 20:45:42252 // Cache this value, because otherwise it walks the entire subtree several times.
[email protected]498ec6e0e2012-11-30 18:24:57253 int descendantsDrawContent = layer->descendantsDrawContent();
[email protected]ecc12622012-10-30 20:45:42254
[email protected]94f206c12012-08-25 00:09:14255 // If the layer flattens its subtree (i.e. the layer doesn't preserve-3d), but it is
256 // treated as a 3D object by its parent (i.e. parent does preserve-3d).
[email protected]498ec6e0e2012-11-30 18:24:57257 if (layerIsInExisting3DRenderingContext(layer) && !layer->preserves3D() && descendantsDrawContent > 0)
[email protected]94f206c12012-08-25 00:09:14258 return true;
259
260 // If the layer clips its descendants but it is not axis-aligned with respect to its parent.
[email protected]498ec6e0e2012-11-30 18:24:57261 if (layerClipsSubtree(layer) && !axisAlignedWithRespectToParent && descendantsDrawContent > 0)
[email protected]94f206c12012-08-25 00:09:14262 return true;
263
264 // If the layer has opacity != 1 and does not have a preserves-3d transform style.
[email protected]498ec6e0e2012-11-30 18:24:57265 if (layer->opacity() != 1 && !layer->preserves3D() && descendantsDrawContent > 0
266 && (layer->drawsContent() || descendantsDrawContent > 1))
[email protected]94f206c12012-08-25 00:09:14267 return true;
268
269 return false;
270}
271
[email protected]c8686a02012-11-27 08:29:00272gfx::Transform computeScrollCompensationForThisLayer(LayerImpl* scrollingLayer, const gfx::Transform& parentMatrix)
[email protected]94f206c12012-08-25 00:09:14273{
274 // For every layer that has non-zero scrollDelta, we have to compute a transform that can undo the
275 // scrollDelta translation. In particular, we want this matrix to premultiply a fixed-position layer's
276 // parentMatrix, so we design this transform in three steps as follows. The steps described here apply
277 // from right-to-left, so Step 1 would be the right-most matrix:
278 //
279 // Step 1. transform from target surface space to the exact space where scrollDelta is actually applied.
280 // -- this is inverse of the matrix in step 3
281 // Step 2. undo the scrollDelta
282 // -- this is just a translation by scrollDelta.
283 // Step 3. transform back to target surface space.
284 // -- this transform is the "partialLayerOriginTransform" = (parentMatrix * scale(layer->pageScaleDelta()));
285 //
286 // These steps create a matrix that both start and end in targetSurfaceSpace. So this matrix can
287 // pre-multiply any fixed-position layer's drawTransform to undo the scrollDeltas -- as long as
288 // that fixed position layer is fixed onto the same renderTarget as this scrollingLayer.
289 //
290
[email protected]c8686a02012-11-27 08:29:00291 gfx::Transform partialLayerOriginTransform = parentMatrix;
292 partialLayerOriginTransform.PreconcatTransform(scrollingLayer->implTransform());
[email protected]94f206c12012-08-25 00:09:14293
[email protected]c8686a02012-11-27 08:29:00294 gfx::Transform scrollCompensationForThisLayer = partialLayerOriginTransform; // Step 3
295 scrollCompensationForThisLayer.Translate(scrollingLayer->scrollDelta().x(), scrollingLayer->scrollDelta().y()); // Step 2
296 scrollCompensationForThisLayer.PreconcatTransform(MathUtil::inverse(partialLayerOriginTransform)); // Step 1
[email protected]94f206c12012-08-25 00:09:14297 return scrollCompensationForThisLayer;
298}
299
[email protected]c8686a02012-11-27 08:29:00300gfx::Transform computeScrollCompensationMatrixForChildren(Layer* currentLayer, const gfx::Transform& currentParentMatrix, const gfx::Transform& currentScrollCompensation)
[email protected]94f206c12012-08-25 00:09:14301{
[email protected]96baf3e2012-10-22 23:09:55302 // The main thread (i.e. Layer) does not need to worry about scroll compensation.
[email protected]94f206c12012-08-25 00:09:14303 // So we can just return an identity matrix here.
[email protected]c8686a02012-11-27 08:29:00304 return gfx::Transform();
[email protected]94f206c12012-08-25 00:09:14305}
306
[email protected]c8686a02012-11-27 08:29:00307gfx::Transform computeScrollCompensationMatrixForChildren(LayerImpl* layer, const gfx::Transform& parentMatrix, const gfx::Transform& currentScrollCompensationMatrix)
[email protected]94f206c12012-08-25 00:09:14308{
309 // "Total scroll compensation" is the transform needed to cancel out all scrollDelta translations that
310 // occurred since the nearest container layer, even if there are renderSurfaces in-between.
311 //
312 // There are some edge cases to be aware of, that are not explicit in the code:
313 // - A layer that is both a fixed-position and container should not be its own container, instead, that means
314 // it is fixed to an ancestor, and is a container for any fixed-position descendants.
315 // - A layer that is a fixed-position container and has a renderSurface should behave the same as a container
316 // without a renderSurface, the renderSurface is irrelevant in that case.
[email protected]ecc12622012-10-30 20:45:42317 // - A layer that does not have an explicit container is simply fixed to the viewport.
318 // (i.e. the root renderSurface.)
[email protected]94f206c12012-08-25 00:09:14319 // - If the fixed-position layer has its own renderSurface, then the renderSurface is
320 // the one who gets fixed.
321 //
322 // This function needs to be called AFTER layers create their own renderSurfaces.
323 //
324
325 // 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:13326 if (!layer->isContainerForFixedPositionLayers() && layer->scrollDelta().IsZero() && !layer->renderSurface())
[email protected]94f206c12012-08-25 00:09:14327 return currentScrollCompensationMatrix;
328
329 // Start as identity matrix.
[email protected]c8686a02012-11-27 08:29:00330 gfx::Transform nextScrollCompensationMatrix;
[email protected]94f206c12012-08-25 00:09:14331
332 // If this layer is not a container, then it inherits the existing scroll compensations.
333 if (!layer->isContainerForFixedPositionLayers())
334 nextScrollCompensationMatrix = currentScrollCompensationMatrix;
335
336 // If the current layer has a non-zero scrollDelta, then we should compute its local scrollCompensation
337 // and accumulate it to the nextScrollCompensationMatrix.
[email protected]c9c1ebe2012-11-05 20:46:13338 if (!layer->scrollDelta().IsZero()) {
[email protected]c8686a02012-11-27 08:29:00339 gfx::Transform scrollCompensationForThisLayer = computeScrollCompensationForThisLayer(layer, parentMatrix);
340 nextScrollCompensationMatrix.PreconcatTransform(scrollCompensationForThisLayer);
[email protected]94f206c12012-08-25 00:09:14341 }
342
343 // If the layer created its own renderSurface, we have to adjust nextScrollCompensationMatrix.
344 // The adjustment allows us to continue using the scrollCompensation on the next surface.
345 // Step 1 (right-most in the math): transform from the new surface to the original ancestor surface
346 // Step 2: apply the scroll compensation
347 // Step 3: transform back to the new surface.
[email protected]c8686a02012-11-27 08:29:00348 if (layer->renderSurface() && !nextScrollCompensationMatrix.IsIdentity())
349 nextScrollCompensationMatrix = MathUtil::inverse(layer->renderSurface()->drawTransform()) * nextScrollCompensationMatrix * layer->renderSurface()->drawTransform();
[email protected]94f206c12012-08-25 00:09:14350
351 return nextScrollCompensationMatrix;
352}
353
[email protected]518ee582012-10-24 18:29:44354// There is no contentsScale on impl thread.
[email protected]c8686a02012-11-27 08:29:00355static inline void updateLayerContentsScale(LayerImpl*, const gfx::Transform&, float, float, bool) { }
[email protected]518ee582012-10-24 18:29:44356
[email protected]c8686a02012-11-27 08:29:00357static inline void updateLayerContentsScale(Layer* layer, const gfx::Transform& combinedTransform, float deviceScaleFactor, float pageScaleFactor, bool animatingTransformToScreen)
[email protected]518ee582012-10-24 18:29:44358{
359 float rasterScale = layer->rasterScale();
360 if (!rasterScale) {
361 rasterScale = 1;
362
[email protected]6a9cff92012-11-08 11:53:26363 if (!animatingTransformToScreen && layer->automaticallyComputeRasterScale()) {
[email protected]aad0a0072012-11-01 18:15:58364 gfx::Vector2dF transformScale = MathUtil::computeTransform2dScaleComponents(combinedTransform);
[email protected]518ee582012-10-24 18:29:44365 float combinedScale = std::max(transformScale.x(), transformScale.y());
366 rasterScale = combinedScale / deviceScaleFactor;
367 if (!layer->boundsContainPageScale())
368 rasterScale /= pageScaleFactor;
[email protected]11ec92972012-11-10 03:06:21369 // Prevent scale factors below 1 from being used or saved.
370 if (rasterScale < 1)
371 rasterScale = 1;
372 else
373 layer->setRasterScale(rasterScale);
[email protected]518ee582012-10-24 18:29:44374 }
375 }
376
377 float contentsScale = rasterScale * deviceScaleFactor;
378 if (!layer->boundsContainPageScale())
379 contentsScale *= pageScaleFactor;
380 layer->setContentsScale(contentsScale);
381
382 Layer* maskLayer = layer->maskLayer();
383 if (maskLayer)
384 maskLayer->setContentsScale(contentsScale);
385
386 Layer* replicaMaskLayer = layer->replicaLayer() ? layer->replicaLayer()->maskLayer() : 0;
387 if (replicaMaskLayer)
388 replicaMaskLayer->setContentsScale(contentsScale);
389}
390
[email protected]94f206c12012-08-25 00:09:14391// Recursively walks the layer tree starting at the given node and computes all the
392// necessary transformations, clipRects, render surfaces, etc.
393template<typename LayerType, typename LayerList, typename RenderSurfaceType, typename LayerSorter>
[email protected]d76806f82012-12-05 21:41:50394static void calculateDrawPropertiesInternal(LayerType* layer, const gfx::Transform& parentMatrix,
[email protected]c8686a02012-11-27 08:29:00395 const gfx::Transform& fullHierarchyMatrix, const gfx::Transform& currentScrollCompensationMatrix,
[email protected]aad0a0072012-11-01 18:15:58396 const gfx::Rect& clipRectFromAncestor, bool ancestorClipsSubtree,
[email protected]94f206c12012-08-25 00:09:14397 RenderSurfaceType* nearestAncestorThatMovesPixels, LayerList& renderSurfaceLayerList, LayerList& layerList,
[email protected]aad0a0072012-11-01 18:15:58398 LayerSorter* layerSorter, int maxTextureSize, float deviceScaleFactor, float pageScaleFactor, gfx::Rect& drawableContentRectOfSubtree)
[email protected]94f206c12012-08-25 00:09:14399{
400 // This function computes the new matrix transformations recursively for this
401 // layer and all its descendants. It also computes the appropriate render surfaces.
402 // Some important points to remember:
403 //
404 // 0. Here, transforms are notated in Matrix x Vector order, and in words we describe what
405 // the transform does from left to right.
406 //
407 // 1. In our terminology, the "layer origin" refers to the top-left corner of a layer, and the
408 // positive Y-axis points downwards. This interpretation is valid because the orthographic
409 // projection applied at draw time flips the Y axis appropriately.
410 //
[email protected]aad0a0072012-11-01 18:15:58411 // 2. The anchor point, when given as a PointF object, is specified in "unit layer space",
[email protected]c8686a02012-11-27 08:29:00412 // where the bounds of the layer map to [0, 1]. However, as a Transform object,
[email protected]b8d3c022012-10-08 17:38:04413 // the transform to the anchor point is specified in "layer space", where the bounds
[email protected]94f206c12012-08-25 00:09:14414 // of the layer map to [bounds.width(), bounds.height()].
415 //
416 // 3. Definition of various transforms used:
417 // M[parent] is the parent matrix, with respect to the nearest render surface, passed down recursively.
418 // M[root] is the full hierarchy, with respect to the root, passed down recursively.
419 // Tr[origin] is the translation matrix from the parent's origin to this layer's origin.
420 // Tr[origin2anchor] is the translation from the layer's origin to its anchor point
421 // Tr[origin2center] is the translation from the layer's origin to its center
422 // M[layer] is the layer's matrix (applied at the anchor point)
423 // M[sublayer] is the layer's sublayer transform (applied at the layer's center)
[email protected]b8d3c022012-10-08 17:38:04424 // S[layer2content] is the ratio of a layer's contentBounds() to its bounds().
[email protected]94f206c12012-08-25 00:09:14425 //
426 // Some composite transforms can help in understanding the sequence of transforms:
427 // compositeLayerTransform = Tr[origin2anchor] * M[layer] * Tr[origin2anchor].inverse()
428 // compositeSublayerTransform = Tr[origin2center] * M[sublayer] * Tr[origin2center].inverse()
429 //
430 // In words, the layer transform is applied about the anchor point, and the sublayer transform is
431 // applied about the center of the layer.
432 //
433 // 4. When a layer (or render surface) is drawn, it is drawn into a "target render surface". Therefore the draw
434 // transform does not necessarily transform from screen space to local layer space. Instead, the draw transform
435 // is the transform between the "target render surface space" and local layer space. Note that render surfaces,
436 // except for the root, also draw themselves into a different target render surface, and so their draw
437 // transform and origin transforms are also described with respect to the target.
438 //
439 // Using these definitions, then:
440 //
441 // The draw transform for the layer is:
[email protected]b8d3c022012-10-08 17:38:04442 // M[draw] = M[parent] * Tr[origin] * compositeLayerTransform * S[layer2content]
443 // = M[parent] * Tr[layer->position() + anchor] * M[layer] * Tr[anchor2origin] * S[layer2content]
[email protected]94f206c12012-08-25 00:09:14444 //
445 // Interpreting the math left-to-right, this transforms from the layer's render surface to the origin of the layer in content space.
446 //
447 // The screen space transform is:
[email protected]b8d3c022012-10-08 17:38:04448 // M[screenspace] = M[root] * Tr[origin] * compositeLayerTransform * S[layer2content]
449 // = M[root] * Tr[layer->position() + anchor] * M[layer] * Tr[anchor2origin] * S[layer2content]
[email protected]94f206c12012-08-25 00:09:14450 //
[email protected]655fa7f92012-11-15 00:14:33451 // Interpreting the math left-to-right, this transforms from the root render surface's content space to the origin of the layer in content space.
[email protected]94f206c12012-08-25 00:09:14452 //
453 // The transform hierarchy that is passed on to children (i.e. the child's parentMatrix) is:
454 // M[parent]_for_child = M[parent] * Tr[origin] * compositeLayerTransform * compositeSublayerTransform
[email protected]b8d3c022012-10-08 17:38:04455 // = M[parent] * Tr[layer->position() + anchor] * M[layer] * Tr[anchor2origin] * compositeSublayerTransform
[email protected]94f206c12012-08-25 00:09:14456 //
457 // and a similar matrix for the full hierarchy with respect to the root.
458 //
459 // Finally, note that the final matrix used by the shader for the layer is P * M[draw] * S . This final product
460 // is computed in drawTexturedQuad(), where:
461 // P is the projection matrix
[email protected]b8d3c022012-10-08 17:38:04462 // S is the scale adjustment (to scale up a canonical quad to the layer's size)
[email protected]94f206c12012-08-25 00:09:14463 //
464 // When a render surface has a replica layer, that layer's transform is used to draw a second copy of the surface.
[email protected]c8686a02012-11-27 08:29:00465 // gfx::Transforms named here are relative to the surface, unless they specify they are relative to the replica layer.
[email protected]94f206c12012-08-25 00:09:14466 //
467 // We will denote a scale by device scale S[deviceScale]
468 //
469 // The render surface draw transform to its target surface origin is:
470 // M[surfaceDraw] = M[owningLayer->Draw]
471 //
472 // The render surface origin transform to its the root (screen space) origin is:
473 // M[surface2root] = M[owningLayer->screenspace] * S[deviceScale].inverse()
474 //
475 // The replica draw transform to its target surface origin is:
476 // M[replicaDraw] = S[deviceScale] * M[surfaceDraw] * Tr[replica->position() + replica->anchor()] * Tr[replica] * Tr[origin2anchor].inverse() * S[contentsScale].inverse()
477 //
478 // The replica draw transform to the root (screen space) origin is:
479 // M[replica2root] = M[surface2root] * Tr[replica->position()] * Tr[replica] * Tr[origin2anchor].inverse()
480 //
481
482 // If we early-exit anywhere in this function, the drawableContentRect of this subtree should be considered empty.
[email protected]aad0a0072012-11-01 18:15:58483 drawableContentRectOfSubtree = gfx::Rect();
[email protected]94f206c12012-08-25 00:09:14484
[email protected]d76806f82012-12-05 21:41:50485 // The root layer cannot skip calcDrawProperties.
[email protected]ecc12622012-10-30 20:45:42486 if (!isRootLayer(layer) && subtreeShouldBeSkipped(layer))
[email protected]94f206c12012-08-25 00:09:14487 return;
488
[email protected]d76806f82012-12-05 21:41:50489 // As this function proceeds, these are the properties for the current
490 // layer that actually get computed. To avoid unnecessary copies
491 // (particularly for matrices), we do computations directly on these values
492 // when possible.
493 DrawProperties<LayerType, RenderSurfaceType>& layerDrawProperties = layer->drawProperties();
494
[email protected]aad0a0072012-11-01 18:15:58495 gfx::Rect clipRectForSubtree;
[email protected]94f206c12012-08-25 00:09:14496 bool subtreeShouldBeClipped = false;
[email protected]498ec6e0e2012-11-30 18:24:57497
[email protected]d76806f82012-12-05 21:41:50498 float accumulatedDrawOpacity = layer->opacity();
[email protected]94f206c12012-08-25 00:09:14499 bool drawOpacityIsAnimating = layer->opacityIsAnimating();
[email protected]498ec6e0e2012-11-30 18:24:57500 if (layer->parent()) {
[email protected]d76806f82012-12-05 21:41:50501 accumulatedDrawOpacity *= layer->parent()->drawOpacity();
[email protected]94f206c12012-08-25 00:09:14502 drawOpacityIsAnimating |= layer->parent()->drawOpacityIsAnimating();
503 }
504
[email protected]6a9cff92012-11-08 11:53:26505 bool animatingTransformToTarget = layer->transformIsAnimating();
506 bool animatingTransformToScreen = animatingTransformToTarget;
507 if (layer->parent()) {
508 animatingTransformToTarget |= layer->parent()->drawTransformIsAnimating();
509 animatingTransformToScreen |= layer->parent()->screenSpaceTransformIsAnimating();
510 }
511
[email protected]aad0a0072012-11-01 18:15:58512 gfx::Size bounds = layer->bounds();
513 gfx::PointF anchorPoint = layer->anchorPoint();
[email protected]c9c1ebe2012-11-05 20:46:13514 gfx::PointF position = layer->position() - layer->scrollDelta();
[email protected]94f206c12012-08-25 00:09:14515
[email protected]c8686a02012-11-27 08:29:00516 gfx::Transform combinedTransform = parentMatrix;
[email protected]b9ab6d542012-12-04 21:33:06517 // LT = Tr[origin] * Tr[origin2anchor]
518 combinedTransform.Translate3d(position.x() + anchorPoint.x() * bounds.width(), position.y() + anchorPoint.y() * bounds.height(), layer->anchorPointZ());
519 // LT = Tr[origin] * Tr[origin2anchor] * M[layer]
520 combinedTransform.PreconcatTransform(layer->transform());
521 // LT = Tr[origin] * Tr[origin2anchor] * M[layer] * Tr[anchor2origin]
522 combinedTransform.Translate3d(-anchorPoint.x() * bounds.width(), -anchorPoint.y() * bounds.height(), -layer->anchorPointZ());
[email protected]94f206c12012-08-25 00:09:14523
[email protected]518ee582012-10-24 18:29:44524 // The layer's contentsSize is determined from the combinedTransform, which then informs the
525 // layer's drawTransform.
[email protected]6a9cff92012-11-08 11:53:26526 updateLayerContentsScale(layer, combinedTransform, deviceScaleFactor, pageScaleFactor, animatingTransformToScreen);
[email protected]518ee582012-10-24 18:29:44527
[email protected]b9ab6d542012-12-04 21:33:06528 // If there is a transformation from the impl thread then it should be at
529 // the start of the combinedTransform, but we don't want it to affect the
530 // computation of contentsScale above.
531 // Note carefully: this is Concat, not Preconcat (implTransform * combinedTransform).
532 combinedTransform.ConcatTransform(layer->implTransform());
[email protected]518ee582012-10-24 18:29:44533
[email protected]94f206c12012-08-25 00:09:14534 if (layer->fixedToContainerLayer()) {
535 // Special case: this layer is a composited fixed-position layer; we need to
536 // explicitly compensate for all ancestors' nonzero scrollDeltas to keep this layer
537 // fixed correctly.
[email protected]b9ab6d542012-12-04 21:33:06538 // Note carefully: this is Concat, not Preconcat (currentScrollCompensation * combinedTransform).
539 combinedTransform.ConcatTransform(currentScrollCompensationMatrix);
[email protected]94f206c12012-08-25 00:09:14540 }
541
542 // The drawTransform that gets computed below is effectively the layer's drawTransform, unless
543 // the layer itself creates a renderSurface. In that case, the renderSurface re-parents the transforms.
[email protected]d76806f82012-12-05 21:41:50544 layerDrawProperties.target_space_transform = combinedTransform;
[email protected]f89f5632012-11-14 23:34:45545 // M[draw] = M[parent] * LT * S[layer2content]
[email protected]d76806f82012-12-05 21:41:50546 layerDrawProperties.target_space_transform.Scale(1.0 / layer->contentsScaleX(), 1.0 / layer->contentsScaleY());
[email protected]94f206c12012-08-25 00:09:14547
548 // layerScreenSpaceTransform represents the transform between root layer's "screen space" and local content space.
[email protected]d76806f82012-12-05 21:41:50549 layerDrawProperties.screen_space_transform = fullHierarchyMatrix;
[email protected]94f206c12012-08-25 00:09:14550 if (!layer->preserves3D())
[email protected]d76806f82012-12-05 21:41:50551 MathUtil::flattenTransformTo2d(layerDrawProperties.screen_space_transform);
552 layerDrawProperties.screen_space_transform.PreconcatTransform(layerDrawProperties.target_space_transform);
[email protected]94f206c12012-08-25 00:09:14553
[email protected]aad0a0072012-11-01 18:15:58554 gfx::RectF contentRect(gfx::PointF(), layer->contentBounds());
[email protected]94f206c12012-08-25 00:09:14555
[email protected]96baf3e2012-10-22 23:09:55556 // fullHierarchyMatrix is the matrix that transforms objects between screen space (except projection matrix) and the most recent RenderSurfaceImpl's space.
557 // nextHierarchyMatrix will only change if this layer uses a new RenderSurfaceImpl, otherwise remains the same.
[email protected]c8686a02012-11-27 08:29:00558 gfx::Transform nextHierarchyMatrix = fullHierarchyMatrix;
559 gfx::Transform sublayerMatrix;
[email protected]94f206c12012-08-25 00:09:14560
[email protected]aad0a0072012-11-01 18:15:58561 gfx::Vector2dF renderSurfaceSublayerScale = MathUtil::computeTransform2dScaleComponents(combinedTransform);
[email protected]518ee582012-10-24 18:29:44562
[email protected]2c7cd6d2012-11-28 23:49:26563 if (subtreeShouldRenderToSeparateSurface(layer, combinedTransform.IsScaleOrTranslation())) {
[email protected]94f206c12012-08-25 00:09:14564 // Check back-face visibility before continuing with this surface and its subtree
565 if (!layer->doubleSided() && transformToParentIsKnown(layer) && isSurfaceBackFaceVisible(layer, combinedTransform))
566 return;
567
568 if (!layer->renderSurface())
569 layer->createRenderSurface();
570
571 RenderSurfaceType* renderSurface = layer->renderSurface();
[email protected]7d929c02012-09-20 17:26:57572 renderSurface->clearLayerLists();
[email protected]94f206c12012-08-25 00:09:14573
[email protected]b9ab6d542012-12-04 21:33:06574 // The owning layer's draw transform has a scale from content to layer
575 // space which we do not want; so here we use the combinedTransform
576 // instead of the drawTransform. However, we do need to add a different
577 // scale factor that accounts for the surface's pixel dimensions.
578 combinedTransform.Scale(1 / renderSurfaceSublayerScale.x(), 1 / renderSurfaceSublayerScale.y());
579 renderSurface->setDrawTransform(combinedTransform);
[email protected]518ee582012-10-24 18:29:44580
[email protected]b9ab6d542012-12-04 21:33:06581 // The owning layer's transform was re-parented by the surface, so the layer's new drawTransform
582 // only needs to scale the layer to surface space.
[email protected]d76806f82012-12-05 21:41:50583 layerDrawProperties.target_space_transform.MakeIdentity();
584 layerDrawProperties.target_space_transform.Scale(renderSurfaceSublayerScale.x() / layer->contentsScaleX(), renderSurfaceSublayerScale.y() / layer->contentsScaleY());
[email protected]94f206c12012-08-25 00:09:14585
[email protected]518ee582012-10-24 18:29:44586 // Inside the surface's subtree, we scale everything to the owning layer's scale.
[email protected]94f206c12012-08-25 00:09:14587 // The sublayer matrix transforms centered layer rects into target
588 // surface content space.
[email protected]b9ab6d542012-12-04 21:33:06589 DCHECK(sublayerMatrix.IsIdentity());
[email protected]c8686a02012-11-27 08:29:00590 sublayerMatrix.Scale(renderSurfaceSublayerScale.x(), renderSurfaceSublayerScale.y());
[email protected]94f206c12012-08-25 00:09:14591
592 // The opacity value is moved from the layer to its surface, so that the entire subtree properly inherits opacity.
[email protected]d76806f82012-12-05 21:41:50593 renderSurface->setDrawOpacity(accumulatedDrawOpacity);
[email protected]94f206c12012-08-25 00:09:14594 renderSurface->setDrawOpacityIsAnimating(drawOpacityIsAnimating);
[email protected]d76806f82012-12-05 21:41:50595 layerDrawProperties.opacity = 1;
596 layerDrawProperties.opacity_is_animating = false;
[email protected]94f206c12012-08-25 00:09:14597
598 renderSurface->setTargetSurfaceTransformsAreAnimating(animatingTransformToTarget);
599 renderSurface->setScreenSpaceTransformsAreAnimating(animatingTransformToScreen);
600 animatingTransformToTarget = false;
[email protected]d76806f82012-12-05 21:41:50601 layerDrawProperties.target_space_transform_is_animating = animatingTransformToTarget;
602 layerDrawProperties.screen_space_transform_is_animating = animatingTransformToScreen;
[email protected]94f206c12012-08-25 00:09:14603
604 // Update the aggregate hierarchy matrix to include the transform of the
[email protected]96baf3e2012-10-22 23:09:55605 // newly created RenderSurfaceImpl.
[email protected]c8686a02012-11-27 08:29:00606 nextHierarchyMatrix.PreconcatTransform(renderSurface->drawTransform());
[email protected]94f206c12012-08-25 00:09:14607
608 // The new renderSurface here will correctly clip the entire subtree. So, we do
609 // not need to continue propagating the clipping state further down the tree. This
610 // way, we can avoid transforming clipRects from ancestor target surface space to
611 // current target surface space that could cause more w < 0 headaches.
612 subtreeShouldBeClipped = false;
613
[email protected]9bab0bb2012-10-08 19:11:58614 if (layer->maskLayer()) {
[email protected]d76806f82012-12-05 21:41:50615 DrawProperties<LayerType, RenderSurfaceType>& maskLayerDrawProperties = layer->maskLayer()->drawProperties();
616 maskLayerDrawProperties.render_target = layer;
617 maskLayerDrawProperties.visible_content_rect = gfx::Rect(gfx::Point(), layer->contentBounds());
[email protected]9bab0bb2012-10-08 19:11:58618 }
[email protected]94f206c12012-08-25 00:09:14619
[email protected]9bab0bb2012-10-08 19:11:58620 if (layer->replicaLayer() && layer->replicaLayer()->maskLayer()) {
[email protected]d76806f82012-12-05 21:41:50621 DrawProperties<LayerType, RenderSurfaceType>& replicaMaskDrawProperties = layer->replicaLayer()->maskLayer()->drawProperties();
622 replicaMaskDrawProperties.render_target = layer;
623 replicaMaskDrawProperties.visible_content_rect = gfx::Rect(gfx::Point(), layer->contentBounds());
[email protected]9bab0bb2012-10-08 19:11:58624 }
[email protected]94f206c12012-08-25 00:09:14625
[email protected]4000abf2012-10-23 04:45:45626 // FIXME: make this smarter for the SkImageFilter case (check for
627 // pixel-moving filters)
628 if (layer->filters().hasFilterThatMovesPixels() || layer->filter())
[email protected]94f206c12012-08-25 00:09:14629 nearestAncestorThatMovesPixels = renderSurface;
630
[email protected]9bab0bb2012-10-08 19:11:58631 // The render surface clipRect is expressed in the space where this surface draws, i.e. the same space as clipRectFromAncestor.
[email protected]dc462d782012-11-21 21:43:01632 renderSurface->setIsClipped(ancestorClipsSubtree);
[email protected]9bab0bb2012-10-08 19:11:58633 if (ancestorClipsSubtree)
634 renderSurface->setClipRect(clipRectFromAncestor);
635 else
[email protected]aad0a0072012-11-01 18:15:58636 renderSurface->setClipRect(gfx::Rect());
[email protected]9bab0bb2012-10-08 19:11:58637
[email protected]94f206c12012-08-25 00:09:14638 renderSurface->setNearestAncestorThatMovesPixels(nearestAncestorThatMovesPixels);
639
[email protected]d58499a2012-10-09 22:27:47640 renderSurfaceLayerList.push_back(layer);
[email protected]94f206c12012-08-25 00:09:14641 } else {
[email protected]ecc12622012-10-30 20:45:42642 DCHECK(layer->parent());
643
[email protected]d76806f82012-12-05 21:41:50644 // Note: layerDrawProperties.target_space_transform is computed above,
645 // before this if-else statement.
646 layerDrawProperties.target_space_transform_is_animating = animatingTransformToTarget;
647 layerDrawProperties.screen_space_transform_is_animating = animatingTransformToScreen;
648 layerDrawProperties.opacity = accumulatedDrawOpacity;
649 layerDrawProperties.opacity_is_animating = drawOpacityIsAnimating;
[email protected]94f206c12012-08-25 00:09:14650 sublayerMatrix = combinedTransform;
651
[email protected]ecc12622012-10-30 20:45:42652 layer->clearRenderSurface();
[email protected]94f206c12012-08-25 00:09:14653
[email protected]ecc12622012-10-30 20:45:42654 // Layers without renderSurfaces directly inherit the ancestor's clip status.
655 subtreeShouldBeClipped = ancestorClipsSubtree;
656 if (ancestorClipsSubtree)
657 clipRectForSubtree = clipRectFromAncestor;
[email protected]94f206c12012-08-25 00:09:14658
[email protected]ecc12622012-10-30 20:45:42659 // Layers that are not their own renderTarget will render into the target of their nearest ancestor.
[email protected]d76806f82012-12-05 21:41:50660 layerDrawProperties.render_target = layer->parent()->renderTarget();
[email protected]94f206c12012-08-25 00:09:14661 }
662
[email protected]aad0a0072012-11-01 18:15:58663 gfx::Rect rectInTargetSpace = ToEnclosingRect(MathUtil::mapClippedRect(layer->drawTransform(), contentRect));
[email protected]94f206c12012-08-25 00:09:14664
665 if (layerClipsSubtree(layer)) {
666 subtreeShouldBeClipped = true;
667 if (ancestorClipsSubtree && !layer->renderSurface()) {
668 clipRectForSubtree = clipRectFromAncestor;
[email protected]aad0a0072012-11-01 18:15:58669 clipRectForSubtree.Intersect(rectInTargetSpace);
[email protected]94f206c12012-08-25 00:09:14670 } else
671 clipRectForSubtree = rectInTargetSpace;
672 }
673
674 // Flatten to 2D if the layer doesn't preserve 3D.
675 if (!layer->preserves3D())
[email protected]96baf3e2012-10-22 23:09:55676 MathUtil::flattenTransformTo2d(sublayerMatrix);
[email protected]94f206c12012-08-25 00:09:14677
678 // Apply the sublayer transform at the center of the layer.
[email protected]b9ab6d542012-12-04 21:33:06679 if (!layer->sublayerTransform().IsIdentity()) {
680 sublayerMatrix.Translate(0.5 * bounds.width(), 0.5 * bounds.height());
681 sublayerMatrix.PreconcatTransform(layer->sublayerTransform());
682 sublayerMatrix.Translate(-0.5 * bounds.width(), -0.5 * bounds.height());
683 }
[email protected]94f206c12012-08-25 00:09:14684
685 LayerList& descendants = (layer->renderSurface() ? layer->renderSurface()->layerList() : layerList);
686
687 // Any layers that are appended after this point are in the layer's subtree and should be included in the sorting process.
688 unsigned sortingStartIndex = descendants.size();
689
690 if (!layerShouldBeSkipped(layer))
[email protected]d58499a2012-10-09 22:27:47691 descendants.push_back(layer);
[email protected]94f206c12012-08-25 00:09:14692
[email protected]c8686a02012-11-27 08:29:00693 gfx::Transform nextScrollCompensationMatrix = computeScrollCompensationMatrixForChildren(layer, parentMatrix, currentScrollCompensationMatrix);;
[email protected]94f206c12012-08-25 00:09:14694
[email protected]aad0a0072012-11-01 18:15:58695 gfx::Rect accumulatedDrawableContentRectOfChildren;
[email protected]94f206c12012-08-25 00:09:14696 for (size_t i = 0; i < layer->children().size(); ++i) {
[email protected]96baf3e2012-10-22 23:09:55697 LayerType* child = LayerTreeHostCommon::getChildAsRawPtr(layer->children(), i);
[email protected]aad0a0072012-11-01 18:15:58698 gfx::Rect drawableContentRectOfChildSubtree;
[email protected]d76806f82012-12-05 21:41:50699 calculateDrawPropertiesInternal<LayerType, LayerList, RenderSurfaceType, LayerSorter>(child, sublayerMatrix, nextHierarchyMatrix, nextScrollCompensationMatrix,
[email protected]94f206c12012-08-25 00:09:14700 clipRectForSubtree, subtreeShouldBeClipped, nearestAncestorThatMovesPixels,
[email protected]518ee582012-10-24 18:29:44701 renderSurfaceLayerList, descendants, layerSorter, maxTextureSize, deviceScaleFactor, pageScaleFactor, drawableContentRectOfChildSubtree);
[email protected]aad0a0072012-11-01 18:15:58702 if (!drawableContentRectOfChildSubtree.IsEmpty()) {
703 accumulatedDrawableContentRectOfChildren.Union(drawableContentRectOfChildSubtree);
[email protected]94f206c12012-08-25 00:09:14704 if (child->renderSurface())
[email protected]d58499a2012-10-09 22:27:47705 descendants.push_back(child);
[email protected]94f206c12012-08-25 00:09:14706 }
707 }
708
709 // Compute the total drawableContentRect for this subtree (the rect is in targetSurface space)
[email protected]aad0a0072012-11-01 18:15:58710 gfx::Rect localDrawableContentRectOfSubtree = accumulatedDrawableContentRectOfChildren;
[email protected]94f206c12012-08-25 00:09:14711 if (layer->drawsContent())
[email protected]aad0a0072012-11-01 18:15:58712 localDrawableContentRectOfSubtree.Union(rectInTargetSpace);
[email protected]94f206c12012-08-25 00:09:14713 if (subtreeShouldBeClipped)
[email protected]aad0a0072012-11-01 18:15:58714 localDrawableContentRectOfSubtree.Intersect(clipRectForSubtree);
[email protected]94f206c12012-08-25 00:09:14715
716 // Compute the layer's drawable content rect (the rect is in targetSurface space)
[email protected]d76806f82012-12-05 21:41:50717 layerDrawProperties.drawable_content_rect = rectInTargetSpace;
[email protected]94f206c12012-08-25 00:09:14718 if (subtreeShouldBeClipped)
[email protected]d76806f82012-12-05 21:41:50719 layerDrawProperties.drawable_content_rect.Intersect(clipRectForSubtree);
[email protected]94f206c12012-08-25 00:09:14720
[email protected]dc462d782012-11-21 21:43:01721 // Tell the layer the rect that is clipped by. In theory we could use a
722 // tighter clipRect here (drawableContentRect), but that actually does not
723 // reduce how much would be drawn, and instead it would create unnecessary
724 // changes to scissor state affecting GPU performance.
[email protected]d76806f82012-12-05 21:41:50725 layerDrawProperties.is_clipped = subtreeShouldBeClipped;
[email protected]dc462d782012-11-21 21:43:01726 if (subtreeShouldBeClipped)
[email protected]d76806f82012-12-05 21:41:50727 layerDrawProperties.clip_rect = clipRectForSubtree;
[email protected]dc462d782012-11-21 21:43:01728 else {
729 // Initialize the clipRect to a safe value that will not clip the
730 // layer, just in case clipping is still accidentally used.
[email protected]d76806f82012-12-05 21:41:50731 layerDrawProperties.clip_rect = rectInTargetSpace;
[email protected]dc462d782012-11-21 21:43:01732 }
733
[email protected]9bab0bb2012-10-08 19:11:58734 // Compute the layer's visible content rect (the rect is in content space)
[email protected]d76806f82012-12-05 21:41:50735 layerDrawProperties.visible_content_rect = calculateVisibleContentRect(layer);
[email protected]9bab0bb2012-10-08 19:11:58736
[email protected]94f206c12012-08-25 00:09:14737 // Compute the remaining properties for the render surface, if the layer has one.
[email protected]ecc12622012-10-30 20:45:42738 if (isRootLayer(layer)) {
739 // The root layer's surface's contentRect is always the entire viewport.
740 DCHECK(layer->renderSurface());
741 layer->renderSurface()->setContentRect(clipRectFromAncestor);
742 } else if (layer->renderSurface() && !isRootLayer(layer)) {
[email protected]94f206c12012-08-25 00:09:14743 RenderSurfaceType* renderSurface = layer->renderSurface();
[email protected]aad0a0072012-11-01 18:15:58744 gfx::Rect clippedContentRect = localDrawableContentRectOfSubtree;
[email protected]94f206c12012-08-25 00:09:14745
[email protected]94f206c12012-08-25 00:09:14746 // Don't clip if the layer is reflected as the reflection shouldn't be
747 // clipped. If the layer is animating, then the surface's transform to
748 // its target is not known on the main thread, and we should not use it
749 // to clip.
750 if (!layer->replicaLayer() && transformToParentIsKnown(layer)) {
751 // 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:58752 if (ancestorClipsSubtree && !clippedContentRect.IsEmpty()) {
753 gfx::Rect surfaceClipRect = LayerTreeHostCommon::calculateVisibleRect(renderSurface->clipRect(), clippedContentRect, renderSurface->drawTransform());
754 clippedContentRect.Intersect(surfaceClipRect);
[email protected]94f206c12012-08-25 00:09:14755 }
756 }
757
[email protected]96baf3e2012-10-22 23:09:55758 // The RenderSurfaceImpl backing texture cannot exceed the maximum supported
[email protected]94f206c12012-08-25 00:09:14759 // texture size.
[email protected]aad0a0072012-11-01 18:15:58760 clippedContentRect.set_width(std::min(clippedContentRect.width(), maxTextureSize));
761 clippedContentRect.set_height(std::min(clippedContentRect.height(), maxTextureSize));
[email protected]94f206c12012-08-25 00:09:14762
[email protected]aad0a0072012-11-01 18:15:58763 if (clippedContentRect.IsEmpty())
[email protected]7d929c02012-09-20 17:26:57764 renderSurface->clearLayerLists();
[email protected]94f206c12012-08-25 00:09:14765
766 renderSurface->setContentRect(clippedContentRect);
[email protected]518ee582012-10-24 18:29:44767
768 // The owning layer's screenSpaceTransform has a scale from content to layer space which we need to undo and
769 // replace with a scale from the surface's subtree into layer space.
[email protected]c8686a02012-11-27 08:29:00770 gfx::Transform screenSpaceTransform = layer->screenSpaceTransform();
[email protected]b9ab6d542012-12-04 21:33:06771 screenSpaceTransform.Scale(layer->contentsScaleX() / renderSurfaceSublayerScale.x(), layer->contentsScaleY() / renderSurfaceSublayerScale.y());
[email protected]518ee582012-10-24 18:29:44772 renderSurface->setScreenSpaceTransform(screenSpaceTransform);
[email protected]94f206c12012-08-25 00:09:14773
774 if (layer->replicaLayer()) {
[email protected]c8686a02012-11-27 08:29:00775 gfx::Transform surfaceOriginToReplicaOriginTransform;
776 surfaceOriginToReplicaOriginTransform.Scale(renderSurfaceSublayerScale.x(), renderSurfaceSublayerScale.y());
777 surfaceOriginToReplicaOriginTransform.Translate(layer->replicaLayer()->position().x() + layer->replicaLayer()->anchorPoint().x() * bounds.width(),
[email protected]94f206c12012-08-25 00:09:14778 layer->replicaLayer()->position().y() + layer->replicaLayer()->anchorPoint().y() * bounds.height());
[email protected]c8686a02012-11-27 08:29:00779 surfaceOriginToReplicaOriginTransform.PreconcatTransform(layer->replicaLayer()->transform());
780 surfaceOriginToReplicaOriginTransform.Translate(-layer->replicaLayer()->anchorPoint().x() * bounds.width(), -layer->replicaLayer()->anchorPoint().y() * bounds.height());
781 surfaceOriginToReplicaOriginTransform.Scale(1 / renderSurfaceSublayerScale.x(), 1 / renderSurfaceSublayerScale.y());
[email protected]94f206c12012-08-25 00:09:14782
783 // Compute the replica's "originTransform" that maps from the replica's origin space to the target surface origin space.
[email protected]c8686a02012-11-27 08:29:00784 gfx::Transform replicaOriginTransform = layer->renderSurface()->drawTransform() * surfaceOriginToReplicaOriginTransform;
[email protected]94f206c12012-08-25 00:09:14785 renderSurface->setReplicaDrawTransform(replicaOriginTransform);
786
787 // Compute the replica's "screenSpaceTransform" that maps from the replica's origin space to the screen's origin space.
[email protected]c8686a02012-11-27 08:29:00788 gfx::Transform replicaScreenSpaceTransform = layer->renderSurface()->screenSpaceTransform() * surfaceOriginToReplicaOriginTransform;
[email protected]94f206c12012-08-25 00:09:14789 renderSurface->setReplicaScreenSpaceTransform(replicaScreenSpaceTransform);
790 }
791
792 // If a render surface has no layer list, then it and none of its children needed to get drawn.
793 if (!layer->renderSurface()->layerList().size()) {
794 // FIXME: Originally we asserted that this layer was already at the end of the
795 // list, and only needed to remove that layer. For now, we remove the
796 // entire subtree of surfaces to fix a crash bug. The root cause is
797 // https://bugs.webkit.org/show_bug.cgi?id=74147 and we should be able
798 // to put the original assert after fixing that.
[email protected]d58499a2012-10-09 22:27:47799 while (renderSurfaceLayerList.back() != layer) {
800 renderSurfaceLayerList.back()->clearRenderSurface();
801 renderSurfaceLayerList.pop_back();
[email protected]94f206c12012-08-25 00:09:14802 }
[email protected]1d993172012-10-18 18:15:04803 DCHECK(renderSurfaceLayerList.back() == layer);
[email protected]d58499a2012-10-09 22:27:47804 renderSurfaceLayerList.pop_back();
[email protected]94f206c12012-08-25 00:09:14805 layer->clearRenderSurface();
806 return;
807 }
808 }
809
[email protected]6ef21ffc2012-11-28 05:58:54810 markLayerAsUpdated(layer);
811
[email protected]94f206c12012-08-25 00:09:14812 // If neither this layer nor any of its children were added, early out.
813 if (sortingStartIndex == descendants.size())
814 return;
815
816 // If preserves-3d then sort all the descendants in 3D so that they can be
817 // drawn from back to front. If the preserves-3d property is also set on the parent then
818 // skip the sorting as the parent will sort all the descendants anyway.
819 if (descendants.size() && layer->preserves3D() && (!layer->parent() || !layer->parent()->preserves3D()))
[email protected]d58499a2012-10-09 22:27:47820 sortLayers(descendants.begin() + sortingStartIndex, descendants.end(), layerSorter);
[email protected]94f206c12012-08-25 00:09:14821
822 if (layer->renderSurface())
[email protected]aad0a0072012-11-01 18:15:58823 drawableContentRectOfSubtree = gfx::ToEnclosingRect(layer->renderSurface()->drawableContentRect());
[email protected]94f206c12012-08-25 00:09:14824 else
825 drawableContentRectOfSubtree = localDrawableContentRectOfSubtree;
826
[email protected]7d929c02012-09-20 17:26:57827 if (layer->hasContributingDelegatedRenderPasses())
828 layer->renderTarget()->renderSurface()->addContributingDelegatedRenderPassLayer(layer);
[email protected]94f206c12012-08-25 00:09:14829}
830
[email protected]d76806f82012-12-05 21:41:50831void LayerTreeHostCommon::calculateDrawProperties(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:14832{
[email protected]aad0a0072012-11-01 18:15:58833 gfx::Rect totalDrawableContentRect;
[email protected]c8686a02012-11-27 08:29:00834 gfx::Transform identityMatrix;
835 gfx::Transform deviceScaleTransform;
836 deviceScaleTransform.Scale(deviceScaleFactor, deviceScaleFactor);
[email protected]ecc12622012-10-30 20:45:42837 std::vector<scoped_refptr<Layer> > 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]d76806f82012-12-05 21:41:50846 cc::calculateDrawPropertiesInternal<Layer, std::vector<scoped_refptr<Layer> >, RenderSurface, void>(
[email protected]ecc12622012-10-30 20:45:42847 rootLayer, deviceScaleTransform, identityMatrix, identityMatrix,
848 deviceViewportRect, subtreeShouldBeClipped, 0, renderSurfaceLayerList,
849 dummyLayerList, 0, 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);
[email protected]d76806f82012-12-05 21:41:50854 // A root layer renderSurface should always exist after calculateDrawProperties.
[email protected]ecc12622012-10-30 20:45:42855 DCHECK(rootLayer->renderSurface());
[email protected]94f206c12012-08-25 00:09:14856}
857
[email protected]d76806f82012-12-05 21:41:50858void LayerTreeHostCommon::calculateDrawProperties(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:14859{
[email protected]aad0a0072012-11-01 18:15:58860 gfx::Rect totalDrawableContentRect;
[email protected]c8686a02012-11-27 08:29:00861 gfx::Transform identityMatrix;
862 gfx::Transform deviceScaleTransform;
863 deviceScaleTransform.Scale(deviceScaleFactor, deviceScaleFactor);
[email protected]ecc12622012-10-30 20:45:42864 std::vector<LayerImpl*> dummyLayerList;
[email protected]94f206c12012-08-25 00:09:14865
[email protected]ecc12622012-10-30 20:45:42866 // The root layer's renderSurface should receive the deviceViewport as the initial clipRect.
867 bool subtreeShouldBeClipped = true;
[email protected]aad0a0072012-11-01 18:15:58868 gfx::Rect deviceViewportRect(gfx::Point(), deviceViewportSize);
[email protected]ecc12622012-10-30 20:45:42869
870 // This function should have received a root layer.
871 DCHECK(isRootLayer(rootLayer));
[email protected]94f206c12012-08-25 00:09:14872
[email protected]d76806f82012-12-05 21:41:50873 cc::calculateDrawPropertiesInternal<LayerImpl, std::vector<LayerImpl*>, RenderSurfaceImpl, LayerSorter>(
[email protected]ecc12622012-10-30 20:45:42874 rootLayer, deviceScaleTransform, identityMatrix, identityMatrix,
875 deviceViewportRect, subtreeShouldBeClipped, 0, renderSurfaceLayerList,
876 dummyLayerList, layerSorter, maxTextureSize,
[email protected]518ee582012-10-24 18:29:44877 deviceScaleFactor, pageScaleFactor, totalDrawableContentRect);
[email protected]ecc12622012-10-30 20:45:42878
879 // The dummy layer list should not have been used.
880 DCHECK(dummyLayerList.size() == 0);
[email protected]d76806f82012-12-05 21:41:50881 // A root layer renderSurface should always exist after calculateDrawProperties.
[email protected]ecc12622012-10-30 20:45:42882 DCHECK(rootLayer->renderSurface());
[email protected]94f206c12012-08-25 00:09:14883}
884
[email protected]c8686a02012-11-27 08:29:00885static bool pointHitsRect(const gfx::PointF& screenSpacePoint, const gfx::Transform& localSpaceToScreenSpaceTransform, gfx::RectF localSpaceRect)
[email protected]94f206c12012-08-25 00:09:14886{
887 // If the transform is not invertible, then assume that this point doesn't hit this rect.
[email protected]c8686a02012-11-27 08:29:00888 if (!localSpaceToScreenSpaceTransform.IsInvertible())
[email protected]94f206c12012-08-25 00:09:14889 return false;
890
891 // Transform the hit test point from screen space to the local space of the given rect.
892 bool clipped = false;
[email protected]c8686a02012-11-27 08:29:00893 gfx::PointF hitTestPointInLocalSpace = MathUtil::projectPoint(MathUtil::inverse(localSpaceToScreenSpaceTransform), screenSpacePoint, clipped);
[email protected]94f206c12012-08-25 00:09:14894
895 // If projectPoint could not project to a valid value, then we assume that this point doesn't hit this rect.
896 if (clipped)
897 return false;
898
[email protected]aad0a0072012-11-01 18:15:58899 return localSpaceRect.Contains(hitTestPointInLocalSpace);
[email protected]94f206c12012-08-25 00:09:14900}
901
[email protected]c8686a02012-11-27 08:29:00902static bool pointHitsRegion(gfx::PointF screenSpacePoint, const gfx::Transform& screenSpaceTransform, const Region& layerSpaceRegion, float layerContentScaleX, float layerContentScaleY)
[email protected]2f1acc262012-11-16 21:42:22903{
904 // If the transform is not invertible, then assume that this point doesn't hit this region.
[email protected]c8686a02012-11-27 08:29:00905 if (!screenSpaceTransform.IsInvertible())
[email protected]2f1acc262012-11-16 21:42:22906 return false;
907
908 // Transform the hit test point from screen space to the local space of the given region.
909 bool clipped = false;
[email protected]c8686a02012-11-27 08:29:00910 gfx::PointF hitTestPointInContentSpace = MathUtil::projectPoint(MathUtil::inverse(screenSpaceTransform), screenSpacePoint, clipped);
[email protected]2f1acc262012-11-16 21:42:22911 gfx::PointF hitTestPointInLayerSpace = gfx::ScalePoint(hitTestPointInContentSpace, 1 / layerContentScaleX, 1 / layerContentScaleY);
912
913 // If projectPoint could not project to a valid value, then we assume that this point doesn't hit this region.
914 if (clipped)
915 return false;
916
917 return layerSpaceRegion.Contains(gfx::ToRoundedPoint(hitTestPointInLayerSpace));
918}
919
[email protected]d455d552012-11-02 00:19:06920static bool pointIsClippedBySurfaceOrClipRect(const gfx::PointF& screenSpacePoint, LayerImpl* layer)
[email protected]94f206c12012-08-25 00:09:14921{
[email protected]96baf3e2012-10-22 23:09:55922 LayerImpl* currentLayer = layer;
[email protected]94f206c12012-08-25 00:09:14923
924 // Walk up the layer tree and hit-test any renderSurfaces and any layer clipRects that are active.
925 while (currentLayer) {
[email protected]01527732012-10-19 18:16:19926 if (currentLayer->renderSurface() && !pointHitsRect(screenSpacePoint, currentLayer->renderSurface()->screenSpaceTransform(), currentLayer->renderSurface()->contentRect()))
[email protected]94f206c12012-08-25 00:09:14927 return true;
928
929 // Note that drawableContentRects are actually in targetSurface space, so the transform we
930 // have to provide is the target surface's screenSpaceTransform.
[email protected]96baf3e2012-10-22 23:09:55931 LayerImpl* renderTarget = currentLayer->renderTarget();
[email protected]01527732012-10-19 18:16:19932 if (layerClipsSubtree(currentLayer) && !pointHitsRect(screenSpacePoint, renderTarget->renderSurface()->screenSpaceTransform(), currentLayer->drawableContentRect()))
[email protected]94f206c12012-08-25 00:09:14933 return true;
934
935 currentLayer = currentLayer->parent();
936 }
937
938 // If we have finished walking all ancestors without having already exited, then the point is not clipped by any ancestors.
939 return false;
940}
941
[email protected]d455d552012-11-02 00:19:06942LayerImpl* LayerTreeHostCommon::findLayerThatIsHitByPoint(const gfx::PointF& screenSpacePoint, std::vector<LayerImpl*>& renderSurfaceLayerList)
[email protected]94f206c12012-08-25 00:09:14943{
[email protected]96baf3e2012-10-22 23:09:55944 LayerImpl* foundLayer = 0;
[email protected]94f206c12012-08-25 00:09:14945
[email protected]96baf3e2012-10-22 23:09:55946 typedef LayerIterator<LayerImpl, std::vector<LayerImpl*>, RenderSurfaceImpl, LayerIteratorActions::FrontToBack> LayerIteratorType;
947 LayerIteratorType end = LayerIteratorType::end(&renderSurfaceLayerList);
[email protected]94f206c12012-08-25 00:09:14948
[email protected]96baf3e2012-10-22 23:09:55949 for (LayerIteratorType it = LayerIteratorType::begin(&renderSurfaceLayerList); it != end; ++it) {
[email protected]94f206c12012-08-25 00:09:14950 // We don't want to consider renderSurfaces for hit testing.
951 if (!it.representsItself())
952 continue;
953
[email protected]96baf3e2012-10-22 23:09:55954 LayerImpl* currentLayer = (*it);
[email protected]94f206c12012-08-25 00:09:14955
[email protected]aad0a0072012-11-01 18:15:58956 gfx::RectF contentRect(gfx::PointF(), currentLayer->contentBounds());
[email protected]01527732012-10-19 18:16:19957 if (!pointHitsRect(screenSpacePoint, currentLayer->screenSpaceTransform(), contentRect))
[email protected]94f206c12012-08-25 00:09:14958 continue;
959
960 // At this point, we think the point does hit the layer, but we need to walk up
961 // the parents to ensure that the layer was not clipped in such a way that the
962 // hit point actually should not hit the layer.
[email protected]01527732012-10-19 18:16:19963 if (pointIsClippedBySurfaceOrClipRect(screenSpacePoint, currentLayer))
[email protected]94f206c12012-08-25 00:09:14964 continue;
965
966 foundLayer = currentLayer;
967 break;
968 }
969
[email protected]01527732012-10-19 18:16:19970 // 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:14971 return foundLayer;
972}
973
[email protected]2f1acc262012-11-16 21:42:22974LayerImpl* LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(const gfx::PointF& screenSpacePoint, std::vector<LayerImpl*>& renderSurfaceLayerList)
975{
976 LayerImpl* foundLayer = 0;
977
978 typedef LayerIterator<LayerImpl, std::vector<LayerImpl*>, RenderSurfaceImpl, LayerIteratorActions::FrontToBack> LayerIteratorType;
979 LayerIteratorType end = LayerIteratorType::end(&renderSurfaceLayerList);
980
981 for (LayerIteratorType it = LayerIteratorType::begin(&renderSurfaceLayerList); it != end; ++it) {
982 // We don't want to consider renderSurfaces for hit testing.
983 if (!it.representsItself())
984 continue;
985
986 LayerImpl* currentLayer = (*it);
987
988 if (currentLayer->touchEventHandlerRegion().IsEmpty())
989 continue;
990
991 if (!pointHitsRegion(screenSpacePoint, currentLayer->screenSpaceTransform(), currentLayer->touchEventHandlerRegion(), currentLayer->contentsScaleX(), currentLayer->contentsScaleY()))
992 continue;
993
994 // At this point, we think the point does hit the touch event handler region on the layer, but we need to walk up
995 // the parents to ensure that the layer was not clipped in such a way that the
996 // hit point actually should not hit the layer.
997 if (pointIsClippedBySurfaceOrClipRect(screenSpacePoint, currentLayer))
998 continue;
999
1000 foundLayer = currentLayer;
1001 break;
1002 }
1003
1004 // This can potentially return 0, which means the screenSpacePoint did not successfully hit test any layers, not even the root layer.
1005 return foundLayer;
1006}
1007
[email protected]bc5e77c2012-11-05 20:00:491008} // namespace cc