Return wgpu::Status from SharedTexture/BufferMemory Begin/EndAccess

Note, there is a small behavior change where previously, you could
EndAccess and return "true" if the access was successfully ended, but
there was some other error exporting the fence. We could return a new
enum to differentiate these cases, but ultimately I decided it is not
worth it. If the fence failed to export, then it would be unsafe to
use the texture after anyway since you can't wait on it. All the error
cases are merged into one now.

Bug: 42241199
Change-Id: I19f2700bd00b9661bd37943072b9d149b956f55f
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/193282
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
Commit-Queue: Austin Eng <enga@chromium.org>
Reviewed-by: Loko Kung <lokokung@google.com>
diff --git a/src/dawn/dawn.json b/src/dawn/dawn.json
index 3f1bf5d..b161138 100644
--- a/src/dawn/dawn.json
+++ b/src/dawn/dawn.json
@@ -1974,7 +1974,7 @@
             },
             {
                 "name": "begin access",
-                "returns": "bool",
+                "returns": "status",
                 "args": [
                     {"name": "buffer", "type": "buffer"},
                     {"name": "descriptor", "type": "shared buffer memory begin access descriptor", "annotation": "const*"}
@@ -1982,7 +1982,7 @@
             },
             {
                 "name": "end access",
-                "returns": "bool",
+                "returns": "status",
                 "args": [
                     {"name": "buffer", "type": "buffer"},
                     {"name": "descriptor", "type": "shared buffer memory end access state", "annotation": "*"}
@@ -2039,7 +2039,7 @@
             },
             {
                 "name": "begin access",
-                "returns": "bool",
+                "returns": "status",
                 "args": [
                     {"name": "texture", "type": "texture"},
                     {"name": "descriptor", "type": "shared texture memory begin access descriptor", "annotation": "const*"}
@@ -2047,7 +2047,7 @@
             },
             {
                 "name": "end access",
-                "returns": "bool",
+                "returns": "status",
                 "args": [
                     {"name": "texture", "type": "texture"},
                     {"name": "descriptor", "type": "shared texture memory end access state", "annotation": "*"}
diff --git a/src/dawn/native/SharedResourceMemory.cpp b/src/dawn/native/SharedResourceMemory.cpp
index e4ee6f9..73c08d7 100644
--- a/src/dawn/native/SharedResourceMemory.cpp
+++ b/src/dawn/native/SharedResourceMemory.cpp
@@ -90,18 +90,22 @@
     return {};
 }
 
-bool SharedResourceMemory::APIBeginAccess(
+wgpu::Status SharedResourceMemory::APIBeginAccess(
     TextureBase* texture,
     const SharedTextureMemoryBeginAccessDescriptor* descriptor) {
-    return !GetDevice()->ConsumedError(BeginAccess(texture, descriptor),
-                                       "calling %s.BeginAccess(%s).", this, texture);
+    return GetDevice()->ConsumedError(BeginAccess(texture, descriptor),
+                                      "calling %s.BeginAccess(%s).", this, texture)
+               ? wgpu::Status::Error
+               : wgpu::Status::Success;
 }
 
-bool SharedResourceMemory::APIBeginAccess(
+wgpu::Status SharedResourceMemory::APIBeginAccess(
     BufferBase* buffer,
     const SharedBufferMemoryBeginAccessDescriptor* descriptor) {
-    return !GetDevice()->ConsumedError(BeginAccess(buffer, descriptor),
-                                       "calling %s.BeginAccess(%s).", this, buffer);
+    return GetDevice()->ConsumedError(BeginAccess(buffer, descriptor),
+                                      "calling %s.BeginAccess(%s).", this, buffer)
+               ? wgpu::Status::Error
+               : wgpu::Status::Success;
 }
 
 template <typename Resource, typename BeginAccessDescriptor>
@@ -184,20 +188,20 @@
     return {};
 }
 
-bool SharedResourceMemory::APIEndAccess(TextureBase* texture,
-                                        SharedTextureMemoryEndAccessState* state) {
-    bool didEnd = false;
-    [[maybe_unused]] bool hadError = GetDevice()->ConsumedError(
-        EndAccess(texture, state, &didEnd), "calling %s.EndAccess(%s).", this, texture);
-    return didEnd;
+wgpu::Status SharedResourceMemory::APIEndAccess(TextureBase* texture,
+                                                SharedTextureMemoryEndAccessState* state) {
+    return GetDevice()->ConsumedError(EndAccess(texture, state), "calling %s.EndAccess(%s).", this,
+                                      texture)
+               ? wgpu::Status::Error
+               : wgpu::Status::Success;
 }
 
-bool SharedResourceMemory::APIEndAccess(BufferBase* buffer,
-                                        SharedBufferMemoryEndAccessState* state) {
-    bool didEnd = false;
-    [[maybe_unused]] bool hadError = GetDevice()->ConsumedError(
-        EndAccess(buffer, state, &didEnd), "calling %s.EndAccess(%s).", this, buffer);
-    return didEnd;
+wgpu::Status SharedResourceMemory::APIEndAccess(BufferBase* buffer,
+                                                SharedBufferMemoryEndAccessState* state) {
+    return GetDevice()->ConsumedError(EndAccess(buffer, state), "calling %s.EndAccess(%s).", this,
+                                      buffer)
+               ? wgpu::Status::Error
+               : wgpu::Status::Success;
 }
 
 MaybeError SharedResourceMemory::BeginAccessImpl(
@@ -231,9 +235,7 @@
 }
 
 template <typename Resource, typename EndAccessState>
-MaybeError SharedResourceMemory::EndAccess(Resource* resource,
-                                           EndAccessState* state,
-                                           bool* didEnd) {
+MaybeError SharedResourceMemory::EndAccess(Resource* resource, EndAccessState* state) {
     DAWN_TRY(GetDevice()->ValidateObject(resource));
     DAWN_TRY(ValidateResourceCreatedFromSelf(resource));
 
@@ -282,8 +284,6 @@
     DAWN_ASSERT(!resource->IsError());
     ExecutionSerial lastUsageSerial = resource->OnEndAccess();
 
-    *didEnd = true;
-
     // If the last usage serial is non-zero, the texture was used.
     // Call the error-generating part of the EndAccess implementation to export a fence.
     // This is separated out because writing the output state must happen regardless of whether
diff --git a/src/dawn/native/SharedResourceMemory.h b/src/dawn/native/SharedResourceMemory.h
index a5a9956..c7b908a 100644
--- a/src/dawn/native/SharedResourceMemory.h
+++ b/src/dawn/native/SharedResourceMemory.h
@@ -77,18 +77,18 @@
     // Returns true if access was acquired. If it returns true, then APIEndAccess must
     // be called to release access. Other errors may occur even if `true` is returned.
     // Use an error scope to catch them.
-    bool APIBeginAccess(TextureBase* texture,
-                        const SharedTextureMemoryBeginAccessDescriptor* descriptor);
+    wgpu::Status APIBeginAccess(TextureBase* texture,
+                                const SharedTextureMemoryBeginAccessDescriptor* descriptor);
     // Returns true if access was released.
-    bool APIEndAccess(TextureBase* texture, SharedTextureMemoryEndAccessState* state);
+    wgpu::Status APIEndAccess(TextureBase* texture, SharedTextureMemoryEndAccessState* state);
 
     // Returns true if access was acquired. If it returns true, then APIEndAccess must
     // be called to release access. Other errors may occur even if `true` is returned.
     // Use an error scope to catch them.
-    bool APIBeginAccess(BufferBase* buffer,
-                        const SharedBufferMemoryBeginAccessDescriptor* descriptor);
+    wgpu::Status APIBeginAccess(BufferBase* buffer,
+                                const SharedBufferMemoryBeginAccessDescriptor* descriptor);
     // Returns true if access was released.
-    bool APIEndAccess(BufferBase* buffer, SharedBufferMemoryEndAccessState* state);
+    wgpu::Status APIEndAccess(BufferBase* buffer, SharedBufferMemoryEndAccessState* state);
 
     // Returns true iff the device passed to this object on creation is now lost.
     // TODO(crbug.com/1506468): Eliminate this API once Chromium has been
@@ -111,7 +111,7 @@
     MaybeError BeginAccess(Resource* resource, const BeginAccessDescriptor* rawDescriptor);
 
     template <typename Resource, typename EndAccessState>
-    MaybeError EndAccess(Resource* resource, EndAccessState* state, bool* didEnd);
+    MaybeError EndAccess(Resource* resource, EndAccessState* state);
 
     template <typename Resource, typename EndAccessState>
     ResultOrError<FenceAndSignalValue> EndAccessInternal(ExecutionSerial lastUsageSerial,