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