Simplify wire to have less custom object creation

use std::is_constructible_v to decide when objects should be passed
more args instead of writing it by hand each time

Change-Id: I82b1f50464996f54ed2e35bfff4c08ea4078947c
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/138841
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Loko Kung <lokokung@google.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Austin Eng <enga@chromium.org>
diff --git a/dawn_wire.json b/dawn_wire.json
index a22d133..5b78ea5 100644
--- a/dawn_wire.json
+++ b/dawn_wire.json
@@ -222,11 +222,6 @@
             "AdapterGetInstance",
             "BufferDestroy",
             "BufferUnmap",
-            "DeviceCreateErrorBuffer",
-            "DeviceCreateQuerySet",
-            "DeviceCreateSwapChain",
-            "DeviceCreateTexture",
-            "DeviceCreateErrorTexture",
             "DeviceGetAdapter",
             "DeviceGetQueue",
             "DeviceGetSupportedSurfaceUsage",
diff --git a/generator/templates/dawn/wire/client/ApiProcs.cpp b/generator/templates/dawn/wire/client/ApiProcs.cpp
index 9c8656d..a5220f6 100644
--- a/generator/templates/dawn/wire/client/ApiProcs.cpp
+++ b/generator/templates/dawn/wire/client/ApiProcs.cpp
@@ -58,7 +58,23 @@
 
                     //* For object creation, store the object ID the client will use for the result.
                     {% if method.return_type.category == "object" %}
-                        auto* returnObject = self->GetClient()->Make<{{method.return_type.name.CamelCase()}}>();
+                        {% set ReturnObj = method.return_type.name.CamelCase() %}
+
+                        {{ReturnObj}}* returnObject;
+                        if constexpr (std::is_constructible_v<
+                            {{- ReturnObj}}, const ObjectBaseParams&
+                            {%- for arg in method.arguments -%}
+                                , decltype({{as_varName(arg.name)}})
+                            {%- endfor -%}
+                        >) {
+                            returnObject = self->GetClient()->Make<{{ReturnObj}}>(
+                                {%- for arg in method.arguments -%}
+                                    {% if not loop.first %}, {% endif %}{{as_varName(arg.name)}}
+                                {%- endfor -%}
+                            );
+                        } else {
+                            returnObject = self->GetClient()->Make<{{ReturnObj}}>();
+                        }
                         cmd.result = returnObject->GetWireHandle();
                     {% endif %}
 
diff --git a/src/dawn/wire/client/Buffer.cpp b/src/dawn/wire/client/Buffer.cpp
index add9f96..c5492e2 100644
--- a/src/dawn/wire/client/Buffer.cpp
+++ b/src/dawn/wire/client/Buffer.cpp
@@ -34,7 +34,7 @@
     errorInfo.chain.sType = WGPUSType_DawnBufferDescriptorErrorInfoFromWireClient;
     errorInfo.outOfMemory = true;
     errorBufferDescriptor.nextInChain = &errorInfo.chain;
-    return device->CreateErrorBuffer(&errorBufferDescriptor);
+    return GetProcs().deviceCreateErrorBuffer(ToAPI(device), &errorBufferDescriptor);
 }
 }  // anonymous namespace
 
@@ -87,9 +87,9 @@
     }
 
     // Create the buffer and send the creation command.
-    // This must happen after any potential device->CreateErrorBuffer()
+    // This must happen after any potential error buffer creation
     // as server expects allocating ids to be monotonically increasing
-    Buffer* buffer = wireClient->Make<Buffer>(device, descriptor);
+    Buffer* buffer = wireClient->Make<Buffer>(descriptor);
     buffer->mDestructWriteHandleOnUnmap = false;
 
     if (descriptor->mappedAtCreation) {
@@ -135,28 +135,10 @@
     return ToAPI(buffer);
 }
 
-// static
-WGPUBuffer Buffer::CreateError(Device* device, const WGPUBufferDescriptor* descriptor) {
-    Client* client = device->GetClient();
-    Buffer* buffer = client->Make<Buffer>(device, descriptor);
-
-    DeviceCreateErrorBufferCmd cmd;
-    cmd.self = ToAPI(device);
-    cmd.selfId = device->GetWireId();
-    cmd.descriptor = descriptor;
-    cmd.result = buffer->GetWireHandle();
-    client->SerializeCommand(cmd);
-
-    return ToAPI(buffer);
-}
-
-Buffer::Buffer(const ObjectBaseParams& params,
-               Device* device,
-               const WGPUBufferDescriptor* descriptor)
+Buffer::Buffer(const ObjectBaseParams& params, const WGPUBufferDescriptor* descriptor)
     : ObjectBase(params),
       mSize(descriptor->size),
