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