/src/yoga/yoga/node/Node.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) Meta Platforms, Inc. and affiliates. |
3 | | * |
4 | | * This source code is licensed under the MIT license found in the |
5 | | * LICENSE file in the root directory of this source tree. |
6 | | */ |
7 | | |
8 | | #pragma once |
9 | | |
10 | | #include <atomic> |
11 | | #include <cstdint> |
12 | | #include <cstdio> |
13 | | #include <vector> |
14 | | |
15 | | #include <yoga/Yoga.h> |
16 | | #include <yoga/node/LayoutableChildren.h> |
17 | | |
18 | | #include <yoga/config/Config.h> |
19 | | #include <yoga/enums/Dimension.h> |
20 | | #include <yoga/enums/Direction.h> |
21 | | #include <yoga/enums/Edge.h> |
22 | | #include <yoga/enums/Errata.h> |
23 | | #include <yoga/enums/MeasureMode.h> |
24 | | #include <yoga/enums/NodeType.h> |
25 | | #include <yoga/enums/PhysicalEdge.h> |
26 | | #include <yoga/node/LayoutResults.h> |
27 | | #include <yoga/style/Style.h> |
28 | | |
29 | | // Tag struct used to form the opaque YGNodeRef for the public C API |
30 | | struct YGNode {}; |
31 | | |
32 | | namespace facebook::yoga { |
33 | | |
34 | | class YG_EXPORT Node : public ::YGNode { |
35 | | public: |
36 | | using LayoutableChildren = yoga::LayoutableChildren<Node>; |
37 | | Node(); |
38 | | explicit Node(const Config* config); |
39 | | |
40 | | Node(Node&& node) noexcept; |
41 | | |
42 | | // Does not expose true value semantics, as children are not cloned eagerly. |
43 | | // Should we remove this? |
44 | 0 | Node(const Node& node) = default; |
45 | | |
46 | | // assignment means potential leaks of existing children, or alternatively |
47 | | // freeing unowned memory, double free, or freeing stack memory. |
48 | | Node& operator=(const Node&) = delete; |
49 | | |
50 | | // Getters |
51 | 0 | void* getContext() const { |
52 | 0 | return context_; |
53 | 0 | } |
54 | | |
55 | 0 | bool alwaysFormsContainingBlock() const { |
56 | 0 | return alwaysFormsContainingBlock_; |
57 | 0 | } |
58 | | |
59 | 0 | bool getHasNewLayout() const { |
60 | 0 | return hasNewLayout_; |
61 | 0 | } |
62 | | |
63 | 1.69M | NodeType getNodeType() const { |
64 | 1.69M | return nodeType_; |
65 | 1.69M | } |
66 | | |
67 | 84.7M | bool hasMeasureFunc() const noexcept { |
68 | 84.7M | return measureFunc_ != nullptr; |
69 | 84.7M | } |
70 | | |
71 | | YGSize measure( |
72 | | float availableWidth, |
73 | | MeasureMode widthMode, |
74 | | float availableHeight, |
75 | | MeasureMode heightMode); |
76 | | |
77 | 0 | bool hasMinContentMeasureFunc() const noexcept { |
78 | 0 | return minContentMeasureFunc_ != nullptr; |
79 | 0 | } |
80 | | |
81 | | YGSize measureMinContent( |
82 | | float availableWidth, |
83 | | MeasureMode widthMode, |
84 | | float availableHeight, |
85 | | MeasureMode heightMode); |
86 | | |
87 | 0 | FloatOptional getMinContentWidth() const noexcept { |
88 | 0 | return minContentWidth_; |
89 | 0 | } |
90 | | |
91 | 0 | FloatOptional getMinContentHeight() const noexcept { |
92 | 0 | return minContentHeight_; |
93 | 0 | } |
94 | | |
95 | 0 | bool hasBaselineFunc() const noexcept { |
96 | 0 | return baselineFunc_ != nullptr; |
97 | 0 | } |
98 | | |
99 | | float baseline(float width, float height) const; |
100 | | |
101 | | float dimensionWithMargin(FlexDirection axis, float widthSize); |
102 | | |
103 | | bool isLayoutDimensionDefined(FlexDirection axis); |
104 | | |
105 | | /** |
106 | | * Whether the node has a "definite length" along the given axis. |
107 | | * https://www.w3.org/TR/css-sizing-3/#definite |
108 | | */ |
109 | 78.9M | inline bool hasDefiniteLength(Dimension dimension, float ownerSize) { |
110 | 78.9M | auto usedValue = getProcessedDimension(dimension).resolve(ownerSize); |
111 | 78.9M | return usedValue.isDefined() && usedValue.unwrap() >= 0.0f; |
112 | 78.9M | } |
113 | | |
114 | 131M | bool hasErrata(Errata errata) const { |
115 | 131M | return config_->hasErrata(errata); |
116 | 131M | } |
117 | | |
118 | 39.6M | bool hasContentsChildren() const { |
119 | 39.6M | return contentsChildrenCount_ != 0; |
120 | 39.6M | } |
121 | | |
122 | 1.69M | YGDirtiedFunc getDirtiedFunc() const { |
123 | 1.69M | return dirtiedFunc_; |
124 | 1.69M | } |
125 | | |
126 | | // For Performance reasons passing as reference. |
127 | 1.15G | Style& style() { |
128 | 1.15G | return style_; |
129 | 1.15G | } |
130 | | |
131 | 873M | const Style& style() const { |
132 | 873M | return style_; |
133 | 873M | } |
134 | | |
135 | | // For Performance reasons passing as reference. |
136 | 320M | LayoutResults& getLayout() { |
137 | 320M | return layout_; |
138 | 320M | } |
139 | | |
140 | 88.1M | const LayoutResults& getLayout() const { |
141 | 88.1M | return layout_; |
142 | 88.1M | } |
143 | | |
144 | 0 | size_t getLineIndex() const { |
145 | 0 | return lineIndex_; |
146 | 0 | } |
147 | | |
148 | 0 | bool isReferenceBaseline() const { |
149 | 0 | return isReferenceBaseline_; |
150 | 0 | } |
151 | | |
152 | | // returns the Node that owns this Node. An owner is used to identify |
153 | | // the YogaTree that a Node belongs to. This method will return the parent |
154 | | // of the Node when a Node only belongs to one YogaTree or nullptr when |
155 | | // the Node is shared between two or more YogaTrees. |
156 | 105M | Node* getOwner() const { |
157 | 105M | return owner_.load(); |
158 | 105M | } |
159 | | |
160 | 9.27M | const std::vector<Node*>& getChildren() const { |
161 | 9.27M | return children_; |
162 | 9.27M | } |
163 | | |
164 | 113M | Node* getChild(size_t index) const { |
165 | 113M | return children_.at(index); |
166 | 113M | } |
167 | | |
168 | 101M | size_t getChildCount() const { |
169 | 101M | return children_.size(); |
170 | 101M | } |
171 | | |
172 | 15.0M | LayoutableChildren getLayoutChildren() const { |
173 | 15.0M | return LayoutableChildren(this); |
174 | 15.0M | } |
175 | | |
176 | 32.0M | size_t getLayoutChildCount() const { |
177 | 32.0M | if (contentsChildrenCount_ == 0) { |
178 | 32.0M | return children_.size(); |
179 | 32.0M | } else { |
180 | 0 | size_t count = 0; |
181 | 0 | for (auto iter = getLayoutChildren().begin(); |
182 | 0 | iter != getLayoutChildren().end(); |
183 | 0 | iter++) { |
184 | 0 | count++; |
185 | 0 | } |
186 | 0 | return count; |
187 | 0 | } |
188 | 32.0M | } |
189 | | |
190 | 144M | const Config* getConfig() const { |
191 | 144M | return config_; |
192 | 144M | } |
193 | | |
194 | 67.0M | bool isDirty() const { |
195 | 67.0M | return isDirty_; |
196 | 67.0M | } |
197 | | |
198 | 82.1M | Style::SizeLength getProcessedDimension(Dimension dimension) const { |
199 | 82.1M | return processedDimensions_[static_cast<size_t>(dimension)]; |
200 | 82.1M | } |
201 | | |
202 | | FloatOptional getResolvedDimension( |
203 | | Direction direction, |
204 | | Dimension dimension, |
205 | | float referenceLength, |
206 | 2.46M | float ownerWidth) const { |
207 | 2.46M | FloatOptional value = |
208 | 2.46M | getProcessedDimension(dimension).resolve(referenceLength); |
209 | 2.46M | if (style_.boxSizing() == BoxSizing::BorderBox) { |
210 | 2.46M | return value; |
211 | 2.46M | } |
212 | | |
213 | 0 | FloatOptional dimensionPaddingAndBorder = |
214 | 0 | FloatOptional{style_.computePaddingAndBorderForDimension( |
215 | 0 | direction, dimension, ownerWidth)}; |
216 | |
|
217 | 0 | return value + |
218 | 0 | (dimensionPaddingAndBorder.isDefined() ? dimensionPaddingAndBorder |
219 | 0 | : FloatOptional{0.0}); |
220 | 2.46M | } |
221 | | |
222 | | // Setters |
223 | | |
224 | 0 | void setContext(void* context) { |
225 | 0 | context_ = context; |
226 | 0 | } |
227 | | |
228 | 0 | void setAlwaysFormsContainingBlock(bool alwaysFormsContainingBlock) { |
229 | 0 | alwaysFormsContainingBlock_ = alwaysFormsContainingBlock; |
230 | 0 | } |
231 | | |
232 | 1.69M | void setHasNewLayout(bool hasNewLayout) { |
233 | 1.69M | hasNewLayout_ = hasNewLayout; |
234 | 1.69M | } |
235 | | |
236 | 0 | void setNodeType(NodeType nodeType) { |
237 | 0 | nodeType_ = nodeType; |
238 | 0 | } |
239 | | |
240 | | void setMeasureFunc(YGMeasureFunc measureFunc); |
241 | | |
242 | 0 | void setMinContentMeasureFunc(YGMinContentMeasureFunc minContentMeasureFunc) { |
243 | 0 | minContentMeasureFunc_ = minContentMeasureFunc; |
244 | 0 | } |
245 | | |
246 | 0 | void setMinContentWidth(FloatOptional minContentWidth) noexcept { |
247 | 0 | minContentWidth_ = minContentWidth; |
248 | 0 | } |
249 | | |
250 | 0 | void setMinContentHeight(FloatOptional minContentHeight) noexcept { |
251 | 0 | minContentHeight_ = minContentHeight; |
252 | 0 | } |
253 | | |
254 | 0 | void setBaselineFunc(YGBaselineFunc baseLineFunc) { |
255 | 0 | baselineFunc_ = baseLineFunc; |
256 | 0 | } |
257 | | |
258 | 3.38M | void setDirtiedFunc(YGDirtiedFunc dirtiedFunc) { |
259 | 3.38M | dirtiedFunc_ = dirtiedFunc; |
260 | 3.38M | } |
261 | | |
262 | 0 | void setStyle(const Style& style) { |
263 | 0 | style_ = style; |
264 | 0 | } |
265 | | |
266 | 1.69M | void setLayout(const LayoutResults& layout) { |
267 | 1.69M | layout_ = layout; |
268 | 1.69M | } |
269 | | |
270 | 25.8M | void setLineIndex(size_t lineIndex) { |
271 | 25.8M | lineIndex_ = lineIndex; |
272 | 25.8M | } |
273 | | |
274 | 0 | void setIsReferenceBaseline(bool isReferenceBaseline) { |
275 | 0 | isReferenceBaseline_ = isReferenceBaseline; |
276 | 0 | } |
277 | | |
278 | 3.38M | void setOwner(Node* owner) { |
279 | 3.38M | owner_.store(owner); |
280 | 3.38M | } |
281 | | |
282 | | // TODO: rvalue override for setChildren |
283 | | |
284 | | void setConfig(Config* config); |
285 | | |
286 | | void setDirty(bool isDirty); |
287 | | void setChildren(const std::vector<Node*>& children); |
288 | | void setLayoutLastOwnerDirection(Direction direction); |
289 | | void setLayoutComputedFlexBasis(FloatOptional computedFlexBasis); |
290 | | void setLayoutComputedFlexBasisGeneration( |
291 | | uint32_t computedFlexBasisGeneration); |
292 | | void setLayoutMeasuredDimension(float measuredDimension, Dimension dimension); |
293 | | void setLayoutHadOverflow(bool hadOverflow); |
294 | | void setLayoutDimension(float lengthValue, Dimension dimension); |
295 | | void setLayoutDirection(Direction direction); |
296 | | void setLayoutMargin(float margin, PhysicalEdge edge); |
297 | | void setLayoutBorder(float border, PhysicalEdge edge); |
298 | | void setLayoutPadding(float padding, PhysicalEdge edge); |
299 | | void setLayoutPosition(float position, PhysicalEdge edge); |
300 | | void setPosition(Direction direction, float ownerWidth, float ownerHeight); |
301 | | |
302 | | // Other methods |
303 | | Style::SizeLength processFlexBasis() const; |
304 | | FloatOptional resolveFlexBasis( |
305 | | Direction direction, |
306 | | FlexDirection flexDirection, |
307 | | float referenceLength, |
308 | | float ownerWidth) const; |
309 | | FloatOptional resolveProcessedFlexBasis( |
310 | | Style::SizeLength flexBasis, |
311 | | Direction direction, |
312 | | FlexDirection flexDirection, |
313 | | float referenceLength, |
314 | | float ownerWidth) const; |
315 | | void processDimensions(); |
316 | | Direction resolveDirection(Direction ownerDirection) const; |
317 | | void clearChildren(); |
318 | | /// Replaces the occurrences of oldChild with newChild |
319 | | void replaceChild(Node* oldChild, Node* newChild); |
320 | | void replaceChild(Node* child, size_t index); |
321 | | void insertChild(Node* child, size_t index); |
322 | | /// Removes the first occurrence of child |
323 | | bool removeChild(Node* child); |
324 | | void removeChild(size_t index); |
325 | | |
326 | | void cloneChildrenIfNeeded(); |
327 | | void cloneChildIfNeeded(size_t index); |
328 | | void cloneContentsChildrenIfNeeded(); |
329 | | void markDirtyAndPropagate(); |
330 | | float resolveFlexGrow() const; |
331 | | float resolveFlexShrink() const; |
332 | | bool isNodeFlexible(); |
333 | | void reset(); |
334 | | |
335 | | float relativePosition( |
336 | | FlexDirection axis, |
337 | | Direction direction, |
338 | | float axisSize) const; |
339 | | |
340 | | private: |
341 | | /** |
342 | | * A pointer with relaxed atomic loads and stores. A node shared between trees |
343 | | * can have its owner read by a layout pass on one thread while a clone of its |
344 | | * previous owner, on another thread, writes it. Copies read the pointer once, |
345 | | * so Node keeps its defaulted copy operations. |
346 | | */ |
347 | | template <typename T> |
348 | | class RelaxedAtomicPointer { |
349 | | public: |
350 | 1.69M | RelaxedAtomicPointer() = default; |
351 | | RelaxedAtomicPointer(const RelaxedAtomicPointer& other) noexcept |
352 | 0 | : value_(other.load()) {} |
353 | | RelaxedAtomicPointer& operator=( |
354 | 0 | const RelaxedAtomicPointer& other) noexcept { |
355 | 0 | store(other.load()); |
356 | 0 | return *this; |
357 | 0 | } |
358 | | |
359 | 105M | T* load() const noexcept { |
360 | 105M | return value_.load(std::memory_order_relaxed); |
361 | 105M | } |
362 | | |
363 | 3.38M | void store(T* value) noexcept { |
364 | 3.38M | value_.store(value, std::memory_order_relaxed); |
365 | 3.38M | } |
366 | | |
367 | | private: |
368 | | std::atomic<T*> value_{nullptr}; |
369 | | }; |
370 | | |
371 | | // Used to allow resetting the node |
372 | 0 | Node& operator=(Node&&) noexcept = default; |
373 | | |
374 | 0 | void useWebDefaults() { |
375 | 0 | style_.setFlexDirection(FlexDirection::Row); |
376 | 0 | style_.setAlignContent(Align::Stretch); |
377 | 0 | } |
378 | | |
379 | | bool hasNewLayout_ : 1 = true; |
380 | | bool isReferenceBaseline_ : 1 = false; |
381 | | bool isDirty_ : 1 = true; |
382 | | bool alwaysFormsContainingBlock_ : 1 = false; |
383 | | NodeType nodeType_ : bitCount<NodeType>() = NodeType::Default; |
384 | | void* context_ = nullptr; |
385 | | YGMeasureFunc measureFunc_ = nullptr; |
386 | | YGMinContentMeasureFunc minContentMeasureFunc_ = nullptr; |
387 | | FloatOptional minContentWidth_{}; |
388 | | FloatOptional minContentHeight_{}; |
389 | | YGBaselineFunc baselineFunc_ = nullptr; |
390 | | YGDirtiedFunc dirtiedFunc_ = nullptr; |
391 | | Style style_; |
392 | | LayoutResults layout_; |
393 | | size_t lineIndex_ = 0; |
394 | | size_t contentsChildrenCount_ = 0; |
395 | | RelaxedAtomicPointer<Node> owner_; |
396 | | std::vector<Node*> children_; |
397 | | const Config* config_; |
398 | | std::array<Style::SizeLength, 2> processedDimensions_{ |
399 | | {StyleSizeLength::undefined(), StyleSizeLength::undefined()}}; |
400 | | }; |
401 | | |
402 | 26.1M | inline Node* resolveRef(const YGNodeRef ref) { |
403 | 26.1M | return static_cast<Node*>(ref); |
404 | 26.1M | } |
405 | | |
406 | 0 | inline const Node* resolveRef(const YGNodeConstRef ref) { |
407 | 0 | return static_cast<const Node*>(ref); |
408 | 0 | } |
409 | | |
410 | | } // namespace facebook::yoga |