Add dawnGetProcAddress.

This will become wgpuGetProcAddress that is part of the webgpu.h and the
last gap in functionality for dawn.h to match webgpu.h.

BUG=dawn:22

Change-Id: I0dcb3b5e6bd99cb10db273fc101d3ec0161b7da0
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/12120
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index 111ada7..0d06daf 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -773,6 +773,7 @@
     "src/tests/unittests/EnumClassBitmasksTests.cpp",
     "src/tests/unittests/ErrorTests.cpp",
     "src/tests/unittests/ExtensionTests.cpp",
+    "src/tests/unittests/GetProcAddressTests.cpp",
     "src/tests/unittests/MathTests.cpp",
     "src/tests/unittests/ObjectBaseTests.cpp",
     "src/tests/unittests/PerStageTests.cpp",
diff --git a/generator/dawn_json_generator.py b/generator/dawn_json_generator.py
index 2489588..f92ad92 100644
--- a/generator/dawn_json_generator.py
+++ b/generator/dawn_json_generator.py
@@ -448,6 +448,12 @@
         Method(Name('release'), types['void'], []),
     ]
 
+def get_methods_sorted_by_name(api_params):
+    unsorted = [(as_MethodSuffix(typ.name, method.name), typ, method) \
+            for typ in api_params['by_category']['object'] \
+            for method in c_native_methods(api_params['types'], typ) ]
+    return [(typ, method) for (_, typ, method) in sorted(unsorted)]
+
 class MultiGeneratorFromDawnJSON(Generator):
     def get_description(self):
         return 'Generates code for various target from Dawn.json.'
@@ -486,6 +492,7 @@
             'convert_cType_to_cppType': convert_cType_to_cppType,
             'as_varName': as_varName,
             'decorate': decorate,
+            'methods_sorted_by_name': get_methods_sorted_by_name(api_params),
         }
 
         renders = []
diff --git a/generator/templates/api.h b/generator/templates/api.h
index 733c387..bb71141 100644
--- a/generator/templates/api.h
+++ b/generator/templates/api.h
@@ -54,7 +54,6 @@
 #endif
 
 // Custom types depending on the target language
-typedef void (*DawnErrorCallback)(DawnErrorType type, const char* message, void* userdata);
 typedef void (*DawnBufferCreateMappedCallback)(DawnBufferMapAsyncStatus status,
                                                DawnCreateBufferMappedResult result,
                                                void* userdata);
@@ -67,9 +66,14 @@
                                            uint64_t dataLength,
                                            void* userdata);
 typedef void (*DawnFenceOnCompletionCallback)(DawnFenceCompletionStatus status, void* userdata);
+typedef void (*DawnErrorCallback)(DawnErrorType type, const char* message, void* userdata);
+
+typedef void (*DawnProc)();
 
 #if !defined(DAWN_SKIP_PROCS)
 
+typedef DawnProc (*DawnProcGetProcAddress)(DawnDevice device, const char* procName);
+
 {% for type in by_category["object"] %}
     // Procs of {{type.name.CamelCase()}}
     {% for method in native_methods(type) %}
@@ -86,6 +90,8 @@
 
 #if !defined(DAWN_SKIP_DECLARATIONS)
 
+DAWN_EXPORT DawnProc DawnGetProcAddress(DawnDevice device, const char* procName);
+
 {% for type in by_category["object"] %}
     // Methods of {{type.name.CamelCase()}}
     {% for method in native_methods(type) %}
diff --git a/generator/templates/api_proc.c b/generator/templates/api_proc.c
index a09175b..a622591 100644
--- a/generator/templates/api_proc.c
+++ b/generator/templates/api_proc.c
@@ -26,6 +26,10 @@
     }
 }
 
