2018-09-12 01:27:47 +03:00
|
|
|
// Copyright (c) Facebook, Inc. and its affiliates.
|
2018-06-01 01:31:03 +03: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-09 08:56:16 +03:00
|
|
|
|
|
|
|
#include "ShadowTree.h"
|
|
|
|
|
2019-03-04 00:47:14 +03:00
|
|
|
#include <react/components/root/RootComponentDescriptor.h>
|
2019-05-02 02:24:23 +03:00
|
|
|
#include <react/components/view/ViewShadowNode.h>
|
2018-11-11 01:19:08 +03:00
|
|
|
#include <react/core/LayoutContext.h>
|
|
|
|
#include <react/core/LayoutPrimitives.h>
|
2019-02-12 02:51:34 +03:00
|
|
|
#include <react/debug/SystraceSection.h>
|
2019-05-04 01:05:26 +03:00
|
|
|
#include <react/mounting/MountingTelemetry.h>
|
2019-05-04 01:05:26 +03:00
|
|
|
#include <react/mounting/ShadowTreeRevision.h>
|
2018-11-28 05:29:39 +03:00
|
|
|
#include <react/mounting/ShadowViewMutation.h>
|
2018-05-09 08:56:16 +03:00
|
|
|
|
2018-10-10 02:25:13 +03:00
|
|
|
#include "ShadowTreeDelegate.h"
|
2018-05-09 08:56:16 +03:00
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
2019-01-17 07:17:01 +03:00
|
|
|
static void updateMountedFlag(
|
|
|
|
const SharedShadowNodeList &oldChildren,
|
|
|
|
const SharedShadowNodeList &newChildren) {
|
|
|
|
// This is a simplified version of Diffing algorithm that only updates
|
|
|
|
// `mounted` flag on `ShadowNode`s. The algorithm sets "mounted" flag before
|
|
|
|
// "unmounted" to allow `ShadowNode` detect a situation where the node was
|
|
|
|
// remounted.
|
|
|
|
|
|
|
|
if (&oldChildren == &newChildren) {
|
|
|
|
// Lists are identical, nothing to do.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldChildren.size() == 0 && newChildren.size() == 0) {
|
|
|
|
// Both lists are empty, nothing to do.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int index;
|
|
|
|
|
|
|
|
// Stage 1: Mount and unmount "updated" children.
|
|
|
|
for (index = 0; index < oldChildren.size() && index < newChildren.size();
|
|
|
|
index++) {
|
|
|
|
const auto &oldChild = oldChildren[index];
|
|
|
|
const auto &newChild = newChildren[index];
|
|
|
|
|
|
|
|
if (oldChild == newChild) {
|
|
|
|
// Nodes are identical, skipping the subtree.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldChild->getTag() != newChild->getTag()) {
|
|
|
|
// Totally different nodes, updating is impossible.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
newChild->setMounted(true);
|
|
|
|
oldChild->setMounted(false);
|
|
|
|
|
|
|
|
updateMountedFlag(oldChild->getChildren(), newChild->getChildren());
|
|
|
|
}
|
|
|
|
|
|
|
|
int lastIndexAfterFirstStage = index;
|
|
|
|
|
|
|
|
// State 2: Mount new children.
|
|
|
|
for (index = lastIndexAfterFirstStage; index < newChildren.size(); index++) {
|
|
|
|
const auto &newChild = newChildren[index];
|
|
|
|
newChild->setMounted(true);
|
|
|
|
updateMountedFlag({}, newChild->getChildren());
|
|
|
|
}
|
|
|
|
|
|
|
|
// State 3: Unmount old children.
|
|
|
|
for (index = lastIndexAfterFirstStage; index < oldChildren.size(); index++) {
|
|
|
|
const auto &oldChild = oldChildren[index];
|
|
|
|
oldChild->setMounted(false);
|
|
|
|
updateMountedFlag(oldChild->getChildren(), {});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 02:25:08 +03:00
|
|
|
ShadowTree::ShadowTree(
|
2018-10-10 02:25:13 +03:00
|
|
|
SurfaceId surfaceId,
|
|
|
|
const LayoutConstraints &layoutConstraints,
|
2019-03-04 00:47:14 +03:00
|
|
|
const LayoutContext &layoutContext,
|
|
|
|
const RootComponentDescriptor &rootComponentDescriptor)
|
2018-10-10 02:25:13 +03:00
|
|
|
: surfaceId_(surfaceId) {
|
|
|
|
const auto noopEventEmitter = std::make_shared<const ViewEventEmitter>(
|
|
|
|
nullptr, -1, std::shared_ptr<const EventDispatcher>());
|
2018-10-10 02:25:08 +03:00
|
|
|
|
|
|
|
const auto props = std::make_shared<const RootProps>(
|
2018-10-10 02:25:13 +03:00
|
|
|
*RootShadowNode::defaultSharedProps(), layoutConstraints, layoutContext);
|
2018-05-09 08:56:16 +03:00
|
|
|
|
2019-03-04 00:47:14 +03:00
|
|
|
rootShadowNode_ = std::static_pointer_cast<const RootShadowNode>(
|
|
|
|
rootComponentDescriptor.createShadowNode(ShadowNodeFragment{
|
2019-03-04 23:54:32 +03:00
|
|
|
/* .tag = */ surfaceId,
|
2019-04-30 07:17:38 +03:00
|
|
|
/* .surfaceId = */ surfaceId,
|
2019-03-04 23:54:32 +03:00
|
|
|
/* .props = */ props,
|
|
|
|
/* .eventEmitter = */ noopEventEmitter,
|
2019-03-04 00:47:14 +03:00
|
|
|
}));
|
2019-04-01 20:47:04 +03:00
|
|
|
|
2019-05-04 01:05:26 +03:00
|
|
|
mountingCoordinator_ = std::make_shared<MountingCoordinator const>(
|
|
|
|
ShadowTreeRevision{rootShadowNode_, 0, {}});
|
2018-05-09 08:56:16 +03:00
|
|
|
}
|
|
|
|
|
2018-09-14 08:56:00 +03:00
|
|
|
ShadowTree::~ShadowTree() {
|
2019-02-05 04:24:23 +03:00
|
|
|
commit(
|
|
|
|
[](const SharedRootShadowNode &oldRootShadowNode) {
|
|
|
|
return std::make_shared<RootShadowNode>(
|
|
|
|
*oldRootShadowNode,
|
|
|
|
ShadowNodeFragment{
|
2019-03-04 23:54:32 +03:00
|
|
|
/* .tag = */ ShadowNodeFragment::tagPlaceholder(),
|
2019-04-30 07:17:38 +03:00
|
|
|
/* .surfaceId = */ ShadowNodeFragment::surfaceIdPlaceholder(),
|
2019-03-04 23:54:32 +03:00
|
|
|
/* .props = */ ShadowNodeFragment::propsPlaceholder(),
|
|
|
|
/* .eventEmitter = */
|
|
|
|
ShadowNodeFragment::eventEmitterPlaceholder(),
|
|
|
|
/* .children = */ ShadowNode::emptySharedShadowNodeSharedList(),
|
|
|
|
});
|
2019-05-04 01:05:26 +03:00
|
|
|
});
|
2019-09-10 06:22:55 +03:00
|
|
|
mountingCoordinator_->revoke();
|
2018-09-14 08:56:00 +03:00
|
|
|
}
|
|
|
|
|
2018-10-10 02:25:08 +03:00
|
|
|
Tag ShadowTree::getSurfaceId() const {
|
|
|
|
return surfaceId_;
|
2018-05-09 08:56:16 +03:00
|
|
|
}
|
|
|
|
|
2019-05-04 01:05:26 +03:00
|
|
|
void ShadowTree::commit(ShadowTreeCommitTransaction transaction) const {
|
2019-02-12 02:51:34 +03:00
|
|
|
SystraceSection s("ShadowTree::commit");
|
|
|
|
|
2019-01-17 07:17:01 +03:00
|
|
|
int attempts = 0;
|
2018-05-25 04:23:13 +03:00
|
|
|
|
2019-01-17 07:17:01 +03:00
|
|
|
while (true) {
|
|
|
|
attempts++;
|
2019-05-04 01:05:26 +03:00
|
|
|
if (tryCommit(transaction)) {
|
2019-01-17 07:17:01 +03:00
|
|
|
return;
|
2019-01-17 07:17:00 +03:00
|
|
|
}
|
2018-10-10 02:25:01 +03:00
|
|
|
|
2019-01-17 07:17:01 +03:00
|
|
|
// After multiple attempts, we failed to commit the transaction.
|
|
|
|
// Something internally went terribly wrong.
|
|
|
|
assert(attempts < 1024);
|
|
|
|
}
|
|
|
|
}
|
2018-10-10 02:25:01 +03:00
|
|
|
|
2019-05-04 01:05:26 +03:00
|
|
|
bool ShadowTree::tryCommit(ShadowTreeCommitTransaction transaction) const {
|
2019-02-12 02:51:34 +03:00
|
|
|
SystraceSection s("ShadowTree::tryCommit");
|
|
|
|
|
2019-05-04 01:05:26 +03:00
|
|
|
auto telemetry = MountingTelemetry{};
|
|
|
|
telemetry.willCommit();
|
|
|
|
|
2019-01-17 07:17:01 +03:00
|
|
|
SharedRootShadowNode oldRootShadowNode;
|
2018-05-09 08:56:16 +03:00
|
|
|
|
2019-01-17 07:17:01 +03:00
|
|
|
{
|
|
|
|
// Reading `rootShadowNode_` in shared manner.
|
2019-03-21 10:13:39 +03:00
|
|
|
std::shared_lock<better::shared_mutex> lock(commitMutex_);
|
2019-01-17 07:17:01 +03:00
|
|
|
oldRootShadowNode = rootShadowNode_;
|
|
|
|
}
|
2019-01-17 07:17:00 +03:00
|
|
|
|
2019-01-17 07:17:01 +03:00
|
|
|
UnsharedRootShadowNode newRootShadowNode = transaction(oldRootShadowNode);
|
|
|
|
|
|
|
|
if (!newRootShadowNode) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-02 02:24:23 +03:00
|
|
|
std::vector<LayoutableShadowNode const *> affectedLayoutableNodes{};
|
|
|
|
affectedLayoutableNodes.reserve(1024);
|
|
|
|
|
2019-05-04 01:05:26 +03:00
|
|
|
telemetry.willLayout();
|
2019-05-02 02:24:23 +03:00
|
|
|
newRootShadowNode->layout(&affectedLayoutableNodes);
|
2019-05-04 01:05:26 +03:00
|
|
|
telemetry.didLayout();
|
|
|
|
|
2019-01-17 07:17:01 +03:00
|
|
|
newRootShadowNode->sealRecursive();
|
2018-05-09 08:56:16 +03:00
|
|
|
|
2019-05-04 01:05:26 +03:00
|
|
|
auto revisionNumber = ShadowTreeRevision::Number{};
|
2018-08-27 17:21:18 +03:00
|
|
|
|
2019-01-17 07:17:01 +03:00
|
|
|
{
|
|
|
|
// Updating `rootShadowNode_` in unique manner if it hasn't changed.
|
2019-03-21 10:13:39 +03:00
|
|
|
std::unique_lock<better::shared_mutex> lock(commitMutex_);
|
2019-01-17 07:17:00 +03:00
|
|
|
|
2019-01-17 07:17:01 +03:00
|
|
|
if (rootShadowNode_ != oldRootShadowNode) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-17 07:17:01 +03:00
|
|
|
|
2019-01-17 07:17:01 +03:00
|
|
|
rootShadowNode_ = newRootShadowNode;
|
2019-01-17 07:17:00 +03:00
|
|
|
|
2019-01-17 07:17:01 +03:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> dispatchLock(EventEmitter::DispatchMutex());
|
2019-01-17 07:17:00 +03:00
|
|
|
|
2019-01-17 07:17:01 +03:00
|
|
|
updateMountedFlag(
|
|
|
|
oldRootShadowNode->getChildren(), newRootShadowNode->getChildren());
|
2019-01-17 07:17:00 +03:00
|
|
|
}
|
|
|
|
|
2019-05-04 01:05:26 +03:00
|
|
|
revisionNumber_++;
|
|
|
|
revisionNumber = revisionNumber_;
|
2019-01-17 07:17:01 +03:00
|
|
|
}
|
|
|
|
|
2019-05-02 02:24:23 +03:00
|
|
|
emitLayoutEvents(affectedLayoutableNodes);
|
2019-01-17 07:17:00 +03:00
|
|
|
|
2019-05-04 01:05:26 +03:00
|
|
|
telemetry.didCommit();
|
|
|
|
|
2019-05-04 01:05:26 +03:00
|
|
|
mountingCoordinator_->push(
|
|
|
|
ShadowTreeRevision{newRootShadowNode, revisionNumber, telemetry});
|
|
|
|
|
2019-01-17 07:17:01 +03:00
|
|
|
if (delegate_) {
|
2019-05-04 01:05:26 +03:00
|
|
|
delegate_->shadowTreeDidCommit(*this, mountingCoordinator_);
|
2019-01-17 07:17:00 +03:00
|
|
|
}
|
2018-10-10 02:24:57 +03:00
|
|
|
|
2019-01-17 07:17:01 +03:00
|
|
|
return true;
|
2018-05-09 08:56:16 +03:00
|
|
|
}
|
|
|
|
|
2018-10-10 02:25:13 +03:00
|
|
|
void ShadowTree::emitLayoutEvents(
|
2019-05-02 02:24:23 +03:00
|
|
|
std::vector<LayoutableShadowNode const *> &affectedLayoutableNodes) const {
|
2019-02-12 02:51:34 +03:00
|
|
|
SystraceSection s("ShadowTree::emitLayoutEvents");
|
|
|
|
|
2019-05-02 02:24:23 +03:00
|
|
|
for (auto const *layoutableNode : affectedLayoutableNodes) {
|
|
|
|
// Only instances of `ViewShadowNode` (and subclasses) are supported.
|
|
|
|
auto const &viewShadowNode =
|
|
|
|
static_cast<ViewShadowNode const &>(*layoutableNode);
|
|
|
|
auto const &viewEventEmitter = static_cast<ViewEventEmitter const &>(
|
|
|
|
*viewShadowNode.getEventEmitter());
|
2018-09-04 08:53:18 +03:00
|
|
|
|
2018-10-10 02:25:13 +03:00
|
|
|
// Checking if the `onLayout` event was requested for the particular Shadow
|
|
|
|
// Node.
|
2019-05-02 02:24:23 +03:00
|
|
|
auto const &viewProps =
|
|
|
|
static_cast<ViewProps const &>(*viewShadowNode.getProps());
|
|
|
|
if (!viewProps.onLayout) {
|
2018-09-04 08:53:18 +03:00
|
|
|
continue;
|
2018-05-25 04:23:13 +03:00
|
|
|
}
|
2018-09-04 08:53:18 +03:00
|
|
|
|
2019-05-02 02:24:23 +03:00
|
|
|
viewEventEmitter.onLayout(layoutableNode->getLayoutMetrics());
|
2018-05-25 04:23:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-09 08:56:16 +03:00
|
|
|
#pragma mark - Delegate
|
|
|
|
|
2018-09-26 20:02:04 +03:00
|
|
|
void ShadowTree::setDelegate(ShadowTreeDelegate const *delegate) {
|
2018-05-09 08:56:16 +03:00
|
|
|
delegate_ = delegate;
|
|
|
|
}
|
|
|
|
|
2018-09-26 20:02:04 +03:00
|
|
|
ShadowTreeDelegate const *ShadowTree::getDelegate() const {
|
2018-05-09 08:56:16 +03:00
|
|
|
return delegate_;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace react
|
|
|
|
} // namespace facebook
|