Files
react-native/ReactCommon/fabric/core/layout/LayoutableShadowNode.h
T
Valentin Shergin 28a5f122a8 Fabric: MountingCoordinator::revoke()
Summary:
MountingCoordinator is a borderline between Core and Mounting. Some of Core design constraints are impossible/impractical to enforce on Mounting layer, so we have to handle all of those cases in `MountingCoordinator`.

One of the constrains is that all ShadowNodes implicitly depend on associated ComponentDescriptor instances without retaining them (retaining is expensive and creates a retain cycle).
The problem is that the Mounting layer can call `MountingCoordinator::pull()` at any moment (even after the whole Core is already destroyed). To prevent this, the owner of a `MountingCoordinator` on the Core side calls `revoke()` right before being deallocated (right before the moment the owner cannot guarantee the constraint).

Reviewed By: JoshuaGross

Differential Revision: D17272295

fbshipit-source-id: ba8b02eab8f84cce68aa65c1ad36950cd2498049
2019-09-09 20:26:25 -07:00

143 lines
4.1 KiB
C++

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <array>
#include <cmath>
#include <memory>
#include <vector>
#include <better/small_vector.h>
#include <react/core/LayoutMetrics.h>
#include <react/core/Sealable.h>
#include <react/core/ShadowNode.h>
#include <react/debug/DebugStringConvertible.h>
#include <react/graphics/Transform.h>
namespace facebook {
namespace react {
struct LayoutConstraints;
struct LayoutContext;
/*
* Describes all sufficient layout API (in approach-agnostic way)
* which makes a concurrent layout possible.
*/
class LayoutableShadowNode : public virtual Sealable {
public:
using UnsharedList = better::
small_vector<LayoutableShadowNode *, kShadowNodeChildrenSmallVectorSize>;
virtual ~LayoutableShadowNode() noexcept = default;
/*
* Measures the node (and node content, probably recursively) with
* given constrains and relying on possible layout.
* Default implementation returns zero size.
*/
virtual Size measure(LayoutConstraints layoutConstraints) const;
/*
* Computes layout recursively.
* Additional environmental constraints might be provided via `layoutContext`
* argument.
* Default implementation basically calls `layoutChildren()` and then
* `layout()` (recursively), and provides some obvious performance
* optimization.
*/
virtual void layout(LayoutContext layoutContext);
/*
* Returns layout metrics computed during previous layout pass.
*/
virtual LayoutMetrics getLayoutMetrics() const;
/*
* Returns `true` if the node represents only information necessary for
* layout computation and can be safely removed from view hierarchy.
* Default implementation returns `false`.
*/
virtual bool isLayoutOnly() const;
/*
* Returns a transform object that represents transformations that will/should
* be applied on top of regular layout metrics by mounting layer.
* The `transform` value modifies a coordinate space of a layout system.
* Default implementation returns `Identity` transform.
*/
virtual Transform getTransform() const;
/*
* Returns layout metrics relatively to the given ancestor node.
*/
LayoutMetrics getRelativeLayoutMetrics(
const LayoutableShadowNode &ancestorLayoutableShadowNode) const;
protected:
/*
* Clean or Dirty layout state:
* Indicates whether all nodes (and possibly their subtrees) along the path
* to the root node should be re-laid out.
*/
virtual void cleanLayout() = 0;
virtual void dirtyLayout() = 0;
virtual bool getIsLayoutClean() const = 0;
/*
* Indicates does the shadow node (or any descendand node of the node)
* get a new layout metrics during a previous layout pass.
*/
virtual void setHasNewLayout(bool hasNewLayout) = 0;
virtual bool getHasNewLayout() const = 0;
/*
* Applies layout for all children;
* does not call anything in recursive manner *by desing*.
*/
virtual void layoutChildren(LayoutContext layoutContext);
/*
* Unifed methods to access text layout metrics.
*/
virtual Float firstBaseline(Size size) const;
virtual Float lastBaseline(Size size) const;
/*
* Returns layoutable children to interate on.
*/
virtual LayoutableShadowNode::UnsharedList getLayoutableChildNodes()
const = 0;
/*
* In case layout algorithm needs to mutate this (probably sealed) node,
* it has to clone and replace it in the hierarchy before to do so.
*/
virtual LayoutableShadowNode *cloneAndReplaceChild(
LayoutableShadowNode *child,
int suggestedIndex = -1) = 0;
/*
* Sets layout metrics for the shadow node.
* Returns true if the metrics are different from previous ones.
*/
virtual bool setLayoutMetrics(LayoutMetrics layoutMetrics);
#pragma mark - DebugStringConvertible
#if RN_DEBUG_STRING_CONVERTIBLE
SharedDebugStringConvertibleList getDebugProps() const;
#endif
private:
LayoutMetrics layoutMetrics_{};
};
} // namespace react
} // namespace facebook