OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "core/layout/ng/ng_layout_opportunity_iterator.h" | 5 #include "core/layout/ng/ng_layout_opportunity_iterator.h" |
6 #include "core/layout/ng/ng_constraint_space.h" | 6 #include "core/layout/ng/ng_constraint_space.h" |
7 #include "core/layout/ng/ng_exclusion.h" | 7 #include "core/layout/ng/ng_exclusion.h" |
8 #include "platform/wtf/NonCopyingSort.h" | 8 #include "platform/wtf/NonCopyingSort.h" |
9 #include "platform/wtf/text/StringBuilder.h" | 9 #include "platform/wtf/text/StringBuilder.h" |
10 | 10 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 NGLayoutOpportunities& opportunities) { | 46 NGLayoutOpportunities& opportunities) { |
47 if (!node) | 47 if (!node) |
48 return; | 48 return; |
49 if (node->IsLeafNode()) | 49 if (node->IsLeafNode()) |
50 opportunities.push_back(node->opportunity); | 50 opportunities.push_back(node->opportunity); |
51 CollectAllOpportunities(node->left.get(), opportunities); | 51 CollectAllOpportunities(node->left.get(), opportunities); |
52 CollectAllOpportunities(node->bottom.get(), opportunities); | 52 CollectAllOpportunities(node->bottom.get(), opportunities); |
53 CollectAllOpportunities(node->right.get(), opportunities); | 53 CollectAllOpportunities(node->right.get(), opportunities); |
54 } | 54 } |
55 | 55 |
56 // Creates layout opportunity from the provided space and the origin point. | 56 // Creates layout opportunity from the provided size and the origin point. |
57 NGLayoutOpportunity CreateLayoutOpportunityFromConstraintSpace( | 57 NGLayoutOpportunity CreateInitialOpportunity( |
58 const NGLogicalSize& size, | 58 const NGLogicalSize& size, |
59 const NGLogicalOffset& origin_point) { | 59 const NGLogicalOffset& origin_point) { |
60 NGLayoutOpportunity opportunity; | 60 NGLayoutOpportunity opportunity; |
61 // TODO(glebl): Perhaps fix other methods (e.g IsContained) instead of using | 61 // TODO(glebl): Perhaps fix other methods (e.g IsContained) instead of using |
62 // INT_MAX here. | 62 // INT_MAX here. |
63 opportunity.size.block_size = | 63 opportunity.size.block_size = |
64 size.block_size >= 0 ? size.block_size : LayoutUnit(INT_MAX); | 64 size.block_size >= 0 ? size.block_size : LayoutUnit(INT_MAX); |
65 opportunity.size.inline_size = | 65 opportunity.size.inline_size = |
66 size.inline_size >= 0 ? size.inline_size : LayoutUnit(INT_MAX); | 66 size.inline_size >= 0 ? size.inline_size : LayoutUnit(INT_MAX); |
67 | 67 |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 if (rhs.offset.inline_offset > lhs.offset.inline_offset) { | 250 if (rhs.offset.inline_offset > lhs.offset.inline_offset) { |
251 return true; | 251 return true; |
252 } | 252 } |
253 if (rhs.offset.inline_offset < lhs.offset.inline_offset) { | 253 if (rhs.offset.inline_offset < lhs.offset.inline_offset) { |
254 return false; | 254 return false; |
255 } | 255 } |
256 | 256 |
257 // TOP and LEFT are the same -> Sort by width | 257 // TOP and LEFT are the same -> Sort by width |
258 return rhs.size.inline_size < lhs.size.inline_size; | 258 return rhs.size.inline_size < lhs.size.inline_size; |
259 } | 259 } |
260 | |
261 NGExclusion ToLeaderExclusion(const NGLogicalOffset& origin_point, | |
262 const NGLogicalOffset& leader_point) { | |
263 LayoutUnit inline_size = | |
264 leader_point.inline_offset - origin_point.inline_offset; | |
265 LayoutUnit block_size = leader_point.block_offset - origin_point.block_offset; | |
266 | |
267 NGExclusion leader_exclusion; | |
268 leader_exclusion.rect.offset = origin_point; | |
269 leader_exclusion.rect.size = {inline_size, block_size}; | |
270 return leader_exclusion; | |
271 } | |
272 | |
273 } // namespace | 260 } // namespace |
274 | 261 |
275 NGLayoutOpportunityIterator::NGLayoutOpportunityIterator( | 262 NGLayoutOpportunityIterator::NGLayoutOpportunityIterator( |
276 const NGConstraintSpace* space, | 263 const NGExclusions* exclusions, |
277 const NGLogicalSize& available_size, | 264 const NGLogicalSize& available_size, |
278 const WTF::Optional<NGLogicalOffset>& opt_offset, | 265 const NGLogicalOffset& offset) |
279 const WTF::Optional<NGLogicalOffset>& opt_leader_point) | 266 : offset_(offset) { |
280 : constraint_space_(space), | 267 DCHECK(exclusions); |
281 offset_(opt_offset ? opt_offset.value() : space->BfcOffset()) { | |
282 // TODO(chrome-layout-team): Combine exclusions that shadow each other. | |
283 auto& exclusions = constraint_space_->Exclusions(); | |
284 DCHECK(std::is_sorted(exclusions->storage.begin(), exclusions->storage.end(), | 268 DCHECK(std::is_sorted(exclusions->storage.begin(), exclusions->storage.end(), |
285 &CompareNGExclusionsByTopAsc)) | 269 &CompareNGExclusionsByTopAsc)) |
286 << "Exclusions are expected to be sorted by TOP"; | 270 << "Exclusions are expected to be sorted by TOP"; |
287 | 271 |
288 NGLayoutOpportunity initial_opportunity = | 272 NGLayoutOpportunity initial_opportunity = |
289 CreateLayoutOpportunityFromConstraintSpace(available_size, Offset()); | 273 CreateInitialOpportunity(available_size, Offset()); |
290 opportunity_tree_root_.reset( | 274 opportunity_tree_root_.reset( |
291 new NGLayoutOpportunityTreeNode(initial_opportunity)); | 275 new NGLayoutOpportunityTreeNode(initial_opportunity)); |
292 | 276 |
293 if (opt_leader_point) { | |
294 const NGExclusion leader_exclusion = | |
295 ToLeaderExclusion(Offset(), opt_leader_point.value()); | |
296 InsertExclusion(MutableOpportunityTreeRoot(), &leader_exclusion, | |
297 opportunities_); | |
298 } | |
299 | |
300 for (const auto& exclusion : exclusions->storage) { | 277 for (const auto& exclusion : exclusions->storage) { |
301 InsertExclusion(MutableOpportunityTreeRoot(), exclusion.get(), | 278 InsertExclusion(MutableOpportunityTreeRoot(), exclusion.get(), |
302 opportunities_); | 279 opportunities_); |
303 } | 280 } |
304 CollectAllOpportunities(OpportunityTreeRoot(), opportunities_); | 281 CollectAllOpportunities(OpportunityTreeRoot(), opportunities_); |
305 std::sort(opportunities_.begin(), opportunities_.end(), | 282 std::sort(opportunities_.begin(), opportunities_.end(), |
306 &CompareNGLayoutOpportunitesByStartPoint); | 283 &CompareNGLayoutOpportunitesByStartPoint); |
307 | 284 |
308 opportunity_iter_ = opportunities_.begin(); | 285 opportunity_iter_ = opportunities_.begin(); |
309 } | 286 } |
310 | 287 |
311 const NGLayoutOpportunity NGLayoutOpportunityIterator::Next() { | 288 const NGLayoutOpportunity NGLayoutOpportunityIterator::Next() { |
312 if (opportunity_iter_ == opportunities_.end()) | 289 if (opportunity_iter_ == opportunities_.end()) |
313 return NGLayoutOpportunity(); | 290 return NGLayoutOpportunity(); |
314 auto* opportunity = opportunity_iter_; | 291 auto* opportunity = opportunity_iter_; |
315 opportunity_iter_++; | 292 opportunity_iter_++; |
316 return NGLayoutOpportunity(*opportunity); | 293 return NGLayoutOpportunity(*opportunity); |
317 } | 294 } |
318 | 295 |
319 #ifndef NDEBUG | 296 #ifndef NDEBUG |
320 void NGLayoutOpportunityIterator::ShowLayoutOpportunityTree() const { | 297 void NGLayoutOpportunityIterator::ShowLayoutOpportunityTree() const { |
321 StringBuilder string_builder; | 298 StringBuilder string_builder; |
322 string_builder.Append("\n.:: LayoutOpportunity Tree ::.\n\nRoot Node: "); | 299 string_builder.Append("\n.:: LayoutOpportunity Tree ::.\n\nRoot Node: "); |
323 AppendNodeToString(opportunity_tree_root_.get(), &string_builder); | 300 AppendNodeToString(opportunity_tree_root_.get(), &string_builder); |
324 fprintf(stderr, "%s\n", string_builder.ToString().Utf8().Data()); | 301 fprintf(stderr, "%s\n", string_builder.ToString().Utf8().Data()); |
325 } | 302 } |
326 #endif | 303 #endif |
327 | 304 |
328 } // namespace blink | 305 } // namespace blink |
OLD | NEW |