Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void AnimatedPropsRegistry::update(
const auto& animatedProps = mutation.props;
auto it = pendingMap.find(tag);
if (it == pendingMap.end()) {
it = pendingMap.insert_or_assign(tag, std::make_unique<PropsSnapshot>())
it = pendingMap.insert_or_assign(tag, std::make_shared<PropsSnapshot>())
.first;
}
auto& snapshot = it->second;
Expand Down Expand Up @@ -114,8 +114,8 @@ void AnimatedPropsRegistry::initializeSurface(SurfaceId surfaceId) {
}

std::pair<
std::unordered_set<std::shared_ptr<const ShadowNodeFamily>>&,
SnapshotMap&>
std::unordered_set<std::shared_ptr<const ShadowNodeFamily>>,
SnapshotMap>
AnimatedPropsRegistry::getMap(SurfaceId surfaceId) {
auto lock = std::lock_guard(mutex_);
auto& [pendingMap, map, pendingFamilies, families] =
Expand All @@ -129,27 +129,34 @@ AnimatedPropsRegistry::getMap(SurfaceId surfaceId) {
if (currentIt == map.end()) {
map.insert_or_assign(tag, std::move(propsSnapshot));
} else {
auto& currentSnapshot = currentIt->second;
const auto& currentSnapshot = currentIt->second;
auto merged = std::make_shared<PropsSnapshot>();
if (currentSnapshot->rawProps) {
merged->rawProps =
std::make_unique<folly::dynamic>(*currentSnapshot->rawProps);
}
if (propsSnapshot->rawProps) {
if (currentSnapshot->rawProps) {
mergeAnimatedRawProps(
*currentSnapshot->rawProps, *propsSnapshot->rawProps);
if (merged->rawProps) {
mergeAnimatedRawProps(*merged->rawProps, *propsSnapshot->rawProps);
} else {
currentSnapshot->rawProps = std::move(propsSnapshot->rawProps);
merged->rawProps = std::move(propsSnapshot->rawProps);
}
}
if (propsSnapshot->text) {
currentSnapshot->text = std::move(propsSnapshot->text);
}
merged->text = propsSnapshot->text ? std::move(propsSnapshot->text)
: currentSnapshot->text;
if (!currentSnapshot->props) {
currentSnapshot->props = std::move(propsSnapshot->props);
currentSnapshot->propNames = std::move(propsSnapshot->propNames);
merged->props = std::move(propsSnapshot->props);
merged->propNames = std::move(propsSnapshot->propNames);
} else {
merged->props =
std::make_unique<BaseViewProps>(*currentSnapshot->props);
merged->propNames = currentSnapshot->propNames;
for (auto& propName : propsSnapshot->propNames) {
currentSnapshot->propNames.insert(propName);
updateProp(propName, *currentSnapshot->props, *propsSnapshot);
merged->propNames.insert(propName);
updateProp(propName, *merged->props, *propsSnapshot);
}
}
currentIt->second = std::move(merged);
}
}
pendingMap.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ struct PropsSnapshot {
std::optional<std::string> text;
};

using SnapshotMap = std::unordered_map<Tag, std::shared_ptr<const PropsSnapshot>>;

struct SurfaceContext {
std::unordered_map<Tag, std::unique_ptr<PropsSnapshot>> pendingMap, map;
std::unordered_map<Tag, std::shared_ptr<PropsSnapshot>> pendingMap;
SnapshotMap map;
std::unordered_set<std::shared_ptr<const ShadowNodeFamily>> pendingFamilies, families;
};

using SnapshotMap = std::unordered_map<Tag, std::unique_ptr<PropsSnapshot>>;

/*
* Merges raw animated props per key, like the props merged by the animation
* drivers. A null value is kept, so it still resets the prop on commit.
Expand All @@ -53,7 +54,7 @@ class AnimatedPropsRegistry {
void initializeSurface(SurfaceId surfaceId);
void clear(SurfaceId surfaceId);
void clearOnSurfaceStop(SurfaceId surfaceId);
std::pair<std::unordered_set<std::shared_ptr<const ShadowNodeFamily>> &, SnapshotMap &> getMap(SurfaceId surfaceId);
std::pair<std::unordered_set<std::shared_ptr<const ShadowNodeFamily>>, SnapshotMap> getMap(SurfaceId surfaceId);

private:
std::unordered_map<SurfaceId, SurfaceContext> surfaceContexts_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ RootShadowNode::Unshared AnimationBackendCommitHook::shadowTreeWillCommit(
return newRootShadowNode;
}

const auto& res = animatedPropsRegistry_->getMap(shadowTree.getSurfaceId());
auto& surfaceFamilies = res.first;
auto& updates = res.second;
const auto res = animatedPropsRegistry_->getMap(shadowTree.getSurfaceId());
const auto& surfaceFamilies = res.first;
const auto& updates = res.second;

if (surfaceFamilies.empty()) {
return newRootShadowNode;
Expand All @@ -48,7 +48,7 @@ RootShadowNode::Unshared AnimationBackendCommitHook::shadowTreeWillCommit(
if (auto updatesIter = updates.find(shadowNode.getTag());
updatesIter != updates.end() &&
surfaceFamilies.contains(shadowNode.getFamilyShared())) {
auto& snapshot = updatesIter->second;
const auto& snapshot = updatesIter->second;
if (!snapshot->propNames.empty() || snapshot->rawProps) {
PropsParserContext propsParserContext{
shadowNode.getSurfaceId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include <react/renderer/element/Element.h>
#include <react/renderer/element/testUtils.h>
#include <react/utils/ContextContainer.h>
#include <atomic>
#include <thread>

namespace facebook::react {

Expand Down Expand Up @@ -52,8 +54,11 @@ class AnimatedPropsRegistryTest : public ::testing::Test {
EXPECT_EQ(*snapshot().rawProps, expected);
}

PropsSnapshot& snapshot() {
return *registry.getMap(1).second.at(10);
std::shared_ptr<const PropsSnapshot> lastSnapshot;

const PropsSnapshot& snapshot() {
lastSnapshot = registry.getMap(1).second.at(10);
return *lastSnapshot;
}
};

Expand Down Expand Up @@ -269,4 +274,84 @@ TEST_F(AnimatedPropsRegistryTest, childrenWithTextKeepsNodeWithoutRawText) {
EXPECT_EQ(childrenWithText(*textNode, {}, "new"), nullptr);
}

TEST_F(AnimatedPropsRegistryTest, concurrentGetMapClearAndSurfaceStop) {
constexpr int kNumViews = 16;
constexpr int kIterations = 200;

std::vector<std::shared_ptr<ViewShadowNode>> views;
views.reserve(kNumViews);
for (int i = 0; i < kNumViews; ++i) {
views.push_back(
builder.build(Element<ViewShadowNode>().surfaceId(1).tag(100 + i)));
}

std::atomic<bool> start{false};
std::atomic<bool> done{false};

std::thread writer([&]() {
while (!start.load(std::memory_order_acquire)) {
}
for (int iter = 0; iter < kIterations; ++iter) {
std::vector<AnimationMutations> batches(1);
for (int i = 0; i < kNumViews; ++i) {
AnimatedPropsBuilder propsBuilder;
propsBuilder.setOpacity(static_cast<Float>((iter + i) % 100) / 100.0f);
propsBuilder.setText(std::to_string(iter + i));
folly::dynamic raw = folly::dynamic::object("zIndex", iter + i);
propsBuilder.storeDynamic(raw);
batches[0].batch.push_back(
AnimationMutation{
.tag = views[i]->getTag(),
.family = views[i]->getFamilyShared(),
.props = propsBuilder.get()});
}
registry.update(batches);
}
done.store(true, std::memory_order_release);
});

std::thread reader([&]() {
while (!start.load(std::memory_order_acquire)) {
}
while (!done.load(std::memory_order_acquire)) {
const auto [families, map] = registry.getMap(1);
for (const auto& family : families) {
if (auto it = map.find(family->getTag()); it != map.end()) {
const auto& snapshot = it->second;
BaseViewProps viewProps;
for (const auto& propName : snapshot->propNames) {
updateProp(propName, viewProps, *snapshot);
}
if (snapshot->rawProps) {
EXPECT_TRUE(snapshot->rawProps->isObject());
}
if (snapshot->text) {
EXPECT_FALSE(snapshot->text->empty());
}
}
}
}
});

std::thread clearer([&]() {
while (!start.load(std::memory_order_acquire)) {
}
int iter = 0;
while (!done.load(std::memory_order_acquire)) {
if (iter % 10 == 0) {
registry.clearOnSurfaceStop(1);
registry.initializeSurface(1);
} else {
registry.clear(1);
}
++iter;
}
});

start.store(true, std::memory_order_release);
writer.join();
reader.join();
clearer.join();
}

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
#include <gtest/gtest.h>
#include <react/renderer/animationbackend/AnimatedPropsBuilder.h>
#include <react/renderer/animationbackend/AnimationBackend.h>
#include <react/renderer/components/root/RootComponentDescriptor.h>
#include <react/renderer/components/root/RootShadowNode.h>
#include <react/renderer/components/view/ViewShadowNode.h>
#include <react/renderer/element/ComponentBuilder.h>
#include <react/renderer/element/Element.h>
#include <react/renderer/element/testUtils.h>
#include <react/renderer/mounting/ShadowTree.h>
#include <react/renderer/uimanager/UIManager.h>
#include <react/renderer/uimanager/UIManagerDelegate.h>
#include <react/utils/ContextContainer.h>
#include <atomic>
#include <thread>

namespace facebook::react {

Expand Down Expand Up @@ -138,4 +143,120 @@ TEST_F(AnimationBackendTest, textOnlyMutationIsNotWrittenDirectly) {
EXPECT_TRUE(delegate.synchronousUpdates.empty());
}

TEST_F(
AnimationBackendTest,
concurrentCommitsAndRegistryClearDoNotCorruptHeap) {
constexpr SurfaceId kSurfaceId = 1;
constexpr int kNumViews = 32;
constexpr int kIterations = 200;

std::vector<std::shared_ptr<ViewShadowNode>> views(kNumViews);
std::vector<ElementFragment> childElements;
childElements.reserve(kNumViews);
for (int i = 0; i < kNumViews; ++i) {
childElements.push_back(
Element<ViewShadowNode>()
.surfaceId(kSurfaceId)
.tag(100 + i)
.reference(views[i]));
}

std::shared_ptr<RootShadowNode> initialRoot;
builder.build(
Element<RootShadowNode>()
.surfaceId(kSurfaceId)
.tag(1)
.reference(initialRoot)
.children(std::move(childElements)));

auto localUiManager = std::make_shared<UIManager>(
[](std::function<void(jsi::Runtime&)>&& /*callback*/) {},
contextContainer);
RecordingUIManagerDelegate localDelegate;
localUiManager->setDelegate(&localDelegate);

auto shadowTree = std::make_unique<ShadowTree>(
kSurfaceId,
LayoutConstraints{},
LayoutContext{},
*localUiManager,
*contextContainer);
auto* shadowTreePtr = shadowTree.get();

shadowTree->commit(
[&initialRoot](const RootShadowNode& /*oldRootShadowNode*/) {
return initialRoot;
},
{.mountSynchronously = true});

localUiManager->startEmptySurface(std::move(shadowTree));
auto localBackend = std::make_shared<AnimationBackend>(
std::make_shared<TestAnimationChoreographer>(), localUiManager);
localUiManager->unstable_setAnimationBackend(localBackend);

std::atomic<bool> start{false};

std::thread mainThread([&]() {
while (!start.load(std::memory_order_acquire)) {
}
for (int iter = 0; iter < kIterations; ++iter) {
localBackend->pushAnimationMutations(
[&, iter](AnimationTimestamp /*ts*/) {
AnimationMutations mutations;
for (int i = 0; i < kNumViews; ++i) {
AnimatedPropsBuilder propsBuilder;
propsBuilder.setOpacity(
static_cast<Float>((iter + i) % 100) / 100.0f);
folly::dynamic raw = folly::dynamic::object(
"opacity", (iter + i) % 100 / 100.0)("zIndex", iter + i);
propsBuilder.storeDynamic(raw);
mutations.batch.push_back(
AnimationMutation{
.tag = views[i]->getTag(),
.family = views[i]->getFamilyShared(),
.props = propsBuilder.get(),
.hasLayoutUpdates = false});
}
return mutations;
});

shadowTreePtr->commit(
[](const RootShadowNode& oldRootShadowNode) {
return std::make_shared<RootShadowNode>(
oldRootShadowNode, ShadowNodeFragment{});
},
{
.mountSynchronously = true,
.source = ShadowTreeCommitSource::ReactRevisionMerge,
});
}
});

std::thread jsThread([&]() {
while (!start.load(std::memory_order_acquire)) {
}
for (int iter = 0; iter < kIterations * 2; ++iter) {
shadowTreePtr->commit(
[](const RootShadowNode& oldRootShadowNode) {
return std::make_shared<RootShadowNode>(
oldRootShadowNode, ShadowNodeFragment{});
},
{
.mountSynchronously = false,
.source = (iter % 2 == 0)
? ShadowTreeCommitSource::React
: ShadowTreeCommitSource::AnimationEndSync,
});
localBackend->clearRegistry(kSurfaceId);
}
});

start.store(true, std::memory_order_release);
mainThread.join();
jsThread.join();

localUiManager->stopSurface(kSurfaceId);
localUiManager->setDelegate(nullptr);
}

} // namespace facebook::react
Loading