2018-07-18 09:40:26 +00:00
|
|
|
//* Copyright 2017 The Dawn Authors
|
2017-04-20 18:38:20 +00:00
|
|
|
//*
|
|
|
|
//* 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.
|
|
|
|
|
2019-01-15 20:49:53 +00:00
|
|
|
#include "dawn_wire/WireCmd_autogen.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-06-08 16:30:50 +00:00
|
|
|
#include "common/Assert.h"
|
2021-02-01 16:48:18 +00:00
|
|
|
#include "common/Log.h"
|
2021-02-25 20:21:25 +00:00
|
|
|
#include "dawn_wire/BufferConsumer_impl.h"
|
2019-10-20 03:01:56 +00:00
|
|
|
#include "dawn_wire/Wire.h"
|
2018-06-08 16:30:50 +00:00
|
|
|
|
2019-10-07 20:38:47 +00:00
|
|
|
#include <algorithm>
|
2018-06-06 15:36:49 +00:00
|
|
|
#include <cstring>
|
2019-01-04 09:54:10 +00:00
|
|
|
#include <limits>
|
2018-06-06 15:36:49 +00:00
|
|
|
|
2021-09-23 17:34:53 +00:00
|
|
|
#ifdef __GNUC__
|
|
|
|
// error: 'offsetof' within non-standard-layout type 'wgpu::XXX' is conditionally-supported
|
|
|
|
#pragma GCC diagnostic ignored "-Winvalid-offsetof"
|
|
|
|
#endif
|
|
|
|
|
2018-06-08 16:30:50 +00:00
|
|
|
//* Helper macros so that the main [de]serialization functions can be written in a generic manner.
|
|
|
|
|
|
|
|
//* Outputs an rvalue that's the number of elements a pointer member points to.
|
|
|
|
{% macro member_length(member, record_accessor) -%}
|
|
|
|
{%- if member.length == "constant" -%}
|
2021-02-18 19:28:29 +00:00
|
|
|
{{member.constant_length}}u
|
2018-06-08 16:30:50 +00:00
|
|
|
{%- else -%}
|
|
|
|
{{record_accessor}}{{as_varName(member.length.name)}}
|
|
|
|
{%- endif -%}
|
|
|
|
{%- endmacro %}
|
|
|
|
|
|
|
|
//* Outputs the type that will be used on the wire for the member
|
|
|
|
{% macro member_transfer_type(member) -%}
|
|
|
|
{%- if member.type.category == "object" -%}
|
|
|
|
ObjectId
|
|
|
|
{%- elif member.type.category == "structure" -%}
|
|
|
|
{{as_cType(member.type.name)}}Transfer
|
2019-10-17 08:46:07 +00:00
|
|
|
{%- elif member.type.category == "bitmask" -%}
|
|
|
|
{{as_cType(member.type.name)}}Flags
|
2018-06-08 16:30:50 +00:00
|
|
|
{%- else -%}
|
2021-02-18 22:36:19 +00:00
|
|
|
{{ assert(as_cType(member.type.name) != "size_t") }}
|
2018-06-08 16:30:50 +00:00
|
|
|
{{as_cType(member.type.name)}}
|
|
|
|
{%- endif -%}
|
|
|
|
{%- endmacro %}
|
|
|
|
|
|
|
|
//* Outputs the size of one element of the type that will be used on the wire for the member
|
|
|
|
{% macro member_transfer_sizeof(member) -%}
|
|
|
|
sizeof({{member_transfer_type(member)}})
|
|
|
|
{%- endmacro %}
|
|
|
|
|
|
|
|
//* Outputs the serialization code to put `in` in `out`
|
|
|
|
{% macro serialize_member(member, in, out) %}
|
|
|
|
{%- if member.type.category == "object" -%}
|
2019-01-15 20:49:53 +00:00
|
|
|
{%- set Optional = "Optional" if member.optional else "" -%}
|
2021-09-10 20:36:20 +00:00
|
|
|
WIRE_TRY(provider.Get{{Optional}}Id({{in}}, &{{out}}));
|
2021-12-21 04:04:51 +00:00
|
|
|
{%- elif member.type.category == "structure" -%}
|
|
|
|
{%- if member.type.is_wire_transparent -%}
|
|
|
|
static_assert(sizeof({{out}}) == sizeof({{in}}), "Serialize memcpy size must match.");
|
|
|
|
memcpy(&{{out}}, &{{in}}, {{member_transfer_sizeof(member)}});
|
|
|
|
{%- else -%}
|
|
|
|
{%- set Provider = ", provider" if member.type.may_have_dawn_object else "" -%}
|
|
|
|
WIRE_TRY({{as_cType(member.type.name)}}Serialize({{in}}, &{{out}}, buffer{{Provider}}));
|
|
|
|
{%- endif -%}
|
2018-06-08 16:30:50 +00:00
|
|
|
{%- else -%}
|
|
|
|
{{out}} = {{in}};
|
|
|
|
{%- endif -%}
|
|
|
|
{% endmacro %}
|
|
|
|
|
|
|
|
//* Outputs the deserialization code to put `in` in `out`
|
|
|
|
{% macro deserialize_member(member, in, out) %}
|
|
|
|
{%- if member.type.category == "object" -%}
|
2019-01-15 20:49:53 +00:00
|
|
|
{%- set Optional = "Optional" if member.optional else "" -%}
|
2021-02-25 20:21:25 +00:00
|
|
|
WIRE_TRY(resolver.Get{{Optional}}FromId({{in}}, &{{out}}));
|
2019-01-15 20:49:53 +00:00
|
|
|
{%- elif member.type.category == "structure" -%}
|
2021-12-21 04:04:51 +00:00
|
|
|
{%- if member.type.is_wire_transparent -%}
|
|
|
|
static_assert(sizeof({{out}}) == sizeof({{in}}), "Deserialize memcpy size must match.");
|
|
|
|
memcpy(&{{out}}, const_cast<const {{member_transfer_type(member)}}*>(&{{in}}), {{member_transfer_sizeof(member)}});
|
|
|
|
{%- else -%}
|
|
|
|
WIRE_TRY({{as_cType(member.type.name)}}Deserialize(&{{out}}, &{{in}}, deserializeBuffer, allocator
|
|
|
|
{%- if member.type.may_have_dawn_object -%}
|
|
|
|
, resolver
|
|
|
|
{%- endif -%}
|
|
|
|
));
|
|
|
|
{%- endif -%}
|
2018-06-08 16:30:50 +00:00
|
|
|
{%- else -%}
|
2021-02-18 22:36:19 +00:00
|
|
|
static_assert(sizeof({{out}}) >= sizeof({{in}}), "Deserialize assignment may not narrow.");
|
2018-06-08 16:30:50 +00:00
|
|
|
{{out}} = {{in}};
|
|
|
|
{%- endif -%}
|
|
|
|
{% endmacro %}
|
|
|
|
|
|
|
|
//* The main [de]serialization macro
|
|
|
|
//* Methods are very similar to structures that have one member corresponding to each arguments.
|
|
|
|
//* This macro takes advantage of the similarity to output [de]serialization code for a record
|
|
|
|
//* that is either a structure or a method, with some special cases for each.
|
2019-04-01 21:04:17 +00:00
|
|
|
{% macro write_record_serialization_helpers(record, name, members, is_cmd=False, is_return_command=False) %}
|
2018-06-08 16:30:50 +00:00
|
|
|
{% set Return = "Return" if is_return_command else "" %}
|
2019-01-15 20:49:53 +00:00
|
|
|
{% set Cmd = "Cmd" if is_cmd else "" %}
|
2020-10-13 22:35:34 +00:00
|
|
|
{% set Inherits = " : CmdHeader" if is_cmd else "" %}
|
2018-06-08 16:30:50 +00:00
|
|
|
|
|
|
|
//* Structure for the wire format of each of the records. Members that are values
|
|
|
|
//* are embedded directly in the structure. Other members are assumed to be in the
|
|
|
|
//* memory directly following the structure in the buffer.
|
2020-10-13 22:35:34 +00:00
|
|
|
struct {{Return}}{{name}}Transfer{{Inherits}} {
|
2020-04-03 17:37:48 +00:00
|
|
|
static_assert({{[is_cmd, record.extensible, record.chained].count(True)}} <= 1,
|
|
|
|
"Record must be at most one of is_cmd, extensible, and chained.");
|
2019-01-15 20:49:53 +00:00
|
|
|
{% if is_cmd %}
|
2018-06-08 16:30:50 +00:00
|
|
|
//* Start the transfer structure with the command ID, so that casting to WireCmd gives the ID.
|
2018-07-26 13:07:57 +00:00
|
|
|
{{Return}}WireCmd commandId;
|
2020-04-03 17:37:48 +00:00
|
|
|
{% elif record.extensible %}
|
|
|
|
bool hasNextInChain;
|
|
|
|
{% elif record.chained %}
|
|
|
|
WGPUChainedStructTransfer chain;
|
2018-06-08 16:30:50 +00:00
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
//* Value types are directly in the command, objects being replaced with their IDs.
|
|
|
|
{% for member in members if member.annotation == "value" %}
|
|
|
|
{{member_transfer_type(member)}} {{as_varName(member.name)}};
|
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
//* const char* have their length embedded directly in the command.
|
|
|
|
{% for member in members if member.length == "strlen" %}
|
2021-02-18 22:36:19 +00:00
|
|
|
uint64_t {{as_varName(member.name)}}Strlen;
|
2018-06-08 16:30:50 +00:00
|
|
|
{% endfor %}
|
2019-02-13 10:15:38 +00:00
|
|
|
|
2019-10-02 06:35:08 +00:00
|
|
|
{% for member in members if member.optional and member.annotation != "value" and member.type.category != "object" %}
|
2019-02-13 10:15:38 +00:00
|
|
|
bool has_{{as_varName(member.name)}};
|
|
|
|
{% endfor %}
|
2018-06-08 16:30:50 +00:00
|
|
|
};
|
|
|
|
|
2020-10-13 22:35:34 +00:00
|
|
|
{% if is_cmd %}
|
|
|
|
static_assert(offsetof({{Return}}{{name}}Transfer, commandSize) == 0, "");
|
|
|
|
static_assert(offsetof({{Return}}{{name}}Transfer, commandId) == sizeof(CmdHeader), "");
|
|
|
|
{% endif %}
|
|
|
|
|
2020-04-03 17:37:48 +00:00
|
|
|
{% if record.chained %}
|
|
|
|
static_assert(offsetof({{Return}}{{name}}Transfer, chain) == 0, "");
|
|
|
|
{% endif %}
|
|
|
|
|
2018-06-08 16:30:50 +00:00
|
|
|
//* Returns the required transfer size for `record` in addition to the transfer structure.
|
2019-01-15 20:49:53 +00:00
|
|
|
DAWN_DECLARE_UNUSED size_t {{Return}}{{name}}GetExtraRequiredSize(const {{Return}}{{name}}{{Cmd}}& record) {
|
2018-07-18 11:37:54 +00:00
|
|
|
DAWN_UNUSED(record);
|
2018-06-08 16:30:50 +00:00
|
|
|
|
|
|
|
size_t result = 0;
|
|
|
|
|
2020-04-03 17:37:48 +00:00
|
|
|
//* Gather how much space will be needed for the extension chain.
|
|
|
|
{% if record.extensible %}
|
|
|
|
if (record.nextInChain != nullptr) {
|
|
|
|
result += GetChainedStructExtraRequiredSize(record.nextInChain);
|
|
|
|
}
|
|
|
|
{% endif %}
|
|
|
|
|
2018-06-08 16:30:50 +00:00
|
|
|
//* Special handling of const char* that have their length embedded directly in the command
|
|
|
|
{% for member in members if member.length == "strlen" %}
|
2019-09-27 21:25:43 +00:00
|
|
|
{% set memberName = as_varName(member.name) %}
|
|
|
|
|
|
|
|
{% if member.optional %}
|
|
|
|
bool has_{{memberName}} = record.{{memberName}} != nullptr;
|
|
|
|
if (has_{{memberName}})
|
|
|
|
{% endif %}
|
|
|
|
{
|
|
|
|
result += std::strlen(record.{{memberName}});
|
|
|
|
}
|
2018-06-08 16:30:50 +00:00
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
//* Gather how much space will be needed for pointer members.
|
2019-09-05 09:35:07 +00:00
|
|
|
{% for member in members if member.length != "strlen" and not member.skip_serialize %}
|
2019-02-13 10:15:38 +00:00
|
|
|
{% if member.type.category != "object" and member.optional %}
|
|
|
|
if (record.{{as_varName(member.name)}} != nullptr)
|
|
|
|
{% endif %}
|
2018-06-08 16:30:50 +00:00
|
|
|
{
|
2019-09-05 09:35:07 +00:00
|
|
|
{% if member.annotation != "value" %}
|
2021-02-18 19:28:29 +00:00
|
|
|
auto memberLength = {{member_length(member, "record.")}};
|
2019-09-05 09:35:07 +00:00
|
|
|
result += memberLength * {{member_transfer_sizeof(member)}};
|
|
|
|
//* Structures might contain more pointers so we need to add their extra size as well.
|
|
|
|
{% if member.type.category == "structure" %}
|
2021-02-18 19:28:29 +00:00
|
|
|
for (decltype(memberLength) i = 0; i < memberLength; ++i) {
|
2021-12-09 02:43:28 +00:00
|
|
|
{{assert(member.annotation == "const*")}}
|
|
|
|
result += {{as_cType(member.type.name)}}GetExtraRequiredSize(record.{{as_varName(member.name)}}[i]);
|
2019-09-05 09:35:07 +00:00
|
|
|
}
|
|
|
|
{% endif %}
|
|
|
|
{% elif member.type.category == "structure" %}
|
|
|
|
result += {{as_cType(member.type.name)}}GetExtraRequiredSize(record.{{as_varName(member.name)}});
|
2018-06-08 16:30:50 +00:00
|
|
|
{% endif %}
|
|
|
|
}
|
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2018-09-17 22:59:08 +00:00
|
|
|
// GetExtraRequiredSize isn't used for structures that are value members of other structures
|
|
|
|
// because we assume they cannot contain pointers themselves.
|
2019-01-15 20:49:53 +00:00
|
|
|
DAWN_UNUSED_FUNC({{Return}}{{name}}GetExtraRequiredSize);
|
2018-06-08 16:30:50 +00:00
|
|
|
|
|
|
|
//* Serializes `record` into `transfer`, using `buffer` to get more space for pointed-to data
|
|
|
|
//* and `provider` to serialize objects.
|
2021-11-15 17:58:34 +00:00
|
|
|
DAWN_DECLARE_UNUSED WireResult {{Return}}{{name}}Serialize(
|
|
|
|
const {{Return}}{{name}}{{Cmd}}& record,
|
|
|
|
{{Return}}{{name}}Transfer* transfer,
|
|
|
|
SerializeBuffer* buffer
|
2020-04-03 17:37:48 +00:00
|
|
|
{%- if record.may_have_dawn_object -%}
|
2019-01-15 20:49:53 +00:00
|
|
|
, const ObjectIdProvider& provider
|
|
|
|
{%- endif -%}
|
|
|
|
) {
|
2018-07-18 11:37:54 +00:00
|
|
|
DAWN_UNUSED(buffer);
|
2018-06-08 16:30:50 +00:00
|
|
|
|
|
|
|
//* Handle special transfer members of methods.
|
2019-01-15 20:49:53 +00:00
|
|
|
{% if is_cmd %}
|
2018-07-26 13:07:57 +00:00
|
|
|
transfer->commandId = {{Return}}WireCmd::{{name}};
|
2018-06-08 16:30:50 +00:00
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
//* Value types are directly in the transfer record, objects being replaced with their IDs.
|
|
|
|
{% for member in members if member.annotation == "value" %}
|
|
|
|
{% set memberName = as_varName(member.name) %}
|
|
|
|
{{serialize_member(member, "record." + memberName, "transfer->" + memberName)}}
|
|
|
|
{% endfor %}
|
|
|
|
|
2020-04-03 17:37:48 +00:00
|
|
|
{% if record.extensible %}
|
|
|
|
if (record.nextInChain != nullptr) {
|
|
|
|
transfer->hasNextInChain = true;
|
2021-02-25 20:21:25 +00:00
|
|
|
WIRE_TRY(SerializeChainedStruct(record.nextInChain, buffer, provider));
|
2020-04-03 17:37:48 +00:00
|
|
|
} else {
|
|
|
|
transfer->hasNextInChain = false;
|
|
|
|
}
|
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
{% if record.chained %}
|
|
|
|
//* Should be set by the root descriptor's call to SerializeChainedStruct.
|
|
|
|
ASSERT(transfer->chain.sType == {{as_cEnum(types["s type"].name, record.name)}});
|
|
|
|
ASSERT(transfer->chain.hasNext == (record.chain.next != nullptr));
|
|
|
|
{% endif %}
|
|
|
|
|
2018-06-08 16:30:50 +00:00
|
|
|
//* Special handling of const char* that have their length embedded directly in the command
|
|
|
|
{% for member in members if member.length == "strlen" %}
|
|
|
|
{% set memberName = as_varName(member.name) %}
|
2019-09-27 21:25:43 +00:00
|
|
|
|
|
|
|
{% if member.optional %}
|
2019-10-02 06:35:08 +00:00
|
|
|
bool has_{{memberName}} = record.{{memberName}} != nullptr;
|
|
|
|
transfer->has_{{memberName}} = has_{{memberName}};
|
2019-09-27 21:25:43 +00:00
|
|
|
if (has_{{memberName}})
|
|
|
|
{% endif %}
|
|
|
|
{
|
2021-02-17 22:14:56 +00:00
|
|
|
transfer->{{memberName}}Strlen = std::strlen(record.{{memberName}});
|
2018-06-08 16:30:50 +00:00
|
|
|
|
2021-02-17 22:14:56 +00:00
|
|
|
char* stringInBuffer;
|
2021-02-25 20:21:25 +00:00
|
|
|
WIRE_TRY(buffer->NextN(transfer->{{memberName}}Strlen, &stringInBuffer));
|
2021-02-17 22:14:56 +00:00
|
|
|
memcpy(stringInBuffer, record.{{memberName}}, transfer->{{memberName}}Strlen);
|
2019-09-27 21:25:43 +00:00
|
|
|
}
|
2018-06-08 16:30:50 +00:00
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
//* Allocate space and write the non-value arguments in it.
|
2019-07-19 16:01:48 +00:00
|
|
|
{% for member in members if member.annotation != "value" and member.length != "strlen" and not member.skip_serialize %}
|
2018-06-08 16:30:50 +00:00
|
|
|
{% set memberName = as_varName(member.name) %}
|
2019-02-13 10:15:38 +00:00
|
|
|
|
|
|
|
{% if member.type.category != "object" and member.optional %}
|
|
|
|
bool has_{{memberName}} = record.{{memberName}} != nullptr;
|
|
|
|
transfer->has_{{memberName}} = has_{{memberName}};
|
|
|
|
if (has_{{memberName}})
|
|
|
|
{% endif %}
|
2018-06-08 16:30:50 +00:00
|
|
|
{
|
2021-02-18 19:28:29 +00:00
|
|
|
auto memberLength = {{member_length(member, "record.")}};
|
2021-02-17 22:14:56 +00:00
|
|
|
|
|
|
|
{{member_transfer_type(member)}}* memberBuffer;
|
2021-02-25 20:21:25 +00:00
|
|
|
WIRE_TRY(buffer->NextN(memberLength, &memberBuffer));
|
2019-09-05 09:35:07 +00:00
|
|
|
|
2021-12-02 07:29:41 +00:00
|
|
|
{% if member.type.is_wire_transparent %}
|
|
|
|
memcpy(
|
|
|
|
memberBuffer, record.{{memberName}},
|
|
|
|
{{member_transfer_sizeof(member)}} * memberLength);
|
|
|
|
{% else %}
|
|
|
|
//* This loop cannot overflow because it iterates up to |memberLength|. Even if
|
|
|
|
//* memberLength were the maximum integer value, |i| would become equal to it
|
|
|
|
//* just before exiting the loop, but not increment past or wrap around.
|
|
|
|
for (decltype(memberLength) i = 0; i < memberLength; ++i) {
|
|
|
|
{{serialize_member(member, "record." + memberName + "[i]", "memberBuffer[i]" )}}
|
|
|
|
}
|
|
|
|
{% endif %}
|
2018-06-08 16:30:50 +00:00
|
|
|
}
|
|
|
|
{% endfor %}
|
2021-02-25 20:21:25 +00:00
|
|
|
return WireResult::Success;
|
2018-06-08 16:30:50 +00:00
|
|
|
}
|
2019-05-15 18:55:22 +00:00
|
|
|
DAWN_UNUSED_FUNC({{Return}}{{name}}Serialize);
|
2018-06-08 16:30:50 +00:00
|
|
|
|
|
|
|
//* Deserializes `transfer` into `record` getting more serialized data from `buffer` and `size`
|
|
|
|
//* if needed, using `allocator` to store pointed-to values and `resolver` to translate object
|
|
|
|
//* Ids to actual objects.
|
2021-11-15 17:58:34 +00:00
|
|
|
DAWN_DECLARE_UNUSED WireResult {{Return}}{{name}}Deserialize(
|
|
|
|
{{Return}}{{name}}{{Cmd}}* record,
|
|
|
|
const volatile {{Return}}{{name}}Transfer* transfer,
|
|
|
|
DeserializeBuffer* deserializeBuffer,
|
|
|
|
DeserializeAllocator* allocator
|
2020-04-03 17:37:48 +00:00
|
|
|
{%- if record.may_have_dawn_object -%}
|
2019-01-15 20:49:53 +00:00
|
|
|
, const ObjectIdResolver& resolver
|
|
|
|
{%- endif -%}
|
|
|
|
) {
|
2018-07-18 11:37:54 +00:00
|
|
|
DAWN_UNUSED(allocator);
|
2018-06-08 16:30:50 +00:00
|
|
|
|
2019-01-15 20:49:53 +00:00
|
|
|
{% if is_cmd %}
|
2018-07-26 13:07:57 +00:00
|
|
|
ASSERT(transfer->commandId == {{Return}}WireCmd::{{name}});
|
2019-01-15 20:49:53 +00:00
|
|
|
{% endif %}
|
|
|
|
|
2019-04-01 21:04:17 +00:00
|
|
|
{% if record.derived_method %}
|
|
|
|
record->selfId = transfer->self;
|
|
|
|
{% endif %}
|
|
|
|
|
2018-06-08 16:30:50 +00:00
|
|
|
//* Value types are directly in the transfer record, objects being replaced with their IDs.
|
|
|
|
{% for member in members if member.annotation == "value" %}
|
|
|
|
{% set memberName = as_varName(member.name) %}
|
|
|
|
{{deserialize_member(member, "transfer->" + memberName, "record->" + memberName)}}
|
|
|
|
{% endfor %}
|
|
|
|
|
2020-04-03 17:37:48 +00:00
|
|
|
{% if record.extensible %}
|
|
|
|
record->nextInChain = nullptr;
|
|
|
|
if (transfer->hasNextInChain) {
|
2021-02-25 20:21:25 +00:00
|
|
|
WIRE_TRY(DeserializeChainedStruct(&record->nextInChain, deserializeBuffer, allocator, resolver));
|
2020-04-03 17:37:48 +00:00
|
|
|
}
|
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
{% if record.chained %}
|
|
|
|
//* Should be set by the root descriptor's call to DeserializeChainedStruct.
|
|
|
|
//* Don't check |record->chain.next| matches because it is not set until the
|
|
|
|
//* next iteration inside DeserializeChainedStruct.
|
|
|
|
ASSERT(record->chain.sType == {{as_cEnum(types["s type"].name, record.name)}});
|
|
|
|
ASSERT(record->chain.next == nullptr);
|
|
|
|
{% endif %}
|
|
|
|
|
2018-06-08 16:30:50 +00:00
|
|
|
//* Special handling of const char* that have their length embedded directly in the command
|
|
|
|
{% for member in members if member.length == "strlen" %}
|
|
|
|
{% set memberName = as_varName(member.name) %}
|
2019-09-27 21:25:43 +00:00
|
|
|
|
2019-10-02 06:35:08 +00:00
|
|
|
{% if member.optional %}
|
2019-10-07 20:38:47 +00:00
|
|
|
bool has_{{memberName}} = transfer->has_{{memberName}};
|
|
|
|
record->{{memberName}} = nullptr;
|
|
|
|
if (has_{{memberName}})
|
2019-10-02 06:35:08 +00:00
|
|
|
{% endif %}
|
2018-06-08 16:30:50 +00:00
|
|
|
{
|
2021-02-18 22:36:19 +00:00
|
|
|
uint64_t stringLength64 = transfer->{{memberName}}Strlen;
|
|
|
|
if (stringLength64 >= std::numeric_limits<size_t>::max()) {
|
|
|
|
//* Cannot allocate space for the string. It can be at most
|
|
|
|
//* size_t::max() - 1. We need 1 byte for the null-terminator.
|
2021-02-25 20:21:25 +00:00
|
|
|
return WireResult::FatalError;
|
2021-02-18 18:45:09 +00:00
|
|
|
}
|
2021-02-18 22:36:19 +00:00
|
|
|
size_t stringLength = static_cast<size_t>(stringLength64);
|
2021-02-18 18:45:09 +00:00
|
|
|
|
2021-02-18 19:28:29 +00:00
|
|
|
const volatile char* stringInBuffer;
|
2021-02-25 20:21:25 +00:00
|
|
|
WIRE_TRY(deserializeBuffer->ReadN(stringLength, &stringInBuffer));
|
2018-06-08 16:30:50 +00:00
|
|
|
|
2021-02-18 19:28:29 +00:00
|
|
|
char* copiedString;
|
2021-02-25 20:21:25 +00:00
|
|
|
WIRE_TRY(GetSpace(allocator, stringLength + 1, &copiedString));
|
2021-02-18 22:36:19 +00:00
|
|
|
//* We can cast away the volatile qualifier because DeserializeBuffer::ReadN already
|
|
|
|
//* validated that the range [stringInBuffer, stringInBuffer + stringLength) is valid.
|
2021-02-18 18:45:09 +00:00
|
|
|
//* memcpy may have an unknown access pattern, but this is fine since the string is only
|
|
|
|
//* data and won't affect control flow of this function.
|
|
|
|
memcpy(copiedString, const_cast<const char*>(stringInBuffer), stringLength);
|
2018-06-08 16:30:50 +00:00
|
|
|
copiedString[stringLength] = '\0';
|
|
|
|
record->{{memberName}} = copiedString;
|
|
|
|
}
|
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
//* Get extra buffer data, and copy pointed to values in extra allocated space.
|
|
|
|
{% for member in members if member.annotation != "value" and member.length != "strlen" %}
|
|
|
|
{% set memberName = as_varName(member.name) %}
|
2019-02-13 10:15:38 +00:00
|
|
|
|
|
|
|
{% if member.type.category != "object" and member.optional %}
|
2021-06-17 16:04:29 +00:00
|
|
|
//* Non-constant length optional members use length=0 to denote they aren't present.
|
|
|
|
//* Otherwise we could have length=N and has_member=false, causing reads from an
|
|
|
|
//* uninitialized pointer.
|
|
|
|
{{ assert(member.length == "constant") }}
|
2019-02-13 10:15:38 +00:00
|
|
|
bool has_{{memberName}} = transfer->has_{{memberName}};
|
|
|
|
record->{{memberName}} = nullptr;
|
|
|
|
if (has_{{memberName}})
|
|
|
|
{% endif %}
|
2018-06-08 16:30:50 +00:00
|
|
|
{
|
2021-02-18 19:28:29 +00:00
|
|
|
auto memberLength = {{member_length(member, "record->")}};
|
|
|
|
const volatile {{member_transfer_type(member)}}* memberBuffer;
|
2021-02-25 20:21:25 +00:00
|
|
|
WIRE_TRY(deserializeBuffer->ReadN(memberLength, &memberBuffer));
|
2018-06-08 16:30:50 +00:00
|
|
|
|
2021-12-10 02:05:28 +00:00
|
|
|
//* For data-only members (e.g. "data" in WriteBuffer and WriteTexture), they are
|
|
|
|
//* not security sensitive so we can directly refer the data inside the transfer
|
|
|
|
//* buffer in dawn_native. For other members, as prevention of TOCTOU attacks is an
|
|
|
|
//* important feature of the wire, we must make sure every single value returned to
|
|
|
|
//* dawn_native must be a copy of what's in the wire.
|
|
|
|
{% if member.json_data["wire_is_data_only"] %}
|
|
|
|
record->{{memberName}} =
|
|
|
|
const_cast<const {{member_transfer_type(member)}}*>(memberBuffer);
|
2018-06-08 16:30:50 +00:00
|
|
|
|
2021-12-02 07:29:41 +00:00
|
|
|
{% else %}
|
2021-12-10 02:05:28 +00:00
|
|
|
{{as_cType(member.type.name)}}* copiedMembers;
|
|
|
|
WIRE_TRY(GetSpace(allocator, memberLength, &copiedMembers));
|
|
|
|
record->{{memberName}} = copiedMembers;
|
|
|
|
|
|
|
|
{% if member.type.is_wire_transparent %}
|
|
|
|
//* memcpy is not allowed to copy from volatile objects. However, these
|
|
|
|
//* arrays are just used as plain data, and don't impact control flow. So if
|
|
|
|
//* the underlying data were changed while the copy was still executing, we
|
|
|
|
//* would get different data - but it wouldn't cause unexpected downstream
|
|
|
|
//* effects.
|
|
|
|
memcpy(
|
|
|
|
copiedMembers,
|
|
|
|
const_cast<const {{member_transfer_type(member)}}*>(memberBuffer),
|
|
|
|
{{member_transfer_sizeof(member)}} * memberLength);
|
|
|
|
{% else %}
|
|
|
|
//* This loop cannot overflow because it iterates up to |memberLength|. Even
|
|
|
|
//* if memberLength were the maximum integer value, |i| would become equal
|
|
|
|
//* to it just before exiting the loop, but not increment past or wrap
|
|
|
|
//* around.
|
|
|
|
for (decltype(memberLength) i = 0; i < memberLength; ++i) {
|
|
|
|
{{deserialize_member(member, "memberBuffer[i]", "copiedMembers[i]")}}
|
|
|
|
}
|
|
|
|
{% endif %}
|
2021-12-02 07:29:41 +00:00
|
|
|
{% endif %}
|
2018-06-08 16:30:50 +00:00
|
|
|
}
|
|
|
|
{% endfor %}
|
|
|
|
|
2021-02-25 20:21:25 +00:00
|
|
|
return WireResult::Success;
|
2018-06-08 16:30:50 +00:00
|
|
|
}
|
2019-05-15 18:55:22 +00:00
|
|
|
DAWN_UNUSED_FUNC({{Return}}{{name}}Deserialize);
|
2018-06-08 16:30:50 +00:00
|
|
|
{% endmacro %}
|
|
|
|
|
2019-01-15 20:49:53 +00:00
|
|
|
{% macro write_command_serialization_methods(command, is_return) %}
|
|
|
|
{% set Return = "Return" if is_return else "" %}
|
|
|
|
{% set Name = Return + command.name.CamelCase() %}
|
|
|
|
{% set Cmd = Name + "Cmd" %}
|
|
|
|
|
|
|
|
size_t {{Cmd}}::GetRequiredSize() const {
|
|
|
|
size_t size = sizeof({{Name}}Transfer) + {{Name}}GetExtraRequiredSize(*this);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2021-11-16 09:25:35 +00:00
|
|
|
{% if command.may_have_dawn_object %}
|
|
|
|
WireResult {{Cmd}}::Serialize(
|
|
|
|
size_t commandSize,
|
|
|
|
SerializeBuffer* buffer,
|
|
|
|
const ObjectIdProvider& provider
|
|
|
|
) const {
|
|
|
|
{{Name}}Transfer* transfer;
|
|
|
|
WIRE_TRY(buffer->Next(&transfer));
|
|
|
|
transfer->commandSize = commandSize;
|
|
|
|
return ({{Name}}Serialize(*this, transfer, buffer, provider));
|
|
|
|
}
|
|
|
|
WireResult {{Cmd}}::Serialize(size_t commandSize, SerializeBuffer* buffer) const {
|
|
|
|
ErrorObjectIdProvider provider;
|
|
|
|
return Serialize(commandSize, buffer, provider);
|
|
|
|
}
|
|
|
|
|
|
|
|
WireResult {{Cmd}}::Deserialize(
|
|
|
|
DeserializeBuffer* deserializeBuffer,
|
|
|
|
DeserializeAllocator* allocator,
|
|
|
|
const ObjectIdResolver& resolver
|
|
|
|
) {
|
|
|
|
const volatile {{Name}}Transfer* transfer;
|
|
|
|
WIRE_TRY(deserializeBuffer->Read(&transfer));
|
|
|
|
return {{Name}}Deserialize(this, transfer, deserializeBuffer, allocator, resolver);
|
|
|
|
}
|
|
|
|
WireResult {{Cmd}}::Deserialize(DeserializeBuffer* deserializeBuffer, DeserializeAllocator* allocator) {
|
|
|
|
ErrorObjectIdResolver resolver;
|
|
|
|
return Deserialize(deserializeBuffer, allocator, resolver);
|
|
|
|
}
|
|
|
|
{% else %}
|
|
|
|
WireResult {{Cmd}}::Serialize(size_t commandSize, SerializeBuffer* buffer) const {
|
|
|
|
{{Name}}Transfer* transfer;
|
|
|
|
WIRE_TRY(buffer->Next(&transfer));
|
|
|
|
transfer->commandSize = commandSize;
|
|
|
|
return ({{Name}}Serialize(*this, transfer, buffer));
|
|
|
|
}
|
|
|
|
WireResult {{Cmd}}::Serialize(
|
|
|
|
size_t commandSize,
|
|
|
|
SerializeBuffer* buffer,
|
|
|
|
const ObjectIdProvider&
|
|
|
|
) const {
|
|
|
|
return Serialize(commandSize, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
WireResult {{Cmd}}::Deserialize(DeserializeBuffer* deserializeBuffer, DeserializeAllocator* allocator) {
|
|
|
|
const volatile {{Name}}Transfer* transfer;
|
|
|
|
WIRE_TRY(deserializeBuffer->Read(&transfer));
|
|
|
|
return {{Name}}Deserialize(this, transfer, deserializeBuffer, allocator);
|
|
|
|
}
|
|
|
|
WireResult {{Cmd}}::Deserialize(
|
|
|
|
DeserializeBuffer* deserializeBuffer,
|
|
|
|
DeserializeAllocator* allocator,
|
|
|
|
const ObjectIdResolver&
|
|
|
|
) {
|
|
|
|
return Deserialize(deserializeBuffer, allocator);
|
|
|
|
}
|
|
|
|
{% endif %}
|
2019-01-15 20:49:53 +00:00
|
|
|
{% endmacro %}
|
|
|
|
|
2021-11-15 17:58:34 +00:00
|
|
|
{% macro make_chained_struct_serialization_helpers(out=None) %}
|
2021-09-17 15:36:00 +00:00
|
|
|
{% set ChainedStructPtr = "WGPUChainedStructOut*" if out else "const WGPUChainedStruct*" %}
|
|
|
|
{% set ChainedStruct = "WGPUChainedStructOut" if out else "WGPUChainedStruct" %}
|
|
|
|
size_t GetChainedStructExtraRequiredSize({{ChainedStructPtr}} chainedStruct) {
|
2020-04-03 17:37:48 +00:00
|
|
|
ASSERT(chainedStruct != nullptr);
|
|
|
|
size_t result = 0;
|
|
|
|
while (chainedStruct != nullptr) {
|
|
|
|
switch (chainedStruct->sType) {
|
2021-09-17 15:36:00 +00:00
|
|
|
{% for sType in types["s type"].values if (
|
|
|
|
sType.valid and
|
|
|
|
(sType.name.CamelCase() not in client_side_structures) and
|
|
|
|
(types[sType.name.get()].output == out)
|
|
|
|
) %}
|
2020-04-03 17:37:48 +00:00
|
|
|
case {{as_cEnum(types["s type"].name, sType.name)}}: {
|
|
|
|
const auto& typedStruct = *reinterpret_cast<{{as_cType(sType.name)}} const *>(chainedStruct);
|
|
|
|
result += sizeof({{as_cType(sType.name)}}Transfer);
|
|
|
|
result += {{as_cType(sType.name)}}GetExtraRequiredSize(typedStruct);
|
|
|
|
chainedStruct = typedStruct.chain.next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
{% endfor %}
|
2021-09-17 15:36:00 +00:00
|
|
|
// Explicitly list the Invalid enum. MSVC complains about no case labels.
|
|
|
|
case WGPUSType_Invalid:
|
2020-04-03 17:37:48 +00:00
|
|
|
default:
|
|
|
|
// Invalid enum. Reserve space just for the transfer header (sType and hasNext).
|
|
|
|
result += sizeof(WGPUChainedStructTransfer);
|
2021-02-01 16:48:18 +00:00
|
|
|
chainedStruct = chainedStruct->next;
|
|
|
|
break;
|
2020-04-03 17:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-09-17 15:36:00 +00:00
|
|
|
DAWN_NO_DISCARD WireResult SerializeChainedStruct({{ChainedStructPtr}} chainedStruct,
|
2021-02-25 20:21:25 +00:00
|
|
|
SerializeBuffer* buffer,
|
|
|
|
const ObjectIdProvider& provider) {
|
2020-04-03 17:37:48 +00:00
|
|
|
ASSERT(chainedStruct != nullptr);
|
|
|
|
ASSERT(buffer != nullptr);
|
|
|
|
do {
|
|
|
|
switch (chainedStruct->sType) {
|
2021-09-17 15:36:00 +00:00
|
|
|
{% for sType in types["s type"].values if (
|
|
|
|
sType.valid and
|
|
|
|
(sType.name.CamelCase() not in client_side_structures) and
|
|
|
|
(types[sType.name.get()].output == out)
|
|
|
|
) %}
|
2020-04-03 17:37:48 +00:00
|
|
|
{% set CType = as_cType(sType.name) %}
|
|
|
|
case {{as_cEnum(types["s type"].name, sType.name)}}: {
|
|
|
|
|
2021-02-17 22:14:56 +00:00
|
|
|
{{CType}}Transfer* transfer;
|
2021-02-25 20:21:25 +00:00
|
|
|
WIRE_TRY(buffer->Next(&transfer));
|
2020-04-03 17:37:48 +00:00
|
|
|
transfer->chain.sType = chainedStruct->sType;
|
|
|
|
transfer->chain.hasNext = chainedStruct->next != nullptr;
|
|
|
|
|
2021-02-25 20:21:25 +00:00
|
|
|
WIRE_TRY({{CType}}Serialize(*reinterpret_cast<{{CType}} const*>(chainedStruct), transfer, buffer
|
2020-04-03 17:37:48 +00:00
|
|
|
{%- if types[sType.name.get()].may_have_dawn_object -%}
|
|
|
|
, provider
|
|
|
|
{%- endif -%}
|
2021-02-17 22:14:56 +00:00
|
|
|
));
|
2020-04-03 17:37:48 +00:00
|
|
|
|
|
|
|
chainedStruct = chainedStruct->next;
|
|
|
|
} break;
|
|
|
|
{% endfor %}
|
2021-09-17 15:36:00 +00:00
|
|
|
// Explicitly list the Invalid enum. MSVC complains about no case labels.
|
|
|
|
case WGPUSType_Invalid:
|
2020-04-03 17:37:48 +00:00
|
|
|
default: {
|
|
|
|
// Invalid enum. Serialize just the transfer header with Invalid as the sType.
|
|
|
|
// TODO(crbug.com/dawn/369): Unknown sTypes are silently discarded.
|
2021-02-01 16:48:18 +00:00
|
|
|
if (chainedStruct->sType != WGPUSType_Invalid) {
|
|
|
|
dawn::WarningLog() << "Unknown sType " << chainedStruct->sType << " discarded.";
|
|
|
|
}
|
|
|
|
|
2021-02-17 22:14:56 +00:00
|
|
|
WGPUChainedStructTransfer* transfer;
|
2021-02-25 20:21:25 +00:00
|
|
|
WIRE_TRY(buffer->Next(&transfer));
|
2020-04-03 17:37:48 +00:00
|
|
|
transfer->sType = WGPUSType_Invalid;
|
2021-02-01 16:48:18 +00:00
|
|
|
transfer->hasNext = chainedStruct->next != nullptr;
|
2020-04-03 17:37:48 +00:00
|
|
|
|
2021-02-01 16:48:18 +00:00
|
|
|
// Still move on in case there are valid structs after this.
|
|
|
|
chainedStruct = chainedStruct->next;
|
|
|
|
break;
|
2020-04-03 17:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (chainedStruct != nullptr);
|
2021-02-25 20:21:25 +00:00
|
|
|
return WireResult::Success;
|
2020-04-03 17:37:48 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 15:36:00 +00:00
|
|
|
WireResult DeserializeChainedStruct({{ChainedStructPtr}}* outChainNext,
|
|
|
|
DeserializeBuffer* deserializeBuffer,
|
|
|
|
DeserializeAllocator* allocator,
|
|
|
|
const ObjectIdResolver& resolver) {
|
2020-04-03 17:37:48 +00:00
|
|
|
bool hasNext;
|
|
|
|
do {
|
2021-02-18 19:28:29 +00:00
|
|
|
const volatile WGPUChainedStructTransfer* header;
|
2021-02-25 20:21:25 +00:00
|
|
|
WIRE_TRY(deserializeBuffer->Peek(&header));
|
2021-02-18 19:28:29 +00:00
|
|
|
WGPUSType sType = header->sType;
|
2020-04-03 17:37:48 +00:00
|
|
|
switch (sType) {
|
2021-09-17 15:36:00 +00:00
|
|
|
{% for sType in types["s type"].values if (
|
|
|
|
sType.valid and
|
|
|
|
(sType.name.CamelCase() not in client_side_structures) and
|
|
|
|
(types[sType.name.get()].output == out)
|
|
|
|
) %}
|
2020-04-03 17:37:48 +00:00
|
|
|
{% set CType = as_cType(sType.name) %}
|
|
|
|
case {{as_cEnum(types["s type"].name, sType.name)}}: {
|
2021-02-18 19:28:29 +00:00
|
|
|
const volatile {{CType}}Transfer* transfer;
|
2021-02-25 20:21:25 +00:00
|
|
|
WIRE_TRY(deserializeBuffer->Read(&transfer));
|
2020-04-03 17:37:48 +00:00
|
|
|
|
2021-02-18 19:28:29 +00:00
|
|
|
{{CType}}* outStruct;
|
2021-02-25 20:21:25 +00:00
|
|
|
WIRE_TRY(GetSpace(allocator, sizeof({{CType}}), &outStruct));
|
2020-04-03 17:37:48 +00:00
|
|
|
outStruct->chain.sType = sType;
|
|
|
|
outStruct->chain.next = nullptr;
|
|
|
|
|
|
|
|
*outChainNext = &outStruct->chain;
|
|
|
|
outChainNext = &outStruct->chain.next;
|
|
|
|
|
2021-02-25 20:21:25 +00:00
|
|
|
WIRE_TRY({{CType}}Deserialize(outStruct, transfer, deserializeBuffer, allocator
|
2020-04-03 17:37:48 +00:00
|
|
|
{%- if types[sType.name.get()].may_have_dawn_object -%}
|
|
|
|
, resolver
|
|
|
|
{%- endif -%}
|
|
|
|
));
|
|
|
|
|
|
|
|
hasNext = transfer->chain.hasNext;
|
|
|
|
} break;
|
|
|
|
{% endfor %}
|
2021-09-17 15:36:00 +00:00
|
|
|
// Explicitly list the Invalid enum. MSVC complains about no case labels.
|
|
|
|
case WGPUSType_Invalid:
|
2021-02-01 16:48:18 +00:00
|
|
|
default: {
|
|
|
|
// Invalid enum. Deserialize just the transfer header with Invalid as the sType.
|
|
|
|
// TODO(crbug.com/dawn/369): Unknown sTypes are silently discarded.
|
|
|
|
if (sType != WGPUSType_Invalid) {
|
|
|
|
dawn::WarningLog() << "Unknown sType " << sType << " discarded.";
|
|
|
|
}
|
|
|
|
|
2021-02-18 19:28:29 +00:00
|
|
|
const volatile WGPUChainedStructTransfer* transfer;
|
2021-02-25 20:21:25 +00:00
|
|
|
WIRE_TRY(deserializeBuffer->Read(&transfer));
|
2021-02-01 16:48:18 +00:00
|
|
|
|
2021-09-17 15:36:00 +00:00
|
|
|
{{ChainedStruct}}* outStruct;
|
|
|
|
WIRE_TRY(GetSpace(allocator, sizeof({{ChainedStruct}}), &outStruct));
|
2021-02-01 16:48:18 +00:00
|
|
|
outStruct->sType = WGPUSType_Invalid;
|
|
|
|
outStruct->next = nullptr;
|
|
|
|
|
|
|
|
// Still move on in case there are valid structs after this.
|
|
|
|
*outChainNext = outStruct;
|
|
|
|
outChainNext = &outStruct->next;
|
|
|
|
hasNext = transfer->hasNext;
|
|
|
|
break;
|
|
|
|
}
|
2020-04-03 17:37:48 +00:00
|
|
|
}
|
|
|
|
} while (hasNext);
|
|
|
|
|
2021-02-25 20:21:25 +00:00
|
|
|
return WireResult::Success;
|
2020-04-03 17:37:48 +00:00
|
|
|
}
|
2021-09-17 15:36:00 +00:00
|
|
|
{% endmacro %}
|
|
|
|
|
2021-11-15 17:58:34 +00:00
|
|
|
namespace dawn_wire {
|
|
|
|
|
|
|
|
ObjectHandle::ObjectHandle() = default;
|
|
|
|
ObjectHandle::ObjectHandle(ObjectId id, ObjectGeneration generation)
|
|
|
|
: id(id), generation(generation) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectHandle::ObjectHandle(const volatile ObjectHandle& rhs)
|
|
|
|
: id(rhs.id), generation(rhs.generation) {
|
|
|
|
}
|
|
|
|
ObjectHandle& ObjectHandle::operator=(const volatile ObjectHandle& rhs) {
|
|
|
|
id = rhs.id;
|
|
|
|
generation = rhs.generation;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectHandle& ObjectHandle::AssignFrom(const ObjectHandle& rhs) {
|
|
|
|
id = rhs.id;
|
|
|
|
generation = rhs.generation;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
ObjectHandle& ObjectHandle::AssignFrom(const volatile ObjectHandle& rhs) {
|
|
|
|
id = rhs.id;
|
|
|
|
generation = rhs.generation;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
// Allocates enough space from allocator to countain T[count] and return it in out.
|
|
|
|
// Return FatalError if the allocator couldn't allocate the memory.
|
|
|
|
// Always writes to |out| on success.
|
|
|
|
template <typename T, typename N>
|
|
|
|
WireResult GetSpace(DeserializeAllocator* allocator, N count, T** out) {
|
|
|
|
constexpr size_t kMaxCountWithoutOverflows = std::numeric_limits<size_t>::max() / sizeof(T);
|
|
|
|
if (count > kMaxCountWithoutOverflows) {
|
|
|
|
return WireResult::FatalError;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t totalSize = sizeof(T) * count;
|
|
|
|
*out = static_cast<T*>(allocator->GetSpace(totalSize));
|
|
|
|
if (*out == nullptr) {
|
|
|
|
return WireResult::FatalError;
|
|
|
|
}
|
|
|
|
|
|
|
|
return WireResult::Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct WGPUChainedStructTransfer {
|
|
|
|
WGPUSType sType;
|
|
|
|
bool hasNext;
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t GetChainedStructExtraRequiredSize(const WGPUChainedStruct* chainedStruct);
|
|
|
|
DAWN_NO_DISCARD WireResult SerializeChainedStruct(const WGPUChainedStruct* chainedStruct,
|
|
|
|
SerializeBuffer* buffer,
|
|
|
|
const ObjectIdProvider& provider);
|
|
|
|
WireResult DeserializeChainedStruct(const WGPUChainedStruct** outChainNext,
|
|
|
|
DeserializeBuffer* deserializeBuffer,
|
|
|
|
DeserializeAllocator* allocator,
|
|
|
|
const ObjectIdResolver& resolver);
|
|
|
|
|
|
|
|
size_t GetChainedStructExtraRequiredSize(WGPUChainedStructOut* chainedStruct);
|
|
|
|
DAWN_NO_DISCARD WireResult SerializeChainedStruct(WGPUChainedStructOut* chainedStruct,
|
|
|
|
SerializeBuffer* buffer,
|
|
|
|
const ObjectIdProvider& provider);
|
|
|
|
WireResult DeserializeChainedStruct(WGPUChainedStructOut** outChainNext,
|
|
|
|
DeserializeBuffer* deserializeBuffer,
|
|
|
|
DeserializeAllocator* allocator,
|
|
|
|
const ObjectIdResolver& resolver);
|
|
|
|
|
|
|
|
//* Output structure [de]serialization first because it is used by commands.
|
|
|
|
{% for type in by_category["structure"] %}
|
|
|
|
{% set name = as_cType(type.name) %}
|
|
|
|
{% if type.name.CamelCase() not in client_side_structures %}
|
|
|
|
{{write_record_serialization_helpers(type, name, type.members, is_cmd=False)}}
|
|
|
|
{% endif %}
|
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
|
|
|
|
{{ make_chained_struct_serialization_helpers(out=False) }}
|
|
|
|
{{ make_chained_struct_serialization_helpers(out=True) }}
|
2020-04-03 17:37:48 +00:00
|
|
|
|
2019-01-15 20:49:53 +00:00
|
|
|
//* Output [de]serialization helpers for commands
|
|
|
|
{% for command in cmd_records["command"] %}
|
|
|
|
{% set name = command.name.CamelCase() %}
|
2021-11-15 17:58:34 +00:00
|
|
|
{{write_record_serialization_helpers(command, name, command.members, is_cmd=True)}}
|
2019-01-15 20:49:53 +00:00
|
|
|
{% endfor %}
|
2018-06-06 15:36:49 +00:00
|
|
|
|
2019-01-15 20:49:53 +00:00
|
|
|
//* Output [de]serialization helpers for return commands
|
|
|
|
{% for command in cmd_records["return command"] %}
|
|
|
|
{% set name = command.name.CamelCase() %}
|
|
|
|
{{write_record_serialization_helpers(command, name, command.members,
|
2021-11-15 17:58:34 +00:00
|
|
|
is_cmd=True, is_return_command=True)}}
|
2018-06-08 16:30:50 +00:00
|
|
|
{% endfor %}
|
2021-11-15 17:58:34 +00:00
|
|
|
|
|
|
|
// Implementation of ObjectIdResolver that always errors.
|
|
|
|
// Used when the generator adds a provider argument because of a chained
|
|
|
|
// struct, but in practice, a chained struct in that location is invalid.
|
|
|
|
class ErrorObjectIdResolver final : public ObjectIdResolver {
|
|
|
|
public:
|
|
|
|
{% for type in by_category["object"] %}
|
|
|
|
WireResult GetFromId(ObjectId id, {{as_cType(type.name)}}* out) const override {
|
|
|
|
return WireResult::FatalError;
|
|
|
|
}
|
|
|
|
WireResult GetOptionalFromId(ObjectId id, {{as_cType(type.name)}}* out) const override {
|
|
|
|
return WireResult::FatalError;
|
|
|
|
}
|
|
|
|
{% endfor %}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Implementation of ObjectIdProvider that always errors.
|
|
|
|
// Used when the generator adds a provider argument because of a chained
|
|
|
|
// struct, but in practice, a chained struct in that location is invalid.
|
|
|
|
class ErrorObjectIdProvider final : public ObjectIdProvider {
|
|
|
|
public:
|
|
|
|
{% for type in by_category["object"] %}
|
|
|
|
WireResult GetId({{as_cType(type.name)}} object, ObjectId* out) const override {
|
|
|
|
return WireResult::FatalError;
|
|
|
|
}
|
|
|
|
WireResult GetOptionalId({{as_cType(type.name)}} object, ObjectId* out) const override {
|
|
|
|
return WireResult::FatalError;
|
|
|
|
}
|
|
|
|
{% endfor %}
|
|
|
|
};
|
|
|
|
|
2018-06-08 16:30:50 +00:00
|
|
|
} // anonymous namespace
|
2018-06-06 15:36:49 +00:00
|
|
|
|
2019-01-15 20:49:53 +00:00
|
|
|
{% for command in cmd_records["command"] %}
|
|
|
|
{{ write_command_serialization_methods(command, False) }}
|
|
|
|
{% endfor %}
|
2018-06-06 15:36:49 +00:00
|
|
|
|
2019-01-15 20:49:53 +00:00
|
|
|
{% for command in cmd_records["return command"] %}
|
|
|
|
{{ write_command_serialization_methods(command, True) }}
|
2017-04-20 18:43:11 +00:00
|
|
|
{% endfor %}
|
|
|
|
|
2021-11-15 17:58:34 +00:00
|
|
|
// Implementations of serialization/deserialization of WPGUDeviceProperties.
|
|
|
|
size_t SerializedWGPUDevicePropertiesSize(const WGPUDeviceProperties* deviceProperties) {
|
|
|
|
return sizeof(WGPUDeviceProperties) +
|
|
|
|
WGPUDevicePropertiesGetExtraRequiredSize(*deviceProperties);
|
|
|
|
}
|
2021-09-10 20:36:20 +00:00
|
|
|
|
2021-11-15 17:58:34 +00:00
|
|
|
void SerializeWGPUDeviceProperties(const WGPUDeviceProperties* deviceProperties,
|
|
|
|
char* buffer) {
|
|
|
|
SerializeBuffer serializeBuffer(buffer, SerializedWGPUDevicePropertiesSize(deviceProperties));
|
2019-10-20 03:01:56 +00:00
|
|
|
|
2021-11-15 17:58:34 +00:00
|
|
|
WGPUDevicePropertiesTransfer* transfer;
|
2021-02-17 22:14:56 +00:00
|
|
|
|
2021-11-15 17:58:34 +00:00
|
|
|
WireResult result = serializeBuffer.Next(&transfer);
|
|
|
|
ASSERT(result == WireResult::Success);
|
2021-02-25 20:21:25 +00:00
|
|
|
|
2021-11-15 17:58:34 +00:00
|
|
|
ErrorObjectIdProvider provider;
|
|
|
|
result = WGPUDevicePropertiesSerialize(*deviceProperties, transfer, &serializeBuffer, provider);
|
|
|
|
ASSERT(result == WireResult::Success);
|
|
|
|
}
|
2021-02-25 20:21:25 +00:00
|
|
|
|
2021-11-15 17:58:34 +00:00
|
|
|
bool DeserializeWGPUDeviceProperties(WGPUDeviceProperties* deviceProperties,
|
|
|
|
const volatile char* buffer,
|
|
|
|
size_t size) {
|
|
|
|
const volatile WGPUDevicePropertiesTransfer* transfer;
|
|
|
|
DeserializeBuffer deserializeBuffer(buffer, size);
|
|
|
|
if (deserializeBuffer.Read(&transfer) != WireResult::Success) {
|
|
|
|
return false;
|
2019-10-20 03:01:56 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 17:58:34 +00:00
|
|
|
ErrorObjectIdResolver resolver;
|
|
|
|
return WGPUDevicePropertiesDeserialize(deviceProperties, transfer, &deserializeBuffer,
|
|
|
|
nullptr, resolver) == WireResult::Success;
|
|
|
|
}
|
2019-10-20 03:01:56 +00:00
|
|
|
|
2021-11-15 17:58:34 +00:00
|
|
|
size_t SerializedWGPUSupportedLimitsSize(const WGPUSupportedLimits* supportedLimits) {
|
|
|
|
return sizeof(WGPUSupportedLimits) +
|
|
|
|
WGPUSupportedLimitsGetExtraRequiredSize(*supportedLimits);
|
|
|
|
}
|
2019-10-20 03:01:56 +00:00
|
|
|
|
2021-11-15 17:58:34 +00:00
|
|
|
void SerializeWGPUSupportedLimits(
|
|
|
|
const WGPUSupportedLimits* supportedLimits,
|
|
|
|
char* buffer) {
|
|
|
|
SerializeBuffer serializeBuffer(buffer, SerializedWGPUSupportedLimitsSize(supportedLimits));
|
2021-09-17 15:36:00 +00:00
|
|
|
|
2021-11-15 17:58:34 +00:00
|
|
|
WGPUSupportedLimitsTransfer* transfer;
|
2021-09-17 15:36:00 +00:00
|
|
|
|
2021-11-15 17:58:34 +00:00
|
|
|
WireResult result = serializeBuffer.Next(&transfer);
|
|
|
|
ASSERT(result == WireResult::Success);
|
2021-09-17 15:36:00 +00:00
|
|
|
|
2021-11-15 17:58:34 +00:00
|
|
|
ErrorObjectIdProvider provider;
|
|
|
|
result = WGPUSupportedLimitsSerialize(*supportedLimits, transfer, &serializeBuffer, provider);
|
|
|
|
ASSERT(result == WireResult::Success);
|
|
|
|
}
|
2021-09-17 15:36:00 +00:00
|
|
|
|
2021-11-15 17:58:34 +00:00
|
|
|
bool DeserializeWGPUSupportedLimits(WGPUSupportedLimits* supportedLimits,
|
|
|
|
const volatile char* buffer,
|
|
|
|
size_t size) {
|
|
|
|
const volatile WGPUSupportedLimitsTransfer* transfer;
|
|
|
|
DeserializeBuffer deserializeBuffer(buffer, size);
|
|
|
|
if (deserializeBuffer.Read(&transfer) != WireResult::Success) {
|
|
|
|
return false;
|
2021-09-17 15:36:00 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 17:58:34 +00:00
|
|
|
ErrorObjectIdResolver resolver;
|
|
|
|
return WGPUSupportedLimitsDeserialize(supportedLimits, transfer, &deserializeBuffer,
|
|
|
|
nullptr, resolver) == WireResult::Success;
|
|
|
|
}
|
2021-09-17 15:36:00 +00:00
|
|
|
|
2018-07-26 13:07:57 +00:00
|
|
|
} // namespace dawn_wire
|