blob: c05ff659cbd81d985ae5d8891ab70694cc851726 [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.
621 if (ancestorClipsSubtree)
622 renderSurface->setClipRect(clipRectFromAncestor);
623 else
[email protected]aad0a0072012-11-01 18:15:58624 renderSurface->setClipRect(gfx::Rect());
[email protected]9bab0bb2012-10-08 19:11:58625
[email protected]94f206c12012-08-25 00:09:14626 renderSurface->setNearestAncestorThatMovesPixels(nearestAncestorThatMovesPixels);
627
[email protected]d58499a2012-10-09 22:27:47628 renderSurfaceLayerList.push_back(layer);
[email protected]94f206c12012-08-25 00:09:14629 } else {
[email protected]ecc12622012-10-30 20:45:42630 DCHECK(layer->parent());
631
[email protected]94f206c12012-08-25 00:09:14632 layer->setDrawTransform(drawTransform);
633 layer->setDrawTransformIsAnimating(animatingTransformToTarget);
634 layer->setScreenSpaceTransformIsAnimating(animatingTransformToScreen);
635 sublayerMatrix = combinedTransform;
636
637 layer->setDrawOpacity(drawOpacity);
638 layer->setDrawOpacityIsAnimating(drawOpacityIsAnimating);
639
[email protected]ecc12622012-10-30 20:45:42640 layer->clearRenderSurface();
[email protected]94f206c12012-08-25 00:09:14641
[email protected]ecc12622012-10-30 20:45:42642 // Layers without renderSurfaces directly inherit the ancestor's clip status.
643 subtreeShouldBeClipped = ancestorClipsSubtree;
644 if (ancestorClipsSubtree)
645 clipRectForSubtree = clipRectFromAncestor;
[email protected]94f206c12012-08-25 00:09:14646
[email protected]ecc12622012-10-30 20:45:42647 // Layers that are not their own renderTarget will render into the target of their nearest ancestor.
648 layer->setRenderTarget(layer->parent()->renderTarget());
[email protected]94f206c12012-08-25 00:09:14649 }
650
[email protected]aad0a0072012-11-01 18:15:58651 gfx::Rect rectInTargetSpace = ToEnclosingRect(MathUtil::mapClippedRect(layer->drawTransform(), contentRect));
[email protected]94f206c12012-08-25 00:09:14652
653 if (layerClipsSubtree(layer)) {
654 subtreeShouldBeClipped = true;
655 if (ancestorClipsSubtree && !layer->renderSurface()) {
656 clipRectForSubtree = clipRectFromAncestor;
[email protected]aad0a0072012-11-01 18:15:58657 clipRectForSubtree.Intersect(rectInTargetSpace);
[email protected]94f206c12012-08-25 00:09:14658 } else
659 clipRectForSubtree = rectInTargetSpace;
660 }
661
662 // Flatten to 2D if the layer doesn't preserve 3D.
663 if (!layer->preserves3D())
[email protected]96baf3e2012-10-22 23:09:55664 MathUtil::flattenTransformTo2d(sublayerMatrix);
[email protected]94f206c12012-08-25 00:09:14665
666 // Apply the sublayer transform at the center of the layer.
[email protected]b8d3c022012-10-08 17:38:04667 sublayerMatrix.translate(0.5 * bounds.width(), 0.5 * bounds.height());
[email protected]94f206c12012-08-25 00:09:14668 sublayerMatrix.multiply(layer->sublayerTransform());
[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
671 LayerList& descendants = (layer->renderSurface() ? layer->renderSurface()->layerList() : layerList);
672
673 // Any layers that are appended after this point are in the layer's subtree and should be included in the sorting process.
674 unsigned sortingStartIndex = descendants.size();
675
676 if (!layerShouldBeSkipped(layer))
[email protected]d58499a2012-10-09 22:27:47677 descendants.push_back(layer);
[email protected]94f206c12012-08-25 00:09:14678
679 WebTransformationMatrix nextScrollCompensationMatrix = computeScrollCompensationMatrixForChildren(layer, parentMatrix, currentScrollCompensationMatrix);;
680
[email protected]aad0a0072012-11-01 18:15:58681 gfx::Rect accumulatedDrawableContentRectOfChildren;
[email protected]94f206c12012-08-25 00:09:14682 for (size_t i = 0; i < layer->children().size(); ++i) {
[email protected]96baf3e2012-10-22 23:09:55683 LayerType* child = LayerTreeHostCommon::getChildAsRawPtr(layer->children(), i);
[email protected]aad0a0072012-11-01 18:15:58684 gfx::Rect drawableContentRectOfChildSubtree;
[email protected]ecc12622012-10-30 20:45:42685 calculateDrawTransformsInternal<LayerType, LayerList, RenderSurfaceType, LayerSorter>(child, sublayerMatrix, nextHierarchyMatrix, nextScrollCompensationMatrix,
[email protected]94f206c12012-08-25 00:09:14686 clipRectForSubtree, subtreeShouldBeClipped, nearestAncestorThatMovesPixels,
[email protected]518ee582012-10-24 18:29:44687 renderSurfaceLayerList, descendants, layerSorter, maxTextureSize, deviceScaleFactor, pageScaleFactor, drawableContentRectOfChildSubtree);
[email protected]aad0a0072012-11-01 18:15:58688 if (!drawableContentRectOfChildSubtree.IsEmpty()) {
689 accumulatedDrawableContentRectOfChildren.Union(drawableContentRectOfChildSubtree);
[email protected]94f206c12012-08-25 00:09:14690 if (child->renderSurface())
[email protected]d58499a2012-10-09 22:27:47691 descendants.push_back(child);
[email protected]94f206c12012-08-25 00:09:14692 }
693 }
694
695 // Compute the total drawableContentRect for this subtree (the rect is in targetSurface space)
[email protected]aad0a0072012-11-01 18:15:58696 gfx::Rect localDrawableContentRectOfSubtree = accumulatedDrawableContentRectOfChildren;
[email protected]94f206c12012-08-25 00:09:14697 if (layer->drawsContent())
[email protected]aad0a0072012-11-01 18:15:58698 localDrawableContentRectOfSubtree.Union(rectInTargetSpace);
[email protected]94f206c12012-08-25 00:09:14699 if (subtreeShouldBeClipped)
[email protected]aad0a0072012-11-01 18:15:58700 localDrawableContentRectOfSubtree.Intersect(clipRectForSubtree);
[email protected]94f206c12012-08-25 00:09:14701
702 // Compute the layer's drawable content rect (the rect is in targetSurface space)
[email protected]aad0a0072012-11-01 18:15:58703 gfx::Rect drawableContentRectOfLayer = rectInTargetSpace;
[email protected]94f206c12012-08-25 00:09:14704 if (subtreeShouldBeClipped)
[email protected]aad0a0072012-11-01 18:15:58705 drawableContentRectOfLayer.Intersect(clipRectForSubtree);
[email protected]94f206c12012-08-25 00:09:14706 layer->setDrawableContentRect(drawableContentRectOfLayer);
707
[email protected]9bab0bb2012-10-08 19:11:58708 // Compute the layer's visible content rect (the rect is in content space)
[email protected]aad0a0072012-11-01 18:15:58709 gfx::Rect visibleContentRectOfLayer = calculateVisibleContentRect(layer);
[email protected]9bab0bb2012-10-08 19:11:58710 layer->setVisibleContentRect(visibleContentRectOfLayer);
711
[email protected]94f206c12012-08-25 00:09:14712 // Compute the remaining properties for the render surface, if the layer has one.
[email protected]ecc12622012-10-30 20:45:42713 if (isRootLayer(layer)) {
714 // The root layer's surface's contentRect is always the entire viewport.
715 DCHECK(layer->renderSurface());
716 layer->renderSurface()->setContentRect(clipRectFromAncestor);
717 } else if (layer->renderSurface() && !isRootLayer(layer)) {
[email protected]94f206c12012-08-25 00:09:14718 RenderSurfaceType* renderSurface = layer->renderSurface();
[email protected]aad0a0072012-11-01 18:15:58719 gfx::Rect clippedContentRect = localDrawableContentRectOfSubtree;
[email protected]94f206c12012-08-25 00:09:14720
[email protected]94f206c12012-08-25 00:09:14721 // Don't clip if the layer is reflected as the reflection shouldn't be
722 // clipped. If the layer is animating, then the surface's transform to
723 // its target is not known on the main thread, and we should not use it
724 // to clip.
725 if (!layer->replicaLayer() && transformToParentIsKnown(layer)) {
726 // 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:58727 if (ancestorClipsSubtree && !clippedContentRect.IsEmpty()) {
728 gfx::Rect surfaceClipRect = LayerTreeHostCommon::calculateVisibleRect(renderSurface->clipRect(), clippedContentRect, renderSurface->drawTransform());
729 clippedContentRect.Intersect(surfaceClipRect);
[email protected]94f206c12012-08-25 00:09:14730 }
731 }
732
[email protected]96baf3e2012-10-22 23:09:55733 // The RenderSurfaceImpl backing texture cannot exceed the maximum supported
[email protected]94f206c12012-08-25 00:09:14734 // texture size.
[email protected]aad0a0072012-11-01 18:15:58735 clippedContentRect.set_width(std::min(clippedContentRect.width(), maxTextureSize));
736 clippedContentRect.set_height(std::min(clippedContentRect.height(), maxTextureSize));
[email protected]94f206c12012-08-25 00:09:14737
[email protected]aad0a0072012-11-01 18:15:58738 if (clippedContentRect.IsEmpty())
[email protected]7d929c02012-09-20 17:26:57739 renderSurface->clearLayerLists();
[email protected]94f206c12012-08-25 00:09:14740
741 renderSurface->setContentRect(clippedContentRect);
[email protected]518ee582012-10-24 18:29:44742
743 // The owning layer's screenSpaceTransform has a scale from content to layer space which we need to undo and
744 // replace with a scale from the surface's subtree into layer space.
745 WebTransformationMatrix screenSpaceTransform = layer->screenSpaceTransform();
[email protected]f89f5632012-11-14 23:34:45746 screenSpaceTransform.scaleNonUniform(layer->contentsScaleX(), layer->contentsScaleY());
[email protected]518ee582012-10-24 18:29:44747 screenSpaceTransform.scaleNonUniform(1 / renderSurfaceSublayerScale.x(), 1 / renderSurfaceSublayerScale.y());
748 renderSurface->setScreenSpaceTransform(screenSpaceTransform);
[email protected]94f206c12012-08-25 00:09:14749
750 if (layer->replicaLayer()) {
751 WebTransformationMatrix surfaceOriginToReplicaOriginTransform;
[email protected]518ee582012-10-24 18:29:44752 surfaceOriginToReplicaOriginTransform.scaleNonUniform(renderSurfaceSublayerScale.x(), renderSurfaceSublayerScale.y());
[email protected]94f206c12012-08-25 00:09:14753 surfaceOriginToReplicaOriginTransform.translate(layer->replicaLayer()->position().x() + layer->replicaLayer()->anchorPoint().x() * bounds.width(),
754 layer->replicaLayer()->position().y() + layer->replicaLayer()->anchorPoint().y() * bounds.height());
755 surfaceOriginToReplicaOriginTransform.multiply(layer->replicaLayer()->transform());
756 surfaceOriginToReplicaOriginTransform.translate(-layer->replicaLayer()->anchorPoint().x() * bounds.width(), -layer->replicaLayer()->anchorPoint().y() * bounds.height());
[email protected]518ee582012-10-24 18:29:44757 surfaceOriginToReplicaOriginTransform.scaleNonUniform(1 / renderSurfaceSublayerScale.x(), 1 / renderSurfaceSublayerScale.y());
[email protected]94f206c12012-08-25 00:09:14758
759 // Compute the replica's "originTransform" that maps from the replica's origin space to the target surface origin space.
760 WebTransformationMatrix replicaOriginTransform = layer->renderSurface()->drawTransform() * surfaceOriginToReplicaOriginTransform;
761 renderSurface->setReplicaDrawTransform(replicaOriginTransform);
762
763 // Compute the replica's "screenSpaceTransform" that maps from the replica's origin space to the screen's origin space.
764 WebTransformationMatrix replicaScreenSpaceTransform = layer->renderSurface()->screenSpaceTransform() * surfaceOriginToReplicaOriginTransform;
765 renderSurface->setReplicaScreenSpaceTransform(replicaScreenSpaceTransform);
766 }
767
768 // If a render surface has no layer list, then it and none of its children needed to get drawn.
769 if (!layer->renderSurface()->layerList().size()) {
770 // FIXME: Originally we asserted that this layer was already at the end of the
771 // list, and only needed to remove that layer. For now, we remove the
772 // entire subtree of surfaces to fix a crash bug. The root cause is
773 // https://bugs.webkit.org/show_bug.cgi?id=74147 and we should be able
774 // to put the original assert after fixing that.
[email protected]d58499a2012-10-09 22:27:47775 while (renderSurfaceLayerList.back() != layer) {
776 renderSurfaceLayerList.back()->clearRenderSurface();
777 renderSurfaceLayerList.pop_back();
[email protected]94f206c12012-08-25 00:09:14778 }
[email protected]1d993172012-10-18 18:15:04779 DCHECK(renderSurfaceLayerList.back() == layer);
[email protected]d58499a2012-10-09 22:27:47780 renderSurfaceLayerList.pop_back();
[email protected]94f206c12012-08-25 00:09:14781 layer->clearRenderSurface();
782 return;
783 }
784 }
785
786 // If neither this layer nor any of its children were added, early out.
787 if (sortingStartIndex == descendants.size())
788 return;
789
790 // If preserves-3d then sort all the descendants in 3D so that they can be
791 // drawn from back to front. If the preserves-3d property is also set on the parent then
792 // skip the sorting as the parent will sort all the descendants anyway.
793 if (descendants.size() && layer->preserves3D() && (!layer->parent() || !layer->parent()->preserves3D()))
[email protected]d58499a2012-10-09 22:27:47794 sortLayers(descendants.begin() + sortingStartIndex, descendants.end(), layerSorter);
[email protected]94f206c12012-08-25 00:09:14795
796 if (layer->renderSurface())
[email protected]aad0a0072012-11-01 18:15:58797 drawableContentRectOfSubtree = gfx::ToEnclosingRect(layer->renderSurface()->drawableContentRect());
[email protected]94f206c12012-08-25 00:09:14798 else
799 drawableContentRectOfSubtree = localDrawableContentRectOfSubtree;
800
[email protected]7d929c02012-09-20 17:26:57801 if (layer->hasContributingDelegatedRenderPasses())
802 layer->renderTarget()->renderSurface()->addContributingDelegatedRenderPassLayer(layer);
[email protected]94f206c12012-08-25 00:09:14803}
804
[email protected]aad0a0072012-11-01 18:15:58805void 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:14806{
[email protected]aad0a0072012-11-01 18:15:58807 gfx::Rect totalDrawableContentRect;
[email protected]94f206c12012-08-25 00:09:14808 WebTransformationMatrix identityMatrix;
809 WebTransformationMatrix deviceScaleTransform;
810 deviceScaleTransform.scale(deviceScaleFactor);
[email protected]ecc12622012-10-30 20:45:42811 std::vector<scoped_refptr<Layer> > dummyLayerList;
[email protected]94f206c12012-08-25 00:09:14812
[email protected]ecc12622012-10-30 20:45:42813 // The root layer's renderSurface should receive the deviceViewport as the initial clipRect.
814 bool subtreeShouldBeClipped = true;
[email protected]aad0a0072012-11-01 18:15:58815 gfx::Rect deviceViewportRect(gfx::Point(), deviceViewportSize);
[email protected]ecc12622012-10-30 20:45:42816
817 // This function should have received a root layer.
818 DCHECK(isRootLayer(rootLayer));
[email protected]94f206c12012-08-25 00:09:14819
[email protected]518ee582012-10-24 18:29:44820 cc::calculateDrawTransformsInternal<Layer, std::vector<scoped_refptr<Layer> >, RenderSurface, void>(
[email protected]ecc12622012-10-30 20:45:42821 rootLayer, deviceScaleTransform, identityMatrix, identityMatrix,
822 deviceViewportRect, subtreeShouldBeClipped, 0, renderSurfaceLayerList,
823 dummyLayerList, 0, maxTextureSize,
[email protected]518ee582012-10-24 18:29:44824 deviceScaleFactor, pageScaleFactor, totalDrawableContentRect);
[email protected]ecc12622012-10-30 20:45:42825
826 // The dummy layer list should not have been used.
827 DCHECK(dummyLayerList.size() == 0);
828 // A root layer renderSurface should always exist after calculateDrawTransforms.
829 DCHECK(rootLayer->renderSurface());
[email protected]94f206c12012-08-25 00:09:14830}
831
[email protected]aad0a0072012-11-01 18:15:58832void 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:14833{
[email protected]aad0a0072012-11-01 18:15:58834 gfx::Rect totalDrawableContentRect;
[email protected]94f206c12012-08-25 00:09:14835 WebTransformationMatrix identityMatrix;
836 WebTransformationMatrix deviceScaleTransform;
837 deviceScaleTransform.scale(deviceScaleFactor);
[email protected]ecc12622012-10-30 20:45:42838 std::vector<LayerImpl*> dummyLayerList;
[email protected]94f206c12012-08-25 00:09:14839
[email protected]ecc12622012-10-30 20:45:42840 // The root layer's renderSurface should receive the deviceViewport as the initial clipRect.
841 bool subtreeShouldBeClipped = true;
[email protected]aad0a0072012-11-01 18:15:58842 gfx::Rect deviceViewportRect(gfx::Point(), deviceViewportSize);
[email protected]ecc12622012-10-30 20:45:42843
844 // This function should have received a root layer.
845 DCHECK(isRootLayer(rootLayer));
[email protected]94f206c12012-08-25 00:09:14846
[email protected]518ee582012-10-24 18:29:44847 cc::calculateDrawTransformsInternal<LayerImpl, std::vector<LayerImpl*>, RenderSurfaceImpl, LayerSorter>(
[email protected]ecc12622012-10-30 20:45:42848 rootLayer, deviceScaleTransform, identityMatrix, identityMatrix,
849 deviceViewportRect, subtreeShouldBeClipped, 0, renderSurfaceLayerList,
850 dummyLayerList, layerSorter, maxTextureSize,
[email protected]518ee582012-10-24 18:29:44851 deviceScaleFactor, pageScaleFactor, totalDrawableContentRect);
[email protected]ecc12622012-10-30 20:45:42852
853 // The dummy layer list should not have been used.
854 DCHECK(dummyLayerList.size() == 0);
855 // A root layer renderSurface should always exist after calculateDrawTransforms.
856 DCHECK(rootLayer->renderSurface());
[email protected]94f206c12012-08-25 00:09:14857}
858
[email protected]d455d552012-11-02 00:19:06859static bool pointHitsRect(const gfx::PointF& screenSpacePoint, const WebTransformationMatrix& localSpaceToScreenSpaceTransform, gfx::RectF localSpaceRect)
[email protected]94f206c12012-08-25 00:09:14860{
861 // If the transform is not invertible, then assume that this point doesn't hit this rect.
862 if (!localSpaceToScreenSpaceTransform.isInvertible())
863 return false;
864
865 // Transform the hit test point from screen space to the local space of the given rect.
866 bool clipped = false;
[email protected]d455d552012-11-02 00:19:06867 gfx::PointF hitTestPointInLocalSpace = MathUtil::projectPoint(localSpaceToScreenSpaceTransform.inverse(), screenSpacePoint, clipped);
[email protected]94f206c12012-08-25 00:09:14868
869 // If projectPoint could not project to a valid value, then we assume that this point doesn't hit this rect.
870 if (clipped)
871 return false;
872
[email protected]aad0a0072012-11-01 18:15:58873 return localSpaceRect.Contains(hitTestPointInLocalSpace);
[email protected]94f206c12012-08-25 00:09:14874}
875
[email protected]2f1acc262012-11-16 21:42:22876static bool pointHitsRegion(gfx::PointF screenSpacePoint, const WebTransformationMatrix& screenSpaceTransform, const Region& layerSpaceRegion, float layerContentScaleX, float layerContentScaleY)
877{
878 // If the transform is not invertible, then assume that this point doesn't hit this region.
879 if (!screenSpaceTransform.isInvertible())
880 return false;
881
882 // Transform the hit test point from screen space to the local space of the given region.
883 bool clipped = false;
884 gfx::PointF hitTestPointInContentSpace = MathUtil::projectPoint(screenSpaceTransform.inverse(), screenSpacePoint, clipped);
885 gfx::PointF hitTestPointInLayerSpace = gfx::ScalePoint(hitTestPointInContentSpace, 1 / layerContentScaleX, 1 / layerContentScaleY);
886
887 // If projectPoint could not project to a valid value, then we assume that this point doesn't hit this region.
888 if (clipped)
889 return false;
890
891 return layerSpaceRegion.Contains(gfx::ToRoundedPoint(hitTestPointInLayerSpace));
892}
893
[email protected]d455d552012-11-02 00:19:06894static bool pointIsClippedBySurfaceOrClipRect(const gfx::PointF& screenSpacePoint, LayerImpl* layer)
[email protected]94f206c12012-08-25 00:09:14895{
[email protected]96baf3e2012-10-22 23:09:55896 LayerImpl* currentLayer = layer;
[email protected]94f206c12012-08-25 00:09:14897
898 // Walk up the layer tree and hit-test any renderSurfaces and any layer clipRects that are active.
899 while (currentLayer) {
[email protected]01527732012-10-19 18:16:19900 if (currentLayer->renderSurface() && !pointHitsRect(screenSpacePoint, currentLayer->renderSurface()->screenSpaceTransform(), currentLayer->renderSurface()->contentRect()))
[email protected]94f206c12012-08-25 00:09:14901 return true;
902
903 // Note that drawableContentRects are actually in targetSurface space, so the transform we
904 // have to provide is the target surface's screenSpaceTransform.
[email protected]96baf3e2012-10-22 23:09:55905 LayerImpl* renderTarget = currentLayer->renderTarget();
[email protected]01527732012-10-19 18:16:19906 if (layerClipsSubtree(currentLayer) && !pointHitsRect(screenSpacePoint, renderTarget->renderSurface()->screenSpaceTransform(), currentLayer->drawableContentRect()))
[email protected]94f206c12012-08-25 00:09:14907 return true;
908
909 currentLayer = currentLayer->parent();
910 }
911
912 // If we have finished walking all ancestors without having already exited, then the point is not clipped by any ancestors.
913 return false;
914}
915
[email protected]d455d552012-11-02 00:19:06916LayerImpl* LayerTreeHostCommon::findLayerThatIsHitByPoint(const gfx::PointF& screenSpacePoint, std::vector<LayerImpl*>& renderSurfaceLayerList)
[email protected]94f206c12012-08-25 00:09:14917{
[email protected]96baf3e2012-10-22 23:09:55918 LayerImpl* foundLayer = 0;
[email protected]94f206c12012-08-25 00:09:14919
[email protected]96baf3e2012-10-22 23:09:55920 typedef LayerIterator<LayerImpl, std::vector<LayerImpl*>, RenderSurfaceImpl, LayerIteratorActions::FrontToBack> LayerIteratorType;
921 LayerIteratorType end = LayerIteratorType::end(&renderSurfaceLayerList);
[email protected]94f206c12012-08-25 00:09:14922
[email protected]96baf3e2012-10-22 23:09:55923 for (LayerIteratorType it = LayerIteratorType::begin(&renderSurfaceLayerList); it != end; ++it) {
[email protected]94f206c12012-08-25 00:09:14924 // We don't want to consider renderSurfaces for hit testing.
925 if (!it.representsItself())
926 continue;
927
[email protected]96baf3e2012-10-22 23:09:55928 LayerImpl* currentLayer = (*it);
[email protected]94f206c12012-08-25 00:09:14929
[email protected]aad0a0072012-11-01 18:15:58930 gfx::RectF contentRect(gfx::PointF(), currentLayer->contentBounds());
[email protected]01527732012-10-19 18:16:19931 if (!pointHitsRect(screenSpacePoint, currentLayer->screenSpaceTransform(), contentRect))
[email protected]94f206c12012-08-25 00:09:14932 continue;
933
934 // At this point, we think the point does hit the layer, but we need to walk up
935 // the parents to ensure that the layer was not clipped in such a way that the
936 // hit point actually should not hit the layer.
[email protected]01527732012-10-19 18:16:19937 if (pointIsClippedBySurfaceOrClipRect(screenSpacePoint, currentLayer))
[email protected]94f206c12012-08-25 00:09:14938 continue;
939
940 foundLayer = currentLayer;
941 break;
942 }
943
[email protected]01527732012-10-19 18:16:19944 // 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:14945 return foundLayer;
946}
947
[email protected]2f1acc262012-11-16 21:42:22948LayerImpl* LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(const gfx::PointF& screenSpacePoint, std::vector<LayerImpl*>& renderSurfaceLayerList)
949{
950 LayerImpl* foundLayer = 0;
951
952 typedef LayerIterator<LayerImpl, std::vector<LayerImpl*>, RenderSurfaceImpl, LayerIteratorActions::FrontToBack> LayerIteratorType;
953 LayerIteratorType end = LayerIteratorType::end(&renderSurfaceLayerList);
954
955 for (LayerIteratorType it = LayerIteratorType::begin(&renderSurfaceLayerList); it != end; ++it) {
956 // We don't want to consider renderSurfaces for hit testing.
957 if (!it.representsItself())
958 continue;
959
960 LayerImpl* currentLayer = (*it);
961
962 if (currentLayer->touchEventHandlerRegion().IsEmpty())
963 continue;
964
965 if (!pointHitsRegion(screenSpacePoint, currentLayer->screenSpaceTransform(), currentLayer->touchEventHandlerRegion(), currentLayer->contentsScaleX(), currentLayer->contentsScaleY()))
966 continue;
967
968 // At this point, we think the point does hit the touch event handler region on the layer, but we need to walk up
969 // the parents to ensure that the layer was not clipped in such a way that the
970 // hit point actually should not hit the layer.
971 if (pointIsClippedBySurfaceOrClipRect(screenSpacePoint, currentLayer))
972 continue;
973
974 foundLayer = currentLayer;
975 break;
976 }
977
978 // This can potentially return 0, which means the screenSpacePoint did not successfully hit test any layers, not even the root layer.
979 return foundLayer;
980}
981
[email protected]bc5e77c2012-11-05 20:00:49982} // namespace cc