/* * WiVRn VR streaming * Copyright (C) 2022 Guillaume Meunier * Copyright (C) 2022 Patrick Nicolas * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #pragma once #include "boost/pfr/core.hpp" #include "boost/pfr/tuple_size.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "wivrn_serialization_types.h" namespace xrt::drivers::wivrn { namespace details { struct hash_context { // FNV1a hash static const uint64_t fnv_prime = 0x100000001b3; static const uint64_t fnv_offset_basis = 0xcbf29ce484222325; uint64_t hash = fnv_offset_basis; constexpr uint64_t feed(std::string_view s) { for (const char c: s) hash = (hash ^ c) * fnv_prime; return hash; } constexpr uint64_t feed_unsigned(uint64_t n) { if (n >= 10) feed(n / 10); char digit[] = "0"; digit[0] += n % 10; feed(digit); return hash; } constexpr uint64_t feed(int64_t n) { if (n < 0) { feed("-"); feed_unsigned(static_cast(-n)); } else { feed_unsigned(static_cast(n)); } return hash; } }; } // namespace details template struct serialization_traits; class deserialization_error : public std::runtime_error { public: deserialization_error() : std::runtime_error("Deserialization error") {} }; class serialization_packet { std::vector buffer; // Either the size to read from the buffer, or an actual span // Last element is always an integer std::vector>> spans = {size_t(0)}; public: serialization_packet() = default; void clear() { buffer.clear(); spans.clear(); spans.push_back({size_t(0)}); } void write(const void * data, size_t size) { size_t index = buffer.size(); buffer.resize(index + size); memcpy(&buffer[index], data, size); std::get(spans.back()) += size; } void write(std::span span) { spans.push_back(span); spans.push_back(size_t(0)); } template void serialize(const T & value) { serialization_traits::serialize(value, *this); } operator std::vector>() { struct visitor { std::vector::iterator it; std::vector> res; void operator()(size_t s) { if (s > 0) res.emplace_back(it, it + s); it += s; } void operator()(const std::span & span) { res.push_back(span); } }; visitor v{.it = buffer.begin()}; for (const auto & span_variant: spans) { std::visit(v, span_variant); } return v.res; } }; class deserialization_packet { std::vector buffer; size_t read_index; public: deserialization_packet() : read_index(0) {} explicit deserialization_packet(std::vector buffer, size_t skip = 0) : buffer(std::move(buffer)), read_index(skip) {} void read(void * data, size_t size) { check_remaining_size(size); memcpy(data, &buffer[read_index], size); read_index += size; } std::span read_span(size_t size) { check_remaining_size(size); std::span res(&buffer[read_index], size); read_index += size; return res; } size_t remaining() const { return buffer.size() - read_index; } bool empty() const { return buffer.size() <= read_index; } void check_remaining_size(size_t min_size) const { if (min_size > remaining()) throw deserialization_error(); } template T deserialize() { return serialization_traits::deserialize(*this); } template void deserialize(T & v) { v = deserialize(); } std::pair> steal_buffer() { return {read_index, std::move(buffer)}; } }; template struct serialization_traits>> { static constexpr void type_hash(details::hash_context & h) { if constexpr (std::is_floating_point_v) h.feed("float"); else if constexpr (std::is_integral_v) { if constexpr (std::is_unsigned_v) h.feed("uint"); else h.feed("int"); } h.feed(sizeof(T) * 8); } static void serialize(T value, serialization_packet & packet) { packet.write(&value, sizeof(value)); } static T deserialize(deserialization_packet & packet) { T value; packet.read((char *)&value, sizeof(value)); return value; } }; template struct serialization_traits>> { static constexpr void type_hash(details::hash_context & h) { h.feed("enum"); h.feed(sizeof(T) * 8); } static void serialize(T value, serialization_packet & packet) { packet.write(&value, sizeof(value)); } static T deserialize(deserialization_packet & packet) { T value; packet.read((char *)&value, sizeof(value)); return value; } }; template struct is_stdarray : public std::false_type {}; template struct is_stdarray> : public std::true_type {}; template inline constexpr bool is_stdarray_v = is_stdarray::value; template struct serialization_traits && !is_stdarray_v>> { template static constexpr void aux2(details::hash_context & h) { if constexpr (I > 0) h.feed(","); serialization_traits>::type_hash(h); } template static constexpr void aux1(details::hash_context & h, std::index_sequence) { (aux2(h), ...); } static constexpr void type_hash(details::hash_context & h) { h.feed("structure{"); aux1(h, std::make_index_sequence>()); h.feed("}"); } static void serialize(const T & value, serialization_packet & packet) { boost::pfr::for_each_field(value, [&](const auto & x) { packet.serialize(x); }); } static T deserialize(deserialization_packet & packet) { T value; boost::pfr::for_each_field( value, [&](auto & x) { x = packet.deserialize>(); }); return value; } }; template <> struct serialization_traits { static constexpr void type_hash(details::hash_context & h) { h.feed("string"); } static void serialize(const std::string & value, serialization_packet & packet) { packet.serialize(value.size()); packet.write(value.data(), value.size()); } static std::string deserialize(deserialization_packet & packet) { std::string value; size_t size = packet.deserialize(); packet.check_remaining_size(size); value.resize(size); packet.read(value.data(), size); return value; } }; template struct serialization_traits> { static constexpr void type_hash(details::hash_context & h) { h.feed("vector<"); serialization_traits::type_hash(h); h.feed(">"); } static void serialize(const std::vector & value, serialization_packet & packet) { packet.serialize(value.size()); if constexpr (std::is_arithmetic_v) { packet.write(value.data(), value.size() * sizeof(T)); } else { for (const T & i: value) packet.serialize(i); } } static std::vector deserialize(deserialization_packet & packet) { std::vector value; size_t size = packet.deserialize(); if constexpr (std::is_arithmetic_v) { packet.check_remaining_size(size * sizeof(T)); value.resize(size); packet.read(value.data(), size * sizeof(T)); } else { value.reserve(size); for (size_t i = 0; i < size; i++) { value.emplace_back(packet.deserialize()); } } return value; } }; template struct serialization_traits> { static constexpr void type_hash(details::hash_context & h) { h.feed("optional<"); serialization_traits::type_hash(h); h.feed(">"); } static void serialize(const std::optional & value, serialization_packet & packet) { if (value) { packet.serialize(true); packet.serialize(*value); } else { packet.serialize(false); } } static std::optional deserialize(deserialization_packet & packet) { if (packet.deserialize()) return packet.deserialize(); else return std::nullopt; } }; template struct serialization_traits> { static constexpr void type_hash(details::hash_context & h) { h.feed("array<"); serialization_traits::type_hash(h); h.feed(","); h.feed(N); h.feed(">"); } static void serialize(const std::array & value, serialization_packet & packet) { for (const T & i: value) packet.serialize(i); } static std::array deserialize(deserialization_packet & packet) { std::array value; for (size_t i = 0; i < N; i++) { value[i] = packet.deserialize(); } return value; } }; template struct serialization_traits> { template static constexpr void type_list_hash(details::hash_context & h) { serialization_traits::type_hash(h); if constexpr (sizeof...(V) > 0) { h.feed(","); type_list_hash(h); } } static constexpr void type_hash(details::hash_context & h) { h.feed("variant<"); type_list_hash(h); h.feed(">"); } static void serialize(const std::variant & value, serialization_packet & packet) { packet.serialize(value.index()); std::visit([&](const auto & x) { packet.serialize(x); }, value); } template using i_th_type = std::tuple_element_t>; template static std::variant deserialize_aux(deserialization_packet & packet, size_t type_index, std::index_sequence) { std::variant value; ((I == type_index ? (void)(value = packet.deserialize>()) : (void)0), ...); return value; } static std::variant deserialize(deserialization_packet & packet) { uint8_t type_index = packet.deserialize(); if (type_index >= sizeof...(T)) throw deserialization_error(); return deserialize_aux(packet, type_index, std::make_index_sequence()); } }; template struct serialization_traits> { static constexpr void type_hash(details::hash_context & h) { h.feed("duration<"); serialization_traits::type_hash(h); h.feed(","); h.feed(Period::num); h.feed("/"); h.feed(Period::den); h.feed(">"); } static void serialize(const std::chrono::duration & value, serialization_packet & packet) { packet.serialize(value.count()); } static std::chrono::duration deserialize(deserialization_packet & packet) { Rep nsec = packet.deserialize(); return std::chrono::duration{nsec}; } }; template <> struct serialization_traits> { static constexpr void type_hash(details::hash_context & h) { h.feed("span"); } static void serialize(const std::span & value, serialization_packet & packet) { packet.serialize(value.size()); packet.write(value); } static std::span deserialize(deserialization_packet & packet) { size_t size = packet.deserialize(); return packet.read_span(size); } }; template <> struct serialization_traits { static constexpr void type_hash(details::hash_context & h) { } static void serialize(const data_holder &, serialization_packet &) { } static data_holder deserialize(deserialization_packet & packet) { data_holder value; size_t size; std::tie(size, value.c) = packet.steal_buffer(); return value; } }; template constexpr uint64_t serialization_type_hash() { details::hash_context h; serialization_traits::type_hash(h); return h.hash; } } // namespace xrt::drivers::wivrn