Add FromAPI / ToAPI helpers for dawn_native

Helps to avoid explicitly using reinterpret_cast

Change-Id: I4df0e7094c7d8460385c6f23dac529ace9df9bdc
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/67605
Auto-Submit: Austin Eng <enga@chromium.org>
Reviewed-by: Loko Kung <lokokung@google.com>
Commit-Queue: Austin Eng <enga@chromium.org>
diff --git a/generator/dawn_json_generator.py b/generator/dawn_json_generator.py
index c0456af..8783eb0 100644
--- a/generator/dawn_json_generator.py
+++ b/generator/dawn_json_generator.py
@@ -819,6 +819,10 @@
                            'src/dawn_native/ValidationUtils_autogen.cpp',
                            frontend_params))
             renders.append(
+                FileRender('dawn_native/dawn_platform.h',
+                           'src/dawn_native/dawn_platform_autogen.h',
+                           frontend_params))
+            renders.append(
                 FileRender('dawn_native/wgpu_structs.h',
                            'src/dawn_native/wgpu_structs_autogen.h',
                            frontend_params))
diff --git a/generator/templates/dawn_native/dawn_platform.h b/generator/templates/dawn_native/dawn_platform.h
new file mode 100644
index 0000000..af42e03
--- /dev/null
+++ b/generator/templates/dawn_native/dawn_platform.h
@@ -0,0 +1,64 @@
+//* Copyright 2021 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 DAWNNATIVE_DAWN_PLATFORM_AUTOGEN_H_
+#define DAWNNATIVE_DAWN_PLATFORM_AUTOGEN_H_
+
+#include "dawn/webgpu_cpp.h"
+#include "dawn_native/Forward.h"
+
+// Use our autogenerated version of the wgpu structures that point to dawn_native object types
+// (wgpu::Buffer is dawn_native::BufferBase*)
+#include <dawn_native/wgpu_structs_autogen.h>
+
+namespace dawn_native {
+
+    {% for type in by_category["structure"] %}
+        inline const {{as_cType(type.name)}}* ToAPI(const {{as_cppType(type.name)}}* rhs) {
+            return reinterpret_cast<const {{as_cType(type.name)}}*>(rhs);
+        }
+
+        inline {{as_cType(type.name)}}* ToAPI({{as_cppType(type.name)}}* rhs) {
+            return reinterpret_cast<{{as_cType(type.name)}}*>(rhs);
+        }
+
+        inline const {{as_cppType(type.name)}}* FromAPI(const {{as_cType(type.name)}}* rhs) {
+            return reinterpret_cast<const {{as_cppType(type.name)}}*>(rhs);
+        }
+
+        inline {{as_cppType(type.name)}}* FromAPI({{as_cType(type.name)}}* rhs) {
+            return reinterpret_cast<{{as_cppType(type.name)}}*>(rhs);
+        }
+    {% endfor %}
+
+    {% for type in by_category["object"] %}
+        inline const {{as_cType(type.name)}}Impl* ToAPI(const {{as_cppType(type.name)}}Base* rhs) {
+            return reinterpret_cast<const {{as_cType(type.name)}}Impl*>(rhs);
+        }
+
+        inline {{as_cType(type.name)}}Impl* ToAPI({{as_cppType(type.name)}}Base* rhs) {
+            return reinterpret_cast<{{as_cType(type.name)}}Impl*>(rhs);
+        }
+
+        inline const {{as_cppType(type.name)}}Base* FromAPI(const {{as_cType(type.name)}}Impl* rhs) {
+            return reinterpret_cast<const {{as_cppType(type.name)}}Base*>(rhs);
+        }
+
+        inline {{as_cppType(type.name)}}Base* FromAPI({{as_cType(type.name)}}Impl* rhs) {
+            return reinterpret_cast<{{as_cppType(type.name)}}Base*>(rhs);
+        }
+    {% endfor %}
+}
+
+#endif  // DAWNNATIVE_DAWN_PLATFORM_AUTOGEN_H_
\ No newline at end of file
diff --git a/src/dawn_native/BUILD.gn b/src/dawn_native/BUILD.gn
index 37254d7..979c7c4 100644
--- a/src/dawn_native/BUILD.gn
+++ b/src/dawn_native/BUILD.gn
@@ -105,6 +105,7 @@
     "src/dawn_native/ChainUtils_autogen.h",
     "src/dawn_native/ChainUtils_autogen.cpp",
     "src/dawn_native/ProcTable.cpp",
