Add SharedTextureMemory::IsDeviceLost()

This API adds an IsDeviceLost() API to SharedTextureMemory. This API
will be used within Chromium to determine when to drop cached per-Device
SharedTextureMemory instances. Once crbug.com/1506468 is fixed, there
will no longer be any need for this API and we will remove it.

Change-Id: Ifdf78f8cc9e8936ecfe023efcfd45656afa13bad
Bug: chromium:1493854
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/163740
Reviewed-by: Austin Eng <enga@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Colin Blundell <blundell@chromium.org>
diff --git a/dawn.json b/dawn.json
index f4af0e0..58b83c0 100644
--- a/dawn.json
+++ b/dawn.json
@@ -1589,6 +1589,11 @@
                     {"name": "texture", "type": "texture"},
                     {"name": "descriptor", "type": "shared texture memory end access state", "annotation": "*"}
                 ]
+            },
+            {
+                "name": "is device lost",
+                "returns": "bool",
+                "args": []
             }
         ]
     },
diff --git a/src/dawn/native/SharedTextureMemory.cpp b/src/dawn/native/SharedTextureMemory.cpp
index c45fb13..aff1319 100644
--- a/src/dawn/native/SharedTextureMemory.cpp
+++ b/src/dawn/native/SharedTextureMemory.cpp
@@ -223,6 +223,10 @@
     return didBegin;
 }
 
+bool SharedTextureMemoryBase::APIIsDeviceLost() {
+    return GetDevice()->IsLost();
+}
+
 MaybeError SharedTextureMemoryBase::BeginAccess(TextureBase* texture,
                                                 const BeginAccessDescriptor* descriptor) {
     // Append begin fences first. Fences should be tracked regardless of whether later errors occur.
diff --git a/src/dawn/native/SharedTextureMemory.h b/src/dawn/native/SharedTextureMemory.h
index 674456e..f16b114 100644
--- a/src/dawn/native/SharedTextureMemory.h
+++ b/src/dawn/native/SharedTextureMemory.h
@@ -69,6 +69,10 @@
     bool APIBeginAccess(TextureBase* texture, const BeginAccessDescriptor* descriptor);
     // Returns true if access was released.
     bool APIEndAccess(TextureBase* texture, EndAccessState* 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
+    // transitioned away from using it in favor of observing device lost events.
+    bool APIIsDeviceLost();
 
     ObjectType GetType() const override;
 
diff --git a/src/dawn/tests/end2end/SharedTextureMemoryTests.cpp b/src/dawn/tests/end2end/SharedTextureMemoryTests.cpp
index 77dbf8e..be1ed54 100644
--- a/src/dawn/tests/end2end/SharedTextureMemoryTests.cpp
+++ b/src/dawn/tests/end2end/SharedTextureMemoryTests.cpp
@@ -405,6 +405,26 @@
         HasSubstr("lost"));
 }
 
+// Test that SharedTextureMemory::IsDeviceLost() returns the expected value before and
+// after destroying the device.
+TEST_P(SharedTextureMemoryTests, CheckIsDeviceLostBeforeAndAfterDestroyingDevice) {
+    wgpu::SharedTextureMemory memory = GetParam().mBackend->CreateSharedTextureMemory(device);
+
+    EXPECT_FALSE(memory.IsDeviceLost());
+    device.Destroy();
+    EXPECT_TRUE(memory.IsDeviceLost());
+}
+
+// Test that SharedTextureMemory::IsDeviceLost() returns the expected value before and
+// after losing the device.
+TEST_P(SharedTextureMemoryTests, CheckIsDeviceLostBeforeAndAfterLosingDevice) {
+    wgpu::SharedTextureMemory memory = GetParam().mBackend->CreateSharedTextureMemory(device);
+
+    EXPECT_FALSE(memory.IsDeviceLost());
+    LoseDeviceForTesting(device);
+    EXPECT_TRUE(memory.IsDeviceLost());
+}
+
 // Test that it is an error to import a shared fence when the device is destroyed
 TEST_P(SharedTextureMemoryTests, ImportSharedFenceDeviceDestroyed) {
     device.Destroy();