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"
|
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
|
|
|
|
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" -%}
|
|
|
|
{{member.constant_length}}
|
|
|
|
{%- 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 -%}
|
|
|
|
{{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 "" -%}
|
2018-11-28 17:00:33 +00:00
|
|
|
{{out}} = provider.Get{{Optional}}Id({{in}});
|
2019-01-21 08:29:01 +00:00
|
|
|
{% elif member.type.category == "structure"%}
|
|
|
|
{%- set Provider = ", provider" if member.type.has_dawn_object else "" -%}
|
|
|
|
{% if member.annotation == "const*const*" %}
|
|
|
|
{{as_cType(member.type.name)}}Serialize(*{{in}}, &{{out}}, buffer{{Provider}});
|
|
|
|
{% else %}
|
|
|
|
{{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 "" -%}
|
2018-11-28 17:00:33 +00:00
|
|
|
DESERIALIZE_TRY(resolver.Get{{Optional}}FromId({{in}}, &{{out}}));
|
2019-01-15 20:49:53 +00:00
|
|
|
{%- elif member.type.category == "structure" -%}
|
|
|
|
DESERIALIZE_TRY({{as_cType(member.type.name)}}Deserialize(&{{out}}, &{{in}}, buffer, size, allocator
|
|
|
|
{%- if member.type.has_dawn_object -%}
|
|
|
|
, resolver
|
|
|
|
{%- endif -%}
|
|
|
|
));
|
2018-06-08 16:30:50 +00:00
|
|
|
{%- else -%}
|
|
|
|
{{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 "" %}
|
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.
|
2019-01-15 20:49:53 +00:00
|
|
|
struct {{Return}}{{name}}Transfer {
|
|
|
|
{% 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;
|
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" %}
|
|
|
|
size_t {{as_varName(member.name)}}Strlen;
|
|
|
|
{% 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
|
|
|
};
|
|
|
|
|
|
|
|
//* 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;
|
|
|
|
|
|
|
|
//* 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" %}
|
|
|
|
size_t memberLength = {{member_length(member, "record.")}};
|
|
|
|
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" %}
|
|
|
|
for (size_t i = 0; i < memberLength; ++i) {
|
|
|
|
{% if member.annotation == "const*const*" %}
|
|
|
|
result += {{as_cType(member.type.name)}}GetExtraRequiredSize(*record.{{as_varName(member.name)}}[i]);
|
|
|
|
{% else %}
|
|
|
|
{{assert(member.annotation == "const*")}}
|
|
|
|
result += {{as_cType(member.type.name)}}GetExtraRequiredSize(record.{{as_varName(member.name)}}[i]);
|
|
|
|
{% endif %}
|
|
|
|
}
|
|
|
|
{% 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.
|
2019-05-15 18:55:22 +00:00
|
|
|
DAWN_DECLARE_UNUSED void {{Return}}{{name}}Serialize(const {{Return}}{{name}}{{Cmd}}& record, {{Return}}{{name}}Transfer* transfer,
|
2019-01-15 20:49:53 +00:00
|
|
|
char** buffer
|
|
|
|
{%- if record.has_dawn_object -%}
|
|
|
|
, 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 %}
|
|
|
|
|
|
|
|
//* 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 %}
|
|
|
|
{
|
2018-06-08 16:30:50 +00:00
|
|
|
transfer->{{memberName}}Strlen = std::strlen(record.{{memberName}});
|
|
|
|
|
2018-12-05 10:13:52 +00:00
|
|
|
memcpy(*buffer, record.{{memberName}}, transfer->{{memberName}}Strlen);
|
|
|
|
*buffer += 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
|
|
|
{
|
|
|
|
size_t memberLength = {{member_length(member, "record.")}};
|
2018-12-05 10:13:52 +00:00
|
|
|
auto memberBuffer = reinterpret_cast<{{member_transfer_type(member)}}*>(*buffer);
|
|
|
|
*buffer += memberLength * {{member_transfer_sizeof(member)}};
|
2019-09-05 09:35:07 +00:00
|
|
|
|
2018-06-08 16:30:50 +00:00
|
|
|
for (size_t i = 0; i < memberLength; ++i) {
|
|
|
|
{{serialize_member(member, "record." + memberName + "[i]", "memberBuffer[i]" )}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{% endfor %}
|
|
|
|
}
|
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.
|
2019-10-07 20:38:47 +00:00
|
|
|
DAWN_DECLARE_UNUSED DeserializeResult {{Return}}{{name}}Deserialize({{Return}}{{name}}{{Cmd}}* record, const volatile {{Return}}{{name}}Transfer* transfer,
|
|
|
|
const volatile char** buffer, size_t* size, DeserializeAllocator* allocator
|
2019-01-15 20:49:53 +00:00
|
|
|
{%- if record.has_dawn_object -%}
|
|
|
|
, const ObjectIdResolver& resolver
|
|
|
|
{%- endif -%}
|
|
|
|
) {
|
2018-07-18 11:37:54 +00:00
|
|
|
DAWN_UNUSED(allocator);
|
|
|
|
DAWN_UNUSED(buffer);
|
|
|
|
DAWN_UNUSED(size);
|
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 %}
|
|
|
|
|
|
|
|
{% if record.extensible %}
|
2018-06-08 16:30:50 +00:00
|
|
|
record->nextInChain = nullptr;
|
|
|
|
{% 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 %}
|
|
|
|
|
|
|
|
//* 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
|
|
|
{
|
|
|
|
size_t stringLength = transfer->{{memberName}}Strlen;
|
2019-10-07 20:38:47 +00:00
|
|
|
const volatile char* stringInBuffer = nullptr;
|
2018-06-08 16:30:50 +00:00
|
|
|
DESERIALIZE_TRY(GetPtrFromBuffer(buffer, size, stringLength, &stringInBuffer));
|
|
|
|
|
|
|
|
char* copiedString = nullptr;
|
|
|
|
DESERIALIZE_TRY(GetSpace(allocator, stringLength + 1, &copiedString));
|
2019-10-07 20:38:47 +00:00
|
|
|
std::copy(stringInBuffer, stringInBuffer + stringLength, copiedString);
|
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 %}
|
|
|
|
bool has_{{memberName}} = transfer->has_{{memberName}};
|
|
|
|
record->{{memberName}} = nullptr;
|
|
|
|
if (has_{{memberName}})
|
|
|
|
{% endif %}
|
2018-06-08 16:30:50 +00:00
|
|
|
{
|
|
|
|
size_t memberLength = {{member_length(member, "record->")}};
|
2019-10-07 20:38:47 +00:00
|
|
|
auto memberBuffer = reinterpret_cast<const volatile {{member_transfer_type(member)}}*>(buffer);
|
2018-06-08 16:30:50 +00:00
|
|
|
DESERIALIZE_TRY(GetPtrFromBuffer(buffer, size, memberLength, &memberBuffer));
|
|
|
|
|
|
|
|
{{as_cType(member.type.name)}}* copiedMembers = nullptr;
|
|
|
|
DESERIALIZE_TRY(GetSpace(allocator, memberLength, &copiedMembers));
|
2019-01-21 08:29:01 +00:00
|
|
|
{% if member.annotation == "const*const*" %}
|
|
|
|
{{as_cType(member.type.name)}}** pointerArray = nullptr;
|
|
|
|
DESERIALIZE_TRY(GetSpace(allocator, memberLength, &pointerArray));
|
|
|
|
for (size_t i = 0; i < memberLength; ++i) {
|
|
|
|
pointerArray[i] = &copiedMembers[i];
|
|
|
|
}
|
|
|
|
record->{{memberName}} = pointerArray;
|
|
|
|
{% else %}
|
|
|
|
record->{{memberName}} = copiedMembers;
|
|
|
|
{% endif %}
|
2018-06-08 16:30:50 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < memberLength; ++i) {
|
|
|
|
{{deserialize_member(member, "memberBuffer[i]", "copiedMembers[i]")}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
return DeserializeResult::Success;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void {{Cmd}}::Serialize(char* buffer
|
|
|
|
{%- if command.has_dawn_object -%}
|
|
|
|
, const ObjectIdProvider& objectIdProvider
|
|
|
|
{%- endif -%}
|
|
|
|
) const {
|
|
|
|
auto transfer = reinterpret_cast<{{Name}}Transfer*>(buffer);
|
|
|
|
buffer += sizeof({{Name}}Transfer);
|
|
|
|
|
|
|
|
{{Name}}Serialize(*this, transfer, &buffer
|
|
|
|
{%- if command.has_dawn_object -%}
|
|
|
|
, objectIdProvider
|
|
|
|
{%- endif -%}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-07 20:38:47 +00:00
|
|
|
DeserializeResult {{Cmd}}::Deserialize(const volatile char** buffer, size_t* size, DeserializeAllocator* allocator
|
2019-01-15 20:49:53 +00:00
|
|
|
{%- if command.has_dawn_object -%}
|
|
|
|
, const ObjectIdResolver& resolver
|
|
|
|
{%- endif -%}
|
|
|
|
) {
|
2019-10-07 20:38:47 +00:00
|
|
|
const volatile {{Name}}Transfer* transfer = nullptr;
|
2019-01-15 20:49:53 +00:00
|
|
|
DESERIALIZE_TRY(GetPtrFromBuffer(buffer, size, 1, &transfer));
|
|
|
|
|
|
|
|
return {{Name}}Deserialize(this, transfer, buffer, size, allocator
|
|
|
|
{%- if command.has_dawn_object -%}
|
|
|
|
, resolver
|
|
|
|
{%- endif -%}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
{% endmacro %}
|
|
|
|
|
2018-07-26 13:07:57 +00:00
|
|
|
namespace dawn_wire {
|
2018-06-06 15:36:49 +00:00
|
|
|
|
2018-07-18 09:45:17 +00:00
|
|
|
// Macro to simplify error handling, similar to DAWN_TRY but for DeserializeResult.
|
2018-06-06 15:36:49 +00:00
|
|
|
#define DESERIALIZE_TRY(EXPR) \
|
|
|
|
{ \
|
|
|
|
DeserializeResult exprResult = EXPR; \
|
|
|
|
if (exprResult != DeserializeResult::Success) { \
|
|
|
|
return exprResult; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2019-10-07 20:38:47 +00:00
|
|
|
ObjectHandle::ObjectHandle() = default;
|
|
|
|
ObjectHandle::ObjectHandle(ObjectId id, ObjectSerial serial) : id(id), serial(serial) {}
|
|
|
|
ObjectHandle::ObjectHandle(const volatile ObjectHandle& rhs) : id(rhs.id), serial(rhs.serial) {}
|
2019-10-15 07:30:35 +00:00
|
|
|
ObjectHandle& ObjectHandle::AssignFrom(const ObjectHandle& rhs) {
|
|
|
|
id = rhs.id;
|
|
|
|
serial = rhs.serial;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
ObjectHandle& ObjectHandle::AssignFrom(const volatile ObjectHandle& rhs) {
|
2019-10-07 20:38:47 +00:00
|
|
|
id = rhs.id;
|
|
|
|
serial = rhs.serial;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-06-08 16:30:50 +00:00
|
|
|
namespace {
|
2018-06-06 15:36:49 +00:00
|
|
|
|
2018-06-08 16:30:50 +00:00
|
|
|
// Consumes from (buffer, size) enough memory to contain T[count] and return it in data.
|
|
|
|
// Returns FatalError if not enough memory was available
|
|
|
|
template <typename T>
|
2019-10-07 20:38:47 +00:00
|
|
|
DeserializeResult GetPtrFromBuffer(const volatile char** buffer, size_t* size, size_t count, const volatile T** data) {
|
2019-01-04 09:54:10 +00:00
|
|
|
constexpr size_t kMaxCountWithoutOverflows = std::numeric_limits<size_t>::max() / sizeof(T);
|
|
|
|
if (count > kMaxCountWithoutOverflows) {
|
|
|
|
return DeserializeResult::FatalError;
|
|
|
|
}
|
|
|
|
|
2018-06-08 16:30:50 +00:00
|
|
|
size_t totalSize = sizeof(T) * count;
|
|
|
|
if (totalSize > *size) {
|
|
|
|
return DeserializeResult::FatalError;
|
|
|
|
}
|
2018-06-06 15:36:49 +00:00
|
|
|
|
2019-10-07 20:38:47 +00:00
|
|
|
*data = reinterpret_cast<const volatile T*>(*buffer);
|
2018-06-08 16:30:50 +00:00
|
|
|
*buffer += totalSize;
|
|
|
|
*size -= totalSize;
|
2018-06-06 15:36:49 +00:00
|
|
|
|
2018-06-08 16:30:50 +00:00
|
|
|
return DeserializeResult::Success;
|
2018-06-06 15:36:49 +00:00
|
|
|
}
|
|
|
|
|
2018-06-08 16:30:50 +00:00
|
|
|
// Allocates enough space from allocator to countain T[count] and return it in out.
|
|
|
|
// Return FatalError if the allocator couldn't allocate the memory.
|
|
|
|
template <typename T>
|
|
|
|
DeserializeResult GetSpace(DeserializeAllocator* allocator, size_t count, T** out) {
|
2019-01-04 09:54:10 +00:00
|
|
|
constexpr size_t kMaxCountWithoutOverflows = std::numeric_limits<size_t>::max() / sizeof(T);
|
|
|
|
if (count > kMaxCountWithoutOverflows) {
|
|
|
|
return DeserializeResult::FatalError;
|
|
|
|
}
|
|
|
|
|
2018-06-08 16:30:50 +00:00
|
|
|
size_t totalSize = sizeof(T) * count;
|
|
|
|
*out = static_cast<T*>(allocator->GetSpace(totalSize));
|
|
|
|
if (*out == nullptr) {
|
|
|
|
return DeserializeResult::FatalError;
|
|
|
|
}
|
2018-06-06 15:36:49 +00:00
|
|
|
|
2018-06-08 16:30:50 +00:00
|
|
|
return DeserializeResult::Success;
|
|
|
|
}
|
2018-06-06 15:36:49 +00:00
|
|
|
|
2019-01-15 20:49:53 +00:00
|
|
|
//* Output structure [de]serialization first because it is used by commands.
|
2018-06-08 16:30:50 +00:00
|
|
|
{% for type in by_category["structure"] %}
|
|
|
|
{% set name = as_cType(type.name) %}
|
2019-06-06 16:19:15 +00:00
|
|
|
{% if type.name.CamelCase() not in client_side_structures %}
|
|
|
|
{{write_record_serialization_helpers(type, name, type.members,
|
|
|
|
is_cmd=False)}}
|
|
|
|
{% endif %}
|
2018-06-08 16:30:50 +00:00
|
|
|
{% endfor %}
|
2018-06-06 15:36:49 +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() %}
|
|
|
|
{{write_record_serialization_helpers(command, name, command.members,
|
2019-04-01 21:04:17 +00:00
|
|
|
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,
|
2019-04-01 21:04:17 +00:00
|
|
|
is_cmd=True, is_return_command=True)}}
|
2018-06-08 16:30:50 +00:00
|
|
|
{% endfor %}
|
|
|
|
} // 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 %}
|
|
|
|
|
2019-10-20 03:01:56 +00:00
|
|
|
// Implementations of serialization/deserialization of DawnDeviceProperties.
|
2019-10-21 20:04:10 +00:00
|
|
|
size_t SerializedWGPUDevicePropertiesSize(const DawnDeviceProperties* deviceProperties) {
|
2019-10-20 03:01:56 +00:00
|
|
|
return sizeof(DawnDeviceProperties) +
|
2019-10-21 20:04:10 +00:00
|
|
|
WGPUDevicePropertiesGetExtraRequiredSize(*deviceProperties);
|
2019-10-20 03:01:56 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 20:04:10 +00:00
|
|
|
void SerializeWGPUDeviceProperties(const DawnDeviceProperties* deviceProperties,
|
2019-10-20 03:01:56 +00:00
|
|
|
char* serializeBuffer) {
|
2019-10-21 20:04:10 +00:00
|
|
|
size_t devicePropertiesSize = SerializedWGPUDevicePropertiesSize(deviceProperties);
|
|
|
|
WGPUDevicePropertiesTransfer* transfer =
|
|
|
|
reinterpret_cast<WGPUDevicePropertiesTransfer*>(serializeBuffer);
|
2019-10-20 03:01:56 +00:00
|
|
|
serializeBuffer += devicePropertiesSize;
|
|
|
|
|
2019-10-21 20:04:10 +00:00
|
|
|
WGPUDevicePropertiesSerialize(*deviceProperties, transfer, &serializeBuffer);
|
2019-10-20 03:01:56 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 20:04:10 +00:00
|
|
|
bool DeserializeWGPUDeviceProperties(DawnDeviceProperties* deviceProperties,
|
2019-10-20 03:01:56 +00:00
|
|
|
const volatile char* deserializeBuffer) {
|
2019-10-21 20:04:10 +00:00
|
|
|
size_t devicePropertiesSize = SerializedWGPUDevicePropertiesSize(deviceProperties);
|
|
|
|
const volatile WGPUDevicePropertiesTransfer* transfer = nullptr;
|
2019-10-20 03:01:56 +00:00
|
|
|
if (GetPtrFromBuffer(&deserializeBuffer, &devicePropertiesSize, 1, &transfer) !=
|
|
|
|
DeserializeResult::Success) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-21 20:04:10 +00:00
|
|
|
return WGPUDevicePropertiesDeserialize(deviceProperties, transfer, &deserializeBuffer,
|
2019-10-20 03:01:56 +00:00
|
|
|
&devicePropertiesSize,
|
|
|
|
nullptr) == DeserializeResult::Success;
|
|
|
|
}
|
|
|
|
|
2018-07-26 13:07:57 +00:00
|
|
|
} // namespace dawn_wire
|