Synchronize replaceRegion calls.

In the Discussion part of
https://developer.apple.com/documentation/metal/mtltexture/1515679-replaceregion
it seems like we should sync the calls to replaceRegion (inside
setupTexturesForFrame) in RTCMTLRenderer and not just the command
buffer.

This is a speculative fix for the linked bug, but we don't have any
clear repro case. Have done basic testing in AppRTCMobile and don't
see any obvious regressions, so might be worth trying.

Bug: webrtc:10024
Change-Id: Id6848691129fba8845f38c3dfe0ba53b9e5a27ce
Reviewed-on: https://webrtc-review.googlesource.com/c/123766
Reviewed-by: Kári Helgason <kthelgason@webrtc.org>
Commit-Queue: Anders Carlsson <andersc@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26819}
diff --git a/sdk/objc/components/renderer/metal/RTCMTLRenderer.mm b/sdk/objc/components/renderer/metal/RTCMTLRenderer.mm
index fb478d2..63cf225 100644
--- a/sdk/objc/components/renderer/metal/RTCMTLRenderer.mm
+++ b/sdk/objc/components/renderer/metal/RTCMTLRenderer.mm
@@ -274,10 +274,6 @@
 }
 
 - (void)render {
-  // Wait until the inflight (curently sent to GPU) command buffer
-  // has completed the GPU work.
-  dispatch_semaphore_wait(_inflight_semaphore, DISPATCH_TIME_FOREVER);
-
   id<MTLCommandBuffer> commandBuffer = [_commandQueue commandBuffer];
   commandBuffer.label = commandBufferLabel;
 
@@ -317,8 +313,14 @@
 
 - (void)drawFrame:(RTCVideoFrame *)frame {
   @autoreleasepool {
+    // Wait until the inflight (curently sent to GPU) command buffer
+    // has completed the GPU work.
+    dispatch_semaphore_wait(_inflight_semaphore, DISPATCH_TIME_FOREVER);
+
     if ([self setupTexturesForFrame:frame]) {
       [self render];
+    } else {
+      dispatch_semaphore_signal(_inflight_semaphore);
     }
   }
 }