Add SurfaceGetPreferredFormat standard method

This is a standard method from webgpu.h that has been missing forever.
AFAIK Dawn in native context only supports BGRA8Unorm, so it is natural
to implement this method to always return this value.

This makes compatibility with other WebGPU backends easier, and gets
Dawn one small step closer to standard compliance (though I guess it'll
have to be reworked once other surface formats are available in native
mode).

Bug: dawn:1362
Change-Id: I2829d0091346fd19a28c16eba965140f85082239
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/167560
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Élie Michel <elie.michel.fr@gmail.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
diff --git a/src/dawn/dawn.json b/src/dawn/dawn.json
index f14beb9..d88fcfa 100644
--- a/src/dawn/dawn.json
+++ b/src/dawn/dawn.json
@@ -3241,7 +3241,6 @@
             {
                 "name": "get preferred format",
                 "returns": "texture format",
-                "tags": ["upstream", "emscripten"],
                 "args": [
                     {"name": "adapter", "type": "adapter"}
                 ]
diff --git a/src/dawn/dawn_wire.json b/src/dawn/dawn_wire.json
index 1539db6..48c931d 100644
--- a/src/dawn/dawn_wire.json
+++ b/src/dawn/dawn_wire.json
@@ -236,6 +236,7 @@
             "QueueOnSubmittedWorkDoneF",
             "QueueWriteBuffer",
             "QueueWriteTexture",
+            "SurfaceGetPreferredFormat",
             "TextureGetWidth",
             "TextureGetHeight",
             "TextureGetDepthOrArrayLayers",
@@ -265,6 +266,7 @@
             "QuerySet",
             "Queue",
             "ShaderModule",
+            "Surface",
             "SwapChain",
             "Texture"
         ],
diff --git a/src/dawn/native/Surface.cpp b/src/dawn/native/Surface.cpp
index 126aab3..8929dd6 100644
--- a/src/dawn/native/Surface.cpp
+++ b/src/dawn/native/Surface.cpp
@@ -348,4 +348,13 @@
     return mXWindow;
 }
 
+wgpu::TextureFormat Surface::APIGetPreferredFormat(AdapterBase* adapter) const {
+    // This is the only supported format in native mode (see crbug.com/dawn/160).
+#if DAWN_PLATFORM_IS(ANDROID)
+    return wgpu::TextureFormat::RGBA8Unorm;
+#else
+    return wgpu::TextureFormat::BGRA8Unorm;
+#endif  // !DAWN_PLATFORM_IS(ANDROID)
+}
+
 }  // namespace dawn::native
diff --git a/src/dawn/native/Surface.h b/src/dawn/native/Surface.h
index 48fed0b..71abec4 100644
--- a/src/dawn/native/Surface.h
+++ b/src/dawn/native/Surface.h
@@ -103,6 +103,9 @@
     void* GetXDisplay() const;
     uint32_t GetXWindow() const;
 
+    // Dawn API
+    wgpu::TextureFormat APIGetPreferredFormat(AdapterBase* adapter) const;
+
   private:
     Surface(InstanceBase* instance, ErrorMonad::ErrorTag tag);
     ~Surface() override;
diff --git a/src/dawn/wire/BUILD.gn b/src/dawn/wire/BUILD.gn
index e294cdb..8b8629d 100644
--- a/src/dawn/wire/BUILD.gn
+++ b/src/dawn/wire/BUILD.gn
@@ -131,6 +131,8 @@
     "client/RequestTracker.h",
     "client/ShaderModule.cpp",
     "client/ShaderModule.h",
+    "client/Surface.cpp",
+    "client/Surface.h",
     "client/SwapChain.cpp",
     "client/SwapChain.h",
     "client/Texture.cpp",
diff --git a/src/dawn/wire/CMakeLists.txt b/src/dawn/wire/CMakeLists.txt
index 22c81b8..854dd7a 100644
--- a/src/dawn/wire/CMakeLists.txt
+++ b/src/dawn/wire/CMakeLists.txt
@@ -91,6 +91,8 @@
     "client/RequestTracker.h"
     "client/ShaderModule.cpp"
     "client/ShaderModule.h"
+    "client/Surface.cpp"
+    "client/Surface.h"
     "client/SwapChain.cpp"
     "client/SwapChain.h"
     "client/Texture.cpp"
diff --git a/src/dawn/wire/client/ApiObjects.h b/src/dawn/wire/client/ApiObjects.h
index f5567cf..fdd57cd 100644
--- a/src/dawn/wire/client/ApiObjects.h
+++ b/src/dawn/wire/client/ApiObjects.h
@@ -37,6 +37,7 @@
 #include "dawn/wire/client/QuerySet.h"
 #include "dawn/wire/client/Queue.h"
 #include "dawn/wire/client/ShaderModule.h"
+#include "dawn/wire/client/Surface.h"
 #include "dawn/wire/client/SwapChain.h"
 #include "dawn/wire/client/Texture.h"
 
diff --git a/src/dawn/wire/client/Surface.cpp b/src/dawn/wire/client/Surface.cpp
new file mode 100644
index 0000000..43e22b9
--- /dev/null
+++ b/src/dawn/wire/client/Surface.cpp
@@ -0,0 +1,43 @@
+// Copyright 2024 The Dawn & Tint Authors
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its
+//    contributors may be used to endorse or promote products derived from
+//    this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "dawn/wire/client/Surface.h"
+
+#include "dawn/common/Platform.h"
+
+namespace dawn::wire::client {
+
+Surface::Surface(const ObjectBaseParams& params) : ObjectBase(params) {}
+
+Surface::~Surface() = default;
+
+WGPUTextureFormat Surface::GetPreferredFormat([[maybe_unused]] WGPUAdapter adapter) const {
+    // This is the only supported format in native mode (see crbug.com/dawn/160).
+    return WGPUTextureFormat_BGRA8Unorm;
+}
+
+}  // namespace dawn::wire::client
diff --git a/src/dawn/wire/client/Surface.h b/src/dawn/wire/client/Surface.h
new file mode 100644
index 0000000..06ced11
--- /dev/null
+++ b/src/dawn/wire/client/Surface.h
@@ -0,0 +1,49 @@
+// Copyright 2024 The Dawn & Tint Authors
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its
+//    contributors may be used to endorse or promote products derived from
+//    this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#ifndef SRC_DAWN_WIRE_CLIENT_SURFACE_H_
+#define SRC_DAWN_WIRE_CLIENT_SURFACE_H_
+
+#include "dawn/webgpu.h"
+
+#include "dawn/wire/client/ObjectBase.h"
+
+namespace dawn::wire::client {
+
+class Device;
+
+class Surface final : public ObjectBase {
+  public:
+    explicit Surface(const ObjectBaseParams& params);
+    ~Surface() override;
+
+    WGPUTextureFormat GetPreferredFormat(WGPUAdapter adapter) const;
+};
+
+}  // namespace dawn::wire::client
+
+#endif  // SRC_DAWN_WIRE_CLIENT_SURFACE_H_