Adds a dawn native version of ProcessEvents that returns a bool.

- This is a backdoor that Chromium can use for now in their process
  events loop until we standardize/decide how this sort of thing
  should be handled in webgpu.h.

Change-Id: I3945ce1423329b61031648243406303c1abaf7df
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/165526
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Loko Kung <lokokung@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
diff --git a/include/dawn/native/DawnNative.h b/include/dawn/native/DawnNative.h
index 3590818..267b089 100644
--- a/include/dawn/native/DawnNative.h
+++ b/include/dawn/native/DawnNative.h
@@ -218,7 +218,7 @@
 
 DAWN_NATIVE_EXPORT bool DeviceTick(WGPUDevice device);
 
-DAWN_NATIVE_EXPORT void InstanceProcessEvents(WGPUInstance instance);
+DAWN_NATIVE_EXPORT bool InstanceProcessEvents(WGPUInstance instance);
 
 // ErrorInjector functions used for testing only. Defined in dawn_native/ErrorInjector.cpp
 DAWN_NATIVE_EXPORT void EnableErrorInjector();
diff --git a/src/dawn/native/DawnNative.cpp b/src/dawn/native/DawnNative.cpp
index 3826128..0587429 100644
--- a/src/dawn/native/DawnNative.cpp
+++ b/src/dawn/native/DawnNative.cpp
@@ -258,8 +258,8 @@
     return FromAPI(device)->APITick();
 }
 
-DAWN_NATIVE_EXPORT void InstanceProcessEvents(WGPUInstance instance) {
-    FromAPI(instance)->APIProcessEvents();
+DAWN_NATIVE_EXPORT bool InstanceProcessEvents(WGPUInstance instance) {
+    return FromAPI(instance)->ProcessEvents();
 }
 
 // ExternalImageDescriptor
diff --git a/src/dawn/native/EventManager.cpp b/src/dawn/native/EventManager.cpp
index aeaada8..939b686 100644
--- a/src/dawn/native/EventManager.cpp
+++ b/src/dawn/native/EventManager.cpp
@@ -301,7 +301,7 @@
     return futureID;
 }
 
-void EventManager::ProcessPollEvents() {
+bool EventManager::ProcessPollEvents() {
     DAWN_ASSERT(mEvents.has_value());
 
     std::vector<TrackedFutureWaitInfo> futures;
@@ -323,7 +323,7 @@
         std::lock_guard<std::mutex> lock(mProcessEventLock);
         wgpu::WaitStatus waitStatus = WaitImpl(futures, Nanoseconds(0));
         if (waitStatus == wgpu::WaitStatus::TimedOut) {
-            return;
+            return false;
         }
         DAWN_ASSERT(waitStatus == wgpu::WaitStatus::Success);
     }
@@ -343,6 +343,7 @@
     for (auto it = futures.begin(); it != readyEnd; ++it) {
         it->event->EnsureComplete(EventCompletionType::Ready);
     }
+    return readyEnd != futures.end();
 }
 
 wgpu::WaitStatus EventManager::WaitAny(size_t count, FutureWaitInfo* infos, Nanoseconds timeout) {
diff --git a/src/dawn/native/EventManager.h b/src/dawn/native/EventManager.h
index c0d4668..ada7d37 100644
--- a/src/dawn/native/EventManager.h
+++ b/src/dawn/native/EventManager.h
@@ -73,7 +73,7 @@
     class TrackedEvent;
     // Track a TrackedEvent and give it a FutureID.
     [[nodiscard]] FutureID TrackEvent(wgpu::CallbackMode mode, Ref<TrackedEvent>&&);
-    void ProcessPollEvents();
+    bool ProcessPollEvents();
     [[nodiscard]] wgpu::WaitStatus WaitAny(size_t count,
                                            FutureWaitInfo* infos,
                                            Nanoseconds timeout);
diff --git a/src/dawn/native/Instance.cpp b/src/dawn/native/Instance.cpp
index d35d3e1..1d8c865 100644
--- a/src/dawn/native/Instance.cpp
+++ b/src/dawn/native/Instance.cpp
@@ -539,7 +539,7 @@
     mDevicesList.Use([&](auto deviceList) { deviceList->erase(device); });
 }
 
-void InstanceBase::APIProcessEvents() {
+bool InstanceBase::ProcessEvents() {
     std::vector<Ref<DeviceBase>> devices;
     mDevicesList.Use([&](auto deviceList) {
         for (auto device : *deviceList) {
@@ -547,12 +547,18 @@
         }
     });
 
+    bool processedEvents = false;
     for (auto device : devices) {
-        device->APITick();
+        processedEvents |= device->APITick();
     }
 
     mCallbackTaskManager->Flush();
-    mEventManager.ProcessPollEvents();
+    processedEvents |= mEventManager.ProcessPollEvents();
+    return processedEvents;
+}
+
+void InstanceBase::APIProcessEvents() {
+    ProcessEvents();
 }
 
 wgpu::WaitStatus InstanceBase::APIWaitAny(size_t count,
diff --git a/src/dawn/native/Instance.h b/src/dawn/native/Instance.h
index 059e38a..b75f0ef 100644
--- a/src/dawn/native/Instance.h
+++ b/src/dawn/native/Instance.h
@@ -162,6 +162,10 @@
     const X11Functions* GetOrLoadX11Functions();
     const AHBFunctions* GetOrLoadAHBFunctions();
 
+    // TODO(dawn:752) Standardize webgpu.h to decide if we should return bool.
+    //   Currently this is a backdoor for Chromium's process event loop.
+    bool ProcessEvents();
+
     // Dawn API
     Surface* APICreateSurface(const SurfaceDescriptor* descriptor);
     void APIProcessEvents();