blob: dd1022511d941434b89820d6dd3dbdfeca596a9f [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]94f206c12012-08-25 00:09:145#include "config.h"
6
[email protected]d50c6862012-10-23 02:08:317#include "cc/layer_tree_host_common.h"
[email protected]94f206c12012-08-25 00:09:148
[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]aad0a0072012-11-01 18:15:5816#include "ui/gfx/rect_conversions.h"
[email protected]518ee582012-10-24 18:29:4417#include <algorithm>
[email protected]94f206c12012-08-25 00:09:1418#include <public/WebTransformationMatrix.h>
19
20using WebKit::WebTransformationMatrix;
21
[email protected]9c88e562012-09-14 22:21:3022namespace cc {
[email protected]94f206c12012-08-25 00:09:1423
[email protected]96baf3e2012-10-22 23:09:5524ScrollAndScaleSet::ScrollAndScaleSet()
[email protected]493067512012-09-19 23:34:1025{
26}
27
[email protected]96baf3e2012-10-22 23:09:5528ScrollAndScaleSet::~ScrollAndScaleSet()
[email protected]493067512012-09-19 23:34:1029{
30}
31
[email protected]aad0a0072012-11-01 18:15:5832gfx::Rect LayerTreeHostCommon::calculateVisibleRect(const gfx::Rect& targetSurfaceRect, const gfx::Rect& layerBoundRect, const WebTransformationMatrix& transform)
[email protected]94f206c12012-08-25 00:09:1433{
34 // Is this layer fully contained within the target surface?
[email protected]aad0a0072012-11-01 18:15:5835 gfx::Rect layerInSurfaceSpace = MathUtil::mapClippedRect(transform, layerBoundRect);
36 if (targetSurfaceRect.Contains(layerInSurfaceSpace))
[email protected]94f206c12012-08-25 00:09:1437 return layerBoundRect;
38
39 // If the layer doesn't fill up the entire surface, then find the part of
40 // the surface rect where the layer could be visible. This avoids trying to
41 // project surface rect points that are behind the projection point.
[email protected]aad0a0072012-11-01 18:15:5842 gfx::Rect minimalSurfaceRect = targetSurfaceRect;
43 minimalSurfaceRect.Intersect(layerInSurfaceSpace);
[email protected]94f206c12012-08-25 00:09:1444
45 // Project the corners of the target surface rect into the layer space.
46 // This bounding rectangle may be larger than it needs to be (being
47 // axis-aligned), but is a reasonable filter on the space to consider.
48 // Non-invertible transforms will create an empty rect here.
49 const WebTransformationMatrix surfaceToLayer = transform.inverse();
[email protected]aad0a0072012-11-01 18:15:5850 gfx::Rect layerRect = gfx::ToEnclosingRect(MathUtil::projectClippedRect(surfaceToLayer, gfx::RectF(minimalSurfaceRect)));
51 layerRect.Intersect(layerBoundRect);
[email protected]94f206c12012-08-25 00:09:1452 return layerRect;
53}
54
[email protected]ecc12622012-10-30 20:45:4255template <typename LayerType>
56static inline bool isRootLayer(LayerType* layer)
57{
58 return !layer->parent();
59}
60
[email protected]94f206c12012-08-25 00:09:1461template<typename LayerType>
62static inline bool layerIsInExisting3DRenderingContext(LayerType* layer)
63{
64 // According to current W3C spec on CSS transforms, a layer is part of an established
65 // 3d rendering context if its parent has transform-style of preserves-3d.
66 return layer->parent() && layer->parent()->preserves3D();
67}
68
69template<typename LayerType>
[email protected]ecc12622012-10-30 20:45:4270static bool isRootLayerOfNewRenderingContext(LayerType* layer)
[email protected]94f206c12012-08-25 00:09:1471{
72 // According to current W3C spec on CSS transforms (Section 6.1), a layer is the
73 // beginning of 3d rendering context if its parent does not have transform-style:
74 // preserve-3d, but this layer itself does.
75 if (layer->parent())
76 return !layer->parent()->preserves3D() && layer->preserves3D();
77
78 return layer->preserves3D();
79}
80
81template<typename LayerType>
82static bool isLayerBackFaceVisible(LayerType* layer)
83{
84 // The current W3C spec on CSS transforms says that backface visibility should be
85 // determined differently depending on whether the layer is in a "3d rendering
86 // context" or not. For Chromium code, we can determine whether we are in a 3d
87 // rendering context by checking if the parent preserves 3d.
88
89 if (layerIsInExisting3DRenderingContext(layer))
90 return layer->drawTransform().isBackFaceVisible();
91
92 // In this case, either the layer establishes a new 3d rendering context, or is not in
93 // a 3d rendering context at all.
94 return layer->transform().isBackFaceVisible();
95}
96
97template<typename LayerType>
98static bool isSurfaceBackFaceVisible(LayerType* layer, const WebTransformationMatrix& drawTransform)
99{
100 if (layerIsInExisting3DRenderingContext(layer))
101 return drawTransform.isBackFaceVisible();
102
[email protected]ecc12622012-10-30 20:45:42103 if (isRootLayerOfNewRenderingContext(layer))
[email protected]94f206c12012-08-25 00:09:14104 return layer->transform().isBackFaceVisible();
105
106 // If the renderSurface is not part of a new or existing rendering context, then the
107 // layers that contribute to this surface will decide back-face visibility for themselves.
108 return false;
109}
110
111template<typename LayerType>
112static inline bool layerClipsSubtree(LayerType* layer)
113{
114 return layer->masksToBounds() || layer->maskLayer();
115}
116
117template<typename LayerType>
[email protected]aad0a0072012-11-01 18:15:58118static gfx::Rect calculateVisibleContentRect(LayerType* layer)
[email protected]94f206c12012-08-25 00:09:14119{
[email protected]1d993172012-10-18 18:15:04120 DCHECK(layer->renderTarget());
[email protected]94f206c12012-08-25 00:09:14121
[email protected]9bab0bb2012-10-08 19:11:58122 // Nothing is visible if the layer bounds are empty.
[email protected]aad0a0072012-11-01 18:15:58123 if (!layer->drawsContent() || layer->contentBounds().IsEmpty() || layer->drawableContentRect().IsEmpty())
124 return gfx::Rect();
[email protected]94f206c12012-08-25 00:09:14125
[email protected]aad0a0072012-11-01 18:15:58126 gfx::Rect targetSurfaceClipRect;
[email protected]9bab0bb2012-10-08 19:11:58127
128 // First, compute visible bounds in target surface space.
[email protected]aad0a0072012-11-01 18:15:58129 if (layer->renderTarget()->renderSurface()->clipRect().IsEmpty())
[email protected]9bab0bb2012-10-08 19:11:58130 targetSurfaceClipRect = layer->drawableContentRect();
131 else {
132 // In this case the target surface does clip layers that contribute to it. So, we
133 // have convert the current surface's clipRect from its ancestor surface space to
134 // the current surface space.
[email protected]aad0a0072012-11-01 18:15:58135 targetSurfaceClipRect = gfx::ToEnclosingRect(MathUtil::projectClippedRect(layer->renderTarget()->renderSurface()->drawTransform().inverse(), layer->renderTarget()->renderSurface()->clipRect()));
136 targetSurfaceClipRect.Intersect(layer->drawableContentRect());
[email protected]9bab0bb2012-10-08 19:11:58137 }
138
[email protected]aad0a0072012-11-01 18:15:58139 if (targetSurfaceClipRect.IsEmpty())
140 return gfx::Rect();
[email protected]9bab0bb2012-10-08 19:11:58141
[email protected]aad0a0072012-11-01 18:15:58142 return LayerTreeHostCommon::calculateVisibleRect(targetSurfaceClipRect, gfx::Rect(gfx::Point(), layer->contentBounds()), layer->drawTransform());
[email protected]94f206c12012-08-25 00:09:14143}
144
145static bool isScaleOrTranslation(const WebTransformationMatrix& m)
146{
147 return !m.m12() && !m.m13() && !m.m14()
148 && !m.m21() && !m.m23() && !m.m24()
149 && !m.m31() && !m.m32() && !m.m43()
150 && m.m44();
151}
152
[email protected]96baf3e2012-10-22 23:09:55153static inline bool transformToParentIsKnown(LayerImpl*)
[email protected]94f206c12012-08-25 00:09:14154{
155 return true;
156}
157
[email protected]96baf3e2012-10-22 23:09:55158static inline bool transformToParentIsKnown(Layer* layer)
[email protected]94f206c12012-08-25 00:09:14159{
160 return !layer->transformIsAnimating();
161}
162
[email protected]96baf3e2012-10-22 23:09:55163static inline bool transformToScreenIsKnown(LayerImpl*)
[email protected]94f206c12012-08-25 00:09:14164{
165 return true;
166}
167
[email protected]96baf3e2012-10-22 23:09:55168static inline bool transformToScreenIsKnown(Layer* layer)
[email protected]94f206c12012-08-25 00:09:14169{
170 return !layer->screenSpaceTransformIsAnimating();
171}
172
173template<typename LayerType>
174static bool layerShouldBeSkipped(LayerType* layer)
175{
176 // Layers can be skipped if any of these conditions are met.
177 // - does not draw content.
178 // - is transparent
179 // - has empty bounds
180 // - the layer is not double-sided, but its back face is visible.
181 //
182 // Some additional conditions need to be computed at a later point after the recursion is finished.
183 // - the intersection of render surface content and layer clipRect is empty
184 // - the visibleContentRect is empty
185 //
186 // Note, if the layer should not have been drawn due to being fully transparent,
187 // we would have skipped the entire subtree and never made it into this function,
188 // so it is safe to omit this check here.
189
[email protected]aad0a0072012-11-01 18:15:58190 if (!layer->drawsContent() || layer->bounds().IsEmpty())
[email protected]94f206c12012-08-25 00:09:14191 return true;
192
193 LayerType* backfaceTestLayer = layer;
194 if (layer->useParentBackfaceVisibility()) {
[email protected]1d993172012-10-18 18:15:04195 DCHECK(layer->parent());
196 DCHECK(!layer->parent()->useParentBackfaceVisibility());
[email protected]94f206c12012-08-25 00:09:14197 backfaceTestLayer = layer->parent();
198 }
199
200 // 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.
201 if (!backfaceTestLayer->doubleSided() && transformToScreenIsKnown(backfaceTestLayer) && isLayerBackFaceVisible(backfaceTestLayer))
202 return true;
203
204 return false;
205}
206
[email protected]96baf3e2012-10-22 23:09:55207static inline bool subtreeShouldBeSkipped(LayerImpl* layer)
[email protected]94f206c12012-08-25 00:09:14208{
209 // The opacity of a layer always applies to its children (either implicitly
210 // via a render surface or explicitly if the parent preserves 3D), so the
211 // entire subtree can be skipped if this layer is fully transparent.
212 return !layer->opacity();
213}
214
[email protected]96baf3e2012-10-22 23:09:55215static inline bool subtreeShouldBeSkipped(Layer* layer)
[email protected]94f206c12012-08-25 00:09:14216{
217 // If the opacity is being animated then the opacity on the main thread is unreliable
218 // (since the impl thread may be using a different opacity), so it should not be trusted.
219 // In particular, it should not cause the subtree to be skipped.
220 return !layer->opacity() && !layer->opacityIsAnimating();
221}
222
223template<typename LayerType>
224static bool subtreeShouldRenderToSeparateSurface(LayerType* layer, bool axisAlignedWithRespectToParent)
225{
[email protected]94f206c12012-08-25 00:09:14226 //
[email protected]96baf3e2012-10-22 23:09:55227 // A layer and its descendants should render onto a new RenderSurfaceImpl if any of these rules hold:
[email protected]94f206c12012-08-25 00:09:14228 //
229
[email protected]ecc12622012-10-30 20:45:42230 // The root layer should always have a renderSurface.
231 if (isRootLayer(layer))
232 return true;
233
[email protected]94f206c12012-08-25 00:09:14234 // If we force it.
235 if (layer->forceRenderSurface())
236 return true;
237
238 // If the layer uses a mask.
239 if (layer->maskLayer())
240 return true;
241
242 // If the layer has a reflection.
243 if (layer->replicaLayer())
244 return true;
245
246 // If the layer uses a CSS filter.
[email protected]4000abf2012-10-23 04:45:45247 if (!layer->filters().isEmpty() || !layer->backgroundFilters().isEmpty() || layer->filter())
[email protected]94f206c12012-08-25 00:09:14248 return true;
249
[email protected]ecc12622012-10-30 20:45:42250 // Cache this value, because otherwise it walks the entire subtree several times.
251 bool descendantDrawsContent = layer->descendantDrawsContent();
252
[email protected]94f206c12012-08-25 00:09:14253 // If the layer flattens its subtree (i.e. the layer doesn't preserve-3d), but it is
254 // treated as a 3D object by its parent (i.e. parent does preserve-3d).
255 if (layerIsInExisting3DRenderingContext(layer) && !layer->preserves3D() && descendantDrawsContent)
256 return true;
257
258 // If the layer clips its descendants but it is not axis-aligned with respect to its parent.
259 if (layerClipsSubtree(layer) && !axisAlignedWithRespectToParent && descendantDrawsContent)
260 return true;
261
262 // If the layer has opacity != 1 and does not have a preserves-3d transform style.
263 if (layer->opacity() != 1 && !layer->preserves3D() && descendantDrawsContent)
264 return true;
265
266 return false;
267}
268
[email protected]96baf3e2012-10-22 23:09:55269WebTransformationMatrix computeScrollCompensationForThisLayer(LayerImpl* scrollingLayer, const WebTransformationMatrix& parentMatrix)
[email protected]94f206c12012-08-25 00:09:14270{
271 // For every layer that has non-zero scrollDelta, we have to compute a transform that can undo the
272 // scrollDelta translation. In particular, we want this matrix to premultiply a fixed-position layer's
273 // parentMatrix, so we design this transform in three steps as follows. The steps described here apply
274 // from right-to-left, so Step 1 would be the right-most matrix:
275 //
276 // Step 1. transform from target surface space to the exact space where scrollDelta is actually applied.
277 // -- this is inverse of the matrix in step 3
278 // Step 2. undo the scrollDelta
279 // -- this is just a translation by scrollDelta.
280 // Step 3. transform back to target surface space.
281 // -- this transform is the "partialLayerOriginTransform" = (parentMatrix * scale(layer->pageScaleDelta()));
282 //
283 // These steps create a matrix that both start and end in targetSurfaceSpace. So this matrix can
284 // pre-multiply any fixed-position layer's drawTransform to undo the scrollDeltas -- as long as
285 // that fixed position layer is fixed onto the same renderTarget as this scrollingLayer.
286 //
287
288 WebTransformationMatrix partialLayerOriginTransform = parentMatrix;
[email protected]1c0c9bc2012-10-08 22:41:48289 partialLayerOriginTransform.multiply(scrollingLayer->implTransform());
[email protected]94f206c12012-08-25 00:09:14290
291 WebTransformationMatrix scrollCompensationForThisLayer = partialLayerOriginTransform; // Step 3
[email protected]cf817fb2012-11-05 17:17:34292 scrollCompensationForThisLayer.translate(scrollingLayer->scrollDelta().width(), scrollingLayer->scrollDelta().height()); // Step 2
[email protected]94f206c12012-08-25 00:09:14293 scrollCompensationForThisLayer.multiply(partialLayerOriginTransform.inverse()); // Step 1
294 return scrollCompensationForThisLayer;
295}
296
[email protected]96baf3e2012-10-22 23:09:55297WebTransformationMatrix computeScrollCompensationMatrixForChildren(Layer* currentLayer, const WebTransformationMatrix& currentParentMatrix, const WebTransformationMatrix& currentScrollCompensation)
[email protected]94f206c12012-08-25 00:09:14298{
[email protected]96baf3e2012-10-22 23:09:55299 // The main thread (i.e. Layer) does not need to worry about scroll compensation.
[email protected]94f206c12012-08-25 00:09:14300 // So we can just return an identity matrix here.
301 return WebTransformationMatrix();
302}
303
[email protected]96baf3e2012-10-22 23:09:55304WebTransformationMatrix computeScrollCompensationMatrixForChildren(LayerImpl* layer, const WebTransformationMatrix& parentMatrix, const WebTransformationMatrix& currentScrollCompensationMatrix)
[email protected]94f206c12012-08-25 00:09:14305{
306 // "Total scroll compensation" is the transform needed to cancel out all scrollDelta translations that
307 // occurred since the nearest container layer, even if there are renderSurfaces in-between.
308 //
309 // There are some edge cases to be aware of, that are not explicit in the code:
310 // - A layer that is both a fixed-position and container should not be its own container, instead, that means
311 // it is fixed to an ancestor, and is a container for any fixed-position descendants.
312 // - A layer that is a fixed-position container and has a renderSurface should behave the same as a container
313 // without a renderSurface, the renderSurface is irrelevant in that case.
[email protected]ecc12622012-10-30 20:45:42314 // - A layer that does not have an explicit container is simply fixed to the viewport.
315 // (i.e. the root renderSurface.)
[email protected]94f206c12012-08-25 00:09:14316 // - If the fixed-position layer has its own renderSurface, then the renderSurface is
317 // the one who gets fixed.
318 //
319 // This function needs to be called AFTER layers create their own renderSurfaces.
320 //
321
322 // 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]cf817fb2012-11-05 17:17:34323 if (!layer->isContainerForFixedPositionLayers() && layer->scrollDelta().isZero() && !layer->renderSurface())
[email protected]94f206c12012-08-25 00:09:14324 return currentScrollCompensationMatrix;
325
326 // Start as identity matrix.
327 WebTransformationMatrix nextScrollCompensationMatrix;
328
329 // If this layer is not a container, then it inherits the existing scroll compensations.
330 if (!layer->isContainerForFixedPositionLayers())
331 nextScrollCompensationMatrix = currentScrollCompensationMatrix;
332
333 // If the current layer has a non-zero scrollDelta, then we should compute its local scrollCompensation
334 // and accumulate it to the nextScrollCompensationMatrix.
[email protected]cf817fb2012-11-05 17:17:34335 if (!layer->scrollDelta().isZero()) {
[email protected]94f206c12012-08-25 00:09:14336 WebTransformationMatrix scrollCompensationForThisLayer = computeScrollCompensationForThisLayer(layer, parentMatrix);
337 nextScrollCompensationMatrix.multiply(scrollCompensationForThisLayer);
338 }
339
340 // If the layer created its own renderSurface, we have to adjust nextScrollCompensationMatrix.
341 // The adjustment allows us to continue using the scrollCompensation on the next surface.
342 // Step 1 (right-most in the math): transform from the new surface to the original ancestor surface
343 // Step 2: apply the scroll compensation
344 // Step 3: transform back to the new surface.
345 if (layer->renderSurface() && !nextScrollCompensationMatrix.isIdentity())
346 nextScrollCompensationMatrix = layer->renderSurface()->drawTransform().inverse() * nextScrollCompensationMatrix * layer->renderSurface()->drawTransform();
347
348 return nextScrollCompensationMatrix;
349}
350
[email protected]518ee582012-10-24 18:29:44351// There is no contentsScale on impl thread.
352static inline void updateLayerContentsScale(LayerImpl*, const WebTransformationMatrix&, float, float) { }
353
354static inline void updateLayerContentsScale(Layer* layer, const WebTransformationMatrix& combinedTransform, float deviceScaleFactor, float pageScaleFactor)
355{
356 float rasterScale = layer->rasterScale();
357 if (!rasterScale) {
358 rasterScale = 1;
359
360 if (layer->automaticallyComputeRasterScale()) {
[email protected]aad0a0072012-11-01 18:15:58361 gfx::Vector2dF transformScale = MathUtil::computeTransform2dScaleComponents(combinedTransform);
[email protected]518ee582012-10-24 18:29:44362 float combinedScale = std::max(transformScale.x(), transformScale.y());
363 rasterScale = combinedScale / deviceScaleFactor;
364 if (!layer->boundsContainPageScale())
365 rasterScale /= pageScaleFactor;
366 layer->setRasterScale(rasterScale);
367 }
368 }
369
370 float contentsScale = rasterScale * deviceScaleFactor;
371 if (!layer->boundsContainPageScale())
372 contentsScale *= pageScaleFactor;
373 layer->setContentsScale(contentsScale);
374
375 Layer* maskLayer = layer->maskLayer();
376 if (maskLayer)
377 maskLayer->setContentsScale(contentsScale);
378
379 Layer* replicaMaskLayer = layer->replicaLayer() ? layer->replicaLayer()->maskLayer() : 0;
380 if (replicaMaskLayer)
381 replicaMaskLayer->setContentsScale(contentsScale);
382}
383
[email protected]94f206c12012-08-25 00:09:14384// Recursively walks the layer tree starting at the given node and computes all the
385// necessary transformations, clipRects, render surfaces, etc.
386template<typename LayerType, typename LayerList, typename RenderSurfaceType, typename LayerSorter>
[email protected]ecc12622012-10-30 20:45:42387static void calculateDrawTransformsInternal(LayerType* layer, const WebTransformationMatrix& parentMatrix,
[email protected]94f206c12012-08-25 00:09:14388 const WebTransformationMatrix& fullHierarchyMatrix, const WebTransformationMatrix& currentScrollCompensationMatrix,
[email protected]aad0a0072012-11-01 18:15:58389 const gfx::Rect& clipRectFromAncestor, bool ancestorClipsSubtree,
[email protected]94f206c12012-08-25 00:09:14390 RenderSurfaceType* nearestAncestorThatMovesPixels, LayerList& renderSurfaceLayerList, LayerList& layerList,
[email protected]aad0a0072012-11-01 18:15:58391 LayerSorter* layerSorter, int maxTextureSize, float deviceScaleFactor, float pageScaleFactor, gfx::Rect& drawableContentRectOfSubtree)
[email protected]94f206c12012-08-25 00:09:14392{
393 // This function computes the new matrix transformations recursively for this
394 // layer and all its descendants. It also computes the appropriate render surfaces.
395 // Some important points to remember:
396 //
397 // 0. Here, transforms are notated in Matrix x Vector order, and in words we describe what
398 // the transform does from left to right.
399 //
400 // 1. In our terminology, the "layer origin" refers to the top-left corner of a layer, and the
401 // positive Y-axis points downwards. This interpretation is valid because the orthographic
402 // projection applied at draw time flips the Y axis appropriately.
403 //
[email protected]aad0a0072012-11-01 18:15:58404 // 2. The anchor point, when given as a PointF object, is specified in "unit layer space",
[email protected]94f206c12012-08-25 00:09:14405 // where the bounds of the layer map to [0, 1]. However, as a WebTransformationMatrix object,
[email protected]b8d3c022012-10-08 17:38:04406 // the transform to the anchor point is specified in "layer space", where the bounds
[email protected]94f206c12012-08-25 00:09:14407 // of the layer map to [bounds.width(), bounds.height()].
408 //
409 // 3. Definition of various transforms used:
410 // M[parent] is the parent matrix, with respect to the nearest render surface, passed down recursively.
411 // M[root] is the full hierarchy, with respect to the root, passed down recursively.
412 // Tr[origin] is the translation matrix from the parent's origin to this layer's origin.
413 // Tr[origin2anchor] is the translation from the layer's origin to its anchor point
414 // Tr[origin2center] is the translation from the layer's origin to its center
415 // M[layer] is the layer's matrix (applied at the anchor point)
416 // M[sublayer] is the layer's sublayer transform (applied at the layer's center)
[email protected]b8d3c022012-10-08 17:38:04417 // S[layer2content] is the ratio of a layer's contentBounds() to its bounds().
[email protected]94f206c12012-08-25 00:09:14418 //
419 // Some composite transforms can help in understanding the sequence of transforms:
420 // compositeLayerTransform = Tr[origin2anchor] * M[layer] * Tr[origin2anchor].inverse()
421 // compositeSublayerTransform = Tr[origin2center] * M[sublayer] * Tr[origin2center].inverse()
422 //
423 // In words, the layer transform is applied about the anchor point, and the sublayer transform is
424 // applied about the center of the layer.
425 //
426 // 4. When a layer (or render surface) is drawn, it is drawn into a "target render surface". Therefore the draw
427 // transform does not necessarily transform from screen space to local layer space. Instead, the draw transform
428 // is the transform between the "target render surface space" and local layer space. Note that render surfaces,
429 // except for the root, also draw themselves into a different target render surface, and so their draw
430 // transform and origin transforms are also described with respect to the target.
431 //
432 // Using these definitions, then:
433 //
434 // The draw transform for the layer is:
[email protected]b8d3c022012-10-08 17:38:04435 // M[draw] = M[parent] * Tr[origin] * compositeLayerTransform * S[layer2content]
436 // = M[parent] * Tr[layer->position() + anchor] * M[layer] * Tr[anchor2origin] * S[layer2content]
[email protected]94f206c12012-08-25 00:09:14437 //
438 // Interpreting the math left-to-right, this transforms from the layer's render surface to the origin of the layer in content space.
439 //
440 // The screen space transform is:
[email protected]b8d3c022012-10-08 17:38:04441 // M[screenspace] = M[root] * Tr[origin] * compositeLayerTransform * S[layer2content]
442 // = M[root] * Tr[layer->position() + anchor] * M[layer] * Tr[anchor2origin] * S[layer2content]
[email protected]94f206c12012-08-25 00:09:14443 //
444 // Interpreting the math left-to-right, this transforms from the root render surface's content space to the local layer's origin in layer space.
445 //
446 // The transform hierarchy that is passed on to children (i.e. the child's parentMatrix) is:
447 // M[parent]_for_child = M[parent] * Tr[origin] * compositeLayerTransform * compositeSublayerTransform
[email protected]b8d3c022012-10-08 17:38:04448 // = M[parent] * Tr[layer->position() + anchor] * M[layer] * Tr[anchor2origin] * compositeSublayerTransform
[email protected]94f206c12012-08-25 00:09:14449 //
450 // and a similar matrix for the full hierarchy with respect to the root.
451 //
452 // Finally, note that the final matrix used by the shader for the layer is P * M[draw] * S . This final product
453 // is computed in drawTexturedQuad(), where:
454 // P is the projection matrix
[email protected]b8d3c022012-10-08 17:38:04455 // S is the scale adjustment (to scale up a canonical quad to the layer's size)
[email protected]94f206c12012-08-25 00:09:14456 //
457 // When a render surface has a replica layer, that layer's transform is used to draw a second copy of the surface.
458 // Transforms named here are relative to the surface, unless they specify they are relative to the replica layer.
459 //
460 // We will denote a scale by device scale S[deviceScale]
461 //
462 // The render surface draw transform to its target surface origin is:
463 // M[surfaceDraw] = M[owningLayer->Draw]
464 //
465 // The render surface origin transform to its the root (screen space) origin is:
466 // M[surface2root] = M[owningLayer->screenspace] * S[deviceScale].inverse()
467 //
468 // The replica draw transform to its target surface origin is:
469 // M[replicaDraw] = S[deviceScale] * M[surfaceDraw] * Tr[replica->position() + replica->anchor()] * Tr[replica] * Tr[origin2anchor].inverse() * S[contentsScale].inverse()
470 //
471 // The replica draw transform to the root (screen space) origin is:
472 // M[replica2root] = M[surface2root] * Tr[replica->position()] * Tr[replica] * Tr[origin2anchor].inverse()
473 //
474
475 // If we early-exit anywhere in this function, the drawableContentRect of this subtree should be considered empty.
[email protected]aad0a0072012-11-01 18:15:58476 drawableContentRectOfSubtree = gfx::Rect();
[email protected]94f206c12012-08-25 00:09:14477
[email protected]ecc12622012-10-30 20:45:42478 // The root layer cannot skip calcDrawTransforms.
479 if (!isRootLayer(layer) && subtreeShouldBeSkipped(layer))
[email protected]94f206c12012-08-25 00:09:14480 return;
481
[email protected]aad0a0072012-11-01 18:15:58482 gfx::Rect clipRectForSubtree;
[email protected]94f206c12012-08-25 00:09:14483 bool subtreeShouldBeClipped = false;
484
485 float drawOpacity = layer->opacity();
486 bool drawOpacityIsAnimating = layer->opacityIsAnimating();
487 if (layer->parent() && layer->parent()->preserves3D()) {
488 drawOpacity *= layer->parent()->drawOpacity();
489 drawOpacityIsAnimating |= layer->parent()->drawOpacityIsAnimating();
490 }
491
[email protected]aad0a0072012-11-01 18:15:58492 gfx::Size bounds = layer->bounds();
493 gfx::PointF anchorPoint = layer->anchorPoint();
[email protected]cf817fb2012-11-05 17:17:34494 gfx::PointF position = layer->position() - gfx::Vector2d(layer->scrollDelta().width(), layer->scrollDelta().height());
[email protected]94f206c12012-08-25 00:09:14495
[email protected]94f206c12012-08-25 00:09:14496 WebTransformationMatrix layerLocalTransform;
[email protected]518ee582012-10-24 18:29:44497 // LT = Tr[origin] * Tr[origin2anchor]
[email protected]94f206c12012-08-25 00:09:14498 layerLocalTransform.translate3d(position.x() + anchorPoint.x() * bounds.width(), position.y() + anchorPoint.y() * bounds.height(), layer->anchorPointZ());
[email protected]518ee582012-10-24 18:29:44499 // LT = Tr[origin] * Tr[origin2anchor] * M[layer]
[email protected]94f206c12012-08-25 00:09:14500 layerLocalTransform.multiply(layer->transform());
[email protected]518ee582012-10-24 18:29:44501 // LT = Tr[origin] * Tr[origin2anchor] * M[layer] * Tr[anchor2origin]
[email protected]b8d3c022012-10-08 17:38:04502 layerLocalTransform.translate3d(-anchorPoint.x() * bounds.width(), -anchorPoint.y() * bounds.height(), -layer->anchorPointZ());
[email protected]94f206c12012-08-25 00:09:14503
504 WebTransformationMatrix combinedTransform = parentMatrix;
505 combinedTransform.multiply(layerLocalTransform);
506
[email protected]518ee582012-10-24 18:29:44507 // The layer's contentsSize is determined from the combinedTransform, which then informs the
508 // layer's drawTransform.
509 updateLayerContentsScale(layer, combinedTransform, deviceScaleFactor, pageScaleFactor);
510
511 // If there is a tranformation from the impl thread then it should be at the
512 // start of the combinedTransform, but we don't want it to affect the contentsScale.
513 combinedTransform = layer->implTransform() * combinedTransform;
514
[email protected]94f206c12012-08-25 00:09:14515 if (layer->fixedToContainerLayer()) {
516 // Special case: this layer is a composited fixed-position layer; we need to
517 // explicitly compensate for all ancestors' nonzero scrollDeltas to keep this layer
518 // fixed correctly.
519 combinedTransform = currentScrollCompensationMatrix * combinedTransform;
520 }
521
522 // The drawTransform that gets computed below is effectively the layer's drawTransform, unless
523 // the layer itself creates a renderSurface. In that case, the renderSurface re-parents the transforms.
524 WebTransformationMatrix drawTransform = combinedTransform;
[email protected]aad0a0072012-11-01 18:15:58525 if (!layer->contentBounds().IsEmpty() && !layer->bounds().IsEmpty()) {
[email protected]b8d3c022012-10-08 17:38:04526 // M[draw] = M[parent] * LT * S[layer2content]
[email protected]904e9132012-11-01 00:12:47527 drawTransform.scaleNonUniform(1.0 / layer->contentsScaleX(),
528 1.0 / layer->contentsScaleY());
[email protected]94f206c12012-08-25 00:09:14529 }
530
531 // layerScreenSpaceTransform represents the transform between root layer's "screen space" and local content space.
532 WebTransformationMatrix layerScreenSpaceTransform = fullHierarchyMatrix;
533 if (!layer->preserves3D())
[email protected]96baf3e2012-10-22 23:09:55534 MathUtil::flattenTransformTo2d(layerScreenSpaceTransform);
[email protected]94f206c12012-08-25 00:09:14535 layerScreenSpaceTransform.multiply(drawTransform);
536 layer->setScreenSpaceTransform(layerScreenSpaceTransform);
537
538 bool animatingTransformToTarget = layer->transformIsAnimating();
539 bool animatingTransformToScreen = animatingTransformToTarget;
540 if (layer->parent()) {
541 animatingTransformToTarget |= layer->parent()->drawTransformIsAnimating();
542 animatingTransformToScreen |= layer->parent()->screenSpaceTransformIsAnimating();
543 }
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]aad0a0072012-11-01 18:15:58567 if (!layer->contentBounds().IsEmpty() && !layer->bounds().IsEmpty())
568 drawTransform.scaleNonUniform(layer->contentsScaleX(), layer->contentsScaleY());
[email protected]518ee582012-10-24 18:29:44569 drawTransform.scaleNonUniform(1 / renderSurfaceSublayerScale.x(), 1 / renderSurfaceSublayerScale.y());
[email protected]f3922f22012-10-12 09:20:38570 renderSurface->setDrawTransform(drawTransform);
[email protected]518ee582012-10-24 18:29:44571
572 // The origin of the new surface is the upper left corner of the layer.
[email protected]94f206c12012-08-25 00:09:14573 WebTransformationMatrix layerDrawTransform;
[email protected]518ee582012-10-24 18:29:44574 layerDrawTransform.scaleNonUniform(renderSurfaceSublayerScale.x(), renderSurfaceSublayerScale.y());
[email protected]aad0a0072012-11-01 18:15:58575 if (!layer->contentBounds().IsEmpty() && !layer->bounds().IsEmpty())
576 layerDrawTransform.scaleNonUniform(1.0 / layer->contentsScaleX(), 1.0 / layer->contentsScaleY());
[email protected]94f206c12012-08-25 00:09:14577 layer->setDrawTransform(layerDrawTransform);
578
[email protected]518ee582012-10-24 18:29:44579 // Inside the surface's subtree, we scale everything to the owning layer's scale.
[email protected]94f206c12012-08-25 00:09:14580 // The sublayer matrix transforms centered layer rects into target
581 // surface content space.
582 sublayerMatrix.makeIdentity();
[email protected]518ee582012-10-24 18:29:44583 sublayerMatrix.scaleNonUniform(renderSurfaceSublayerScale.x(), renderSurfaceSublayerScale.y());
[email protected]94f206c12012-08-25 00:09:14584
585 // The opacity value is moved from the layer to its surface, so that the entire subtree properly inherits opacity.
586 renderSurface->setDrawOpacity(drawOpacity);
587 renderSurface->setDrawOpacityIsAnimating(drawOpacityIsAnimating);
588 layer->setDrawOpacity(1);
589 layer->setDrawOpacityIsAnimating(false);
590
591 renderSurface->setTargetSurfaceTransformsAreAnimating(animatingTransformToTarget);
592 renderSurface->setScreenSpaceTransformsAreAnimating(animatingTransformToScreen);
593 animatingTransformToTarget = false;
594 layer->setDrawTransformIsAnimating(animatingTransformToTarget);
595 layer->setScreenSpaceTransformIsAnimating(animatingTransformToScreen);
596
597 // Update the aggregate hierarchy matrix to include the transform of the
[email protected]96baf3e2012-10-22 23:09:55598 // newly created RenderSurfaceImpl.
[email protected]94f206c12012-08-25 00:09:14599 nextHierarchyMatrix.multiply(renderSurface->drawTransform());
600
601 // The new renderSurface here will correctly clip the entire subtree. So, we do
602 // not need to continue propagating the clipping state further down the tree. This
603 // way, we can avoid transforming clipRects from ancestor target surface space to
604 // current target surface space that could cause more w < 0 headaches.
605 subtreeShouldBeClipped = false;
606
[email protected]9bab0bb2012-10-08 19:11:58607 if (layer->maskLayer()) {
[email protected]94f206c12012-08-25 00:09:14608 layer->maskLayer()->setRenderTarget(layer);
[email protected]aad0a0072012-11-01 18:15:58609 layer->maskLayer()->setVisibleContentRect(gfx::Rect(gfx::Point(), layer->contentBounds()));
[email protected]9bab0bb2012-10-08 19:11:58610 }
[email protected]94f206c12012-08-25 00:09:14611
[email protected]9bab0bb2012-10-08 19:11:58612 if (layer->replicaLayer() && layer->replicaLayer()->maskLayer()) {
[email protected]94f206c12012-08-25 00:09:14613 layer->replicaLayer()->maskLayer()->setRenderTarget(layer);
[email protected]aad0a0072012-11-01 18:15:58614 layer->replicaLayer()->maskLayer()->setVisibleContentRect(gfx::Rect(gfx::Point(), layer->contentBounds()));
[email protected]9bab0bb2012-10-08 19:11:58615 }
[email protected]94f206c12012-08-25 00:09:14616
[email protected]4000abf2012-10-23 04:45:45617 // FIXME: make this smarter for the SkImageFilter case (check for
618 // pixel-moving filters)
619 if (layer->filters().hasFilterThatMovesPixels() || layer->filter())
[email protected]94f206c12012-08-25 00:09:14620 nearestAncestorThatMovesPixels = renderSurface;
621
[email protected]9bab0bb2012-10-08 19:11:58622 // The render surface clipRect is expressed in the space where this surface draws, i.e. the same space as clipRectFromAncestor.
623 if (ancestorClipsSubtree)
624 renderSurface->setClipRect(clipRectFromAncestor);
625 else
[email protected]aad0a0072012-11-01 18:15:58626 renderSurface->setClipRect(gfx::Rect());
[email protected]9bab0bb2012-10-08 19:11:58627
[email protected]94f206c12012-08-25 00:09:14628 renderSurface->setNearestAncestorThatMovesPixels(nearestAncestorThatMovesPixels);
629
[email protected]d58499a2012-10-09 22:27:47630 renderSurfaceLayerList.push_back(layer);
[email protected]94f206c12012-08-25 00:09:14631 } else {
[email protected]ecc12622012-10-30 20:45:42632 DCHECK(layer->parent());
633
[email protected]94f206c12012-08-25 00:09:14634 layer->setDrawTransform(drawTransform);
635 layer->setDrawTransformIsAnimating(animatingTransformToTarget);
636 layer->setScreenSpaceTransformIsAnimating(animatingTransformToScreen);
637 sublayerMatrix = combinedTransform;
638
639 layer->setDrawOpacity(drawOpacity);
640 layer->setDrawOpacityIsAnimating(drawOpacityIsAnimating);
641
[email protected]ecc12622012-10-30 20:45:42642 layer->clearRenderSurface();
[email protected]94f206c12012-08-25 00:09:14643
[email protected]ecc12622012-10-30 20:45:42644 // Layers without renderSurfaces directly inherit the ancestor's clip status.
645 subtreeShouldBeClipped = ancestorClipsSubtree;
646 if (ancestorClipsSubtree)
647 clipRectForSubtree = clipRectFromAncestor;
[email protected]94f206c12012-08-25 00:09:14648
[email protected]ecc12622012-10-30 20:45:42649 // Layers that are not their own renderTarget will render into the target of their nearest ancestor.
650 layer->setRenderTarget(layer->parent()->renderTarget());
[email protected]94f206c12012-08-25 00:09:14651 }
652
[email protected]aad0a0072012-11-01 18:15:58653 gfx::Rect rectInTargetSpace = ToEnclosingRect(MathUtil::mapClippedRect(layer->drawTransform(), contentRect));
[email protected]94f206c12012-08-25 00:09:14654
655 if (layerClipsSubtree(layer)) {
656 subtreeShouldBeClipped = true;
657 if (ancestorClipsSubtree && !layer->renderSurface()) {
658 clipRectForSubtree = clipRectFromAncestor;
[email protected]aad0a0072012-11-01 18:15:58659 clipRectForSubtree.Intersect(rectInTargetSpace);
[email protected]94f206c12012-08-25 00:09:14660 } else
661 clipRectForSubtree = rectInTargetSpace;
662 }
663
664 // Flatten to 2D if the layer doesn't preserve 3D.
665 if (!layer->preserves3D())
[email protected]96baf3e2012-10-22 23:09:55666 MathUtil::flattenTransformTo2d(sublayerMatrix);
[email protected]94f206c12012-08-25 00:09:14667
668 // Apply the sublayer transform at the center of the layer.
[email protected]b8d3c022012-10-08 17:38:04669 sublayerMatrix.translate(0.5 * bounds.width(), 0.5 * bounds.height());
[email protected]94f206c12012-08-25 00:09:14670 sublayerMatrix.multiply(layer->sublayerTransform());
[email protected]b8d3c022012-10-08 17:38:04671 sublayerMatrix.translate(-0.5 * bounds.width(), -0.5 * bounds.height());
[email protected]94f206c12012-08-25 00:09:14672
673 LayerList& descendants = (layer->renderSurface() ? layer->renderSurface()->layerList() : layerList);
674
675 // Any layers that are appended after this point are in the layer's subtree and should be included in the sorting process.
676 unsigned sortingStartIndex = descendants.size();
677
678 if (!layerShouldBeSkipped(layer))
[email protected]d58499a2012-10-09 22:27:47679 descendants.push_back(layer);
[email protected]94f206c12012-08-25 00:09:14680
681 WebTransformationMatrix nextScrollCompensationMatrix = computeScrollCompensationMatrixForChildren(layer, parentMatrix, currentScrollCompensationMatrix);;
682
[email protected]aad0a0072012-11-01 18:15:58683 gfx::Rect accumulatedDrawableContentRectOfChildren;
[email protected]94f206c12012-08-25 00:09:14684 for (size_t i = 0; i < layer->children().size(); ++i) {
[email protected]96baf3e2012-10-22 23:09:55685 LayerType* child = LayerTreeHostCommon::getChildAsRawPtr(layer->children(), i);
[email protected]aad0a0072012-11-01 18:15:58686 gfx::Rect drawableContentRectOfChildSubtree;
[email protected]ecc12622012-10-30 20:45:42687 calculateDrawTransformsInternal<LayerType, LayerList, RenderSurfaceType, LayerSorter>(child, sublayerMatrix, nextHierarchyMatrix, nextScrollCompensationMatrix,
[email protected]94f206c12012-08-25 00:09:14688 clipRectForSubtree, subtreeShouldBeClipped, nearestAncestorThatMovesPixels,
[email protected]518ee582012-10-24 18:29:44689 renderSurfaceLayerList, descendants, layerSorter, maxTextureSize, deviceScaleFactor, pageScaleFactor, drawableContentRectOfChildSubtree);
[email protected]aad0a0072012-11-01 18:15:58690 if (!drawableContentRectOfChildSubtree.IsEmpty()) {
691 accumulatedDrawableContentRectOfChildren.Union(drawableContentRectOfChildSubtree);
[email protected]94f206c12012-08-25 00:09:14692 if (child->renderSurface())
[email protected]d58499a2012-10-09 22:27:47693 descendants.push_back(child);
[email protected]94f206c12012-08-25 00:09:14694 }
695 }
696
697 // Compute the total drawableContentRect for this subtree (the rect is in targetSurface space)
[email protected]aad0a0072012-11-01 18:15:58698 gfx::Rect localDrawableContentRectOfSubtree = accumulatedDrawableContentRectOfChildren;
[email protected]94f206c12012-08-25 00:09:14699 if (layer->drawsContent())
[email protected]aad0a0072012-11-01 18:15:58700 localDrawableContentRectOfSubtree.Union(rectInTargetSpace);
[email protected]94f206c12012-08-25 00:09:14701 if (subtreeShouldBeClipped)
[email protected]aad0a0072012-11-01 18:15:58702 localDrawableContentRectOfSubtree.Intersect(clipRectForSubtree);
[email protected]94f206c12012-08-25 00:09:14703
704 // Compute the layer's drawable content rect (the rect is in targetSurface space)
[email protected]aad0a0072012-11-01 18:15:58705 gfx::Rect drawableContentRectOfLayer = rectInTargetSpace;
[email protected]94f206c12012-08-25 00:09:14706 if (subtreeShouldBeClipped)
[email protected]aad0a0072012-11-01 18:15:58707 drawableContentRectOfLayer.Intersect(clipRectForSubtree);
[email protected]94f206c12012-08-25 00:09:14708 layer->setDrawableContentRect(drawableContentRectOfLayer);
709
[email protected]9bab0bb2012-10-08 19:11:58710 // Compute the layer's visible content rect (the rect is in content space)
[email protected]aad0a0072012-11-01 18:15:58711 gfx::Rect visibleContentRectOfLayer = calculateVisibleContentRect(layer);
[email protected]9bab0bb2012-10-08 19:11:58712 layer->setVisibleContentRect(visibleContentRectOfLayer);
713
[email protected]94f206c12012-08-25 00:09:14714 // Compute the remaining properties for the render surface, if the layer has one.
[email protected]ecc12622012-10-30 20:45:42715 if (isRootLayer(layer)) {
716 // The root layer's surface's contentRect is always the entire viewport.
717 DCHECK(layer->renderSurface());
718 layer->renderSurface()->setContentRect(clipRectFromAncestor);
719 } else if (layer->renderSurface() && !isRootLayer(layer)) {
[email protected]94f206c12012-08-25 00:09:14720 RenderSurfaceType* renderSurface = layer->renderSurface();
[email protected]aad0a0072012-11-01 18:15:58721 gfx::Rect clippedContentRect = localDrawableContentRectOfSubtree;
[email protected]94f206c12012-08-25 00:09:14722
[email protected]94f206c12012-08-25 00:09:14723 // Don't clip if the layer is reflected as the reflection shouldn't be
724 // clipped. If the layer is animating, then the surface's transform to
725 // its target is not known on the main thread, and we should not use it
726 // to clip.
727 if (!layer->replicaLayer() && transformToParentIsKnown(layer)) {
728 // 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:58729 if (ancestorClipsSubtree && !clippedContentRect.IsEmpty()) {
730 gfx::Rect surfaceClipRect = LayerTreeHostCommon::calculateVisibleRect(renderSurface->clipRect(), clippedContentRect, renderSurface->drawTransform());
731 clippedContentRect.Intersect(surfaceClipRect);
[email protected]94f206c12012-08-25 00:09:14732 }
733 }
734
[email protected]96baf3e2012-10-22 23:09:55735 // The RenderSurfaceImpl backing texture cannot exceed the maximum supported
[email protected]94f206c12012-08-25 00:09:14736 // texture size.
[email protected]aad0a0072012-11-01 18:15:58737 clippedContentRect.set_width(std::min(clippedContentRect.width(), maxTextureSize));
738 clippedContentRect.set_height(std::min(clippedContentRect.height(), maxTextureSize));
[email protected]94f206c12012-08-25 00:09:14739
[email protected]aad0a0072012-11-01 18:15:58740 if (clippedContentRect.IsEmpty())
[email protected]7d929c02012-09-20 17:26:57741 renderSurface->clearLayerLists();
[email protected]94f206c12012-08-25 00:09:14742
743 renderSurface->setContentRect(clippedContentRect);
[email protected]518ee582012-10-24 18:29:44744
745 // The owning layer's screenSpaceTransform has a scale from content to layer space which we need to undo and
746 // replace with a scale from the surface's subtree into layer space.
747 WebTransformationMatrix screenSpaceTransform = layer->screenSpaceTransform();
[email protected]aad0a0072012-11-01 18:15:58748 if (!layer->contentBounds().IsEmpty() && !layer->bounds().IsEmpty())
749 screenSpaceTransform.scaleNonUniform(layer->contentsScaleX(), layer->contentsScaleY());
[email protected]518ee582012-10-24 18:29:44750 screenSpaceTransform.scaleNonUniform(1 / renderSurfaceSublayerScale.x(), 1 / renderSurfaceSublayerScale.y());
751 renderSurface->setScreenSpaceTransform(screenSpaceTransform);
[email protected]94f206c12012-08-25 00:09:14752
753 if (layer->replicaLayer()) {
754 WebTransformationMatrix surfaceOriginToReplicaOriginTransform;
[email protected]518ee582012-10-24 18:29:44755 surfaceOriginToReplicaOriginTransform.scaleNonUniform(renderSurfaceSublayerScale.x(), renderSurfaceSublayerScale.y());
[email protected]94f206c12012-08-25 00:09:14756 surfaceOriginToReplicaOriginTransform.translate(layer->replicaLayer()->position().x() + layer->replicaLayer()->anchorPoint().x() * bounds.width(),
757 layer->replicaLayer()->position().y() + layer->replicaLayer()->anchorPoint().y() * bounds.height());
758 surfaceOriginToReplicaOriginTransform.multiply(layer->replicaLayer()->transform());
759 surfaceOriginToReplicaOriginTransform.translate(-layer->replicaLayer()->anchorPoint().x() * bounds.width(), -layer->replicaLayer()->anchorPoint().y() * bounds.height());
[email protected]518ee582012-10-24 18:29:44760 surfaceOriginToReplicaOriginTransform.scaleNonUniform(1 / renderSurfaceSublayerScale.x(), 1 / renderSurfaceSublayerScale.y());
[email protected]94f206c12012-08-25 00:09:14761
762 // Compute the replica's "originTransform" that maps from the replica's origin space to the target surface origin space.
763 WebTransformationMatrix replicaOriginTransform = layer->renderSurface()->drawTransform() * surfaceOriginToReplicaOriginTransform;
764 renderSurface->setReplicaDrawTransform(replicaOriginTransform);
765
766 // Compute the replica's "screenSpaceTransform" that maps from the replica's origin space to the screen's origin space.
767 WebTransformationMatrix replicaScreenSpaceTransform = layer->renderSurface()->screenSpaceTransform() * surfaceOriginToReplicaOriginTransform;
768 renderSurface->setReplicaScreenSpaceTransform(replicaScreenSpaceTransform);
769 }
770
771 // If a render surface has no layer list, then it and none of its children needed to get drawn.
772 if (!layer->renderSurface()->layerList().size()) {
773 // FIXME: Originally we asserted that this layer was already at the end of the
774 // list, and only needed to remove that layer. For now, we remove the
775 // entire subtree of surfaces to fix a crash bug. The root cause is
776 // https://bugs.webkit.org/show_bug.cgi?id=74147 and we should be able
777 // to put the original assert after fixing that.
[email protected]d58499a2012-10-09 22:27:47778 while (renderSurfaceLayerList.back() != layer) {
779 renderSurfaceLayerList.back()->clearRenderSurface();
780 renderSurfaceLayerList.pop_back();
[email protected]94f206c12012-08-25 00:09:14781 }
[email protected]1d993172012-10-18 18:15:04782 DCHECK(renderSurfaceLayerList.back() == layer);
[email protected]d58499a2012-10-09 22:27:47783 renderSurfaceLayerList.pop_back();
[email protected]94f206c12012-08-25 00:09:14784 layer->clearRenderSurface();
785 return;
786 }
787 }
788
789 // If neither this layer nor any of its children were added, early out.
790 if (sortingStartIndex == descendants.size())
791 return;
792
793 // If preserves-3d then sort all the descendants in 3D so that they can be
794 // drawn from back to front. If the preserves-3d property is also set on the parent then
795 // skip the sorting as the parent will sort all the descendants anyway.
796 if (descendants.size() && layer->preserves3D() && (!layer->parent() || !layer->parent()->preserves3D()))
[email protected]d58499a2012-10-09 22:27:47797 sortLayers(descendants.begin() + sortingStartIndex, descendants.end(), layerSorter);
[email protected]94f206c12012-08-25 00:09:14798
799 if (layer->renderSurface())
[email protected]aad0a0072012-11-01 18:15:58800 drawableContentRectOfSubtree = gfx::ToEnclosingRect(layer->renderSurface()->drawableContentRect());
[email protected]94f206c12012-08-25 00:09:14801 else
802 drawableContentRectOfSubtree = localDrawableContentRectOfSubtree;
803
[email protected]7d929c02012-09-20 17:26:57804 if (layer->hasContributingDelegatedRenderPasses())
805 layer->renderTarget()->renderSurface()->addContributingDelegatedRenderPassLayer(layer);
[email protected]94f206c12012-08-25 00:09:14806}
807
[email protected]aad0a0072012-11-01 18:15:58808void 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:14809{
[email protected]aad0a0072012-11-01 18:15:58810 gfx::Rect totalDrawableContentRect;
[email protected]94f206c12012-08-25 00:09:14811 WebTransformationMatrix identityMatrix;
812 WebTransformationMatrix deviceScaleTransform;
813 deviceScaleTransform.scale(deviceScaleFactor);
[email protected]ecc12622012-10-30 20:45:42814 std::vector<scoped_refptr<Layer> > dummyLayerList;
[email protected]94f206c12012-08-25 00:09:14815
[email protected]ecc12622012-10-30 20:45:42816 // The root layer's renderSurface should receive the deviceViewport as the initial clipRect.
817 bool subtreeShouldBeClipped = true;
[email protected]aad0a0072012-11-01 18:15:58818 gfx::Rect deviceViewportRect(gfx::Point(), deviceViewportSize);
[email protected]ecc12622012-10-30 20:45:42819
820 // This function should have received a root layer.
821 DCHECK(isRootLayer(rootLayer));
[email protected]94f206c12012-08-25 00:09:14822
[email protected]518ee582012-10-24 18:29:44823 cc::calculateDrawTransformsInternal<Layer, std::vector<scoped_refptr<Layer> >, RenderSurface, void>(
[email protected]ecc12622012-10-30 20:45:42824 rootLayer, deviceScaleTransform, identityMatrix, identityMatrix,
825 deviceViewportRect, subtreeShouldBeClipped, 0, renderSurfaceLayerList,
826 dummyLayerList, 0, maxTextureSize,
[email protected]518ee582012-10-24 18:29:44827 deviceScaleFactor, pageScaleFactor, totalDrawableContentRect);
[email protected]ecc12622012-10-30 20:45:42828
829 // The dummy layer list should not have been used.
830 DCHECK(dummyLayerList.size() == 0);
831 // A root layer renderSurface should always exist after calculateDrawTransforms.
832 DCHECK(rootLayer->renderSurface());
[email protected]94f206c12012-08-25 00:09:14833}
834
[email protected]aad0a0072012-11-01 18:15:58835void 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:14836{
[email protected]aad0a0072012-11-01 18:15:58837 gfx::Rect totalDrawableContentRect;
[email protected]94f206c12012-08-25 00:09:14838 WebTransformationMatrix identityMatrix;
839 WebTransformationMatrix deviceScaleTransform;
840 deviceScaleTransform.scale(deviceScaleFactor);
[email protected]ecc12622012-10-30 20:45:42841 std::vector<LayerImpl*> dummyLayerList;
[email protected]94f206c12012-08-25 00:09:14842
[email protected]ecc12622012-10-30 20:45:42843 // The root layer's renderSurface should receive the deviceViewport as the initial clipRect.
844 bool subtreeShouldBeClipped = true;
[email protected]aad0a0072012-11-01 18:15:58845 gfx::Rect deviceViewportRect(gfx::Point(), deviceViewportSize);
[email protected]ecc12622012-10-30 20:45:42846
847 // This function should have received a root layer.
848 DCHECK(isRootLayer(rootLayer));
[email protected]94f206c12012-08-25 00:09:14849
[email protected]518ee582012-10-24 18:29:44850 cc::calculateDrawTransformsInternal<LayerImpl, std::vector<LayerImpl*>, RenderSurfaceImpl, LayerSorter>(
[email protected]ecc12622012-10-30 20:45:42851 rootLayer, deviceScaleTransform, identityMatrix, identityMatrix,
852 deviceViewportRect, subtreeShouldBeClipped, 0, renderSurfaceLayerList,
853 dummyLayerList, layerSorter, maxTextureSize,
[email protected]518ee582012-10-24 18:29:44854 deviceScaleFactor, pageScaleFactor, totalDrawableContentRect);
[email protected]ecc12622012-10-30 20:45:42855
856 // The dummy layer list should not have been used.
857 DCHECK(dummyLayerList.size() == 0);
858 // A root layer renderSurface should always exist after calculateDrawTransforms.
859 DCHECK(rootLayer->renderSurface());
[email protected]94f206c12012-08-25 00:09:14860}
861
[email protected]d455d552012-11-02 00:19:06862static bool pointHitsRect(const gfx::PointF& screenSpacePoint, const WebTransformationMatrix& localSpaceToScreenSpaceTransform, gfx::RectF localSpaceRect)
[email protected]94f206c12012-08-25 00:09:14863{
864 // If the transform is not invertible, then assume that this point doesn't hit this rect.
865 if (!localSpaceToScreenSpaceTransform.isInvertible())
866 return false;
867
868 // Transform the hit test point from screen space to the local space of the given rect.
869 bool clipped = false;
[email protected]d455d552012-11-02 00:19:06870 gfx::PointF hitTestPointInLocalSpace = MathUtil::projectPoint(localSpaceToScreenSpaceTransform.inverse(), screenSpacePoint, clipped);
[email protected]94f206c12012-08-25 00:09:14871
872 // If projectPoint could not project to a valid value, then we assume that this point doesn't hit this rect.
873 if (clipped)
874 return false;
875
[email protected]aad0a0072012-11-01 18:15:58876 return localSpaceRect.Contains(hitTestPointInLocalSpace);
[email protected]94f206c12012-08-25 00:09:14877}
878
[email protected]d455d552012-11-02 00:19:06879static bool pointIsClippedBySurfaceOrClipRect(const gfx::PointF& screenSpacePoint, LayerImpl* layer)
[email protected]94f206c12012-08-25 00:09:14880{
[email protected]96baf3e2012-10-22 23:09:55881 LayerImpl* currentLayer = layer;
[email protected]94f206c12012-08-25 00:09:14882
883 // Walk up the layer tree and hit-test any renderSurfaces and any layer clipRects that are active.
884 while (currentLayer) {
[email protected]01527732012-10-19 18:16:19885 if (currentLayer->renderSurface() && !pointHitsRect(screenSpacePoint, currentLayer->renderSurface()->screenSpaceTransform(), currentLayer->renderSurface()->contentRect()))
[email protected]94f206c12012-08-25 00:09:14886 return true;
887
888 // Note that drawableContentRects are actually in targetSurface space, so the transform we
889 // have to provide is the target surface's screenSpaceTransform.
[email protected]96baf3e2012-10-22 23:09:55890 LayerImpl* renderTarget = currentLayer->renderTarget();
[email protected]01527732012-10-19 18:16:19891 if (layerClipsSubtree(currentLayer) && !pointHitsRect(screenSpacePoint, renderTarget->renderSurface()->screenSpaceTransform(), currentLayer->drawableContentRect()))
[email protected]94f206c12012-08-25 00:09:14892 return true;
893
894 currentLayer = currentLayer->parent();
895 }
896
897 // If we have finished walking all ancestors without having already exited, then the point is not clipped by any ancestors.
898 return false;
899}
900
[email protected]d455d552012-11-02 00:19:06901LayerImpl* LayerTreeHostCommon::findLayerThatIsHitByPoint(const gfx::PointF& screenSpacePoint, std::vector<LayerImpl*>& renderSurfaceLayerList)
[email protected]94f206c12012-08-25 00:09:14902{
[email protected]96baf3e2012-10-22 23:09:55903 LayerImpl* foundLayer = 0;
[email protected]94f206c12012-08-25 00:09:14904
[email protected]96baf3e2012-10-22 23:09:55905 typedef LayerIterator<LayerImpl, std::vector<LayerImpl*>, RenderSurfaceImpl, LayerIteratorActions::FrontToBack> LayerIteratorType;
906 LayerIteratorType end = LayerIteratorType::end(&renderSurfaceLayerList);
[email protected]94f206c12012-08-25 00:09:14907
[email protected]96baf3e2012-10-22 23:09:55908 for (LayerIteratorType it = LayerIteratorType::begin(&renderSurfaceLayerList); it != end; ++it) {
[email protected]94f206c12012-08-25 00:09:14909 // We don't want to consider renderSurfaces for hit testing.
910 if (!it.representsItself())
911 continue;
912
[email protected]96baf3e2012-10-22 23:09:55913 LayerImpl* currentLayer = (*it);
[email protected]94f206c12012-08-25 00:09:14914
[email protected]aad0a0072012-11-01 18:15:58915 gfx::RectF contentRect(gfx::PointF(), currentLayer->contentBounds());
[email protected]01527732012-10-19 18:16:19916 if (!pointHitsRect(screenSpacePoint, currentLayer->screenSpaceTransform(), contentRect))
[email protected]94f206c12012-08-25 00:09:14917 continue;
918
919 // At this point, we think the point does hit the layer, but we need to walk up
920 // the parents to ensure that the layer was not clipped in such a way that the
921 // hit point actually should not hit the layer.
[email protected]01527732012-10-19 18:16:19922 if (pointIsClippedBySurfaceOrClipRect(screenSpacePoint, currentLayer))
[email protected]94f206c12012-08-25 00:09:14923 continue;
924
925 foundLayer = currentLayer;
926 break;
927 }
928
[email protected]01527732012-10-19 18:16:19929 // 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:14930 return foundLayer;
931}
932
[email protected]bc5e77c2012-11-05 20:00:49933} // namespace cc