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