MemoryTransferService: Separate functions to serialize and get serialization size

Bug: dawn:156
Change-Id: I19317954c64700bdd67aa414d8eb2422d2c3544d
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/9860
Commit-Queue: Kai Ninomiya <kainino@chromium.org>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
This commit is contained in:
Austin Eng 2019-08-09 00:03:20 +00:00 committed by Commit Bot service account
parent 42e1bba480
commit a9658d967a
11 changed files with 105 additions and 55 deletions

View File

@ -27,7 +27,7 @@ namespace dawn_wire { namespace client {
std::is_same<Handle, MemoryTransferService::WriteHandle>::value; std::is_same<Handle, MemoryTransferService::WriteHandle>::value;
// Get the serialization size of the handle. // Get the serialization size of the handle.
size_t handleCreateInfoLength = handle->SerializeCreate(); size_t handleCreateInfoLength = handle->SerializeCreateSize();
BufferMapAsyncCmd cmd; BufferMapAsyncCmd cmd;
cmd.bufferId = buffer->id; cmd.bufferId = buffer->id;
@ -171,7 +171,7 @@ namespace dawn_wire { namespace client {
buffer->writeHandle = std::move(writeHandle); buffer->writeHandle = std::move(writeHandle);
// Get the serialization size of the WriteHandle. // Get the serialization size of the WriteHandle.
size_t handleCreateInfoLength = buffer->writeHandle->SerializeCreate(); size_t handleCreateInfoLength = buffer->writeHandle->SerializeCreateSize();
DeviceCreateBufferMappedCmd cmd; DeviceCreateBufferMappedCmd cmd;
cmd.device = cDevice; cmd.device = cDevice;
@ -246,7 +246,7 @@ namespace dawn_wire { namespace client {
buffer->requests[serial] = std::move(request); buffer->requests[serial] = std::move(request);
// Get the serialization size of the WriteHandle. // Get the serialization size of the WriteHandle.
size_t handleCreateInfoLength = writeHandle->SerializeCreate(); size_t handleCreateInfoLength = writeHandle->SerializeCreateSize();
DeviceCreateBufferMappedAsyncCmd cmd; DeviceCreateBufferMappedAsyncCmd cmd;
cmd.device = cDevice; cmd.device = cDevice;
@ -326,7 +326,7 @@ namespace dawn_wire { namespace client {
ASSERT(buffer->readHandle == nullptr); ASSERT(buffer->readHandle == nullptr);
// Get the serialization size of metadata to flush writes. // Get the serialization size of metadata to flush writes.
size_t writeFlushInfoLength = buffer->writeHandle->SerializeFlush(); size_t writeFlushInfoLength = buffer->writeHandle->SerializeFlushSize();
BufferUpdateMappedDataCmd cmd; BufferUpdateMappedDataCmd cmd;
cmd.bufferId = buffer->id; cmd.bufferId = buffer->id;

View File

@ -26,10 +26,13 @@ namespace dawn_wire { namespace client {
~ReadHandleImpl() override = default; ~ReadHandleImpl() override = default;
size_t SerializeCreate(void*) override { size_t SerializeCreateSize() override {
return 0; return 0;
} }
void SerializeCreate(void*) override {
}
bool DeserializeInitialData(const void* deserializePointer, bool DeserializeInitialData(const void* deserializePointer,
size_t deserializeSize, size_t deserializeSize,
const void** data, const void** data,
@ -61,24 +64,29 @@ namespace dawn_wire { namespace client {
~WriteHandleImpl() override = default; ~WriteHandleImpl() override = default;
size_t SerializeCreate(void*) override { size_t SerializeCreateSize() override {
return 0; return 0;
} }
void SerializeCreate(void*) override {
}
std::pair<void*, size_t> Open() override { std::pair<void*, size_t> Open() override {
mStagingData = std::unique_ptr<uint8_t[]>(new uint8_t[mSize]); mStagingData = std::unique_ptr<uint8_t[]>(new uint8_t[mSize]);
memset(mStagingData.get(), 0, mSize); memset(mStagingData.get(), 0, mSize);
return std::make_pair(mStagingData.get(), mSize); return std::make_pair(mStagingData.get(), mSize);
} }
size_t SerializeFlush(void* serializePointer) override { size_t SerializeFlushSize() override {
if (serializePointer != nullptr) {
ASSERT(mStagingData != nullptr);
memcpy(serializePointer, mStagingData.get(), mSize);
}
return mSize; return mSize;
} }
void SerializeFlush(void* serializePointer) override {
ASSERT(mStagingData != nullptr);
ASSERT(serializePointer != nullptr);
memcpy(serializePointer, mStagingData.get(), mSize);
}
private: private:
size_t mSize; size_t mSize;
std::unique_ptr<uint8_t[]> mStagingData; std::unique_ptr<uint8_t[]> mStagingData;

View File

@ -27,8 +27,12 @@ namespace dawn_wire { namespace client {
mService->OnReadHandleDestroy(this); mService->OnReadHandleDestroy(this);
} }
size_t MockMemoryTransferService::MockReadHandle::SerializeCreate(void* serializePointer) { size_t MockMemoryTransferService::MockReadHandle::SerializeCreateSize() {
return mService->OnReadHandleSerializeCreate(this, serializePointer); return mService->OnReadHandleSerializeCreateSize(this);
}
void MockMemoryTransferService::MockReadHandle::SerializeCreate(void* serializePointer) {
mService->OnReadHandleSerializeCreate(this, serializePointer);
} }
bool MockMemoryTransferService::MockReadHandle::DeserializeInitialData( bool MockMemoryTransferService::MockReadHandle::DeserializeInitialData(
@ -50,16 +54,24 @@ namespace dawn_wire { namespace client {
mService->OnWriteHandleDestroy(this); mService->OnWriteHandleDestroy(this);
} }
size_t MockMemoryTransferService::MockWriteHandle::SerializeCreate(void* serializePointer) { size_t MockMemoryTransferService::MockWriteHandle::SerializeCreateSize() {
return mService->OnWriteHandleSerializeCreate(this, serializePointer); return mService->OnWriteHandleSerializeCreateSize(this);
}
void MockMemoryTransferService::MockWriteHandle::SerializeCreate(void* serializePointer) {
mService->OnWriteHandleSerializeCreate(this, serializePointer);
} }
std::pair<void*, size_t> MockMemoryTransferService::MockWriteHandle::Open() { std::pair<void*, size_t> MockMemoryTransferService::MockWriteHandle::Open() {
return mService->OnWriteHandleOpen(this); return mService->OnWriteHandleOpen(this);
} }
size_t MockMemoryTransferService::MockWriteHandle::SerializeFlush(void* serializePointer) { size_t MockMemoryTransferService::MockWriteHandle::SerializeFlushSize() {
return mService->OnWriteHandleSerializeFlush(this, serializePointer); return mService->OnWriteHandleSerializeFlushSize(this);
}
void MockMemoryTransferService::MockWriteHandle::SerializeFlush(void* serializePointer) {
mService->OnWriteHandleSerializeFlush(this, serializePointer);
} }
MockMemoryTransferService::MockMemoryTransferService() = default; MockMemoryTransferService::MockMemoryTransferService() = default;

View File

@ -29,7 +29,8 @@ namespace dawn_wire { namespace client {
explicit MockReadHandle(MockMemoryTransferService* service); explicit MockReadHandle(MockMemoryTransferService* service);
~MockReadHandle() override; ~MockReadHandle() override;
size_t SerializeCreate(void* serializePointer) override; size_t SerializeCreateSize() override;
void SerializeCreate(void* serializePointer) override;
bool DeserializeInitialData(const void* deserializePointer, bool DeserializeInitialData(const void* deserializePointer,
size_t deserializeSize, size_t deserializeSize,
const void** data, const void** data,
@ -44,9 +45,11 @@ namespace dawn_wire { namespace client {
explicit MockWriteHandle(MockMemoryTransferService* service); explicit MockWriteHandle(MockMemoryTransferService* service);
~MockWriteHandle() override; ~MockWriteHandle() override;
size_t SerializeCreate(void* serializePointer) override; size_t SerializeCreateSize() override;
void SerializeCreate(void* serializePointer) override;
std::pair<void*, size_t> Open() override; std::pair<void*, size_t> Open() override;
size_t SerializeFlush(void* serializePointer) override; size_t SerializeFlushSize() override;
void SerializeFlush(void* serializePointer) override;
private: private:
MockMemoryTransferService* mService; MockMemoryTransferService* mService;
@ -64,8 +67,8 @@ namespace dawn_wire { namespace client {
MOCK_METHOD1(OnCreateReadHandle, ReadHandle*(size_t)); MOCK_METHOD1(OnCreateReadHandle, ReadHandle*(size_t));
MOCK_METHOD1(OnCreateWriteHandle, WriteHandle*(size_t)); MOCK_METHOD1(OnCreateWriteHandle, WriteHandle*(size_t));
MOCK_METHOD2(OnReadHandleSerializeCreate, MOCK_METHOD1(OnReadHandleSerializeCreateSize, size_t(const ReadHandle*));
size_t(const ReadHandle*, void* serializePointer)); MOCK_METHOD2(OnReadHandleSerializeCreate, void(const ReadHandle*, void* serializePointer));
MOCK_METHOD5(OnReadHandleDeserializeInitialData, MOCK_METHOD5(OnReadHandleDeserializeInitialData,
bool(const ReadHandle*, bool(const ReadHandle*,
const uint32_t* deserializePointer, const uint32_t* deserializePointer,
@ -74,11 +77,13 @@ namespace dawn_wire { namespace client {
size_t* dataLength)); size_t* dataLength));
MOCK_METHOD1(OnReadHandleDestroy, void(const ReadHandle*)); MOCK_METHOD1(OnReadHandleDestroy, void(const ReadHandle*));
MOCK_METHOD1(OnWriteHandleSerializeCreateSize, size_t(const void* WriteHandle));
MOCK_METHOD2(OnWriteHandleSerializeCreate, MOCK_METHOD2(OnWriteHandleSerializeCreate,
size_t(const void* WriteHandle, void* serializePointer)); void(const void* WriteHandle, void* serializePointer));
MOCK_METHOD1(OnWriteHandleOpen, std::pair<void*, size_t>(const void* WriteHandle)); MOCK_METHOD1(OnWriteHandleOpen, std::pair<void*, size_t>(const void* WriteHandle));
MOCK_METHOD1(OnWriteHandleSerializeFlushSize, size_t(const void* WriteHandle));
MOCK_METHOD2(OnWriteHandleSerializeFlush, MOCK_METHOD2(OnWriteHandleSerializeFlush,
size_t(const void* WriteHandle, void* serializePointer)); void(const void* WriteHandle, void* serializePointer));
MOCK_METHOD1(OnWriteHandleDestroy, void(const void* WriteHandle)); MOCK_METHOD1(OnWriteHandleDestroy, void(const void* WriteHandle));
}; };

View File

@ -254,7 +254,7 @@ namespace dawn_wire { namespace server {
size_t initialDataInfoLength = 0; size_t initialDataInfoLength = 0;
if (status == DAWN_BUFFER_MAP_ASYNC_STATUS_SUCCESS) { if (status == DAWN_BUFFER_MAP_ASYNC_STATUS_SUCCESS) {
// Get the serialization size of the message to initialize ReadHandle data. // Get the serialization size of the message to initialize ReadHandle data.
initialDataInfoLength = data->readHandle->SerializeInitialData(ptr, dataLength); initialDataInfoLength = data->readHandle->SerializeInitialDataSize(ptr, dataLength);
} else { } else {
dataLength = 0; dataLength = 0;
} }

View File

@ -26,14 +26,18 @@ namespace dawn_wire { namespace server {
} }
~ReadHandleImpl() override = default; ~ReadHandleImpl() override = default;
size_t SerializeInitialData(const void* data, size_t SerializeInitialDataSize(const void* data, size_t dataLength) override {
return dataLength;
}
void SerializeInitialData(const void* data,
size_t dataLength, size_t dataLength,
void* serializePointer) override { void* serializePointer) override {
if (serializePointer != nullptr && dataLength > 0) { if (dataLength > 0) {
ASSERT(data != nullptr); ASSERT(data != nullptr);
ASSERT(serializePointer != nullptr);
memcpy(serializePointer, data, dataLength); memcpy(serializePointer, data, dataLength);
} }
return dataLength;
} }
}; };

View File

@ -26,10 +26,15 @@ namespace dawn_wire { namespace server {
mService->OnReadHandleDestroy(this); mService->OnReadHandleDestroy(this);
} }
size_t MockMemoryTransferService::MockReadHandle::SerializeInitialData(const void* data, size_t MockMemoryTransferService::MockReadHandle::SerializeInitialDataSize(const void* data,
size_t dataLength) {
return mService->OnReadHandleSerializeInitialDataSize(this, data, dataLength);
}
void MockMemoryTransferService::MockReadHandle::SerializeInitialData(const void* data,
size_t dataLength, size_t dataLength,
void* serializePointer) { void* serializePointer) {
return mService->OnReadHandleSerializeInitialData(this, data, dataLength, serializePointer); mService->OnReadHandleSerializeInitialData(this, data, dataLength, serializePointer);
} }
MockMemoryTransferService::MockWriteHandle::MockWriteHandle(MockMemoryTransferService* service) MockMemoryTransferService::MockWriteHandle::MockWriteHandle(MockMemoryTransferService* service)

View File

@ -29,7 +29,8 @@ namespace dawn_wire { namespace server {
MockReadHandle(MockMemoryTransferService* service); MockReadHandle(MockMemoryTransferService* service);
~MockReadHandle() override; ~MockReadHandle() override;
size_t SerializeInitialData(const void* data, size_t SerializeInitialDataSize(const void* data, size_t dataLength) override;
void SerializeInitialData(const void* data,
size_t dataLength, size_t dataLength,
void* serializePointer) override; void* serializePointer) override;
@ -74,8 +75,10 @@ namespace dawn_wire { namespace server {
size_t deserializeSize, size_t deserializeSize,
WriteHandle** writeHandle)); WriteHandle** writeHandle));
MOCK_METHOD3(OnReadHandleSerializeInitialDataSize,
size_t(const ReadHandle* readHandle, const void* data, size_t dataLength));
MOCK_METHOD4(OnReadHandleSerializeInitialData, MOCK_METHOD4(OnReadHandleSerializeInitialData,
size_t(const ReadHandle* readHandle, void(const ReadHandle* readHandle,
const void* data, const void* data,
size_t dataLength, size_t dataLength,
void* serializePointer)); void* serializePointer));

View File

@ -76,9 +76,11 @@ namespace dawn_wire {
class DAWN_WIRE_EXPORT ReadHandle { class DAWN_WIRE_EXPORT ReadHandle {
public: public:
// Get the required serialization size for SerializeCreate
virtual size_t SerializeCreateSize() = 0;
// Serialize the handle into |serializePointer| so it can be received by the server. // Serialize the handle into |serializePointer| so it can be received by the server.
// If |serializePointer| is nullptr, this returns the required serialization space. virtual void SerializeCreate(void* serializePointer) = 0;
virtual size_t SerializeCreate(void* serializePointer = nullptr) = 0;
// Load initial data and open the handle for reading. // Load initial data and open the handle for reading.
// This function takes in the serialized result of // This function takes in the serialized result of
@ -95,19 +97,24 @@ namespace dawn_wire {
class DAWN_WIRE_EXPORT WriteHandle { class DAWN_WIRE_EXPORT WriteHandle {
public: public:
// Get the required serialization size for SerializeCreate
virtual size_t SerializeCreateSize() = 0;
// Serialize the handle into |serializePointer| so it can be received by the server. // Serialize the handle into |serializePointer| so it can be received by the server.
// If |serializePointer| is nullptr, this returns the required serialization space. virtual void SerializeCreate(void* serializePointer) = 0;
virtual size_t SerializeCreate(void* serializePointer = nullptr) = 0;
// Open the handle for reading. The data returned should be zero-initialized. // Open the handle for reading. The data returned should be zero-initialized.
// The data returned must live at least until the WriteHandle is destructed. // The data returned must live at least until the WriteHandle is destructed.
// On failure, the pointer returned should be null. // On failure, the pointer returned should be null.
virtual std::pair<void*, size_t> Open() = 0; virtual std::pair<void*, size_t> Open() = 0;
// Get the required serialization size for SerializeFlush
virtual size_t SerializeFlushSize() = 0;
// Flush writes to the handle. This should serialize info to send updates to the // Flush writes to the handle. This should serialize info to send updates to the
// server. // server.
// If |serializePointer| is nullptr, this returns the required serialization space. virtual void SerializeFlush(void* serializePointer) = 0;
virtual size_t SerializeFlush(void* serializePointer = nullptr) = 0;
virtual ~WriteHandle(); virtual ~WriteHandle();
}; };
}; };

View File

@ -65,12 +65,14 @@ namespace dawn_wire {
class DAWN_WIRE_EXPORT ReadHandle { class DAWN_WIRE_EXPORT ReadHandle {
public: public:
// Get the required serialization size for SerializeInitialData
virtual size_t SerializeInitialDataSize(const void* data, size_t dataLength) = 0;
// Initialize the handle data. // Initialize the handle data.
// Serialize into |serializePointer| so the client can update handle data. // Serialize into |serializePointer| so the client can update handle data.
// If |serializePointer| is nullptr, this returns the required serialization space. virtual void SerializeInitialData(const void* data,
virtual size_t SerializeInitialData(const void* data,
size_t dataLength, size_t dataLength,
void* serializePointer = nullptr) = 0; void* serializePointer) = 0;
virtual ~ReadHandle(); virtual ~ReadHandle();
}; };

View File

@ -230,8 +230,9 @@ class WireMemoryTransferServiceTests : public WireTest {
} }
void ExpectReadHandleSerialization(ClientReadHandle* handle) { void ExpectReadHandleSerialization(ClientReadHandle* handle) {
EXPECT_CALL(clientMemoryTransferService, OnReadHandleSerializeCreateSize(handle))
.WillOnce(InvokeWithoutArgs([&]() { return sizeof(mSerializeCreateInfo); }));
EXPECT_CALL(clientMemoryTransferService, OnReadHandleSerializeCreate(handle, _)) EXPECT_CALL(clientMemoryTransferService, OnReadHandleSerializeCreate(handle, _))
.WillOnce(InvokeWithoutArgs([&]() { return sizeof(mSerializeCreateInfo); }))
.WillOnce(WithArg<1>([&](void* serializePointer) { .WillOnce(WithArg<1>([&](void* serializePointer) {
memcpy(serializePointer, &mSerializeCreateInfo, sizeof(mSerializeCreateInfo)); memcpy(serializePointer, &mSerializeCreateInfo, sizeof(mSerializeCreateInfo));
return sizeof(mSerializeCreateInfo); return sizeof(mSerializeCreateInfo);
@ -261,8 +262,9 @@ class WireMemoryTransferServiceTests : public WireTest {
} }
void ExpectServerReadHandleInitialize(ServerReadHandle* handle) { void ExpectServerReadHandleInitialize(ServerReadHandle* handle) {
EXPECT_CALL(serverMemoryTransferService, OnReadHandleSerializeInitialDataSize(handle, _, _))
.WillOnce(InvokeWithoutArgs([&]() { return sizeof(mSerializeInitialDataInfo); }));
EXPECT_CALL(serverMemoryTransferService, OnReadHandleSerializeInitialData(handle, _, _, _)) EXPECT_CALL(serverMemoryTransferService, OnReadHandleSerializeInitialData(handle, _, _, _))
.WillOnce(InvokeWithoutArgs([&]() { return sizeof(mSerializeInitialDataInfo); }))
.WillOnce(WithArg<3>([&](void* serializePointer) { .WillOnce(WithArg<3>([&](void* serializePointer) {
memcpy(serializePointer, &mSerializeInitialDataInfo, memcpy(serializePointer, &mSerializeInitialDataInfo,
sizeof(mSerializeInitialDataInfo)); sizeof(mSerializeInitialDataInfo));
@ -307,8 +309,9 @@ class WireMemoryTransferServiceTests : public WireTest {
} }
void ExpectWriteHandleSerialization(ClientWriteHandle* handle) { void ExpectWriteHandleSerialization(ClientWriteHandle* handle) {
EXPECT_CALL(clientMemoryTransferService, OnWriteHandleSerializeCreateSize(handle))
.WillOnce(InvokeWithoutArgs([&]() { return sizeof(mSerializeCreateInfo); }));
EXPECT_CALL(clientMemoryTransferService, OnWriteHandleSerializeCreate(handle, _)) EXPECT_CALL(clientMemoryTransferService, OnWriteHandleSerializeCreate(handle, _))
.WillOnce(InvokeWithoutArgs([&]() { return sizeof(mSerializeCreateInfo); }))
.WillOnce(WithArg<1>([&](void* serializePointer) { .WillOnce(WithArg<1>([&](void* serializePointer) {
memcpy(serializePointer, &mSerializeCreateInfo, sizeof(mSerializeCreateInfo)); memcpy(serializePointer, &mSerializeCreateInfo, sizeof(mSerializeCreateInfo));
return sizeof(mSerializeCreateInfo); return sizeof(mSerializeCreateInfo);
@ -353,8 +356,9 @@ class WireMemoryTransferServiceTests : public WireTest {
} }
void ExpectClientWriteHandleSerializeFlush(ClientWriteHandle* handle) { void ExpectClientWriteHandleSerializeFlush(ClientWriteHandle* handle) {
EXPECT_CALL(clientMemoryTransferService, OnWriteHandleSerializeFlushSize(handle))
.WillOnce(InvokeWithoutArgs([&]() { return sizeof(mSerializeFlushInfo); }));
EXPECT_CALL(clientMemoryTransferService, OnWriteHandleSerializeFlush(handle, _)) EXPECT_CALL(clientMemoryTransferService, OnWriteHandleSerializeFlush(handle, _))
.WillOnce(InvokeWithoutArgs([&]() { return sizeof(mSerializeFlushInfo); }))
.WillOnce(WithArg<1>([&](void* serializePointer) { .WillOnce(WithArg<1>([&](void* serializePointer) {
memcpy(serializePointer, &mSerializeFlushInfo, sizeof(mSerializeFlushInfo)); memcpy(serializePointer, &mSerializeFlushInfo, sizeof(mSerializeFlushInfo));
return sizeof(mSerializeFlushInfo); return sizeof(mSerializeFlushInfo);