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