[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1 | // 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] | d50c686 | 2012-10-23 02:08:31 | [diff] [blame] | 5 | #include "cc/layer_tree_host_common.h" |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 6 | |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
[email protected] | a8461d8 | 2012-10-16 21:11:14 | [diff] [blame] | 9 | #include "cc/layer.h" |
[email protected] | d50c686 | 2012-10-23 02:08:31 | [diff] [blame] | 10 | #include "cc/layer_impl.h" |
| 11 | #include "cc/layer_iterator.h" |
| 12 | #include "cc/layer_sorter.h" |
[email protected] | 55a124d0 | 2012-10-22 03:07:13 | [diff] [blame] | 13 | #include "cc/math_util.h" |
[email protected] | a8461d8 | 2012-10-16 21:11:14 | [diff] [blame] | 14 | #include "cc/render_surface.h" |
[email protected] | d50c686 | 2012-10-23 02:08:31 | [diff] [blame] | 15 | #include "cc/render_surface_impl.h" |
[email protected] | 2f1acc26 | 2012-11-16 21:42:22 | [diff] [blame] | 16 | #include "ui/gfx/point_conversions.h" |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 17 | #include "ui/gfx/rect_conversions.h" |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 18 | #include "ui/gfx/transform.h" |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 19 | |
[email protected] | 9c88e56 | 2012-09-14 22:21:30 | [diff] [blame] | 20 | namespace cc { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 21 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 22 | ScrollAndScaleSet::ScrollAndScaleSet() |
[email protected] | 49306751 | 2012-09-19 23:34:10 | [diff] [blame] | 23 | { |
| 24 | } |
| 25 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 26 | ScrollAndScaleSet::~ScrollAndScaleSet() |
[email protected] | 49306751 | 2012-09-19 23:34:10 | [diff] [blame] | 27 | { |
| 28 | } |
| 29 | |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 30 | gfx::Rect LayerTreeHostCommon::calculateVisibleRect(const gfx::Rect& targetSurfaceRect, const gfx::Rect& layerBoundRect, const gfx::Transform& transform) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 31 | { |
| 32 | // Is this layer fully contained within the target surface? |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 33 | gfx::Rect layerInSurfaceSpace = MathUtil::mapClippedRect(transform, layerBoundRect); |
| 34 | if (targetSurfaceRect.Contains(layerInSurfaceSpace)) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 35 | return layerBoundRect; |
| 36 | |
| 37 | // If the layer doesn't fill up the entire surface, then find the part of |
| 38 | // the surface rect where the layer could be visible. This avoids trying to |
| 39 | // project surface rect points that are behind the projection point. |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 40 | gfx::Rect minimalSurfaceRect = targetSurfaceRect; |
| 41 | minimalSurfaceRect.Intersect(layerInSurfaceSpace); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 42 | |
| 43 | // Project the corners of the target surface rect into the layer space. |
| 44 | // This bounding rectangle may be larger than it needs to be (being |
| 45 | // axis-aligned), but is a reasonable filter on the space to consider. |
| 46 | // Non-invertible transforms will create an empty rect here. |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 47 | const gfx::Transform surfaceToLayer = MathUtil::inverse(transform); |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 48 | gfx::Rect layerRect = gfx::ToEnclosingRect(MathUtil::projectClippedRect(surfaceToLayer, gfx::RectF(minimalSurfaceRect))); |
| 49 | layerRect.Intersect(layerBoundRect); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 50 | return layerRect; |
| 51 | } |
| 52 | |
[email protected] | ecc1262 | 2012-10-30 20:45:42 | [diff] [blame] | 53 | template <typename LayerType> |
| 54 | static inline bool isRootLayer(LayerType* layer) |
| 55 | { |
| 56 | return !layer->parent(); |
| 57 | } |
| 58 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 59 | template<typename LayerType> |
| 60 | static inline bool layerIsInExisting3DRenderingContext(LayerType* layer) |
| 61 | { |
| 62 | // According to current W3C spec on CSS transforms, a layer is part of an established |
| 63 | // 3d rendering context if its parent has transform-style of preserves-3d. |
| 64 | return layer->parent() && layer->parent()->preserves3D(); |
| 65 | } |
| 66 | |
| 67 | template<typename LayerType> |
[email protected] | ecc1262 | 2012-10-30 20:45:42 | [diff] [blame] | 68 | static bool isRootLayerOfNewRenderingContext(LayerType* layer) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 69 | { |
| 70 | // According to current W3C spec on CSS transforms (Section 6.1), a layer is the |
| 71 | // beginning of 3d rendering context if its parent does not have transform-style: |
| 72 | // preserve-3d, but this layer itself does. |
| 73 | if (layer->parent()) |
| 74 | return !layer->parent()->preserves3D() && layer->preserves3D(); |
| 75 | |
| 76 | return layer->preserves3D(); |
| 77 | } |
| 78 | |
| 79 | template<typename LayerType> |
| 80 | static bool isLayerBackFaceVisible(LayerType* layer) |
| 81 | { |
| 82 | // The current W3C spec on CSS transforms says that backface visibility should be |
| 83 | // determined differently depending on whether the layer is in a "3d rendering |
| 84 | // context" or not. For Chromium code, we can determine whether we are in a 3d |
| 85 | // rendering context by checking if the parent preserves 3d. |
| 86 | |
| 87 | if (layerIsInExisting3DRenderingContext(layer)) |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 88 | return layer->drawTransform().IsBackFaceVisible(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 89 | |
| 90 | // In this case, either the layer establishes a new 3d rendering context, or is not in |
| 91 | // a 3d rendering context at all. |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 92 | return layer->transform().IsBackFaceVisible(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | template<typename LayerType> |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 96 | static bool isSurfaceBackFaceVisible(LayerType* layer, const gfx::Transform& drawTransform) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 97 | { |
| 98 | if (layerIsInExisting3DRenderingContext(layer)) |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 99 | return drawTransform.IsBackFaceVisible(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 100 | |
[email protected] | ecc1262 | 2012-10-30 20:45:42 | [diff] [blame] | 101 | if (isRootLayerOfNewRenderingContext(layer)) |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 102 | return layer->transform().IsBackFaceVisible(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 103 | |
| 104 | // If the renderSurface is not part of a new or existing rendering context, then the |
| 105 | // layers that contribute to this surface will decide back-face visibility for themselves. |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | template<typename LayerType> |
| 110 | static inline bool layerClipsSubtree(LayerType* layer) |
| 111 | { |
| 112 | return layer->masksToBounds() || layer->maskLayer(); |
| 113 | } |
| 114 | |
| 115 | template<typename LayerType> |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 116 | static gfx::Rect calculateVisibleContentRect(LayerType* layer) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 117 | { |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 118 | DCHECK(layer->renderTarget()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 119 | |
[email protected] | 9bab0bb | 2012-10-08 19:11:58 | [diff] [blame] | 120 | // Nothing is visible if the layer bounds are empty. |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 121 | if (!layer->drawsContent() || layer->contentBounds().IsEmpty() || layer->drawableContentRect().IsEmpty()) |
| 122 | return gfx::Rect(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 123 | |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 124 | gfx::Rect targetSurfaceClipRect; |
[email protected] | 9bab0bb | 2012-10-08 19:11:58 | [diff] [blame] | 125 | |
| 126 | // First, compute visible bounds in target surface space. |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 127 | if (layer->renderTarget()->renderSurface()->clipRect().IsEmpty()) |
[email protected] | 9bab0bb | 2012-10-08 19:11:58 | [diff] [blame] | 128 | targetSurfaceClipRect = layer->drawableContentRect(); |
| 129 | else { |
| 130 | // In this case the target surface does clip layers that contribute to it. So, we |
| 131 | // have convert the current surface's clipRect from its ancestor surface space to |
| 132 | // the current surface space. |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 133 | targetSurfaceClipRect = gfx::ToEnclosingRect(MathUtil::projectClippedRect(MathUtil::inverse(layer->renderTarget()->renderSurface()->drawTransform()), layer->renderTarget()->renderSurface()->clipRect())); |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 134 | targetSurfaceClipRect.Intersect(layer->drawableContentRect()); |
[email protected] | 9bab0bb | 2012-10-08 19:11:58 | [diff] [blame] | 135 | } |
| 136 | |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 137 | if (targetSurfaceClipRect.IsEmpty()) |
| 138 | return gfx::Rect(); |
[email protected] | 9bab0bb | 2012-10-08 19:11:58 | [diff] [blame] | 139 | |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 140 | return LayerTreeHostCommon::calculateVisibleRect(targetSurfaceClipRect, gfx::Rect(gfx::Point(), layer->contentBounds()), layer->drawTransform()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 141 | } |
| 142 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 143 | static inline bool transformToParentIsKnown(LayerImpl*) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 144 | { |
| 145 | return true; |
| 146 | } |
| 147 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 148 | static inline bool transformToParentIsKnown(Layer* layer) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 149 | { |
| 150 | return !layer->transformIsAnimating(); |
| 151 | } |
| 152 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 153 | static inline bool transformToScreenIsKnown(LayerImpl*) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 154 | { |
| 155 | return true; |
| 156 | } |
| 157 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 158 | static inline bool transformToScreenIsKnown(Layer* layer) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 159 | { |
| 160 | return !layer->screenSpaceTransformIsAnimating(); |
| 161 | } |
| 162 | |
| 163 | template<typename LayerType> |
| 164 | static bool layerShouldBeSkipped(LayerType* layer) |
| 165 | { |
| 166 | // Layers can be skipped if any of these conditions are met. |
| 167 | // - does not draw content. |
| 168 | // - is transparent |
| 169 | // - has empty bounds |
| 170 | // - the layer is not double-sided, but its back face is visible. |
| 171 | // |
| 172 | // Some additional conditions need to be computed at a later point after the recursion is finished. |
| 173 | // - the intersection of render surface content and layer clipRect is empty |
| 174 | // - the visibleContentRect is empty |
| 175 | // |
| 176 | // Note, if the layer should not have been drawn due to being fully transparent, |
| 177 | // we would have skipped the entire subtree and never made it into this function, |
| 178 | // so it is safe to omit this check here. |
| 179 | |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 180 | if (!layer->drawsContent() || layer->bounds().IsEmpty()) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 181 | return true; |
| 182 | |
| 183 | LayerType* backfaceTestLayer = layer; |
| 184 | if (layer->useParentBackfaceVisibility()) { |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 185 | DCHECK(layer->parent()); |
| 186 | DCHECK(!layer->parent()->useParentBackfaceVisibility()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 187 | backfaceTestLayer = layer->parent(); |
| 188 | } |
| 189 | |
| 190 | // 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. |
| 191 | if (!backfaceTestLayer->doubleSided() && transformToScreenIsKnown(backfaceTestLayer) && isLayerBackFaceVisible(backfaceTestLayer)) |
| 192 | return true; |
| 193 | |
| 194 | return false; |
| 195 | } |
| 196 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 197 | static inline bool subtreeShouldBeSkipped(LayerImpl* layer) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 198 | { |
| 199 | // The opacity of a layer always applies to its children (either implicitly |
| 200 | // via a render surface or explicitly if the parent preserves 3D), so the |
| 201 | // entire subtree can be skipped if this layer is fully transparent. |
| 202 | return !layer->opacity(); |
| 203 | } |
| 204 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 205 | static inline bool subtreeShouldBeSkipped(Layer* layer) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 206 | { |
| 207 | // If the opacity is being animated then the opacity on the main thread is unreliable |
| 208 | // (since the impl thread may be using a different opacity), so it should not be trusted. |
| 209 | // In particular, it should not cause the subtree to be skipped. |
| 210 | return !layer->opacity() && !layer->opacityIsAnimating(); |
| 211 | } |
| 212 | |
[email protected] | 6ef21ffc | 2012-11-28 05:58:54 | [diff] [blame] | 213 | // Called on each layer that could be drawn after all information from |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 214 | // calcDrawProperties has been updated on that layer. May have some false |
[email protected] | 6ef21ffc | 2012-11-28 05:58:54 | [diff] [blame] | 215 | // positives (e.g. layers get this called on them but don't actually get drawn). |
| 216 | static inline void markLayerAsUpdated(LayerImpl* layer) |
| 217 | { |
| 218 | layer->didUpdateTransforms(); |
| 219 | } |
| 220 | |
| 221 | static inline void markLayerAsUpdated(Layer* layer) |
| 222 | { |
| 223 | } |
| 224 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 225 | template<typename LayerType> |
| 226 | static bool subtreeShouldRenderToSeparateSurface(LayerType* layer, bool axisAlignedWithRespectToParent) |
| 227 | { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 228 | // |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 229 | // A layer and its descendants should render onto a new RenderSurfaceImpl if any of these rules hold: |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 230 | // |
| 231 | |
[email protected] | ecc1262 | 2012-10-30 20:45:42 | [diff] [blame] | 232 | // The root layer should always have a renderSurface. |
| 233 | if (isRootLayer(layer)) |
| 234 | return true; |
| 235 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 236 | // If we force it. |
| 237 | if (layer->forceRenderSurface()) |
| 238 | return true; |
| 239 | |
| 240 | // If the layer uses a mask. |
| 241 | if (layer->maskLayer()) |
| 242 | return true; |
| 243 | |
| 244 | // If the layer has a reflection. |
| 245 | if (layer->replicaLayer()) |
| 246 | return true; |
| 247 | |
| 248 | // If the layer uses a CSS filter. |
[email protected] | 4000abf | 2012-10-23 04:45:45 | [diff] [blame] | 249 | if (!layer->filters().isEmpty() || !layer->backgroundFilters().isEmpty() || layer->filter()) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 250 | return true; |
| 251 | |
[email protected] | ecc1262 | 2012-10-30 20:45:42 | [diff] [blame] | 252 | // Cache this value, because otherwise it walks the entire subtree several times. |
[email protected] | 498ec6e0e | 2012-11-30 18:24:57 | [diff] [blame] | 253 | int descendantsDrawContent = layer->descendantsDrawContent(); |
[email protected] | ecc1262 | 2012-10-30 20:45:42 | [diff] [blame] | 254 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 255 | // If the layer flattens its subtree (i.e. the layer doesn't preserve-3d), but it is |
| 256 | // treated as a 3D object by its parent (i.e. parent does preserve-3d). |
[email protected] | 498ec6e0e | 2012-11-30 18:24:57 | [diff] [blame] | 257 | if (layerIsInExisting3DRenderingContext(layer) && !layer->preserves3D() && descendantsDrawContent > 0) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 258 | return true; |
| 259 | |
| 260 | // If the layer clips its descendants but it is not axis-aligned with respect to its parent. |
[email protected] | 498ec6e0e | 2012-11-30 18:24:57 | [diff] [blame] | 261 | if (layerClipsSubtree(layer) && !axisAlignedWithRespectToParent && descendantsDrawContent > 0) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 262 | return true; |
| 263 | |
| 264 | // If the layer has opacity != 1 and does not have a preserves-3d transform style. |
[email protected] | 498ec6e0e | 2012-11-30 18:24:57 | [diff] [blame] | 265 | if (layer->opacity() != 1 && !layer->preserves3D() && descendantsDrawContent > 0 |
| 266 | && (layer->drawsContent() || descendantsDrawContent > 1)) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 267 | return true; |
| 268 | |
| 269 | return false; |
| 270 | } |
| 271 | |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 272 | gfx::Transform computeScrollCompensationForThisLayer(LayerImpl* scrollingLayer, const gfx::Transform& parentMatrix) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 273 | { |
| 274 | // For every layer that has non-zero scrollDelta, we have to compute a transform that can undo the |
| 275 | // scrollDelta translation. In particular, we want this matrix to premultiply a fixed-position layer's |
| 276 | // parentMatrix, so we design this transform in three steps as follows. The steps described here apply |
| 277 | // from right-to-left, so Step 1 would be the right-most matrix: |
| 278 | // |
| 279 | // Step 1. transform from target surface space to the exact space where scrollDelta is actually applied. |
| 280 | // -- this is inverse of the matrix in step 3 |
| 281 | // Step 2. undo the scrollDelta |
| 282 | // -- this is just a translation by scrollDelta. |
| 283 | // Step 3. transform back to target surface space. |
| 284 | // -- this transform is the "partialLayerOriginTransform" = (parentMatrix * scale(layer->pageScaleDelta())); |
| 285 | // |
| 286 | // These steps create a matrix that both start and end in targetSurfaceSpace. So this matrix can |
| 287 | // pre-multiply any fixed-position layer's drawTransform to undo the scrollDeltas -- as long as |
| 288 | // that fixed position layer is fixed onto the same renderTarget as this scrollingLayer. |
| 289 | // |
| 290 | |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 291 | gfx::Transform partialLayerOriginTransform = parentMatrix; |
| 292 | partialLayerOriginTransform.PreconcatTransform(scrollingLayer->implTransform()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 293 | |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 294 | gfx::Transform scrollCompensationForThisLayer = partialLayerOriginTransform; // Step 3 |
| 295 | scrollCompensationForThisLayer.Translate(scrollingLayer->scrollDelta().x(), scrollingLayer->scrollDelta().y()); // Step 2 |
| 296 | scrollCompensationForThisLayer.PreconcatTransform(MathUtil::inverse(partialLayerOriginTransform)); // Step 1 |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 297 | return scrollCompensationForThisLayer; |
| 298 | } |
| 299 | |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 300 | gfx::Transform computeScrollCompensationMatrixForChildren(Layer* currentLayer, const gfx::Transform& currentParentMatrix, const gfx::Transform& currentScrollCompensation) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 301 | { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 302 | // The main thread (i.e. Layer) does not need to worry about scroll compensation. |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 303 | // So we can just return an identity matrix here. |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 304 | return gfx::Transform(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 305 | } |
| 306 | |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 307 | gfx::Transform computeScrollCompensationMatrixForChildren(LayerImpl* layer, const gfx::Transform& parentMatrix, const gfx::Transform& currentScrollCompensationMatrix) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 308 | { |
| 309 | // "Total scroll compensation" is the transform needed to cancel out all scrollDelta translations that |
| 310 | // occurred since the nearest container layer, even if there are renderSurfaces in-between. |
| 311 | // |
| 312 | // There are some edge cases to be aware of, that are not explicit in the code: |
| 313 | // - A layer that is both a fixed-position and container should not be its own container, instead, that means |
| 314 | // it is fixed to an ancestor, and is a container for any fixed-position descendants. |
| 315 | // - A layer that is a fixed-position container and has a renderSurface should behave the same as a container |
| 316 | // without a renderSurface, the renderSurface is irrelevant in that case. |
[email protected] | ecc1262 | 2012-10-30 20:45:42 | [diff] [blame] | 317 | // - A layer that does not have an explicit container is simply fixed to the viewport. |
| 318 | // (i.e. the root renderSurface.) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 319 | // - If the fixed-position layer has its own renderSurface, then the renderSurface is |
| 320 | // the one who gets fixed. |
| 321 | // |
| 322 | // This function needs to be called AFTER layers create their own renderSurfaces. |
| 323 | // |
| 324 | |
| 325 | // 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] | c9c1ebe | 2012-11-05 20:46:13 | [diff] [blame] | 326 | if (!layer->isContainerForFixedPositionLayers() && layer->scrollDelta().IsZero() && !layer->renderSurface()) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 327 | return currentScrollCompensationMatrix; |
| 328 | |
| 329 | // Start as identity matrix. |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 330 | gfx::Transform nextScrollCompensationMatrix; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 331 | |
| 332 | // If this layer is not a container, then it inherits the existing scroll compensations. |
| 333 | if (!layer->isContainerForFixedPositionLayers()) |
| 334 | nextScrollCompensationMatrix = currentScrollCompensationMatrix; |
| 335 | |
| 336 | // If the current layer has a non-zero scrollDelta, then we should compute its local scrollCompensation |
| 337 | // and accumulate it to the nextScrollCompensationMatrix. |
[email protected] | c9c1ebe | 2012-11-05 20:46:13 | [diff] [blame] | 338 | if (!layer->scrollDelta().IsZero()) { |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 339 | gfx::Transform scrollCompensationForThisLayer = computeScrollCompensationForThisLayer(layer, parentMatrix); |
| 340 | nextScrollCompensationMatrix.PreconcatTransform(scrollCompensationForThisLayer); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | // If the layer created its own renderSurface, we have to adjust nextScrollCompensationMatrix. |
| 344 | // The adjustment allows us to continue using the scrollCompensation on the next surface. |
| 345 | // Step 1 (right-most in the math): transform from the new surface to the original ancestor surface |
| 346 | // Step 2: apply the scroll compensation |
| 347 | // Step 3: transform back to the new surface. |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 348 | if (layer->renderSurface() && !nextScrollCompensationMatrix.IsIdentity()) |
| 349 | nextScrollCompensationMatrix = MathUtil::inverse(layer->renderSurface()->drawTransform()) * nextScrollCompensationMatrix * layer->renderSurface()->drawTransform(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 350 | |
| 351 | return nextScrollCompensationMatrix; |
| 352 | } |
| 353 | |
[email protected] | 518ee58 | 2012-10-24 18:29:44 | [diff] [blame] | 354 | // There is no contentsScale on impl thread. |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 355 | static inline void updateLayerContentsScale(LayerImpl*, const gfx::Transform&, float, float, bool) { } |
[email protected] | 518ee58 | 2012-10-24 18:29:44 | [diff] [blame] | 356 | |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 357 | static inline void updateLayerContentsScale(Layer* layer, const gfx::Transform& combinedTransform, float deviceScaleFactor, float pageScaleFactor, bool animatingTransformToScreen) |
[email protected] | 518ee58 | 2012-10-24 18:29:44 | [diff] [blame] | 358 | { |
| 359 | float rasterScale = layer->rasterScale(); |
| 360 | if (!rasterScale) { |
| 361 | rasterScale = 1; |
| 362 | |
[email protected] | 6a9cff9 | 2012-11-08 11:53:26 | [diff] [blame] | 363 | if (!animatingTransformToScreen && layer->automaticallyComputeRasterScale()) { |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 364 | gfx::Vector2dF transformScale = MathUtil::computeTransform2dScaleComponents(combinedTransform); |
[email protected] | 518ee58 | 2012-10-24 18:29:44 | [diff] [blame] | 365 | float combinedScale = std::max(transformScale.x(), transformScale.y()); |
| 366 | rasterScale = combinedScale / deviceScaleFactor; |
| 367 | if (!layer->boundsContainPageScale()) |
| 368 | rasterScale /= pageScaleFactor; |
[email protected] | 11ec9297 | 2012-11-10 03:06:21 | [diff] [blame] | 369 | // Prevent scale factors below 1 from being used or saved. |
| 370 | if (rasterScale < 1) |
| 371 | rasterScale = 1; |
| 372 | else |
| 373 | layer->setRasterScale(rasterScale); |
[email protected] | 518ee58 | 2012-10-24 18:29:44 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
| 377 | float contentsScale = rasterScale * deviceScaleFactor; |
| 378 | if (!layer->boundsContainPageScale()) |
| 379 | contentsScale *= pageScaleFactor; |
| 380 | layer->setContentsScale(contentsScale); |
| 381 | |
| 382 | Layer* maskLayer = layer->maskLayer(); |
| 383 | if (maskLayer) |
| 384 | maskLayer->setContentsScale(contentsScale); |
| 385 | |
| 386 | Layer* replicaMaskLayer = layer->replicaLayer() ? layer->replicaLayer()->maskLayer() : 0; |
| 387 | if (replicaMaskLayer) |
| 388 | replicaMaskLayer->setContentsScale(contentsScale); |
| 389 | } |
| 390 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 391 | // Recursively walks the layer tree starting at the given node and computes all the |
| 392 | // necessary transformations, clipRects, render surfaces, etc. |
| 393 | template<typename LayerType, typename LayerList, typename RenderSurfaceType, typename LayerSorter> |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 394 | static void calculateDrawPropertiesInternal(LayerType* layer, const gfx::Transform& parentMatrix, |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 395 | const gfx::Transform& fullHierarchyMatrix, const gfx::Transform& currentScrollCompensationMatrix, |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 396 | const gfx::Rect& clipRectFromAncestor, bool ancestorClipsSubtree, |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 397 | RenderSurfaceType* nearestAncestorThatMovesPixels, LayerList& renderSurfaceLayerList, LayerList& layerList, |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 398 | LayerSorter* layerSorter, int maxTextureSize, float deviceScaleFactor, float pageScaleFactor, gfx::Rect& drawableContentRectOfSubtree) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 399 | { |
| 400 | // This function computes the new matrix transformations recursively for this |
| 401 | // layer and all its descendants. It also computes the appropriate render surfaces. |
| 402 | // Some important points to remember: |
| 403 | // |
| 404 | // 0. Here, transforms are notated in Matrix x Vector order, and in words we describe what |
| 405 | // the transform does from left to right. |
| 406 | // |
| 407 | // 1. In our terminology, the "layer origin" refers to the top-left corner of a layer, and the |
| 408 | // positive Y-axis points downwards. This interpretation is valid because the orthographic |
| 409 | // projection applied at draw time flips the Y axis appropriately. |
| 410 | // |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 411 | // 2. The anchor point, when given as a PointF object, is specified in "unit layer space", |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 412 | // where the bounds of the layer map to [0, 1]. However, as a Transform object, |
[email protected] | b8d3c02 | 2012-10-08 17:38:04 | [diff] [blame] | 413 | // the transform to the anchor point is specified in "layer space", where the bounds |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 414 | // of the layer map to [bounds.width(), bounds.height()]. |
| 415 | // |
| 416 | // 3. Definition of various transforms used: |
| 417 | // M[parent] is the parent matrix, with respect to the nearest render surface, passed down recursively. |
| 418 | // M[root] is the full hierarchy, with respect to the root, passed down recursively. |
| 419 | // Tr[origin] is the translation matrix from the parent's origin to this layer's origin. |
| 420 | // Tr[origin2anchor] is the translation from the layer's origin to its anchor point |
| 421 | // Tr[origin2center] is the translation from the layer's origin to its center |
| 422 | // M[layer] is the layer's matrix (applied at the anchor point) |
| 423 | // M[sublayer] is the layer's sublayer transform (applied at the layer's center) |
[email protected] | b8d3c02 | 2012-10-08 17:38:04 | [diff] [blame] | 424 | // S[layer2content] is the ratio of a layer's contentBounds() to its bounds(). |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 425 | // |
| 426 | // Some composite transforms can help in understanding the sequence of transforms: |
| 427 | // compositeLayerTransform = Tr[origin2anchor] * M[layer] * Tr[origin2anchor].inverse() |
| 428 | // compositeSublayerTransform = Tr[origin2center] * M[sublayer] * Tr[origin2center].inverse() |
| 429 | // |
| 430 | // In words, the layer transform is applied about the anchor point, and the sublayer transform is |
| 431 | // applied about the center of the layer. |
| 432 | // |
| 433 | // 4. When a layer (or render surface) is drawn, it is drawn into a "target render surface". Therefore the draw |
| 434 | // transform does not necessarily transform from screen space to local layer space. Instead, the draw transform |
| 435 | // is the transform between the "target render surface space" and local layer space. Note that render surfaces, |
| 436 | // except for the root, also draw themselves into a different target render surface, and so their draw |
| 437 | // transform and origin transforms are also described with respect to the target. |
| 438 | // |
| 439 | // Using these definitions, then: |
| 440 | // |
| 441 | // The draw transform for the layer is: |
[email protected] | b8d3c02 | 2012-10-08 17:38:04 | [diff] [blame] | 442 | // M[draw] = M[parent] * Tr[origin] * compositeLayerTransform * S[layer2content] |
| 443 | // = M[parent] * Tr[layer->position() + anchor] * M[layer] * Tr[anchor2origin] * S[layer2content] |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 444 | // |
| 445 | // Interpreting the math left-to-right, this transforms from the layer's render surface to the origin of the layer in content space. |
| 446 | // |
| 447 | // The screen space transform is: |
[email protected] | b8d3c02 | 2012-10-08 17:38:04 | [diff] [blame] | 448 | // M[screenspace] = M[root] * Tr[origin] * compositeLayerTransform * S[layer2content] |
| 449 | // = M[root] * Tr[layer->position() + anchor] * M[layer] * Tr[anchor2origin] * S[layer2content] |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 450 | // |
[email protected] | 655fa7f9 | 2012-11-15 00:14:33 | [diff] [blame] | 451 | // 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] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 452 | // |
| 453 | // The transform hierarchy that is passed on to children (i.e. the child's parentMatrix) is: |
| 454 | // M[parent]_for_child = M[parent] * Tr[origin] * compositeLayerTransform * compositeSublayerTransform |
[email protected] | b8d3c02 | 2012-10-08 17:38:04 | [diff] [blame] | 455 | // = M[parent] * Tr[layer->position() + anchor] * M[layer] * Tr[anchor2origin] * compositeSublayerTransform |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 456 | // |
| 457 | // and a similar matrix for the full hierarchy with respect to the root. |
| 458 | // |
| 459 | // Finally, note that the final matrix used by the shader for the layer is P * M[draw] * S . This final product |
| 460 | // is computed in drawTexturedQuad(), where: |
| 461 | // P is the projection matrix |
[email protected] | b8d3c02 | 2012-10-08 17:38:04 | [diff] [blame] | 462 | // S is the scale adjustment (to scale up a canonical quad to the layer's size) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 463 | // |
| 464 | // When a render surface has a replica layer, that layer's transform is used to draw a second copy of the surface. |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 465 | // gfx::Transforms named here are relative to the surface, unless they specify they are relative to the replica layer. |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 466 | // |
| 467 | // We will denote a scale by device scale S[deviceScale] |
| 468 | // |
| 469 | // The render surface draw transform to its target surface origin is: |
| 470 | // M[surfaceDraw] = M[owningLayer->Draw] |
| 471 | // |
| 472 | // The render surface origin transform to its the root (screen space) origin is: |
| 473 | // M[surface2root] = M[owningLayer->screenspace] * S[deviceScale].inverse() |
| 474 | // |
| 475 | // The replica draw transform to its target surface origin is: |
| 476 | // M[replicaDraw] = S[deviceScale] * M[surfaceDraw] * Tr[replica->position() + replica->anchor()] * Tr[replica] * Tr[origin2anchor].inverse() * S[contentsScale].inverse() |
| 477 | // |
| 478 | // The replica draw transform to the root (screen space) origin is: |
| 479 | // M[replica2root] = M[surface2root] * Tr[replica->position()] * Tr[replica] * Tr[origin2anchor].inverse() |
| 480 | // |
| 481 | |
| 482 | // If we early-exit anywhere in this function, the drawableContentRect of this subtree should be considered empty. |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 483 | drawableContentRectOfSubtree = gfx::Rect(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 484 | |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 485 | // The root layer cannot skip calcDrawProperties. |
[email protected] | ecc1262 | 2012-10-30 20:45:42 | [diff] [blame] | 486 | if (!isRootLayer(layer) && subtreeShouldBeSkipped(layer)) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 487 | return; |
| 488 | |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 489 | // As this function proceeds, these are the properties for the current |
| 490 | // layer that actually get computed. To avoid unnecessary copies |
| 491 | // (particularly for matrices), we do computations directly on these values |
| 492 | // when possible. |
| 493 | DrawProperties<LayerType, RenderSurfaceType>& layerDrawProperties = layer->drawProperties(); |
| 494 | |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 495 | gfx::Rect clipRectForSubtree; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 496 | bool subtreeShouldBeClipped = false; |
[email protected] | 498ec6e0e | 2012-11-30 18:24:57 | [diff] [blame] | 497 | |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 498 | float accumulatedDrawOpacity = layer->opacity(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 499 | bool drawOpacityIsAnimating = layer->opacityIsAnimating(); |
[email protected] | 498ec6e0e | 2012-11-30 18:24:57 | [diff] [blame] | 500 | if (layer->parent()) { |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 501 | accumulatedDrawOpacity *= layer->parent()->drawOpacity(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 502 | drawOpacityIsAnimating |= layer->parent()->drawOpacityIsAnimating(); |
| 503 | } |
| 504 | |
[email protected] | 6a9cff9 | 2012-11-08 11:53:26 | [diff] [blame] | 505 | bool animatingTransformToTarget = layer->transformIsAnimating(); |
| 506 | bool animatingTransformToScreen = animatingTransformToTarget; |
| 507 | if (layer->parent()) { |
| 508 | animatingTransformToTarget |= layer->parent()->drawTransformIsAnimating(); |
| 509 | animatingTransformToScreen |= layer->parent()->screenSpaceTransformIsAnimating(); |
| 510 | } |
| 511 | |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 512 | gfx::Size bounds = layer->bounds(); |
| 513 | gfx::PointF anchorPoint = layer->anchorPoint(); |
[email protected] | c9c1ebe | 2012-11-05 20:46:13 | [diff] [blame] | 514 | gfx::PointF position = layer->position() - layer->scrollDelta(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 515 | |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 516 | gfx::Transform combinedTransform = parentMatrix; |
[email protected] | b9ab6d54 | 2012-12-04 21:33:06 | [diff] [blame] | 517 | // LT = Tr[origin] * Tr[origin2anchor] |
| 518 | combinedTransform.Translate3d(position.x() + anchorPoint.x() * bounds.width(), position.y() + anchorPoint.y() * bounds.height(), layer->anchorPointZ()); |
| 519 | // LT = Tr[origin] * Tr[origin2anchor] * M[layer] |
| 520 | combinedTransform.PreconcatTransform(layer->transform()); |
| 521 | // LT = Tr[origin] * Tr[origin2anchor] * M[layer] * Tr[anchor2origin] |
| 522 | combinedTransform.Translate3d(-anchorPoint.x() * bounds.width(), -anchorPoint.y() * bounds.height(), -layer->anchorPointZ()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 523 | |
[email protected] | 518ee58 | 2012-10-24 18:29:44 | [diff] [blame] | 524 | // The layer's contentsSize is determined from the combinedTransform, which then informs the |
| 525 | // layer's drawTransform. |
[email protected] | 6a9cff9 | 2012-11-08 11:53:26 | [diff] [blame] | 526 | updateLayerContentsScale(layer, combinedTransform, deviceScaleFactor, pageScaleFactor, animatingTransformToScreen); |
[email protected] | 518ee58 | 2012-10-24 18:29:44 | [diff] [blame] | 527 | |
[email protected] | b9ab6d54 | 2012-12-04 21:33:06 | [diff] [blame] | 528 | // If there is a transformation from the impl thread then it should be at |
| 529 | // the start of the combinedTransform, but we don't want it to affect the |
| 530 | // computation of contentsScale above. |
| 531 | // Note carefully: this is Concat, not Preconcat (implTransform * combinedTransform). |
| 532 | combinedTransform.ConcatTransform(layer->implTransform()); |
[email protected] | 518ee58 | 2012-10-24 18:29:44 | [diff] [blame] | 533 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 534 | if (layer->fixedToContainerLayer()) { |
| 535 | // Special case: this layer is a composited fixed-position layer; we need to |
| 536 | // explicitly compensate for all ancestors' nonzero scrollDeltas to keep this layer |
| 537 | // fixed correctly. |
[email protected] | b9ab6d54 | 2012-12-04 21:33:06 | [diff] [blame] | 538 | // Note carefully: this is Concat, not Preconcat (currentScrollCompensation * combinedTransform). |
| 539 | combinedTransform.ConcatTransform(currentScrollCompensationMatrix); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | // The drawTransform that gets computed below is effectively the layer's drawTransform, unless |
| 543 | // the layer itself creates a renderSurface. In that case, the renderSurface re-parents the transforms. |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 544 | layerDrawProperties.target_space_transform = combinedTransform; |
[email protected] | f89f563 | 2012-11-14 23:34:45 | [diff] [blame] | 545 | // M[draw] = M[parent] * LT * S[layer2content] |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 546 | layerDrawProperties.target_space_transform.Scale(1.0 / layer->contentsScaleX(), 1.0 / layer->contentsScaleY()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 547 | |
| 548 | // layerScreenSpaceTransform represents the transform between root layer's "screen space" and local content space. |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 549 | layerDrawProperties.screen_space_transform = fullHierarchyMatrix; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 550 | if (!layer->preserves3D()) |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 551 | MathUtil::flattenTransformTo2d(layerDrawProperties.screen_space_transform); |
| 552 | layerDrawProperties.screen_space_transform.PreconcatTransform(layerDrawProperties.target_space_transform); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 553 | |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 554 | gfx::RectF contentRect(gfx::PointF(), layer->contentBounds()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 555 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 556 | // fullHierarchyMatrix is the matrix that transforms objects between screen space (except projection matrix) and the most recent RenderSurfaceImpl's space. |
| 557 | // nextHierarchyMatrix will only change if this layer uses a new RenderSurfaceImpl, otherwise remains the same. |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 558 | gfx::Transform nextHierarchyMatrix = fullHierarchyMatrix; |
| 559 | gfx::Transform sublayerMatrix; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 560 | |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 561 | gfx::Vector2dF renderSurfaceSublayerScale = MathUtil::computeTransform2dScaleComponents(combinedTransform); |
[email protected] | 518ee58 | 2012-10-24 18:29:44 | [diff] [blame] | 562 | |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 563 | if (subtreeShouldRenderToSeparateSurface(layer, combinedTransform.IsScaleOrTranslation())) { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 564 | // Check back-face visibility before continuing with this surface and its subtree |
| 565 | if (!layer->doubleSided() && transformToParentIsKnown(layer) && isSurfaceBackFaceVisible(layer, combinedTransform)) |
| 566 | return; |
| 567 | |
| 568 | if (!layer->renderSurface()) |
| 569 | layer->createRenderSurface(); |
| 570 | |
| 571 | RenderSurfaceType* renderSurface = layer->renderSurface(); |
[email protected] | 7d929c0 | 2012-09-20 17:26:57 | [diff] [blame] | 572 | renderSurface->clearLayerLists(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 573 | |
[email protected] | b9ab6d54 | 2012-12-04 21:33:06 | [diff] [blame] | 574 | // The owning layer's draw transform has a scale from content to layer |
| 575 | // space which we do not want; so here we use the combinedTransform |
| 576 | // instead of the drawTransform. However, we do need to add a different |
| 577 | // scale factor that accounts for the surface's pixel dimensions. |
| 578 | combinedTransform.Scale(1 / renderSurfaceSublayerScale.x(), 1 / renderSurfaceSublayerScale.y()); |
| 579 | renderSurface->setDrawTransform(combinedTransform); |
[email protected] | 518ee58 | 2012-10-24 18:29:44 | [diff] [blame] | 580 | |
[email protected] | b9ab6d54 | 2012-12-04 21:33:06 | [diff] [blame] | 581 | // The owning layer's transform was re-parented by the surface, so the layer's new drawTransform |
| 582 | // only needs to scale the layer to surface space. |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 583 | layerDrawProperties.target_space_transform.MakeIdentity(); |
| 584 | layerDrawProperties.target_space_transform.Scale(renderSurfaceSublayerScale.x() / layer->contentsScaleX(), renderSurfaceSublayerScale.y() / layer->contentsScaleY()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 585 | |
[email protected] | 518ee58 | 2012-10-24 18:29:44 | [diff] [blame] | 586 | // Inside the surface's subtree, we scale everything to the owning layer's scale. |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 587 | // The sublayer matrix transforms centered layer rects into target |
| 588 | // surface content space. |
[email protected] | b9ab6d54 | 2012-12-04 21:33:06 | [diff] [blame] | 589 | DCHECK(sublayerMatrix.IsIdentity()); |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 590 | sublayerMatrix.Scale(renderSurfaceSublayerScale.x(), renderSurfaceSublayerScale.y()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 591 | |
| 592 | // The opacity value is moved from the layer to its surface, so that the entire subtree properly inherits opacity. |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 593 | renderSurface->setDrawOpacity(accumulatedDrawOpacity); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 594 | renderSurface->setDrawOpacityIsAnimating(drawOpacityIsAnimating); |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 595 | layerDrawProperties.opacity = 1; |
| 596 | layerDrawProperties.opacity_is_animating = false; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 597 | |
| 598 | renderSurface->setTargetSurfaceTransformsAreAnimating(animatingTransformToTarget); |
| 599 | renderSurface->setScreenSpaceTransformsAreAnimating(animatingTransformToScreen); |
| 600 | animatingTransformToTarget = false; |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 601 | layerDrawProperties.target_space_transform_is_animating = animatingTransformToTarget; |
| 602 | layerDrawProperties.screen_space_transform_is_animating = animatingTransformToScreen; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 603 | |
| 604 | // Update the aggregate hierarchy matrix to include the transform of the |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 605 | // newly created RenderSurfaceImpl. |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 606 | nextHierarchyMatrix.PreconcatTransform(renderSurface->drawTransform()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 607 | |
| 608 | // The new renderSurface here will correctly clip the entire subtree. So, we do |
| 609 | // not need to continue propagating the clipping state further down the tree. This |
| 610 | // way, we can avoid transforming clipRects from ancestor target surface space to |
| 611 | // current target surface space that could cause more w < 0 headaches. |
| 612 | subtreeShouldBeClipped = false; |
| 613 | |
[email protected] | 9bab0bb | 2012-10-08 19:11:58 | [diff] [blame] | 614 | if (layer->maskLayer()) { |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 615 | DrawProperties<LayerType, RenderSurfaceType>& maskLayerDrawProperties = layer->maskLayer()->drawProperties(); |
| 616 | maskLayerDrawProperties.render_target = layer; |
| 617 | maskLayerDrawProperties.visible_content_rect = gfx::Rect(gfx::Point(), layer->contentBounds()); |
[email protected] | 9bab0bb | 2012-10-08 19:11:58 | [diff] [blame] | 618 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 619 | |
[email protected] | 9bab0bb | 2012-10-08 19:11:58 | [diff] [blame] | 620 | if (layer->replicaLayer() && layer->replicaLayer()->maskLayer()) { |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 621 | DrawProperties<LayerType, RenderSurfaceType>& replicaMaskDrawProperties = layer->replicaLayer()->maskLayer()->drawProperties(); |
| 622 | replicaMaskDrawProperties.render_target = layer; |
| 623 | replicaMaskDrawProperties.visible_content_rect = gfx::Rect(gfx::Point(), layer->contentBounds()); |
[email protected] | 9bab0bb | 2012-10-08 19:11:58 | [diff] [blame] | 624 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 625 | |
[email protected] | 4000abf | 2012-10-23 04:45:45 | [diff] [blame] | 626 | // FIXME: make this smarter for the SkImageFilter case (check for |
| 627 | // pixel-moving filters) |
| 628 | if (layer->filters().hasFilterThatMovesPixels() || layer->filter()) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 629 | nearestAncestorThatMovesPixels = renderSurface; |
| 630 | |
[email protected] | 9bab0bb | 2012-10-08 19:11:58 | [diff] [blame] | 631 | // The render surface clipRect is expressed in the space where this surface draws, i.e. the same space as clipRectFromAncestor. |
[email protected] | dc462d78 | 2012-11-21 21:43:01 | [diff] [blame] | 632 | renderSurface->setIsClipped(ancestorClipsSubtree); |
[email protected] | 9bab0bb | 2012-10-08 19:11:58 | [diff] [blame] | 633 | if (ancestorClipsSubtree) |
| 634 | renderSurface->setClipRect(clipRectFromAncestor); |
| 635 | else |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 636 | renderSurface->setClipRect(gfx::Rect()); |
[email protected] | 9bab0bb | 2012-10-08 19:11:58 | [diff] [blame] | 637 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 638 | renderSurface->setNearestAncestorThatMovesPixels(nearestAncestorThatMovesPixels); |
| 639 | |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 640 | renderSurfaceLayerList.push_back(layer); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 641 | } else { |
[email protected] | ecc1262 | 2012-10-30 20:45:42 | [diff] [blame] | 642 | DCHECK(layer->parent()); |
| 643 | |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 644 | // Note: layerDrawProperties.target_space_transform is computed above, |
| 645 | // before this if-else statement. |
| 646 | layerDrawProperties.target_space_transform_is_animating = animatingTransformToTarget; |
| 647 | layerDrawProperties.screen_space_transform_is_animating = animatingTransformToScreen; |
| 648 | layerDrawProperties.opacity = accumulatedDrawOpacity; |
| 649 | layerDrawProperties.opacity_is_animating = drawOpacityIsAnimating; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 650 | sublayerMatrix = combinedTransform; |
| 651 | |
[email protected] | ecc1262 | 2012-10-30 20:45:42 | [diff] [blame] | 652 | layer->clearRenderSurface(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 653 | |
[email protected] | ecc1262 | 2012-10-30 20:45:42 | [diff] [blame] | 654 | // Layers without renderSurfaces directly inherit the ancestor's clip status. |
| 655 | subtreeShouldBeClipped = ancestorClipsSubtree; |
| 656 | if (ancestorClipsSubtree) |
| 657 | clipRectForSubtree = clipRectFromAncestor; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 658 | |
[email protected] | ecc1262 | 2012-10-30 20:45:42 | [diff] [blame] | 659 | // Layers that are not their own renderTarget will render into the target of their nearest ancestor. |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 660 | layerDrawProperties.render_target = layer->parent()->renderTarget(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 661 | } |
| 662 | |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 663 | gfx::Rect rectInTargetSpace = ToEnclosingRect(MathUtil::mapClippedRect(layer->drawTransform(), contentRect)); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 664 | |
| 665 | if (layerClipsSubtree(layer)) { |
| 666 | subtreeShouldBeClipped = true; |
| 667 | if (ancestorClipsSubtree && !layer->renderSurface()) { |
| 668 | clipRectForSubtree = clipRectFromAncestor; |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 669 | clipRectForSubtree.Intersect(rectInTargetSpace); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 670 | } else |
| 671 | clipRectForSubtree = rectInTargetSpace; |
| 672 | } |
| 673 | |
| 674 | // Flatten to 2D if the layer doesn't preserve 3D. |
| 675 | if (!layer->preserves3D()) |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 676 | MathUtil::flattenTransformTo2d(sublayerMatrix); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 677 | |
| 678 | // Apply the sublayer transform at the center of the layer. |
[email protected] | b9ab6d54 | 2012-12-04 21:33:06 | [diff] [blame] | 679 | if (!layer->sublayerTransform().IsIdentity()) { |
| 680 | sublayerMatrix.Translate(0.5 * bounds.width(), 0.5 * bounds.height()); |
| 681 | sublayerMatrix.PreconcatTransform(layer->sublayerTransform()); |
| 682 | sublayerMatrix.Translate(-0.5 * bounds.width(), -0.5 * bounds.height()); |
| 683 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 684 | |
| 685 | LayerList& descendants = (layer->renderSurface() ? layer->renderSurface()->layerList() : layerList); |
| 686 | |
| 687 | // Any layers that are appended after this point are in the layer's subtree and should be included in the sorting process. |
| 688 | unsigned sortingStartIndex = descendants.size(); |
| 689 | |
| 690 | if (!layerShouldBeSkipped(layer)) |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 691 | descendants.push_back(layer); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 692 | |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 693 | gfx::Transform nextScrollCompensationMatrix = computeScrollCompensationMatrixForChildren(layer, parentMatrix, currentScrollCompensationMatrix);; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 694 | |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 695 | gfx::Rect accumulatedDrawableContentRectOfChildren; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 696 | for (size_t i = 0; i < layer->children().size(); ++i) { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 697 | LayerType* child = LayerTreeHostCommon::getChildAsRawPtr(layer->children(), i); |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 698 | gfx::Rect drawableContentRectOfChildSubtree; |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 699 | calculateDrawPropertiesInternal<LayerType, LayerList, RenderSurfaceType, LayerSorter>(child, sublayerMatrix, nextHierarchyMatrix, nextScrollCompensationMatrix, |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 700 | clipRectForSubtree, subtreeShouldBeClipped, nearestAncestorThatMovesPixels, |
[email protected] | 518ee58 | 2012-10-24 18:29:44 | [diff] [blame] | 701 | renderSurfaceLayerList, descendants, layerSorter, maxTextureSize, deviceScaleFactor, pageScaleFactor, drawableContentRectOfChildSubtree); |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 702 | if (!drawableContentRectOfChildSubtree.IsEmpty()) { |
| 703 | accumulatedDrawableContentRectOfChildren.Union(drawableContentRectOfChildSubtree); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 704 | if (child->renderSurface()) |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 705 | descendants.push_back(child); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 706 | } |
| 707 | } |
| 708 | |
| 709 | // Compute the total drawableContentRect for this subtree (the rect is in targetSurface space) |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 710 | gfx::Rect localDrawableContentRectOfSubtree = accumulatedDrawableContentRectOfChildren; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 711 | if (layer->drawsContent()) |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 712 | localDrawableContentRectOfSubtree.Union(rectInTargetSpace); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 713 | if (subtreeShouldBeClipped) |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 714 | localDrawableContentRectOfSubtree.Intersect(clipRectForSubtree); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 715 | |
| 716 | // Compute the layer's drawable content rect (the rect is in targetSurface space) |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 717 | layerDrawProperties.drawable_content_rect = rectInTargetSpace; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 718 | if (subtreeShouldBeClipped) |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 719 | layerDrawProperties.drawable_content_rect.Intersect(clipRectForSubtree); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 720 | |
[email protected] | dc462d78 | 2012-11-21 21:43:01 | [diff] [blame] | 721 | // Tell the layer the rect that is clipped by. In theory we could use a |
| 722 | // tighter clipRect here (drawableContentRect), but that actually does not |
| 723 | // reduce how much would be drawn, and instead it would create unnecessary |
| 724 | // changes to scissor state affecting GPU performance. |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 725 | layerDrawProperties.is_clipped = subtreeShouldBeClipped; |
[email protected] | dc462d78 | 2012-11-21 21:43:01 | [diff] [blame] | 726 | if (subtreeShouldBeClipped) |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 727 | layerDrawProperties.clip_rect = clipRectForSubtree; |
[email protected] | dc462d78 | 2012-11-21 21:43:01 | [diff] [blame] | 728 | else { |
| 729 | // Initialize the clipRect to a safe value that will not clip the |
| 730 | // layer, just in case clipping is still accidentally used. |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 731 | layerDrawProperties.clip_rect = rectInTargetSpace; |
[email protected] | dc462d78 | 2012-11-21 21:43:01 | [diff] [blame] | 732 | } |
| 733 | |
[email protected] | 9bab0bb | 2012-10-08 19:11:58 | [diff] [blame] | 734 | // Compute the layer's visible content rect (the rect is in content space) |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 735 | layerDrawProperties.visible_content_rect = calculateVisibleContentRect(layer); |
[email protected] | 9bab0bb | 2012-10-08 19:11:58 | [diff] [blame] | 736 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 737 | // Compute the remaining properties for the render surface, if the layer has one. |
[email protected] | ecc1262 | 2012-10-30 20:45:42 | [diff] [blame] | 738 | if (isRootLayer(layer)) { |
| 739 | // The root layer's surface's contentRect is always the entire viewport. |
| 740 | DCHECK(layer->renderSurface()); |
| 741 | layer->renderSurface()->setContentRect(clipRectFromAncestor); |
| 742 | } else if (layer->renderSurface() && !isRootLayer(layer)) { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 743 | RenderSurfaceType* renderSurface = layer->renderSurface(); |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 744 | gfx::Rect clippedContentRect = localDrawableContentRectOfSubtree; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 745 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 746 | // Don't clip if the layer is reflected as the reflection shouldn't be |
| 747 | // clipped. If the layer is animating, then the surface's transform to |
| 748 | // its target is not known on the main thread, and we should not use it |
| 749 | // to clip. |
| 750 | if (!layer->replicaLayer() && transformToParentIsKnown(layer)) { |
| 751 | // Note, it is correct to use ancestorClipsSubtree here, because we are looking at this layer's renderSurface, not the layer itself. |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 752 | if (ancestorClipsSubtree && !clippedContentRect.IsEmpty()) { |
| 753 | gfx::Rect surfaceClipRect = LayerTreeHostCommon::calculateVisibleRect(renderSurface->clipRect(), clippedContentRect, renderSurface->drawTransform()); |
| 754 | clippedContentRect.Intersect(surfaceClipRect); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 758 | // The RenderSurfaceImpl backing texture cannot exceed the maximum supported |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 759 | // texture size. |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 760 | clippedContentRect.set_width(std::min(clippedContentRect.width(), maxTextureSize)); |
| 761 | clippedContentRect.set_height(std::min(clippedContentRect.height(), maxTextureSize)); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 762 | |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 763 | if (clippedContentRect.IsEmpty()) |
[email protected] | 7d929c0 | 2012-09-20 17:26:57 | [diff] [blame] | 764 | renderSurface->clearLayerLists(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 765 | |
| 766 | renderSurface->setContentRect(clippedContentRect); |
[email protected] | 518ee58 | 2012-10-24 18:29:44 | [diff] [blame] | 767 | |
| 768 | // The owning layer's screenSpaceTransform has a scale from content to layer space which we need to undo and |
| 769 | // replace with a scale from the surface's subtree into layer space. |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 770 | gfx::Transform screenSpaceTransform = layer->screenSpaceTransform(); |
[email protected] | b9ab6d54 | 2012-12-04 21:33:06 | [diff] [blame] | 771 | screenSpaceTransform.Scale(layer->contentsScaleX() / renderSurfaceSublayerScale.x(), layer->contentsScaleY() / renderSurfaceSublayerScale.y()); |
[email protected] | 518ee58 | 2012-10-24 18:29:44 | [diff] [blame] | 772 | renderSurface->setScreenSpaceTransform(screenSpaceTransform); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 773 | |
| 774 | if (layer->replicaLayer()) { |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 775 | gfx::Transform surfaceOriginToReplicaOriginTransform; |
| 776 | surfaceOriginToReplicaOriginTransform.Scale(renderSurfaceSublayerScale.x(), renderSurfaceSublayerScale.y()); |
| 777 | surfaceOriginToReplicaOriginTransform.Translate(layer->replicaLayer()->position().x() + layer->replicaLayer()->anchorPoint().x() * bounds.width(), |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 778 | layer->replicaLayer()->position().y() + layer->replicaLayer()->anchorPoint().y() * bounds.height()); |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 779 | surfaceOriginToReplicaOriginTransform.PreconcatTransform(layer->replicaLayer()->transform()); |
| 780 | surfaceOriginToReplicaOriginTransform.Translate(-layer->replicaLayer()->anchorPoint().x() * bounds.width(), -layer->replicaLayer()->anchorPoint().y() * bounds.height()); |
| 781 | surfaceOriginToReplicaOriginTransform.Scale(1 / renderSurfaceSublayerScale.x(), 1 / renderSurfaceSublayerScale.y()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 782 | |
| 783 | // Compute the replica's "originTransform" that maps from the replica's origin space to the target surface origin space. |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 784 | gfx::Transform replicaOriginTransform = layer->renderSurface()->drawTransform() * surfaceOriginToReplicaOriginTransform; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 785 | renderSurface->setReplicaDrawTransform(replicaOriginTransform); |
| 786 | |
| 787 | // Compute the replica's "screenSpaceTransform" that maps from the replica's origin space to the screen's origin space. |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 788 | gfx::Transform replicaScreenSpaceTransform = layer->renderSurface()->screenSpaceTransform() * surfaceOriginToReplicaOriginTransform; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 789 | renderSurface->setReplicaScreenSpaceTransform(replicaScreenSpaceTransform); |
| 790 | } |
| 791 | |
| 792 | // If a render surface has no layer list, then it and none of its children needed to get drawn. |
| 793 | if (!layer->renderSurface()->layerList().size()) { |
| 794 | // FIXME: Originally we asserted that this layer was already at the end of the |
| 795 | // list, and only needed to remove that layer. For now, we remove the |
| 796 | // entire subtree of surfaces to fix a crash bug. The root cause is |
| 797 | // https://bugs.webkit.org/show_bug.cgi?id=74147 and we should be able |
| 798 | // to put the original assert after fixing that. |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 799 | while (renderSurfaceLayerList.back() != layer) { |
| 800 | renderSurfaceLayerList.back()->clearRenderSurface(); |
| 801 | renderSurfaceLayerList.pop_back(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 802 | } |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 803 | DCHECK(renderSurfaceLayerList.back() == layer); |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 804 | renderSurfaceLayerList.pop_back(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 805 | layer->clearRenderSurface(); |
| 806 | return; |
| 807 | } |
| 808 | } |
| 809 | |
[email protected] | 6ef21ffc | 2012-11-28 05:58:54 | [diff] [blame] | 810 | markLayerAsUpdated(layer); |
| 811 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 812 | // If neither this layer nor any of its children were added, early out. |
| 813 | if (sortingStartIndex == descendants.size()) |
| 814 | return; |
| 815 | |
| 816 | // If preserves-3d then sort all the descendants in 3D so that they can be |
| 817 | // drawn from back to front. If the preserves-3d property is also set on the parent then |
| 818 | // skip the sorting as the parent will sort all the descendants anyway. |
| 819 | if (descendants.size() && layer->preserves3D() && (!layer->parent() || !layer->parent()->preserves3D())) |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 820 | sortLayers(descendants.begin() + sortingStartIndex, descendants.end(), layerSorter); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 821 | |
| 822 | if (layer->renderSurface()) |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 823 | drawableContentRectOfSubtree = gfx::ToEnclosingRect(layer->renderSurface()->drawableContentRect()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 824 | else |
| 825 | drawableContentRectOfSubtree = localDrawableContentRectOfSubtree; |
| 826 | |
[email protected] | 7d929c0 | 2012-09-20 17:26:57 | [diff] [blame] | 827 | if (layer->hasContributingDelegatedRenderPasses()) |
| 828 | layer->renderTarget()->renderSurface()->addContributingDelegatedRenderPassLayer(layer); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 829 | } |
| 830 | |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 831 | void LayerTreeHostCommon::calculateDrawProperties(Layer* rootLayer, const gfx::Size& deviceViewportSize, float deviceScaleFactor, float pageScaleFactor, int maxTextureSize, std::vector<scoped_refptr<Layer> >& renderSurfaceLayerList) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 832 | { |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 833 | gfx::Rect totalDrawableContentRect; |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 834 | gfx::Transform identityMatrix; |
| 835 | gfx::Transform deviceScaleTransform; |
| 836 | deviceScaleTransform.Scale(deviceScaleFactor, deviceScaleFactor); |
[email protected] | ecc1262 | 2012-10-30 20:45:42 | [diff] [blame] | 837 | std::vector<scoped_refptr<Layer> > dummyLayerList; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 838 | |
[email protected] | ecc1262 | 2012-10-30 20:45:42 | [diff] [blame] | 839 | // The root layer's renderSurface should receive the deviceViewport as the initial clipRect. |
| 840 | bool subtreeShouldBeClipped = true; |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 841 | gfx::Rect deviceViewportRect(gfx::Point(), deviceViewportSize); |
[email protected] | ecc1262 | 2012-10-30 20:45:42 | [diff] [blame] | 842 | |
| 843 | // This function should have received a root layer. |
| 844 | DCHECK(isRootLayer(rootLayer)); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 845 | |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 846 | cc::calculateDrawPropertiesInternal<Layer, std::vector<scoped_refptr<Layer> >, RenderSurface, void>( |
[email protected] | ecc1262 | 2012-10-30 20:45:42 | [diff] [blame] | 847 | rootLayer, deviceScaleTransform, identityMatrix, identityMatrix, |
| 848 | deviceViewportRect, subtreeShouldBeClipped, 0, renderSurfaceLayerList, |
| 849 | dummyLayerList, 0, maxTextureSize, |
[email protected] | 518ee58 | 2012-10-24 18:29:44 | [diff] [blame] | 850 | deviceScaleFactor, pageScaleFactor, totalDrawableContentRect); |
[email protected] | ecc1262 | 2012-10-30 20:45:42 | [diff] [blame] | 851 | |
| 852 | // The dummy layer list should not have been used. |
| 853 | DCHECK(dummyLayerList.size() == 0); |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 854 | // A root layer renderSurface should always exist after calculateDrawProperties. |
[email protected] | ecc1262 | 2012-10-30 20:45:42 | [diff] [blame] | 855 | DCHECK(rootLayer->renderSurface()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 856 | } |
| 857 | |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 858 | void LayerTreeHostCommon::calculateDrawProperties(LayerImpl* rootLayer, const gfx::Size& deviceViewportSize, float deviceScaleFactor, float pageScaleFactor, LayerSorter* layerSorter, int maxTextureSize, std::vector<LayerImpl*>& renderSurfaceLayerList) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 859 | { |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 860 | gfx::Rect totalDrawableContentRect; |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 861 | gfx::Transform identityMatrix; |
| 862 | gfx::Transform deviceScaleTransform; |
| 863 | deviceScaleTransform.Scale(deviceScaleFactor, deviceScaleFactor); |
[email protected] | ecc1262 | 2012-10-30 20:45:42 | [diff] [blame] | 864 | std::vector<LayerImpl*> dummyLayerList; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 865 | |
[email protected] | ecc1262 | 2012-10-30 20:45:42 | [diff] [blame] | 866 | // The root layer's renderSurface should receive the deviceViewport as the initial clipRect. |
| 867 | bool subtreeShouldBeClipped = true; |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 868 | gfx::Rect deviceViewportRect(gfx::Point(), deviceViewportSize); |
[email protected] | ecc1262 | 2012-10-30 20:45:42 | [diff] [blame] | 869 | |
| 870 | // This function should have received a root layer. |
| 871 | DCHECK(isRootLayer(rootLayer)); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 872 | |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 873 | cc::calculateDrawPropertiesInternal<LayerImpl, std::vector<LayerImpl*>, RenderSurfaceImpl, LayerSorter>( |
[email protected] | ecc1262 | 2012-10-30 20:45:42 | [diff] [blame] | 874 | rootLayer, deviceScaleTransform, identityMatrix, identityMatrix, |
| 875 | deviceViewportRect, subtreeShouldBeClipped, 0, renderSurfaceLayerList, |
| 876 | dummyLayerList, layerSorter, maxTextureSize, |
[email protected] | 518ee58 | 2012-10-24 18:29:44 | [diff] [blame] | 877 | deviceScaleFactor, pageScaleFactor, totalDrawableContentRect); |
[email protected] | ecc1262 | 2012-10-30 20:45:42 | [diff] [blame] | 878 | |
| 879 | // The dummy layer list should not have been used. |
| 880 | DCHECK(dummyLayerList.size() == 0); |
[email protected] | d76806f8 | 2012-12-05 21:41:50 | [diff] [blame^] | 881 | // A root layer renderSurface should always exist after calculateDrawProperties. |
[email protected] | ecc1262 | 2012-10-30 20:45:42 | [diff] [blame] | 882 | DCHECK(rootLayer->renderSurface()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 883 | } |
| 884 | |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 885 | static bool pointHitsRect(const gfx::PointF& screenSpacePoint, const gfx::Transform& localSpaceToScreenSpaceTransform, gfx::RectF localSpaceRect) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 886 | { |
| 887 | // If the transform is not invertible, then assume that this point doesn't hit this rect. |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 888 | if (!localSpaceToScreenSpaceTransform.IsInvertible()) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 889 | return false; |
| 890 | |
| 891 | // Transform the hit test point from screen space to the local space of the given rect. |
| 892 | bool clipped = false; |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 893 | gfx::PointF hitTestPointInLocalSpace = MathUtil::projectPoint(MathUtil::inverse(localSpaceToScreenSpaceTransform), screenSpacePoint, clipped); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 894 | |
| 895 | // If projectPoint could not project to a valid value, then we assume that this point doesn't hit this rect. |
| 896 | if (clipped) |
| 897 | return false; |
| 898 | |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 899 | return localSpaceRect.Contains(hitTestPointInLocalSpace); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 900 | } |
| 901 | |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 902 | static bool pointHitsRegion(gfx::PointF screenSpacePoint, const gfx::Transform& screenSpaceTransform, const Region& layerSpaceRegion, float layerContentScaleX, float layerContentScaleY) |
[email protected] | 2f1acc26 | 2012-11-16 21:42:22 | [diff] [blame] | 903 | { |
| 904 | // If the transform is not invertible, then assume that this point doesn't hit this region. |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 905 | if (!screenSpaceTransform.IsInvertible()) |
[email protected] | 2f1acc26 | 2012-11-16 21:42:22 | [diff] [blame] | 906 | return false; |
| 907 | |
| 908 | // Transform the hit test point from screen space to the local space of the given region. |
| 909 | bool clipped = false; |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 910 | gfx::PointF hitTestPointInContentSpace = MathUtil::projectPoint(MathUtil::inverse(screenSpaceTransform), screenSpacePoint, clipped); |
[email protected] | 2f1acc26 | 2012-11-16 21:42:22 | [diff] [blame] | 911 | gfx::PointF hitTestPointInLayerSpace = gfx::ScalePoint(hitTestPointInContentSpace, 1 / layerContentScaleX, 1 / layerContentScaleY); |
| 912 | |
| 913 | // If projectPoint could not project to a valid value, then we assume that this point doesn't hit this region. |
| 914 | if (clipped) |
| 915 | return false; |
| 916 | |
| 917 | return layerSpaceRegion.Contains(gfx::ToRoundedPoint(hitTestPointInLayerSpace)); |
| 918 | } |
| 919 | |
[email protected] | d455d55 | 2012-11-02 00:19:06 | [diff] [blame] | 920 | static bool pointIsClippedBySurfaceOrClipRect(const gfx::PointF& screenSpacePoint, LayerImpl* layer) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 921 | { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 922 | LayerImpl* currentLayer = layer; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 923 | |
| 924 | // Walk up the layer tree and hit-test any renderSurfaces and any layer clipRects that are active. |
| 925 | while (currentLayer) { |
[email protected] | 0152773 | 2012-10-19 18:16:19 | [diff] [blame] | 926 | if (currentLayer->renderSurface() && !pointHitsRect(screenSpacePoint, currentLayer->renderSurface()->screenSpaceTransform(), currentLayer->renderSurface()->contentRect())) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 927 | return true; |
| 928 | |
| 929 | // Note that drawableContentRects are actually in targetSurface space, so the transform we |
| 930 | // have to provide is the target surface's screenSpaceTransform. |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 931 | LayerImpl* renderTarget = currentLayer->renderTarget(); |
[email protected] | 0152773 | 2012-10-19 18:16:19 | [diff] [blame] | 932 | if (layerClipsSubtree(currentLayer) && !pointHitsRect(screenSpacePoint, renderTarget->renderSurface()->screenSpaceTransform(), currentLayer->drawableContentRect())) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 933 | return true; |
| 934 | |
| 935 | currentLayer = currentLayer->parent(); |
| 936 | } |
| 937 | |
| 938 | // If we have finished walking all ancestors without having already exited, then the point is not clipped by any ancestors. |
| 939 | return false; |
| 940 | } |
| 941 | |
[email protected] | d455d55 | 2012-11-02 00:19:06 | [diff] [blame] | 942 | LayerImpl* LayerTreeHostCommon::findLayerThatIsHitByPoint(const gfx::PointF& screenSpacePoint, std::vector<LayerImpl*>& renderSurfaceLayerList) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 943 | { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 944 | LayerImpl* foundLayer = 0; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 945 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 946 | typedef LayerIterator<LayerImpl, std::vector<LayerImpl*>, RenderSurfaceImpl, LayerIteratorActions::FrontToBack> LayerIteratorType; |
| 947 | LayerIteratorType end = LayerIteratorType::end(&renderSurfaceLayerList); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 948 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 949 | for (LayerIteratorType it = LayerIteratorType::begin(&renderSurfaceLayerList); it != end; ++it) { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 950 | // We don't want to consider renderSurfaces for hit testing. |
| 951 | if (!it.representsItself()) |
| 952 | continue; |
| 953 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 954 | LayerImpl* currentLayer = (*it); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 955 | |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 956 | gfx::RectF contentRect(gfx::PointF(), currentLayer->contentBounds()); |
[email protected] | 0152773 | 2012-10-19 18:16:19 | [diff] [blame] | 957 | if (!pointHitsRect(screenSpacePoint, currentLayer->screenSpaceTransform(), contentRect)) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 958 | continue; |
| 959 | |
| 960 | // At this point, we think the point does hit the layer, but we need to walk up |
| 961 | // the parents to ensure that the layer was not clipped in such a way that the |
| 962 | // hit point actually should not hit the layer. |
[email protected] | 0152773 | 2012-10-19 18:16:19 | [diff] [blame] | 963 | if (pointIsClippedBySurfaceOrClipRect(screenSpacePoint, currentLayer)) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 964 | continue; |
| 965 | |
| 966 | foundLayer = currentLayer; |
| 967 | break; |
| 968 | } |
| 969 | |
[email protected] | 0152773 | 2012-10-19 18:16:19 | [diff] [blame] | 970 | // This can potentially return 0, which means the screenSpacePoint did not successfully hit test any layers, not even the root layer. |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 971 | return foundLayer; |
| 972 | } |
| 973 | |
[email protected] | 2f1acc26 | 2012-11-16 21:42:22 | [diff] [blame] | 974 | LayerImpl* LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(const gfx::PointF& screenSpacePoint, std::vector<LayerImpl*>& renderSurfaceLayerList) |
| 975 | { |
| 976 | LayerImpl* foundLayer = 0; |
| 977 | |
| 978 | typedef LayerIterator<LayerImpl, std::vector<LayerImpl*>, RenderSurfaceImpl, LayerIteratorActions::FrontToBack> LayerIteratorType; |
| 979 | LayerIteratorType end = LayerIteratorType::end(&renderSurfaceLayerList); |
| 980 | |
| 981 | for (LayerIteratorType it = LayerIteratorType::begin(&renderSurfaceLayerList); it != end; ++it) { |
| 982 | // We don't want to consider renderSurfaces for hit testing. |
| 983 | if (!it.representsItself()) |
| 984 | continue; |
| 985 | |
| 986 | LayerImpl* currentLayer = (*it); |
| 987 | |
| 988 | if (currentLayer->touchEventHandlerRegion().IsEmpty()) |
| 989 | continue; |
| 990 | |
| 991 | if (!pointHitsRegion(screenSpacePoint, currentLayer->screenSpaceTransform(), currentLayer->touchEventHandlerRegion(), currentLayer->contentsScaleX(), currentLayer->contentsScaleY())) |
| 992 | continue; |
| 993 | |
| 994 | // At this point, we think the point does hit the touch event handler region on the layer, but we need to walk up |
| 995 | // the parents to ensure that the layer was not clipped in such a way that the |
| 996 | // hit point actually should not hit the layer. |
| 997 | if (pointIsClippedBySurfaceOrClipRect(screenSpacePoint, currentLayer)) |
| 998 | continue; |
| 999 | |
| 1000 | foundLayer = currentLayer; |
| 1001 | break; |
| 1002 | } |
| 1003 | |
| 1004 | // This can potentially return 0, which means the screenSpacePoint did not successfully hit test any layers, not even the root layer. |
| 1005 | return foundLayer; |
| 1006 | } |
| 1007 | |
[email protected] | bc5e77c | 2012-11-05 20:00:49 | [diff] [blame] | 1008 | } // namespace cc |