blob: c28db4087d3f31f8d5c671f1042c957d8e43b441 [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]586543832013-02-20 02:07:0710#include "cc/heads_up_display_layer_impl.h"
[email protected]a8461d82012-10-16 21:11:1411#include "cc/layer.h"
[email protected]d50c6862012-10-23 02:08:3112#include "cc/layer_impl.h"
13#include "cc/layer_iterator.h"
14#include "cc/layer_sorter.h"
[email protected]586543832013-02-20 02:07:0715#include "cc/layer_tree_impl.h"
[email protected]55a124d02012-10-22 03:07:1316#include "cc/math_util.h"
[email protected]a8461d82012-10-16 21:11:1417#include "cc/render_surface.h"
[email protected]d50c6862012-10-23 02:08:3118#include "cc/render_surface_impl.h"
[email protected]2f1acc262012-11-16 21:42:2219#include "ui/gfx/point_conversions.h"
[email protected]aad0a0072012-11-01 18:15:5820#include "ui/gfx/rect_conversions.h"
[email protected]c8686a02012-11-27 08:29:0021#include "ui/gfx/transform.h"
[email protected]94f206c12012-08-25 00:09:1422
[email protected]9c88e562012-09-14 22:21:3023namespace cc {
[email protected]94f206c12012-08-25 00:09:1424
[email protected]96baf3e2012-10-22 23:09:5525ScrollAndScaleSet::ScrollAndScaleSet()
[email protected]493067512012-09-19 23:34:1026{
27}
28
[email protected]96baf3e2012-10-22 23:09:5529ScrollAndScaleSet::~ScrollAndScaleSet()
[email protected]493067512012-09-19 23:34:1030{
31}
32
[email protected]93698c12012-12-07 00:43:5633static void sortLayers(std::vector<Layer*>::iterator first, std::vector<Layer*>::iterator end, LayerSorter* layerSorter)
34{
35 NOTREACHED();
36}
37
38static void sortLayers(std::vector<LayerImpl*>::iterator first, std::vector<LayerImpl*>::iterator end, LayerSorter* layerSorter)
39{
40 DCHECK(layerSorter);
41 TRACE_EVENT0("cc", "layer_tree_host_common::sortLayers");
42 layerSorter->sort(first, end);
43}
44
[email protected]6af5cab72012-12-12 00:49:4745inline 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:1446{
47 // Is this layer fully contained within the target surface?
[email protected]6af5cab72012-12-12 00:49:4748 if (targetSurfaceRect.Contains(layerRectInTargetSpace))
[email protected]94f206c12012-08-25 00:09:1449 return layerBoundRect;
50
51 // If the layer doesn't fill up the entire surface, then find the part of
52 // the surface rect where the layer could be visible. This avoids trying to
53 // project surface rect points that are behind the projection point.
[email protected]aad0a0072012-11-01 18:15:5854 gfx::Rect minimalSurfaceRect = targetSurfaceRect;
[email protected]6af5cab72012-12-12 00:49:4755 minimalSurfaceRect.Intersect(layerRectInTargetSpace);
[email protected]94f206c12012-08-25 00:09:1456
57 // Project the corners of the target surface rect into the layer space.
58 // This bounding rectangle may be larger than it needs to be (being
59 // axis-aligned), but is a reasonable filter on the space to consider.
60 // Non-invertible transforms will create an empty rect here.
[email protected]bda41962013-01-07 18:46:1761
62 gfx::Transform surfaceToLayer(gfx::Transform::kSkipInitialization);
63 if (!transform.GetInverse(&surfaceToLayer)) {
64 // TODO(shawnsingh): Either we need to handle uninvertible transforms
65 // here, or DCHECK that the transform is invertible.
66 }
[email protected]aad0a0072012-11-01 18:15:5867 gfx::Rect layerRect = gfx::ToEnclosingRect(MathUtil::projectClippedRect(surfaceToLayer, gfx::RectF(minimalSurfaceRect)));
68 layerRect.Intersect(layerBoundRect);
[email protected]94f206c12012-08-25 00:09:1469 return layerRect;
70}
71
[email protected]6af5cab72012-12-12 00:49:4772gfx::Rect LayerTreeHostCommon::calculateVisibleRect(const gfx::Rect& targetSurfaceRect, const gfx::Rect& layerBoundRect, const gfx::Transform& transform)
73{
74 gfx::Rect layerInSurfaceSpace = MathUtil::mapClippedRect(transform, layerBoundRect);
75 return calculateVisibleRectWithCachedLayerRect(targetSurfaceRect, layerBoundRect, layerInSurfaceSpace, transform);
76}
77
[email protected]ecc12622012-10-30 20:45:4278template <typename LayerType>
79static inline bool isRootLayer(LayerType* layer)
80{
81 return !layer->parent();
82}
83
[email protected]94f206c12012-08-25 00:09:1484template<typename LayerType>
85static inline bool layerIsInExisting3DRenderingContext(LayerType* layer)
86{
87 // According to current W3C spec on CSS transforms, a layer is part of an established
88 // 3d rendering context if its parent has transform-style of preserves-3d.
89 return layer->parent() && layer->parent()->preserves3D();
90}
91
92template<typename LayerType>
[email protected]ecc12622012-10-30 20:45:4293static bool isRootLayerOfNewRenderingContext(LayerType* layer)
[email protected]94f206c12012-08-25 00:09:1494{
95 // According to current W3C spec on CSS transforms (Section 6.1), a layer is the
96 // beginning of 3d rendering context if its parent does not have transform-style:
97 // preserve-3d, but this layer itself does.
98 if (layer->parent())
99 return !layer->parent()->preserves3D() && layer->preserves3D();
100
101 return layer->preserves3D();
102}
103
104template<typename LayerType>
105static bool isLayerBackFaceVisible(LayerType* layer)
106{
107 // The current W3C spec on CSS transforms says that backface visibility should be
108 // determined differently depending on whether the layer is in a "3d rendering
109 // context" or not. For Chromium code, we can determine whether we are in a 3d
110 // rendering context by checking if the parent preserves 3d.
111
112 if (layerIsInExisting3DRenderingContext(layer))
[email protected]2c7cd6d2012-11-28 23:49:26113 return layer->drawTransform().IsBackFaceVisible();
[email protected]94f206c12012-08-25 00:09:14114
115 // In this case, either the layer establishes a new 3d rendering context, or is not in
116 // a 3d rendering context at all.
[email protected]2c7cd6d2012-11-28 23:49:26117 return layer->transform().IsBackFaceVisible();
[email protected]94f206c12012-08-25 00:09:14118}
119
120template<typename LayerType>
[email protected]c8686a02012-11-27 08:29:00121static bool isSurfaceBackFaceVisible(LayerType* layer, const gfx::Transform& drawTransform)
[email protected]94f206c12012-08-25 00:09:14122{
123 if (layerIsInExisting3DRenderingContext(layer))
[email protected]2c7cd6d2012-11-28 23:49:26124 return drawTransform.IsBackFaceVisible();
[email protected]94f206c12012-08-25 00:09:14125
[email protected]ecc12622012-10-30 20:45:42126 if (isRootLayerOfNewRenderingContext(layer))
[email protected]2c7cd6d2012-11-28 23:49:26127 return layer->transform().IsBackFaceVisible();
[email protected]94f206c12012-08-25 00:09:14128
129 // If the renderSurface is not part of a new or existing rendering context, then the
130 // layers that contribute to this surface will decide back-face visibility for themselves.
131 return false;
132}
133
134template<typename LayerType>
135static inline bool layerClipsSubtree(LayerType* layer)
136{
[email protected]f6a2b742013-01-22 22:55:12137 return layer->masksToBounds() || layer->maskLayer();
[email protected]94f206c12012-08-25 00:09:14138}
139
140template<typename LayerType>
[email protected]6af5cab72012-12-12 00:49:47141static gfx::Rect calculateVisibleContentRect(LayerType* layer, const gfx::Rect& ancestorClipRectInDescendantSurfaceSpace, const gfx::Rect& layerRectInTargetSpace)
[email protected]94f206c12012-08-25 00:09:14142{
[email protected]1d993172012-10-18 18:15:04143 DCHECK(layer->renderTarget());
[email protected]94f206c12012-08-25 00:09:14144
[email protected]9bab0bb2012-10-08 19:11:58145 // Nothing is visible if the layer bounds are empty.
[email protected]aad0a0072012-11-01 18:15:58146 if (!layer->drawsContent() || layer->contentBounds().IsEmpty() || layer->drawableContentRect().IsEmpty())
147 return gfx::Rect();
[email protected]94f206c12012-08-25 00:09:14148
[email protected]6af5cab72012-12-12 00:49:47149 // Compute visible bounds in target surface space.
150 gfx::Rect visibleRectInTargetSurfaceSpace = layer->drawableContentRect();
[email protected]9bab0bb2012-10-08 19:11:58151
[email protected]6af5cab72012-12-12 00:49:47152 if (!layer->renderTarget()->renderSurface()->clipRect().IsEmpty()) {
153 // In this case the target surface does clip layers that contribute to
154 // it. So, we have to convert the current surface's clipRect from its
155 // ancestor surface space to the current (descendant) surface
156 // space. This conversion is done outside this function so that it can
157 // be cached instead of computing it redundantly for every layer.
158 visibleRectInTargetSurfaceSpace.Intersect(ancestorClipRectInDescendantSurfaceSpace);
[email protected]9bab0bb2012-10-08 19:11:58159 }
160
[email protected]6af5cab72012-12-12 00:49:47161 if (visibleRectInTargetSurfaceSpace.IsEmpty())
[email protected]aad0a0072012-11-01 18:15:58162 return gfx::Rect();
[email protected]9bab0bb2012-10-08 19:11:58163
[email protected]6af5cab72012-12-12 00:49:47164 return calculateVisibleRectWithCachedLayerRect(visibleRectInTargetSurfaceSpace, gfx::Rect(gfx::Point(), layer->contentBounds()), layerRectInTargetSpace, layer->drawTransform());
[email protected]94f206c12012-08-25 00:09:14165}
166
[email protected]96baf3e2012-10-22 23:09:55167static inline bool transformToParentIsKnown(LayerImpl*)
[email protected]94f206c12012-08-25 00:09:14168{
169 return true;
170}
171
[email protected]96baf3e2012-10-22 23:09:55172static inline bool transformToParentIsKnown(Layer* layer)
[email protected]94f206c12012-08-25 00:09:14173{
[email protected]9a3e6b72013-02-12 18:31:21174
[email protected]94f206c12012-08-25 00:09:14175 return !layer->transformIsAnimating();
176}
177
[email protected]96baf3e2012-10-22 23:09:55178static inline bool transformToScreenIsKnown(LayerImpl*)
[email protected]94f206c12012-08-25 00:09:14179{
180 return true;
181}
182
[email protected]96baf3e2012-10-22 23:09:55183static inline bool transformToScreenIsKnown(Layer* layer)
[email protected]94f206c12012-08-25 00:09:14184{
185 return !layer->screenSpaceTransformIsAnimating();
186}
187
188template<typename LayerType>
189static bool layerShouldBeSkipped(LayerType* layer)
190{
191 // Layers can be skipped if any of these conditions are met.
192 // - does not draw content.
193 // - is transparent
194 // - has empty bounds
195 // - the layer is not double-sided, but its back face is visible.
196 //
197 // Some additional conditions need to be computed at a later point after the recursion is finished.
198 // - the intersection of render surface content and layer clipRect is empty
199 // - the visibleContentRect is empty
200 //
201 // Note, if the layer should not have been drawn due to being fully transparent,
202 // we would have skipped the entire subtree and never made it into this function,
203 // so it is safe to omit this check here.
204
[email protected]aad0a0072012-11-01 18:15:58205 if (!layer->drawsContent() || layer->bounds().IsEmpty())
[email protected]94f206c12012-08-25 00:09:14206 return true;
207
208 LayerType* backfaceTestLayer = layer;
209 if (layer->useParentBackfaceVisibility()) {
[email protected]1d993172012-10-18 18:15:04210 DCHECK(layer->parent());
211 DCHECK(!layer->parent()->useParentBackfaceVisibility());
[email protected]94f206c12012-08-25 00:09:14212 backfaceTestLayer = layer->parent();
213 }
214
215 // 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.
216 if (!backfaceTestLayer->doubleSided() && transformToScreenIsKnown(backfaceTestLayer) && isLayerBackFaceVisible(backfaceTestLayer))
217 return true;
218
219 return false;
220}
221
[email protected]96baf3e2012-10-22 23:09:55222static inline bool subtreeShouldBeSkipped(LayerImpl* layer)
[email protected]94f206c12012-08-25 00:09:14223{
224 // The opacity of a layer always applies to its children (either implicitly
225 // via a render surface or explicitly if the parent preserves 3D), so the
226 // entire subtree can be skipped if this layer is fully transparent.
227 return !layer->opacity();
228}
229
[email protected]96baf3e2012-10-22 23:09:55230static inline bool subtreeShouldBeSkipped(Layer* layer)
[email protected]94f206c12012-08-25 00:09:14231{
232 // If the opacity is being animated then the opacity on the main thread is unreliable
233 // (since the impl thread may be using a different opacity), so it should not be trusted.
234 // In particular, it should not cause the subtree to be skipped.
235 return !layer->opacity() && !layer->opacityIsAnimating();
236}
237
[email protected]6ef21ffc2012-11-28 05:58:54238// Called on each layer that could be drawn after all information from
[email protected]d76806f82012-12-05 21:41:50239// calcDrawProperties has been updated on that layer. May have some false
[email protected]6ef21ffc2012-11-28 05:58:54240// positives (e.g. layers get this called on them but don't actually get drawn).
[email protected]4c9bb952013-01-27 05:41:18241static inline void updateTilePrioritiesForLayer(LayerImpl* layer)
[email protected]6ef21ffc2012-11-28 05:58:54242{
[email protected]4c9bb952013-01-27 05:41:18243 layer->updateTilePriorities();
[email protected]f6776532012-12-21 20:24:33244
245 // Mask layers don't get this call, so explicitly update them so they can
246 // kick off tile rasterization.
247 if (layer->maskLayer())
[email protected]4c9bb952013-01-27 05:41:18248 layer->maskLayer()->updateTilePriorities();
249 if (layer->replicaLayer() && layer->replicaLayer()->maskLayer())
250 layer->replicaLayer()->maskLayer()->updateTilePriorities();
[email protected]6ef21ffc2012-11-28 05:58:54251}
252
[email protected]4c9bb952013-01-27 05:41:18253static inline void updateTilePrioritiesForLayer(Layer* layer)
[email protected]6ef21ffc2012-11-28 05:58:54254{
255}
256
[email protected]94f206c12012-08-25 00:09:14257template<typename LayerType>
258static bool subtreeShouldRenderToSeparateSurface(LayerType* layer, bool axisAlignedWithRespectToParent)
259{
[email protected]94f206c12012-08-25 00:09:14260 //
[email protected]96baf3e2012-10-22 23:09:55261 // A layer and its descendants should render onto a new RenderSurfaceImpl if any of these rules hold:
[email protected]94f206c12012-08-25 00:09:14262 //
263
[email protected]ecc12622012-10-30 20:45:42264 // The root layer should always have a renderSurface.
265 if (isRootLayer(layer))
266 return true;
267
[email protected]94f206c12012-08-25 00:09:14268 // If we force it.
269 if (layer->forceRenderSurface())
270 return true;
271
272 // If the layer uses a mask.
273 if (layer->maskLayer())
274 return true;
275
276 // If the layer has a reflection.
277 if (layer->replicaLayer())
278 return true;
279
280 // If the layer uses a CSS filter.
[email protected]4000abf2012-10-23 04:45:45281 if (!layer->filters().isEmpty() || !layer->backgroundFilters().isEmpty() || layer->filter())
[email protected]94f206c12012-08-25 00:09:14282 return true;
283
[email protected]b5b1fce2012-12-12 22:49:36284 int numDescendantsThatDrawContent = layer->drawProperties().num_descendants_that_draw_content;
[email protected]ecc12622012-10-30 20:45:42285
[email protected]94f206c12012-08-25 00:09:14286 // If the layer flattens its subtree (i.e. the layer doesn't preserve-3d), but it is
287 // treated as a 3D object by its parent (i.e. parent does preserve-3d).
[email protected]ec2eb5f2012-12-16 04:42:27288 if (layerIsInExisting3DRenderingContext(layer) && !layer->preserves3D() && numDescendantsThatDrawContent > 0) {
289 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostCommon::requireSurface flattening");
[email protected]94f206c12012-08-25 00:09:14290 return true;
[email protected]ec2eb5f2012-12-16 04:42:27291 }
[email protected]94f206c12012-08-25 00:09:14292
293 // If the layer clips its descendants but it is not axis-aligned with respect to its parent.
[email protected]f6a2b742013-01-22 22:55:12294 bool layerClipsExternalContent = layerClipsSubtree(layer) || layer->hasDelegatedContent();
295 if (layerClipsExternalContent && !axisAlignedWithRespectToParent && !layer->drawProperties().descendants_can_clip_selves)
[email protected]aedf4e52013-01-09 23:24:44296 {
[email protected]ec2eb5f2012-12-16 04:42:27297 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostCommon::requireSurface clipping");
[email protected]94f206c12012-08-25 00:09:14298 return true;
[email protected]ec2eb5f2012-12-16 04:42:27299 }
[email protected]94f206c12012-08-25 00:09:14300
[email protected]b5b1fce2012-12-12 22:49:36301 // If the layer has some translucency and does not have a preserves-3d transform style.
302 // This condition only needs a render surface if two or more layers in the
303 // subtree overlap. But checking layer overlaps is unnecessarily costly so
304 // instead we conservatively create a surface whenever at least two layers
305 // draw content for this subtree.
[email protected]9606d842013-01-20 00:38:28306 bool atLeastTwoLayersInSubtreeDrawContent = numDescendantsThatDrawContent > 0 && (layer->drawsContent() || numDescendantsThatDrawContent > 1);
[email protected]b5b1fce2012-12-12 22:49:36307
[email protected]ec2eb5f2012-12-16 04:42:27308 if (layer->opacity() != 1 && !layer->preserves3D() && atLeastTwoLayersInSubtreeDrawContent) {
309 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostCommon::requireSurface opacity");
[email protected]94f206c12012-08-25 00:09:14310 return true;
[email protected]ec2eb5f2012-12-16 04:42:27311 }
[email protected]94f206c12012-08-25 00:09:14312
313 return false;
314}
315
[email protected]c8686a02012-11-27 08:29:00316gfx::Transform computeScrollCompensationForThisLayer(LayerImpl* scrollingLayer, const gfx::Transform& parentMatrix)
[email protected]94f206c12012-08-25 00:09:14317{
318 // For every layer that has non-zero scrollDelta, we have to compute a transform that can undo the
319 // scrollDelta translation. In particular, we want this matrix to premultiply a fixed-position layer's
320 // parentMatrix, so we design this transform in three steps as follows. The steps described here apply
321 // from right-to-left, so Step 1 would be the right-most matrix:
322 //
323 // Step 1. transform from target surface space to the exact space where scrollDelta is actually applied.
324 // -- this is inverse of the matrix in step 3
325 // Step 2. undo the scrollDelta
326 // -- this is just a translation by scrollDelta.
327 // Step 3. transform back to target surface space.
328 // -- this transform is the "partialLayerOriginTransform" = (parentMatrix * scale(layer->pageScaleDelta()));
329 //
330 // These steps create a matrix that both start and end in targetSurfaceSpace. So this matrix can
331 // pre-multiply any fixed-position layer's drawTransform to undo the scrollDeltas -- as long as
332 // that fixed position layer is fixed onto the same renderTarget as this scrollingLayer.
333 //
334
[email protected]c8686a02012-11-27 08:29:00335 gfx::Transform partialLayerOriginTransform = parentMatrix;
336 partialLayerOriginTransform.PreconcatTransform(scrollingLayer->implTransform());
[email protected]94f206c12012-08-25 00:09:14337
[email protected]c8686a02012-11-27 08:29:00338 gfx::Transform scrollCompensationForThisLayer = partialLayerOriginTransform; // Step 3
339 scrollCompensationForThisLayer.Translate(scrollingLayer->scrollDelta().x(), scrollingLayer->scrollDelta().y()); // Step 2
[email protected]bda41962013-01-07 18:46:17340
341 gfx::Transform inversePartialLayerOriginTransform(gfx::Transform::kSkipInitialization);
342 if (!partialLayerOriginTransform.GetInverse(&inversePartialLayerOriginTransform)) {
343 // TODO(shawnsingh): Either we need to handle uninvertible transforms
344 // here, or DCHECK that the transform is invertible.
345 }
346 scrollCompensationForThisLayer.PreconcatTransform(inversePartialLayerOriginTransform); // Step 1
[email protected]94f206c12012-08-25 00:09:14347 return scrollCompensationForThisLayer;
348}
349
[email protected]c8686a02012-11-27 08:29:00350gfx::Transform computeScrollCompensationMatrixForChildren(Layer* currentLayer, const gfx::Transform& currentParentMatrix, const gfx::Transform& currentScrollCompensation)
[email protected]94f206c12012-08-25 00:09:14351{
[email protected]96baf3e2012-10-22 23:09:55352 // The main thread (i.e. Layer) does not need to worry about scroll compensation.
[email protected]94f206c12012-08-25 00:09:14353 // So we can just return an identity matrix here.
[email protected]c8686a02012-11-27 08:29:00354 return gfx::Transform();
[email protected]94f206c12012-08-25 00:09:14355}
356
[email protected]c8686a02012-11-27 08:29:00357gfx::Transform computeScrollCompensationMatrixForChildren(LayerImpl* layer, const gfx::Transform& parentMatrix, const gfx::Transform& currentScrollCompensationMatrix)
[email protected]94f206c12012-08-25 00:09:14358{
359 // "Total scroll compensation" is the transform needed to cancel out all scrollDelta translations that
360 // occurred since the nearest container layer, even if there are renderSurfaces in-between.
361 //
362 // There are some edge cases to be aware of, that are not explicit in the code:
363 // - A layer that is both a fixed-position and container should not be its own container, instead, that means
364 // it is fixed to an ancestor, and is a container for any fixed-position descendants.
365 // - A layer that is a fixed-position container and has a renderSurface should behave the same as a container
366 // without a renderSurface, the renderSurface is irrelevant in that case.
[email protected]ecc12622012-10-30 20:45:42367 // - A layer that does not have an explicit container is simply fixed to the viewport.
368 // (i.e. the root renderSurface.)
[email protected]94f206c12012-08-25 00:09:14369 // - If the fixed-position layer has its own renderSurface, then the renderSurface is
370 // the one who gets fixed.
371 //
372 // This function needs to be called AFTER layers create their own renderSurfaces.
373 //
374
375 // 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:13376 if (!layer->isContainerForFixedPositionLayers() && layer->scrollDelta().IsZero() && !layer->renderSurface())
[email protected]94f206c12012-08-25 00:09:14377 return currentScrollCompensationMatrix;
378
379 // Start as identity matrix.
[email protected]c8686a02012-11-27 08:29:00380 gfx::Transform nextScrollCompensationMatrix;
[email protected]94f206c12012-08-25 00:09:14381
382 // If this layer is not a container, then it inherits the existing scroll compensations.
383 if (!layer->isContainerForFixedPositionLayers())
384 nextScrollCompensationMatrix = currentScrollCompensationMatrix;
385
386 // If the current layer has a non-zero scrollDelta, then we should compute its local scrollCompensation
387 // and accumulate it to the nextScrollCompensationMatrix.
[email protected]c9c1ebe2012-11-05 20:46:13388 if (!layer->scrollDelta().IsZero()) {
[email protected]c8686a02012-11-27 08:29:00389 gfx::Transform scrollCompensationForThisLayer = computeScrollCompensationForThisLayer(layer, parentMatrix);
390 nextScrollCompensationMatrix.PreconcatTransform(scrollCompensationForThisLayer);
[email protected]94f206c12012-08-25 00:09:14391 }
392
393 // If the layer created its own renderSurface, we have to adjust nextScrollCompensationMatrix.
394 // The adjustment allows us to continue using the scrollCompensation on the next surface.
395 // Step 1 (right-most in the math): transform from the new surface to the original ancestor surface
396 // Step 2: apply the scroll compensation
397 // Step 3: transform back to the new surface.
[email protected]bda41962013-01-07 18:46:17398 if (layer->renderSurface() && !nextScrollCompensationMatrix.IsIdentity()) {
399 gfx::Transform inverseSurfaceDrawTransform(gfx::Transform::kSkipInitialization);
400 if (!layer->renderSurface()->drawTransform().GetInverse(&inverseSurfaceDrawTransform)) {
401 // TODO(shawnsingh): Either we need to handle uninvertible transforms
402 // here, or DCHECK that the transform is invertible.
403 }
404 nextScrollCompensationMatrix = inverseSurfaceDrawTransform * nextScrollCompensationMatrix * layer->renderSurface()->drawTransform();
405 }
[email protected]94f206c12012-08-25 00:09:14406
407 return nextScrollCompensationMatrix;
408}
409
[email protected]e0eb7c42013-01-08 00:00:19410template<typename LayerType>
[email protected]650b3faf2013-02-06 01:48:29411static inline void calculateContentsScale(LayerType* layer, float contentsScale, bool animatingTransformToScreen)
[email protected]e0eb7c42013-01-08 00:00:19412{
413 layer->calculateContentsScale(
414 contentsScale,
[email protected]650b3faf2013-02-06 01:48:29415 animatingTransformToScreen,
[email protected]e0eb7c42013-01-08 00:00:19416 &layer->drawProperties().contents_scale_x,
417 &layer->drawProperties().contents_scale_y,
418 &layer->drawProperties().content_bounds);
419
420 LayerType* maskLayer = layer->maskLayer();
421 if (maskLayer)
422 {
423 maskLayer->calculateContentsScale(
424 contentsScale,
[email protected]650b3faf2013-02-06 01:48:29425 animatingTransformToScreen,
[email protected]e0eb7c42013-01-08 00:00:19426 &maskLayer->drawProperties().contents_scale_x,
427 &maskLayer->drawProperties().contents_scale_y,
428 &maskLayer->drawProperties().content_bounds);
429 }
430
431 LayerType* replicaMaskLayer = layer->replicaLayer() ? layer->replicaLayer()->maskLayer() : 0;
432 if (replicaMaskLayer)
433 {
434 replicaMaskLayer->calculateContentsScale(
435 contentsScale,
[email protected]650b3faf2013-02-06 01:48:29436 animatingTransformToScreen,
[email protected]e0eb7c42013-01-08 00:00:19437 &replicaMaskLayer->drawProperties().contents_scale_x,
438 &replicaMaskLayer->drawProperties().contents_scale_y,
439 &replicaMaskLayer->drawProperties().content_bounds);
440 }
441}
442
[email protected]344e58d02012-12-16 04:52:53443static inline void updateLayerContentsScale(LayerImpl* layer, const gfx::Transform& combinedTransform, float deviceScaleFactor, float pageScaleFactor, bool animatingTransformToScreen)
444{
[email protected]e0eb7c42013-01-08 00:00:19445 gfx::Vector2dF transformScale = MathUtil::computeTransform2dScaleComponents(combinedTransform, deviceScaleFactor * pageScaleFactor);
446 float contentsScale = std::max(transformScale.x(), transformScale.y());
[email protected]650b3faf2013-02-06 01:48:29447 calculateContentsScale(layer, contentsScale, animatingTransformToScreen);
[email protected]344e58d02012-12-16 04:52:53448}
[email protected]518ee582012-10-24 18:29:44449
[email protected]c8686a02012-11-27 08:29:00450static inline void updateLayerContentsScale(Layer* layer, const gfx::Transform& combinedTransform, float deviceScaleFactor, float pageScaleFactor, bool animatingTransformToScreen)
[email protected]518ee582012-10-24 18:29:44451{
452 float rasterScale = layer->rasterScale();
[email protected]518ee582012-10-24 18:29:44453
[email protected]d0518202013-02-08 02:06:49454 if (layer->automaticallyComputeRasterScale()) {
455 gfx::Vector2dF transformScale = MathUtil::computeTransform2dScaleComponents(combinedTransform, 0.f);
456 float combinedScale = std::max(transformScale.x(), transformScale.y());
457 float idealRasterScale = combinedScale / deviceScaleFactor;
458 if (!layer->boundsContainPageScale())
459 idealRasterScale /= pageScaleFactor;
460
461 bool needToSetRasterScale = !rasterScale;
462
463 // If we've previously saved a rasterScale but the ideal changes, things are unpredictable and we should just use 1.
464 if (rasterScale && rasterScale != 1.f && idealRasterScale != rasterScale) {
465 idealRasterScale = 1.f;
466 needToSetRasterScale = true;
467 }
468
469 if (needToSetRasterScale) {
470 bool useAndSaveIdealScale = idealRasterScale >= 1.f && !animatingTransformToScreen;
471 if (useAndSaveIdealScale) {
472 rasterScale = idealRasterScale;
[email protected]11ec92972012-11-10 03:06:21473 layer->setRasterScale(rasterScale);
[email protected]d0518202013-02-08 02:06:49474 }
[email protected]518ee582012-10-24 18:29:44475 }
476 }
477
[email protected]d0518202013-02-08 02:06:49478 if (!rasterScale)
479 rasterScale = 1.f;
480
[email protected]518ee582012-10-24 18:29:44481 float contentsScale = rasterScale * deviceScaleFactor;
482 if (!layer->boundsContainPageScale())
483 contentsScale *= pageScaleFactor;
[email protected]518ee582012-10-24 18:29:44484
[email protected]650b3faf2013-02-06 01:48:29485 calculateContentsScale(layer, contentsScale, animatingTransformToScreen);
[email protected]518ee582012-10-24 18:29:44486}
487
[email protected]6af5cab72012-12-12 00:49:47488template<typename LayerType, typename LayerList>
489static inline void removeSurfaceForEarlyExit(LayerType* layerToRemove, LayerList& renderSurfaceLayerList)
490{
491 DCHECK(layerToRemove->renderSurface());
492 // Technically, we know that the layer we want to remove should be
493 // at the back of the renderSurfaceLayerList. However, we have had
494 // bugs before that added unnecessary layers here
495 // (https://bugs.webkit.org/show_bug.cgi?id=74147), but that causes
496 // things to crash. So here we proactively remove any additional
497 // layers from the end of the list.
498 while (renderSurfaceLayerList.back() != layerToRemove) {
499 renderSurfaceLayerList.back()->clearRenderSurface();
500 renderSurfaceLayerList.pop_back();
501 }
502 DCHECK(renderSurfaceLayerList.back() == layerToRemove);
503 renderSurfaceLayerList.pop_back();
504 layerToRemove->clearRenderSurface();
505}
506
[email protected]b5b1fce2012-12-12 22:49:36507// Recursively walks the layer tree to compute any information that is needed
508// before doing the main recursion.
509template<typename LayerType>
510static void preCalculateMetaInformation(LayerType* layer)
511{
[email protected]9606d842013-01-20 00:38:28512 if (layer->hasDelegatedContent()) {
513 // Layers with delegated content need to be treated as if they have as many children as the number
514 // of layers they own delegated quads for. Since we don't know this number right now, we choose
515 // one that acts like infinity for our purposes.
516 layer->drawProperties().num_descendants_that_draw_content = 1000;
517 layer->drawProperties().descendants_can_clip_selves = false;
518 return;
519 }
520
[email protected]b5b1fce2012-12-12 22:49:36521 int numDescendantsThatDrawContent = 0;
[email protected]aedf4e52013-01-09 23:24:44522 bool descendantsCanClipSelves = true;
523 bool sublayerTransformPreventsClip = !layer->sublayerTransform().IsPositiveScaleOrTranslation();
[email protected]b5b1fce2012-12-12 22:49:36524
525 for (size_t i = 0; i < layer->children().size(); ++i) {
526 LayerType* childLayer = layer->children()[i];
527 preCalculateMetaInformation<LayerType>(childLayer);
[email protected]aedf4e52013-01-09 23:24:44528
[email protected]b5b1fce2012-12-12 22:49:36529 numDescendantsThatDrawContent += childLayer->drawsContent() ? 1 : 0;
530 numDescendantsThatDrawContent += childLayer->drawProperties().num_descendants_that_draw_content;
[email protected]aedf4e52013-01-09 23:24:44531
532 if ((childLayer->drawsContent() && !childLayer->canClipSelf()) ||
533 !childLayer->drawProperties().descendants_can_clip_selves ||
534 sublayerTransformPreventsClip ||
535 !childLayer->transform().IsPositiveScaleOrTranslation())
536 descendantsCanClipSelves = false;
[email protected]b5b1fce2012-12-12 22:49:36537 }
538
539 layer->drawProperties().num_descendants_that_draw_content = numDescendantsThatDrawContent;
[email protected]aedf4e52013-01-09 23:24:44540 layer->drawProperties().descendants_can_clip_selves = descendantsCanClipSelves;
[email protected]b5b1fce2012-12-12 22:49:36541}
542
[email protected]657b24c2013-03-06 09:01:20543static void roundTranslationComponents(gfx::Transform* transform)
544{
545 transform->matrix().setDouble(0, 3, MathUtil::Round(transform->matrix().getDouble(0, 3)));
546 transform->matrix().setDouble(1, 3, MathUtil::Round(transform->matrix().getDouble(1, 3)));
547}
548
[email protected]94f206c12012-08-25 00:09:14549// Recursively walks the layer tree starting at the given node and computes all the
550// necessary transformations, clipRects, render surfaces, etc.
[email protected]93698c12012-12-07 00:43:56551template<typename LayerType, typename LayerList, typename RenderSurfaceType>
[email protected]d76806f82012-12-05 21:41:50552static void calculateDrawPropertiesInternal(LayerType* layer, const gfx::Transform& parentMatrix,
[email protected]c8686a02012-11-27 08:29:00553 const gfx::Transform& fullHierarchyMatrix, const gfx::Transform& currentScrollCompensationMatrix,
[email protected]6af5cab72012-12-12 00:49:47554 const gfx::Rect& clipRectFromAncestor, const gfx::Rect& clipRectFromAncestorInDescendantSpace, bool ancestorClipsSubtree,
[email protected]94f206c12012-08-25 00:09:14555 RenderSurfaceType* nearestAncestorThatMovesPixels, LayerList& renderSurfaceLayerList, LayerList& layerList,
[email protected]10aabcc32012-12-13 09:18:59556 LayerSorter* layerSorter, int maxTextureSize, float deviceScaleFactor, float pageScaleFactor, bool subtreeCanUseLCDText,
[email protected]4c9bb952013-01-27 05:41:18557 gfx::Rect& drawableContentRectOfSubtree, bool updateTilePriorities)
[email protected]94f206c12012-08-25 00:09:14558{
559 // This function computes the new matrix transformations recursively for this
560 // layer and all its descendants. It also computes the appropriate render surfaces.
561 // Some important points to remember:
562 //
563 // 0. Here, transforms are notated in Matrix x Vector order, and in words we describe what
564 // the transform does from left to right.
565 //
566 // 1. In our terminology, the "layer origin" refers to the top-left corner of a layer, and the
567 // positive Y-axis points downwards. This interpretation is valid because the orthographic
568 // projection applied at draw time flips the Y axis appropriately.
569 //
[email protected]aad0a0072012-11-01 18:15:58570 // 2. The anchor point, when given as a PointF object, is specified in "unit layer space",
[email protected]c8686a02012-11-27 08:29:00571 // where the bounds of the layer map to [0, 1]. However, as a Transform object,
[email protected]b8d3c022012-10-08 17:38:04572 // the transform to the anchor point is specified in "layer space", where the bounds
[email protected]94f206c12012-08-25 00:09:14573 // of the layer map to [bounds.width(), bounds.height()].
574 //
575 // 3. Definition of various transforms used:
576 // M[parent] is the parent matrix, with respect to the nearest render surface, passed down recursively.
577 // M[root] is the full hierarchy, with respect to the root, passed down recursively.
578 // Tr[origin] is the translation matrix from the parent's origin to this layer's origin.
579 // Tr[origin2anchor] is the translation from the layer's origin to its anchor point
580 // Tr[origin2center] is the translation from the layer's origin to its center
581 // M[layer] is the layer's matrix (applied at the anchor point)
[email protected]9a3e6b72013-02-12 18:31:21582 // M[sublayer] is the layer's sublayer transform (also applied at the layer's anchor point)
[email protected]b8d3c022012-10-08 17:38:04583 // S[layer2content] is the ratio of a layer's contentBounds() to its bounds().
[email protected]94f206c12012-08-25 00:09:14584 //
585 // Some composite transforms can help in understanding the sequence of transforms:
586 // compositeLayerTransform = Tr[origin2anchor] * M[layer] * Tr[origin2anchor].inverse()
[email protected]9a3e6b72013-02-12 18:31:21587 // compositeSublayerTransform = Tr[origin2anchor] * M[sublayer] * Tr[origin2anchor].inverse()
[email protected]94f206c12012-08-25 00:09:14588 //
589 // 4. When a layer (or render surface) is drawn, it is drawn into a "target render surface". Therefore the draw
590 // transform does not necessarily transform from screen space to local layer space. Instead, the draw transform
591 // is the transform between the "target render surface space" and local layer space. Note that render surfaces,
592 // except for the root, also draw themselves into a different target render surface, and so their draw
593 // transform and origin transforms are also described with respect to the target.
594 //
595 // Using these definitions, then:
596 //
597 // The draw transform for the layer is:
[email protected]b8d3c022012-10-08 17:38:04598 // M[draw] = M[parent] * Tr[origin] * compositeLayerTransform * S[layer2content]
599 // = M[parent] * Tr[layer->position() + anchor] * M[layer] * Tr[anchor2origin] * S[layer2content]
[email protected]94f206c12012-08-25 00:09:14600 //
601 // Interpreting the math left-to-right, this transforms from the layer's render surface to the origin of the layer in content space.
602 //
603 // The screen space transform is:
[email protected]b8d3c022012-10-08 17:38:04604 // M[screenspace] = M[root] * Tr[origin] * compositeLayerTransform * S[layer2content]
605 // = M[root] * Tr[layer->position() + anchor] * M[layer] * Tr[anchor2origin] * S[layer2content]
[email protected]94f206c12012-08-25 00:09:14606 //
[email protected]655fa7f92012-11-15 00:14:33607 // 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:14608 //
609 // The transform hierarchy that is passed on to children (i.e. the child's parentMatrix) is:
610 // M[parent]_for_child = M[parent] * Tr[origin] * compositeLayerTransform * compositeSublayerTransform
[email protected]b8d3c022012-10-08 17:38:04611 // = M[parent] * Tr[layer->position() + anchor] * M[layer] * Tr[anchor2origin] * compositeSublayerTransform
[email protected]94f206c12012-08-25 00:09:14612 //
613 // and a similar matrix for the full hierarchy with respect to the root.
614 //
615 // Finally, note that the final matrix used by the shader for the layer is P * M[draw] * S . This final product
616 // is computed in drawTexturedQuad(), where:
617 // P is the projection matrix
[email protected]b8d3c022012-10-08 17:38:04618 // S is the scale adjustment (to scale up a canonical quad to the layer's size)
[email protected]94f206c12012-08-25 00:09:14619 //
620 // 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:00621 // 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:14622 //
623 // We will denote a scale by device scale S[deviceScale]
624 //
625 // The render surface draw transform to its target surface origin is:
626 // M[surfaceDraw] = M[owningLayer->Draw]
627 //
628 // The render surface origin transform to its the root (screen space) origin is:
629 // M[surface2root] = M[owningLayer->screenspace] * S[deviceScale].inverse()
630 //
631 // The replica draw transform to its target surface origin is:
632 // M[replicaDraw] = S[deviceScale] * M[surfaceDraw] * Tr[replica->position() + replica->anchor()] * Tr[replica] * Tr[origin2anchor].inverse() * S[contentsScale].inverse()
633 //
634 // The replica draw transform to the root (screen space) origin is:
635 // M[replica2root] = M[surface2root] * Tr[replica->position()] * Tr[replica] * Tr[origin2anchor].inverse()
636 //
637
638 // If we early-exit anywhere in this function, the drawableContentRect of this subtree should be considered empty.
[email protected]aad0a0072012-11-01 18:15:58639 drawableContentRectOfSubtree = gfx::Rect();
[email protected]94f206c12012-08-25 00:09:14640
[email protected]d76806f82012-12-05 21:41:50641 // The root layer cannot skip calcDrawProperties.
[email protected]ecc12622012-10-30 20:45:42642 if (!isRootLayer(layer) && subtreeShouldBeSkipped(layer))
[email protected]94f206c12012-08-25 00:09:14643 return;
644
[email protected]d76806f82012-12-05 21:41:50645 // As this function proceeds, these are the properties for the current
646 // layer that actually get computed. To avoid unnecessary copies
647 // (particularly for matrices), we do computations directly on these values
648 // when possible.
649 DrawProperties<LayerType, RenderSurfaceType>& layerDrawProperties = layer->drawProperties();
650
[email protected]aad0a0072012-11-01 18:15:58651 gfx::Rect clipRectForSubtree;
[email protected]94f206c12012-08-25 00:09:14652 bool subtreeShouldBeClipped = false;
[email protected]498ec6e0e2012-11-30 18:24:57653
[email protected]6af5cab72012-12-12 00:49:47654 // This value is cached on the stack so that we don't have to inverse-project
655 // the surface's clipRect redundantly for every layer. This value is the
656 // same as the surface's clipRect, except that instead of being described
657 // in the target surface space (i.e. the ancestor surface space), it is
658 // described in the current surface space.
659 gfx::Rect clipRectForSubtreeInDescendantSpace;
660
[email protected]d76806f82012-12-05 21:41:50661 float accumulatedDrawOpacity = layer->opacity();
[email protected]10aabcc32012-12-13 09:18:59662 bool animatingOpacityToTarget = layer->opacityIsAnimating();
663 bool animatingOpacityToScreen = animatingOpacityToTarget;
[email protected]498ec6e0e2012-11-30 18:24:57664 if (layer->parent()) {
[email protected]d76806f82012-12-05 21:41:50665 accumulatedDrawOpacity *= layer->parent()->drawOpacity();
[email protected]10aabcc32012-12-13 09:18:59666 animatingOpacityToTarget |= layer->parent()->drawOpacityIsAnimating();
667 animatingOpacityToScreen |= layer->parent()->screenSpaceOpacityIsAnimating();
[email protected]94f206c12012-08-25 00:09:14668 }
669
[email protected]6a9cff92012-11-08 11:53:26670 bool animatingTransformToTarget = layer->transformIsAnimating();
671 bool animatingTransformToScreen = animatingTransformToTarget;
672 if (layer->parent()) {
673 animatingTransformToTarget |= layer->parent()->drawTransformIsAnimating();
674 animatingTransformToScreen |= layer->parent()->screenSpaceTransformIsAnimating();
675 }
676
[email protected]aad0a0072012-11-01 18:15:58677 gfx::Size bounds = layer->bounds();
678 gfx::PointF anchorPoint = layer->anchorPoint();
[email protected]c9c1ebe2012-11-05 20:46:13679 gfx::PointF position = layer->position() - layer->scrollDelta();
[email protected]94f206c12012-08-25 00:09:14680
[email protected]c8686a02012-11-27 08:29:00681 gfx::Transform combinedTransform = parentMatrix;
[email protected]6af5cab72012-12-12 00:49:47682 if (!layer->transform().IsIdentity()) {
683 // LT = Tr[origin] * Tr[origin2anchor]
684 combinedTransform.Translate3d(position.x() + anchorPoint.x() * bounds.width(), position.y() + anchorPoint.y() * bounds.height(), layer->anchorPointZ());
685 // LT = Tr[origin] * Tr[origin2anchor] * M[layer]
686 combinedTransform.PreconcatTransform(layer->transform());
687 // LT = Tr[origin] * Tr[origin2anchor] * M[layer] * Tr[anchor2origin]
688 combinedTransform.Translate3d(-anchorPoint.x() * bounds.width(), -anchorPoint.y() * bounds.height(), -layer->anchorPointZ());
689 } else {
690 combinedTransform.Translate(position.x(), position.y());
691 }
[email protected]94f206c12012-08-25 00:09:14692
[email protected]518ee582012-10-24 18:29:44693 // The layer's contentsSize is determined from the combinedTransform, which then informs the
694 // layer's drawTransform.
[email protected]6a9cff92012-11-08 11:53:26695 updateLayerContentsScale(layer, combinedTransform, deviceScaleFactor, pageScaleFactor, animatingTransformToScreen);
[email protected]518ee582012-10-24 18:29:44696
[email protected]b9ab6d542012-12-04 21:33:06697 // If there is a transformation from the impl thread then it should be at
698 // the start of the combinedTransform, but we don't want it to affect the
699 // computation of contentsScale above.
700 // Note carefully: this is Concat, not Preconcat (implTransform * combinedTransform).
701 combinedTransform.ConcatTransform(layer->implTransform());
[email protected]518ee582012-10-24 18:29:44702
[email protected]657b24c2013-03-06 09:01:20703 if (!animatingTransformToTarget && layer->scrollable() && combinedTransform.IsScaleOrTranslation()) {
704 // Align the scrollable layer's position to screen space pixels to avoid blurriness.
705 // To avoid side-effects, do this only if the transform is simple.
706 roundTranslationComponents(&combinedTransform);
707 }
708
[email protected]94f206c12012-08-25 00:09:14709 if (layer->fixedToContainerLayer()) {
710 // Special case: this layer is a composited fixed-position layer; we need to
711 // explicitly compensate for all ancestors' nonzero scrollDeltas to keep this layer
712 // fixed correctly.
[email protected]b9ab6d542012-12-04 21:33:06713 // Note carefully: this is Concat, not Preconcat (currentScrollCompensation * combinedTransform).
714 combinedTransform.ConcatTransform(currentScrollCompensationMatrix);
[email protected]94f206c12012-08-25 00:09:14715 }
716
717 // The drawTransform that gets computed below is effectively the layer's drawTransform, unless
718 // the layer itself creates a renderSurface. In that case, the renderSurface re-parents the transforms.
[email protected]d76806f82012-12-05 21:41:50719 layerDrawProperties.target_space_transform = combinedTransform;
[email protected]f89f5632012-11-14 23:34:45720 // M[draw] = M[parent] * LT * S[layer2content]
[email protected]d76806f82012-12-05 21:41:50721 layerDrawProperties.target_space_transform.Scale(1.0 / layer->contentsScaleX(), 1.0 / layer->contentsScaleY());
[email protected]94f206c12012-08-25 00:09:14722
723 // layerScreenSpaceTransform represents the transform between root layer's "screen space" and local content space.
[email protected]d76806f82012-12-05 21:41:50724 layerDrawProperties.screen_space_transform = fullHierarchyMatrix;
[email protected]94f206c12012-08-25 00:09:14725 if (!layer->preserves3D())
[email protected]78634b0c2013-01-15 07:49:40726 layerDrawProperties.screen_space_transform.FlattenTo2d();
[email protected]d76806f82012-12-05 21:41:50727 layerDrawProperties.screen_space_transform.PreconcatTransform(layerDrawProperties.target_space_transform);
[email protected]94f206c12012-08-25 00:09:14728
[email protected]10aabcc32012-12-13 09:18:59729 // Adjusting text AA method during animation may cause repaints, which in-turn causes jank.
730 bool adjustTextAA = !animatingOpacityToScreen && !animatingTransformToScreen;
731 // To avoid color fringing, LCD text should only be used on opaque layers with just integral translation.
732 bool layerCanUseLCDText = subtreeCanUseLCDText &&
733 (accumulatedDrawOpacity == 1.0) &&
734 layerDrawProperties.target_space_transform.IsIdentityOrIntegerTranslation();
735
[email protected]aad0a0072012-11-01 18:15:58736 gfx::RectF contentRect(gfx::PointF(), layer->contentBounds());
[email protected]94f206c12012-08-25 00:09:14737
[email protected]96baf3e2012-10-22 23:09:55738 // fullHierarchyMatrix is the matrix that transforms objects between screen space (except projection matrix) and the most recent RenderSurfaceImpl's space.
739 // nextHierarchyMatrix will only change if this layer uses a new RenderSurfaceImpl, otherwise remains the same.
[email protected]c8686a02012-11-27 08:29:00740 gfx::Transform nextHierarchyMatrix = fullHierarchyMatrix;
741 gfx::Transform sublayerMatrix;
[email protected]94f206c12012-08-25 00:09:14742
[email protected]1b30e8e2012-12-21 02:59:09743 gfx::Vector2dF renderSurfaceSublayerScale = MathUtil::computeTransform2dScaleComponents(combinedTransform, deviceScaleFactor * pageScaleFactor);
[email protected]518ee582012-10-24 18:29:44744
[email protected]2c7cd6d2012-11-28 23:49:26745 if (subtreeShouldRenderToSeparateSurface(layer, combinedTransform.IsScaleOrTranslation())) {
[email protected]94f206c12012-08-25 00:09:14746 // Check back-face visibility before continuing with this surface and its subtree
747 if (!layer->doubleSided() && transformToParentIsKnown(layer) && isSurfaceBackFaceVisible(layer, combinedTransform))
748 return;
749
750 if (!layer->renderSurface())
751 layer->createRenderSurface();
752
753 RenderSurfaceType* renderSurface = layer->renderSurface();
[email protected]7d929c02012-09-20 17:26:57754 renderSurface->clearLayerLists();
[email protected]94f206c12012-08-25 00:09:14755
[email protected]b9ab6d542012-12-04 21:33:06756 // The owning layer's draw transform has a scale from content to layer
757 // space which we do not want; so here we use the combinedTransform
758 // instead of the drawTransform. However, we do need to add a different
759 // scale factor that accounts for the surface's pixel dimensions.
760 combinedTransform.Scale(1 / renderSurfaceSublayerScale.x(), 1 / renderSurfaceSublayerScale.y());
761 renderSurface->setDrawTransform(combinedTransform);
[email protected]518ee582012-10-24 18:29:44762
[email protected]b9ab6d542012-12-04 21:33:06763 // The owning layer's transform was re-parented by the surface, so the layer's new drawTransform
764 // only needs to scale the layer to surface space.
[email protected]d76806f82012-12-05 21:41:50765 layerDrawProperties.target_space_transform.MakeIdentity();
766 layerDrawProperties.target_space_transform.Scale(renderSurfaceSublayerScale.x() / layer->contentsScaleX(), renderSurfaceSublayerScale.y() / layer->contentsScaleY());
[email protected]94f206c12012-08-25 00:09:14767
[email protected]518ee582012-10-24 18:29:44768 // Inside the surface's subtree, we scale everything to the owning layer's scale.
[email protected]9a3e6b72013-02-12 18:31:21769 // The sublayer matrix transforms layer rects into target
[email protected]94f206c12012-08-25 00:09:14770 // surface content space.
[email protected]b9ab6d542012-12-04 21:33:06771 DCHECK(sublayerMatrix.IsIdentity());
[email protected]c8686a02012-11-27 08:29:00772 sublayerMatrix.Scale(renderSurfaceSublayerScale.x(), renderSurfaceSublayerScale.y());
[email protected]94f206c12012-08-25 00:09:14773
774 // 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:50775 renderSurface->setDrawOpacity(accumulatedDrawOpacity);
[email protected]10aabcc32012-12-13 09:18:59776 renderSurface->setDrawOpacityIsAnimating(animatingOpacityToTarget);
777 animatingOpacityToTarget = false;
[email protected]d76806f82012-12-05 21:41:50778 layerDrawProperties.opacity = 1;
[email protected]10aabcc32012-12-13 09:18:59779 layerDrawProperties.opacity_is_animating = animatingOpacityToTarget;
780 layerDrawProperties.screen_space_opacity_is_animating = animatingOpacityToScreen;
[email protected]94f206c12012-08-25 00:09:14781
782 renderSurface->setTargetSurfaceTransformsAreAnimating(animatingTransformToTarget);
783 renderSurface->setScreenSpaceTransformsAreAnimating(animatingTransformToScreen);
784 animatingTransformToTarget = false;
[email protected]d76806f82012-12-05 21:41:50785 layerDrawProperties.target_space_transform_is_animating = animatingTransformToTarget;
786 layerDrawProperties.screen_space_transform_is_animating = animatingTransformToScreen;
[email protected]94f206c12012-08-25 00:09:14787
788 // Update the aggregate hierarchy matrix to include the transform of the
[email protected]96baf3e2012-10-22 23:09:55789 // newly created RenderSurfaceImpl.
[email protected]c8686a02012-11-27 08:29:00790 nextHierarchyMatrix.PreconcatTransform(renderSurface->drawTransform());
[email protected]94f206c12012-08-25 00:09:14791
792 // The new renderSurface here will correctly clip the entire subtree. So, we do
793 // not need to continue propagating the clipping state further down the tree. This
794 // way, we can avoid transforming clipRects from ancestor target surface space to
795 // current target surface space that could cause more w < 0 headaches.
796 subtreeShouldBeClipped = false;
797
[email protected]9bab0bb2012-10-08 19:11:58798 if (layer->maskLayer()) {
[email protected]d76806f82012-12-05 21:41:50799 DrawProperties<LayerType, RenderSurfaceType>& maskLayerDrawProperties = layer->maskLayer()->drawProperties();
800 maskLayerDrawProperties.render_target = layer;
801 maskLayerDrawProperties.visible_content_rect = gfx::Rect(gfx::Point(), layer->contentBounds());
[email protected]9bab0bb2012-10-08 19:11:58802 }
[email protected]94f206c12012-08-25 00:09:14803
[email protected]9bab0bb2012-10-08 19:11:58804 if (layer->replicaLayer() && layer->replicaLayer()->maskLayer()) {
[email protected]d76806f82012-12-05 21:41:50805 DrawProperties<LayerType, RenderSurfaceType>& replicaMaskDrawProperties = layer->replicaLayer()->maskLayer()->drawProperties();
806 replicaMaskDrawProperties.render_target = layer;
807 replicaMaskDrawProperties.visible_content_rect = gfx::Rect(gfx::Point(), layer->contentBounds());
[email protected]9bab0bb2012-10-08 19:11:58808 }
[email protected]94f206c12012-08-25 00:09:14809
[email protected]4000abf2012-10-23 04:45:45810 // FIXME: make this smarter for the SkImageFilter case (check for
811 // pixel-moving filters)
812 if (layer->filters().hasFilterThatMovesPixels() || layer->filter())
[email protected]94f206c12012-08-25 00:09:14813 nearestAncestorThatMovesPixels = renderSurface;
814
[email protected]9bab0bb2012-10-08 19:11:58815 // 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:01816 renderSurface->setIsClipped(ancestorClipsSubtree);
[email protected]6af5cab72012-12-12 00:49:47817 if (ancestorClipsSubtree) {
[email protected]9bab0bb2012-10-08 19:11:58818 renderSurface->setClipRect(clipRectFromAncestor);
[email protected]bda41962013-01-07 18:46:17819
820 gfx::Transform inverseSurfaceDrawTransform(gfx::Transform::kSkipInitialization);
821 if (!renderSurface->drawTransform().GetInverse(&inverseSurfaceDrawTransform)) {
822 // TODO(shawnsingh): Either we need to handle uninvertible transforms
823 // here, or DCHECK that the transform is invertible.
824 }
825 clipRectForSubtreeInDescendantSpace = gfx::ToEnclosingRect(MathUtil::projectClippedRect(inverseSurfaceDrawTransform, renderSurface->clipRect()));
[email protected]6af5cab72012-12-12 00:49:47826 } else {
[email protected]aad0a0072012-11-01 18:15:58827 renderSurface->setClipRect(gfx::Rect());
[email protected]6af5cab72012-12-12 00:49:47828 clipRectForSubtreeInDescendantSpace = clipRectFromAncestorInDescendantSpace;
829 }
[email protected]9bab0bb2012-10-08 19:11:58830
[email protected]94f206c12012-08-25 00:09:14831 renderSurface->setNearestAncestorThatMovesPixels(nearestAncestorThatMovesPixels);
832
[email protected]10aabcc32012-12-13 09:18:59833 // If the new render surface is drawn translucent or with a non-integral translation
834 // then the subtree that gets drawn on this render surface cannot use LCD text.
835 subtreeCanUseLCDText = layerCanUseLCDText;
836
[email protected]d58499a2012-10-09 22:27:47837 renderSurfaceLayerList.push_back(layer);
[email protected]94f206c12012-08-25 00:09:14838 } else {
[email protected]ecc12622012-10-30 20:45:42839 DCHECK(layer->parent());
840
[email protected]d76806f82012-12-05 21:41:50841 // Note: layerDrawProperties.target_space_transform is computed above,
842 // before this if-else statement.
843 layerDrawProperties.target_space_transform_is_animating = animatingTransformToTarget;
844 layerDrawProperties.screen_space_transform_is_animating = animatingTransformToScreen;
845 layerDrawProperties.opacity = accumulatedDrawOpacity;
[email protected]10aabcc32012-12-13 09:18:59846 layerDrawProperties.opacity_is_animating = animatingOpacityToTarget;
847 layerDrawProperties.screen_space_opacity_is_animating = animatingOpacityToScreen;
[email protected]94f206c12012-08-25 00:09:14848 sublayerMatrix = combinedTransform;
849
[email protected]ecc12622012-10-30 20:45:42850 layer->clearRenderSurface();
[email protected]94f206c12012-08-25 00:09:14851
[email protected]ecc12622012-10-30 20:45:42852 // Layers without renderSurfaces directly inherit the ancestor's clip status.
853 subtreeShouldBeClipped = ancestorClipsSubtree;
854 if (ancestorClipsSubtree)
855 clipRectForSubtree = clipRectFromAncestor;
[email protected]94f206c12012-08-25 00:09:14856
[email protected]6af5cab72012-12-12 00:49:47857 // The surface's cached clipRect value propagates regardless of what clipping goes on between layers here.
858 clipRectForSubtreeInDescendantSpace = clipRectFromAncestorInDescendantSpace;
859
[email protected]ecc12622012-10-30 20:45:42860 // Layers that are not their own renderTarget will render into the target of their nearest ancestor.
[email protected]d76806f82012-12-05 21:41:50861 layerDrawProperties.render_target = layer->parent()->renderTarget();
[email protected]94f206c12012-08-25 00:09:14862 }
863
[email protected]10aabcc32012-12-13 09:18:59864 if (adjustTextAA)
865 layerDrawProperties.can_use_lcd_text = layerCanUseLCDText;
866
[email protected]aad0a0072012-11-01 18:15:58867 gfx::Rect rectInTargetSpace = ToEnclosingRect(MathUtil::mapClippedRect(layer->drawTransform(), contentRect));
[email protected]94f206c12012-08-25 00:09:14868
869 if (layerClipsSubtree(layer)) {
870 subtreeShouldBeClipped = true;
871 if (ancestorClipsSubtree && !layer->renderSurface()) {
872 clipRectForSubtree = clipRectFromAncestor;
[email protected]aad0a0072012-11-01 18:15:58873 clipRectForSubtree.Intersect(rectInTargetSpace);
[email protected]94f206c12012-08-25 00:09:14874 } else
875 clipRectForSubtree = rectInTargetSpace;
876 }
877
878 // Flatten to 2D if the layer doesn't preserve 3D.
879 if (!layer->preserves3D())
[email protected]78634b0c2013-01-15 07:49:40880 sublayerMatrix.FlattenTo2d();
[email protected]94f206c12012-08-25 00:09:14881
[email protected]9a3e6b72013-02-12 18:31:21882 // Apply the sublayer transform at the anchor point of the layer.
[email protected]b9ab6d542012-12-04 21:33:06883 if (!layer->sublayerTransform().IsIdentity()) {
[email protected]9a3e6b72013-02-12 18:31:21884 sublayerMatrix.Translate(layer->anchorPoint().x() * bounds.width(), layer->anchorPoint().y() * bounds.height());
[email protected]b9ab6d542012-12-04 21:33:06885 sublayerMatrix.PreconcatTransform(layer->sublayerTransform());
[email protected]9a3e6b72013-02-12 18:31:21886 sublayerMatrix.Translate(-layer->anchorPoint().x() * bounds.width(), -layer->anchorPoint().y() * bounds.height());
[email protected]b9ab6d542012-12-04 21:33:06887 }
[email protected]94f206c12012-08-25 00:09:14888
889 LayerList& descendants = (layer->renderSurface() ? layer->renderSurface()->layerList() : layerList);
890
891 // Any layers that are appended after this point are in the layer's subtree and should be included in the sorting process.
892 unsigned sortingStartIndex = descendants.size();
893
894 if (!layerShouldBeSkipped(layer))
[email protected]d58499a2012-10-09 22:27:47895 descendants.push_back(layer);
[email protected]94f206c12012-08-25 00:09:14896
[email protected]c8686a02012-11-27 08:29:00897 gfx::Transform nextScrollCompensationMatrix = computeScrollCompensationMatrixForChildren(layer, parentMatrix, currentScrollCompensationMatrix);;
[email protected]94f206c12012-08-25 00:09:14898
[email protected]aad0a0072012-11-01 18:15:58899 gfx::Rect accumulatedDrawableContentRectOfChildren;
[email protected]94f206c12012-08-25 00:09:14900 for (size_t i = 0; i < layer->children().size(); ++i) {
[email protected]96baf3e2012-10-22 23:09:55901 LayerType* child = LayerTreeHostCommon::getChildAsRawPtr(layer->children(), i);
[email protected]aad0a0072012-11-01 18:15:58902 gfx::Rect drawableContentRectOfChildSubtree;
[email protected]93698c12012-12-07 00:43:56903 calculateDrawPropertiesInternal<LayerType, LayerList, RenderSurfaceType>(child, sublayerMatrix, nextHierarchyMatrix, nextScrollCompensationMatrix,
[email protected]6af5cab72012-12-12 00:49:47904 clipRectForSubtree, clipRectForSubtreeInDescendantSpace, subtreeShouldBeClipped, nearestAncestorThatMovesPixels,
[email protected]10aabcc32012-12-13 09:18:59905 renderSurfaceLayerList, descendants, layerSorter, maxTextureSize, deviceScaleFactor, pageScaleFactor,
[email protected]4c9bb952013-01-27 05:41:18906 subtreeCanUseLCDText, drawableContentRectOfChildSubtree, updateTilePriorities);
[email protected]aad0a0072012-11-01 18:15:58907 if (!drawableContentRectOfChildSubtree.IsEmpty()) {
908 accumulatedDrawableContentRectOfChildren.Union(drawableContentRectOfChildSubtree);
[email protected]94f206c12012-08-25 00:09:14909 if (child->renderSurface())
[email protected]d58499a2012-10-09 22:27:47910 descendants.push_back(child);
[email protected]94f206c12012-08-25 00:09:14911 }
912 }
913
[email protected]6af5cab72012-12-12 00:49:47914 if (layer->renderSurface() && !isRootLayer(layer) && !layer->renderSurface()->layerList().size()) {
915 removeSurfaceForEarlyExit(layer, renderSurfaceLayerList);
916 return;
917 }
918
[email protected]94f206c12012-08-25 00:09:14919 // Compute the total drawableContentRect for this subtree (the rect is in targetSurface space)
[email protected]aad0a0072012-11-01 18:15:58920 gfx::Rect localDrawableContentRectOfSubtree = accumulatedDrawableContentRectOfChildren;
[email protected]94f206c12012-08-25 00:09:14921 if (layer->drawsContent())
[email protected]aad0a0072012-11-01 18:15:58922 localDrawableContentRectOfSubtree.Union(rectInTargetSpace);
[email protected]94f206c12012-08-25 00:09:14923 if (subtreeShouldBeClipped)
[email protected]aad0a0072012-11-01 18:15:58924 localDrawableContentRectOfSubtree.Intersect(clipRectForSubtree);
[email protected]94f206c12012-08-25 00:09:14925
926 // Compute the layer's drawable content rect (the rect is in targetSurface space)
[email protected]d76806f82012-12-05 21:41:50927 layerDrawProperties.drawable_content_rect = rectInTargetSpace;
[email protected]94f206c12012-08-25 00:09:14928 if (subtreeShouldBeClipped)
[email protected]d76806f82012-12-05 21:41:50929 layerDrawProperties.drawable_content_rect.Intersect(clipRectForSubtree);
[email protected]94f206c12012-08-25 00:09:14930
[email protected]dc462d782012-11-21 21:43:01931 // Tell the layer the rect that is clipped by. In theory we could use a
932 // tighter clipRect here (drawableContentRect), but that actually does not
933 // reduce how much would be drawn, and instead it would create unnecessary
934 // changes to scissor state affecting GPU performance.
[email protected]d76806f82012-12-05 21:41:50935 layerDrawProperties.is_clipped = subtreeShouldBeClipped;
[email protected]dc462d782012-11-21 21:43:01936 if (subtreeShouldBeClipped)
[email protected]d76806f82012-12-05 21:41:50937 layerDrawProperties.clip_rect = clipRectForSubtree;
[email protected]dc462d782012-11-21 21:43:01938 else {
939 // Initialize the clipRect to a safe value that will not clip the
940 // layer, just in case clipping is still accidentally used.
[email protected]d76806f82012-12-05 21:41:50941 layerDrawProperties.clip_rect = rectInTargetSpace;
[email protected]dc462d782012-11-21 21:43:01942 }
943
[email protected]9bab0bb2012-10-08 19:11:58944 // Compute the layer's visible content rect (the rect is in content space)
[email protected]6af5cab72012-12-12 00:49:47945 layerDrawProperties.visible_content_rect = calculateVisibleContentRect(layer, clipRectForSubtreeInDescendantSpace, rectInTargetSpace);
[email protected]9bab0bb2012-10-08 19:11:58946
[email protected]94f206c12012-08-25 00:09:14947 // Compute the remaining properties for the render surface, if the layer has one.
[email protected]ecc12622012-10-30 20:45:42948 if (isRootLayer(layer)) {
949 // The root layer's surface's contentRect is always the entire viewport.
950 DCHECK(layer->renderSurface());
951 layer->renderSurface()->setContentRect(clipRectFromAncestor);
952 } else if (layer->renderSurface() && !isRootLayer(layer)) {
[email protected]94f206c12012-08-25 00:09:14953 RenderSurfaceType* renderSurface = layer->renderSurface();
[email protected]aad0a0072012-11-01 18:15:58954 gfx::Rect clippedContentRect = localDrawableContentRectOfSubtree;
[email protected]94f206c12012-08-25 00:09:14955
[email protected]94f206c12012-08-25 00:09:14956 // Don't clip if the layer is reflected as the reflection shouldn't be
957 // clipped. If the layer is animating, then the surface's transform to
958 // its target is not known on the main thread, and we should not use it
959 // to clip.
960 if (!layer->replicaLayer() && transformToParentIsKnown(layer)) {
961 // 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:58962 if (ancestorClipsSubtree && !clippedContentRect.IsEmpty()) {
963 gfx::Rect surfaceClipRect = LayerTreeHostCommon::calculateVisibleRect(renderSurface->clipRect(), clippedContentRect, renderSurface->drawTransform());
964 clippedContentRect.Intersect(surfaceClipRect);
[email protected]94f206c12012-08-25 00:09:14965 }
966 }
967
[email protected]96baf3e2012-10-22 23:09:55968 // The RenderSurfaceImpl backing texture cannot exceed the maximum supported
[email protected]94f206c12012-08-25 00:09:14969 // texture size.
[email protected]aad0a0072012-11-01 18:15:58970 clippedContentRect.set_width(std::min(clippedContentRect.width(), maxTextureSize));
971 clippedContentRect.set_height(std::min(clippedContentRect.height(), maxTextureSize));
[email protected]94f206c12012-08-25 00:09:14972
[email protected]6af5cab72012-12-12 00:49:47973 if (clippedContentRect.IsEmpty()) {
[email protected]7d929c02012-09-20 17:26:57974 renderSurface->clearLayerLists();
[email protected]6af5cab72012-12-12 00:49:47975 removeSurfaceForEarlyExit(layer, renderSurfaceLayerList);
976 return;
977 }
978
[email protected]94f206c12012-08-25 00:09:14979 renderSurface->setContentRect(clippedContentRect);
[email protected]518ee582012-10-24 18:29:44980
981 // The owning layer's screenSpaceTransform has a scale from content to layer space which we need to undo and
982 // replace with a scale from the surface's subtree into layer space.
[email protected]c8686a02012-11-27 08:29:00983 gfx::Transform screenSpaceTransform = layer->screenSpaceTransform();
[email protected]b9ab6d542012-12-04 21:33:06984 screenSpaceTransform.Scale(layer->contentsScaleX() / renderSurfaceSublayerScale.x(), layer->contentsScaleY() / renderSurfaceSublayerScale.y());
[email protected]518ee582012-10-24 18:29:44985 renderSurface->setScreenSpaceTransform(screenSpaceTransform);
[email protected]94f206c12012-08-25 00:09:14986
987 if (layer->replicaLayer()) {
[email protected]c8686a02012-11-27 08:29:00988 gfx::Transform surfaceOriginToReplicaOriginTransform;
989 surfaceOriginToReplicaOriginTransform.Scale(renderSurfaceSublayerScale.x(), renderSurfaceSublayerScale.y());
990 surfaceOriginToReplicaOriginTransform.Translate(layer->replicaLayer()->position().x() + layer->replicaLayer()->anchorPoint().x() * bounds.width(),
[email protected]94f206c12012-08-25 00:09:14991 layer->replicaLayer()->position().y() + layer->replicaLayer()->anchorPoint().y() * bounds.height());
[email protected]c8686a02012-11-27 08:29:00992 surfaceOriginToReplicaOriginTransform.PreconcatTransform(layer->replicaLayer()->transform());
993 surfaceOriginToReplicaOriginTransform.Translate(-layer->replicaLayer()->anchorPoint().x() * bounds.width(), -layer->replicaLayer()->anchorPoint().y() * bounds.height());
994 surfaceOriginToReplicaOriginTransform.Scale(1 / renderSurfaceSublayerScale.x(), 1 / renderSurfaceSublayerScale.y());
[email protected]94f206c12012-08-25 00:09:14995
996 // 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:00997 gfx::Transform replicaOriginTransform = layer->renderSurface()->drawTransform() * surfaceOriginToReplicaOriginTransform;
[email protected]94f206c12012-08-25 00:09:14998 renderSurface->setReplicaDrawTransform(replicaOriginTransform);
999
1000 // 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:001001 gfx::Transform replicaScreenSpaceTransform = layer->renderSurface()->screenSpaceTransform() * surfaceOriginToReplicaOriginTransform;
[email protected]94f206c12012-08-25 00:09:141002 renderSurface->setReplicaScreenSpaceTransform(replicaScreenSpaceTransform);
1003 }
[email protected]94f206c12012-08-25 00:09:141004 }
1005
[email protected]4c9bb952013-01-27 05:41:181006 if (updateTilePriorities)
1007 updateTilePrioritiesForLayer(layer);
[email protected]6ef21ffc2012-11-28 05:58:541008
[email protected]94f206c12012-08-25 00:09:141009 // If neither this layer nor any of its children were added, early out.
1010 if (sortingStartIndex == descendants.size())
1011 return;
1012
1013 // If preserves-3d then sort all the descendants in 3D so that they can be
1014 // drawn from back to front. If the preserves-3d property is also set on the parent then
1015 // skip the sorting as the parent will sort all the descendants anyway.
[email protected]93698c12012-12-07 00:43:561016 if (layerSorter && descendants.size() && layer->preserves3D() && (!layer->parent() || !layer->parent()->preserves3D()))
[email protected]d58499a2012-10-09 22:27:471017 sortLayers(descendants.begin() + sortingStartIndex, descendants.end(), layerSorter);
[email protected]94f206c12012-08-25 00:09:141018
1019 if (layer->renderSurface())
[email protected]aad0a0072012-11-01 18:15:581020 drawableContentRectOfSubtree = gfx::ToEnclosingRect(layer->renderSurface()->drawableContentRect());
[email protected]94f206c12012-08-25 00:09:141021 else
1022 drawableContentRectOfSubtree = localDrawableContentRectOfSubtree;
1023
[email protected]7d929c02012-09-20 17:26:571024 if (layer->hasContributingDelegatedRenderPasses())
1025 layer->renderTarget()->renderSurface()->addContributingDelegatedRenderPassLayer(layer);
[email protected]94f206c12012-08-25 00:09:141026}
1027
[email protected]10aabcc32012-12-13 09:18:591028void 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:141029{
[email protected]aad0a0072012-11-01 18:15:581030 gfx::Rect totalDrawableContentRect;
[email protected]c8686a02012-11-27 08:29:001031 gfx::Transform identityMatrix;
1032 gfx::Transform deviceScaleTransform;
1033 deviceScaleTransform.Scale(deviceScaleFactor, deviceScaleFactor);
[email protected]ecc12622012-10-30 20:45:421034 std::vector<scoped_refptr<Layer> > dummyLayerList;
[email protected]94f206c12012-08-25 00:09:141035
[email protected]ecc12622012-10-30 20:45:421036 // The root layer's renderSurface should receive the deviceViewport as the initial clipRect.
1037 bool subtreeShouldBeClipped = true;
[email protected]aad0a0072012-11-01 18:15:581038 gfx::Rect deviceViewportRect(gfx::Point(), deviceViewportSize);
[email protected]4c9bb952013-01-27 05:41:181039 bool updateTilePriorities = false;
[email protected]ecc12622012-10-30 20:45:421040
1041 // This function should have received a root layer.
1042 DCHECK(isRootLayer(rootLayer));
[email protected]94f206c12012-08-25 00:09:141043
[email protected]b5b1fce2012-12-12 22:49:361044 preCalculateMetaInformation<Layer>(rootLayer);
1045 calculateDrawPropertiesInternal<Layer, std::vector<scoped_refptr<Layer> >, RenderSurface>(
[email protected]ecc12622012-10-30 20:45:421046 rootLayer, deviceScaleTransform, identityMatrix, identityMatrix,
[email protected]6af5cab72012-12-12 00:49:471047 deviceViewportRect, deviceViewportRect, subtreeShouldBeClipped, 0, renderSurfaceLayerList,
[email protected]ecc12622012-10-30 20:45:421048 dummyLayerList, 0, maxTextureSize,
[email protected]4c9bb952013-01-27 05:41:181049 deviceScaleFactor, pageScaleFactor, canUseLCDText, totalDrawableContentRect,
1050 updateTilePriorities);
[email protected]ecc12622012-10-30 20:45:421051
1052 // The dummy layer list should not have been used.
1053 DCHECK(dummyLayerList.size() == 0);
[email protected]d76806f82012-12-05 21:41:501054 // A root layer renderSurface should always exist after calculateDrawProperties.
[email protected]ecc12622012-10-30 20:45:421055 DCHECK(rootLayer->renderSurface());
[email protected]94f206c12012-08-25 00:09:141056}
1057
[email protected]4c9bb952013-01-27 05:41:181058void LayerTreeHostCommon::calculateDrawProperties(LayerImpl* rootLayer, const gfx::Size& deviceViewportSize, float deviceScaleFactor, float pageScaleFactor, int maxTextureSize, bool canUseLCDText, std::vector<LayerImpl*>& renderSurfaceLayerList, bool updateTilePriorities)
[email protected]94f206c12012-08-25 00:09:141059{
[email protected]aad0a0072012-11-01 18:15:581060 gfx::Rect totalDrawableContentRect;
[email protected]c8686a02012-11-27 08:29:001061 gfx::Transform identityMatrix;
1062 gfx::Transform deviceScaleTransform;
1063 deviceScaleTransform.Scale(deviceScaleFactor, deviceScaleFactor);
[email protected]ecc12622012-10-30 20:45:421064 std::vector<LayerImpl*> dummyLayerList;
[email protected]93698c12012-12-07 00:43:561065 LayerSorter layerSorter;
1066
[email protected]ecc12622012-10-30 20:45:421067 // The root layer's renderSurface should receive the deviceViewport as the initial clipRect.
1068 bool subtreeShouldBeClipped = true;
[email protected]aad0a0072012-11-01 18:15:581069 gfx::Rect deviceViewportRect(gfx::Point(), deviceViewportSize);
[email protected]ecc12622012-10-30 20:45:421070
1071 // This function should have received a root layer.
1072 DCHECK(isRootLayer(rootLayer));
[email protected]94f206c12012-08-25 00:09:141073
[email protected]b5b1fce2012-12-12 22:49:361074 preCalculateMetaInformation<LayerImpl>(rootLayer);
1075 calculateDrawPropertiesInternal<LayerImpl, std::vector<LayerImpl*>, RenderSurfaceImpl>(
[email protected]ecc12622012-10-30 20:45:421076 rootLayer, deviceScaleTransform, identityMatrix, identityMatrix,
[email protected]6af5cab72012-12-12 00:49:471077 deviceViewportRect, deviceViewportRect, subtreeShouldBeClipped, 0, renderSurfaceLayerList,
[email protected]93698c12012-12-07 00:43:561078 dummyLayerList, &layerSorter, maxTextureSize,
[email protected]4c9bb952013-01-27 05:41:181079 deviceScaleFactor, pageScaleFactor, canUseLCDText, totalDrawableContentRect,
1080 updateTilePriorities);
[email protected]ecc12622012-10-30 20:45:421081
1082 // The dummy layer list should not have been used.
1083 DCHECK(dummyLayerList.size() == 0);
[email protected]d76806f82012-12-05 21:41:501084 // A root layer renderSurface should always exist after calculateDrawProperties.
[email protected]ecc12622012-10-30 20:45:421085 DCHECK(rootLayer->renderSurface());
[email protected]94f206c12012-08-25 00:09:141086}
1087
[email protected]c8686a02012-11-27 08:29:001088static bool pointHitsRect(const gfx::PointF& screenSpacePoint, const gfx::Transform& localSpaceToScreenSpaceTransform, gfx::RectF localSpaceRect)
[email protected]94f206c12012-08-25 00:09:141089{
1090 // If the transform is not invertible, then assume that this point doesn't hit this rect.
[email protected]bda41962013-01-07 18:46:171091 gfx::Transform inverseLocalSpaceToScreenSpace(gfx::Transform::kSkipInitialization);
1092 if (!localSpaceToScreenSpaceTransform.GetInverse(&inverseLocalSpaceToScreenSpace))
[email protected]94f206c12012-08-25 00:09:141093 return false;
1094
1095 // Transform the hit test point from screen space to the local space of the given rect.
1096 bool clipped = false;
[email protected]bda41962013-01-07 18:46:171097 gfx::PointF hitTestPointInLocalSpace = MathUtil::projectPoint(inverseLocalSpaceToScreenSpace, screenSpacePoint, clipped);
[email protected]94f206c12012-08-25 00:09:141098
1099 // If projectPoint could not project to a valid value, then we assume that this point doesn't hit this rect.
1100 if (clipped)
1101 return false;
1102
[email protected]aad0a0072012-11-01 18:15:581103 return localSpaceRect.Contains(hitTestPointInLocalSpace);
[email protected]94f206c12012-08-25 00:09:141104}
1105
[email protected]c8686a02012-11-27 08:29:001106static bool pointHitsRegion(gfx::PointF screenSpacePoint, const gfx::Transform& screenSpaceTransform, const Region& layerSpaceRegion, float layerContentScaleX, float layerContentScaleY)
[email protected]2f1acc262012-11-16 21:42:221107{
1108 // If the transform is not invertible, then assume that this point doesn't hit this region.
[email protected]bda41962013-01-07 18:46:171109 gfx::Transform inverseScreenSpaceTransform(gfx::Transform::kSkipInitialization);
1110 if (!screenSpaceTransform.GetInverse(&inverseScreenSpaceTransform))
[email protected]2f1acc262012-11-16 21:42:221111 return false;
1112
1113 // Transform the hit test point from screen space to the local space of the given region.
1114 bool clipped = false;
[email protected]bda41962013-01-07 18:46:171115 gfx::PointF hitTestPointInContentSpace = MathUtil::projectPoint(inverseScreenSpaceTransform, screenSpacePoint, clipped);
[email protected]2f1acc262012-11-16 21:42:221116 gfx::PointF hitTestPointInLayerSpace = gfx::ScalePoint(hitTestPointInContentSpace, 1 / layerContentScaleX, 1 / layerContentScaleY);
1117
1118 // If projectPoint could not project to a valid value, then we assume that this point doesn't hit this region.
1119 if (clipped)
1120 return false;
1121
1122 return layerSpaceRegion.Contains(gfx::ToRoundedPoint(hitTestPointInLayerSpace));
1123}
1124
[email protected]d455d552012-11-02 00:19:061125static bool pointIsClippedBySurfaceOrClipRect(const gfx::PointF& screenSpacePoint, LayerImpl* layer)
[email protected]94f206c12012-08-25 00:09:141126{
[email protected]96baf3e2012-10-22 23:09:551127 LayerImpl* currentLayer = layer;
[email protected]94f206c12012-08-25 00:09:141128
1129 // Walk up the layer tree and hit-test any renderSurfaces and any layer clipRects that are active.
1130 while (currentLayer) {
[email protected]01527732012-10-19 18:16:191131 if (currentLayer->renderSurface() && !pointHitsRect(screenSpacePoint, currentLayer->renderSurface()->screenSpaceTransform(), currentLayer->renderSurface()->contentRect()))
[email protected]94f206c12012-08-25 00:09:141132 return true;
1133
1134 // Note that drawableContentRects are actually in targetSurface space, so the transform we
1135 // have to provide is the target surface's screenSpaceTransform.
[email protected]96baf3e2012-10-22 23:09:551136 LayerImpl* renderTarget = currentLayer->renderTarget();
[email protected]01527732012-10-19 18:16:191137 if (layerClipsSubtree(currentLayer) && !pointHitsRect(screenSpacePoint, renderTarget->renderSurface()->screenSpaceTransform(), currentLayer->drawableContentRect()))
[email protected]94f206c12012-08-25 00:09:141138 return true;
1139
1140 currentLayer = currentLayer->parent();
1141 }
1142
1143 // If we have finished walking all ancestors without having already exited, then the point is not clipped by any ancestors.
1144 return false;
1145}
1146
[email protected]76ffd9e2012-12-20 19:12:471147LayerImpl* LayerTreeHostCommon::findLayerThatIsHitByPoint(const gfx::PointF& screenSpacePoint, const std::vector<LayerImpl*>& renderSurfaceLayerList)
[email protected]94f206c12012-08-25 00:09:141148{
[email protected]96baf3e2012-10-22 23:09:551149 LayerImpl* foundLayer = 0;
[email protected]94f206c12012-08-25 00:09:141150
[email protected]96baf3e2012-10-22 23:09:551151 typedef LayerIterator<LayerImpl, std::vector<LayerImpl*>, RenderSurfaceImpl, LayerIteratorActions::FrontToBack> LayerIteratorType;
1152 LayerIteratorType end = LayerIteratorType::end(&renderSurfaceLayerList);
[email protected]94f206c12012-08-25 00:09:141153
[email protected]96baf3e2012-10-22 23:09:551154 for (LayerIteratorType it = LayerIteratorType::begin(&renderSurfaceLayerList); it != end; ++it) {
[email protected]94f206c12012-08-25 00:09:141155 // We don't want to consider renderSurfaces for hit testing.
1156 if (!it.representsItself())
1157 continue;
1158
[email protected]96baf3e2012-10-22 23:09:551159 LayerImpl* currentLayer = (*it);
[email protected]94f206c12012-08-25 00:09:141160
[email protected]aad0a0072012-11-01 18:15:581161 gfx::RectF contentRect(gfx::PointF(), currentLayer->contentBounds());
[email protected]01527732012-10-19 18:16:191162 if (!pointHitsRect(screenSpacePoint, currentLayer->screenSpaceTransform(), contentRect))
[email protected]94f206c12012-08-25 00:09:141163 continue;
1164
1165 // At this point, we think the point does hit the layer, but we need to walk up
1166 // the parents to ensure that the layer was not clipped in such a way that the
1167 // hit point actually should not hit the layer.
[email protected]01527732012-10-19 18:16:191168 if (pointIsClippedBySurfaceOrClipRect(screenSpacePoint, currentLayer))
[email protected]94f206c12012-08-25 00:09:141169 continue;
1170
[email protected]586543832013-02-20 02:07:071171 // Skip the HUD layer.
1172 if (currentLayer == currentLayer->layerTreeImpl()->hud_layer())
1173 continue;
1174
[email protected]94f206c12012-08-25 00:09:141175 foundLayer = currentLayer;
1176 break;
1177 }
1178
[email protected]01527732012-10-19 18:16:191179 // 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:141180 return foundLayer;
1181}
1182
[email protected]76ffd9e2012-12-20 19:12:471183LayerImpl* LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(const gfx::PointF& screenSpacePoint, const std::vector<LayerImpl*>& renderSurfaceLayerList)
[email protected]2f1acc262012-11-16 21:42:221184{
1185 LayerImpl* foundLayer = 0;
1186
1187 typedef LayerIterator<LayerImpl, std::vector<LayerImpl*>, RenderSurfaceImpl, LayerIteratorActions::FrontToBack> LayerIteratorType;
1188 LayerIteratorType end = LayerIteratorType::end(&renderSurfaceLayerList);
1189
1190 for (LayerIteratorType it = LayerIteratorType::begin(&renderSurfaceLayerList); it != end; ++it) {
1191 // We don't want to consider renderSurfaces for hit testing.
1192 if (!it.representsItself())
1193 continue;
1194
1195 LayerImpl* currentLayer = (*it);
1196
[email protected]35d2edd22012-12-13 02:19:051197 if (!layerHasTouchEventHandlersAt(screenSpacePoint, currentLayer))
[email protected]2f1acc262012-11-16 21:42:221198 continue;
1199
1200 foundLayer = currentLayer;
1201 break;
1202 }
1203
1204 // This can potentially return 0, which means the screenSpacePoint did not successfully hit test any layers, not even the root layer.
1205 return foundLayer;
1206}
1207
[email protected]35d2edd22012-12-13 02:19:051208bool LayerTreeHostCommon::layerHasTouchEventHandlersAt(const gfx::PointF& screenSpacePoint, LayerImpl* layerImpl) {
1209 if (layerImpl->touchEventHandlerRegion().IsEmpty())
1210 return false;
1211
1212 if (!pointHitsRegion(screenSpacePoint, layerImpl->screenSpaceTransform(), layerImpl->touchEventHandlerRegion(), layerImpl->contentsScaleX(), layerImpl->contentsScaleY()))
1213 return false;;
1214
1215 // At this point, we think the point does hit the touch event handler region on the layer, but we need to walk up
1216 // the parents to ensure that the layer was not clipped in such a way that the
1217 // hit point actually should not hit the layer.
1218 if (pointIsClippedBySurfaceOrClipRect(screenSpacePoint, layerImpl))
1219 return false;
1220
1221 return true;
1222}
[email protected]bc5e77c2012-11-05 20:00:491223} // namespace cc