Skip to content

Commit a66f445

Browse files
rubennortefacebook-github-bot
authored andcommitted
Improve logic to report mounted surfaces (#43337)
Summary: Pull Request resolved: #43337 Changelog: [internal] Mount instructions and listeners are only called from the UI thread, so there's no need to have synchronization mechanisms for concurrency. We're also scheduling mount hooks notifications once, but subsequent calls are ignored instead of accumulated to be notified together. This also changes that to collect all the surface IDs in the array that is read on notification. Reviewed By: sammy-SC Differential Revision: D54547194 fbshipit-source-id: a861e3b0113914aae5325c1486bcf8acd50eef79
1 parent 0286151 commit a66f445

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java

+21-20
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@
9191
import java.util.Map;
9292
import java.util.Queue;
9393
import java.util.concurrent.CopyOnWriteArrayList;
94-
import java.util.concurrent.atomic.AtomicBoolean;
9594

9695
/**
9796
* We instruct ProGuard not to strip out any fields or methods, because many of these methods are
@@ -173,7 +172,8 @@ public class FabricUIManager implements UIManager, LifecycleEventListener {
173172
@NonNull
174173
private final CopyOnWriteArrayList<UIManagerListener> mListeners = new CopyOnWriteArrayList<>();
175174

176-
@NonNull private final AtomicBoolean mMountNotificationScheduled = new AtomicBoolean(false);
175+
private boolean mMountNotificationScheduled = false;
176+
private final List<Integer> mMountedSurfaceIds = new ArrayList<>();
177177

178178
@ThreadConfined(UI)
179179
@NonNull
@@ -1203,54 +1203,55 @@ public Map<String, Long> getPerformanceCounters() {
12031203
}
12041204

12051205
private class MountItemDispatchListener implements MountItemDispatcher.ItemDispatchListener {
1206+
@UiThread
1207+
@ThreadConfined(UI)
12061208
@Override
12071209
public void willMountItems(@Nullable List<MountItem> mountItems) {
12081210
for (UIManagerListener listener : mListeners) {
12091211
listener.willMountItems(FabricUIManager.this);
12101212
}
12111213
}
12121214

1215+
@UiThread
1216+
@ThreadConfined(UI)
12131217
@Override
12141218
public void didMountItems(@Nullable List<MountItem> mountItems) {
12151219
for (UIManagerListener listener : mListeners) {
12161220
listener.didMountItems(FabricUIManager.this);
12171221
}
12181222

1219-
if (!ReactFeatureFlags.enableMountHooks) {
1223+
if (!ReactFeatureFlags.enableMountHooks || mountItems == null || mountItems.isEmpty()) {
12201224
return;
12211225
}
12221226

1223-
boolean mountNotificationScheduled = mMountNotificationScheduled.getAndSet(true);
1224-
if (!mountNotificationScheduled) {
1227+
// Collect surface IDs for all the mount items
1228+
for (MountItem mountItem : mountItems) {
1229+
if (mountItem != null && !mMountedSurfaceIds.contains(mountItem.getSurfaceId())) {
1230+
mMountedSurfaceIds.add(mountItem.getSurfaceId());
1231+
}
1232+
}
1233+
1234+
if (!mMountNotificationScheduled && !mMountedSurfaceIds.isEmpty()) {
12251235
// Notify mount when the effects are visible and prevent mount hooks to
12261236
// delay paint.
12271237
UiThreadUtil.getUiThreadHandler()
12281238
.postAtFrontOfQueue(
12291239
new Runnable() {
12301240
@Override
12311241
public void run() {
1232-
mMountNotificationScheduled.set(false);
1233-
1234-
if (mDestroyed) {
1235-
return;
1236-
}
1242+
mMountNotificationScheduled = false;
12371243

12381244
final @Nullable Binding binding = mBinding;
1239-
if (mountItems == null || binding == null) {
1245+
if (binding == null || mDestroyed) {
1246+
mMountedSurfaceIds.clear();
12401247
return;
12411248
}
12421249

1243-
// Collect surface IDs for all the mount items
1244-
List<Integer> surfaceIds = new ArrayList<>();
1245-
for (MountItem mountItem : mountItems) {
1246-
if (mountItem != null && !surfaceIds.contains(mountItem.getSurfaceId())) {
1247-
surfaceIds.add(mountItem.getSurfaceId());
1248-
}
1249-
}
1250-
1251-
for (int surfaceId : surfaceIds) {
1250+
for (int surfaceId : mMountedSurfaceIds) {
12521251
binding.reportMount(surfaceId);
12531252
}
1253+
1254+
mMountedSurfaceIds.clear();
12541255
}
12551256
});
12561257
}

0 commit comments

Comments
 (0)