+    "src/dawn_native/dawn_platform_autogen.h",
     "src/dawn_native/wgpu_structs_autogen.h",
     "src/dawn_native/wgpu_structs_autogen.cpp",
     "src/dawn_native/ValidationUtils_autogen.h",
diff --git a/src/dawn_native/CommandEncoder.h b/src/dawn_native/CommandEncoder.h
index f7597e0..6547e7d 100644
--- a/src/dawn_native/CommandEncoder.h
+++ b/src/dawn_native/CommandEncoder.h
@@ -103,9 +103,6 @@
         uint64_t mDebugGroupStackSize = 0;
     };
 
-    // For the benefit of template generation.
-    using CommandEncoderBase = CommandEncoder;
-
 }  // namespace dawn_native
 
 #endif  // DAWNNATIVE_COMMANDENCODER_H_
diff --git a/src/dawn_native/ComputePassEncoder.h b/src/dawn_native/ComputePassEncoder.h
index 57a9750..b0962f4 100644
--- a/src/dawn_native/ComputePassEncoder.h
+++ b/src/dawn_native/ComputePassEncoder.h
@@ -69,9 +69,6 @@
         Ref<CommandEncoder> mCommandEncoder;
     };
 
-    // For the benefit of template generation.
-    using ComputePassEncoderBase = ComputePassEncoder;
-
 }  // namespace dawn_native
 
 #endif  // DAWNNATIVE_COMPUTEPASSENCODER_H_
diff --git a/src/dawn_native/Forward.h b/src/dawn_native/Forward.h
index 61b628c..2716f63 100644
--- a/src/dawn_native/Forward.h
+++ b/src/dawn_native/Forward.h
@@ -59,6 +59,13 @@
 
     struct Format;
 
+    // Aliases for frontend-only types.
+    using CommandEncoderBase = CommandEncoder;
+    using ComputePassEncoderBase = ComputePassEncoder;
+    using RenderBundleEncoderBase = RenderBundleEncoder;
+    using RenderPassEncoderBase = RenderPassEncoder;
+    using SurfaceBase = Surface;
+
 }  // namespace dawn_native
 
 #endif  // DAWNNATIVE_FORWARD_H_
diff --git a/src/dawn_native/RenderBundleEncoder.h b/src/dawn_native/RenderBundleEncoder.h
index 0c7ab44..841c9fc 100644
--- a/src/dawn_native/RenderBundleEncoder.h
+++ b/src/dawn_native/RenderBundleEncoder.h
@@ -49,9 +49,6 @@
         EncodingContext mBundleEncodingContext;
     };
 
-    // For the benefit of template generation.
-    using RenderBundleEncoderBase = RenderBundleEncoder;
-
 }  // namespace dawn_native
 
 #endif  // DAWNNATIVE_RENDERBUNDLEENCODER_H_
diff --git a/src/dawn_native/RenderPassEncoder.h b/src/dawn_native/RenderPassEncoder.h
index ae9ccbb..296c32e 100644
--- a/src/dawn_native/RenderPassEncoder.h
+++ b/src/dawn_native/RenderPassEncoder.h
@@ -82,9 +82,6 @@
         bool mOcclusionQueryActive = false;
     };
 
-    // For the benefit of template generation.
-    using RenderPassEncoderBase = RenderPassEncoder;
-
 }  // namespace dawn_native
 
 #endif  // DAWNNATIVE_RENDERPASSENCODER_H_
diff --git a/src/dawn_native/Surface.h b/src/dawn_native/Surface.h
index d3b3b69..f9b47d3 100644
--- a/src/dawn_native/Surface.h
+++ b/src/dawn_native/Surface.h
@@ -105,9 +105,6 @@
         const absl::FormatConversionSpec& spec,
         absl::FormatSink* s);
 
-    // For the benefit of template generation.
-    using SurfaceBase = Surface;
-
 }  // namespace dawn_native
 
 #endif  // DAWNNATIVE_SURFACE_H_
diff --git a/src/dawn_native/dawn_platform.h b/src/dawn_native/dawn_platform.h
index d36a48a..d4dfec1 100644
--- a/src/dawn_native/dawn_platform.h
+++ b/src/dawn_native/dawn_platform.h
@@ -18,9 +18,7 @@
 // Use webgpu_cpp to have the enum and bitfield definitions
 #include <dawn/webgpu_cpp.h>
 
-// Use our autogenerated version of the wgpu structures that point to dawn_native object types
-// (wgpu::Buffer is dawn_native::BufferBase*)
-#include <dawn_native/wgpu_structs_autogen.h>
+#include <dawn_native/dawn_platform_autogen.h>
 
 namespace dawn_native {
     // Extra buffer usages