blob: ca7d45050f85c0857e5f88a4def5818ef1b5374e [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]2f1acc262012-11-16 21:42:2214#include "ui/gfx/point_conversions.h"
[email protected]aad0a0072012-11-01 18:15:5815#include "ui/gfx/rect_conversions.h"
[email protected]518ee582012-10-24 18:29:4416#include <algorithm>
[email protected]94f206c12012-08-25 00:09:1417#include <public/WebTransformationMatrix.h>
18
19using WebKit::WebTransformationMatrix;
20
[email protected]9c88e562012-09-14 22:21:3021namespace cc {
[email protected]94f206c12012-08-25 00:09:1422
[email protected]96baf3e2012-10-22 23:09:5523ScrollAndScaleSet::ScrollAndScaleSet()
[email protected]493067512012-09-19 23:34:1024{
25}
26
[email protected]96baf3e2012-10-22 23:09:5527ScrollAndScaleSet::~ScrollAndScaleSet()
[email protected]493067512012-09-19 23:34:1028{
29}
30
[email protected]aad0a0072012-11-01 18:15:5831gfx::Rect LayerTreeHostCommon::calculateVisibleRect(const gfx::Rect& targetSurfaceRect, const gfx::Rect& layerBoundRect, const WebTransformationMatrix& transform)
[email protected]94f206c12012-08-25 00:09:1432{
33 // Is this layer fully contained within the target surface?
[email protected]aad0a0072012-11-01 18:15:5834 gfx::Rect layerInSurfaceSpace = MathUtil::mapClippedRect(transform, layerBoundRect);
35 if (targetSurfaceRect.Contains(layerInSurfaceSpace))
[email protected]94f206c12012-08-25 00:09:1436 return layerBoundRect;
37
38 // If the layer doesn't fill up the entire surface, then find the part of
39 // the surface rect where the layer could be visible. This avoids trying to
40 // project surface rect points that are behind the projection point.
[email protected]aad0a0072012-11-01 18:15:5841 gfx::Rect minimalSurfaceRect = targetSurfaceRect;
42 minimalSurfaceRect.Intersect(layerInSurfaceSpace);
[email protected]94f206c12012-08-25 00:09:1443
44 // Project the corners of the target surface rect into the layer space.
45 // This bounding rectangle may be larger than it needs to be (being
46 // axis-aligned), but is a reasonable filter on the space to consider.
47 // Non-invertible transforms will create an empty rect here.
48 const WebTransformationMatrix surfaceToLayer = transform.inverse();
[email protected]aad0a0072012-11-01 18:15:5849 gfx::Rect layerRect = gfx::ToEnclosingRect(MathUtil::projectClippedRect(surfaceToLayer, gfx::RectF(minimalSurfaceRect)));
50 layerRect.Intersect(layerBoundRect);
[email protected]94f206c12012-08-25 00:09:1451 return layerRect;
52}
53
[email protected]ecc12622012-10-30 20:45:4254template <typename LayerType>
55static inline bool isRootLayer(LayerType* layer)
56{
57 return !layer->parent();
58}
59
[email protected]94f206c12012-08-25 00:09:1460template<typename LayerType>
61static inline bool layerIsInExisting3DRenderingContext(LayerType* layer)
62{
63 // According to current W3C spec on CSS transforms, a layer is part of an established
64 // 3d rendering context if its parent has transform-style of preserves-3d.
65 return layer->parent() && layer->parent()->preserves3D();
66}
67
68template<typename LayerType>
[email protected]ecc12622012-10-30 20:45:4269static bool isRootLayerOfNewRenderingContext(LayerType* layer)
[email protected]94f206c12012-08-25 00:09:1470{
71 // According to current W3C spec on CSS transforms (Section 6.1), a layer is the
72 // beginning of 3d rendering context if its parent does not have transform-style:
73 // preserve-3d, but this layer itself does.
74 if (layer->parent())
75 return !layer->parent()->preserves3D() && layer->preserves3D();
76
77 return layer->preserves3D();
78}
79
80template<typename LayerType>
81static bool isLayerBackFaceVisible(LayerType* layer)
82{
83 // The current W3C spec on CSS transforms says that backface visibility should be
84 // determined differently depending on whether the layer is in a "3d rendering
85 // context" or not. For Chromium code, we can determine whether we are in a 3d
86 // rendering context by checking if the parent preserves 3d.
87
88 if (layerIsInExisting3DRenderingContext(layer))
89 return layer->drawTransform().isBackFaceVisible();
90
91 // In this case, either the layer establishes a new 3d rendering context, or is not in
92 // a 3d rendering context at all.
93 return layer->transform().isBackFaceVisible();
94}
95
96template<typename LayerType>
97static bool isSurfaceBackFaceVisible(LayerType* layer, const WebTransformationMatrix& drawTransform)
98{
99 if (layerIsInExisting3DRenderingContext(layer))
100 return drawTransform.isBackFaceVisible();
101
[email protected]ecc12622012-10-30 20:45:42102 if (isRootLayerOfNewRenderingContext(layer))
[email protected]94f206c12012-08-25 00:09:14103 return layer->transform().isBackFaceVisible();
104
105 // If the renderSurface is not part of a new or existing rendering context, then the
106 // layers that contribute to this surface will decide back-face visibility for themselves.
107 return false;
108}
109
110template<typename LayerType>
111static inline bool layerClipsSubtree(LayerType* layer)
112{
113 return layer->masksToBounds() || layer->maskLayer();
114}
115
116template<typename LayerType>
[email protected]aad0a0072012-11-01 18:15:58117static gfx::Rect calculateVisibleContentRect(LayerType* layer)
[email protected]94f206c12012-08-25 00:09:14118{
[email protected]1d993172012-10-18 18:15:04119 DCHECK(layer->renderTarget());
[email protected]94f206c12012-08-25 00:09:14120
[email protected]9bab0bb2012-10-08 19:11:58121 // Nothing is visible if the layer bounds are empty.
[email protected]aad0a0072012-11-01 18:15:58122 if (!layer->drawsContent() || layer->contentBounds().IsEmpty() || layer->drawableContentRect().IsEmpty())
123 return gfx::Rect();
[email protected]94f206c12012-08-25 00:09:14124
[email protected]aad0a0072012-11-01 18:15:58125 gfx::Rect targetSurfaceClipRect;
[email protected]9bab0bb2012-10-08 19:11:58126
127 // First, compute visible bounds in target surface space.
[email protected]aad0a0072012-11-01 18:15:58128 if (layer->renderTarget()->renderSurface()->clipRect().IsEmpty())
[email protected]9bab0bb2012-10-08 19:11:58129 targetSurfaceClipRect = layer->drawableContentRect();
130 else {
131 // In this case the target surface does clip layers that contribute to it. So, we
132 // have convert the current surface's clipRect from its ancestor surface space to
133 // the current surface space.
[email protected]aad0a0072012-11-01 18:15:58134 targetSurfaceClipRect = gfx::ToEnclosingRect(MathUtil::projectClippedRect(layer->renderTarget()->renderSurface()->drawTransform().inverse(), layer->renderTarget()->renderSurface()->clipRect()));
135 targetSurfaceClipRect.Intersect(layer->drawableContentRect());
[email protected]9bab0bb2012-10-08 19:11:58136 }
137
[email protected]aad0a0072012-11-01 18:15:58138 if (targetSurfaceClipRect.IsEmpty())
139 return gfx::Rect();
[email protected]9bab0bb2012-10-08 19:11:58140
[email protected]aad0a0072012-11-01 18:15:58141 return LayerTreeHostCommon::calculateVisibleRect(targetSurfaceClipRect, gfx::Rect(gfx::Point(), layer->contentBounds()), layer->drawTransform());
[email protected]94f206c12012-08-25 00:09:14142}
143
144static bool isScaleOrTranslation(const WebTransformationMatrix& m)
145{
146 return !m.m12() && !m.m13() && !m.m14()
147 && !m.m21() && !m.m23() && !m.m24()
148 && !m.m31() && !m.m32() && !m.m43()
149 && m.m44();
150}
151
[email protected]96baf3e2012-10-22 23:09:55152static inline bool transformToParentIsKnown(LayerImpl*)
[email protected]94f206c12012-08-25 00:09:14153{
154 return true;
155}
156
[email protected]96baf3e2012-10-22 23:09:55157static inline bool transformToParentIsKnown(Layer* layer)
[email protected]94f206c12012-08-25 00:09:14158{
159 return !layer->transformIsAnimating();
160}
161
[email protected]96baf3e2012-10-22 23:09:55162static inline bool transformToScreenIsKnown(LayerImpl*)
[email protected]94f206c12012-08-25 00:09:14163{
164 return true;
165}
166
[email protected]96baf3e2012-10-22 23:09:55167static inline bool transformToScreenIsKnown(Layer* layer)
[email protected]94f206c12012-08-25 00:09:14168{
169 return !layer->screenSpaceTransformIsAnimating();
170}
171
172template<typename LayerType>
173static bool layerShouldBeSkipped(LayerType* layer)
174{
175 // Layers can be skipped if any of these conditions are met.
176 // - does not draw content.
177 // - is transparent
178 // - has empty bounds
179 // - the layer is not double-sided, but its back face is visible.
180 //
181 // Some additional conditions need to be computed at a later point after the recursion is finished.
182 // - the intersection of render surface content and layer clipRect is empty
183 // - the visibleContentRect is empty
184 //
185 // Note, if the layer should not have been drawn due to being fully transparent,
186 // we would have skipped the entire subtree and never made it into this function,
187 // so it is safe to omit this check here.
188
[email protected]aad0a0072012-11-01 18:15:58189 if (!layer->drawsContent() || layer->bounds().IsEmpty())
[email protected]94f206c12012-08-25 00:09:14190 return true;
191
192 LayerType* backfaceTestLayer = layer;
193 if (layer->useParentBackfaceVisibility()) {
[email protected]1d993172012-10-18 18:15:04194 DCHECK(layer->parent());
195 DCHECK(!layer->parent()->useParentBackfaceVisibility());
[email protected]94f206c12012-08-25 00:09:14196 backfaceTestLayer = layer->parent();
197 }
198
199 // 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.
200 if (!backfaceTestLayer->doubleSided() && transformToScreenIsKnown(backfaceTestLayer) && isLayerBackFaceVisible(backfaceTestLayer))
201 return true;
202
203 return false;
204}
205
[email protected]96baf3e2012-10-22 23:09:55206static inline bool subtreeShouldBeSkipped(LayerImpl* layer)
[email protected]94f206c12012-08-25 00:09:14207{
208 // The opacity of a layer always applies to its children (either implicitly
209 // via a render surface or explicitly if the parent preserves 3D), so the
210 // entire subtree can be skipped if this layer is fully transparent.
211 return !layer->opacity();
212}
213
[email protected]96baf3e2012-10-22 23:09:55214static inline bool subtreeShouldBeSkipped(Layer* layer)
[email protected]94f206c12012-08-25 00:09:14215{
216 // If the opacity is being animated then the opacity on the main thread is unreliable
217 // (since the impl thread may be using a different opacity), so it should not be trusted.
218 // In particular, it should not cause the subtree to be skipped.
219 return !layer->opacity() && !layer->opacityIsAnimating();
220}
221
222template<typename LayerType>
223static bool subtreeShouldRenderToSeparateSurface(LayerType* layer, bool axisAlignedWithRespectToParent)
224{
[email protected]94f206c12012-08-25 00:09:14225 //
[email protected]96baf3e2012-10-22 23:09:55226 // A layer and its descendants should render onto a new RenderSurfaceImpl if any of these rules hold:
[email protected]94f206c12012-08-25 00:09:14227 //
228
[email protected]ecc12622012-10-30 20:45:42229 // The root layer should always have a renderSurface.
230 if (isRootLayer(layer))
231 return true;
232
[email protected]94f206c12012-08-25 00:09:14233 // If we force it.
234 if (layer->forceRenderSurface())
235 return true;
236
237 // If the layer uses a mask.
238 if (layer->maskLayer())
239 return true;
240
241 // If the layer has a reflection.
242 if (layer->replicaLayer())
243 return true;
244
245 // If the layer uses a CSS filter.
[email protected]4000abf2012-10-23 04:45:45246 if (!layer->filters().isEmpty() || !layer->backgroundFilters().isEmpty() || layer->filter())
[email protected]94f206c12012-08-25 00:09:14247 return true;
248
[email protected]ecc12622012-10-30 20:45:42249 // Cache this value, because otherwise it walks the entire subtree several times.
250 bool descendantDrawsContent = layer->descendantDrawsContent();
251
[email protected]94f206c12012-08-25 00:09:14252 // If the layer flattens its subtree (i.e. the layer doesn't preserve-3d), but it is
253 // treated as a 3D object by its parent (i.e. parent does preserve-3d).
254 if (layerIsInExisting3DRenderingContext(layer) && !layer->preserves3D() && descendantDrawsContent)
255 return true;
256
257 // If the layer clips its descendants but it is not axis-aligned with respect to its parent.
258 if (layerClipsSubtree(layer) && !axisAlignedWithRespectToParent && descendantDrawsContent)
259 return true;
260
261 // If the layer has opacity != 1 and does not have a preserves-3d transform style.
262 if (layer->opacity() != 1 && !layer->preserves3D() && descendantDrawsContent)
263 return true;
264
265 return false;
266}
267
[email protected]96baf3e2012-10-22 23:09:55268WebTransformationMatrix computeScrollCompensationForThisLayer(LayerImpl* scrollingLayer, const WebTransformationMatrix& parentMatrix)
[email protected]94f206c12012-08-25 00:09:14269{
270 // For every layer that has non-zero scrollDelta, we have to compute a transform that can undo the
271 // scrollDelta translation. In particular, we want this matrix to premultiply a fixed-position layer's
272 // parentMatrix, so we design this transform in three steps as follows. The steps described here apply
273 // from right-to-left, so Step 1 would be the right-most matrix:
274 //
275 // Step 1. transform from target surface space to the exact space where scrollDelta is actually applied.
276 // -- this is inverse of the matrix in step 3
277 // Step 2. undo the scrollDelta
278 // -- this is just a translation by scrollDelta.
279 // Step 3. transform back to target surface space.
280 // -- this transform is the "partialLayerOriginTransform" = (parentMatrix * scale(layer->pageScaleDelta()));
281 //
282 // These steps create a matrix that both start and end in targetSurfaceSpace. So this matrix can
283 // pre-multiply any fixed-position layer's drawTransform to undo the scrollDeltas -- as long as
284 // that fixed position layer is fixed onto the same renderTarget as this scrollingLayer.
285 //
286
287 WebTransformationMatrix partialLayerOriginTransform = parentMatrix;
[email protected]1c0c9bc2012-10-08 22:41:48288 partialLayerOriginTransform.multiply(scrollingLayer->implTransform());
[email protected]94f206c12012-08-25 00:09:14289
290 WebTransformationMatrix scrollCompensationForThisLayer = partialLayerOriginTransform; // Step 3
[email protected]c9c1ebe2012-11-05 20:46:13291 scrollCompensationForThisLayer.translate(scrollingLayer->scrollDelta().x(), scrollingLayer->scrollDelta().y()); // Step 2
[email protected]94f206c12012-08-25 00:09:14292 scrollCompensationForThisLayer.multiply(partialLayerOriginTransform.inverse()); // Step 1
293 return scrollCompensationForThisLayer;
294}
295
[email protected]96baf3e2012-10-22 23:09:55296WebTransformationMatrix computeScrollCompensationMatrixForChildren(Layer* currentLayer, const WebTransformationMatrix& currentParentMatrix, const WebTransformationMatrix& currentScrollCompensation)
[email protected]94f206c12012-08-25 00:09:14297{
[email protected]96baf3e2012-10-22 23:09:55298 // The main thread (i.e. Layer) does not need to worry about scroll compensation.
[email protected]94f206c12012-08-25 00:09:14299 // So we can just return an identity matrix here.
300 return WebTransformationMatrix();
301}
302
[email protected]96baf3e2012-10-22 23:09:55303WebTransformationMatrix computeScrollCompensationMatrixForChildren(LayerImpl* layer, const WebTransformationMatrix& parentMatrix, const WebTransformationMatrix& currentScrollCompensationMatrix)
[email protected]94f206c12012-08-25 00:09:14304{
305 // "Total scroll compensation" is the transform needed to cancel out all scrollDelta translations that
306 // occurred since the nearest container layer, even if there are renderSurfaces in-between.
307 //
308 // There are some edge cases to be aware of, that are not explicit in the code:
309 // - A layer that is both a fixed-position and container should not be its own container, instead, that means
310 // it is fixed to an ancestor, and is a container for any fixed-position descendants.
311 // - A layer that is a fixed-position container and has a renderSurface should behave the same as a container
312 // without a renderSurface, the renderSurface is irrelevant in that case.
[email protected]ecc12622012-10-30 20:45:42313 // - A layer that does not have an explicit container is simply fixed to the viewport.
314 // (i.e. the root renderSurface.)
[email protected]94f206c12012-08-25 00:09:14315 // - If the fixed-position layer has its own renderSurface, then the renderSurface is
316 // the one who gets fixed.
317 //
318 // This function needs to be called AFTER layers create their own renderSurfaces.
319 //
320
321 // 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:13322 if (!layer->isContainerForFixedPositionLayers() && layer->scrollDelta().IsZero() && !layer->renderSurface())
[email protected]94f206c12012-08-25 00:09:14323 return currentScrollCompensationMatrix;
324
325 // Start as identity matrix.
326 WebTransformationMatrix nextScrollCompensationMatrix;
327
328 // If this layer is not a container, then it inherits the existing scroll compensations.
329 if (!layer->isContainerForFixedPositionLayers())
330 nextScrollCompensationMatrix = currentScrollCompensationMatrix;
331
332 // If the current layer has a non-zero scrollDelta, then we should compute its local scrollCompensation
333 // and accumulate it to the nextScrollCompensationMatrix.
[email protected]c9c1ebe2012-11-05 20:46:13334 if (!layer->scrollDelta().IsZero()) {
[email protected]94f206c12012-08-25 00:09:14335 WebTransformationMatrix scrollCompensationForThisLayer = computeScrollCompensationForThisLayer(layer, parentMatrix);
336 nextScrollCompensationMatrix.multiply(scrollCompensationForThisLayer);
337 }
338
339 // If the layer created its own renderSurface, we have to adjust nextScrollCompensationMatrix.
340 // The adjustment allows us to continue using the scrollCompensation on the next surface.
341 // Step 1 (right-most in the math): transform from the new surface to the original ancestor surface
342 // Step 2: apply the scroll compensation
343 // Step 3: transform back to the new surface.
344 if (layer->renderSurface() && !nextScrollCompensationMatrix.isIdentity())
345 nextScrollCompensationMatrix = layer->renderSurface()->drawTransform().inverse() * nextScrollCompensationMatrix * layer->renderSurface()->drawTransform();
346
347 return nextScrollCompensationMatrix;
348}
349
[email protected]518ee582012-10-24 18:29:44350// There is no contentsScale on impl thread.
[email protected]6a9cff92012-11-08 11:53:26351static inline void updateLayerContentsScale(LayerImpl*, const WebTransformationMatrix&, float, float, bool) { }
[email protected]518ee582012-10-24 18:29:44352
[email protected]6a9cff92012-11-08 11:53:26353static inline void updateLayerContentsScale(Layer* layer, const WebTransformationMatrix& combinedTransform, float deviceScaleFactor, float pageScaleFactor, bool animatingTransformToScreen)
[email protected]518ee582012-10-24 18:29:44354{
355 float rasterScale = layer->rasterScale();
356 if (!rasterScale) {
357 rasterScale = 1;
358
[email protected]6a9cff92012-11-08 11:53:26359 if (!animatingTransformToScreen && layer->automaticallyComputeRasterScale()) {
[email protected]aad0a0072012-11-01 18:15:58360 gfx::Vector2dF transformScale = MathUtil::computeTransform2dScaleComponents(combinedTransform);
[email protected]518ee582012-10-24 18:29:44361 float combinedScale = std::max(transformScale.x(), transformScale.y());
362 rasterScale = combinedScale / deviceScaleFactor;
363 if (!layer->boundsContainPageScale())
364 rasterScale /= pageScaleFactor;
[email protected]11ec92972012-11-10 03:06:21365 // Prevent scale factors below 1 from being used or saved.
366 if (rasterScale < 1)
367 rasterScale = 1;
368 else
369 layer->setRasterScale(rasterScale);
[email protected]518ee582012-10-24 18:29:44370 }
371 }
372
373 float contentsScale = rasterScale * deviceScaleFactor;
374 if (!layer->boundsContainPageScale())
375 contentsScale *= pageScaleFactor;
376 layer->setContentsScale(contentsScale);
377
378 Layer* maskLayer = layer->maskLayer();
379 if (maskLayer)
380 maskLayer->setContentsScale(contentsScale);
381
382 Layer* replicaMaskLayer = layer->replicaLayer() ? layer->replicaLayer()->maskLayer() : 0;
383 if (replicaMaskLayer)
384 replicaMaskLayer->setContentsScale(contentsScale);
385}
386
[email protected]94f206c12012-08-25 00:09:14387// Recursively walks the layer tree starting at the given node and computes all the
388// necessary transformations, clipRects, render surfaces, etc.
389template<typename LayerType, typename LayerList, typename RenderSurfaceType, typename LayerSorter>
[email protected]ecc12622012-10-30 20:45:42390static void calculateDrawTransformsInternal(LayerType* layer, const WebTransformationMatrix& parentMatrix,
[email protected]94f206c12012-08-25 00:09:14391 const WebTransformationMatrix& fullHierarchyMatrix, const WebTransformationMatrix& currentScrollCompensationMatrix,
[email protected]aad0a0072012-11-01 18:15:58392 const gfx::Rect& clipRectFromAncestor, bool ancestorClipsSubtree,
[email protected]94f206c12012-08-25 00:09:14393 RenderSurfaceType* nearestAncestorThatMovesPixels, LayerList& renderSurfaceLayerList, LayerList& layerList,
[email protected]aad0a0072012-11-01 18:15:58394 LayerSorter* layerSorter, int maxTextureSize, float deviceScaleFactor, float pageScaleFactor, gfx::Rect& drawableContentRectOfSubtree)
[email protected]94f206c12012-08-25 00:09:14395{
396 // This function computes the new matrix transformations recursively for this
397 // layer and all its descendants. It also computes the appropriate render surfaces.
398 // Some important points to remember:
399 //
400 // 0. Here, transforms are notated in Matrix x Vector order, and in words we describe what
401 // the transform does from left to right.
402 //
403 // 1. In our terminology, the "layer origin" refers to the top-left corner of a layer, and the
404 // positive Y-axis points downwards. This interpretation is valid because the orthographic
405 // projection applied at draw time flips the Y axis appropriately.
406 //
[email protected]aad0a0072012-11-01 18:15:58407 // 2. The anchor point, when given as a PointF object, is specified in "unit layer space",
[email protected]94f206c12012-08-25 00:09:14408 // where the bounds of the layer map to [0, 1]. However, as a WebTransformationMatrix object,
[email protected]b8d3c022012-10-08 17:38:04409 // the transform to the anchor point is specified in "layer space", where the bounds
[email protected]94f206c12012-08-25 00:09:14410 // of the layer map to [bounds.width(), bounds.height()].
411 //
412 // 3. Definition of various transforms used:
413 // M[parent] is the parent matrix, with respect to the nearest render surface, passed down recursively.
414 // M[root] is the full hierarchy, with respect to the root, passed down recursively.
415 // Tr[origin] is the translation matrix from the parent's origin to this layer's origin.
416 // Tr[origin2anchor] is the translation from the layer's origin to its anchor point
417 // Tr[origin2center] is the translation from the layer's origin to its center
418 // M[layer] is the layer's matrix (applied at the anchor point)
419 // M[sublayer] is the layer's sublayer transform (applied at the layer's center)
[email protected]b8d3c022012-10-08 17:38:04420 // S[layer2content] is the ratio of a layer's contentBounds() to its bounds().
[email protected]94f206c12012-08-25 00:09:14421 //
422 // Some composite transforms can help in understanding the sequence of transforms:
423 // compositeLayerTransform = Tr[origin2anchor] * M[layer] * Tr[origin2anchor].inverse()
424 // compositeSublayerTransform = Tr[origin2center] * M[sublayer] * Tr[origin2center].inverse()
425 //
426 // In words, the layer transform is applied about the anchor point, and the sublayer transform is
427 // applied about the center of the layer.
428 //
429 // 4. When a layer (or render surface) is drawn, it is drawn into a "target render surface". Therefore the draw
430 // transform does not necessarily transform from screen space to local layer space. Instead, the draw transform
431 // is the transform between the "target render surface space" and local layer space. Note that render surfaces,
432 // except for the root, also draw themselves into a different target render surface, and so their draw
433 // transform and origin transforms are also described with respect to the target.
434 //
435 // Using these definitions, then:
436 //
437 // The draw transform for the layer is:
[email protected]b8d3c022012-10-08 17:38:04438 // M[draw] = M[parent] * Tr[origin] * compositeLayerTransform * S[layer2content]
439 // = M[parent] * Tr[layer->position() + anchor] * M[layer] * Tr[anchor2origin] * S[layer2content]
[email protected]94f206c12012-08-25 00:09:14440 //
441 // Interpreting the math left-to-right, this transforms from the layer's render surface to the origin of the layer in content space.
442 //
443 // The screen space transform is:
[email protected]b8d3c022012-10-08 17:38:04444 // M[screenspace] = M[root] * Tr[origin] * compositeLayerTransform * S[layer2content]
445 // = M[root] * Tr[layer->position() + anchor] * M[layer] * Tr[anchor2origin] * S[layer2content]
[email protected]94f206c12012-08-25 00:09:14446 //
[email protected]655fa7f92012-11-15 00:14:33447 // 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:14448 //
449 // The transform hierarchy that is passed on to children (i.e. the child's parentMatrix) is:
450 // M[parent]_for_child = M[parent] * Tr[origin] * compositeLayerTransform * compositeSublayerTransform
[email protected]b8d3c022012-10-08 17:38:04451 // = M[parent] * Tr[layer->position() + anchor] * M[layer] * Tr[anchor2origin] * compositeSublayerTransform
[email protected]94f206c12012-08-25 00:09:14452 //
453 // and a similar matrix for the full hierarchy with respect to the root.
454 //
455 // Finally, note that the final matrix used by the shader for the layer is P * M[draw] * S . This final product
456 // is computed in drawTexturedQuad(), where:
457 // P is the projection matrix
[email protected]b8d3c022012-10-08 17:38:04458 // S is the scale adjustment (to scale up a canonical quad to the layer's size)
[email protected]94f206c12012-08-25 00:09:14459 //
460 // When a render surface has a replica layer, that layer's transform is used to draw a second copy of the surface.
461 // Transforms named here are relative to the surface, unless they specify they are relative to the replica layer.
462 //
463 // We will denote a scale by device scale S[deviceScale]
464 //
465 // The render surface draw transform to its target surface origin is:
466 // M[surfaceDraw] = M[owningLayer->Draw]
467 //
468 // The render surface origin transform to its the root (screen space) origin is:
469 // M[surface2root] = M[owningLayer->screenspace] * S[deviceScale].inverse()
470 //
471 // The replica draw transform to its target surface origin is:
472 // M[replicaDraw] = S[deviceScale] * M[surfaceDraw] * Tr[replica->position() + replica->anchor()] * Tr[replica] * Tr[origin2anchor].inverse() * S[contentsScale].inverse()
473 //
474 // The replica draw transform to the root (screen space) origin is:
475 // M[replica2root] = M[surface2root] * Tr[replica->position()] * Tr[replica] * Tr[origin2anchor].inverse()
476 //
477
478 // If we early-exit anywhere in this function, the drawableContentRect of this subtree should be considered empty.
[email protected]aad0a0072012-11-01 18:15:58479 drawableContentRectOfSubtree = gfx::Rect();
[email protected]94f206c12012-08-25 00:09:14480
[email protected]ecc12622012-10-30 20:45:42481 // The root layer cannot skip calcDrawTransforms.
482 if (!isRootLayer(layer) && subtreeShouldBeSkipped(layer))
[email protected]94f206c12012-08-25 00:09:14483 return;
484
[email protected]aad0a0072012-11-01 18:15:58485 gfx::Rect clipRectForSubtree;
[email protected]94f206c12012-08-25 00:09:14486 bool subtreeShouldBeClipped = false;
487
488 float drawOpacity = layer->opacity();
489 bool drawOpacityIsAnimating = layer->opacityIsAnimating();
490 if (layer->parent() && layer->parent()->preserves3D()) {
491 drawOpacity *= layer->parent()->drawOpacity();
492 drawOpacityIsAnimating |= layer->parent()->drawOpacityIsAnimating();
493 }
494
[email protected]6a9cff92012-11-08 11:53:26495 bool animatingTransformToTarget = layer->transformIsAnimating();
496 bool animatingTransformToScreen = animatingTransformToTarget;
497 if (layer->parent()) {
498 animatingTransformToTarget |= layer->parent()->drawTransformIsAnimating();
499 animatingTransformToScreen |= layer->parent()->screenSpaceTransformIsAnimating();
500 }
501
[email protected]aad0a0072012-11-01 18:15:58502 gfx::Size bounds = layer->bounds();
503 gfx::PointF anchorPoint = layer->anchorPoint();
[email protected]c9c1ebe2012-11-05 20:46:13504 gfx::PointF position = layer->position() - layer->scrollDelta();
[email protected]94f206c12012-08-25 00:09:14505
[email protected]94f206c12012-08-25 00:09:14506 WebTransformationMatrix layerLocalTransform;
[email protected]518ee582012-10-24 18:29:44507 // LT = Tr[origin] * Tr[origin2anchor]
[email protected]94f206c12012-08-25 00:09:14508 layerLocalTransform.translate3d(position.x() + anchorPoint.x() * bounds.width(), position.y() + anchorPoint.y() * bounds.height(), layer->anchorPointZ());
[email protected]518ee582012-10-24 18:29:44509 // LT = Tr[origin] * Tr[origin2anchor] * M[layer]
[email protected]94f206c12012-08-25 00:09:14510 layerLocalTransform.multiply(layer->transform());
[email protected]518ee582012-10-24 18:29:44511 // LT = Tr[origin] * Tr[origin2anchor] * M[layer] * Tr[anchor2origin]
[email protected]b8d3c022012-10-08 17:38:04512 layerLocalTransform.translate3d(-anchorPoint.x() * bounds.width(), -anchorPoint.y() * bounds.height(), -layer->anchorPointZ());
[email protected]94f206c12012-08-25 00:09:14513
514 WebTransformationMatrix combinedTransform = parentMatrix;
515 combinedTransform.multiply(layerLocalTransform);
516
[email protected]518ee582012-10-24 18:29:44517 // The layer's contentsSize is determined from the combinedTransform, which then informs the
518 // layer's drawTransform.
[email protected]6a9cff92012-11-08 11:53:26519 updateLayerContentsScale(layer, combinedTransform, deviceScaleFactor, pageScaleFactor, animatingTransformToScreen);
[email protected]518ee582012-10-24 18:29:44520
521 // If there is a tranformation from the impl thread then it should be at the
522 // start of the combinedTransform, but we don't want it to affect the contentsScale.
523 combinedTransform = layer->implTransform() * combinedTransform;
524
[email protected]94f206c12012-08-25 00:09:14525 if (layer->fixedToContainerLayer()) {
526 // Special case: this layer is a composited fixed-position layer; we need to
527 // explicitly compensate for all ancestors' nonzero scrollDeltas to keep this layer
528 // fixed correctly.
529 combinedTransform = currentScrollCompensationMatrix * combinedTransform;
530 }
531
532 // The drawTransform that gets computed below is effectively the layer's drawTransform, unless
533 // the layer itself creates a renderSurface. In that case, the renderSurface re-parents the transforms.
534 WebTransformationMatrix drawTransform = combinedTransform;
[email protected]f89f5632012-11-14 23:34:45535 // M[draw] = M[parent] * LT * S[layer2content]
536 drawTransform.scaleNonUniform(1.0 / layer->contentsScaleX(), 1.0 / layer->contentsScaleY());
[email protected]94f206c12012-08-25 00:09:14537
538 // layerScreenSpaceTransform represents the transform between root layer's "screen space" and local content space.
539 WebTransformationMatrix layerScreenSpaceTransform = fullHierarchyMatrix;
540 if (!layer->preserves3D())
[email protected]96baf3e2012-10-22 23:09:55541 MathUtil::flattenTransformTo2d(layerScreenSpaceTransform);
[email protected]94f206c12012-08-25 00:09:14542 layerScreenSpaceTransform.multiply(drawTransform);
543 layer->setScreenSpaceTransform(layerScreenSpaceTransform);
544
[email protected]aad0a0072012-11-01 18:15:58545 gfx::RectF contentRect(gfx::PointF(), layer->contentBounds());
[email protected]94f206c12012-08-25 00:09:14546
[email protected]96baf3e2012-10-22 23:09:55547 // fullHierarchyMatrix is the matrix that transforms objects between screen space (except projection matrix) and the most recent RenderSurfaceImpl's space.
548 // nextHierarchyMatrix will only change if this layer uses a new RenderSurfaceImpl, otherwise remains the same.
[email protected]94f206c12012-08-25 00:09:14549 WebTransformationMatrix nextHierarchyMatrix = fullHierarchyMatrix;
550 WebTransformationMatrix sublayerMatrix;
551
[email protected]aad0a0072012-11-01 18:15:58552 gfx::Vector2dF renderSurfaceSublayerScale = MathUtil::computeTransform2dScaleComponents(combinedTransform);
[email protected]518ee582012-10-24 18:29:44553
[email protected]94f206c12012-08-25 00:09:14554 if (subtreeShouldRenderToSeparateSurface(layer, isScaleOrTranslation(combinedTransform))) {
555 // Check back-face visibility before continuing with this surface and its subtree
556 if (!layer->doubleSided() && transformToParentIsKnown(layer) && isSurfaceBackFaceVisible(layer, combinedTransform))
557 return;
558
559 if (!layer->renderSurface())
560 layer->createRenderSurface();
561
562 RenderSurfaceType* renderSurface = layer->renderSurface();
[email protected]7d929c02012-09-20 17:26:57563 renderSurface->clearLayerLists();
[email protected]94f206c12012-08-25 00:09:14564
[email protected]518ee582012-10-24 18:29:44565 // The owning layer's draw transform has a scale from content to layer space which we need to undo and
566 // replace with a scale from the surface's subtree into layer space.
[email protected]f89f5632012-11-14 23:34:45567 drawTransform.scaleNonUniform(layer->contentsScaleX(), layer->contentsScaleY());
[email protected]518ee582012-10-24 18:29:44568 drawTransform.scaleNonUniform(1 / renderSurfaceSublayerScale.x(), 1 / renderSurfaceSublayerScale.y());
[email protected]f3922f22012-10-12 09:20:38569 renderSurface->setDrawTransform(drawTransform);
[email protected]518ee582012-10-24 18:29:44570
571 // The origin of the new surface is the upper left corner of the layer.
[email protected]94f206c12012-08-25 00:09:14572 WebTransformationMatrix layerDrawTransform;
[email protected]518ee582012-10-24 18:29:44573 layerDrawTransform.scaleNonUniform(renderSurfaceSublayerScale.x(), renderSurfaceSublayerScale.y());
[email protected]f89f5632012-11-14 23:34:45574 layerDrawTransform.scaleNonUniform(1.0 / layer->contentsScaleX(), 1.0 / layer->contentsScaleY());
[email protected]94f206c12012-08-25 00:09:14575 layer->setDrawTransform(layerDrawTransform);
576
[email protected]518ee582012-10-24 18:29:44577 // Inside the surface's subtree, we scale everything to the owning layer's scale.
[email protected]94f206c12012-08-25 00:09:14578 // The sublayer matrix transforms centered layer rects into target
579 // surface content space.
580 sublayerMatrix.makeIdentity();
[email protected]518ee582012-10-24 18:29:44581 sublayerMatrix.scaleNonUniform(renderSurfaceSublayerScale.x(), renderSurfaceSublayerScale.y());
[email protected]94f206c12012-08-25 00:09:14582
583 // The opacity value is moved from the layer to its surface, so that the entire subtree properly inherits opacity.
584 renderSurface->setDrawOpacity(drawOpacity);
585 renderSurface->setDrawOpacityIsAnimating(drawOpacityIsAnimating);
586 layer->setDrawOpacity(1);
587 layer->setDrawOpacityIsAnimating(false);
588
589 renderSurface->setTargetSurfaceTransformsAreAnimating(animatingTransformToTarget);
590 renderSurface->setScreenSpaceTransformsAreAnimating(animatingTransformToScreen);
591 animatingTransformToTarget = false;
592 layer->setDrawTransformIsAnimating(animatingTransformToTarget);
593 layer->setScreenSpaceTransformIsAnimating(animatingTransformToScreen);
594
595 // Update the aggregate hierarchy matrix to include the transform of the
[email protected]96baf3e2012-10-22 23:09:55596 // newly created RenderSurfaceImpl.
[email protected]94f206c12012-08-25 00:09:14597 nextHierarchyMatrix.multiply(renderSurface->drawTransform());
598
599 // The new renderSurface here will correctly clip the entire subtree. So, we do
600 // not need to continue propagating the clipping state further down the tree. This
601 // way, we can avoid transforming clipRects from ancestor target surface space to
602 // current target surface space that could cause more w < 0 headaches.
603 subtreeShouldBeClipped = false;
604
[email protected]9bab0bb2012-10-08 19:11:58605 if (layer->maskLayer()) {
[email protected]94f206c12012-08-25 00:09:14606 layer->maskLayer()->setRenderTarget(layer);
[email protected]aad0a0072012-11-01 18:15:58607 layer->maskLayer()->setVisibleContentRect(gfx::Rect(gfx::Point(), layer->contentBounds()));
[email protected]9bab0bb2012-10-08 19:11:58608 }
[email protected]94f206c12012-08-25 00:09:14609
[email protected]9bab0bb2012-10-08 19:11:58610 if (layer->replicaLayer() && layer->replicaLayer()->maskLayer()) {
[email protected]94f206c12012-08-25 00:09:14611 layer->replicaLayer()->maskLayer()->setRenderTarget(layer);
[email protected]aad0a0072012-11-01 18:15:58612 layer->replicaLayer()->maskLayer()->setVisibleContentRect(gfx::Rect(gfx::Point(), layer->contentBounds()));
[email protected]9bab0bb2012-10-08 19:11:58613 }
[email protected]94f206c12012-08-25 00:09:14614
[email protected]4000abf2012-10-23 04:45:45615 // FIXME: make this smarter for the SkImageFilter case (check for
616 // pixel-moving filters)
617 if (layer->filters().hasFilterThatMovesPixels() || layer->filter())
[email protected]94f206c12012-08-25 00:09:14618 nearestAncestorThatMovesPixels = renderSurface;
619
[email protected]9bab0bb2012-10-08 19:11:58620 // 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:01621 renderSurface->setIsClipped(ancestorClipsSubtree);
[email protected]9bab0bb2012-10-08 19:11:58622 if (ancestorClipsSubtree)
623 renderSurface->setClipRect(clipRectFromAncestor);
624 else
[email protected]aad0a0072012-11-01 18:15:58625 renderSurface->setClipRect(gfx::Rect());
[email protected]9bab0bb2012-10-08 19:11:58626
[email protected]94f206c12012-08-25 00:09:14627 renderSurface->setNearestAncestorThatMovesPixels(nearestAncestorThatMovesPixels);
628
[email protected]d58499a2012-10-09 22:27:47629 renderSurfaceLayerList.push_back(layer);
[email protected]94f206c12012-08-25 00:09:14630 } else {
[email protected]ecc12622012-10-30 20:45:42631 DCHECK(layer->parent());
632
[email protected]94f206c12012-08-25 00:09:14633 layer->setDrawTransform(drawTransform);
634 layer->setDrawTransformIsAnimating(animatingTransformToTarget);
635 layer->setScreenSpaceTransformIsAnimating(animatingTransformToScreen);
636 sublayerMatrix = combinedTransform;
637
638 layer->setDrawOpacity(drawOpacity);
639 layer->setDrawOpacityIsAnimating(drawOpacityIsAnimating);
640
[email protected]ecc12622012-10-30 20:45:42641 layer->clearRenderSurface();
[email protected]94f206c12012-08-25 00:09:14642
[email protected]ecc12622012-10-30 20:45:42643 // Layers without renderSurfaces directly inherit the ancestor's clip status.
644 subtreeShouldBeClipped = ancestorClipsSubtree;
645 if (ancestorClipsSubtree)
646 clipRectForSubtree = clipRectFromAncestor;
[email protected]94f206c12012-08-25 00:09:14647
[email protected]ecc12622012-10-30 20:45:42648 // Layers that are not their own renderTarget will render into the target of their nearest ancestor.
649 layer->setRenderTarget(layer->parent()->renderTarget());
[email protected]94f206c12012-08-25 00:09:14650 }
651
[email protected]aad0a0072012-11-01 18:15:58652 gfx::Rect rectInTargetSpace = ToEnclosingRect(MathUtil::mapClippedRect(layer->drawTransform(), contentRect));
[email protected]94f206c12012-08-25 00:09:14653
654 if (layerClipsSubtree(layer)) {
655 subtreeShouldBeClipped = true;
656 if (ancestorClipsSubtree && !layer->renderSurface()) {
657 clipRectForSubtree = clipRectFromAncestor;
[email protected]aad0a0072012-11-01 18:15:58658 clipRectForSubtree.Intersect(rectInTargetSpace);
[email protected]94f206c12012-08-25 00:09:14659 } else
660 clipRectForSubtree = rectInTargetSpace;
661 }
662
663 // Flatten to 2D if the layer doesn't preserve 3D.
664 if (!layer->preserves3D())
[email protected]96baf3e2012-10-22 23:09:55665 MathUtil::flattenTransformTo2d(sublayerMatrix);
[email protected]94f206c12012-08-25 00:09:14666
667 // Apply the sublayer transform at the center of the layer.
[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 sublayerMatrix.multiply(layer->sublayerTransform());
[email protected]b8d3c022012-10-08 17:38:04670 sublayerMatrix.translate(-0.5 * bounds.width(), -0.5 * bounds.height());
[email protected]94f206c12012-08-25 00:09:14671
672 LayerList& descendants = (layer->renderSurface() ? layer->renderSurface()->layerList() : layerList);
673
674 // Any layers that are appended after this point are in the layer's subtree and should be included in the sorting process.
675 unsigned sortingStartIndex = descendants.size();
676
677 if (!layerShouldBeSkipped(layer))
[email protected]d58499a2012-10-09 22:27:47678 descendants.push_back(layer);
[email protected]94f206c12012-08-25 00:09:14679
680 WebTransformationMatrix nextScrollCompensationMatrix = computeScrollCompensationMatrixForChildren(layer, parentMatrix, currentScrollCompensationMatrix);;
681
[email protected]aad0a0072012-11-01 18:15:58682 gfx::Rect accumulatedDrawableContentRectOfChildren;
[email protected]94f206c12012-08-25 00:09:14683 for (size_t i = 0; i < layer->children().size(); ++i) {
[email protected]96baf3e2012-10-22 23:09:55684 LayerType* child = LayerTreeHostCommon::getChildAsRawPtr(layer->children(), i);
[email protected]aad0a0072012-11-01 18:15:58685 gfx::Rect drawableContentRectOfChildSubtree;
[email protected]ecc12622012-10-30 20:45:42686 calculateDrawTransformsInternal<LayerType, LayerList, RenderSurfaceType, LayerSorter>(child, sublayerMatrix, nextHierarchyMatrix, nextScrollCompensationMatrix,
[email protected]94f206c12012-08-25 00:09:14687 clipRectForSubtree, subtreeShouldBeClipped, nearestAncestorThatMovesPixels,
[email protected]518ee582012-10-24 18:29:44688 renderSurfaceLayerList, descendants, layerSorter, maxTextureSize, deviceScaleFactor, pageScaleFactor, drawableContentRectOfChildSubtree);
[email protected]aad0a0072012-11-01 18:15:58689 if (!drawableContentRectOfChildSubtree.IsEmpty()) {
690 accumulatedDrawableContentRectOfChildren.Union(drawableContentRectOfChildSubtree);
[email protected]94f206c12012-08-25 00:09:14691 if (child->renderSurface())
[email protected]d58499a2012-10-09 22:27:47692 descendants.push_back(child);
[email protected]94f206c12012-08-25 00:09:14693 }
694 }
695
696 // Compute the total drawableContentRect for this subtree (the rect is in targetSurface space)
[email protected]aad0a0072012-11-01 18:15:58697 gfx::Rect localDrawableContentRectOfSubtree = accumulatedDrawableContentRectOfChildren;
[email protected]94f206c12012-08-25 00:09:14698 if (layer->drawsContent())
[email protected]aad0a0072012-11-01 18:15:58699 localDrawableContentRectOfSubtree.Union(rectInTargetSpace);
[email protected]94f206c12012-08-25 00:09:14700 if (subtreeShouldBeClipped)
[email protected]aad0a0072012-11-01 18:15:58701 localDrawableContentRectOfSubtree.Intersect(clipRectForSubtree);
[email protected]94f206c12012-08-25 00:09:14702
703 // Compute the layer's drawable content rect (the rect is in targetSurface space)
[email protected]aad0a0072012-11-01 18:15:58704 gfx::Rect drawableContentRectOfLayer = rectInTargetSpace;
[email protected]94f206c12012-08-25 00:09:14705 if (subtreeShouldBeClipped)
[email protected]aad0a0072012-11-01 18:15:58706 drawableContentRectOfLayer.Intersect(clipRectForSubtree);
[email protected]94f206c12012-08-25 00:09:14707 layer->setDrawableContentRect(drawableContentRectOfLayer);
708
[email protected]dc462d782012-11-21 21:43:01709 // Tell the layer the rect that is clipped by. In theory we could use a
710 // tighter clipRect here (drawableContentRect), but that actually does not
711 // reduce how much would be drawn, and instead it would create unnecessary
712 // changes to scissor state affecting GPU performance.
713 layer->setIsClipped(subtreeShouldBeClipped);
714 if (subtreeShouldBeClipped)
715 layer->setClipRect(clipRectForSubtree);
716 else {
717 // Initialize the clipRect to a safe value that will not clip the
718 // layer, just in case clipping is still accidentally used.
719 layer->setClipRect(rectInTargetSpace);
720 }
721
[email protected]9bab0bb2012-10-08 19:11:58722 // Compute the layer's visible content rect (the rect is in content space)
[email protected]aad0a0072012-11-01 18:15:58723 gfx::Rect visibleContentRectOfLayer = calculateVisibleContentRect(layer);
[email protected]9bab0bb2012-10-08 19:11:58724 layer->setVisibleContentRect(visibleContentRectOfLayer);
725
[email protected]94f206c12012-08-25 00:09:14726 // Compute the remaining properties for the render surface, if the layer has one.
[email protected]ecc12622012-10-30 20:45:42727 if (isRootLayer(layer)) {
728 // The root layer's surface's contentRect is always the entire viewport.
729 DCHECK(layer->renderSurface());
730 layer->renderSurface()->setContentRect(clipRectFromAncestor);
731 } else if (layer->renderSurface() && !isRootLayer(layer)) {
[email protected]94f206c12012-08-25 00:09:14732 RenderSurfaceType* renderSurface = layer->renderSurface();
[email protected]aad0a0072012-11-01 18:15:58733 gfx::Rect clippedContentRect = localDrawableContentRectOfSubtree;
[email protected]94f206c12012-08-25 00:09:14734
[email protected]94f206c12012-08-25 00:09:14735 // Don't clip if the layer is reflected as the reflection shouldn't be
736 // clipped. If the layer is animating, then the surface's transform to
737 // its target is not known on the main thread, and we should not use it
738 // to clip.
739 if (!layer->replicaLayer() && transformToParentIsKnown(layer)) {
740 // 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:58741 if (ancestorClipsSubtree && !clippedContentRect.IsEmpty()) {
742 gfx::Rect surfaceClipRect = LayerTreeHostCommon::calculateVisibleRect(renderSurface->clipRect(), clippedContentRect, renderSurface->drawTransform());
743 clippedContentRect.Intersect(surfaceClipRect);
[email protected]94f206c12012-08-25 00:09:14744 }
745 }
746
[email protected]96baf3e2012-10-22 23:09:55747 // The RenderSurfaceImpl backing texture cannot exceed the maximum supported
[email protected]94f206c12012-08-25 00:09:14748 // texture size.
[email protected]aad0a0072012-11-01 18:15:58749 clippedContentRect.set_width(std::min(clippedContentRect.width(), maxTextureSize));
750 clippedContentRect.set_height(std::min(clippedContentRect.height(), maxTextureSize));
[email protected]94f206c12012-08-25 00:09:14751
[email protected]aad0a0072012-11-01 18:15:58752 if (clippedContentRect.IsEmpty())
[email protected]7d929c02012-09-20 17:26:57753 renderSurface->clearLayerLists();
[email protected]94f206c12012-08-25 00:09:14754
755 renderSurface->setContentRect(clippedContentRect);
[email protected]518ee582012-10-24 18:29:44756
757 // The owning layer's screenSpaceTransform has a scale from content to layer space which we need to undo and
758 // replace with a scale from the surface's subtree into layer space.
759 WebTransformationMatrix screenSpaceTransform = layer->screenSpaceTransform();
[email protected]f89f5632012-11-14 23:34:45760 screenSpaceTransform.scaleNonUniform(layer->contentsScaleX(), layer->contentsScaleY());
[email protected]518ee582012-10-24 18:29:44761 screenSpaceTransform.scaleNonUniform(1 / renderSurfaceSublayerScale.x(), 1 / renderSurfaceSublayerScale.y());
762 renderSurface->setScreenSpaceTransform(screenSpaceTransform);
[email protected]94f206c12012-08-25 00:09:14763
764 if (layer->replicaLayer()) {
765 WebTransformationMatrix surfaceOriginToReplicaOriginTransform;
[email protected]518ee582012-10-24 18:29:44766 surfaceOriginToReplicaOriginTransform.scaleNonUniform(renderSurfaceSublayerScale.x(), renderSurfaceSublayerScale.y());
[email protected]94f206c12012-08-25 00:09:14767 surfaceOriginToReplicaOriginTransform.translate(layer->replicaLayer()->position().x() + layer->replicaLayer()->anchorPoint().x() * bounds.width(),
768 layer->replicaLayer()->position().y() + layer->replicaLayer()->anchorPoint().y() * bounds.height());
769 surfaceOriginToReplicaOriginTransform.multiply(layer->replicaLayer()->transform());
770 surfaceOriginToReplicaOriginTransform.translate(-layer->replicaLayer()->anchorPoint().x() * bounds.width(), -layer->replicaLayer()->anchorPoint().y() * bounds.height());
[email protected]518ee582012-10-24 18:29:44771 surfaceOriginToReplicaOriginTransform.scaleNonUniform(1 / renderSurfaceSublayerScale.x(), 1 / renderSurfaceSublayerScale.y());
[email protected]94f206c12012-08-25 00:09:14772
773 // Compute the replica's "originTransform" that maps from the replica's origin space to the target surface origin space.
774 WebTransformationMatrix replicaOriginTransform = layer->renderSurface()->drawTransform() * surfaceOriginToReplicaOriginTransform;
775 renderSurface->setReplicaDrawTransform(replicaOriginTransform);
776
777 // Compute the replica's "screenSpaceTransform" that maps from the replica's origin space to the screen's origin space.
778 WebTransformationMatrix replicaScreenSpaceTransform = layer->renderSurface()->screenSpaceTransform() * surfaceOriginToReplicaOriginTransform;
779 renderSurface->setReplicaScreenSpaceTransform(replicaScreenSpaceTransform);
780 }
781
782 // If a render surface has no layer list, then it and none of its children needed to get drawn.
783 if (!layer->renderSurface()->layerList().size()) {
784 // FIXME: Originally we asserted that this layer was already at the end of the
785 // list, and only needed to remove that layer. For now, we remove the
786 // entire subtree of surfaces to fix a crash bug. The root cause is
787 // https://bugs.webkit.org/show_bug.cgi?id=74147 and we should be able
788 // to put the original assert after fixing that.
[email protected]d58499a2012-10-09 22:27:47789 while (renderSurfaceLayerList.back() != layer) {
790 renderSurfaceLayerList.back()->clearRenderSurface();
791 renderSurfaceLayerList.pop_back();
[email protected]94f206c12012-08-25 00:09:14792 }
[email protected]1d993172012-10-18 18:15:04793 DCHECK(renderSurfaceLayerList.back() == layer);
[email protected]d58499a2012-10-09 22:27:47794 renderSurfaceLayerList.pop_back();
[email protected]94f206c12012-08-25 00:09:14795 layer->clearRenderSurface();
796 return;
797 }
798 }
799
800 // If neither this layer nor any of its children were added, early out.
801 if (sortingStartIndex == descendants.size())
802 return;
803
804 // If preserves-3d then sort all the descendants in 3D so that they can be
805 // drawn from back to front. If the preserves-3d property is also set on the parent then
806 // skip the sorting as the parent will sort all the descendants anyway.
807 if (descendants.size() && layer->preserves3D() && (!layer->parent() || !layer->parent()->preserves3D()))
[email protected]d58499a2012-10-09 22:27:47808 sortLayers(descendants.begin() + sortingStartIndex, descendants.end(), layerSorter);
[email protected]94f206c12012-08-25 00:09:14809
810 if (layer->renderSurface())
[email protected]aad0a0072012-11-01 18:15:58811 drawableContentRectOfSubtree = gfx::ToEnclosingRect(layer->renderSurface()->drawableContentRect());
[email protected]94f206c12012-08-25 00:09:14812 else
813 drawableContentRectOfSubtree = localDrawableContentRectOfSubtree;
814
[email protected]7d929c02012-09-20 17:26:57815 if (layer->hasContributingDelegatedRenderPasses())
816 layer->renderTarget()->renderSurface()->addContributingDelegatedRenderPassLayer(layer);
[email protected]94f206c12012-08-25 00:09:14817}
818
[email protected]aad0a0072012-11-01 18:15:58819void 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:14820{
[email protected]aad0a0072012-11-01 18:15:58821 gfx::Rect totalDrawableContentRect;
[email protected]94f206c12012-08-25 00:09:14822 WebTransformationMatrix identityMatrix;
823 WebTransformationMatrix deviceScaleTransform;
824 deviceScaleTransform.scale(deviceScaleFactor);
[email protected]ecc12622012-10-30 20:45:42825 std::vector<scoped_refptr<Layer> > dummyLayerList;
[email protected]94f206c12012-08-25 00:09:14826
[email protected]ecc12622012-10-30 20:45:42827 // The root layer's renderSurface should receive the deviceViewport as the initial clipRect.
828 bool subtreeShouldBeClipped = true;
[email protected]aad0a0072012-11-01 18:15:58829 gfx::Rect deviceViewportRect(gfx::Point(), deviceViewportSize);
[email protected]ecc12622012-10-30 20:45:42830
831 // This function should have received a root layer.
832 DCHECK(isRootLayer(rootLayer));
[email protected]94f206c12012-08-25 00:09:14833
[email protected]518ee582012-10-24 18:29:44834 cc::calculateDrawTransformsInternal<Layer, std::vector<scoped_refptr<Layer> >, RenderSurface, void>(
[email protected]ecc12622012-10-30 20:45:42835 rootLayer, deviceScaleTransform, identityMatrix, identityMatrix,
836 deviceViewportRect, subtreeShouldBeClipped, 0, renderSurfaceLayerList,
837 dummyLayerList, 0, maxTextureSize,
[email protected]518ee582012-10-24 18:29:44838 deviceScaleFactor, pageScaleFactor, totalDrawableContentRect);
[email protected]ecc12622012-10-30 20:45:42839
840 // The dummy layer list should not have been used.
841 DCHECK(dummyLayerList.size() == 0);
842 // A root layer renderSurface should always exist after calculateDrawTransforms.
843 DCHECK(rootLayer->renderSurface());
[email protected]94f206c12012-08-25 00:09:14844}
845
[email protected]aad0a0072012-11-01 18:15:58846void 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:14847{
[email protected]aad0a0072012-11-01 18:15:58848 gfx::Rect totalDrawableContentRect;
[email protected]94f206c12012-08-25 00:09:14849 WebTransformationMatrix identityMatrix;
850 WebTransformationMatrix deviceScaleTransform;
851 deviceScaleTransform.scale(deviceScaleFactor);
[email protected]ecc12622012-10-30 20:45:42852 std::vector<LayerImpl*> dummyLayerList;
[email protected]94f206c12012-08-25 00:09:14853
[email protected]ecc12622012-10-30 20:45:42854 // The root layer's renderSurface should receive the deviceViewport as the initial clipRect.
855 bool subtreeShouldBeClipped = true;
[email protected]aad0a0072012-11-01 18:15:58856 gfx::Rect deviceViewportRect(gfx::Point(), deviceViewportSize);
[email protected]ecc12622012-10-30 20:45:42857
858 // This function should have received a root layer.
859 DCHECK(isRootLayer(rootLayer));
[email protected]94f206c12012-08-25 00:09:14860
[email protected]518ee582012-10-24 18:29:44861 cc::calculateDrawTransformsInternal<LayerImpl, std::vector<LayerImpl*>, RenderSurfaceImpl, LayerSorter>(
[email protected]ecc12622012-10-30 20:45:42862 rootLayer, deviceScaleTransform, identityMatrix, identityMatrix,
863 deviceViewportRect, subtreeShouldBeClipped, 0, renderSurfaceLayerList,
864 dummyLayerList, layerSorter, maxTextureSize,
[email protected]518ee582012-10-24 18:29:44865 deviceScaleFactor, pageScaleFactor, totalDrawableContentRect);
[email protected]ecc12622012-10-30 20:45:42866
867 // The dummy layer list should not have been used.
868 DCHECK(dummyLayerList.size() == 0);
869 // A root layer renderSurface should always exist after calculateDrawTransforms.
870 DCHECK(rootLayer->renderSurface());
[email protected]94f206c12012-08-25 00:09:14871}
872
[email protected]d455d552012-11-02 00:19:06873static bool pointHitsRect(const gfx::PointF& screenSpacePoint, const WebTransformationMatrix& localSpaceToScreenSpaceTransform, gfx::RectF localSpaceRect)
[email protected]94f206c12012-08-25 00:09:14874{
875 // If the transform is not invertible, then assume that this point doesn't hit this rect.
876 if (!localSpaceToScreenSpaceTransform.isInvertible())
877 return false;
878
879 // Transform the hit test point from screen space to the local space of the given rect.
880 bool clipped = false;
[email protected]d455d552012-11-02 00:19:06881 gfx::PointF hitTestPointInLocalSpace = MathUtil::projectPoint(localSpaceToScreenSpaceTransform.inverse(), screenSpacePoint, clipped);
[email protected]94f206c12012-08-25 00:09:14882
883 // If projectPoint could not project to a valid value, then we assume that this point doesn't hit this rect.
884 if (clipped)
885 return false;
886
[email protected]aad0a0072012-11-01 18:15:58887 return localSpaceRect.Contains(hitTestPointInLocalSpace);
[email protected]94f206c12012-08-25 00:09:14888}
889
[email protected]2f1acc262012-11-16 21:42:22890static bool pointHitsRegion(gfx::PointF screenSpacePoint, const WebTransformationMatrix& screenSpaceTransform, const Region& layerSpaceRegion, float layerContentScaleX, float layerContentScaleY)
891{
892 // If the transform is not invertible, then assume that this point doesn't hit this region.
893 if (!screenSpaceTransform.isInvertible())
894 return false;
895
896 // Transform the hit test point from screen space to the local space of the given region.
897 bool clipped = false;
898 gfx::PointF hitTestPointInContentSpace = MathUtil::projectPoint(screenSpaceTransform.inverse(), screenSpacePoint, clipped);
899 gfx::PointF hitTestPointInLayerSpace = gfx::ScalePoint(hitTestPointInContentSpace, 1 / layerContentScaleX, 1 / layerContentScaleY);
900
901 // If projectPoint could not project to a valid value, then we assume that this point doesn't hit this region.
902 if (clipped)
903 return false;
904
905 return layerSpaceRegion.Contains(gfx::ToRoundedPoint(hitTestPointInLayerSpace));
906}
907
[email protected]d455d552012-11-02 00:19:06908static bool pointIsClippedBySurfaceOrClipRect(const gfx::PointF& screenSpacePoint, LayerImpl* layer)
[email protected]94f206c12012-08-25 00:09:14909{
[email protected]96baf3e2012-10-22 23:09:55910 LayerImpl* currentLayer = layer;
[email protected]94f206c12012-08-25 00:09:14911
912 // Walk up the layer tree and hit-test any renderSurfaces and any layer clipRects that are active.
913 while (currentLayer) {
[email protected]01527732012-10-19 18:16:19914 if (currentLayer->renderSurface() && !pointHitsRect(screenSpacePoint, currentLayer->renderSurface()->screenSpaceTransform(), currentLayer->renderSurface()->contentRect()))
[email protected]94f206c12012-08-25 00:09:14915 return true;
916
917 // Note that drawableContentRects are actually in targetSurface space, so the transform we
918 // have to provide is the target surface's screenSpaceTransform.
[email protected]96baf3e2012-10-22 23:09:55919 LayerImpl* renderTarget = currentLayer->renderTarget();
[email protected]01527732012-10-19 18:16:19920 if (layerClipsSubtree(currentLayer) && !pointHitsRect(screenSpacePoint, renderTarget->renderSurface()->screenSpaceTransform(), currentLayer->drawableContentRect()))
[email protected]94f206c12012-08-25 00:09:14921 return true;
922
923 currentLayer = currentLayer->parent();
924 }
925
926 // If we have finished walking all ancestors without having already exited, then the point is not clipped by any ancestors.
927 return false;
928}
929
[email protected]d455d552012-11-02 00:19:06930LayerImpl* LayerTreeHostCommon::findLayerThatIsHitByPoint(const gfx::PointF& screenSpacePoint, std::vector<LayerImpl*>& renderSurfaceLayerList)
[email protected]94f206c12012-08-25 00:09:14931{
[email protected]96baf3e2012-10-22 23:09:55932 LayerImpl* foundLayer = 0;
[email protected]94f206c12012-08-25 00:09:14933
[email protected]96baf3e2012-10-22 23:09:55934 typedef LayerIterator<LayerImpl, std::vector<LayerImpl*>, RenderSurfaceImpl, LayerIteratorActions::FrontToBack> LayerIteratorType;
935 LayerIteratorType end = LayerIteratorType::end(&renderSurfaceLayerList);
[email protected]94f206c12012-08-25 00:09:14936
[email protected]96baf3e2012-10-22 23:09:55937 for (LayerIteratorType it = LayerIteratorType::begin(&renderSurfaceLayerList); it != end; ++it) {
[email protected]94f206c12012-08-25 00:09:14938 // We don't want to consider renderSurfaces for hit testing.
939 if (!it.representsItself())
940 continue;
941
[email protected]96baf3e2012-10-22 23:09:55942 LayerImpl* currentLayer = (*it);
[email protected]94f206c12012-08-25 00:09:14943
[email protected]aad0a0072012-11-01 18:15:58944 gfx::RectF contentRect(gfx::PointF(), currentLayer->contentBounds());
[email protected]01527732012-10-19 18:16:19945 if (!pointHitsRect(screenSpacePoint, currentLayer->screenSpaceTransform(), contentRect))
[email protected]94f206c12012-08-25 00:09:14946 continue;
947
948 // At this point, we think the point does hit the layer, but we need to walk up
949 // the parents to ensure that the layer was not clipped in such a way that the
950 // hit point actually should not hit the layer.
[email protected]01527732012-10-19 18:16:19951 if (pointIsClippedBySurfaceOrClipRect(screenSpacePoint, currentLayer))
[email protected]94f206c12012-08-25 00:09:14952 continue;
953
954 foundLayer = currentLayer;
955 break;
956 }
957
[email protected]01527732012-10-19 18:16:19958 // 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:14959 return foundLayer;
960}
961
[email protected]2f1acc262012-11-16 21:42:22962LayerImpl* LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(const gfx::PointF& screenSpacePoint, std::vector<LayerImpl*>& renderSurfaceLayerList)
963{
964 LayerImpl* foundLayer = 0;
965
966 typedef LayerIterator<LayerImpl, std::vector<LayerImpl*>, RenderSurfaceImpl, LayerIteratorActions::FrontToBack> LayerIteratorType;
967 LayerIteratorType end = LayerIteratorType::end(&renderSurfaceLayerList);
968
969 for (LayerIteratorType it = LayerIteratorType::begin(&renderSurfaceLayerList); it != end; ++it) {
970 // We don't want to consider renderSurfaces for hit testing.
971 if (!it.representsItself())
972 continue;
973
974 LayerImpl* currentLayer = (*it);
975
976 if (currentLayer->touchEventHandlerRegion().IsEmpty())
977 continue;
978
979 if (!pointHitsRegion(screenSpacePoint, currentLayer->screenSpaceTransform(), currentLayer->touchEventHandlerRegion(), currentLayer->contentsScaleX(), currentLayer->contentsScaleY()))
980 continue;
981
982 // At this point, we think the point does hit the touch event handler region on the layer, but we need to walk up
983 // the parents to ensure that the layer was not clipped in such a way that the
984 // hit point actually should not hit the layer.
985 if (pointIsClippedBySurfaceOrClipRect(screenSpacePoint, currentLayer))
986 continue;
987
988 foundLayer = currentLayer;
989 break;
990 }
991
992 // This can potentially return 0, which means the screenSpacePoint did not successfully hit test any layers, not even the root layer.
993 return foundLayer;
994}
995
[email protected]bc5e77c2012-11-05 20:00:49996} // namespace cc