2019-01-16 02:18:06 +00:00
|
|
|
//* 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.
|
|
|
|
|
2020-04-15 17:58:25 +00:00
|
|
|
#include "common/Log.h"
|
2019-01-16 02:18:06 +00:00
|
|
|
#include "dawn_wire/client/ApiObjects.h"
|
|
|
|
#include "dawn_wire/client/Client.h"
|
|
|
|
|
2019-10-16 15:36:12 +00:00
|
|
|
#include <algorithm>
|
2020-02-18 02:44:05 +00:00
|
|
|
#include <cstring>
|
2019-10-15 12:08:48 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2019-01-16 02:18:06 +00:00
|
|
|
namespace dawn_wire { namespace client {
|
2020-04-15 17:58:25 +00:00
|
|
|
|
2021-02-05 21:08:44 +00:00
|
|
|
//* Outputs an rvalue that's the number of elements a pointer member points to.
|
|
|
|
{% macro member_length(member, accessor) -%}
|
|
|
|
{%- if member.length == "constant" -%}
|
|
|
|
{{member.constant_length}}
|
|
|
|
{%- else -%}
|
|
|
|
{{accessor}}{{as_varName(member.length.name)}}
|
|
|
|
{%- endif -%}
|
|
|
|
{%- endmacro %}
|
2020-04-15 17:58:25 +00:00
|
|
|
|
2019-01-16 02:18:06 +00:00
|
|
|
//* Implementation of the client API functions.
|
|
|
|
{% for type in by_category["object"] %}
|
|
|
|
{% set Type = type.name.CamelCase() %}
|
|
|
|
{% set cType = as_cType(type.name) %}
|
|
|
|
|
|
|
|
{% for method in type.methods %}
|
|
|
|
{% set Suffix = as_MethodSuffix(type.name, method.name) %}
|
2020-04-15 17:58:25 +00:00
|
|
|
|
|
|
|
{% if Suffix in client_handwritten_commands %}
|
|
|
|
static
|
|
|
|
{% endif %}
|
|
|
|
{{as_cType(method.return_type.name)}} Client{{Suffix}}(
|
|
|
|
{{-cType}} cSelf
|
|
|
|
{%- for arg in method.arguments -%}
|
|
|
|
, {{as_annotated_cType(arg)}}
|
|
|
|
{%- endfor -%}
|
|
|
|
) {
|
2020-06-30 18:01:50 +00:00
|
|
|
auto self = reinterpret_cast<{{as_wireType(type)}}>(cSelf);
|
2020-04-15 17:58:25 +00:00
|
|
|
{% if Suffix not in client_handwritten_commands %}
|
2019-01-16 02:18:06 +00:00
|
|
|
{{Suffix}}Cmd cmd;
|
|
|
|
|
|
|
|
//* Create the structure going on the wire on the stack and fill it with the value
|
|
|
|
//* arguments so it can compute its size.
|
|
|
|
cmd.self = cSelf;
|
|
|
|
|
|
|
|
//* For object creation, store the object ID the client will use for the result.
|
|
|
|
{% if method.return_type.category == "object" %}
|
2021-01-13 18:31:47 +00:00
|
|
|
auto* allocation = self->client->{{method.return_type.name.CamelCase()}}Allocator().New(self->client);
|
2020-04-13 17:50:51 +00:00
|
|
|
cmd.result = ObjectHandle{allocation->object->id, allocation->generation};
|
2019-01-16 02:18:06 +00:00
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
{% for arg in method.arguments %}
|
2021-12-15 00:12:30 +00:00
|
|
|
//* Commands with mutable pointers should not be autogenerated.
|
|
|
|
{{assert(arg.annotation != "*")}}
|
2019-01-16 02:18:06 +00:00
|
|
|
cmd.{{as_varName(arg.name)}} = {{as_varName(arg.name)}};
|
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
//* Allocate space to send the command and copy the value args over.
|
2021-01-13 18:31:47 +00:00
|
|
|
self->client->SerializeCommand(cmd);
|
2019-01-16 02:18:06 +00:00
|
|
|
|
|
|
|
{% if method.return_type.category == "object" %}
|
|
|
|
return reinterpret_cast<{{as_cType(method.return_type.name)}}>(allocation->object.get());
|
|
|
|
{% endif %}
|
2020-04-15 17:58:25 +00:00
|
|
|
{% else %}
|
2020-06-30 18:01:50 +00:00
|
|
|
return self->{{method.name.CamelCase()}}(
|
2020-04-15 17:58:25 +00:00
|
|
|
{%- for arg in method.arguments -%}
|
2020-06-30 18:01:50 +00:00
|
|
|
{%if not loop.first %}, {% endif %} {{as_varName(arg.name)}}
|
2020-04-15 17:58:25 +00:00
|
|
|
{%- endfor -%});
|
|
|
|
{% endif %}
|
|
|
|
}
|
2019-01-16 02:18:06 +00:00
|
|
|
{% endfor %}
|
|
|
|
|
2021-01-13 19:23:48 +00:00
|
|
|
//* When an object's refcount reaches 0, notify the server side of it and delete it.
|
|
|
|
void Client{{as_MethodSuffix(type.name, Name("release"))}}({{cType}} cObj) {
|
|
|
|
{{Type}}* obj = reinterpret_cast<{{Type}}*>(cObj);
|
|
|
|
obj->refcount --;
|
2019-01-16 02:18:06 +00:00
|
|
|
|
2021-01-13 19:23:48 +00:00
|
|
|
if (obj->refcount > 0) {
|
|
|
|
return;
|
2019-01-16 02:18:06 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 19:23:48 +00:00
|
|
|
DestroyObjectCmd cmd;
|
|
|
|
cmd.objectType = ObjectType::{{type.name.CamelCase()}};
|
|
|
|
cmd.objectId = obj->id;
|
|
|
|
|
|
|
|
obj->client->SerializeCommand(cmd);
|
|
|
|
obj->client->{{type.name.CamelCase()}}Allocator().Free(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Client{{as_MethodSuffix(type.name, Name("reference"))}}({{cType}} cObj) {
|
|
|
|
{{Type}}* obj = reinterpret_cast<{{Type}}*>(cObj);
|
|
|
|
obj->refcount ++;
|
|
|
|
}
|
2019-01-16 02:18:06 +00:00
|
|
|
{% endfor %}
|
|
|
|
|
2019-10-15 12:08:48 +00:00
|
|
|
namespace {
|
2020-01-10 13:06:48 +00:00
|
|
|
WGPUInstance ClientCreateInstance(WGPUInstanceDescriptor const* descriptor) {
|
|
|
|
UNREACHABLE();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-10-15 12:08:48 +00:00
|
|
|
struct ProcEntry {
|
2019-10-24 22:27:27 +00:00
|
|
|
WGPUProc proc;
|
2019-10-15 12:08:48 +00:00
|
|
|
const char* name;
|
|
|
|
};
|
|
|
|
static const ProcEntry sProcMap[] = {
|
2019-11-22 14:02:52 +00:00
|
|
|
{% for (type, method) in c_methods_sorted_by_name %}
|
2019-10-24 22:27:27 +00:00
|
|
|
{ reinterpret_cast<WGPUProc>(Client{{as_MethodSuffix(type.name, method.name)}}), "{{as_cMethod(type.name, method.name)}}" },
|
2019-10-15 12:08:48 +00:00
|
|
|
{% endfor %}
|
|
|
|
};
|
|
|
|
static constexpr size_t sProcMapSize = sizeof(sProcMap) / sizeof(sProcMap[0]);
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2019-10-24 22:27:27 +00:00
|
|
|
WGPUProc ClientGetProcAddress(WGPUDevice, const char* procName) {
|
2019-10-15 12:08:48 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-01-10 13:06:48 +00:00
|
|
|
// Special case the two free-standing functions of the API.
|
2019-10-21 20:04:10 +00:00
|
|
|
if (strcmp(procName, "wgpuGetProcAddress") == 0) {
|
2019-10-24 22:27:27 +00:00
|
|
|
return reinterpret_cast<WGPUProc>(ClientGetProcAddress);
|
2019-10-15 12:08:48 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 13:06:48 +00:00
|
|
|
if (strcmp(procName, "wgpuCreateInstance") == 0) {
|
|
|
|
return reinterpret_cast<WGPUProc>(ClientCreateInstance);
|
|
|
|
}
|
|
|
|
|
2019-10-15 12:08:48 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-12-05 05:29:44 +00:00
|
|
|
{% set Prefix = metadata.proc_table_prefix %}
|
|
|
|
static {{Prefix}}ProcTable gProcTable = {
|
|
|
|
{% for function in by_category["function"] %}
|
|
|
|
Client{{as_cppType(function.name)}},
|
|
|
|
{% endfor %}
|
2019-01-16 02:18:06 +00:00
|
|
|
{% for type in by_category["object"] %}
|
2019-11-22 14:02:52 +00:00
|
|
|
{% for method in c_methods(type) %}
|
2020-10-06 16:13:42 +00:00
|
|
|
Client{{as_MethodSuffix(type.name, method.name)}},
|
2019-01-16 02:18:06 +00:00
|
|
|
{% endfor %}
|
|
|
|
{% endfor %}
|
2020-10-06 16:13:42 +00:00
|
|
|
};
|
2021-12-05 05:29:44 +00:00
|
|
|
const {{Prefix}}ProcTable& GetProcs() {
|
2020-10-06 16:13:42 +00:00
|
|
|
return gProcTable;
|
2019-01-16 02:18:06 +00:00
|
|
|
}
|
|
|
|
}} // namespace dawn_wire::client
|