-      mUsage(static_cast<WGPUBufferUsage>(descriptor->usage)),
-      mDeviceIsAlive(device->GetAliveWeakPtr()) {}
+      mUsage(static_cast<WGPUBufferUsage>(descriptor->usage)) {}
 
 Buffer::~Buffer() {
     FreeMappedData();
diff --git a/src/dawn/wire/client/Buffer.h b/src/dawn/wire/client/Buffer.h
index 72339b3..d5fc573 100644
--- a/src/dawn/wire/client/Buffer.h
+++ b/src/dawn/wire/client/Buffer.h
@@ -28,9 +28,8 @@
 class Buffer final : public ObjectBase {
   public:
     static WGPUBuffer Create(Device* device, const WGPUBufferDescriptor* descriptor);
-    static WGPUBuffer CreateError(Device* device, const WGPUBufferDescriptor* descriptor);
 
-    Buffer(const ObjectBaseParams& params, Device* device, const WGPUBufferDescriptor* descriptor);
+    Buffer(const ObjectBaseParams& params, const WGPUBufferDescriptor* descriptor);
     ~Buffer() override;
 
     bool OnMapAsyncCallback(uint64_t requestSerial,
@@ -98,8 +97,6 @@
     void* mMappedData = nullptr;
     size_t mMapOffset = 0;
     size_t mMapSize = 0;
-
-    std::weak_ptr<bool> mDeviceIsAlive;
 };
 
 }  // namespace dawn::wire::client
diff --git a/src/dawn/wire/client/Device.cpp b/src/dawn/wire/client/Device.cpp
index edc9a72..2e6362d 100644
--- a/src/dawn/wire/client/Device.cpp
+++ b/src/dawn/wire/client/Device.cpp
@@ -211,27 +211,6 @@
     return Buffer::Create(this, descriptor);
 }
 
