dawn_wire: Remove client/ApiProcs_autogen.h

This header was only used to call DeviceCreateErrorBuffer and
DeviceInjectError that are used in some handwritten client commands.
We remove the need for the header by making these two commands
handwritten.

This also improves readability, previously injecting errors read:

  ClientDeviceInjectError(reinterpret_cast<WGPUDevice>(device),
                           WGPUErrorType_Validation,
                           "Some validation message");

And now reads:

  device->InjectError(WGPUErrorType_Validation, "Some validation message");

Bug: dawn:445

Change-Id: Ie11570aacf3036e13abd174d91670ecb84661226
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/24080
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Stephen White <senorblanco@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
diff --git a/dawn_wire.json b/dawn_wire.json
index 54d8c94..87063e0 100644
--- a/dawn_wire.json
+++ b/dawn_wire.json
@@ -111,7 +111,9 @@
             "BufferUnmap",
             "DeviceCreateBuffer",
             "DeviceCreateBufferMapped",
+            "DeviceCreateErrorBuffer",
             "DeviceGetDefaultQueue",
+            "DeviceInjectError",
             "DevicePushErrorScope",
             "QueueCreateFence",
             "QueueSignal"
diff --git a/generator/dawn_json_generator.py b/generator/dawn_json_generator.py
index 6037aa8..119ba6a 100644
--- a/generator/dawn_json_generator.py
+++ b/generator/dawn_json_generator.py
@@ -627,7 +627,6 @@
             renders.append(FileRender('dawn_wire/WireCmd.cpp', 'src/dawn_wire/WireCmd_autogen.cpp', wire_params))
             renders.append(FileRender('dawn_wire/client/ApiObjects.h', 'src/dawn_wire/client/ApiObjects_autogen.h', wire_params))
             renders.append(FileRender('dawn_wire/client/ApiProcs.cpp', 'src/dawn_wire/client/ApiProcs_autogen.cpp', wire_params))
-            renders.append(FileRender('dawn_wire/client/ApiProcs.h', 'src/dawn_wire/client/ApiProcs_autogen.h', wire_params))
             renders.append(FileRender('dawn_wire/client/ClientBase.h', 'src/dawn_wire/client/ClientBase_autogen.h', wire_params))
             renders.append(FileRender('dawn_wire/client/ClientHandlers.cpp', 'src/dawn_wire/client/ClientHandlers_autogen.cpp', wire_params))
             renders.append(FileRender('dawn_wire/client/ClientPrototypes.inc', 'src/dawn_wire/client/ClientPrototypes_autogen.inc', wire_params))
diff --git a/generator/templates/dawn_wire/client/ApiProcs.cpp b/generator/templates/dawn_wire/client/ApiProcs.cpp
index 00a207f..3edba1a 100644
--- a/generator/templates/dawn_wire/client/ApiProcs.cpp
+++ b/generator/templates/dawn_wire/client/ApiProcs.cpp
@@ -14,7 +14,6 @@
 
 #include "common/Log.h"
 #include "dawn_wire/client/ApiObjects.h"
-#include "dawn_wire/client/ApiProcs_autogen.h"
 #include "dawn_wire/client/Client.h"
 
 #include <algorithm>
