mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Use dynamic for updates in Animated with Animation Backend (#54872)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/54872 For the testing purposes, during the development of Animation Backend, we changed the way props are modified from animated, to use the AnimatedProps. As these would currently not benefit c++ Animated, this diff reverts that to use `folly::dynamic` again. # Changelog [General] [Changed] - C++ animated now passes the calculated `folly::dynamic` directly to the backend, without repacking it into `AnimatedProp` Reviewed By: zeyap Differential Revision: D88743857 fbshipit-source-id: a9f715730e78f4278e95a2df0aa3b6d2628f1651
This commit is contained in:
1 parent
62c567f3cc
commit
84e408cf89
6 files changed
+83
-54
No files matched your search
+41
-28
@@ -1008,9 +1008,21 @@ AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations() {
|
||||
}
|
||||
|
||||
for (auto& [tag, props] : updateViewPropsDirect_) {
|
||||
propsBuilder.storeDynamic(props);
|
||||
mutations.push_back(
|
||||
AnimationMutation{tag, nullptr, propsBuilder.get()});
|
||||
auto familyIt = tagToShadowNodeFamily_.find(tag);
|
||||
if (familyIt == tagToShadowNodeFamily_.end()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
auto weakFamily = familyIt->second;
|
||||
if (auto family = weakFamily.lock()) {
|
||||
propsBuilder.storeDynamic(props);
|
||||
mutations.batch.push_back(
|
||||
AnimationMutation{
|
||||
.tag = tag,
|
||||
.family = family,
|
||||
.props = propsBuilder.get(),
|
||||
});
|
||||
}
|
||||
containsChange = true;
|
||||
}
|
||||
{
|
||||
@@ -1020,22 +1032,12 @@ AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations() {
|
||||
if (familyIt == tagToShadowNodeFamily_.end()) {
|
||||
continue;
|
||||
}
|
||||
if (auto family = familyIt->second.lock()) {
|
||||
// C++ Animated produces props in the form of a folly::dynamic, so
|
||||
// it wouldn't make sense to unpack it here. However, for the
|
||||
// purposes of testing, we want to be able to use the statically
|
||||
// typed AnimationMutation. At a later stage we will instead just
|
||||
// pass the dynamic directly to propsBuilder and the new API could
|
||||
// be used by 3rd party libraries or in the fututre by Animated.
|
||||
if (props.find("width") != props.items().end()) {
|
||||
propsBuilder.setWidth(
|
||||
yoga::Style::SizeLength::points(props["width"].asDouble()));
|
||||
}
|
||||
if (props.find("height") != props.items().end()) {
|
||||
propsBuilder.setHeight(
|
||||
yoga::Style::SizeLength::points(props["height"].asDouble()));
|
||||
}
|
||||
mutations.push_back(
|
||||
|
||||
auto weakFamily = familyIt->second;
|
||||
if (auto family = weakFamily.lock()) {
|
||||
propsBuilder.storeDynamic(props);
|
||||
mutations.hasLayoutUpdates = true;
|
||||
mutations.batch.push_back(
|
||||
AnimationMutation{
|
||||
.tag = tag,
|
||||
.family = family,
|
||||
@@ -1075,13 +1077,21 @@ AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations() {
|
||||
isEventAnimationInProgress_ = false;
|
||||
|
||||
for (auto& [tag, props] : updateViewPropsDirect_) {
|
||||
propsBuilder.storeDynamic(props);
|
||||
mutations.push_back(
|
||||
AnimationMutation{
|
||||
.tag = tag,
|
||||
.family = nullptr,
|
||||
.props = propsBuilder.get(),
|
||||
});
|
||||
auto familyIt = tagToShadowNodeFamily_.find(tag);
|
||||
if (familyIt == tagToShadowNodeFamily_.end()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
auto weakFamily = familyIt->second;
|
||||
if (auto family = weakFamily.lock()) {
|
||||
propsBuilder.storeDynamic(props);
|
||||
mutations.batch.push_back(
|
||||
AnimationMutation{
|
||||
.tag = tag,
|
||||
.family = family,
|
||||
.props = propsBuilder.get(),
|
||||
});
|
||||
}
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(tagToShadowNodeFamilyMutex_);
|
||||
@@ -1090,9 +1100,12 @@ AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations() {
|
||||
if (familyIt == tagToShadowNodeFamily_.end()) {
|
||||
continue;
|
||||
}
|
||||
if (auto family = familyIt->second.lock()) {
|
||||
|
||||
auto weakFamily = familyIt->second;
|
||||
if (auto family = weakFamily.lock()) {
|
||||
propsBuilder.storeDynamic(props);
|
||||
mutations.push_back(
|
||||
mutations.hasLayoutUpdates = true;
|
||||
mutations.batch.push_back(
|
||||
AnimationMutation{
|
||||
.tag = tag,
|
||||
.family = family,
|
||||
|
||||
+12
@@ -35,6 +35,18 @@ void AnimatedPropsRegistry::update(
|
||||
auto& snapshot = it->second;
|
||||
auto& viewProps = snapshot->props;
|
||||
|
||||
if (animatedProps.rawProps) {
|
||||
const auto& newRawProps = *animatedProps.rawProps;
|
||||
auto& currentRawProps = snapshot->rawProps;
|
||||
|
||||
if (currentRawProps) {
|
||||
auto newRawPropsDynamic = newRawProps.toDynamic();
|
||||
currentRawProps->merge_patch(newRawPropsDynamic);
|
||||
} else {
|
||||
currentRawProps =
|
||||
std::make_unique<folly::dynamic>(newRawProps.toDynamic());
|
||||
}
|
||||
}
|
||||
for (const auto& animatedProp : animatedProps.props) {
|
||||
snapshot->propNames.insert(animatedProp->propName);
|
||||
cloneProp(viewProps, *animatedProp);
|
||||
|
||||
+1
@@ -19,6 +19,7 @@ namespace facebook::react {
|
||||
struct PropsSnapshot {
|
||||
BaseViewProps props;
|
||||
std::unordered_set<PropName> propNames;
|
||||
std::unique_ptr<folly::dynamic> rawProps;
|
||||
};
|
||||
|
||||
struct SurfaceContext {
|
||||
|
||||
+15
-21
@@ -6,6 +6,7 @@
|
||||
*/
|
||||
|
||||
#include "AnimationBackend.h"
|
||||
#include <react/debug/react_native_assert.h>
|
||||
#include <react/renderer/animationbackend/AnimatedPropsSerializer.h>
|
||||
#include <react/renderer/graphics/Color.h>
|
||||
#include <chrono>
|
||||
@@ -60,17 +61,6 @@ static inline Props::Shared cloneProps(
|
||||
return newProps;
|
||||
}
|
||||
|
||||
static inline bool mutationHasLayoutUpdates(
|
||||
facebook::react::AnimationMutation& mutation) {
|
||||
for (auto& animatedProp : mutation.props.props) {
|
||||
// TODO: there should also be a check for the dynamic part
|
||||
if (layoutProps.contains(animatedProp->propName)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
AnimationBackend::AnimationBackend(
|
||||
StartOnRenderCallback&& startOnRenderCallback,
|
||||
StopOnRenderCallback&& stopOnRenderCallback,
|
||||
@@ -89,17 +79,19 @@ void AnimationBackend::onAnimationFrame(double timestamp) {
|
||||
std::unordered_map<Tag, AnimatedProps> synchronousUpdates;
|
||||
std::unordered_map<SurfaceId, SurfaceUpdates> surfaceUpdates;
|
||||
|
||||
bool hasAnyLayoutUpdates = false;
|
||||
for (auto& callback : callbacks) {
|
||||
auto muatations = callback(static_cast<float>(timestamp));
|
||||
for (auto& mutation : muatations) {
|
||||
hasAnyLayoutUpdates |= mutationHasLayoutUpdates(mutation);
|
||||
const auto family = mutation.family;
|
||||
if (family != nullptr) {
|
||||
if (muatations.hasLayoutUpdates) {
|
||||
for (auto& mutation : muatations.batch) {
|
||||
const auto family = mutation.family;
|
||||
react_native_assert(family != nullptr);
|
||||
|
||||
auto& [families, updates] = surfaceUpdates[family->getSurfaceId()];
|
||||
families.insert(family.get());
|
||||
updates[mutation.tag] = std::move(mutation.props);
|
||||
} else {
|
||||
}
|
||||
} else {
|
||||
for (auto& mutation : muatations.batch) {
|
||||
synchronousUpdates[mutation.tag] = std::move(mutation.props);
|
||||
}
|
||||
}
|
||||
@@ -107,9 +99,11 @@ void AnimationBackend::onAnimationFrame(double timestamp) {
|
||||
|
||||
animatedPropsRegistry_->update(surfaceUpdates);
|
||||
|
||||
if (hasAnyLayoutUpdates) {
|
||||
if (!surfaceUpdates.empty()) {
|
||||
commitUpdates(surfaceUpdates);
|
||||
} else {
|
||||
}
|
||||
|
||||
if (!synchronousUpdates.empty()) {
|
||||
synchronouslyUpdateProps(synchronousUpdates);
|
||||
}
|
||||
}
|
||||
@@ -175,8 +169,8 @@ void AnimationBackend::commitUpdates(
|
||||
void AnimationBackend::synchronouslyUpdateProps(
|
||||
const std::unordered_map<Tag, AnimatedProps>& updates) {
|
||||
for (auto& [tag, animatedProps] : updates) {
|
||||
// TODO: We shouldn't repack it into dynamic, but for that a rewrite of
|
||||
// directManipulationCallback_ is needed
|
||||
// TODO: We shouldn't repack it into dynamic, but for that a rewrite
|
||||
// of directManipulationCallback_ is needed
|
||||
auto dyn = animationbackend::packAnimatedProps(animatedProps);
|
||||
directManipulationCallback_(tag, std::move(dyn));
|
||||
}
|
||||
|
||||
+4
-1
@@ -38,7 +38,10 @@ struct AnimationMutation {
|
||||
AnimatedProps props;
|
||||
};
|
||||
|
||||
using AnimationMutations = std::vector<AnimationMutation>;
|
||||
struct AnimationMutations {
|
||||
std::vector<AnimationMutation> batch;
|
||||
bool hasLayoutUpdates{false};
|
||||
};
|
||||
|
||||
class AnimationBackend : public UIManagerAnimationBackend {
|
||||
public:
|
||||
|
||||
+10
-4
@@ -43,13 +43,19 @@ RootShadowNode::Unshared AnimationBackendCommitHook::shadowTreeWillCommit(
|
||||
if (surfaceFamilies.contains(&shadowNode.getFamily()) &&
|
||||
updates.contains(shadowNode.getTag())) {
|
||||
auto& snapshot = updates.at(shadowNode.getTag());
|
||||
if (!snapshot->propNames.empty()) {
|
||||
if (!snapshot->propNames.empty() || snapshot->rawProps) {
|
||||
PropsParserContext propsParserContext{
|
||||
shadowNode.getSurfaceId(),
|
||||
*shadowNode.getContextContainer()};
|
||||
|
||||
newProps = shadowNode.getComponentDescriptor().cloneProps(
|
||||
propsParserContext, shadowNode.getProps(), {});
|
||||
if (snapshot->rawProps) {
|
||||
newProps = shadowNode.getComponentDescriptor().cloneProps(
|
||||
propsParserContext,
|
||||
shadowNode.getProps(),
|
||||
RawProps(*snapshot->rawProps));
|
||||
} else {
|
||||
newProps = shadowNode.getComponentDescriptor().cloneProps(
|
||||
propsParserContext, shadowNode.getProps(), {});
|
||||
}
|
||||
viewProps = std::const_pointer_cast<BaseViewProps>(
|
||||
std::static_pointer_cast<const BaseViewProps>(newProps));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user