Replace instead of queueing render updates.

Bug: webrtc:351858995
Change-Id: I6c07d71afeae886ff6a20509bca5b5c65f131e41
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/356800
Reviewed-by: Fabian Bergmark <fabianbergmark@google.com>
Reviewed-by: Zoé Lepaul <xalep@webrtc.org>
Commit-Queue: Ranveer Aggarwal‎ <ranvr@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#42617}
diff --git a/sdk/android/api/org/webrtc/EglThread.java b/sdk/android/api/org/webrtc/EglThread.java
index 73323d5..ed5e017 100644
--- a/sdk/android/api/org/webrtc/EglThread.java
+++ b/sdk/android/api/org/webrtc/EglThread.java
@@ -18,6 +18,7 @@
 import androidx.annotation.Nullable;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Optional;
 import org.webrtc.EglBase.EglConnection;
 
 /** EGL graphics thread that allows multiple clients to share the same underlying EGLContext. */
@@ -122,7 +123,7 @@
   private final HandlerWithExceptionCallbacks handler;
   private final EglConnection eglConnection;
   private final RenderSynchronizer renderSynchronizer;
-  private final List<RenderUpdate> pendingRenderUpdates = new ArrayList<>();
+  private Optional<RenderUpdate> pendingRenderUpdate = Optional.empty();
   private boolean renderWindowOpen = true;
 
   private EglThread(
@@ -193,7 +194,7 @@
     if (renderWindowOpen) {
       update.update(/* runsInline = */true);
     } else {
-      pendingRenderUpdates.add(update);
+      pendingRenderUpdate = Optional.of(update);
     }
   }
 
@@ -202,10 +203,9 @@
     handler.post(
         () -> {
           renderWindowOpen = true;
-          for (RenderUpdate update : pendingRenderUpdates) {
-            update.update(/* runsInline = */false);
-          }
-          pendingRenderUpdates.clear();
+          pendingRenderUpdate.ifPresent(
+              renderUpdate -> renderUpdate.update(/* runsInline = */ false));
+          pendingRenderUpdate = Optional.empty();
         });
   }