blob: 2210894e85570cbeb7f799d5a4490b7ee8a25ce1 [file] [log] [blame]
[email protected]94f206c12012-08-25 00:09:141// Copyright 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]d50c6862012-10-23 02:08:315#include "cc/layer_tree_host_common.h"
[email protected]94f206c12012-08-25 00:09:146
[email protected]c8686a02012-11-27 08:29:007#include <algorithm>
8
[email protected]93698c12012-12-07 00:43:569#include "base/debug/trace_event.h"
[email protected]a8461d82012-10-16 21:11:1410#include "cc/layer.h"
[email protected]d50c6862012-10-23 02:08:3111#include "cc/layer_impl.h"
12#include "cc/layer_iterator.h"
13#include "cc/layer_sorter.h"
[email protected]55a124d02012-10-22 03:07:1314#include "cc/math_util.h"
[email protected]a8461d82012-10-16 21:11:1415#include "cc/render_surface.h"
[email protected]d50c6862012-10-23 02:08:3116#include "cc/render_surface_impl.h"
[email protected]2f1acc262012-11-16 21:42:2217#include "ui/gfx/point_conversions.h"
[email protected]aad0a0072012-11-01 18:15:5818#include "ui/gfx/rect_conversions.h"
[email protected]c8686a02012-11-27 08:29:0019#include "ui/gfx/transform.h"
[email protected]94f206c12012-08-25 00:09:1420
[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]93698c12012-12-07 00:43:5631static void sortLayers(std::vector<Layer*>::iterator first, std::vector<Layer*>::iterator end, LayerSorter* layerSorter)
32{
33 NOTREACHED();
34}
35
36static void sortLayers(std::vector<LayerImpl*>::iterator first, std::vector<LayerImpl*>::iterator end, LayerSorter* layerSorter)
37{
38 DCHECK(layerSorter);
39 TRACE_EVENT0("cc", "layer_tree_host_common::sortLayers");
40 layerSorter->sort(first, end);
41}
42
[email protected]6af5cab72012-12-12 00:49:4743inline gfx::Rect calculateVisibleRectWithCachedLayerRect(const gfx::Rect& targetSurfaceRect, const gfx::Rect& layerBoundRect, const gfx::Rect& layerRectInTargetSpace, const gfx::Transform& transform)
[email protected]94f206c12012-08-25 00:09:1444{
45 // Is this layer fully contained within the target surface?
[email protected]6af5cab72012-12-12 00:49:4746 if (targetSurfaceRect.Contains(layerRectInTargetSpace))
[email protected]94f206c12012-08-25 00:09:1447 return layerBoundRect;
48
49 // If the layer doesn't fill up the entire surface, then find the part of
50 // the surface rect where the layer could be visible. This avoids trying to
51 // project surface rect points that are behind the projection point.
[email protected]aad0a0072012-11-01 18:15:5852 gfx::Rect minimalSurfaceRect = targetSurfaceRect;
[email protected]6af5cab72012-12-12 00:49:4753 minimalSurfaceRect.Intersect(layerRectInTargetSpace);
[email protected]94f206c12012-08-25 00:09:1454
55 // Project the corners of the target surface rect into the layer space.
56 // This bounding rectangle may be larger than it needs to be (being
57 // axis-aligned), but is a reasonable filter on the space to consider.
58 // Non-invertible transforms will create an empty rect here.
[email protected]c8686a02012-11-27 08:29:0059 const gfx::Transform surfaceToLayer = MathUtil::inverse(transform);
[email protected]aad0a0072012-11-01 18:15:5860 gfx::Rect layerRect = gfx::ToEnclosingRect(MathUtil::projectClippedRect(surfaceToLayer, gfx::RectF(minimalSurfaceRect)));
61 layerRect.Intersect(layerBoundRect);
[email protected]94f206c12012-08-25 00:09:1462 return layerRect;
63}
64
[email protected]6af5cab72012-12-12 00:49:4765gfx::Rect LayerTreeHostCommon::calculateVisibleRect(const gfx::Rect& targetSurfaceRect, const gfx::Rect& layerBoundRect, const gfx::Transform& transform)
66{
67 gfx::Rect layerInSurfaceSpace = MathUtil::mapClippedRect(transform, layerBoundRect);
68 return calculateVisibleRectWithCachedLayerRect(targetSurfaceRect, layerBoundRect, layerInSurfaceSpace, transform);
69}
70
[email protected]ecc12622012-10-30 20:45:4271template <typename LayerType>
72static inline bool isRootLayer(LayerType* layer)
73{
74 return !layer->parent();
75}
76
[email protected]94f206c12012-08-25 00:09:1477template<typename LayerType>
78static inline bool layerIsInExisting3DRenderingContext(LayerType* layer)
79{
80 // According to current W3C spec on CSS transforms, a layer is part of an established
81 // 3d rendering context if its parent has transform-style of preserves-3d.
82 return layer->parent() && layer->parent()->preserves3D();
83}
84
85template<typename LayerType>
[email protected]ecc12622012-10-30 20:45:4286static bool isRootLayerOfNewRenderingContext(LayerType* layer)
[email protected]94f206c12012-08-25 00:09:1487{
88 // According to current W3C spec on CSS transforms (Section 6.1), a layer is the
89 // beginning of 3d rendering context if its parent does not have transform-style:
90 // preserve-3d, but this layer itself does.
91 if (layer->parent())
92 return !layer->parent()->preserves3D() && layer->preserves3D();
93
94 return layer->preserves3D();
95}
96
97template<typename LayerType>
98static bool isLayerBackFaceVisible(LayerType* layer)
99{
100 // The current W3C spec on CSS transforms says that backface visibility should be
101 // determined differently depending on whether the layer is in a "3d rendering
102 // context" or not. For Chromium code, we can determine whether we are in a 3d
103 // rendering context by checking if the parent preserves 3d.
104
105 if (layerIsInExisting3DRenderingContext(layer))
[email protected]2c7cd6d2012-11-28 23:49:26106 return layer->drawTransform().IsBackFaceVisible();
[email protected]94f206c12012-08-25 00:09:14107
108 // In this case, either the layer establishes a new 3d rendering context, or is not in
109 // a 3d rendering context at all.
[email protected]2c7cd6d2012-11-28 23:49:26110 return layer->transform().IsBackFaceVisible();
[email protected]94f206c12012-08-25 00:09:14111}
112
113template<typename LayerType>
[email protected]c8686a02012-11-27 08:29:00114static bool isSurfaceBackFaceVisible(LayerType* layer, const gfx::Transform& drawTransform)
[email protected]94f206c12012-08-25 00:09:14115{
116 if (layerIsInExisting3DRenderingContext(layer))
[email protected]2c7cd6d2012-11-28 23:49:26117 return drawTransform.IsBackFaceVisible();
[email protected]94f206c12012-08-25 00:09:14118
[email protected]ecc12622012-10-30 20:45:42119 if (isRootLayerOfNewRenderingContext(layer))
[email protected]2c7cd6d2012-11-28 23:49:26120 return layer->transform().IsBackFaceVisible();
[email protected]94f206c12012-08-25 00:09:14121
122 // If the renderSurface is not part of a new or existing rendering context, then the
123 // layers that contribute to this surface will decide back-face visibility for themselves.
124 return false;
125}
126
127template<typename LayerType>
128static inline bool layerClipsSubtree(LayerType* layer)
129{
130 return layer->masksToBounds() || layer->maskLayer();
131}
132
133template<typename LayerType>
[email protected]6af5cab72012-12-12 00:49:47134static gfx::Rect calculateVisibleContentRect(LayerType* layer, const gfx::Rect& ancestorClipRectInDescendantSurfaceSpace, const gfx::Rect& layerRectInTargetSpace)
[email protected]94f206c12012-08-25 00:09:14135{
[email protected]1d993172012-10-18 18:15:04136 DCHECK(layer->renderTarget());
[email protected]94f206c12012-08-25 00:09:14137
[email protected]9bab0bb2012-10-08 19:11:58138 // Nothing is visible if the layer bounds are empty.
[email protected]aad0a0072012-11-01 18:15:58139 if (!layer->drawsContent() || layer->contentBounds().IsEmpty() || layer->drawableContentRect().IsEmpty())
140 return gfx::Rect();
[email protected]94f206c12012-08-25 00:09:14141
[email protected]6af5cab72012-12-12 00:49:47142 // Compute visible bounds in target surface space.
143 gfx::Rect visibleRectInTargetSurfaceSpace = layer->drawableContentRect();
[email protected]9bab0bb2012-10-08 19:11:58144
[email protected]6af5cab72012-12-12 00:49:47145 if (!layer->renderTarget()->renderSurface()->clipRect().IsEmpty()) {
146 // In this case the target surface does clip layers that contribute to
147 // it. So, we have to convert the current surface's clipRect from its
148 // ancestor surface space to the current (descendant) surface
149 // space. This conversion is done outside this function so that it can
150 // be cached instead of computing it redundantly for every layer.
151 visibleRectInTargetSurfaceSpace.Intersect(ancestorClipRectInDescendantSurfaceSpace);
[email protected]9bab0bb2012-10-08 19:11:58152 }
153
[email protected]6af5cab72012-12-12 00:49:47154 if (visibleRectInTargetSurfaceSpace.IsEmpty())
[email protected]aad0a0072012-11-01 18:15:58155 return gfx::Rect();
[email protected]9bab0bb2012-10-08 19:11:58156
[email protected]6af5cab72012-12-12 00:49:47157 return calculateVisibleRectWithCachedLayerRect(visibleRectInTargetSurfaceSpace, gfx::Rect(gfx::Point(), layer->contentBounds()), layerRectInTargetSpace, layer->drawTransform());
[email protected]94f206c12012-08-25 00:09:14158}
159
[email protected]96baf3e2012-10-22 23:09:55160static inline bool transformToParentIsKnown(LayerImpl*)
[email protected]94f206c12012-08-25 00:09:14161{
162 return true;
163}
164
[email protected]96baf3e2012-10-22 23:09:55165static inline bool transformToParentIsKnown(Layer* layer)
[email protected]94f206c12012-08-25 00:09:14166{
167 return !layer->transformIsAnimating();
168}
169
[email protected]96baf3e2012-10-22 23:09:55170static inline bool transformToScreenIsKnown(LayerImpl*)
[email protected]94f206c12012-08-25 00:09:14171{
172 return true;
173}
174
[email protected]96baf3e2012-10-22 23:09:55175static inline bool transformToScreenIsKnown(Layer* layer)
[email protected]94f206c12012-08-25 00:09:14176{
177 return !layer->screenSpaceTransformIsAnimating();
178}
179
180template<typename LayerType>
181static bool layerShouldBeSkipped(LayerType* layer)
182{
183 // Layers can be skipped if any of these conditions are met.
184 // - does not draw content.
185 // - is transparent
186 // - has empty bounds
187 // - the layer is not double-sided, but its back face is visible.
188 //
189 // Some additional conditions need to be computed at a later point after the recursion is finished.
190 // - the intersection of render surface content and layer clipRect is empty
191 // - the visibleContentRect is empty
192 //
193 // Note, if the layer should not have been drawn due to being fully transparent,
194 // we would have skipped the entire subtree and never made it into this function,
195 // so it is safe to omit this check here.
196
[email protected]aad0a0072012-11-01 18:15:58197 if (!layer->drawsContent() || layer->bounds().IsEmpty())
[email protected]94f206c12012-08-25 00:09:14198 return true;
199
200 LayerType* backfaceTestLayer = layer;
201 if (layer->useParentBackfaceVisibility()) {
[email protected]1d993172012-10-18 18:15:04202 DCHECK(layer->parent());
203 DCHECK(!layer->parent()->useParentBackfaceVisibility());
[email protected]94f206c12012-08-25 00:09:14204 backfaceTestLayer = layer->parent();
205 }
206
207 // 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.
208 if (!backfaceTestLayer->doubleSided() && transformToScreenIsKnown(backfaceTestLayer) && isLayerBackFaceVisible(backfaceTestLayer))
209 return true;
210
211 return false;
212}
213
[email protected]96baf3e2012-10-22 23:09:55214static inline bool subtreeShouldBeSkipped(LayerImpl* layer)
[email protected]94f206c12012-08-25 00:09:14215{
216 // The opacity of a layer always applies to its children (either implicitly
217 // via a render surface or explicitly if the parent preserves 3D), so the
218 // entire subtree can be skipped if this layer is fully transparent.
219 return !layer->opacity();
220}
221
[email protected]96baf3e2012-10-22 23:09:55222static inline bool subtreeShouldBeSkipped(Layer* layer)
[email protected]94f206c12012-08-25 00:09:14223{
224 // If the opacity is being animated then the opacity on the main thread is unreliable
225 // (since the impl thread may be using a different opacity), so it should not be trusted.
226 // In particular, it should not cause the subtree to be skipped.
227 return !layer->opacity() && !layer->opacityIsAnimating();
228}
229
[email protected]6ef21ffc2012-11-28 05:58:54230// Called on each layer that could be drawn after all information from
[email protected]d76806f82012-12-05 21:41:50231// calcDrawProperties has been updated on that layer. May have some false
[email protected]6ef21ffc2012-11-28 05:58:54232// positives (e.g. layers get this called on them but don't actually get drawn).
233static inline void markLayerAsUpdated(LayerImpl* layer)
234{
235 layer->didUpdateTransforms();
[email protected]f6776532012-12-21 20:24:33236
237 // Mask layers don't get this call, so explicitly update them so they can
238 // kick off tile rasterization.
239 if (layer->maskLayer())
240 layer->maskLayer()->didUpdateTransforms();
241 if (layer->replicaLayer()) {
242 layer->replicaLayer()->didUpdateTransforms();
243 if (layer->replicaLayer()->maskLayer())
244 layer->replicaLayer()->maskLayer()->didUpdateTransforms();
245 }
[email protected]6ef21ffc2012-11-28 05:58:54246}
247
248static inline void markLayerAsUpdated(Layer* layer)
249{
250}
251
[email protected]94f206c12012-08-25 00:09:14252template<typename LayerType>
253static bool subtreeShouldRenderToSeparateSurface(LayerType* layer, bool axisAlignedWithRespectToParent)
254{
[email protected]94f206c12012-08-25 00:09:14255 //
[email protected]96baf3e2012-10-22 23:09:55256 // A layer and its descendants should render onto a new RenderSurfaceImpl if any of these rules hold:
[email protected]94f206c12012-08-25 00:09:14257 //
258
[email protected]ecc12622012-10-30 20:45:42259 // The root layer should always have a renderSurface.
260 if (isRootLayer(layer))
261 return true;
262
[email protected]94f206c12012-08-25 00:09:14263 // If we force it.
264 if (layer->forceRenderSurface())
265 return true;
266
267 // If the layer uses a mask.
268 if (layer->maskLayer())
269 return true;
270
271 // If the layer has a reflection.
272 if (layer->replicaLayer())
273 return true;
274
275 // If the layer uses a CSS filter.
[email protected]4000abf2012-10-23 04:45:45276 if (!layer->filters().isEmpty() || !layer->backgroundFilters().isEmpty() || layer->filter())
[email protected]94f206c12012-08-25 00:09:14277 return true;
278
[email protected]b5b1fce2012-12-12 22:49:36279 int numDescendantsThatDrawContent = layer->drawProperties().num_descendants_that_draw_content;
[email protected]ecc12622012-10-30 20:45:42280
[email protected]94f206c12012-08-25 00:09:14281 // If the layer flattens its subtree (i.e. the layer doesn't preserve-3d), but it is
282 // treated as a 3D object by its parent (i.e. parent does preserve-3d).
[email protected]ec2eb5f2012-12-16 04:42:27283 if (layerIsInExisting3DRenderingContext(layer) && !layer->preserves3D() && numDescendantsThatDrawContent > 0) {
284 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostCommon::requireSurface flattening");
[email protected]94f206c12012-08-25 00:09:14285 return true;
[email protected]ec2eb5f2012-12-16 04:42:27286 }
[email protected]94f206c12012-08-25 00:09:14287
288 // If the layer clips its descendants but it is not axis-aligned with respect to its parent.
[email protected]ec2eb5f2012-12-16 04:42:27289 if (layerClipsSubtree(layer) && !axisAlignedWithRespectToParent && numDescendantsThatDrawContent > 0) {
290 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostCommon::requireSurface clipping");
[email protected]94f206c12012-08-25 00:09:14291 return true;
[email protected]ec2eb5f2012-12-16 04:42:27292 }
[email protected]94f206c12012-08-25 00:09:14293
[email protected]b5b1fce2012-12-12 22:49:36294 // If the layer has some translucency and does not have a preserves-3d transform style.
295 // This condition only needs a render surface if two or more layers in the
296 // subtree overlap. But checking layer overlaps is unnecessarily costly so
297 // instead we conservatively create a surface whenever at least two layers
298 // draw content for this subtree.
299 bool atLeastTwoLayersInSubtreeDrawContent = layer->hasDelegatedContent() ||
300 (numDescendantsThatDrawContent > 0 && (layer->drawsContent() || numDescendantsThatDrawContent > 1));
301
[email protected]ec2eb5f2012-12-16 04:42:27302 if (layer->opacity() != 1 && !layer->preserves3D() && atLeastTwoLayersInSubtreeDrawContent) {
303 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostCommon::requireSurface opacity");
[email protected]94f206c12012-08-25 00:09:14304 return true;
[email protected]ec2eb5f2012-12-16 04:42:27305 }
[email protected]94f206c12012-08-25 00:09:14306
307 return false;
308}
309
[email protected]c8686a02012-11-27 08:29:00310gfx::Transform computeScrollCompensationForThisLayer(LayerImpl* scrollingLayer, const gfx::Transform& parentMatrix)
[email protected]94f206c12012-08-25 00:09:14311{
312 // For every layer that has non-zero scrollDelta, we have to compute a transform that can undo the
313 // scrollDelta translation. In particular, we want this matrix to premultiply a fixed-position layer's
314 // parentMatrix, so we design this transform in three steps as follows. The steps described here apply
315 // from right-to-left, so Step 1 would be the right-most matrix:
316 //
317 // Step 1. transform from target surface space to the exact space where scrollDelta is actually applied.
318 // -- this is inverse of the matrix in step 3
319 // Step 2. undo the scrollDelta
320 // -- this is just a translation by scrollDelta.
321 // Step 3. transform back to target surface space.
322 // -- this transform is the "partialLayerOriginTransform" = (parentMatrix * scale(layer->pageScaleDelta()));
323 //
324 // These steps create a matrix that both start and end in targetSurfaceSpace. So this matrix can
325 // pre-multiply any fixed-position layer's drawTransform to undo the scrollDeltas -- as long as
326 // that fixed position layer is fixed onto the same renderTarget as this scrollingLayer.
327 //
328
[email protected]c8686a02012-11-27 08:29:00329 gfx::Transform partialLayerOriginTransform = parentMatrix;
330 partialLayerOriginTransform.PreconcatTransform(scrollingLayer->implTransform());
[email protected]94f206c12012-08-25 00:09:14331
[email protected]c8686a02012-11-27 08:29:00332 gfx::Transform scrollCompensationForThisLayer = partialLayerOriginTransform; // Step 3
333 scrollCompensationForThisLayer.Translate(scrollingLayer->scrollDelta().x(), scrollingLayer->scrollDelta().y()); // Step 2
334 scrollCompensationForThisLayer.PreconcatTransform(MathUtil::inverse(partialLayerOriginTransform)); // Step 1
[email protected]94f206c12012-08-25 00:09:14335 return scrollCompensationForThisLayer;
336}
337
[email protected]c8686a02012-11-27 08:29:00338gfx::Transform computeScrollCompensationMatrixForChildren(Layer* currentLayer, const gfx::Transform& currentParentMatrix, const gfx::Transform& currentScrollCompensation)
[email protected]94f206c12012-08-25 00:09:14339{
[email protected]96baf3e2012-10-22 23:09:55340 // The main thread (i.e. Layer) does not need to worry about scroll compensation.
[email protected]94f206c12012-08-25 00:09:14341 // So we can just return an identity matrix here.
[email protected]c8686a02012-11-27 08:29:00342 return gfx::Transform();
[email protected]94f206c12012-08-25 00:09:14343}
344
[email protected]c8686a02012-11-27 08:29:00345gfx::Transform computeScrollCompensationMatrixForChildren(LayerImpl* layer, const gfx::Transform& parentMatrix, const gfx::Transform& currentScrollCompensationMatrix)
[email protected]94f206c12012-08-25 00:09:14346{
347 // "Total scroll compensation" is the transform needed to cancel out all scrollDelta translations that
348 // occurred since the nearest container layer, even if there are renderSurfaces in-between.
349 //
350 // There are some edge cases to be aware of, that are not explicit in the code:
351 // - A layer that is both a fixed-position and container should not be its own container, instead, that means
352 // it is fixed to an ancestor, and is a container for any fixed-position descendants.
353 // - A layer that is a fixed-position container and has a renderSurface should behave the same as a container
354 // without a renderSurface, the renderSurface is irrelevant in that case.
[email protected]ecc12622012-10-30 20:45:42355 // - A layer that does not have an explicit container is simply fixed to the viewport.
356 // (i.e. the root renderSurface.)
[email protected]94f206c12012-08-25 00:09:14357 // - If the fixed-position layer has its own renderSurface, then the renderSurface is
358 // the one who gets fixed.
359 //
360 // This function needs to be called AFTER layers create their own renderSurfaces.
361 //
362
363 // 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:13364 if (!layer->isContainerForFixedPositionLayers() && layer->scrollDelta().IsZero() && !layer->renderSurface())
[email protected]94f206c12012-08-25 00:09:14365 return currentScrollCompensationMatrix;
366
367 // Start as identity matrix.
[email protected]c8686a02012-11-27 08:29:00368 gfx::Transform nextScrollCompensationMatrix;
[email protected]94f206c12012-08-25 00:09:14369
370 // If this layer is not a container, then it inherits the existing scroll compensations.
371 if (!layer->isContainerForFixedPositionLayers())
372 nextScrollCompensationMatrix = currentScrollCompensationMatrix;
373
374 // If the current layer has a non-zero scrollDelta, then we should compute its local scrollCompensation
375 // and accumulate it to the nextScrollCompensationMatrix.
[email protected]c9c1ebe2012-11-05 20:46:13376 if (!layer->scrollDelta().IsZero()) {
[email protected]c8686a02012-11-27 08:29:00377 gfx::Transform scrollCompensationForThisLayer = computeScrollCompensationForThisLayer(layer, parentMatrix);
378 nextScrollCompensationMatrix.PreconcatTransform(scrollCompensationForThisLayer);
[email protected]94f206c12012-08-25 00:09:14379 }
380
381 // If the layer created its own renderSurface, we have to adjust nextScrollCompensationMatrix.
382 // The adjustment allows us to continue using the scrollCompensation on the next surface.
383 // Step 1 (right-most in the math): transform from the new surface to the original ancestor surface
384 // Step 2: apply the scroll compensation
385 // Step 3: transform back to the new surface.
[email protected]c8686a02012-11-27 08:29:00386 if (layer->renderSurface() && !nextScrollCompensationMatrix.IsIdentity())
387 nextScrollCompensationMatrix = MathUtil::inverse(layer->renderSurface()->drawTransform()) * nextScrollCompensationMatrix * layer->renderSurface()->drawTransform();
[email protected]94f206c12012-08-25 00:09:14388
389 return nextScrollCompensationMatrix;
390}
391
[email protected]344e58d02012-12-16 04:52:53392static inline void updateLayerContentsScale(LayerImpl* layer, const gfx::Transform& combinedTransform, float deviceScaleFactor, float pageScaleFactor, bool animatingTransformToScreen)
393{
394}
[email protected]518ee582012-10-24 18:29:44395
[email protected]c8686a02012-11-27 08:29:00396static inline void updateLayerContentsScale(Layer* layer, const gfx::Transform& combinedTransform, float deviceScaleFactor, float pageScaleFactor, bool animatingTransformToScreen)
[email protected]518ee582012-10-24 18:29:44397{
398 float rasterScale = layer->rasterScale();
399 if (!rasterScale) {
400 rasterScale = 1;
401
[email protected]6a9cff92012-11-08 11:53:26402 if (!animatingTransformToScreen && layer->automaticallyComputeRasterScale()) {
[email protected]1b30e8e2012-12-21 02:59:09403 gfx::Vector2dF transformScale = MathUtil::computeTransform2dScaleComponents(combinedTransform, 0.f);
[email protected]518ee582012-10-24 18:29:44404 float combinedScale = std::max(transformScale.x(), transformScale.y());
405 rasterScale = combinedScale / deviceScaleFactor;
406 if (!layer->boundsContainPageScale())
407 rasterScale /= pageScaleFactor;
[email protected]11ec92972012-11-10 03:06:21408 // Prevent scale factors below 1 from being used or saved.
409 if (rasterScale < 1)
410 rasterScale = 1;
411 else
412 layer->setRasterScale(rasterScale);
[email protected]518ee582012-10-24 18:29:44413 }
414 }
415
416 float contentsScale = rasterScale * deviceScaleFactor;
417 if (!layer->boundsContainPageScale())
418 contentsScale *= pageScaleFactor;
[email protected]344e58d02012-12-16 04:52:53419 layer->calculateContentsScale(
420 contentsScale,
421 &layer->drawProperties().contents_scale_x,
422 &layer->drawProperties().contents_scale_y,
423 &layer->drawProperties().content_bounds);
[email protected]518ee582012-10-24 18:29:44424
425 Layer* maskLayer = layer->maskLayer();
426 if (maskLayer)
[email protected]344e58d02012-12-16 04:52:53427 {
428 maskLayer->calculateContentsScale(
429 contentsScale,
430 &maskLayer->drawProperties().contents_scale_x,
431 &maskLayer->drawProperties().contents_scale_y,
432 &maskLayer->drawProperties().content_bounds);
433 }
[email protected]518ee582012-10-24 18:29:44434
435 Layer* replicaMaskLayer = layer->replicaLayer() ? layer->replicaLayer()->maskLayer() : 0;
436 if (replicaMaskLayer)
[email protected]344e58d02012-12-16 04:52:53437 {
438 replicaMaskLayer->calculateContentsScale(
439 contentsScale,
440 &replicaMaskLayer->drawProperties().contents_scale_x,
441 &replicaMaskLayer->drawProperties().contents_scale_y,
442 &replicaMaskLayer->drawProperties().content_bounds);
443 }
[email protected]518ee582012-10-24 18:29:44444}
445
[email protected]6af5cab72012-12-12 00:49:47446template<typename LayerType, typename LayerList>
447static inline void removeSurfaceForEarlyExit(LayerType* layerToRemove, LayerList& renderSurfaceLayerList)
448{
449 DCHECK(layerToRemove->renderSurface());
450 // Technically, we know that the layer we want to remove should be
451 // at the back of the renderSurfaceLayerList. However, we have had
452 // bugs before that added unnecessary layers here
453 // (https://bugs.webkit.org/show_bug.cgi?id=74147), but that causes
454 // things to crash. So here we proactively remove any additional
455 // layers from the end of the list.
456 while (renderSurfaceLayerList.back() != layerToRemove) {
457 renderSurfaceLayerList.back()->clearRenderSurface();
458 renderSurfaceLayerList.pop_back();
459 }
460 DCHECK(renderSurfaceLayerList.back() == layerToRemove);
461 renderSurfaceLayerList.pop_back();
462 layerToRemove->clearRenderSurface();
463}
464
[email protected]b5b1fce2012-12-12 22:49:36465// Recursively walks the layer tree to compute any information that is needed
466// before doing the main recursion.
467template<typename LayerType>
468static void preCalculateMetaInformation(LayerType* layer)
469{
470 int numDescendantsThatDrawContent = 0;
471
472 for (size_t i = 0; i < layer->children().size(); ++i) {
473 LayerType* childLayer = layer->children()[i];
474 preCalculateMetaInformation<LayerType>(childLayer);
475 numDescendantsThatDrawContent += childLayer->drawsContent() ? 1 : 0;
476 numDescendantsThatDrawContent += childLayer->drawProperties().num_descendants_that_draw_content;
477 }
478
479 layer->drawProperties().num_descendants_that_draw_content = numDescendantsThatDrawContent;
480}
481
[email protected]94f206c12012-08-25 00:09:14482// Recursively walks the layer tree starting at the given node and computes all the
483// necessary transformations, clipRects, render surfaces, etc.
[email protected]93698c12012-12-07 00:43:56484template<typename LayerType, typename LayerList, typename RenderSurfaceType>
[email protected]d76806f82012-12-05 21:41:50485static void calculateDrawPropertiesInternal(LayerType* layer, const gfx::Transform& parentMatrix,
[email protected]c8686a02012-11-27 08:29:00486 const gfx::Transform& fullHierarchyMatrix, const gfx::Transform& currentScrollCompensationMatrix,
[email protected]6af5cab72012-12-12 00:49:47487 const gfx::Rect& clipRectFromAncestor, const gfx::Rect& clipRectFromAncestorInDescendantSpace, bool ancestorClipsSubtree,
[email protected]94f206c12012-08-25 00:09:14488 RenderSurfaceType* nearestAncestorThatMovesPixels, LayerList& renderSurfaceLayerList, LayerList& layerList,
[email protected]10aabcc32012-12-13 09:18:59489 LayerSorter* layerSorter, int maxTextureSize, float deviceScaleFactor, float pageScaleFactor, bool subtreeCanUseLCDText,
490 gfx::Rect& drawableContentRectOfSubtree)
[email protected]94f206c12012-08-25 00:09:14491{
492 // This function computes the new matrix transformations recursively for this
493 // layer and all its descendants. It also computes the appropriate render surfaces.
494 // Some important points to remember:
495 //
496 // 0. Here, transforms are notated in Matrix x Vector order, and in words we describe what
497 // the transform does from left to right.
498 //
499 // 1. In our terminology, the "layer origin" refers to the top-left corner of a layer, and the
500 // positive Y-axis points downwards. This interpretation is valid because the orthographic
501 // projection applied at draw time flips the Y axis appropriately.
502 //
[email protected]aad0a0072012-11-01 18:15:58503 // 2. The anchor point, when given as a PointF object, is specified in "unit layer space",
[email protected]c8686a02012-11-27 08:29:00504 // where the bounds of the layer map to [0, 1]. However, as a Transform object,
[email protected]b8d3c022012-10-08 17:38:04505 // the transform to the anchor point is specified in "layer space", where the bounds
[email protected]94f206c12012-08-25 00:09:14506 // of the layer map to [bounds.width(), bounds.height()].
507 //
508 // 3. Definition of various transforms used:
509 // M[parent] is the parent matrix, with respect to the nearest render surface, passed down recursively.
510 // M[root] is the full hierarchy, with respect to the root, passed down recursively.
511 // Tr[origin] is the translation matrix from the parent's origin to this layer's origin.
512 // Tr[origin2anchor] is the translation from the layer's origin to its anchor point
513 // Tr[origin2center] is the translation from the layer's origin to its center
514 // M[layer] is the layer's matrix (applied at the anchor point)
515 // M[sublayer] is the layer's sublayer transform (applied at the layer's center)
[email protected]b8d3c022012-10-08 17:38:04516 // S[layer2content] is the ratio of a layer's contentBounds() to its bounds().
[email protected]94f206c12012-08-25 00:09:14517 //
518 // Some composite transforms can help in understanding the sequence of transforms:
519 // compositeLayerTransform = Tr[origin2anchor] * M[layer] * Tr[origin2anchor].inverse()
520 // compositeSublayerTransform = Tr[origin2center] * M[sublayer] * Tr[origin2center].inverse()
521 //
522 // In words, the layer transform is applied about the anchor point, and the sublayer transform is
523 // applied about the center of the layer.
524 //
525 // 4. When a layer (or render surface) is drawn, it is drawn into a "target render surface". Therefore the draw
526 // transform does not necessarily transform from screen space to local layer space. Instead, the draw transform
527 // is the transform between the "target render surface space" and local layer space. Note that render surfaces,
528 // except for the root, also draw themselves into a different target render surface, and so their draw
529 // transform and origin transforms are also described with respect to the target.
530 //
531 // Using these definitions, then:
532 //
533 // The draw transform for the layer is:
[email protected]b8d3c022012-10-08 17:38:04534 // M[draw] = M[parent] * Tr[origin] * compositeLayerTransform * S[layer2content]
535 // = M[parent] * Tr[layer->position() + anchor] * M[layer] * Tr[anchor2origin] * S[layer2content]
[email protected]94f206c12012-08-25 00:09:14536 //
537 // Interpreting the math left-to-right, this transforms from the layer's render surface to the origin of the layer in content space.
538 //
539 // The screen space transform is:
[email protected]b8d3c022012-10-08 17:38:04540 // M[screenspace] = M[root] * Tr[origin] * compositeLayerTransform * S[layer2content]
541 // = M[root] * Tr[layer->position() + anchor] * M[layer] * Tr[anchor2origin] * S[layer2content]
[email protected]94f206c12012-08-25 00:09:14542 //
[email protected]655fa7f92012-11-15 00:14:33543 // 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:14544 //
545 // The transform hierarchy that is passed on to children (i.e. the child's parentMatrix) is:
546 // M[parent]_for_child = M[parent] * Tr[origin] * compositeLayerTransform * compositeSublayerTransform
[email protected]b8d3c022012-10-08 17:38:04547 // = M[parent] * Tr[layer->position() + anchor] * M[layer] * Tr[anchor2origin] * compositeSublayerTransform
[email protected]94f206c12012-08-25 00:09:14548 //
549 // and a similar matrix for the full hierarchy with respect to the root.
550 //
551 // Finally, note that the final matrix used by the shader for the layer is P * M[draw] * S . This final product
552 // is computed in drawTexturedQuad(), where:
553 // P is the projection matrix
[email protected]b8d3c022012-10-08 17:38:04554 // S is the scale adjustment (to scale up a canonical quad to the layer's size)
[email protected]94f206c12012-08-25 00:09:14555 //
556 // When a render surface has a replica layer, that layer's transform is used to draw a second copy of the surface.
[email protected]c8686a02012-11-27 08:29:00557 // gfx::Transforms named here are relative to the surface, unless they specify they are relative to the replica layer.
[email protected]94f206c12012-08-25 00:09:14558 //
559 // We will denote a scale by device scale S[deviceScale]
560 //
561 // The render surface draw transform to its target surface origin is:
562 // M[surfaceDraw] = M[owningLayer->Draw]
563 //
564 // The render surface origin transform to its the root (screen space) origin is:
565 // M[surface2root] = M[owningLayer->screenspace] * S[deviceScale].inverse()
566 //
567 // The replica draw transform to its target surface origin is:
568 // M[replicaDraw] = S[deviceScale] * M[surfaceDraw] * Tr[replica->position() + replica->anchor()] * Tr[replica] * Tr[origin2anchor].inverse() * S[contentsScale].inverse()
569 //
570 // The replica draw transform to the root (screen space) origin is:
571 // M[replica2root] = M[surface2root] * Tr[replica->position()] * Tr[replica] * Tr[origin2anchor].inverse()
572 //
573
574 // If we early-exit anywhere in this function, the drawableContentRect of this subtree should be considered empty.
[email protected]aad0a0072012-11-01 18:15:58575 drawableContentRectOfSubtree = gfx::Rect();
[email protected]94f206c12012-08-25 00:09:14576
[email protected]d76806f82012-12-05 21:41:50577 // The root layer cannot skip calcDrawProperties.
[email protected]ecc12622012-10-30 20:45:42578 if (!isRootLayer(layer) && subtreeShouldBeSkipped(layer))
[email protected]94f206c12012-08-25 00:09:14579 return;
580
[email protected]d76806f82012-12-05 21:41:50581 // As this function proceeds, these are the properties for the current
582 // layer that actually get computed. To avoid unnecessary copies
583 // (particularly for matrices), we do computations directly on these values
584 // when possible.
585 DrawProperties<LayerType, RenderSurfaceType>& layerDrawProperties = layer->drawProperties();
586
[email protected]aad0a0072012-11-01 18:15:58587 gfx::Rect clipRectForSubtree;
[email protected]94f206c12012-08-25 00:09:14588 bool subtreeShouldBeClipped = false;
[email protected]498ec6e0e2012-11-30 18:24:57589
[email protected]6af5cab72012-12-12 00:49:47590 // This value is cached on the stack so that we don't have to inverse-project
591 // the surface's clipRect redundantly for every layer. This value is the
592 // same as the surface's clipRect, except that instead of being described
593 // in the target surface space (i.e. the ancestor surface space), it is
594 // described in the current surface space.
595 gfx::Rect clipRectForSubtreeInDescendantSpace;
596
[email protected]d76806f82012-12-05 21:41:50597 float accumulatedDrawOpacity = layer->opacity();
[email protected]10aabcc32012-12-13 09:18:59598 bool animatingOpacityToTarget = layer->opacityIsAnimating();
599 bool animatingOpacityToScreen = animatingOpacityToTarget;
[email protected]498ec6e0e2012-11-30 18:24:57600 if (layer->parent()) {
[email protected]d76806f82012-12-05 21:41:50601 accumulatedDrawOpacity *= layer->parent()->drawOpacity();
[email protected]10aabcc32012-12-13 09:18:59602 animatingOpacityToTarget |= layer->parent()->drawOpacityIsAnimating();
603 animatingOpacityToScreen |= layer->parent()->screenSpaceOpacityIsAnimating();
[email protected]94f206c12012-08-25 00:09:14604 }
605
[email protected]6a9cff92012-11-08 11:53:26606 bool animatingTransformToTarget = layer->transformIsAnimating();
607 bool animatingTransformToScreen = animatingTransformToTarget;
608 if (layer->parent()) {
609 animatingTransformToTarget |= layer->parent()->drawTransformIsAnimating();
610 animatingTransformToScreen |= layer->parent()->screenSpaceTransformIsAnimating();
611 }
612
[email protected]aad0a0072012-11-01 18:15:58613 gfx::Size bounds = layer->bounds();
614 gfx::PointF anchorPoint = layer->anchorPoint();
[email protected]c9c1ebe2012-11-05 20:46:13615 gfx::PointF position = layer->position() - layer->scrollDelta();
[email protected]94f206c12012-08-25 00:09:14616
[email protected]c8686a02012-11-27 08:29:00617 gfx::Transform combinedTransform = parentMatrix;
[email protected]6af5cab72012-12-12 00:49:47618 if (!layer->transform().IsIdentity()) {
619 // LT = Tr[origin] * Tr[origin2anchor]
620 combinedTransform.Translate3d(position.x() + anchorPoint.x() * bounds.width(), position.y() + anchorPoint.y() * bounds.height(), layer->anchorPointZ());
621 // LT = Tr[origin] * Tr[origin2anchor] * M[layer]
622 combinedTransform.PreconcatTransform(layer->transform());
623 // LT = Tr[origin] * Tr[origin2anchor] * M[layer] * Tr[anchor2origin]
624 combinedTransform.Translate3d(-anchorPoint.x() * bounds.width(), -anchorPoint.y() * bounds.height(), -layer->anchorPointZ());
625 } else {
626 combinedTransform.Translate(position.x(), position.y());
627 }
[email protected]94f206c12012-08-25 00:09:14628
[email protected]518ee582012-10-24 18:29:44629 // The layer's contentsSize is determined from the combinedTransform, which then informs the
630 // layer's drawTransform.
[email protected]6a9cff92012-11-08 11:53:26631 updateLayerContentsScale(layer, combinedTransform, deviceScaleFactor, pageScaleFactor, animatingTransformToScreen);
[email protected]518ee582012-10-24 18:29:44632
[email protected]b9ab6d542012-12-04 21:33:06633 // If there is a transformation from the impl thread then it should be at
634 // the start of the combinedTransform, but we don't want it to affect the
635 // computation of contentsScale above.
636 // Note carefully: this is Concat, not Preconcat (implTransform * combinedTransform).
637 combinedTransform.ConcatTransform(layer->implTransform());
[email protected]518ee582012-10-24 18:29:44638
[email protected]94f206c12012-08-25 00:09:14639 if (layer->fixedToContainerLayer()) {
640 // Special case: this layer is a composited fixed-position layer; we need to
641 // explicitly compensate for all ancestors' nonzero scrollDeltas to keep this layer
642 // fixed correctly.
[email protected]b9ab6d542012-12-04 21:33:06643 // Note carefully: this is Concat, not Preconcat (currentScrollCompensation * combinedTransform).
644 combinedTransform.ConcatTransform(currentScrollCompensationMatrix);
[email protected]94f206c12012-08-25 00:09:14645 }
646
647 // The drawTransform that gets computed below is effectively the layer's drawTransform, unless
648 // the layer itself creates a renderSurface. In that case, the renderSurface re-parents the transforms.
[email protected]d76806f82012-12-05 21:41:50649 layerDrawProperties.target_space_transform = combinedTransform;
[email protected]f89f5632012-11-14 23:34:45650 // M[draw] = M[parent] * LT * S[layer2content]
[email protected]d76806f82012-12-05 21:41:50651 layerDrawProperties.target_space_transform.Scale(1.0 / layer->contentsScaleX(), 1.0 / layer->contentsScaleY());
[email protected]94f206c12012-08-25 00:09:14652
653 // layerScreenSpaceTransform represents the transform between root layer's "screen space" and local content space.
[email protected]d76806f82012-12-05 21:41:50654 layerDrawProperties.screen_space_transform = fullHierarchyMatrix;
[email protected]94f206c12012-08-25 00:09:14655 if (!layer->preserves3D())
[email protected]d76806f82012-12-05 21:41:50656 MathUtil::flattenTransformTo2d(layerDrawProperties.screen_space_transform);
657 layerDrawProperties.screen_space_transform.PreconcatTransform(layerDrawProperties.target_space_transform);
[email protected]94f206c12012-08-25 00:09:14658
[email protected]10aabcc32012-12-13 09:18:59659 // Adjusting text AA method during animation may cause repaints, which in-turn causes jank.
660 bool adjustTextAA = !animatingOpacityToScreen && !animatingTransformToScreen;
661 // To avoid color fringing, LCD text should only be used on opaque layers with just integral translation.
662 bool layerCanUseLCDText = subtreeCanUseLCDText &&
663 (accumulatedDrawOpacity == 1.0) &&
664 layerDrawProperties.target_space_transform.IsIdentityOrIntegerTranslation();
665
[email protected]aad0a0072012-11-01 18:15:58666 gfx::RectF contentRect(gfx::PointF(), layer->contentBounds());
[email protected]94f206c12012-08-25 00:09:14667
[email protected]96baf3e2012-10-22 23:09:55668 // fullHierarchyMatrix is the matrix that transforms objects between screen space (except projection matrix) and the most recent RenderSurfaceImpl's space.
669 // nextHierarchyMatrix will only change if this layer uses a new RenderSurfaceImpl, otherwise remains the same.
[email protected]c8686a02012-11-27 08:29:00670 gfx::Transform nextHierarchyMatrix = fullHierarchyMatrix;
671 gfx::Transform sublayerMatrix;
[email protected]94f206c12012-08-25 00:09:14672
[email protected]1b30e8e2012-12-21 02:59:09673 gfx::Vector2dF renderSurfaceSublayerScale = MathUtil::computeTransform2dScaleComponents(combinedTransform, deviceScaleFactor * pageScaleFactor);
[email protected]518ee582012-10-24 18:29:44674
[email protected]2c7cd6d2012-11-28 23:49:26675 if (subtreeShouldRenderToSeparateSurface(layer, combinedTransform.IsScaleOrTranslation())) {
[email protected]94f206c12012-08-25 00:09:14676 // Check back-face visibility before continuing with this surface and its subtree
677 if (!layer->doubleSided() && transformToParentIsKnown(layer) && isSurfaceBackFaceVisible(layer, combinedTransform))
678 return;
679
680 if (!layer->renderSurface())
681 layer->createRenderSurface();
682
683 RenderSurfaceType* renderSurface = layer->renderSurface();
[email protected]7d929c02012-09-20 17:26:57684 renderSurface->clearLayerLists();
[email protected]94f206c12012-08-25 00:09:14685
[email protected]b9ab6d542012-12-04 21:33:06686 // The owning layer's draw transform has a scale from content to layer
687 // space which we do not want; so here we use the combinedTransform
688 // instead of the drawTransform. However, we do need to add a different
689 // scale factor that accounts for the surface's pixel dimensions.
690 combinedTransform.Scale(1 / renderSurfaceSublayerScale.x(), 1 / renderSurfaceSublayerScale.y());
691 renderSurface->setDrawTransform(combinedTransform);
[email protected]518ee582012-10-24 18:29:44692
[email protected]b9ab6d542012-12-04 21:33:06693 // The owning layer's transform was re-parented by the surface, so the layer's new drawTransform
694 // only needs to scale the layer to surface space.
[email protected]d76806f82012-12-05 21:41:50695 layerDrawProperties.target_space_transform.MakeIdentity();
696 layerDrawProperties.target_space_transform.Scale(renderSurfaceSublayerScale.x() / layer->contentsScaleX(), renderSurfaceSublayerScale.y() / layer->contentsScaleY());
[email protected]94f206c12012-08-25 00:09:14697
[email protected]518ee582012-10-24 18:29:44698 // Inside the surface's subtree, we scale everything to the owning layer's scale.
[email protected]94f206c12012-08-25 00:09:14699 // The sublayer matrix transforms centered layer rects into target
700 // surface content space.
[email protected]b9ab6d542012-12-04 21:33:06701 DCHECK(sublayerMatrix.IsIdentity());
[email protected]c8686a02012-11-27 08:29:00702 sublayerMatrix.Scale(renderSurfaceSublayerScale.x(), renderSurfaceSublayerScale.y());
[email protected]94f206c12012-08-25 00:09:14703
704 // The opacity value is moved from the layer to its surface, so that the entire subtree properly inherits opacity.
[email protected]d76806f82012-12-05 21:41:50705 renderSurface->setDrawOpacity(accumulatedDrawOpacity);
[email protected]10aabcc32012-12-13 09:18:59706 renderSurface->setDrawOpacityIsAnimating(animatingOpacityToTarget);
707 animatingOpacityToTarget = false;
[email protected]d76806f82012-12-05 21:41:50708 layerDrawProperties.opacity = 1;
[email protected]10aabcc32012-12-13 09:18:59709 layerDrawProperties.opacity_is_animating = animatingOpacityToTarget;
710 layerDrawProperties.screen_space_opacity_is_animating = animatingOpacityToScreen;
[email protected]94f206c12012-08-25 00:09:14711
712 renderSurface->setTargetSurfaceTransformsAreAnimating(animatingTransformToTarget);
713 renderSurface->setScreenSpaceTransformsAreAnimating(animatingTransformToScreen);
714 animatingTransformToTarget = false;
[email protected]d76806f82012-12-05 21:41:50715 layerDrawProperties.target_space_transform_is_animating = animatingTransformToTarget;
716 layerDrawProperties.screen_space_transform_is_animating = animatingTransformToScreen;
[email protected]94f206c12012-08-25 00:09:14717
718 // Update the aggregate hierarchy matrix to include the transform of the
[email protected]96baf3e2012-10-22 23:09:55719 // newly created RenderSurfaceImpl.
[email protected]c8686a02012-11-27 08:29:00720 nextHierarchyMatrix.PreconcatTransform(renderSurface->drawTransform());
[email protected]94f206c12012-08-25 00:09:14721
722 // The new renderSurface here will correctly clip the entire subtree. So, we do
723 // not need to continue propagating the clipping state further down the tree. This
724 // way, we can avoid transforming clipRects from ancestor target surface space to
725 // current target surface space that could cause more w < 0 headaches.
726 subtreeShouldBeClipped = false;
727
[email protected]9bab0bb2012-10-08 19:11:58728 if (layer->maskLayer()) {
[email protected]d76806f82012-12-05 21:41:50729 DrawProperties<LayerType, RenderSurfaceType>& maskLayerDrawProperties = layer->maskLayer()->drawProperties();
730 maskLayerDrawProperties.render_target = layer;
731 maskLayerDrawProperties.visible_content_rect = gfx::Rect(gfx::Point(), layer->contentBounds());
[email protected]9bab0bb2012-10-08 19:11:58732 }
[email protected]94f206c12012-08-25 00:09:14733
[email protected]9bab0bb2012-10-08 19:11:58734 if (layer->replicaLayer() && layer->replicaLayer()->maskLayer()) {
[email protected]d76806f82012-12-05 21:41:50735 DrawProperties<LayerType, RenderSurfaceType>& replicaMaskDrawProperties = layer->replicaLayer()->maskLayer()->drawProperties();
736 replicaMaskDrawProperties.render_target = layer;
737 replicaMaskDrawProperties.visible_content_rect = gfx::Rect(gfx::Point(), layer->contentBounds());
[email protected]9bab0bb2012-10-08 19:11:58738 }
[email protected]94f206c12012-08-25 00:09:14739
[email protected]4000abf2012-10-23 04:45:45740 // FIXME: make this smarter for the SkImageFilter case (check for
741 // pixel-moving filters)
742 if (layer->filters().hasFilterThatMovesPixels() || layer->filter())
[email protected]94f206c12012-08-25 00:09:14743 nearestAncestorThatMovesPixels = renderSurface;
744
[email protected]9bab0bb2012-10-08 19:11:58745 // The render surface clipRect is expressed in the space where this surface draws, i.e. the same space as clipRectFromAncestor.
[email protected]dc462d782012-11-21 21:43:01746 renderSurface->setIsClipped(ancestorClipsSubtree);
[email protected]6af5cab72012-12-12 00:49:47747 if (ancestorClipsSubtree) {
[email protected]9bab0bb2012-10-08 19:11:58748 renderSurface->setClipRect(clipRectFromAncestor);
[email protected]6af5cab72012-12-12 00:49:47749 clipRectForSubtreeInDescendantSpace = gfx::ToEnclosingRect(MathUtil::projectClippedRect(MathUtil::inverse(renderSurface->drawTransform()), renderSurface->clipRect()));
750 } else {
[email protected]aad0a0072012-11-01 18:15:58751 renderSurface->setClipRect(gfx::Rect());
[email protected]6af5cab72012-12-12 00:49:47752 clipRectForSubtreeInDescendantSpace = clipRectFromAncestorInDescendantSpace;
753 }
[email protected]9bab0bb2012-10-08 19:11:58754
[email protected]94f206c12012-08-25 00:09:14755 renderSurface->setNearestAncestorThatMovesPixels(nearestAncestorThatMovesPixels);
756
[email protected]10aabcc32012-12-13 09:18:59757 // If the new render surface is drawn translucent or with a non-integral translation
758 // then the subtree that gets drawn on this render surface cannot use LCD text.
759 subtreeCanUseLCDText = layerCanUseLCDText;
760
[email protected]d58499a2012-10-09 22:27:47761 renderSurfaceLayerList.push_back(layer);
[email protected]94f206c12012-08-25 00:09:14762 } else {
[email protected]ecc12622012-10-30 20:45:42763 DCHECK(layer->parent());
764
[email protected]d76806f82012-12-05 21:41:50765 // Note: layerDrawProperties.target_space_transform is computed above,
766 // before this if-else statement.
767 layerDrawProperties.target_space_transform_is_animating = animatingTransformToTarget;
768 layerDrawProperties.screen_space_transform_is_animating = animatingTransformToScreen;
769 layerDrawProperties.opacity = accumulatedDrawOpacity;
[email protected]10aabcc32012-12-13 09:18:59770 layerDrawProperties.opacity_is_animating = animatingOpacityToTarget;
771 layerDrawProperties.screen_space_opacity_is_animating = animatingOpacityToScreen;
[email protected]94f206c12012-08-25 00:09:14772 sublayerMatrix = combinedTransform;
773
[email protected]ecc12622012-10-30 20:45:42774 layer->clearRenderSurface();
[email protected]94f206c12012-08-25 00:09:14775
[email protected]ecc12622012-10-30 20:45:42776 // Layers without renderSurfaces directly inherit the ancestor's clip status.
777 subtreeShouldBeClipped = ancestorClipsSubtree;
778 if (ancestorClipsSubtree)
779 clipRectForSubtree = clipRectFromAncestor;
[email protected]94f206c12012-08-25 00:09:14780
[email protected]6af5cab72012-12-12 00:49:47781 // The surface's cached clipRect value propagates regardless of what clipping goes on between layers here.
782 clipRectForSubtreeInDescendantSpace = clipRectFromAncestorInDescendantSpace;
783
[email protected]ecc12622012-10-30 20:45:42784 // Layers that are not their own renderTarget will render into the target of their nearest ancestor.
[email protected]d76806f82012-12-05 21:41:50785 layerDrawProperties.render_target = layer->parent()->renderTarget();
[email protected]94f206c12012-08-25 00:09:14786 }
787
[email protected]10aabcc32012-12-13 09:18:59788 if (adjustTextAA)
789 layerDrawProperties.can_use_lcd_text = layerCanUseLCDText;
790
[email protected]aad0a0072012-11-01 18:15:58791 gfx::Rect rectInTargetSpace = ToEnclosingRect(MathUtil::mapClippedRect(layer->drawTransform(), contentRect));
[email protected]94f206c12012-08-25 00:09:14792
793 if (layerClipsSubtree(layer)) {
794 subtreeShouldBeClipped = true;
795 if (ancestorClipsSubtree && !layer->renderSurface()) {
796 clipRectForSubtree = clipRectFromAncestor;
[email protected]aad0a0072012-11-01 18:15:58797 clipRectForSubtree.Intersect(rectInTargetSpace);
[email protected]94f206c12012-08-25 00:09:14798 } else
799 clipRectForSubtree = rectInTargetSpace;
800 }
801
802 // Flatten to 2D if the layer doesn't preserve 3D.
803 if (!layer->preserves3D())
[email protected]96baf3e2012-10-22 23:09:55804 MathUtil::flattenTransformTo2d(sublayerMatrix);
[email protected]94f206c12012-08-25 00:09:14805
806 // Apply the sublayer transform at the center of the layer.
[email protected]b9ab6d542012-12-04 21:33:06807 if (!layer->sublayerTransform().IsIdentity()) {
808 sublayerMatrix.Translate(0.5 * bounds.width(), 0.5 * bounds.height());
809 sublayerMatrix.PreconcatTransform(layer->sublayerTransform());
810 sublayerMatrix.Translate(-0.5 * bounds.width(), -0.5 * bounds.height());
811 }
[email protected]94f206c12012-08-25 00:09:14812
813 LayerList& descendants = (layer->renderSurface() ? layer->renderSurface()->layerList() : layerList);
814
815 // Any layers that are appended after this point are in the layer's subtree and should be included in the sorting process.
816 unsigned sortingStartIndex = descendants.size();
817
818 if (!layerShouldBeSkipped(layer))
[email protected]d58499a2012-10-09 22:27:47819 descendants.push_back(layer);
[email protected]94f206c12012-08-25 00:09:14820
[email protected]c8686a02012-11-27 08:29:00821 gfx::Transform nextScrollCompensationMatrix = computeScrollCompensationMatrixForChildren(layer, parentMatrix, currentScrollCompensationMatrix);;
[email protected]94f206c12012-08-25 00:09:14822
[email protected]aad0a0072012-11-01 18:15:58823 gfx::Rect accumulatedDrawableContentRectOfChildren;
[email protected]94f206c12012-08-25 00:09:14824 for (size_t i = 0; i < layer->children().size(); ++i) {
[email protected]96baf3e2012-10-22 23:09:55825 LayerType* child = LayerTreeHostCommon::getChildAsRawPtr(layer->children(), i);
[email protected]aad0a0072012-11-01 18:15:58826 gfx::Rect drawableContentRectOfChildSubtree;
[email protected]93698c12012-12-07 00:43:56827 calculateDrawPropertiesInternal<LayerType, LayerList, RenderSurfaceType>(child, sublayerMatrix, nextHierarchyMatrix, nextScrollCompensationMatrix,
[email protected]6af5cab72012-12-12 00:49:47828 clipRectForSubtree, clipRectForSubtreeInDescendantSpace, subtreeShouldBeClipped, nearestAncestorThatMovesPixels,
[email protected]10aabcc32012-12-13 09:18:59829 renderSurfaceLayerList, descendants, layerSorter, maxTextureSize, deviceScaleFactor, pageScaleFactor,
830 subtreeCanUseLCDText, drawableContentRectOfChildSubtree);
[email protected]aad0a0072012-11-01 18:15:58831 if (!drawableContentRectOfChildSubtree.IsEmpty()) {
832 accumulatedDrawableContentRectOfChildren.Union(drawableContentRectOfChildSubtree);
[email protected]94f206c12012-08-25 00:09:14833 if (child->renderSurface())
[email protected]d58499a2012-10-09 22:27:47834 descendants.push_back(child);
[email protected]94f206c12012-08-25 00:09:14835 }
836 }
837
[email protected]6af5cab72012-12-12 00:49:47838 if (layer->renderSurface() && !isRootLayer(layer) && !layer->renderSurface()->layerList().size()) {
839 removeSurfaceForEarlyExit(layer, renderSurfaceLayerList);
840 return;
841 }
842
[email protected]94f206c12012-08-25 00:09:14843 // Compute the total drawableContentRect for this subtree (the rect is in targetSurface space)
[email protected]aad0a0072012-11-01 18:15:58844 gfx::Rect localDrawableContentRectOfSubtree = accumulatedDrawableContentRectOfChildren;
[email protected]94f206c12012-08-25 00:09:14845 if (layer->drawsContent())
[email protected]aad0a0072012-11-01 18:15:58846 localDrawableContentRectOfSubtree.Union(rectInTargetSpace);
[email protected]94f206c12012-08-25 00:09:14847 if (subtreeShouldBeClipped)
[email protected]aad0a0072012-11-01 18:15:58848 localDrawableContentRectOfSubtree.Intersect(clipRectForSubtree);
[email protected]94f206c12012-08-25 00:09:14849
850 // Compute the layer's drawable content rect (the rect is in targetSurface space)
[email protected]d76806f82012-12-05 21:41:50851 layerDrawProperties.drawable_content_rect = rectInTargetSpace;
[email protected]94f206c12012-08-25 00:09:14852 if (subtreeShouldBeClipped)
[email protected]d76806f82012-12-05 21:41:50853 layerDrawProperties.drawable_content_rect.Intersect(clipRectForSubtree);
[email protected]94f206c12012-08-25 00:09:14854
[email protected]dc462d782012-11-21 21:43:01855 // Tell the layer the rect that is clipped by. In theory we could use a
856 // tighter clipRect here (drawableContentRect), but that actually does not
857 // reduce how much would be drawn, and instead it would create unnecessary
858 // changes to scissor state affecting GPU performance.
[email protected]d76806f82012-12-05 21:41:50859 layerDrawProperties.is_clipped = subtreeShouldBeClipped;
[email protected]dc462d782012-11-21 21:43:01860 if (subtreeShouldBeClipped)
[email protected]d76806f82012-12-05 21:41:50861 layerDrawProperties.clip_rect = clipRectForSubtree;
[email protected]dc462d782012-11-21 21:43:01862 else {
863 // Initialize the clipRect to a safe value that will not clip the
864 // layer, just in case clipping is still accidentally used.
[email protected]d76806f82012-12-05 21:41:50865 layerDrawProperties.clip_rect = rectInTargetSpace;
[email protected]dc462d782012-11-21 21:43:01866 }
867
[email protected]9bab0bb2012-10-08 19:11:58868 // Compute the layer's visible content rect (the rect is in content space)
[email protected]6af5cab72012-12-12 00:49:47869 layerDrawProperties.visible_content_rect = calculateVisibleContentRect(layer, clipRectForSubtreeInDescendantSpace, rectInTargetSpace);
[email protected]9bab0bb2012-10-08 19:11:58870
[email protected]94f206c12012-08-25 00:09:14871 // Compute the remaining properties for the render surface, if the layer has one.
[email protected]ecc12622012-10-30 20:45:42872 if (isRootLayer(layer)) {
873 // The root layer's surface's contentRect is always the entire viewport.
874 DCHECK(layer->renderSurface());
875 layer->renderSurface()->setContentRect(clipRectFromAncestor);
876 } else if (layer->renderSurface() && !isRootLayer(layer)) {
[email protected]94f206c12012-08-25 00:09:14877 RenderSurfaceType* renderSurface = layer->renderSurface();
[email protected]aad0a0072012-11-01 18:15:58878 gfx::Rect clippedContentRect = localDrawableContentRectOfSubtree;
[email protected]94f206c12012-08-25 00:09:14879
[email protected]94f206c12012-08-25 00:09:14880 // Don't clip if the layer is reflected as the reflection shouldn't be
881 // clipped. If the layer is animating, then the surface's transform to
882 // its target is not known on the main thread, and we should not use it
883 // to clip.
884 if (!layer->replicaLayer() && transformToParentIsKnown(layer)) {
885 // 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:58886 if (ancestorClipsSubtree && !clippedContentRect.IsEmpty()) {
887 gfx::Rect surfaceClipRect = LayerTreeHostCommon::calculateVisibleRect(renderSurface->clipRect(), clippedContentRect, renderSurface->drawTransform());
888 clippedContentRect.Intersect(surfaceClipRect);
[email protected]94f206c12012-08-25 00:09:14889 }
890 }
891
[email protected]96baf3e2012-10-22 23:09:55892 // The RenderSurfaceImpl backing texture cannot exceed the maximum supported
[email protected]94f206c12012-08-25 00:09:14893 // texture size.
[email protected]aad0a0072012-11-01 18:15:58894 clippedContentRect.set_width(std::min(clippedContentRect.width(), maxTextureSize));
895 clippedContentRect.set_height(std::min(clippedContentRect.height(), maxTextureSize));
[email protected]94f206c12012-08-25 00:09:14896
[email protected]6af5cab72012-12-12 00:49:47897 if (clippedContentRect.IsEmpty()) {
[email protected]7d929c02012-09-20 17:26:57898 renderSurface->clearLayerLists();
[email protected]6af5cab72012-12-12 00:49:47899 removeSurfaceForEarlyExit(layer, renderSurfaceLayerList);
900 return;
901 }
902
[email protected]94f206c12012-08-25 00:09:14903 renderSurface->setContentRect(clippedContentRect);
[email protected]518ee582012-10-24 18:29:44904
905 // The owning layer's screenSpaceTransform has a scale from content to layer space which we need to undo and
906 // replace with a scale from the surface's subtree into layer space.
[email protected]c8686a02012-11-27 08:29:00907 gfx::Transform screenSpaceTransform = layer->screenSpaceTransform();
[email protected]b9ab6d542012-12-04 21:33:06908 screenSpaceTransform.Scale(layer->contentsScaleX() / renderSurfaceSublayerScale.x(), layer->contentsScaleY() / renderSurfaceSublayerScale.y());
[email protected]518ee582012-10-24 18:29:44909 renderSurface->setScreenSpaceTransform(screenSpaceTransform);
[email protected]94f206c12012-08-25 00:09:14910
911 if (layer->replicaLayer()) {
[email protected]c8686a02012-11-27 08:29:00912 gfx::Transform surfaceOriginToReplicaOriginTransform;
913 surfaceOriginToReplicaOriginTransform.Scale(renderSurfaceSublayerScale.x(), renderSurfaceSublayerScale.y());
914 surfaceOriginToReplicaOriginTransform.Translate(layer->replicaLayer()->position().x() + layer->replicaLayer()->anchorPoint().x() * bounds.width(),
[email protected]94f206c12012-08-25 00:09:14915 layer->replicaLayer()->position().y() + layer->replicaLayer()->anchorPoint().y() * bounds.height());
[email protected]c8686a02012-11-27 08:29:00916 surfaceOriginToReplicaOriginTransform.PreconcatTransform(layer->replicaLayer()->transform());
917 surfaceOriginToReplicaOriginTransform.Translate(-layer->replicaLayer()->anchorPoint().x() * bounds.width(), -layer->replicaLayer()->anchorPoint().y() * bounds.height());
918 surfaceOriginToReplicaOriginTransform.Scale(1 / renderSurfaceSublayerScale.x(), 1 / renderSurfaceSublayerScale.y());
[email protected]94f206c12012-08-25 00:09:14919
920 // Compute the replica's "originTransform" that maps from the replica's origin space to the target surface origin space.
[email protected]c8686a02012-11-27 08:29:00921 gfx::Transform replicaOriginTransform = layer->renderSurface()->drawTransform() * surfaceOriginToReplicaOriginTransform;
[email protected]94f206c12012-08-25 00:09:14922 renderSurface->setReplicaDrawTransform(replicaOriginTransform);
923
924 // Compute the replica's "screenSpaceTransform" that maps from the replica's origin space to the screen's origin space.
[email protected]c8686a02012-11-27 08:29:00925 gfx::Transform replicaScreenSpaceTransform = layer->renderSurface()->screenSpaceTransform() * surfaceOriginToReplicaOriginTransform;
[email protected]94f206c12012-08-25 00:09:14926 renderSurface->setReplicaScreenSpaceTransform(replicaScreenSpaceTransform);
927 }
[email protected]94f206c12012-08-25 00:09:14928 }
929
[email protected]6ef21ffc2012-11-28 05:58:54930 markLayerAsUpdated(layer);
931
[email protected]94f206c12012-08-25 00:09:14932 // If neither this layer nor any of its children were added, early out.
933 if (sortingStartIndex == descendants.size())
934 return;
935
936 // If preserves-3d then sort all the descendants in 3D so that they can be
937 // drawn from back to front. If the preserves-3d property is also set on the parent then
938 // skip the sorting as the parent will sort all the descendants anyway.
[email protected]93698c12012-12-07 00:43:56939 if (layerSorter && descendants.size() && layer->preserves3D() && (!layer->parent() || !layer->parent()->preserves3D()))
[email protected]d58499a2012-10-09 22:27:47940 sortLayers(descendants.begin() + sortingStartIndex, descendants.end(), layerSorter);
[email protected]94f206c12012-08-25 00:09:14941
942 if (layer->renderSurface())
[email protected]aad0a0072012-11-01 18:15:58943 drawableContentRectOfSubtree = gfx::ToEnclosingRect(layer->renderSurface()->drawableContentRect());
[email protected]94f206c12012-08-25 00:09:14944 else
945 drawableContentRectOfSubtree = localDrawableContentRectOfSubtree;
946
[email protected]7d929c02012-09-20 17:26:57947 if (layer->hasContributingDelegatedRenderPasses())
948 layer->renderTarget()->renderSurface()->addContributingDelegatedRenderPassLayer(layer);
[email protected]94f206c12012-08-25 00:09:14949}
950
[email protected]10aabcc32012-12-13 09:18:59951void LayerTreeHostCommon::calculateDrawProperties(Layer* rootLayer, const gfx::Size& deviceViewportSize, float deviceScaleFactor, float pageScaleFactor, int maxTextureSize, bool canUseLCDText, std::vector<scoped_refptr<Layer> >& renderSurfaceLayerList)
[email protected]94f206c12012-08-25 00:09:14952{
[email protected]aad0a0072012-11-01 18:15:58953 gfx::Rect totalDrawableContentRect;
[email protected]c8686a02012-11-27 08:29:00954 gfx::Transform identityMatrix;
955 gfx::Transform deviceScaleTransform;
956 deviceScaleTransform.Scale(deviceScaleFactor, deviceScaleFactor);
[email protected]ecc12622012-10-30 20:45:42957 std::vector<scoped_refptr<Layer> > dummyLayerList;
[email protected]94f206c12012-08-25 00:09:14958
[email protected]ecc12622012-10-30 20:45:42959 // The root layer's renderSurface should receive the deviceViewport as the initial clipRect.
960 bool subtreeShouldBeClipped = true;
[email protected]aad0a0072012-11-01 18:15:58961 gfx::Rect deviceViewportRect(gfx::Point(), deviceViewportSize);
[email protected]ecc12622012-10-30 20:45:42962
963 // This function should have received a root layer.
964 DCHECK(isRootLayer(rootLayer));
[email protected]94f206c12012-08-25 00:09:14965
[email protected]b5b1fce2012-12-12 22:49:36966 preCalculateMetaInformation<Layer>(rootLayer);
967 calculateDrawPropertiesInternal<Layer, std::vector<scoped_refptr<Layer> >, RenderSurface>(
[email protected]ecc12622012-10-30 20:45:42968 rootLayer, deviceScaleTransform, identityMatrix, identityMatrix,
[email protected]6af5cab72012-12-12 00:49:47969 deviceViewportRect, deviceViewportRect, subtreeShouldBeClipped, 0, renderSurfaceLayerList,
[email protected]ecc12622012-10-30 20:45:42970 dummyLayerList, 0, maxTextureSize,
[email protected]10aabcc32012-12-13 09:18:59971 deviceScaleFactor, pageScaleFactor, canUseLCDText, totalDrawableContentRect);
[email protected]ecc12622012-10-30 20:45:42972
973 // The dummy layer list should not have been used.
974 DCHECK(dummyLayerList.size() == 0);
[email protected]d76806f82012-12-05 21:41:50975 // A root layer renderSurface should always exist after calculateDrawProperties.
[email protected]ecc12622012-10-30 20:45:42976 DCHECK(rootLayer->renderSurface());
[email protected]94f206c12012-08-25 00:09:14977}
978
[email protected]10aabcc32012-12-13 09:18:59979void LayerTreeHostCommon::calculateDrawProperties(LayerImpl* rootLayer, const gfx::Size& deviceViewportSize, float deviceScaleFactor, float pageScaleFactor, int maxTextureSize, bool canUseLCDText, std::vector<LayerImpl*>& renderSurfaceLayerList)
[email protected]94f206c12012-08-25 00:09:14980{
[email protected]aad0a0072012-11-01 18:15:58981 gfx::Rect totalDrawableContentRect;
[email protected]c8686a02012-11-27 08:29:00982 gfx::Transform identityMatrix;
983 gfx::Transform deviceScaleTransform;
984 deviceScaleTransform.Scale(deviceScaleFactor, deviceScaleFactor);
[email protected]ecc12622012-10-30 20:45:42985 std::vector<LayerImpl*> dummyLayerList;
[email protected]93698c12012-12-07 00:43:56986 LayerSorter layerSorter;
987
[email protected]ecc12622012-10-30 20:45:42988 // The root layer's renderSurface should receive the deviceViewport as the initial clipRect.
989 bool subtreeShouldBeClipped = true;
[email protected]aad0a0072012-11-01 18:15:58990 gfx::Rect deviceViewportRect(gfx::Point(), deviceViewportSize);
[email protected]ecc12622012-10-30 20:45:42991
992 // This function should have received a root layer.
993 DCHECK(isRootLayer(rootLayer));
[email protected]94f206c12012-08-25 00:09:14994
[email protected]b5b1fce2012-12-12 22:49:36995 preCalculateMetaInformation<LayerImpl>(rootLayer);
996 calculateDrawPropertiesInternal<LayerImpl, std::vector<LayerImpl*>, RenderSurfaceImpl>(
[email protected]ecc12622012-10-30 20:45:42997 rootLayer, deviceScaleTransform, identityMatrix, identityMatrix,
[email protected]6af5cab72012-12-12 00:49:47998 deviceViewportRect, deviceViewportRect, subtreeShouldBeClipped, 0, renderSurfaceLayerList,
[email protected]93698c12012-12-07 00:43:56999 dummyLayerList, &layerSorter, maxTextureSize,
[email protected]10aabcc32012-12-13 09:18:591000 deviceScaleFactor, pageScaleFactor, canUseLCDText, totalDrawableContentRect);
[email protected]ecc12622012-10-30 20:45:421001
1002 // The dummy layer list should not have been used.
1003 DCHECK(dummyLayerList.size() == 0);
[email protected]d76806f82012-12-05 21:41:501004 // A root layer renderSurface should always exist after calculateDrawProperties.
[email protected]ecc12622012-10-30 20:45:421005 DCHECK(rootLayer->renderSurface());
[email protected]94f206c12012-08-25 00:09:141006}
1007
[email protected]c8686a02012-11-27 08:29:001008static bool pointHitsRect(const gfx::PointF& screenSpacePoint, const gfx::Transform& localSpaceToScreenSpaceTransform, gfx::RectF localSpaceRect)
[email protected]94f206c12012-08-25 00:09:141009{
1010 // If the transform is not invertible, then assume that this point doesn't hit this rect.
[email protected]c8686a02012-11-27 08:29:001011 if (!localSpaceToScreenSpaceTransform.IsInvertible())
[email protected]94f206c12012-08-25 00:09:141012 return false;
1013
1014 // Transform the hit test point from screen space to the local space of the given rect.
1015 bool clipped = false;
[email protected]c8686a02012-11-27 08:29:001016 gfx::PointF hitTestPointInLocalSpace = MathUtil::projectPoint(MathUtil::inverse(localSpaceToScreenSpaceTransform), screenSpacePoint, clipped);
[email protected]94f206c12012-08-25 00:09:141017
1018 // If projectPoint could not project to a valid value, then we assume that this point doesn't hit this rect.
1019 if (clipped)
1020 return false;
1021
[email protected]aad0a0072012-11-01 18:15:581022 return localSpaceRect.Contains(hitTestPointInLocalSpace);
[email protected]94f206c12012-08-25 00:09:141023}
1024
[email protected]c8686a02012-11-27 08:29:001025static bool pointHitsRegion(gfx::PointF screenSpacePoint, const gfx::Transform& screenSpaceTransform, const Region& layerSpaceRegion, float layerContentScaleX, float layerContentScaleY)
[email protected]2f1acc262012-11-16 21:42:221026{
1027 // If the transform is not invertible, then assume that this point doesn't hit this region.
[email protected]c8686a02012-11-27 08:29:001028 if (!screenSpaceTransform.IsInvertible())
[email protected]2f1acc262012-11-16 21:42:221029 return false;
1030
1031 // Transform the hit test point from screen space to the local space of the given region.
1032 bool clipped = false;
[email protected]c8686a02012-11-27 08:29:001033 gfx::PointF hitTestPointInContentSpace = MathUtil::projectPoint(MathUtil::inverse(screenSpaceTransform), screenSpacePoint, clipped);
[email protected]2f1acc262012-11-16 21:42:221034 gfx::PointF hitTestPointInLayerSpace = gfx::ScalePoint(hitTestPointInContentSpace, 1 / layerContentScaleX, 1 / layerContentScaleY);
1035
1036 // If projectPoint could not project to a valid value, then we assume that this point doesn't hit this region.
1037 if (clipped)
1038 return false;
1039
1040 return layerSpaceRegion.Contains(gfx::ToRoundedPoint(hitTestPointInLayerSpace));
1041}
1042
[email protected]d455d552012-11-02 00:19:061043static bool pointIsClippedBySurfaceOrClipRect(const gfx::PointF& screenSpacePoint, LayerImpl* layer)
[email protected]94f206c12012-08-25 00:09:141044{
[email protected]96baf3e2012-10-22 23:09:551045 LayerImpl* currentLayer = layer;
[email protected]94f206c12012-08-25 00:09:141046
1047 // Walk up the layer tree and hit-test any renderSurfaces and any layer clipRects that are active.
1048 while (currentLayer) {
[email protected]01527732012-10-19 18:16:191049 if (currentLayer->renderSurface() && !pointHitsRect(screenSpacePoint, currentLayer->renderSurface()->screenSpaceTransform(), currentLayer->renderSurface()->contentRect()))
[email protected]94f206c12012-08-25 00:09:141050 return true;
1051
1052 // Note that drawableContentRects are actually in targetSurface space, so the transform we
1053 // have to provide is the target surface's screenSpaceTransform.
[email protected]96baf3e2012-10-22 23:09:551054 LayerImpl* renderTarget = currentLayer->renderTarget();
[email protected]01527732012-10-19 18:16:191055 if (layerClipsSubtree(currentLayer) && !pointHitsRect(screenSpacePoint, renderTarget->renderSurface()->screenSpaceTransform(), currentLayer->drawableContentRect()))
[email protected]94f206c12012-08-25 00:09:141056 return true;
1057
1058 currentLayer = currentLayer->parent();
1059 }
1060
1061 // If we have finished walking all ancestors without having already exited, then the point is not clipped by any ancestors.
1062 return false;
1063}
1064
[email protected]76ffd9e2012-12-20 19:12:471065LayerImpl* LayerTreeHostCommon::findLayerThatIsHitByPoint(const gfx::PointF& screenSpacePoint, const std::vector<LayerImpl*>& renderSurfaceLayerList)
[email protected]94f206c12012-08-25 00:09:141066{
[email protected]96baf3e2012-10-22 23:09:551067 LayerImpl* foundLayer = 0;
[email protected]94f206c12012-08-25 00:09:141068
[email protected]96baf3e2012-10-22 23:09:551069 typedef LayerIterator<LayerImpl, std::vector<LayerImpl*>, RenderSurfaceImpl, LayerIteratorActions::FrontToBack> LayerIteratorType;
1070 LayerIteratorType end = LayerIteratorType::end(&renderSurfaceLayerList);
[email protected]94f206c12012-08-25 00:09:141071
[email protected]96baf3e2012-10-22 23:09:551072 for (LayerIteratorType it = LayerIteratorType::begin(&renderSurfaceLayerList); it != end; ++it) {
[email protected]94f206c12012-08-25 00:09:141073 // We don't want to consider renderSurfaces for hit testing.
1074 if (!it.representsItself())
1075 continue;
1076
[email protected]96baf3e2012-10-22 23:09:551077 LayerImpl* currentLayer = (*it);
[email protected]94f206c12012-08-25 00:09:141078
[email protected]aad0a0072012-11-01 18:15:581079 gfx::RectF contentRect(gfx::PointF(), currentLayer->contentBounds());
[email protected]01527732012-10-19 18:16:191080 if (!pointHitsRect(screenSpacePoint, currentLayer->screenSpaceTransform(), contentRect))
[email protected]94f206c12012-08-25 00:09:141081 continue;
1082
1083 // At this point, we think the point does hit the layer, but we need to walk up
1084 // the parents to ensure that the layer was not clipped in such a way that the
1085 // hit point actually should not hit the layer.
[email protected]01527732012-10-19 18:16:191086 if (pointIsClippedBySurfaceOrClipRect(screenSpacePoint, currentLayer))
[email protected]94f206c12012-08-25 00:09:141087 continue;
1088
1089 foundLayer = currentLayer;
1090 break;
1091 }
1092
[email protected]01527732012-10-19 18:16:191093 // 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:141094 return foundLayer;
1095}
1096
[email protected]76ffd9e2012-12-20 19:12:471097LayerImpl* LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(const gfx::PointF& screenSpacePoint, const std::vector<LayerImpl*>& renderSurfaceLayerList)
[email protected]2f1acc262012-11-16 21:42:221098{
1099 LayerImpl* foundLayer = 0;
1100
1101 typedef LayerIterator<LayerImpl, std::vector<LayerImpl*>, RenderSurfaceImpl, LayerIteratorActions::FrontToBack> LayerIteratorType;
1102 LayerIteratorType end = LayerIteratorType::end(&renderSurfaceLayerList);
1103
1104 for (LayerIteratorType it = LayerIteratorType::begin(&renderSurfaceLayerList); it != end; ++it) {
1105 // We don't want to consider renderSurfaces for hit testing.
1106 if (!it.representsItself())
1107 continue;
1108
1109 LayerImpl* currentLayer = (*it);
1110
[email protected]35d2edd22012-12-13 02:19:051111 if (!layerHasTouchEventHandlersAt(screenSpacePoint, currentLayer))
[email protected]2f1acc262012-11-16 21:42:221112 continue;
1113
1114 foundLayer = currentLayer;
1115 break;
1116 }
1117
1118 // This can potentially return 0, which means the screenSpacePoint did not successfully hit test any layers, not even the root layer.
1119 return foundLayer;
1120}
1121
[email protected]35d2edd22012-12-13 02:19:051122bool LayerTreeHostCommon::layerHasTouchEventHandlersAt(const gfx::PointF& screenSpacePoint, LayerImpl* layerImpl) {
1123 if (layerImpl->touchEventHandlerRegion().IsEmpty())
1124 return false;
1125
1126 if (!pointHitsRegion(screenSpacePoint, layerImpl->screenSpaceTransform(), layerImpl->touchEventHandlerRegion(), layerImpl->contentsScaleX(), layerImpl->contentsScaleY()))
1127 return false;;
1128
1129 // At this point, we think the point does hit the touch event handler region on the layer, but we need to walk up
1130 // the parents to ensure that the layer was not clipped in such a way that the
1131 // hit point actually should not hit the layer.
1132 if (pointIsClippedBySurfaceOrClipRect(screenSpacePoint, layerImpl))
1133 return false;
1134
1135 return true;
1136}
[email protected]bc5e77c2012-11-05 20:00:491137} // namespace cc