Workaround ANGLE hang with a glFlush().

Change-Id: Idce1b71bdb132a1b96836d270eb2ab53fd0086f8
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/36700
Commit-Queue: Stephen White <senorblanco@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
diff --git a/src/dawn_native/Toggles.cpp b/src/dawn_native/Toggles.cpp
index bc4d55b..034eabf 100644
--- a/src/dawn_native/Toggles.cpp
+++ b/src/dawn_native/Toggles.cpp
@@ -152,6 +152,10 @@
             {Toggle::UseTintInspector,
              {"use_tint_inspector", "Use Tint instead of SPRIV-cross for shader reflection.",
               "https://crbug.com/dawn/578"}},
+            {Toggle::FlushBeforeClientWaitSync,
+             {"flush_before_client_wait_sync",
+              "Call glFlush before glClientWaitSync to work around bugs in the latter",
+              "https://crbug.com/dawn/633"}},
             // Dummy comment to separate the }} so it is clearer what to copy-paste to add a toggle.
         }};
 
diff --git a/src/dawn_native/Toggles.h b/src/dawn_native/Toggles.h
index d2f7060..2ecf5e0 100644
--- a/src/dawn_native/Toggles.h
+++ b/src/dawn_native/Toggles.h
@@ -47,6 +47,7 @@
         DisallowUnsafeAPIs,
         UseTintGenerator,
         UseTintInspector,
+        FlushBeforeClientWaitSync,
 
         EnumCount,
         InvalidEnum = EnumCount,
diff --git a/src/dawn_native/opengl/DeviceGL.cpp b/src/dawn_native/opengl/DeviceGL.cpp
index 1d81932..bc64d45 100644
--- a/src/dawn_native/opengl/DeviceGL.cpp
+++ b/src/dawn_native/opengl/DeviceGL.cpp
@@ -86,6 +86,7 @@
         SetToggle(Toggle::DisableBaseVertex, !supportsBaseVertex);
         SetToggle(Toggle::DisableBaseInstance, !supportsBaseInstance);
         SetToggle(Toggle::DisableIndexedDrawBuffers, !supportsIndexedDrawBuffers);
+        SetToggle(Toggle::FlushBeforeClientWaitSync, gl.GetVersion().IsES());
     }
 
     const GLFormat& Device::GetGLFormat(const Format& format) {
@@ -173,6 +174,11 @@
 
             // Fence are added in order, so we can stop searching as soon
             // as we see one that's not ready.
+
+            // TODO(crbug.com/dawn/633): Remove this workaround after the deadlock issue is fixed.
+            if (IsToggleEnabled(Toggle::FlushBeforeClientWaitSync)) {
+                gl.Flush();
+            }
             GLenum result = gl.ClientWaitSync(sync, GL_SYNC_FLUSH_COMMANDS_BIT, 0);
             if (result == GL_TIMEOUT_EXPIRED) {
                 return fenceSerial;