From b01698775b468a04e4d0a9bdd86035ff00e6decb Mon Sep 17 00:00:00 2001 From: liamwhite Date: Thu, 2 Feb 2023 15:53:28 -0500 Subject: Revert "hle_ipc: Use std::span to avoid heap allocations/copies when calling ReadBuffer" --- .../service/nvflinger/buffer_queue_producer.cpp | 4 +- .../service/nvflinger/graphic_buffer_producer.cpp | 2 +- .../service/nvflinger/graphic_buffer_producer.h | 4 +- src/core/hle/service/nvflinger/parcel.h | 87 ++++++++++------------ 4 files changed, 46 insertions(+), 51 deletions(-) (limited to 'src/core/hle/service/nvflinger') diff --git a/src/core/hle/service/nvflinger/buffer_queue_producer.cpp b/src/core/hle/service/nvflinger/buffer_queue_producer.cpp index bcbe05b0d..e601b5da1 100644 --- a/src/core/hle/service/nvflinger/buffer_queue_producer.cpp +++ b/src/core/hle/service/nvflinger/buffer_queue_producer.cpp @@ -815,8 +815,8 @@ Status BufferQueueProducer::SetPreallocatedBuffer(s32 slot, void BufferQueueProducer::Transact(Kernel::HLERequestContext& ctx, TransactionId code, u32 flags) { Status status{Status::NoError}; - InputParcel parcel_in{ctx.ReadBuffer()}; - OutputParcel parcel_out{}; + Parcel parcel_in{ctx.ReadBuffer()}; + Parcel parcel_out{}; switch (code) { case TransactionId::Connect: { diff --git a/src/core/hle/service/nvflinger/graphic_buffer_producer.cpp b/src/core/hle/service/nvflinger/graphic_buffer_producer.cpp index 769e8c0a3..4043c91f1 100644 --- a/src/core/hle/service/nvflinger/graphic_buffer_producer.cpp +++ b/src/core/hle/service/nvflinger/graphic_buffer_producer.cpp @@ -9,7 +9,7 @@ namespace Service::android { -QueueBufferInput::QueueBufferInput(InputParcel& parcel) { +QueueBufferInput::QueueBufferInput(Parcel& parcel) { parcel.ReadFlattened(*this); } diff --git a/src/core/hle/service/nvflinger/graphic_buffer_producer.h b/src/core/hle/service/nvflinger/graphic_buffer_producer.h index 2969f0fd5..6ea327bbe 100644 --- a/src/core/hle/service/nvflinger/graphic_buffer_producer.h +++ b/src/core/hle/service/nvflinger/graphic_buffer_producer.h @@ -14,11 +14,11 @@ namespace Service::android { -class InputParcel; +class Parcel; #pragma pack(push, 1) struct QueueBufferInput final { - explicit QueueBufferInput(InputParcel& parcel); + explicit QueueBufferInput(Parcel& parcel); void Deflate(s64* timestamp_, bool* is_auto_timestamp_, Common::Rectangle* crop_, NativeWindowScalingMode* scaling_mode_, NativeWindowTransform* transform_, diff --git a/src/core/hle/service/nvflinger/parcel.h b/src/core/hle/service/nvflinger/parcel.h index d1b6201e0..f3fa2587d 100644 --- a/src/core/hle/service/nvflinger/parcel.h +++ b/src/core/hle/service/nvflinger/parcel.h @@ -4,7 +4,6 @@ #pragma once #include -#include #include #include "common/alignment.h" @@ -13,17 +12,18 @@ namespace Service::android { -struct ParcelHeader { - u32 data_size; - u32 data_offset; - u32 objects_size; - u32 objects_offset; -}; -static_assert(sizeof(ParcelHeader) == 16, "ParcelHeader has wrong size"); - -class InputParcel final { +class Parcel final { public: - explicit InputParcel(std::span in_data) : read_buffer(std::move(in_data)) { + static constexpr std::size_t DefaultBufferSize = 0x40; + + Parcel() : buffer(DefaultBufferSize) {} + + template + explicit Parcel(const T& out_data) : buffer(DefaultBufferSize) { + Write(out_data); + } + + explicit Parcel(std::vector in_data) : buffer(std::move(in_data)) { DeserializeHeader(); [[maybe_unused]] const std::u16string token = ReadInterfaceToken(); } @@ -31,9 +31,9 @@ public: template void Read(T& val) { static_assert(std::is_trivially_copyable_v, "T must be trivially copyable."); - ASSERT(read_index + sizeof(T) <= read_buffer.size()); + ASSERT(read_index + sizeof(T) <= buffer.size()); - std::memcpy(&val, read_buffer.data() + read_index, sizeof(T)); + std::memcpy(&val, buffer.data() + read_index, sizeof(T)); read_index += sizeof(T); read_index = Common::AlignUp(read_index, 4); } @@ -62,10 +62,10 @@ public: template T ReadUnaligned() { static_assert(std::is_trivially_copyable_v, "T must be trivially copyable."); - ASSERT(read_index + sizeof(T) <= read_buffer.size()); + ASSERT(read_index + sizeof(T) <= buffer.size()); T val; - std::memcpy(&val, read_buffer.data() + read_index, sizeof(T)); + std::memcpy(&val, buffer.data() + read_index, sizeof(T)); read_index += sizeof(T); return val; } @@ -101,31 +101,6 @@ public: return token; } - void DeserializeHeader() { - ASSERT(read_buffer.size() > sizeof(ParcelHeader)); - - ParcelHeader header{}; - std::memcpy(&header, read_buffer.data(), sizeof(ParcelHeader)); - - read_index = header.data_offset; - } - -private: - std::span read_buffer; - std::size_t read_index = 0; -}; - -class OutputParcel final { -public: - static constexpr std::size_t DefaultBufferSize = 0x40; - - OutputParcel() : buffer(DefaultBufferSize) {} - - template - explicit OutputParcel(const T& out_data) : buffer(DefaultBufferSize) { - Write(out_data); - } - template void Write(const T& val) { static_assert(std::is_trivially_copyable_v, "T must be trivially copyable."); @@ -158,20 +133,40 @@ public: WriteObject(ptr.get()); } + void DeserializeHeader() { + ASSERT(buffer.size() > sizeof(Header)); + + Header header{}; + std::memcpy(&header, buffer.data(), sizeof(Header)); + + read_index = header.data_offset; + } + std::vector Serialize() const { - ParcelHeader header{}; - header.data_size = static_cast(write_index - sizeof(ParcelHeader)); - header.data_offset = sizeof(ParcelHeader); + ASSERT(read_index == 0); + + Header header{}; + header.data_size = static_cast(write_index - sizeof(Header)); + header.data_offset = sizeof(Header); header.objects_size = 4; - header.objects_offset = static_cast(sizeof(ParcelHeader) + header.data_size); - std::memcpy(buffer.data(), &header, sizeof(ParcelHeader)); + header.objects_offset = static_cast(sizeof(Header) + header.data_size); + std::memcpy(buffer.data(), &header, sizeof(Header)); return buffer; } private: + struct Header { + u32 data_size; + u32 data_offset; + u32 objects_size; + u32 objects_offset; + }; + static_assert(sizeof(Header) == 16, "ParcelHeader has wrong size"); + mutable std::vector buffer; - std::size_t write_index = sizeof(ParcelHeader); + std::size_t read_index = 0; + std::size_t write_index = sizeof(Header); }; } // namespace Service::android -- cgit v1.2.3