| 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 | 6 |
| 7 #include "core/layout/ng/ng_units.h" | 7 #include "core/layout/ng/ng_units.h" |
| 8 #include "wtf/NonCopyingSort.h" | 8 #include "wtf/NonCopyingSort.h" |
| 9 | 9 |
| 10 namespace blink { | 10 namespace blink { |
| 11 namespace { | 11 namespace { |
| 12 | 12 |
| 13 // Collects all opportunities from leaves of Layout Opportunity spatial tree. | 13 // Collects all opportunities from leaves of Layout Opportunity spatial tree. |
| 14 void CollectAllOpportunities(const NGLayoutOpportunityTreeNode* node, | 14 void CollectAllOpportunities(const NGLayoutOpportunityTreeNode* node, |
| 15 NGLayoutOpportunities& opportunities) { | 15 NGLayoutOpportunities& opportunities) { |
| 16 if (!node) | 16 if (!node) |
| 17 return; | 17 return; |
| 18 if (node->IsLeafNode()) | 18 if (node->IsLeafNode()) |
| 19 opportunities.push_back(node->opportunity); | 19 opportunities.push_back(node->opportunity); |
| 20 CollectAllOpportunities(node->left, opportunities); | 20 CollectAllOpportunities(node->left, opportunities); |
| 21 CollectAllOpportunities(node->bottom, opportunities); | 21 CollectAllOpportunities(node->bottom, opportunities); |
| 22 CollectAllOpportunities(node->right, opportunities); | 22 CollectAllOpportunities(node->right, opportunities); |
| 23 } | 23 } |
| 24 | 24 |
| 25 // Creates layout opportunity from the provided space and the origin point. | 25 // Creates layout opportunity from the provided space and the origin point. |
| 26 NGLayoutOpportunity CreateLayoutOpportunityFromConstraintSpace( | 26 NGLayoutOpportunity CreateLayoutOpportunityFromConstraintSpace( |
| 27 const NGConstraintSpace& space, | 27 const NGConstraintSpace& space, |
| 28 const NGLogicalOffset& origin_point) { | 28 const NGLogicalOffset& origin_point) { |
| 29 NGLayoutOpportunity opportunity; | 29 NGLayoutOpportunity opportunity; |
| 30 opportunity.offset = space.Offset(); | 30 // TODO(glebl): Perhaps fix other methods (e.g IsContained) instead of using |
| 31 opportunity.size = space.AvailableSize(); | 31 // INT_MAX here. |
| 32 opportunity.size.block_size = space.AvailableSize().block_size >= 0 |
| 33 ? space.AvailableSize().block_size |
| 34 : LayoutUnit(INT_MAX); |
| 35 opportunity.size.inline_size = space.AvailableSize().inline_size >= 0 |
| 36 ? space.AvailableSize().inline_size |
| 37 : LayoutUnit(INT_MAX); |
| 32 | 38 |
| 33 // adjust to the origin_point. | 39 // adjust to the origin_point. |
| 34 opportunity.offset += origin_point; | 40 opportunity.offset += origin_point; |
| 35 opportunity.size.inline_size -= origin_point.inline_offset; | |
| 36 opportunity.size.block_size -= origin_point.block_offset; | |
| 37 return opportunity; | 41 return opportunity; |
| 38 } | 42 } |
| 39 | 43 |
| 40 // Whether 2 edges overlap with each other. | 44 // Whether 2 edges overlap with each other. |
| 41 bool IsOverlapping(const NGEdge& edge1, const NGEdge& edge2) { | 45 bool IsOverlapping(const NGEdge& edge1, const NGEdge& edge2) { |
| 42 return std::max(edge1.start, edge2.start) <= std::min(edge1.end, edge2.end); | 46 return std::max(edge1.start, edge2.start) <= std::min(edge1.end, edge2.end); |
| 43 } | 47 } |
| 44 | 48 |
| 45 // Creates the *BOTTOM* positioned Layout Opportunity tree node by splitting | 49 // Creates the *BOTTOM* positioned Layout Opportunity tree node by splitting |
| 46 // the parent node with the exclusion. | 50 // the parent node with the exclusion. |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 return true; | 210 return true; |
| 207 } | 211 } |
| 208 if (rhs.offset.inline_offset < lhs.offset.inline_offset) { | 212 if (rhs.offset.inline_offset < lhs.offset.inline_offset) { |
| 209 return false; | 213 return false; |
| 210 } | 214 } |
| 211 | 215 |
| 212 // TOP and LEFT are the same -> Sort by width | 216 // TOP and LEFT are the same -> Sort by width |
| 213 return rhs.size.inline_size < lhs.size.inline_size; | 217 return rhs.size.inline_size < lhs.size.inline_size; |
| 214 } | 218 } |
| 215 | 219 |
| 216 void RunPreconditionChecks( | |
| 217 const NGConstraintSpace& space, | |
| 218 const WTF::Optional<NGLogicalOffset>& opt_origin_point, | |
| 219 const WTF::Optional<NGLogicalOffset>& opt_leader_point) { | |
| 220 if (opt_origin_point) { | |
| 221 NGLogicalOffset origin_point = opt_origin_point.value(); | |
| 222 DCHECK_GE(origin_point, space.Offset()) | |
| 223 << "Origin point " << origin_point | |
| 224 << " should lay below the constraint space's offset " << space.Offset(); | |
| 225 } | |
| 226 | |
| 227 if (opt_leader_point) { | |
| 228 NGLogicalOffset leader_point = opt_leader_point.value(); | |
| 229 DCHECK_GE(leader_point, space.Offset()) | |
| 230 << "Leader point " << leader_point | |
| 231 << " should lay below the constraint space's offset " << space.Offset(); | |
| 232 } | |
| 233 } | |
| 234 | |
| 235 NGExclusion ToLeaderExclusion(const NGLogicalOffset& origin_point, | 220 NGExclusion ToLeaderExclusion(const NGLogicalOffset& origin_point, |
| 236 const NGLogicalOffset& leader_point) { | 221 const NGLogicalOffset& leader_point) { |
| 237 LayoutUnit inline_size = | 222 LayoutUnit inline_size = |
| 238 leader_point.inline_offset - origin_point.inline_offset; | 223 leader_point.inline_offset - origin_point.inline_offset; |
| 239 LayoutUnit block_size = leader_point.block_offset - origin_point.block_offset; | 224 LayoutUnit block_size = leader_point.block_offset - origin_point.block_offset; |
| 240 | 225 |
| 241 NGExclusion leader_exclusion; | 226 NGExclusion leader_exclusion; |
| 242 leader_exclusion.rect.offset = origin_point; | 227 leader_exclusion.rect.offset = origin_point; |
| 243 leader_exclusion.rect.size = {inline_size, block_size}; | 228 leader_exclusion.rect.size = {inline_size, block_size}; |
| 244 return leader_exclusion; | 229 return leader_exclusion; |
| 245 } | 230 } |
| 246 | 231 |
| 247 } // namespace | 232 } // namespace |
| 248 | 233 |
| 249 NGLayoutOpportunityIterator::NGLayoutOpportunityIterator( | 234 NGLayoutOpportunityIterator::NGLayoutOpportunityIterator( |
| 250 NGConstraintSpace* space, | 235 NGConstraintSpace* space, |
| 251 const WTF::Optional<NGLogicalOffset>& opt_origin_point, | 236 const WTF::Optional<NGLogicalOffset>& opt_origin_point, |
| 252 const WTF::Optional<NGLogicalOffset>& opt_leader_point) | 237 const WTF::Optional<NGLogicalOffset>& opt_leader_point) |
| 253 : constraint_space_(space) { | 238 : constraint_space_(space) { |
| 254 RunPreconditionChecks(*space, opt_origin_point, opt_leader_point); | |
| 255 | |
| 256 // TODO(chrome-layout-team): Combine exclusions that shadow each other. | 239 // TODO(chrome-layout-team): Combine exclusions that shadow each other. |
| 257 auto& exclusions = constraint_space_->Exclusions(); | 240 auto& exclusions = constraint_space_->Exclusions(); |
| 258 DCHECK(std::is_sorted(exclusions->storage.begin(), exclusions->storage.end(), | 241 DCHECK(std::is_sorted(exclusions->storage.begin(), exclusions->storage.end(), |
| 259 &CompareNGExclusionsByTopAsc)) | 242 &CompareNGExclusionsByTopAsc)) |
| 260 << "Exclusions are expected to be sorted by TOP"; | 243 << "Exclusions are expected to be sorted by TOP"; |
| 261 | 244 |
| 262 NGLogicalOffset origin_point = | 245 NGLogicalOffset origin_point = |
| 263 opt_origin_point ? opt_origin_point.value() : NGLogicalOffset(); | 246 opt_origin_point ? opt_origin_point.value() : NGLogicalOffset(); |
| 264 NGLayoutOpportunity initial_opportunity = | 247 NGLayoutOpportunity initial_opportunity = |
| 265 CreateLayoutOpportunityFromConstraintSpace(*space, origin_point); | 248 CreateLayoutOpportunityFromConstraintSpace(*space, origin_point); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 285 | 268 |
| 286 const NGLayoutOpportunity NGLayoutOpportunityIterator::Next() { | 269 const NGLayoutOpportunity NGLayoutOpportunityIterator::Next() { |
| 287 if (opportunity_iter_ == opportunities_.end()) | 270 if (opportunity_iter_ == opportunities_.end()) |
| 288 return NGLayoutOpportunity(); | 271 return NGLayoutOpportunity(); |
| 289 auto* opportunity = opportunity_iter_; | 272 auto* opportunity = opportunity_iter_; |
| 290 opportunity_iter_++; | 273 opportunity_iter_++; |
| 291 return NGLayoutOpportunity(*opportunity); | 274 return NGLayoutOpportunity(*opportunity); |
| 292 } | 275 } |
| 293 | 276 |
| 294 } // namespace blink | 277 } // namespace blink |
| OLD | NEW |