-WGPUBuffer Device::CreateErrorBuffer(const WGPUBufferDescriptor* descriptor) {
-    return Buffer::CreateError(this, descriptor);
-}
-
-WGPUQuerySet Device::CreateQuerySet(const WGPUQuerySetDescriptor* descriptor) {
-    return QuerySet::Create(this, descriptor);
-}
-
-WGPUSwapChain Device::CreateSwapChain(WGPUSurface surface,
-                                      const WGPUSwapChainDescriptor* descriptor) {
-    return SwapChain::Create(this, surface, descriptor);
-}
-
-WGPUTexture Device::CreateTexture(const WGPUTextureDescriptor* descriptor) {
-    return Texture::Create(this, descriptor);
-}
-
-WGPUTexture Device::CreateErrorTexture(const WGPUTextureDescriptor* descriptor) {
-    return Texture::CreateError(this, descriptor);
-}
-
 WGPUAdapter Device::GetAdapter() {
     // Not implemented in the wire.
     UNREACHABLE();
diff --git a/src/dawn/wire/client/Device.h b/src/dawn/wire/client/Device.h
index e5354e1..1beab67 100644
--- a/src/dawn/wire/client/Device.h
+++ b/src/dawn/wire/client/Device.h
@@ -41,17 +41,12 @@
     void InjectError(WGPUErrorType type, const char* message);
     void PopErrorScope(WGPUErrorCallback callback, void* userdata);
     WGPUBuffer CreateBuffer(const WGPUBufferDescriptor* descriptor);
-    WGPUBuffer CreateErrorBuffer(const WGPUBufferDescriptor* descriptor);
     void CreateComputePipelineAsync(WGPUComputePipelineDescriptor const* descriptor,
                                     WGPUCreateComputePipelineAsyncCallback callback,
                                     void* userdata);
     void CreateRenderPipelineAsync(WGPURenderPipelineDescriptor const* descriptor,
                                    WGPUCreateRenderPipelineAsyncCallback callback,
                                    void* userdata);
-    WGPUQuerySet CreateQuerySet(const WGPUQuerySetDescriptor* descriptor);
-    WGPUSwapChain CreateSwapChain(WGPUSurface surface, const WGPUSwapChainDescriptor* descriptor);
-    WGPUTexture CreateTexture(const WGPUTextureDescriptor* descriptor);
-    WGPUTexture CreateErrorTexture(const WGPUTextureDescriptor* descriptor);
 
     void HandleError(WGPUErrorType errorType, const char* message);
     void HandleLogging(WGPULoggingType loggingType, const char* message);
diff --git a/src/dawn/wire/client/QuerySet.cpp b/src/dawn/wire/client/QuerySet.cpp
index 6ecbaa4..fd381fb 100644
--- a/src/dawn/wire/client/QuerySet.cpp
+++ b/src/dawn/wire/client/QuerySet.cpp
@@ -19,22 +19,6 @@
 
 namespace dawn::wire::client {
 
-// static
-WGPUQuerySet QuerySet::Create(Device* device, const WGPUQuerySetDescriptor* descriptor) {
-    Client* wireClient = device->GetClient();
-    QuerySet* querySet = wireClient->Make<QuerySet>(descriptor);
-
-    // Send the Device::CreateQuerySet command without modifications.
-    DeviceCreateQuerySetCmd cmd;
-    cmd.self = ToAPI(device);
-    cmd.selfId = device->GetWireId();
-    cmd.descriptor = descriptor;
-    cmd.result = querySet->GetWireHandle();
-    wireClient->SerializeCommand(cmd);
-
-    return ToAPI(querySet);
-}
-
 QuerySet::QuerySet(const ObjectBaseParams& params, const WGPUQuerySetDescriptor* descriptor)
     : ObjectBase(params), mType(descriptor->type), mCount(descriptor->count) {}
 
diff --git a/src/dawn/wire/client/QuerySet.h b/src/dawn/wire/client/QuerySet.h
index 84c8099..f88e11d 100644
--- a/src/dawn/wire/client/QuerySet.h
+++ b/src/dawn/wire/client/QuerySet.h
@@ -25,8 +25,6 @@
 
 class QuerySet final : public ObjectBase {
   public:
-    static WGPUQuerySet Create(Device* device, const WGPUQuerySetDescriptor* descriptor);
-
     QuerySet(const ObjectBaseParams& params, const WGPUQuerySetDescriptor* descriptor);
     ~QuerySet() override;
 
diff --git a/src/dawn/wire/client/SwapChain.cpp b/src/dawn/wire/client/SwapChain.cpp
index 58b39ab..53d3699 100644
--- a/src/dawn/wire/client/SwapChain.cpp
+++ b/src/dawn/wire/client/SwapChain.cpp
@@ -20,24 +20,6 @@
 
 namespace dawn::wire::client {
 
-// static
-WGPUSwapChain SwapChain::Create(Device* device,
-                                WGPUSurface surface,
-                                const WGPUSwapChainDescriptor* descriptor) {
-    Client* wireClient = device->GetClient();
-    SwapChain* swapChain = wireClient->Make<SwapChain>(surface, descriptor);
-
-    // Send the Device::CreateSwapChain command without modifications.
-    DeviceCreateSwapChainCmd cmd;
-    cmd.self = ToAPI(device);
-    cmd.selfId = device->GetWireId();
-    cmd.descriptor = descriptor;
-    cmd.result = swapChain->GetWireHandle();
-    wireClient->SerializeCommand(cmd);
-
-    return ToAPI(swapChain);
-}
-
 SwapChain::SwapChain(const ObjectBaseParams& params,
                      WGPUSurface,
                      const WGPUSwapChainDescriptor* descriptor)
diff --git a/src/dawn/wire/client/SwapChain.h b/src/dawn/wire/client/SwapChain.h
index ada1058..eb319bf 100644
--- a/src/dawn/wire/client/SwapChain.h
+++ b/src/dawn/wire/client/SwapChain.h
@@ -25,10 +25,6 @@
 
 class SwapChain final : public ObjectBase {
   public:
-    static WGPUSwapChain Create(Device* device,
-                                WGPUSurface surface,
-                                const WGPUSwapChainDescriptor* descriptor);
-
     SwapChain(const ObjectBaseParams& params,
               WGPUSurface surface,
               const WGPUSwapChainDescriptor* descriptor);
diff --git a/src/dawn/wire/client/Texture.cpp b/src/dawn/wire/client/Texture.cpp
index 69f773e..32738a2 100644
--- a/src/dawn/wire/client/Texture.cpp
+++ b/src/dawn/wire/client/Texture.cpp
@@ -19,38 +19,6 @@
 
 namespace dawn::wire::client {
 
-// static
-WGPUTexture Texture::Create(Device* device, const WGPUTextureDescriptor* descriptor) {
-    Client* wireClient = device->GetClient();
-    Texture* texture = wireClient->Make<Texture>(descriptor);
-
-    // Send the Device::CreateTexture command without modifications.
-    DeviceCreateTextureCmd cmd;
-    cmd.self = ToAPI(device);
-    cmd.selfId = device->GetWireId();
-    cmd.descriptor = descriptor;
-    cmd.result = texture->GetWireHandle();
-    wireClient->SerializeCommand(cmd);
-
-    return ToAPI(texture);
-}
-
-// static
-WGPUTexture Texture::CreateError(Device* device, const WGPUTextureDescriptor* descriptor) {
-    Client* wireClient = device->GetClient();
-    Texture* texture = wireClient->Make<Texture>(descriptor);
-
-    // Send the Device::CreateErrorTexture command without modifications.
-    DeviceCreateErrorTextureCmd cmd;
-    cmd.self = ToAPI(device);
-    cmd.selfId = device->GetWireId();
-    cmd.descriptor = descriptor;
-    cmd.result = texture->GetWireHandle();
-    wireClient->SerializeCommand(cmd);
-
-    return ToAPI(texture);
-}
-
 Texture::Texture(const ObjectBaseParams& params, const WGPUTextureDescriptor* descriptor)
     : ObjectBase(params),
       mSize(descriptor->size),
diff --git a/src/dawn/wire/client/Texture.h b/src/dawn/wire/client/Texture.h
index 9798f91..9655eba 100644
--- a/src/dawn/wire/client/Texture.h
+++ b/src/dawn/wire/client/Texture.h
@@ -25,9 +25,6 @@
 
 class Texture final : public ObjectBase {
   public:
-    static WGPUTexture Create(Device* device, const WGPUTextureDescriptor* descriptor);
-    static WGPUTexture CreateError(Device* device, const WGPUTextureDescriptor* descriptor);
-
     Texture(const ObjectBaseParams& params, const WGPUTextureDescriptor* descriptor);
     ~Texture() override;