@@ -23,7 +22,6 @@
 #include <vector>
 
 namespace dawn_wire { namespace client {
-
     namespace {
 
         //* Outputs an rvalue that's the number of elements a pointer member points to.
@@ -158,9 +156,8 @@
                         } while (false);
 
                         if (DAWN_UNLIKELY(!sameDevice)) {
-                            ClientDeviceInjectError(reinterpret_cast<WGPUDevice>(device),
-                                                    WGPUErrorType_Validation,
-                                                    "All objects must be from the same device.");
+                            device->InjectError(WGPUErrorType_Validation,
+                                                "All objects must be from the same device.");
                             {% if method.return_type.category == "object" %}
                                 // Allocate an object without registering it on the server. This is backed by a real allocation on
                                 // the client so commands can be sent with it. But because it's not allocated on the server, it will
@@ -236,18 +233,18 @@
         {% endif %}
     {% endfor %}
 
-    void ClientDeviceReference(WGPUDevice) {
-    }
-
-    void ClientDeviceRelease(WGPUDevice) {
-    }
-
     namespace {
         WGPUInstance ClientCreateInstance(WGPUInstanceDescriptor const* descriptor) {
             UNREACHABLE();
             return nullptr;
         }
 
+        void ClientDeviceReference(WGPUDevice) {
+        }
+
+        void ClientDeviceRelease(WGPUDevice) {
+        }
+
         struct ProcEntry {
             WGPUProc proc;
             const char* name;
diff --git a/generator/templates/dawn_wire/client/ApiProcs.h b/generator/templates/dawn_wire/client/ApiProcs.h
deleted file mode 100644
index 2a27c3c..0000000
--- a/generator/templates/dawn_wire/client/ApiProcs.h
+++ /dev/null
@@ -1,41 +0,0 @@
-//* Copyright 2019 The Dawn Authors
-//*
-//* Licensed under the Apache License, Version 2.0 (the "License");
-//* you may not use this file except in compliance with the License.
-//* You may obtain a copy of the License at
-//*
-//*     http://www.apache.org/licenses/LICENSE-2.0
-//*
-//* Unless required by applicable law or agreed to in writing, software
-//* distributed under the License is distributed on an "AS IS" BASIS,
-//* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-//* See the License for the specific language governing permissions and
-//* limitations under the License.
-
-#ifndef DAWNWIRE_CLIENT_APIPROCS_AUTOGEN_H_
-#define DAWNWIRE_CLIENT_APIPROCS_AUTOGEN_H_
-
-#include <dawn/webgpu.h>
-
-namespace dawn_wire { namespace client {
-
-    //* Dawn API
-    {% for type in by_category["object"] %}
-        {% set cType = as_cType(type.name) %}
-        {% for method in c_methods(type) %}
-            {% set Suffix = as_MethodSuffix(type.name, method.name) %}
-            {% if Suffix in client_handwritten_commands %}
-                {% set Suffix = "Handwritten" + Suffix %}
-            {% endif %}
-            {{as_cType(method.return_type.name)}} Client{{Suffix}}(
-                {{-cType}} cSelf
-                {%- for arg in method.arguments -%}
-                    , {{as_annotated_cType(arg)}}
-                {%- endfor -%}
-            );
-        {% endfor %}
-    {% endfor %}
-
-}}  // namespace dawn_wire::client
-
-#endif  // DAWNWIRE_CLIENT_APIPROCS_AUTOGEN_H_
diff --git a/src/dawn_wire/BUILD.gn b/src/dawn_wire/BUILD.gn
index 8b33d5c..9e46342 100644
--- a/src/dawn_wire/BUILD.gn
+++ b/src/dawn_wire/BUILD.gn
@@ -37,7 +37,6 @@
     "src/dawn_wire/WireCmd_autogen.cpp",
     "src/dawn_wire/client/ApiObjects_autogen.h",
     "src/dawn_wire/client/ApiProcs_autogen.cpp",
-    "src/dawn_wire/client/ApiProcs_autogen.h",
     "src/dawn_wire/client/ClientBase_autogen.h",
     "src/dawn_wire/client/ClientHandlers_autogen.cpp",
     "src/dawn_wire/client/ClientPrototypes_autogen.inc",
diff --git a/src/dawn_wire/client/Buffer.cpp b/src/dawn_wire/client/Buffer.cpp
index 910ffed..0b4889e 100644
--- a/src/dawn_wire/client/Buffer.cpp
+++ b/src/dawn_wire/client/Buffer.cpp
@@ -14,7 +14,6 @@
 
 #include "dawn_wire/client/Buffer.h"
 
-#include "dawn_wire/client/ApiProcs_autogen.h"
 #include "dawn_wire/client/Client.h"
 #include "dawn_wire/client/Device.h"
 
@@ -53,9 +52,8 @@
 
         if ((descriptor->usage & (WGPUBufferUsage_MapRead | WGPUBufferUsage_MapWrite)) != 0 &&
             descriptor->size > std::numeric_limits<size_t>::max()) {
-            ClientDeviceInjectError(cDevice, WGPUErrorType_OutOfMemory,
-                                    "Buffer is too large for map usage");
-            return ClientDeviceCreateErrorBuffer(cDevice);
+            device_->InjectError(WGPUErrorType_OutOfMemory, "Buffer is too large for map usage");
+            return device_->CreateErrorBuffer();
         }
 
         auto* bufferObjectAndSerial = wireClient->BufferAllocator().New(device_);
@@ -86,9 +84,8 @@
 
         // This buffer is too large to be mapped and to make a WriteHandle for.
         if (descriptor->size > std::numeric_limits<size_t>::max()) {
-            ClientDeviceInjectError(cDevice, WGPUErrorType_OutOfMemory,
-                                    "Buffer is too large for mapping");
-            result.buffer = ClientDeviceCreateErrorBuffer(cDevice);
+            device_->InjectError(WGPUErrorType_OutOfMemory, "Buffer is too large for mapping");
+            result.buffer = device_->CreateErrorBuffer();
             return result;
         }
 
@@ -99,9 +96,8 @@
                 wireClient->GetMemoryTransferService()->CreateWriteHandle(descriptor->size));
 
         if (writeHandle == nullptr) {
-            ClientDeviceInjectError(cDevice, WGPUErrorType_OutOfMemory,
-                                    "Buffer mapping allocation failed");
-            result.buffer = ClientDeviceCreateErrorBuffer(cDevice);
+            device_->InjectError(WGPUErrorType_OutOfMemory, "Buffer mapping allocation failed");
+            result.buffer = device_->CreateErrorBuffer();
             return result;
         }
 
@@ -111,9 +107,8 @@
         // |result.data| may be null on error.
         std::tie(result.data, result.dataLength) = writeHandle->Open();
         if (result.data == nullptr) {
-            ClientDeviceInjectError(cDevice, WGPUErrorType_OutOfMemory,
-                                    "Buffer mapping allocation failed");
-            result.buffer = ClientDeviceCreateErrorBuffer(cDevice);
+            device_->InjectError(WGPUErrorType_OutOfMemory, "Buffer mapping allocation failed");
+            result.buffer = device_->CreateErrorBuffer();
             return result;
         }
 
@@ -145,6 +140,18 @@
         return result;
     }
 
+    // static
+    WGPUBuffer Buffer::CreateError(Device* device_) {
+        auto* allocation = device_->GetClient()->BufferAllocator().New(device_);
+
+        DeviceCreateErrorBufferCmd cmd;
+        cmd.self = reinterpret_cast<WGPUDevice>(device_);
+        cmd.result = ObjectHandle{allocation->object->id, allocation->generation};
+        device_->GetClient()->SerializeCommand(cmd);
+
+        return reinterpret_cast<WGPUBuffer>(allocation->object.get());
+    }
+
     Buffer::~Buffer() {
         // Callbacks need to be fired in all cases, as they can handle freeing resources
         // so we call them with "Unknown" status.
@@ -169,8 +176,7 @@
         if (mSize > std::numeric_limits<size_t>::max()) {
             // On buffer creation, we check that mappable buffers do not exceed this size.
             // So this buffer must not have mappable usage. Inject a validation error.
-            ClientDeviceInjectError(reinterpret_cast<WGPUDevice>(device), WGPUErrorType_Validation,
-                                    "Buffer needs the correct map usage bit");
+            device->InjectError(WGPUErrorType_Validation, "Buffer needs the correct map usage bit");
             callback(WGPUBufferMapAsyncStatus_Error, nullptr, 0, userdata);
             return;
         }
@@ -181,8 +187,7 @@
             device->GetClient()->GetMemoryTransferService()->CreateReadHandle(
                 static_cast<size_t>(mSize));
         if (readHandle == nullptr) {
-            ClientDeviceInjectError(reinterpret_cast<WGPUDevice>(device), WGPUErrorType_OutOfMemory,
-                                    "Failed to create buffer mapping");
+            device->InjectError(WGPUErrorType_OutOfMemory, "Failed to create buffer mapping");
             callback(WGPUBufferMapAsyncStatus_Error, nullptr, 0, userdata);
             return;
         }
@@ -207,8 +212,7 @@
         if (mSize > std::numeric_limits<size_t>::max()) {
             // On buffer creation, we check that mappable buffers do not exceed this size.
             // So this buffer must not have mappable usage. Inject a validation error.
-            ClientDeviceInjectError(reinterpret_cast<WGPUDevice>(device), WGPUErrorType_Validation,
-                                    "Buffer needs the correct map usage bit");
+            device->InjectError(WGPUErrorType_Validation, "Buffer needs the correct map usage bit");
             callback(WGPUBufferMapAsyncStatus_Error, nullptr, 0, userdata);
             return;
         }
@@ -219,8 +223,7 @@
             device->GetClient()->GetMemoryTransferService()->CreateWriteHandle(
                 static_cast<size_t>(mSize));
         if (writeHandle == nullptr) {
-            ClientDeviceInjectError(reinterpret_cast<WGPUDevice>(device), WGPUErrorType_OutOfMemory,
-                                    "Failed to create buffer mapping");
+            device->InjectError(WGPUErrorType_OutOfMemory, "Failed to create buffer mapping");
             callback(WGPUBufferMapAsyncStatus_Error, nullptr, 0, userdata);
             return;
         }
@@ -366,8 +369,7 @@
 
     void* Buffer::GetMappedRange() {
         if (!IsMappedForWriting()) {
-            ClientDeviceInjectError(reinterpret_cast<WGPUDevice>(device), WGPUErrorType_Validation,
-                                    "Buffer needs to be mapped for writing");
+            device->InjectError(WGPUErrorType_Validation, "Buffer needs to be mapped for writing");
             return nullptr;
         }
         return mMappedData;
@@ -375,8 +377,7 @@
 
     const void* Buffer::GetConstMappedRange() {
         if (!IsMappedForWriting() && !IsMappedForReading()) {
-            ClientDeviceInjectError(reinterpret_cast<WGPUDevice>(device), WGPUErrorType_Validation,
-                                    "Buffer needs to be mapped");
+            device->InjectError(WGPUErrorType_Validation, "Buffer needs to be mapped");
             return nullptr;
         }
         return mMappedData;
diff --git a/src/dawn_wire/client/Buffer.h b/src/dawn_wire/client/Buffer.h
index 2dd8dd5..a69b054 100644
--- a/src/dawn_wire/client/Buffer.h
+++ b/src/dawn_wire/client/Buffer.h
@@ -31,6 +31,7 @@
         static WGPUBuffer Create(Device* device, const WGPUBufferDescriptor* descriptor);
         static WGPUCreateBufferMappedResult CreateMapped(Device* device,
                                                          const WGPUBufferDescriptor* descriptor);
+        static WGPUBuffer CreateError(Device* device);
 
         ~Buffer();
         void ClearMapRequests(WGPUBufferMapAsyncStatus status);
diff --git a/src/dawn_wire/client/Device.cpp b/src/dawn_wire/client/Device.cpp
index 64b1b04..4df6226 100644
--- a/src/dawn_wire/client/Device.cpp
+++ b/src/dawn_wire/client/Device.cpp
@@ -137,6 +137,14 @@
         return true;
     }
 
+    void Device::InjectError(WGPUErrorType type, const char* message) {
+        DeviceInjectErrorCmd cmd;
+        cmd.self = reinterpret_cast<WGPUDevice>(this);
+        cmd.type = type;
+        cmd.message = message;
+        mClient->SerializeCommand(cmd);
+    }
+
     WGPUBuffer Device::CreateBuffer(const WGPUBufferDescriptor* descriptor) {
         return Buffer::Create(this, descriptor);
     }
@@ -146,6 +154,10 @@
         return Buffer::CreateMapped(this, descriptor);
     }
 
+    WGPUBuffer Device::CreateErrorBuffer() {
+        return Buffer::CreateError(this);
+    }
+
     WGPUQueue Device::GetDefaultQueue() {
         mDefaultQueue->refcount++;
         return reinterpret_cast<WGPUQueue>(mDefaultQueue);
diff --git a/src/dawn_wire/client/Device.h b/src/dawn_wire/client/Device.h
index 5981124..2b554e3 100644
--- a/src/dawn_wire/client/Device.h
+++ b/src/dawn_wire/client/Device.h
@@ -34,10 +34,12 @@
         Client* GetClient();
         void SetUncapturedErrorCallback(WGPUErrorCallback errorCallback, void* errorUserdata);
         void SetDeviceLostCallback(WGPUDeviceLostCallback errorCallback, void* errorUserdata);
+        void InjectError(WGPUErrorType type, const char* message);
         void PushErrorScope(WGPUErrorFilter filter);
         bool PopErrorScope(WGPUErrorCallback callback, void* userdata);
         WGPUBuffer CreateBuffer(const WGPUBufferDescriptor* descriptor);
         WGPUCreateBufferMappedResult CreateBufferMapped(const WGPUBufferDescriptor* descriptor);
+        WGPUBuffer CreateErrorBuffer();
 
         void HandleError(WGPUErrorType errorType, const char* message);
         void HandleDeviceLost(const char* message);
diff --git a/src/dawn_wire/client/Fence.cpp b/src/dawn_wire/client/Fence.cpp
index aa0407c..d9e1fa7 100644
--- a/src/dawn_wire/client/Fence.cpp
+++ b/src/dawn_wire/client/Fence.cpp
@@ -14,7 +14,7 @@
 
 #include "dawn_wire/client/Fence.h"
 
-#include "dawn_wire/client/ApiProcs_autogen.h"
+#include "dawn_wire/client/Device.h"
 
 namespace dawn_wire { namespace client {
 
@@ -46,8 +46,8 @@
                              WGPUFenceOnCompletionCallback callback,
                              void* userdata) {
         if (value > mSignaledValue) {
-            ClientDeviceInjectError(reinterpret_cast<WGPUDevice>(device), WGPUErrorType_Validation,
-                                    "Value greater than fence signaled value");
+            device->InjectError(WGPUErrorType_Validation,
+                                "Value greater than fence signaled value");
             callback(WGPUFenceCompletionStatus_Error, userdata);
             return;
         }
diff --git a/src/dawn_wire/client/Queue.cpp b/src/dawn_wire/client/Queue.cpp
index fa338f3..f515f52 100644
--- a/src/dawn_wire/client/Queue.cpp
+++ b/src/dawn_wire/client/Queue.cpp
@@ -14,7 +14,6 @@
 
 #include "dawn_wire/client/Queue.h"
 
-#include "dawn_wire/client/ApiProcs_autogen.h"
 #include "dawn_wire/client/Client.h"
 #include "dawn_wire/client/Device.h"
 
@@ -37,15 +36,13 @@
     void Queue::Signal(WGPUFence cFence, uint64_t signalValue) {
         Fence* fence = reinterpret_cast<Fence*>(cFence);
         if (fence->GetQueue() != this) {
-            ClientDeviceInjectError(reinterpret_cast<WGPUDevice>(fence->device),
-                                    WGPUErrorType_Validation,
-                                    "Fence must be signaled on the queue on which it was created.");
+            device->InjectError(WGPUErrorType_Validation,
+                                "Fence must be signaled on the queue on which it was created.");
             return;
         }
         if (signalValue <= fence->GetSignaledValue()) {
-            ClientDeviceInjectError(reinterpret_cast<WGPUDevice>(fence->device),
-                                    WGPUErrorType_Validation,
-                                    "Fence value less than or equal to signaled value");
+            device->InjectError(WGPUErrorType_Validation,
+                                "Fence value less than or equal to signaled value");
             return;
         }