Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(246)

Side by Side Diff: cc/trees/layer_tree_host_common.cc

Issue 548963002: Generalize scroll parent work in CalculateDrawProperties Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « cc/trees/layer_tree_host_common.h ('k') | cc/trees/layer_tree_host_common_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "cc/trees/layer_tree_host_common.h" 5 #include "cc/trees/layer_tree_host_common.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/debug/trace_event.h" 9 #include "base/debug/trace_event.h"
10 #include "cc/base/math_util.h" 10 #include "cc/base/math_util.h"
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 static LayerType* NextTargetSurface(LayerType* layer) { 118 static LayerType* NextTargetSurface(LayerType* layer) {
119 return layer->parent() ? layer->parent()->render_target() : 0; 119 return layer->parent() ? layer->parent()->render_target() : 0;
120 } 120 }
121 121
122 // Given two layers, this function finds their respective render targets and, 122 // Given two layers, this function finds their respective render targets and,
123 // computes a change of basis translation. It does this by accumulating the 123 // computes a change of basis translation. It does this by accumulating the
124 // translation components of the draw transforms of each target between the 124 // translation components of the draw transforms of each target between the
125 // ancestor and descendant. These transforms must be 2D translations, and this 125 // ancestor and descendant. These transforms must be 2D translations, and this
126 // requirement is enforced at every step. 126 // requirement is enforced at every step.
127 template <typename LayerType> 127 template <typename LayerType>
128 static gfx::Vector2dF ComputeChangeOfBasisTranslation( 128 static bool ComputeChangeOfBasisTranslation(const LayerType& ancestor_layer,
129 const LayerType& ancestor_layer, 129 const LayerType& descendant_layer,
130 const LayerType& descendant_layer) { 130 gfx::Vector2dF* translation) {
131 DCHECK(descendant_layer.HasAncestor(&ancestor_layer)); 131 DCHECK(descendant_layer.HasAncestor(&ancestor_layer));
132 if (!descendant_layer.HasAncestor(&ancestor_layer))
133 return false;
132 const LayerType* descendant_target = descendant_layer.render_target(); 134 const LayerType* descendant_target = descendant_layer.render_target();
133 DCHECK(descendant_target); 135 DCHECK(descendant_target);
134 const LayerType* ancestor_target = ancestor_layer.render_target(); 136 const LayerType* ancestor_target = ancestor_layer.render_target();
135 DCHECK(ancestor_target); 137 DCHECK(ancestor_target);
136 138
137 gfx::Vector2dF translation;
138 for (const LayerType* target = descendant_target; target != ancestor_target; 139 for (const LayerType* target = descendant_target; target != ancestor_target;
139 target = NextTargetSurface(target)) { 140 target = NextTargetSurface(target)) {
140 const gfx::Transform& trans = target->render_surface()->draw_transform(); 141 const gfx::Transform& trans = target->render_surface()->draw_transform();
141 // Ensure that this translation is truly 2d. 142 // Ensure that this translation is truly 2d.
142 DCHECK(trans.IsIdentityOrTranslation()); 143 if (!trans.IsIdentityOrTranslation())
144 return false;
143 DCHECK_EQ(0.f, trans.matrix().get(2, 3)); 145 DCHECK_EQ(0.f, trans.matrix().get(2, 3));
144 translation += trans.To2dTranslation(); 146 *translation += trans.To2dTranslation();
145 } 147 }
146 148
147 return translation; 149 return true;
148 } 150 }
149 151
150 enum TranslateRectDirection { 152 enum TranslateRectDirection {
151 TranslateRectDirectionToAncestor, 153 TranslateRectDirectionToAncestor,
152 TranslateRectDirectionToDescendant 154 TranslateRectDirectionToDescendant
153 }; 155 };
154 156
155 template <typename LayerType> 157 template <typename LayerType>
156 static gfx::Rect TranslateRectToTargetSpace(const LayerType& ancestor_layer, 158 static bool TranslateRectToTargetSpace(const LayerType& ancestor_layer,
157 const LayerType& descendant_layer, 159 const LayerType& descendant_layer,
158 const gfx::Rect& rect, 160 const gfx::Rect& rect,
159 TranslateRectDirection direction) { 161 TranslateRectDirection direction,
160 gfx::Vector2dF translation = ComputeChangeOfBasisTranslation<LayerType>( 162 gfx::Rect* translated) {
161 ancestor_layer, descendant_layer); 163 gfx::Vector2dF translation;
164 if (!ComputeChangeOfBasisTranslation<LayerType>(
165 ancestor_layer, descendant_layer, &translation)) {
166 return false;
167 }
162 if (direction == TranslateRectDirectionToDescendant) 168 if (direction == TranslateRectDirectionToDescendant)
163 translation.Scale(-1.f); 169 translation.Scale(-1.f);
164 return gfx::ToEnclosingRect( 170 *translated = gfx::ToEnclosingRect(
165 gfx::RectF(rect.origin() + translation, rect.size())); 171 gfx::RectF(rect.origin() + translation, rect.size()));
172 return true;
173 }
174
175 template <typename LayerType>
176 static inline bool HaveVisited(LayerType* layer, int sequence_number) {
177 return layer->draw_properties().sequence_number == sequence_number;
178 }
179
180 template <typename LayerType>
181 static inline bool HaveAssignedSortWeight(LayerType* layer,
182 int sequence_number) {
183 return layer->draw_properties().sort_weight_sequence_number ==
184 sequence_number;
185 }
186
187 template <typename LayerType>
188 static inline LayerType* HighestScrollParentAncestorNotOnChildAncestorChain(
189 int sequence_number,
190 LayerType* scroll_child,
191 LayerType* scroll_parent) {
192 // Say we have the following tree.
193 //
194 // a
195 // +-b
196 // | +-c
197 // | +-scroll_child
198 // +-d
199 // +-e
200 // +-scroll_parent
201 //
202 // This function aims to find find the highest ancestor of |scroll_parent|
203 // that's not on |scroll_child|'s ancestor chain. d, in this case.
204 //
205 // Fortunately, we have some hints that make finding the lowest common
206 // ancestor a bit easier than usual. We know if we've visited a layer, and for
207 // those layers that we've visited, we know their depth in the tree.
208 if (!HaveVisited(scroll_parent, sequence_number)) {
209 DCHECK(scroll_parent->parent());
210 // This case is easy. Since we walk the tree in DFS order, if we haven't
211 // visited the scroll parent, we just have to walk up to the first visited
212 // layer, which will be the LCA.
213 while (!HaveVisited(scroll_parent->parent(), sequence_number))
214 scroll_parent = scroll_parent->parent();
215 // This would mean that |scroll_parent| is a descendant of |scroll_child|,
216 // which is an error.
217 DCHECK_NE(scroll_child, scroll_parent->parent());
218 } else {
219 // In this case, we've visited both the scroll_parent and scroll_child, so
220 // it's not as easy as walking to the first visited ancestor of the scroll
221 // parent. But since we've visited the layers, we know their depths, and we
222 // can efficiently find the LCA by walking up nodes at the same height.
223
224 // First, walk up from the deeper layer until we reach two layers at the
225 // same depth in the tree.
226 while (scroll_child->draw_properties().depth !=
227 scroll_parent->draw_properties().depth) {
228 int child_is_deeper = scroll_child->draw_properties().depth >
229 scroll_parent->draw_properties().depth;
230 if (child_is_deeper)
231 scroll_child = scroll_child->parent();
232 else
233 scroll_parent = scroll_parent->parent();
234 }
235
236 // This would only happen if |scroll_child| and |scroll_parent| were on the
237 // same ancestor chain, which is an error. We only ever set up scroll parent
238 // scroll child relationships if scroll child is not a descendant of the
239 // scroll parent and will therefore not scroll with it naturally and needs
240 // special treatment by the compositor.
241 DCHECK_NE(scroll_child, scroll_parent);
242
243 // Now that scroll_child and scroll_parent are at the same depth, we can
244 // walk up in tandem to find the LCA.
245 while (scroll_child->parent() != scroll_parent->parent()) {
246 scroll_child = scroll_child->parent();
247 scroll_parent = scroll_parent->parent();
248 }
249 }
250
251 return scroll_parent;
166 } 252 }
167 253
168 // Attempts to update the clip rects for the given layer. If the layer has a 254 // Attempts to update the clip rects for the given layer. If the layer has a
169 // clip_parent, it may not inherit its immediate ancestor's clip. 255 // clip_parent, it may not inherit its immediate ancestor's clip.
170 template <typename LayerType> 256 template <typename LayerType>
171 static void UpdateClipRectsForClipChild( 257 static void UpdateClipRectsForClipChild(
172 const LayerType* layer, 258 const LayerType* layer,
173 gfx::Rect* clip_rect_in_parent_target_space, 259 gfx::Rect* clip_rect_in_parent_target_space,
174 bool* subtree_should_be_clipped) { 260 bool* subtree_should_be_clipped) {
175 // If the layer has no clip_parent, or the ancestor is the same as its actual 261 // If the layer has no clip_parent, or the ancestor is the same as its actual
(...skipping 13 matching lines...) Expand all
189 // Grab the cached values. 275 // Grab the cached values.
190 *clip_rect_in_parent_target_space = clip_parent->clip_rect(); 276 *clip_rect_in_parent_target_space = clip_parent->clip_rect();
191 *subtree_should_be_clipped = clip_parent->is_clipped(); 277 *subtree_should_be_clipped = clip_parent->is_clipped();
192 278
193 // We may have to project the clip rect into our parent's target space. Note, 279 // We may have to project the clip rect into our parent's target space. Note,
194 // it must be our parent's target space, not ours. For one, we haven't 280 // it must be our parent's target space, not ours. For one, we haven't
195 // computed our transforms, so we couldn't put it in our space yet even if we 281 // computed our transforms, so we couldn't put it in our space yet even if we
196 // wanted to. But more importantly, this matches the expectations of 282 // wanted to. But more importantly, this matches the expectations of
197 // CalculateDrawPropertiesInternal. If we, say, create a render surface, these 283 // CalculateDrawPropertiesInternal. If we, say, create a render surface, these
198 // clip rects will want to be in its target space, not ours. 284 // clip rects will want to be in its target space, not ours.
199 if (clip_parent == layer->clip_parent()) { 285 // if (layer->HasAncestor(clip_parent)) {
200 *clip_rect_in_parent_target_space = TranslateRectToTargetSpace<LayerType>( 286 if (layer->HasAncestor(clip_parent)) {
201 *clip_parent, 287 TranslateRectToTargetSpace<LayerType>(*clip_parent,
202 *layer->parent(), 288 *layer->parent(),
203 *clip_rect_in_parent_target_space, 289 *clip_rect_in_parent_target_space,
204 TranslateRectDirectionToDescendant); 290 TranslateRectDirectionToDescendant,
291 clip_rect_in_parent_target_space);
205 } else { 292 } else {
206 // If we're being clipped by our scroll parent, we must translate through 293 // If we're being clipped by our scroll parent, we must translate through
207 // our common ancestor. This happens to be our parent, so it is sufficent to 294 // our common ancestor.
208 // translate from our clip parent's space to the space of its ancestor (our 295 const LayerType* lca =
209 // parent). 296 HighestScrollParentAncestorNotOnChildAncestorChain(
210 *clip_rect_in_parent_target_space = 297 layer->draw_properties().sequence_number, layer, clip_parent)
211 TranslateRectToTargetSpace<LayerType>(*layer->parent(), 298 ->parent();
299
300 bool did_translate_rect =
301 TranslateRectToTargetSpace<LayerType>(*lca,
212 *clip_parent, 302 *clip_parent,
213 *clip_rect_in_parent_target_space, 303 *clip_rect_in_parent_target_space,
214 TranslateRectDirectionToAncestor); 304 TranslateRectDirectionToAncestor,
305 clip_rect_in_parent_target_space);
306
307 // The lowest common ancestor is usually, but not always, our parent. We
308 // must check if we need to transform from the lca's space to our parent's.
309 // TODO(vollick): crbug.com/424684.
310 if (did_translate_rect && lca != layer->parent()) {
311 TranslateRectToTargetSpace<LayerType>(*lca,
312 *layer->parent(),
313 *clip_rect_in_parent_target_space,
314 TranslateRectDirectionToDescendant,
315 clip_rect_in_parent_target_space);
316 }
215 } 317 }
216 } 318 }
217 319
218 // We collect an accumulated drawable content rect per render surface. 320 // We collect an accumulated drawable content rect per render surface.
219 // Typically, a layer will contribute to only one surface, the surface 321 // Typically, a layer will contribute to only one surface, the surface
220 // associated with its render target. Clip children, however, may affect 322 // associated with its render target. Clip children, however, may affect
221 // several surfaces since there may be several surfaces between the clip child 323 // several surfaces since there may be several surfaces between the clip child
222 // and its parent. 324 // and its parent.
223 // 325 //
224 // NB: we accumulate the layer's *clipped* drawable content rect. 326 // NB: we accumulate the layer's *clipped* drawable content rect.
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 gfx::Rect target_rect = drawable_content_rect; 366 gfx::Rect target_rect = drawable_content_rect;
265 if (layer->render_surface()) { 367 if (layer->render_surface()) {
266 target_rect = 368 target_rect =
267 gfx::ToEnclosedRect(layer->render_surface()->DrawableContentRect()); 369 gfx::ToEnclosedRect(layer->render_surface()->DrawableContentRect());
268 } 370 }
269 371
270 if (render_target->is_clipped()) { 372 if (render_target->is_clipped()) {
271 gfx::Rect clip_rect = render_target->clip_rect(); 373 gfx::Rect clip_rect = render_target->clip_rect();
272 // If the layer has a clip parent, the clip rect may be in the wrong space, 374 // If the layer has a clip parent, the clip rect may be in the wrong space,
273 // so we'll need to transform it before it is applied. 375 // so we'll need to transform it before it is applied.
274 if (layer->clip_parent()) { 376 if (LayerType* clip_parent = layer->clip_parent()) {
275 clip_rect = TranslateRectToTargetSpace<LayerType>( 377 if (layer->HasAncestor(clip_parent)) {
276 *layer->clip_parent(), 378 TranslateRectToTargetSpace<LayerType>(
277 *layer, 379 *clip_parent,
278 clip_rect, 380 *layer,
279 TranslateRectDirectionToDescendant); 381 clip_rect,
382 TranslateRectDirectionToDescendant,
383 &clip_rect);
384 } else {
385 // If we're being clipped by our scroll parent, we must translate
386 // through
387 // our common ancestor.
388 const LayerType* lca =
389 HighestScrollParentAncestorNotOnChildAncestorChain(
390 layer->draw_properties().sequence_number, layer, clip_parent)
391 ->parent();
392
393 TranslateRectToTargetSpace<LayerType>(*lca,
394 *clip_parent,
395 clip_rect,
396 TranslateRectDirectionToAncestor,
397 &clip_rect);
398
399 // The lowest common ancestor is usually, but not always, our
400 // parent. We
401 // must check if we need to transform from the lca's space to our
402 // parent's.
403 if (lca != layer->parent()) {
404 TranslateRectToTargetSpace<LayerType>(
405 *lca,
406 *layer->parent(),
407 clip_rect,
408 TranslateRectDirectionToDescendant,
409 &clip_rect);
410 }
411 }
280 } 412 }
281 target_rect.Intersect(clip_rect); 413 target_rect.Intersect(clip_rect);
282 } 414 }
283 415
284 // We must have at least one entry in the vector for the root. 416 // We must have at least one entry in the vector for the root.
285 DCHECK_LT(0ul, accumulated_surface_state->size()); 417 DCHECK_LT(0ul, accumulated_surface_state->size());
286 418
287 typedef typename std::vector<AccumulatedSurfaceState<LayerType>> 419 typedef typename std::vector<AccumulatedSurfaceState<LayerType>>
288 AccumulatedSurfaceStateVector; 420 AccumulatedSurfaceStateVector;
289 typedef typename AccumulatedSurfaceStateVector::reverse_iterator 421 typedef typename AccumulatedSurfaceStateVector::reverse_iterator
(...skipping 858 matching lines...) Expand 10 before | Expand all | Expand 10 after
1148 int current_render_surface_layer_list_id) { 1280 int current_render_surface_layer_list_id) {
1149 for (typename LayerListType::iterator it = layer_list->begin(); 1281 for (typename LayerListType::iterator it = layer_list->begin();
1150 it != layer_list->end(); 1282 it != layer_list->end();
1151 ++it) { 1283 ++it) {
1152 MarkLayerWithRenderSurfaceLayerListId(*it, 1284 MarkLayerWithRenderSurfaceLayerListId(*it,
1153 current_render_surface_layer_list_id); 1285 current_render_surface_layer_list_id);
1154 MarkMasksWithRenderSurfaceLayerListId(*it, 1286 MarkMasksWithRenderSurfaceLayerListId(*it,
1155 current_render_surface_layer_list_id); 1287 current_render_surface_layer_list_id);
1156 } 1288 }
1157 } 1289 }
1158
1159 template <typename LayerType> 1290 template <typename LayerType>
1160 static inline void RemoveSurfaceForEarlyExit( 1291 static inline void RemoveSurfaceForEarlyExit(
1161 LayerType* layer_to_remove, 1292 LayerType* layer_to_remove,
1162 typename LayerType::RenderSurfaceListType* render_surface_layer_list) { 1293 typename LayerType::RenderSurfaceListType* render_surface_layer_list) {
1163 DCHECK(layer_to_remove->render_surface()); 1294 DCHECK(layer_to_remove->render_surface());
1164 // Technically, we know that the layer we want to remove should be 1295 // Technically, we know that the layer we want to remove should be
1165 // at the back of the render_surface_layer_list. However, we have had 1296 // at the back of the render_surface_layer_list. However, we have had
1166 // bugs before that added unnecessary layers here 1297 // bugs before that added unnecessary layers here
1167 // (https://bugs.webkit.org/show_bug.cgi?id=74147), but that causes 1298 // (https://bugs.webkit.org/show_bug.cgi?id=74147), but that causes
1168 // things to crash. So here we proactively remove any additional 1299 // things to crash. So here we proactively remove any additional
(...skipping 27 matching lines...) Expand all
1196 void Merge(const PreCalculateMetaInformationRecursiveData& data) { 1327 void Merge(const PreCalculateMetaInformationRecursiveData& data) {
1197 layer_or_descendant_has_copy_request |= 1328 layer_or_descendant_has_copy_request |=
1198 data.layer_or_descendant_has_copy_request; 1329 data.layer_or_descendant_has_copy_request;
1199 layer_or_descendant_has_input_handler |= 1330 layer_or_descendant_has_input_handler |=
1200 data.layer_or_descendant_has_input_handler; 1331 data.layer_or_descendant_has_input_handler;
1201 num_unclipped_descendants += 1332 num_unclipped_descendants +=
1202 data.num_unclipped_descendants; 1333 data.num_unclipped_descendants;
1203 } 1334 }
1204 }; 1335 };
1205 1336
1337 static int next_sort_weight(int* last_sort_weight) {
1338 return ++(*last_sort_weight);
1339 }
1340
1341 template <typename LayerType>
1342 static inline void AssignSortWeights(LayerType* layer,
1343 int sequence_number,
1344 int* last_sort_weight) {
1345 // Give |layer| the highest sort weight in the tree so far.
1346 if (!HaveAssignedSortWeight(layer, sequence_number)) {
1347 layer->draw_properties().sort_weight = next_sort_weight(last_sort_weight);
1348 layer->draw_properties().sort_weight_sequence_number = sequence_number;
1349 }
1350
1351 if (LayerType* scroll_parent = layer->scroll_parent()) {
1352 // It's important to note where we need to take care of ordering so that
1353 // scroll parents are visited before scroll children; it's at their lowest
1354 // common ancestor.
1355 //
1356 // LCA
1357 // +-scroll_parent_ancestor
1358 // | +- ...
1359 // | + scroll_parent
1360 // |
1361 // +-scroll_child_ancestor
1362 // +- ..
1363 // + scroll_child
1364 //
1365 // So given the above tree, we need to guarentee that scroll_parent_ancestor
1366 // is processed before scroll_child. We ensure that by assigning it the
1367 // lowest sort weight in the tree. We also want to flag LCA if we need to do
1368 // any funny business with sort weights so that it knows to do special
1369 // sorting of its descendants.
1370 LayerType* scroll_parent_ancestor =
1371 HighestScrollParentAncestorNotOnChildAncestorChain(
1372 sequence_number, layer, scroll_parent);
1373
1374 // We know we need to reorder if we haven't yet assigned
1375 // |scroll_parent_ancestor| a sort weight. Sort weights are monotonically
1376 // increasing, so unless we intervene, it will be given a higher sort weight
1377 // than |layer|.
1378 bool need_to_reorder =
1379 !HaveAssignedSortWeight(scroll_parent_ancestor, sequence_number);
1380
1381 LayerType* lca = scroll_parent_ancestor->parent();
1382
1383 // We also might need to reorder if scroll_parent_ancestor has been visited
1384 // and has an unacceptable sort weight.
1385 if (!need_to_reorder) {
1386 LayerType* scroll_child_ancestor = layer;
1387 while (scroll_child_ancestor->parent() &&
1388 scroll_child_ancestor->parent() != lca) {
1389 scroll_child_ancestor = scroll_child_ancestor->parent();
1390 }
1391 // We must have assigned a sort weight to this layer since its on our
1392 // ancestor chain.
1393 DCHECK(HaveAssignedSortWeight(scroll_child_ancestor, sequence_number));
1394 if (scroll_child_ancestor->draw_properties().sort_weight <
1395 scroll_parent_ancestor->draw_properties().sort_weight)
1396 need_to_reorder = true;
1397 }
1398
1399 if (need_to_reorder) {
1400 // Give |scroll_parent_ancestor| the lowest weight in the tree so far.
1401 scroll_parent_ancestor->draw_properties().sort_weight =
1402 -next_sort_weight(last_sort_weight);
1403 scroll_parent_ancestor->draw_properties().sort_weight_sequence_number =
1404 sequence_number;
1405
1406 // Mark the LCA as needing to sort.
1407 lca->draw_properties().children_need_sorting = true;
1408 }
1409 }
1410 }
1411
1206 // Recursively walks the layer tree to compute any information that is needed 1412 // Recursively walks the layer tree to compute any information that is needed
1207 // before doing the main recursion. 1413 // before doing the main recursion.
1208 template <typename LayerType> 1414 template <typename LayerType>
1209 static void PreCalculateMetaInformation( 1415 static void PreCalculateMetaInformation(
1210 LayerType* layer, 1416 LayerType* layer,
1211 PreCalculateMetaInformationRecursiveData* recursive_data) { 1417 PreCalculateMetaInformationRecursiveData* recursive_data,
1212 1418 int depth,
1213 layer->draw_properties().sorted_for_recursion = false; 1419 int sequence_number,
1214 layer->draw_properties().has_child_with_a_scroll_parent = false; 1420 int* last_sort_weight) {
1421 layer->draw_properties().children_need_sorting = false;
1422 layer->draw_properties().depth = depth;
1423 layer->draw_properties().sequence_number = sequence_number;
1215 1424
1216 if (!HasInvertibleOrAnimatedTransform(layer)) { 1425 if (!HasInvertibleOrAnimatedTransform(layer)) {
1217 // Layers with singular transforms should not be drawn, the whole subtree 1426 // Layers with singular transforms should not be drawn, the whole subtree
1218 // can be skipped. 1427 // can be skipped.
1219 return; 1428 return;
1220 } 1429 }
1221 1430
1222 if (layer->clip_parent()) 1431 if (layer->clip_parent())
1223 recursive_data->num_unclipped_descendants++; 1432 recursive_data->num_unclipped_descendants++;
1224 1433
1434 AssignSortWeights(layer, sequence_number, last_sort_weight);
1435
1225 for (size_t i = 0; i < layer->children().size(); ++i) { 1436 for (size_t i = 0; i < layer->children().size(); ++i) {
1226 LayerType* child_layer = 1437 LayerType* child_layer =
1227 LayerTreeHostCommon::get_layer_as_raw_ptr(layer->children(), i); 1438 LayerTreeHostCommon::get_layer_as_raw_ptr(layer->children(), i);
1228
1229 PreCalculateMetaInformationRecursiveData data_for_child; 1439 PreCalculateMetaInformationRecursiveData data_for_child;
1230 PreCalculateMetaInformation(child_layer, &data_for_child); 1440 PreCalculateMetaInformation(child_layer,
1231 1441 &data_for_child,
1232 if (child_layer->scroll_parent()) 1442 depth + 1,
1233 layer->draw_properties().has_child_with_a_scroll_parent = true; 1443 sequence_number,
1444 last_sort_weight);
1234 recursive_data->Merge(data_for_child); 1445 recursive_data->Merge(data_for_child);
1235 } 1446 }
1236 1447
1237 if (layer->clip_children()) { 1448 if (layer->clip_children()) {
1238 int num_clip_children = layer->clip_children()->size(); 1449 int num_clip_children = layer->clip_children()->size();
1239 DCHECK_GE(recursive_data->num_unclipped_descendants, num_clip_children);
1240 recursive_data->num_unclipped_descendants -= num_clip_children; 1450 recursive_data->num_unclipped_descendants -= num_clip_children;
1451 if (recursive_data->num_unclipped_descendants < 0)
1452 recursive_data->num_unclipped_descendants = 0;
1241 } 1453 }
1242 1454
1243 if (layer->HasCopyRequest()) 1455 if (layer->HasCopyRequest())
1244 recursive_data->layer_or_descendant_has_copy_request = true; 1456 recursive_data->layer_or_descendant_has_copy_request = true;
1245 1457
1246 if (!layer->touch_event_handler_region().IsEmpty() || 1458 if (!layer->touch_event_handler_region().IsEmpty() ||
1247 layer->have_wheel_event_handlers()) 1459 layer->have_wheel_event_handlers())
1248 recursive_data->layer_or_descendant_has_input_handler = true; 1460 recursive_data->layer_or_descendant_has_input_handler = true;
1249 1461
1250 layer->draw_properties().num_unclipped_descendants = 1462 layer->draw_properties().num_unclipped_descendants =
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
1310 bool ancestor_is_animating_scale; 1522 bool ancestor_is_animating_scale;
1311 bool ancestor_clips_subtree; 1523 bool ancestor_clips_subtree;
1312 typename LayerType::RenderSurfaceType* 1524 typename LayerType::RenderSurfaceType*
1313 nearest_occlusion_immune_ancestor_surface; 1525 nearest_occlusion_immune_ancestor_surface;
1314 bool in_subtree_of_page_scale_application_layer; 1526 bool in_subtree_of_page_scale_application_layer;
1315 bool subtree_can_use_lcd_text; 1527 bool subtree_can_use_lcd_text;
1316 bool subtree_is_visible_from_ancestor; 1528 bool subtree_is_visible_from_ancestor;
1317 }; 1529 };
1318 1530
1319 template <typename LayerType> 1531 template <typename LayerType>
1320 static LayerType* GetChildContainingLayer(const LayerType& parent, 1532 struct AscendingScrollWeightComparator {
1321 LayerType* layer) { 1533 bool operator()(LayerType* lhs, LayerType* rhs) {
1322 for (LayerType* ancestor = layer; ancestor; ancestor = ancestor->parent()) { 1534 return lhs->draw_properties().sort_weight <
1323 if (ancestor->parent() == &parent) 1535 rhs->draw_properties().sort_weight;
1324 return ancestor;
1325 } 1536 }
1326 NOTREACHED(); 1537 };
1327 return 0; 1538
1539 template <typename LayerType>
1540 static void SortChildrenForRecursion(std::vector<LayerType*>* out,
1541 const LayerType& parent) {
1542 out->resize(parent.children().size());
1543 for (size_t i = 0; i < parent.children().size(); ++i)
1544 (*out)[i] = LayerTreeHostCommon::get_layer_as_raw_ptr(parent.children(), i);
1545
1546 // Ensures the children are sorted in ascending sort weight.
1547 std::sort(
1548 out->begin(), out->end(), AscendingScrollWeightComparator<LayerType>());
1328 } 1549 }
1329 1550
1330 template <typename LayerType> 1551 template <typename LayerType>
1331 static void AddScrollParentChain(std::vector<LayerType*>* out,
1332 const LayerType& parent,
1333 LayerType* layer) {
1334 // At a high level, this function walks up the chain of scroll parents
1335 // recursively, and once we reach the end of the chain, we add the child
1336 // of |parent| containing each scroll ancestor as we unwind. The result is
1337 // an ordering of parent's children that ensures that scroll parents are
1338 // visited before their descendants.
1339 // Take for example this layer tree:
1340 //
1341 // + stacking_context
1342 // + scroll_child (1)
1343 // + scroll_parent_graphics_layer (*)
1344 // | + scroll_parent_scrolling_layer
1345 // | + scroll_parent_scrolling_content_layer (2)
1346 // + scroll_grandparent_graphics_layer (**)
1347 // + scroll_grandparent_scrolling_layer
1348 // + scroll_grandparent_scrolling_content_layer (3)
1349 //
1350 // The scroll child is (1), its scroll parent is (2) and its scroll
1351 // grandparent is (3). Note, this doesn't mean that (2)'s scroll parent is
1352 // (3), it means that (*)'s scroll parent is (3). We don't want our list to
1353 // look like [ (3), (2), (1) ], even though that does have the ancestor chain
1354 // in the right order. Instead, we want [ (**), (*), (1) ]. That is, only want
1355 // (1)'s siblings in the list, but we want them to appear in such an order
1356 // that the scroll ancestors get visited in the correct order.
1357 //
1358 // So our first task at this step of the recursion is to determine the layer
1359 // that we will potentionally add to the list. That is, the child of parent
1360 // containing |layer|.
1361 LayerType* child = GetChildContainingLayer(parent, layer);
1362 if (child->draw_properties().sorted_for_recursion)
1363 return;
1364
1365 if (LayerType* scroll_parent = child->scroll_parent())
1366 AddScrollParentChain(out, parent, scroll_parent);
1367
1368 out->push_back(child);
1369 child->draw_properties().sorted_for_recursion = true;
1370 }
1371
1372 template <typename LayerType>
1373 static bool SortChildrenForRecursion(std::vector<LayerType*>* out,
1374 const LayerType& parent) {
1375 out->reserve(parent.children().size());
1376 bool order_changed = false;
1377 for (size_t i = 0; i < parent.children().size(); ++i) {
1378 LayerType* current =
1379 LayerTreeHostCommon::get_layer_as_raw_ptr(parent.children(), i);
1380
1381 if (current->draw_properties().sorted_for_recursion) {
1382 order_changed = true;
1383 continue;
1384 }
1385
1386 AddScrollParentChain(out, parent, current);
1387 }
1388
1389 DCHECK_EQ(parent.children().size(), out->size());
1390 return order_changed;
1391 }
1392
1393 template <typename LayerType>
1394 static void GetNewDescendantsStartIndexAndCount(LayerType* layer, 1552 static void GetNewDescendantsStartIndexAndCount(LayerType* layer,
1395 size_t* start_index, 1553 size_t* start_index,
1396 size_t* count) { 1554 size_t* count) {
1397 *start_index = layer->draw_properties().index_of_first_descendants_addition; 1555 *start_index = layer->draw_properties().index_of_first_descendants_addition;
1398 *count = layer->draw_properties().num_descendants_added; 1556 *count = layer->draw_properties().num_descendants_added;
1399 } 1557 }
1400 1558
1401 template <typename LayerType> 1559 template <typename LayerType>
1402 static void GetNewRenderSurfacesStartIndexAndCount(LayerType* layer, 1560 static void GetNewRenderSurfacesStartIndexAndCount(LayerType* layer,
1403 size_t* start_index, 1561 size_t* start_index,
(...skipping 706 matching lines...) Expand 10 before | Expand all | Expand 10 after
2110 data_for_children.clip_rect_of_target_surface_in_target_space = 2268 data_for_children.clip_rect_of_target_surface_in_target_space =
2111 clip_rect_of_target_surface_in_target_space; 2269 clip_rect_of_target_surface_in_target_space;
2112 data_for_children.ancestor_clips_subtree = 2270 data_for_children.ancestor_clips_subtree =
2113 layer_or_ancestor_clips_descendants; 2271 layer_or_ancestor_clips_descendants;
2114 data_for_children.nearest_occlusion_immune_ancestor_surface = 2272 data_for_children.nearest_occlusion_immune_ancestor_surface =
2115 nearest_occlusion_immune_ancestor_surface; 2273 nearest_occlusion_immune_ancestor_surface;
2116 data_for_children.subtree_is_visible_from_ancestor = layer_is_drawn; 2274 data_for_children.subtree_is_visible_from_ancestor = layer_is_drawn;
2117 } 2275 }
2118 2276
2119 std::vector<LayerType*> sorted_children; 2277 std::vector<LayerType*> sorted_children;
2120 bool child_order_changed = false; 2278 if (layer_draw_properties.children_need_sorting)
2121 if (layer_draw_properties.has_child_with_a_scroll_parent) 2279 SortChildrenForRecursion(&sorted_children, *layer);
2122 child_order_changed = SortChildrenForRecursion(&sorted_children, *layer);
2123 2280
2124 for (size_t i = 0; i < layer->children().size(); ++i) { 2281 for (size_t i = 0; i < layer->children().size(); ++i) {
2125 // If one of layer's children has a scroll parent, then we may have to 2282 // If one of layer's children has a scroll parent, then we may have to
2126 // visit the children out of order. The new order is stored in 2283 // visit the children out of order. The new order is stored in
2127 // sorted_children. Otherwise, we'll grab the child directly from the 2284 // sorted_children. Otherwise, we'll grab the child directly from the
2128 // layer's list of children. 2285 // layer's list of children.
2129 LayerType* child = 2286 LayerType* child =
2130 layer_draw_properties.has_child_with_a_scroll_parent 2287 layer_draw_properties.children_need_sorting
2131 ? sorted_children[i] 2288 ? sorted_children[i]
2132 : LayerTreeHostCommon::get_layer_as_raw_ptr(layer->children(), i); 2289 : LayerTreeHostCommon::get_layer_as_raw_ptr(layer->children(), i);
2133 2290
2134 child->draw_properties().index_of_first_descendants_addition = 2291 child->draw_properties().index_of_first_descendants_addition =
2135 descendants.size(); 2292 descendants.size();
2136 child->draw_properties().index_of_first_render_surface_layer_list_addition = 2293 child->draw_properties().index_of_first_render_surface_layer_list_addition =
2137 render_surface_layer_list->size(); 2294 render_surface_layer_list->size();
2138 2295
2139 CalculateDrawPropertiesInternal<LayerType>( 2296 CalculateDrawPropertiesInternal<LayerType>(
2140 child, 2297 child,
(...skipping 17 matching lines...) Expand all
2158 child->draw_properties().num_descendants_added = 2315 child->draw_properties().num_descendants_added =
2159 descendants.size() - 2316 descendants.size() -
2160 child->draw_properties().index_of_first_descendants_addition; 2317 child->draw_properties().index_of_first_descendants_addition;
2161 child->draw_properties().num_render_surfaces_added = 2318 child->draw_properties().num_render_surfaces_added =
2162 render_surface_layer_list->size() - 2319 render_surface_layer_list->size() -
2163 child->draw_properties() 2320 child->draw_properties()
2164 .index_of_first_render_surface_layer_list_addition; 2321 .index_of_first_render_surface_layer_list_addition;
2165 } 2322 }
2166 2323
2167 // Add the unsorted layer list contributions, if necessary. 2324 // Add the unsorted layer list contributions, if necessary.
2168 if (child_order_changed) { 2325 if (layer_draw_properties.children_need_sorting) {
2169 SortLayerListContributions( 2326 SortLayerListContributions(
2170 *layer, 2327 *layer,
2171 GetLayerListForSorting(render_surface_layer_list), 2328 GetLayerListForSorting(render_surface_layer_list),
2172 render_surface_layer_list_child_sorting_start_index, 2329 render_surface_layer_list_child_sorting_start_index,
2173 &GetNewRenderSurfacesStartIndexAndCount<LayerType>); 2330 &GetNewRenderSurfacesStartIndexAndCount<LayerType>);
2174 2331
2175 SortLayerListContributions( 2332 SortLayerListContributions(
2176 *layer, 2333 *layer,
2177 &descendants, 2334 &descendants,
2178 layer_list_child_sorting_start_index, 2335 layer_list_child_sorting_start_index,
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
2389 data_for_recursion->subtree_is_visible_from_ancestor = true; 2546 data_for_recursion->subtree_is_visible_from_ancestor = true;
2390 } 2547 }
2391 2548
2392 void LayerTreeHostCommon::CalculateDrawProperties( 2549 void LayerTreeHostCommon::CalculateDrawProperties(
2393 CalcDrawPropsMainInputs* inputs) { 2550 CalcDrawPropsMainInputs* inputs) {
2394 LayerList dummy_layer_list; 2551 LayerList dummy_layer_list;
2395 SubtreeGlobals<Layer> globals; 2552 SubtreeGlobals<Layer> globals;
2396 DataForRecursion<Layer> data_for_recursion; 2553 DataForRecursion<Layer> data_for_recursion;
2397 ProcessCalcDrawPropsInputs(*inputs, &globals, &data_for_recursion); 2554 ProcessCalcDrawPropsInputs(*inputs, &globals, &data_for_recursion);
2398 2555
2556 // This is used as a global throughout the precalculate recursion.
2557 int last_sort_weight = 0;
2399 PreCalculateMetaInformationRecursiveData recursive_data; 2558 PreCalculateMetaInformationRecursiveData recursive_data;
2400 PreCalculateMetaInformation(inputs->root_layer, &recursive_data); 2559 PreCalculateMetaInformation(inputs->root_layer,
2560 &recursive_data,
2561 0,
2562 inputs->current_render_surface_layer_list_id,
2563 &last_sort_weight);
2564
2401 std::vector<AccumulatedSurfaceState<Layer>> accumulated_surface_state; 2565 std::vector<AccumulatedSurfaceState<Layer>> accumulated_surface_state;
2402 CalculateDrawPropertiesInternal<Layer>( 2566 CalculateDrawPropertiesInternal<Layer>(
2403 inputs->root_layer, 2567 inputs->root_layer,
2404 globals, 2568 globals,
2405 data_for_recursion, 2569 data_for_recursion,
2406 inputs->render_surface_layer_list, 2570 inputs->render_surface_layer_list,
2407 &dummy_layer_list, 2571 &dummy_layer_list,
2408 &accumulated_surface_state, 2572 &accumulated_surface_state,
2409 inputs->current_render_surface_layer_list_id); 2573 inputs->current_render_surface_layer_list_id);
2410 2574
2411 // The dummy layer list should not have been used. 2575 // The dummy layer list should not have been used.
2412 DCHECK_EQ(0u, dummy_layer_list.size()); 2576 DCHECK_EQ(0u, dummy_layer_list.size());
2413 // A root layer render_surface should always exist after 2577 // A root layer render_surface should always exist after
2414 // CalculateDrawProperties. 2578 // CalculateDrawProperties.
2415 DCHECK(inputs->root_layer->render_surface()); 2579 DCHECK(inputs->root_layer->render_surface());
2416 } 2580 }
2417 2581
2418 void LayerTreeHostCommon::CalculateDrawProperties( 2582 void LayerTreeHostCommon::CalculateDrawProperties(
2419 CalcDrawPropsImplInputs* inputs) { 2583 CalcDrawPropsImplInputs* inputs) {
2420 LayerImplList dummy_layer_list; 2584 LayerImplList dummy_layer_list;
2421 SubtreeGlobals<LayerImpl> globals; 2585 SubtreeGlobals<LayerImpl> globals;
2422 DataForRecursion<LayerImpl> data_for_recursion; 2586 DataForRecursion<LayerImpl> data_for_recursion;
2423 ProcessCalcDrawPropsInputs(*inputs, &globals, &data_for_recursion); 2587 ProcessCalcDrawPropsInputs(*inputs, &globals, &data_for_recursion);
2424 2588
2425 LayerSorter layer_sorter; 2589 LayerSorter layer_sorter;
2426 globals.layer_sorter = &layer_sorter; 2590 globals.layer_sorter = &layer_sorter;
2427 2591
2592 // This is used as a global throughout the precalculate recursion.
2593 int last_sort_weight = 0;
2428 PreCalculateMetaInformationRecursiveData recursive_data; 2594 PreCalculateMetaInformationRecursiveData recursive_data;
2429 PreCalculateMetaInformation(inputs->root_layer, &recursive_data); 2595 PreCalculateMetaInformation(inputs->root_layer,
2596 &recursive_data,
2597 0,
2598 inputs->current_render_surface_layer_list_id,
2599 &last_sort_weight);
2600
2430 std::vector<AccumulatedSurfaceState<LayerImpl>> accumulated_surface_state; 2601 std::vector<AccumulatedSurfaceState<LayerImpl>> accumulated_surface_state;
2431 CalculateDrawPropertiesInternal<LayerImpl>( 2602 CalculateDrawPropertiesInternal<LayerImpl>(
2432 inputs->root_layer, 2603 inputs->root_layer,
2433 globals, 2604 globals,
2434 data_for_recursion, 2605 data_for_recursion,
2435 inputs->render_surface_layer_list, 2606 inputs->render_surface_layer_list,
2436 &dummy_layer_list, 2607 &dummy_layer_list,
2437 &accumulated_surface_state, 2608 &accumulated_surface_state,
2438 inputs->current_render_surface_layer_list_id); 2609 inputs->current_render_surface_layer_list_id);
2439 2610
2440 // The dummy layer list should not have been used. 2611 // The dummy layer list should not have been used.
2441 DCHECK_EQ(0u, dummy_layer_list.size()); 2612 DCHECK_EQ(0u, dummy_layer_list.size());
2442 // A root layer render_surface should always exist after 2613 // A root layer render_surface should always exist after
2443 // CalculateDrawProperties. 2614 // CalculateDrawProperties.
2444 DCHECK(inputs->root_layer->render_surface()); 2615 DCHECK(inputs->root_layer->render_surface());
2445 } 2616 }
2446 2617
2447 } // namespace cc 2618 } // namespace cc
OLDNEW
« no previous file with comments | « cc/trees/layer_tree_host_common.h ('k') | cc/trees/layer_tree_host_common_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698