Skip to content

Commit b94e3a2

Browse files
fix: Fix for multiple surfaces (#8554)
## Summary This PR adds [patch](#8344 (comment)) from @bartlomiejbloniarz to Reanimated. Addresses #8344 ## Test plan
1 parent b42d2d9 commit b94e3a2

File tree

3 files changed

+56
-7
lines changed

3 files changed

+56
-7
lines changed

packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy.cpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ std::optional<MountingTransaction> LayoutAnimationsProxy::pullTransaction(
3232
auto lock = std::unique_lock<std::recursive_mutex>(mutex);
3333
PropsParserContext propsParserContext{surfaceId, *contextContainer_};
3434
ShadowViewMutationList filteredMutations;
35+
auto &[deadNodes] = surfaceContext_[surfaceId];
3536

3637
std::vector<std::shared_ptr<MutationNode>> roots;
3738
std::unordered_map<Tag, Tag> movedViews;
@@ -50,7 +51,9 @@ std::optional<MountingTransaction> LayoutAnimationsProxy::pullTransaction(
5051

5152
parseRemoveMutations(movedViews, mutations, roots);
5253

53-
handleRemovals(filteredMutations, roots);
54+
auto shouldAnimate = !surfacesToRemove_.contains(surfaceId);
55+
surfacesToRemove_.erase(surfaceId);
56+
handleRemovals(filteredMutations, roots, deadNodes, shouldAnimate);
5457

5558
handleUpdatesAndEnterings(filteredMutations, movedViews, mutations, propsParserContext, surfaceId);
5659

@@ -120,6 +123,7 @@ std::optional<SurfaceId> LayoutAnimationsProxy::endLayoutAnimation(int tag, bool
120123
auto node = nodeForTag_[tag];
121124
auto mutationNode = std::static_pointer_cast<MutationNode>(node);
122125
mutationNode->state = DEAD;
126+
auto &[deadNodes] = surfaceContext_[surfaceId];
123127
deadNodes.insert(mutationNode);
124128

125129
return surfaceId;
@@ -229,12 +233,14 @@ void LayoutAnimationsProxy::parseRemoveMutations(
229233

230234
void LayoutAnimationsProxy::handleRemovals(
231235
ShadowViewMutationList &filteredMutations,
232-
std::vector<std::shared_ptr<MutationNode>> &roots) const {
236+
std::vector<std::shared_ptr<MutationNode>> &roots,
237+
std::unordered_set<std::shared_ptr<MutationNode>> &deadNodes,
238+
bool shouldAnimate) const {
233239
// iterate from the end, so that children
234240
// with higher indices appear first in the mutations list
235241
for (auto it = roots.rbegin(); it != roots.rend(); it++) {
236242
auto &node = *it;
237-
if (!startAnimationsRecursively(node, true, true, false, filteredMutations)) {
243+
if (!startAnimationsRecursively(node, true, shouldAnimate, false, filteredMutations)) {
238244
filteredMutations.push_back(node->mutation);
239245
node->unflattenedParent->removeChildFromUnflattenedTree(node); //???
240246
if (node->state != MOVED) {
@@ -866,4 +872,23 @@ const ShadowNode *LayoutAnimationsProxy::findInShadowTreeByTag(const ShadowNode
866872
}
867873
#endif // ANDROID
868874

875+
// UIManagerAnimationDelegate
876+
877+
void LayoutAnimationsProxy::uiManagerDidConfigureNextLayoutAnimation(
878+
jsi::Runtime &runtime,
879+
const RawValue &config,
880+
const jsi::Value &successCallbackValue,
881+
const jsi::Value &failureCallbackValue) const {}
882+
883+
void LayoutAnimationsProxy::setComponentDescriptorRegistry(
884+
const SharedComponentDescriptorRegistry &componentDescriptorRegistry) {}
885+
886+
bool LayoutAnimationsProxy::shouldAnimateFrame() const {
887+
return false;
888+
}
889+
890+
void LayoutAnimationsProxy::stopSurface(SurfaceId surfaceId) {
891+
surfacesToRemove_.insert(surfaceId);
892+
}
893+
869894
} // namespace reanimated

packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy.h

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <react/renderer/mounting/MountingOverrideDelegate.h>
1111
#include <react/renderer/mounting/ShadowView.h>
1212
#include <react/renderer/scheduler/Scheduler.h>
13+
#include <react/renderer/uimanager/UIManagerAnimationDelegate.h>
1314
#include <react/renderer/uimanager/UIManagerBinding.h>
1415

1516
#include <memory>
@@ -33,14 +34,20 @@ struct LayoutAnimation {
3334
LayoutAnimation &operator=(const LayoutAnimation &other) = default;
3435
};
3536

37+
struct SurfaceContext {
38+
mutable std::unordered_set<std::shared_ptr<MutationNode>> deadNodes;
39+
};
40+
3641
struct LayoutAnimationsProxy : public MountingOverrideDelegate,
42+
public UIManagerAnimationDelegate,
3743
public std::enable_shared_from_this<LayoutAnimationsProxy> {
3844
mutable std::unordered_map<Tag, std::shared_ptr<Node>> nodeForTag_;
3945
mutable std::unordered_map<Tag, LayoutAnimation> layoutAnimations_;
4046
mutable std::recursive_mutex mutex;
4147
mutable SurfaceManager surfaceManager;
42-
mutable std::unordered_set<std::shared_ptr<MutationNode>> deadNodes;
48+
mutable std::unordered_map<SurfaceId, SurfaceContext> surfaceContext_;
4349
mutable std::unordered_map<Tag, int> leastRemoved;
50+
mutable std::unordered_set<SurfaceId> surfacesToRemove_;
4451
mutable std::vector<Tag> finishedAnimationTags_;
4552
std::shared_ptr<LayoutAnimationsManager> layoutAnimationsManager_;
4653
std::shared_ptr<const ContextContainer> contextContainer_;
@@ -93,8 +100,11 @@ struct LayoutAnimationsProxy : public MountingOverrideDelegate,
93100
std::unordered_map<Tag, Tag> &movedViews,
94101
ShadowViewMutationList &mutations,
95102
std::vector<std::shared_ptr<MutationNode>> &roots) const;
96-
void handleRemovals(ShadowViewMutationList &filteredMutations, std::vector<std::shared_ptr<MutationNode>> &roots)
97-
const;
103+
void handleRemovals(
104+
ShadowViewMutationList &filteredMutations,
105+
std::vector<std::shared_ptr<MutationNode>> &roots,
106+
std::unordered_set<std::shared_ptr<MutationNode>> &deadNodes,
107+
bool shouldAnimate) const;
98108

99109
void handleUpdatesAndEnterings(
100110
ShadowViewMutationList &filteredMutations,
@@ -143,6 +153,20 @@ struct LayoutAnimationsProxy : public MountingOverrideDelegate,
143153
MountingTransaction::Number number,
144154
const TransactionTelemetry &telemetry,
145155
ShadowViewMutationList mutations) const override;
156+
157+
// UIManagerAnimationDelegate
158+
159+
void uiManagerDidConfigureNextLayoutAnimation(
160+
jsi::Runtime &runtime,
161+
const RawValue &config,
162+
const jsi::Value &successCallbackValue,
163+
const jsi::Value &failureCallbackValue) const override;
164+
165+
void setComponentDescriptorRegistry(const SharedComponentDescriptorRegistry &componentDescriptorRegistry) override;
166+
167+
bool shouldAnimateFrame() const override;
168+
169+
void stopSurface(SurfaceId surfaceId) override;
146170
};
147171

148172
} // namespace reanimated

packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,6 @@ void ReanimatedModuleProxy::initializeFabric(const std::shared_ptr<UIManager> &u
12121212
}
12131213

12141214
void ReanimatedModuleProxy::initializeLayoutAnimationsProxy() {
1215-
uiManager_->setAnimationDelegate(nullptr);
12161215
auto scheduler = reinterpret_cast<Scheduler *>(uiManager_->getDelegate());
12171216
auto componentDescriptorRegistry =
12181217
scheduler->getContextContainer()
@@ -1233,6 +1232,7 @@ void ReanimatedModuleProxy::initializeLayoutAnimationsProxy() {
12331232
jsInvoker_
12341233
#endif
12351234
);
1235+
uiManager_->setAnimationDelegate(layoutAnimationsProxy_.get());
12361236
}
12371237
}
12381238

0 commit comments

Comments
 (0)