+DawnProc DawnGetProcAddress(DawnDevice device, const char* procName) {
+    return procs.getProcAddress(device, procName);
+}
+
 {% for type in by_category["object"] %}
     {% for method in native_methods(type) %}
         {{as_cType(method.return_type.name)}} {{as_cMethod(type.name, method.name)}}(
diff --git a/generator/templates/api_proc_table.h b/generator/templates/api_proc_table.h
index 1f1eb3c..9fb850c 100644
--- a/generator/templates/api_proc_table.h
+++ b/generator/templates/api_proc_table.h
@@ -18,6 +18,8 @@
 #include "dawn/dawn.h"
 
 typedef struct DawnProcTable {
+    DawnProcGetProcAddress getProcAddress;
+
     {% for type in by_category["object"] %}
         {% for method in native_methods(type) %}
             {{as_cProc(type.name, method.name)}} {{as_varName(type.name, method.name)}};
diff --git a/generator/templates/apicpp.cpp b/generator/templates/apicpp.cpp
index ad468d1..b43fb4f 100644
--- a/generator/templates/apicpp.cpp
+++ b/generator/templates/apicpp.cpp
@@ -112,4 +112,8 @@
 
     {% endfor %}
 
+    Proc GetProcAddress(Device const& device, const char* procName) {
+        return reinterpret_cast<Proc>(DawnGetProcAddress(device.Get(), procName));
+    }
+
 }
diff --git a/generator/templates/apicpp.h b/generator/templates/apicpp.h
index d5e9ea1..a0122c9 100644
--- a/generator/templates/apicpp.h
+++ b/generator/templates/apicpp.h
@@ -48,6 +48,7 @@
 
     {% endfor %}
 
+    using Proc = DawnProc;
     {% for type in by_category["natively defined"] %}
         using {{as_cppType(type.name)}} = {{as_cType(type.name)}};
     {% endfor %}
@@ -175,6 +176,8 @@
 
     {% endfor %}
 
+    DAWN_EXPORT Proc GetProcAddress(Device const& device, const char* procName);
+
     {% for type in by_category["structure"] %}
         struct {{as_cppType(type.name)}} {
             {% if type.extensible %}
diff --git a/generator/templates/dawn_native/ProcTable.cpp b/generator/templates/dawn_native/ProcTable.cpp
index 186b334..4f1bc7c 100644
--- a/generator/templates/dawn_native/ProcTable.cpp
+++ b/generator/templates/dawn_native/ProcTable.cpp
@@ -12,12 +12,10 @@
 //* See the License for the specific language governing permissions and
 //* limitations under the License.
 
-#include "common/Assert.h"
-
 #include "dawn_native/dawn_platform.h"
 #include "dawn_native/DawnNative.h"
-#include "dawn_native/ErrorData.h"
-#include "dawn_native/ValidationUtils_autogen.h"
+
+#include <vector>
 
 {% for type in by_category["object"] %}
     {% if type.name.canonical_case() not in ["texture view"] %}
@@ -28,11 +26,12 @@
 namespace dawn_native {
 
     namespace {
+
         {% for type in by_category["object"] %}
             {% for method in native_methods(type) %}
                 {% set suffix = as_MethodSuffix(type.name, method.name) %}
 
-                {{as_cType(method.return_type.name)}} CToCpp{{suffix}}(
+                {{as_cType(method.return_type.name)}} Native{{suffix}}(
                     {{-as_cType(type.name)}} cSelf
                     {%- for arg in method.arguments -%}
                         , {{as_annotated_cType(arg)}}
@@ -71,13 +70,56 @@
                 }
             {% endfor %}
         {% endfor %}
+
+        struct ProcEntry {
+            DawnProc proc;
+            const char* name;
+        };
+        static const ProcEntry sProcMap[] = {
+            {% for (type, method) in methods_sorted_by_name %}
+                { reinterpret_cast<DawnProc>(Native{{as_MethodSuffix(type.name, method.name)}}), "{{as_cMethod(type.name, method.name)}}" },
+            {% endfor %}
+        };
+        static constexpr size_t sProcMapSize = sizeof(sProcMap) / sizeof(sProcMap[0]);
+    }
+
+    DawnProc NativeGetProcAddress(DawnDevice, const char* procName) {
+        if (procName == nullptr) {
+            return nullptr;
+        }
+
+        const ProcEntry* entry = std::lower_bound(&sProcMap[0], &sProcMap[sProcMapSize], procName,
+            [](const ProcEntry &a, const char *b) -> bool {
+                return strcmp(a.name, b) < 0;
+            }
+        );
+
+        if (entry != &sProcMap[sProcMapSize] && strcmp(entry->name, procName) == 0) {
+            return entry->proc;
+        }
+
+        if (strcmp(procName, "dawnGetProcAddress") == 0) {
+            return reinterpret_cast<DawnProc>(NativeGetProcAddress);
+        }
+
+        return nullptr;
+    }
+
+    std::vector<const char*> GetProcMapNamesForTesting() {
+        std::vector<const char*> result;
+        result.reserve(sProcMapSize);
+        for (const ProcEntry& entry : sProcMap) {
+            result.push_back(entry.name);
+        }
+        return result;
     }
 
     DawnProcTable GetProcsAutogen() {
         DawnProcTable table;
+        table.getProcAddress = NativeGetProcAddress;
         {% for type in by_category["object"] %}
             {% for method in native_methods(type) %}
-                table.{{as_varName(type.name, method.name)}} = CToCpp{{as_MethodSuffix(type.name, method.name)}};
+                table.{{as_varName(type.name, method.name)}} = Native{{as_MethodSuffix(type.name, method.name)}};
             {% endfor %}
         {% endfor %}
         return table;
diff --git a/generator/templates/dawn_wire/client/ApiProcs.cpp b/generator/templates/dawn_wire/client/ApiProcs.cpp
index 5b3fa95..1bdccbb 100644
--- a/generator/templates/dawn_wire/client/ApiProcs.cpp
+++ b/generator/templates/dawn_wire/client/ApiProcs.cpp
@@ -16,6 +16,9 @@
 #include "dawn_wire/client/ApiProcs_autogen.h"
 #include "dawn_wire/client/Client.h"
 
+#include <string>
+#include <vector>
+
 namespace dawn_wire { namespace client {
     //* Implementation of the client API functions.
     {% for type in by_category["object"] %}
@@ -89,6 +92,50 @@
         {% endif %}
     {% endfor %}
 
+    namespace {
+        struct ProcEntry {
+            DawnProc proc;
+            const char* name;
+        };
+        static const ProcEntry sProcMap[] = {
+            {% for (type, method) in methods_sorted_by_name %}
+                { reinterpret_cast<DawnProc>(Client{{as_MethodSuffix(type.name, method.name)}}), "{{as_cMethod(type.name, method.name)}}" },
+            {% endfor %}
+        };
+        static constexpr size_t sProcMapSize = sizeof(sProcMap) / sizeof(sProcMap[0]);
+    }  // anonymous namespace
+
+    DawnProc ClientGetProcAddress(DawnDevice, const char* procName) {
+        if (procName == nullptr) {
+            return nullptr;
+        }
+
+        const ProcEntry* entry = std::lower_bound(&sProcMap[0], &sProcMap[sProcMapSize], procName,
+            [](const ProcEntry &a, const char *b) -> bool {
+                return strcmp(a.name, b) < 0;
+            }
+        );
+
+        if (entry != &sProcMap[sProcMapSize] && strcmp(entry->name, procName) == 0) {
+            return entry->proc;
+        }
+
+        if (strcmp(procName, "dawnGetProcAddress") == 0) {
+            return reinterpret_cast<DawnProc>(ClientGetProcAddress);
+        }
+
+        return nullptr;
+    }
+
+    std::vector<const char*> GetProcMapNamesForTesting() {
+        std::vector<const char*> result;
+        result.reserve(sProcMapSize);
+        for (const ProcEntry& entry : sProcMap) {
+            result.push_back(entry.name);
+        }
+        return result;
+    }
+
     //* Some commands don't have a custom wire format, but need to be handled manually to update
     //* some client-side state tracking. For these we have two functions:
     //*  - An autogenerated Client{{suffix}} method that sends the command on the wire
@@ -96,6 +143,7 @@
     //*    the autogenerated one, and that will have to call Client{{suffix}}
     DawnProcTable GetProcs() {
         DawnProcTable table;
+        table.getProcAddress = ClientGetProcAddress;
         {% for type in by_category["object"] %}
             {% for method in native_methods(type) %}
                 {% set suffix = as_MethodSuffix(type.name, method.name) %}
diff --git a/src/include/dawn_native/DawnNative.h b/src/include/dawn_native/DawnNative.h
index d6eeebc..b3125ed 100644
--- a/src/include/dawn_native/DawnNative.h
+++ b/src/include/dawn_native/DawnNative.h
@@ -160,6 +160,9 @@
 
     // Backdoor to get the number of lazy clears for testing
     DAWN_NATIVE_EXPORT size_t GetLazyClearCountForTesting(DawnDevice device);
+
+    // Backdoor to get the order of the ProcMap for testing
+    DAWN_NATIVE_EXPORT std::vector<const char*> GetProcMapNamesForTesting();
 }  // namespace dawn_native
 
 #endif  // DAWNNATIVE_DAWNNATIVE_H_
diff --git a/src/include/dawn_wire/WireClient.h b/src/include/dawn_wire/WireClient.h
index 215e893..d56e090 100644
--- a/src/include/dawn_wire/WireClient.h
+++ b/src/include/dawn_wire/WireClient.h
@@ -120,8 +120,10 @@
                 virtual ~WriteHandle();
             };
         };
-    }  // namespace client
 
+        // Backdoor to get the order of the ProcMap for testing
+        DAWN_WIRE_EXPORT std::vector<const char*> GetProcMapNamesForTesting();
+    }  // namespace client
 }  // namespace dawn_wire
 
 #endif  // DAWNWIRE_WIRECLIENT_H_
diff --git a/src/tests/unittests/GetProcAddressTests.cpp b/src/tests/unittests/GetProcAddressTests.cpp
new file mode 100644
index 0000000..e2a93b0
--- /dev/null
+++ b/src/tests/unittests/GetProcAddressTests.cpp
@@ -0,0 +1,166 @@
+// 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.
+
+#include <gtest/gtest.h>
+
+#include "dawn/dawn_proc.h"
+#include "dawn_native/Instance.h"
+#include "dawn_native/null/DeviceNull.h"
+#include "dawn_wire/WireClient.h"
+#include "utils/TerribleCommandBuffer.h"
+
+namespace {
+
+    // libdawn_wire and libdawn_native contain duplicated code for the handling of GetProcAddress
+    // so we run the tests against both implementations. This enum is used as a test parameters to
+    // know which implementation to test.
+    enum class DawnFlavor {
+        Native,
+        Wire,
+    };
+
+    std::ostream& operator<<(std::ostream& stream, DawnFlavor flavor) {
+        switch (flavor) {
+            case DawnFlavor::Native:
+                stream << "dawn_native";
+                break;
+
+            case DawnFlavor::Wire:
+                stream << "dawn_wire";
+                break;
+
+            default:
+                UNREACHABLE();
+                break;
+        }
+        return stream;
+    }
+
+    class GetProcAddressTests : public testing::TestWithParam<DawnFlavor> {
+      public:
+        GetProcAddressTests()
+            : testing::TestWithParam<DawnFlavor>(),
+              mNativeInstance(),
+              mNativeAdapter(&mNativeInstance) {
+        }
+
+        void SetUp() override {
+            switch (GetParam()) {
+                case DawnFlavor::Native: {
+                    mDevice = dawn::Device::Acquire(
+                        reinterpret_cast<DawnDevice>(mNativeAdapter.CreateDevice(nullptr)));
+                    mProcs = dawn_native::GetProcs();
+                    break;
+                }
+
+                case DawnFlavor::Wire: {
+                    mC2sBuf = std::make_unique<utils::TerribleCommandBuffer>();
+
+                    dawn_wire::WireClientDescriptor clientDesc = {};
+                    clientDesc.serializer = mC2sBuf.get();
+                    mWireClient = std::make_unique<dawn_wire::WireClient>(clientDesc);
+
+                    mDevice = dawn::Device::Acquire(mWireClient->GetDevice());
+                    mProcs = mWireClient->GetProcs();
+                    break;
+                }
+
+                default:
+                    UNREACHABLE();
+                    break;
+            }
+
+            dawnProcSetProcs(&mProcs);
+        }
+
+        void TearDown() override {
+            // Destroy the device before freeing the instance or the wire client in the destructor
+            mDevice = dawn::Device();
+        }
+
+      protected:
+        dawn_native::InstanceBase mNativeInstance;
+        dawn_native::null::Adapter mNativeAdapter;
+
+        std::unique_ptr<utils::TerribleCommandBuffer> mC2sBuf;
+        std::unique_ptr<dawn_wire::WireClient> mWireClient;
+
+        dawn::Device mDevice;
+        DawnProcTable mProcs;
+    };
+
+    // Test GetProcAddress with and without devices on some valid examples
+    TEST_P(GetProcAddressTests, ValidExamples) {
+        ASSERT_EQ(mProcs.getProcAddress(nullptr, "dawnDeviceCreateBuffer"),
+                  reinterpret_cast<DawnProc>(mProcs.deviceCreateBuffer));
+        ASSERT_EQ(mProcs.getProcAddress(mDevice.Get(), "dawnDeviceCreateBuffer"),
+                  reinterpret_cast<DawnProc>(mProcs.deviceCreateBuffer));
+        ASSERT_EQ(mProcs.getProcAddress(nullptr, "dawnQueueSubmit"),
+                  reinterpret_cast<DawnProc>(mProcs.queueSubmit));
+        ASSERT_EQ(mProcs.getProcAddress(mDevice.Get(), "dawnQueueSubmit"),
+                  reinterpret_cast<DawnProc>(mProcs.queueSubmit));
+    }
+
+    // Test GetProcAddress with and without devices on nullptr procName
+    TEST_P(GetProcAddressTests, Nullptr) {
+        ASSERT_EQ(mProcs.getProcAddress(nullptr, nullptr), nullptr);
+        ASSERT_EQ(mProcs.getProcAddress(mDevice.Get(), nullptr), nullptr);
+    }
+
+    // Test GetProcAddress with and without devices on some invalid
+    TEST_P(GetProcAddressTests, InvalidExamples) {
+        ASSERT_EQ(mProcs.getProcAddress(nullptr, "dawnDeviceDoSomething"), nullptr);
+        ASSERT_EQ(mProcs.getProcAddress(mDevice.Get(), "dawnDeviceDoSomething"), nullptr);
+
+        // Trigger the condition where lower_bound will return the end of the procMap.
+        ASSERT_EQ(mProcs.getProcAddress(nullptr, "zzzzzzz"), nullptr);
+        ASSERT_EQ(mProcs.getProcAddress(mDevice.Get(), "zzzzzzz"), nullptr);
+        ASSERT_EQ(mProcs.getProcAddress(nullptr, "ZZ"), nullptr);
+        ASSERT_EQ(mProcs.getProcAddress(mDevice.Get(), "ZZ"), nullptr);
+
+        // Some more potential corner cases.
+        ASSERT_EQ(mProcs.getProcAddress(nullptr, ""), nullptr);
+        ASSERT_EQ(mProcs.getProcAddress(mDevice.Get(), ""), nullptr);
+        ASSERT_EQ(mProcs.getProcAddress(nullptr, "0"), nullptr);
+        ASSERT_EQ(mProcs.getProcAddress(mDevice.Get(), "0"), nullptr);
+    }
+
+    // Test that GetProcAddress supports itself: it is handled specially because it is a
+    // freestanding function and not a method on an object.
+    TEST_P(GetProcAddressTests, GetProcAddressItself) {
+        ASSERT_EQ(mProcs.getProcAddress(nullptr, "dawnGetProcAddress"),
+                  reinterpret_cast<DawnProc>(mProcs.getProcAddress));
+        ASSERT_EQ(mProcs.getProcAddress(mDevice.Get(), "dawnGetProcAddress"),
+                  reinterpret_cast<DawnProc>(mProcs.getProcAddress));
+    }
+
+    INSTANTIATE_TEST_SUITE_P(,
+                             GetProcAddressTests,
+                             testing::Values(DawnFlavor::Native, DawnFlavor::Wire),
+                             testing::PrintToStringParamName());
+
+    TEST(GetProcAddressInternalTests, CheckDawnNativeProcMapOrder) {
+        std::vector<const char*> names = dawn_native::GetProcMapNamesForTesting();
+        for (size_t i = 1; i < names.size(); i++) {
+            ASSERT_LT(std::string(names[i - 1]), std::string(names[i]));
+        }
+    }
+
+    TEST(GetProcAddressInternalTests, CheckDawnWireClientProcMapOrder) {
+        std::vector<const char*> names = dawn_wire::client::GetProcMapNamesForTesting();
+        for (size_t i = 1; i < names.size(); i++) {
+            ASSERT_LT(std::string(names[i - 1]), std::string(names[i]));
+        }
+    }
+}  // anonymous namespace