2018-09-11 15:27:47 -07:00
|
|
|
// Copyright (c) Facebook, Inc. and its affiliates.
|
2018-05-31 15:31:03 -07:00
|
|
|
|
|
|
|
// This source code is licensed under the MIT license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree.
|
2018-05-08 22:56:16 -07:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
|
|
|
|
2018-11-10 14:19:08 -08:00
|
|
|
#include <react/components/root/RootShadowNode.h>
|
|
|
|
#include <react/core/LayoutConstraints.h>
|
|
|
|
#include <react/core/ReactPrimitives.h>
|
|
|
|
#include <react/core/ShadowNode.h>
|
2018-11-27 18:29:39 -08:00
|
|
|
#include <react/mounting/ShadowViewMutation.h>
|
2018-11-10 14:19:08 -08:00
|
|
|
#include <react/uimanager/ShadowTreeDelegate.h>
|
2018-05-08 22:56:16 -07:00
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Represents the shadow tree and its lifecycle.
|
|
|
|
*/
|
2018-09-03 22:53:28 -07:00
|
|
|
class ShadowTree final {
|
2018-10-09 16:25:13 -07:00
|
|
|
public:
|
2018-05-08 22:56:16 -07:00
|
|
|
/*
|
2018-10-09 16:25:08 -07:00
|
|
|
* Creates a new shadow tree instance.
|
2018-05-08 22:56:16 -07:00
|
|
|
*/
|
2018-10-09 16:25:08 -07:00
|
|
|
ShadowTree(
|
2018-10-09 16:25:13 -07:00
|
|
|
SurfaceId surfaceId,
|
|
|
|
const LayoutConstraints &layoutConstraints,
|
|
|
|
const LayoutContext &layoutContext);
|
2018-05-08 22:56:16 -07:00
|
|
|
|
2018-09-13 22:56:00 -07:00
|
|
|
~ShadowTree();
|
|
|
|
|
2018-05-08 22:56:16 -07:00
|
|
|
/*
|
2018-10-09 16:25:08 -07:00
|
|
|
* Returns the `SurfaceId` associated with the shadow tree.
|
2018-05-08 22:56:16 -07:00
|
|
|
*/
|
2018-10-09 16:25:08 -07:00
|
|
|
SurfaceId getSurfaceId() const;
|
2018-05-08 22:56:16 -07:00
|
|
|
|
2018-10-09 16:25:03 -07:00
|
|
|
/*
|
|
|
|
* Synchronously runs `function` when `commitMutex_` is acquired.
|
|
|
|
* It is useful in cases when transactional consistency and/or successful
|
|
|
|
* commit are required. E.g. you might want to run `measure` and
|
|
|
|
* `constraintLayout` as part of a single congious transaction.
|
|
|
|
* Use this only if it is necessary. All public methods of the class are
|
|
|
|
* already thread-safe.
|
|
|
|
*/
|
|
|
|
void synchronize(std::function<void(void)> function) const;
|
|
|
|
|
2018-05-08 22:56:16 -07:00
|
|
|
#pragma mark - Layout
|
|
|
|
|
|
|
|
/*
|
2018-10-09 16:25:13 -07:00
|
|
|
* Measures the shadow tree with given `layoutConstraints` and
|
|
|
|
* `layoutContext`. Can be called from any thread, side-effect-less.
|
2018-05-08 22:56:16 -07:00
|
|
|
*/
|
2018-10-09 16:25:13 -07:00
|
|
|
Size measure(
|
|
|
|
const LayoutConstraints &layoutConstraints,
|
|
|
|
const LayoutContext &layoutContext) const;
|
2018-05-08 22:56:16 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Applies given `layoutConstraints` and `layoutContext` and commit
|
|
|
|
* the new shadow tree.
|
2018-10-09 16:25:01 -07:00
|
|
|
* Returns `true` if the operation finished successfully.
|
2018-05-08 22:56:16 -07:00
|
|
|
* Can be called from any thread.
|
|
|
|
*/
|
2018-10-09 16:25:13 -07:00
|
|
|
bool constraintLayout(
|
|
|
|
const LayoutConstraints &layoutConstraints,
|
|
|
|
const LayoutContext &layoutContext) const;
|
2018-05-08 22:56:16 -07:00
|
|
|
|
|
|
|
#pragma mark - Application
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new shadow tree with given `rootChildNodes` and commit.
|
|
|
|
* Can be called from any thread.
|
2018-10-09 16:25:01 -07:00
|
|
|
* Returns `true` if the operation finished successfully.
|
2018-05-08 22:56:16 -07:00
|
|
|
*/
|
2018-10-09 16:25:01 -07:00
|
|
|
bool complete(const SharedShadowNodeUnsharedList &rootChildNodes) const;
|
2018-05-08 22:56:16 -07:00
|
|
|
|
2018-11-27 18:01:01 -08:00
|
|
|
/*
|
|
|
|
* Replaces a given old shadow node with a new one in the tree by cloning all
|
|
|
|
* nodes on the path to the root node and then complete the tree.
|
|
|
|
* Can be called from any thread.
|
|
|
|
* Returns `true` if the operation finished successfully.
|
|
|
|
*/
|
|
|
|
bool completeByReplacingShadowNode(
|
|
|
|
const SharedShadowNode &oldShadowNode,
|
|
|
|
const SharedShadowNode &newShadowNode) const;
|
|
|
|
|
2018-11-21 17:10:31 -08:00
|
|
|
/*
|
|
|
|
* Returns a root shadow node that represents the last committed three.
|
|
|
|
*/
|
|
|
|
SharedRootShadowNode getRootShadowNode() const;
|
|
|
|
|
2018-05-08 22:56:16 -07:00
|
|
|
#pragma mark - Delegate
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sets and gets the delegate.
|
|
|
|
* The delegate is stored as a raw pointer, so the owner must null
|
|
|
|
* the pointer before being destroyed.
|
|
|
|
*/
|
2018-09-26 10:02:04 -07:00
|
|
|
void setDelegate(ShadowTreeDelegate const *delegate);
|
|
|
|
ShadowTreeDelegate const *getDelegate() const;
|
2018-05-08 22:56:16 -07:00
|
|
|
|
2018-10-09 16:25:13 -07:00
|
|
|
private:
|
2018-10-09 16:25:10 -07:00
|
|
|
UnsharedRootShadowNode cloneRootShadowNode(
|
2018-10-09 16:25:13 -07:00
|
|
|
const SharedRootShadowNode &oldRootShadowNode,
|
|
|
|
const LayoutConstraints &layoutConstraints,
|
|
|
|
const LayoutContext &layoutContext) const;
|
2018-10-09 16:25:10 -07:00
|
|
|
|
|
|
|
bool complete(
|
2018-10-09 16:25:13 -07:00
|
|
|
const SharedRootShadowNode &oldRootShadowNode,
|
|
|
|
const UnsharedRootShadowNode &newRootShadowNode) const;
|
2018-05-08 22:56:16 -07:00
|
|
|
|
2018-08-27 07:21:18 -07:00
|
|
|
bool commit(
|
2018-10-09 16:25:13 -07:00
|
|
|
const SharedRootShadowNode &oldRootShadowNode,
|
|
|
|
const SharedRootShadowNode &newRootShadowNode,
|
|
|
|
const ShadowViewMutationList &mutations) const;
|
2018-10-09 16:25:10 -07:00
|
|
|
|
2018-10-09 16:24:57 -07:00
|
|
|
void toggleEventEmitters(const ShadowViewMutationList &mutations) const;
|
|
|
|
void emitLayoutEvents(const ShadowViewMutationList &mutations) const;
|
|
|
|
|
2018-10-09 16:25:08 -07:00
|
|
|
const SurfaceId surfaceId_;
|
2018-10-09 16:24:57 -07:00
|
|
|
mutable SharedRootShadowNode rootShadowNode_; // Protected by `commitMutex_`.
|
2018-09-26 10:02:04 -07:00
|
|
|
ShadowTreeDelegate const *delegate_;
|
2018-10-09 16:25:03 -07:00
|
|
|
mutable std::recursive_mutex commitMutex_;
|
2018-05-08 22:56:16 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace react
|
|
|
|
} // namespace facebook
|