| 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_exclusion.h" | 7 #include "core/layout/ng/ng_exclusion.h" |
| 8 #include "wtf/NonCopyingSort.h" | 8 #include "wtf/NonCopyingSort.h" |
| 9 #include "wtf/text/StringBuilder.h" | 9 #include "wtf/text/StringBuilder.h" |
| 10 | 10 |
| 11 namespace blink { | 11 namespace blink { |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 void AppendNodeToString(const NGLayoutOpportunityTreeNode* node, | 14 void AppendNodeToString(const NGLayoutOpportunityTreeNode* node, |
| 15 StringBuilder* string_builder, | 15 StringBuilder* string_builder, |
| 16 unsigned indent = 0) { | 16 unsigned indent = 0) { |
| 17 DCHECK(string_builder); | 17 DCHECK(string_builder); |
| 18 if (!node) { | 18 if (!node) { |
| 19 string_builder->append("'null'\n"); | 19 string_builder->append("'null'\n"); |
| 20 return; | 20 return; |
| 21 } | 21 } |
| 22 | 22 |
| 23 string_builder->append(node->ToString()); | 23 string_builder->append(node->ToString()); |
| 24 string_builder->append("\n"); | 24 string_builder->append("\n"); |
| 25 | 25 |
| 26 StringBuilder indent_builder; | 26 StringBuilder indent_builder; |
| 27 for (unsigned i = 0; i < indent; i++) | 27 for (unsigned i = 0; i < indent; i++) |
| 28 indent_builder.append("\t"); | 28 indent_builder.append("\t"); |
| 29 | 29 |
| 30 if (node->IsLeafNode()) | 30 if (!node->exclusion) |
| 31 return; | 31 return; |
| 32 | 32 |
| 33 string_builder->append(indent_builder.toString()); | 33 string_builder->append(indent_builder.toString()); |
| 34 string_builder->append("Left:\t"); | 34 string_builder->append("Left:\t"); |
| 35 AppendNodeToString(node->left, string_builder, indent + 2); | 35 AppendNodeToString(node->left, string_builder, indent + 2); |
| 36 string_builder->append(indent_builder.toString()); | 36 string_builder->append(indent_builder.toString()); |
| 37 string_builder->append("Right:\t"); | 37 string_builder->append("Right:\t"); |
| 38 AppendNodeToString(node->right, string_builder, indent + 2); | 38 AppendNodeToString(node->right, string_builder, indent + 2); |
| 39 string_builder->append(indent_builder.toString()); | 39 string_builder->append(indent_builder.toString()); |
| 40 string_builder->append("Bottom:\t"); | 40 string_builder->append("Bottom:\t"); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 NGLayoutOpportunity opportunity; | 152 NGLayoutOpportunity opportunity; |
| 153 opportunity.offset.inline_offset = exclusion.InlineEndOffset(); | 153 opportunity.offset.inline_offset = exclusion.InlineEndOffset(); |
| 154 opportunity.offset.block_offset = parent_opportunity.BlockStartOffset(); | 154 opportunity.offset.block_offset = parent_opportunity.BlockStartOffset(); |
| 155 opportunity.size.inline_size = right_opportunity_inline_size; | 155 opportunity.size.inline_size = right_opportunity_inline_size; |
| 156 opportunity.size.block_size = parent_opportunity.BlockSize(); | 156 opportunity.size.block_size = parent_opportunity.BlockSize(); |
| 157 return new NGLayoutOpportunityTreeNode(opportunity); | 157 return new NGLayoutOpportunityTreeNode(opportunity); |
| 158 } | 158 } |
| 159 return nullptr; | 159 return nullptr; |
| 160 } | 160 } |
| 161 | 161 |
| 162 void SplitNGLayoutOpportunityTreeNode(const NGLogicalRect& rect, | |
| 163 NGLayoutOpportunityTreeNode* node) { | |
| 164 node->left = CreateLeftNGLayoutOpportunityTreeNode(node, rect); | |
| 165 node->right = CreateRightNGLayoutOpportunityTreeNode(node, rect); | |
| 166 node->bottom = CreateBottomNGLayoutOpportunityTreeNode(node, rect); | |
| 167 } | |
| 168 | |
| 169 // Gets/Creates the "TOP" positioned constraint space by splitting | 162 // Gets/Creates the "TOP" positioned constraint space by splitting |
| 170 // the parent node with the exclusion. | 163 // the parent node with the exclusion. |
| 171 // | 164 // |
| 172 // @param parent_opportunity Parent opportunity that is being split. | 165 // @param parent_opportunity Parent opportunity that is being split. |
| 173 // @param exclusion Exclusion existed in the parent node constraint space. | 166 // @param exclusion Exclusion existed in the parent node constraint space. |
| 174 // @return New node or nullptr if the new block size == 0. | 167 // @return New node or nullptr if the new block size == 0. |
| 175 NGLayoutOpportunity GetTopSpace(const NGLayoutOpportunity& parent_opportunity, | 168 NGLayoutOpportunity GetTopSpace(const NGLayoutOpportunity& parent_opportunity, |
| 176 const NGLogicalRect& exclusion) { | 169 const NGLogicalRect& exclusion) { |
| 177 LayoutUnit top_opportunity_block_size = | 170 LayoutUnit top_opportunity_block_size = |
| 178 exclusion.BlockStartOffset() - parent_opportunity.BlockStartOffset(); | 171 exclusion.BlockStartOffset() - parent_opportunity.BlockStartOffset(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 196 return; | 189 return; |
| 197 | 190 |
| 198 // Base case: there is no node. | 191 // Base case: there is no node. |
| 199 if (!node) | 192 if (!node) |
| 200 return; | 193 return; |
| 201 | 194 |
| 202 // Base case: exclusion is not in the node's constraint space. | 195 // Base case: exclusion is not in the node's constraint space. |
| 203 if (!exclusion->rect.IsContained(node->opportunity)) | 196 if (!exclusion->rect.IsContained(node->opportunity)) |
| 204 return; | 197 return; |
| 205 | 198 |
| 206 if (node->exclusions.isEmpty()) { | 199 if (node->exclusion) { |
| 207 SplitNGLayoutOpportunityTreeNode(exclusion->rect, node); | 200 InsertExclusion(node->left, exclusion, opportunities); |
| 208 | 201 InsertExclusion(node->bottom, exclusion, opportunities); |
| 209 NGLayoutOpportunity top_layout_opp = | 202 InsertExclusion(node->right, exclusion, opportunities); |
| 210 GetTopSpace(node->opportunity, exclusion->rect); | |
| 211 if (!top_layout_opp.IsEmpty()) | |
| 212 opportunities.push_back(top_layout_opp); | |
| 213 | |
| 214 node->exclusions.push_back(exclusion); | |
| 215 node->combined_exclusion = WTF::makeUnique<NGExclusion>(*exclusion); | |
| 216 return; | 203 return; |
| 217 } | 204 } |
| 218 | 205 |
| 219 DCHECK(!node->exclusions.isEmpty()); | 206 // Split the current node. |
| 207 node->left = CreateLeftNGLayoutOpportunityTreeNode(node, exclusion->rect); |
| 208 node->right = CreateRightNGLayoutOpportunityTreeNode(node, exclusion->rect); |
| 209 node->bottom = CreateBottomNGLayoutOpportunityTreeNode(node, exclusion->rect); |
| 220 | 210 |
| 221 if (node->combined_exclusion->MaybeCombineWith(*exclusion)) { | 211 NGLayoutOpportunity top_layout_opp = |
| 222 SplitNGLayoutOpportunityTreeNode(node->combined_exclusion->rect, node); | 212 GetTopSpace(node->opportunity, exclusion->rect); |
| 223 node->exclusions.push_back(exclusion); | 213 if (!top_layout_opp.IsEmpty()) |
| 224 } else { | 214 opportunities.push_back(top_layout_opp); |
| 225 InsertExclusion(node->left, exclusion, opportunities); | 215 |
| 226 InsertExclusion(node->bottom, exclusion, opportunities); | 216 node->exclusion = exclusion; |
| 227 InsertExclusion(node->right, exclusion, opportunities); | |
| 228 } | |
| 229 } | 217 } |
| 230 | 218 |
| 231 // Compares exclusions by their top position. | 219 // Compares exclusions by their top position. |
| 232 bool CompareNGExclusionsByTopAsc( | 220 bool CompareNGExclusionsByTopAsc( |
| 233 const std::unique_ptr<const NGExclusion>& lhs, | 221 const std::unique_ptr<const NGExclusion>& lhs, |
| 234 const std::unique_ptr<const NGExclusion>& rhs) { | 222 const std::unique_ptr<const NGExclusion>& rhs) { |
| 235 return rhs->rect.offset.block_offset > lhs->rect.offset.block_offset; | 223 return rhs->rect.offset.block_offset > lhs->rect.offset.block_offset; |
| 236 } | 224 } |
| 237 | 225 |
| 238 // Compares Layout Opportunities by Start Point. | 226 // Compares Layout Opportunities by Start Point. |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 #ifndef NDEBUG | 309 #ifndef NDEBUG |
| 322 void NGLayoutOpportunityIterator::ShowLayoutOpportunityTree() const { | 310 void NGLayoutOpportunityIterator::ShowLayoutOpportunityTree() const { |
| 323 StringBuilder string_builder; | 311 StringBuilder string_builder; |
| 324 string_builder.append("\n.:: LayoutOpportunity Tree ::.\n\nRoot Node: "); | 312 string_builder.append("\n.:: LayoutOpportunity Tree ::.\n\nRoot Node: "); |
| 325 AppendNodeToString(opportunity_tree_root_.get(), &string_builder); | 313 AppendNodeToString(opportunity_tree_root_.get(), &string_builder); |
| 326 fprintf(stderr, "%s\n", string_builder.toString().utf8().data()); | 314 fprintf(stderr, "%s\n", string_builder.toString().utf8().data()); |
| 327 } | 315 } |
| 328 #endif | 316 #endif |
| 329 | 317 |
| 330 } // namespace blink | 318 } // namespace blink |
| OLD | NEW |