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

Large diffs are not rendered by default.

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 @@ -113,47 +113,63 @@ void AnimatedPropsRegistry::initializeSurface(SurfaceId surfaceId) {
surfaceContexts_.try_emplace(surfaceId);
}

std::pair<
std::unordered_set<std::shared_ptr<const ShadowNodeFamily>>&,
SnapshotMap&>
std::pair<std::shared_ptr<const FamilySet>, std::shared_ptr<const SnapshotMap>>
AnimatedPropsRegistry::getMap(SurfaceId surfaceId) {
auto lock = std::lock_guard(mutex_);
auto& [pendingMap, map, pendingFamilies, families] =
surfaceContexts_[surfaceId];

for (auto& family : pendingFamilies) {
families.insert(family);
if (!pendingFamilies.empty()) {
auto nextFamilies = families ? std::make_shared<FamilySet>(*families)
: std::make_shared<FamilySet>();
nextFamilies->merge(pendingFamilies);
pendingFamilies.clear();
families = std::move(nextFamilies);
}
for (auto& [tag, propsSnapshot] : pendingMap) {
auto currentIt = map.find(tag);
if (currentIt == map.end()) {
map.insert_or_assign(tag, std::move(propsSnapshot));
} else {
auto& currentSnapshot = currentIt->second;
if (propsSnapshot->rawProps) {
if (!pendingMap.empty()) {
auto nextMap = map ? std::make_shared<SnapshotMap>(*map)
: std::make_shared<SnapshotMap>();
for (auto& [tag, propsSnapshot] : pendingMap) {
auto currentIt = nextMap->find(tag);
if (currentIt == nextMap->end()) {
nextMap->insert_or_assign(tag, std::move(propsSnapshot));
} else {
const auto& currentSnapshot = currentIt->second;
auto merged = std::make_shared<PropsSnapshot>();
if (currentSnapshot->rawProps) {
mergeAnimatedRawProps(
*currentSnapshot->rawProps, *propsSnapshot->rawProps);
merged->rawProps =
std::make_unique<folly::dynamic>(*currentSnapshot->rawProps);
}
if (propsSnapshot->rawProps) {
if (merged->rawProps) {
mergeAnimatedRawProps(*merged->rawProps, *propsSnapshot->rawProps);
} else {
merged->rawProps = std::move(propsSnapshot->rawProps);
}
}
if (propsSnapshot->text) {
merged->text = std::move(propsSnapshot->text);
} else {
currentSnapshot->rawProps = std::move(propsSnapshot->rawProps);
merged->text = currentSnapshot->text;
}
}
if (propsSnapshot->text) {
currentSnapshot->text = std::move(propsSnapshot->text);
}
if (!currentSnapshot->props) {
currentSnapshot->props = std::move(propsSnapshot->props);
currentSnapshot->propNames = std::move(propsSnapshot->propNames);
} else {
for (auto& propName : propsSnapshot->propNames) {
currentSnapshot->propNames.insert(propName);
updateProp(propName, *currentSnapshot->props, *propsSnapshot);
if (!currentSnapshot->props) {
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) {
merged->propNames.insert(propName);
updateProp(propName, *merged->props, *propsSnapshot);
}
}
currentIt->second = std::move(merged);
}
}
pendingMap.clear();
map = std::move(nextMap);
}
pendingMap.clear();
pendingFamilies.clear();

return {families, map};
}
Expand All @@ -163,8 +179,8 @@ void AnimatedPropsRegistry::clear(SurfaceId surfaceId) {
if (auto it = surfaceContexts_.find(surfaceId);
it != surfaceContexts_.end()) {
auto& surfaceContext = it->second;
surfaceContext.families.clear();
surfaceContext.map.clear();
surfaceContext.families.reset();
surfaceContext.map.reset();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ struct PropsSnapshot {
std::optional<std::string> text;
};

using FamilySet = std::unordered_set<std::shared_ptr<const ShadowNodeFamily>>;
using PendingSnapshotMap = std::unordered_map<Tag, std::shared_ptr<PropsSnapshot>>;
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_set<std::shared_ptr<const ShadowNodeFamily>> pendingFamilies, families;
PendingSnapshotMap pendingMap;
std::shared_ptr<const SnapshotMap> map;
FamilySet pendingFamilies;
std::shared_ptr<const FamilySet> 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 +57,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::shared_ptr<const FamilySet>, std::shared_ptr<const 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 @@ -12,8 +12,14 @@
#include <folly/dynamic.h>
#include "AnimatedProps.h"

namespace facebook::react {
struct PropsSnapshot;
} // namespace facebook::react

namespace facebook::react::animationbackend {

folly::dynamic packAnimatedProps(const AnimatedProps &animatedProps);
folly::dynamic packAnimatedPropsForClone(const AnimatedProps &animatedProps);
folly::dynamic packPropsSnapshotForClone(const PropsSnapshot &snapshot);

} // namespace facebook::react::animationbackend
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,27 @@ static inline Props::Shared cloneProps(

PropsParserContext propsParserContext{
shadowNode.getSurfaceId(), *shadowNode.getContextContainer()};
Props::Shared newProps;
if (!animatedProps.props.empty()) {
return shadowNode.getComponentDescriptor().cloneProps(
propsParserContext,
shadowNode.getProps(),
RawProps(animationbackend::packAnimatedPropsForClone(animatedProps)));
}

if (animatedProps.rawProps) {
if (ReactNativeFeatureFlags::enableFabricCommitBranching()) {
newProps = shadowNode.getComponentDescriptor().cloneProps(
return shadowNode.getComponentDescriptor().cloneProps(
propsParserContext,
shadowNode.getProps(),
std::move(*animatedProps.rawProps));
} else {
newProps = shadowNode.getComponentDescriptor().cloneProps(
propsParserContext,
shadowNode.getProps(),
RawProps(*animatedProps.rawProps));
}
} else {
newProps = shadowNode.getComponentDescriptor().cloneProps(
propsParserContext, shadowNode.getProps(), {});
return shadowNode.getComponentDescriptor().cloneProps(
propsParserContext,
shadowNode.getProps(),
RawProps(*animatedProps.rawProps));
}

auto viewProps = std::const_pointer_cast<BaseViewProps>(
std::static_pointer_cast<const BaseViewProps>(newProps));
for (auto& animatedProp : animatedProps.props) {
cloneProp(*viewProps, *animatedProp);
}
return newProps;
return ShadowNodeFragment::propsPlaceholder();
}

// Combines two mutations of the same view from one frame.
Expand Down Expand Up @@ -107,7 +104,11 @@ AnimationBackend::AnimationBackend(
}
}

AnimationBackend::~AnimationBackend() = default;
AnimationBackend::~AnimationBackend() {
if (auto uiManager = uiManager_.lock()) {
uiManager->unregisterCommitHook(*commitHook_);
}
}

void AnimationBackend::unpackMutations(
AnimationMutations& mutations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ RootShadowNode::Unshared AnimationBackendCommitHook::shadowTreeWillCommit(
return newRootShadowNode;
}

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

if (surfaceFamilies.empty()) {
const auto res = animatedPropsRegistry_->getMap(shadowTree.getSurfaceId());
if (!res.first || res.first->empty() || !res.second) {
return newRootShadowNode;
}
const auto& surfaceFamilies = *res.first;
const auto& updates = *res.second;
auto clonedRootShadowNode =
std::static_pointer_cast<RootShadowNode>(newRootShadowNode->cloneMultiple(
surfaceFamilies,
Expand All @@ -48,7 +47,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 All @@ -62,12 +61,16 @@ RootShadowNode::Unshared AnimationBackendCommitHook::shadowTreeWillCommit(
newProps = shadowNode.getComponentDescriptor().cloneProps(
propsParserContext, shadowNode.getProps(), {});
}
viewProps = std::const_pointer_cast<BaseViewProps>(
std::static_pointer_cast<const BaseViewProps>(newProps));
if (!snapshot->propNames.empty()) {
viewProps = std::const_pointer_cast<BaseViewProps>(
std::dynamic_pointer_cast<const BaseViewProps>(newProps));
}
}

for (const auto& propName : snapshot->propNames) {
updateProp(propName, *viewProps, *snapshot);
if (viewProps) {
for (const auto& propName : snapshot->propNames) {
updateProp(propName, *viewProps, *snapshot);
}
}

if (snapshot->text) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ target_link_libraries(react_renderer_animationbackend
react_renderer_mounting
react_renderer_uimanager
react_timing
yogacore
glog
folly_runtime
)
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,13 @@ 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() {
auto map = registry.getMap(1).second;
EXPECT_NE(map, nullptr);
lastSnapshot = map->at(10);
return *lastSnapshot;
}
};

Expand Down Expand Up @@ -216,7 +223,8 @@ TEST_F(AnimatedPropsRegistryTest, stoppedSurfacesIgnoreUpdates) {
registry.clearOnSurfaceStop(1);
updateRaw(folly::dynamic::object("opacity", 0.5));
registry.initializeSurface(1);
EXPECT_TRUE(registry.getMap(1).second.empty());
auto map = registry.getMap(1).second;
EXPECT_TRUE(!map || map->empty());
}

TEST_F(AnimatedPropsRegistryTest, keepsLatestText) {
Expand Down Expand Up @@ -269,4 +277,87 @@ 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);
if (!families || !map) {
continue;
}
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
Loading
Loading