diff --git a/dawn_wire.json b/dawn_wire.json index c618b93e45..51ab477805 100644 --- a/dawn_wire.json +++ b/dawn_wire.json @@ -38,7 +38,7 @@ { "name": "write flush info length", "type": "uint64_t" }, { "name": "write flush info", "type": "uint8_t", "annotation": "const*", "length": "write flush info length", "skip_serialize": true} ], - "device create buffer mapped": [ + "device create buffer": [ { "name": "device", "type": "device" }, { "name": "descriptor", "type": "buffer descriptor", "annotation": "const*" }, { "name": "result", "type": "ObjectHandle", "handle_type": "buffer" }, @@ -108,6 +108,8 @@ "BufferSetSubData", "BufferGetConstMappedRange", "BufferGetMappedRange", + "DeviceCreateBuffer", + "DeviceCreateBufferMapped", "DevicePopErrorScope", "DeviceSetDeviceLostCallback", "DeviceSetUncapturedErrorCallback", @@ -119,8 +121,6 @@ "client_handwritten_commands": [ "BufferDestroy", "BufferUnmap", - "DeviceCreateBuffer", - "DeviceCreateBufferMapped", "DeviceCreateErrorBuffer", "DeviceGetDefaultQueue", "DeviceInjectError", diff --git a/src/dawn_wire/client/Buffer.cpp b/src/dawn_wire/client/Buffer.cpp index af217b3364..8c71b30741 100644 --- a/src/dawn_wire/client/Buffer.cpp +++ b/src/dawn_wire/client/Buffer.cpp @@ -29,89 +29,61 @@ namespace dawn_wire { namespace client { return device_->CreateErrorBuffer(); } + std::unique_ptr writeHandle = nullptr; + void* writeData = nullptr; + size_t writeHandleCreateInfoLength = 0; + + // If the buffer is mapped at creation, create a write handle that will represent the + // mapping of the whole buffer. + if (descriptor->mappedAtCreation) { + // Create the handle. + writeHandle.reset( + wireClient->GetMemoryTransferService()->CreateWriteHandle(descriptor->size)); + if (writeHandle == nullptr) { + device_->InjectError(WGPUErrorType_OutOfMemory, "Buffer mapping allocation failed"); + return device_->CreateErrorBuffer(); + } + + // Open the handle, it may fail by returning a nullptr in writeData. + size_t writeDataLength = 0; + std::tie(writeData, writeDataLength) = writeHandle->Open(); + if (writeData == nullptr) { + device_->InjectError(WGPUErrorType_OutOfMemory, "Buffer mapping allocation failed"); + return device_->CreateErrorBuffer(); + } + ASSERT(writeDataLength == descriptor->size); + + // Get the serialization size of the write handle. + writeHandleCreateInfoLength = writeHandle->SerializeCreateSize(); + } + + // Create the buffer and send the creation command. auto* bufferObjectAndSerial = wireClient->BufferAllocator().New(device_); Buffer* buffer = bufferObjectAndSerial->object.get(); - // Store the size of the buffer so that mapping operations can allocate a - // MemoryTransfer handle of the proper size. buffer->mSize = descriptor->size; DeviceCreateBufferCmd cmd; - cmd.self = ToAPI(device_); - cmd.descriptor = descriptor; - cmd.result = ObjectHandle{buffer->id, bufferObjectAndSerial->generation}; - - wireClient->SerializeCommand(cmd); - - return ToAPI(buffer); - } - - // static - WGPUCreateBufferMappedResult Buffer::CreateMapped(Device* device_, - const WGPUBufferDescriptor* descriptor) { - Client* wireClient = device_->GetClient(); - - WGPUCreateBufferMappedResult result; - result.data = nullptr; - result.dataLength = 0; - - // This buffer is too large to be mapped and to make a WriteHandle for. - if (descriptor->size > std::numeric_limits::max()) { - device_->InjectError(WGPUErrorType_OutOfMemory, "Buffer is too large for mapping"); - result.buffer = device_->CreateErrorBuffer(); - return result; - } - - // Create a WriteHandle for the map request. This is the client's intent to write GPU - // memory. - std::unique_ptr writeHandle = - std::unique_ptr( - wireClient->GetMemoryTransferService()->CreateWriteHandle(descriptor->size)); - - if (writeHandle == nullptr) { - device_->InjectError(WGPUErrorType_OutOfMemory, "Buffer mapping allocation failed"); - result.buffer = device_->CreateErrorBuffer(); - return result; - } - - // CreateBufferMapped is synchronous and the staging buffer for upload should be immediately - // available. - // Open the WriteHandle. This returns a pointer and size of mapped memory. - // |result.data| may be null on error. - std::tie(result.data, result.dataLength) = writeHandle->Open(); - if (result.data == nullptr) { - device_->InjectError(WGPUErrorType_OutOfMemory, "Buffer mapping allocation failed"); - result.buffer = device_->CreateErrorBuffer(); - return result; - } - - auto* bufferObjectAndSerial = wireClient->BufferAllocator().New(device_); - Buffer* buffer = bufferObjectAndSerial->object.get(); - buffer->mSize = descriptor->size; - // Successfully created staging memory. The buffer now owns the WriteHandle. - buffer->mWriteHandle = std::move(writeHandle); - buffer->mMappedData = result.data; - buffer->mMapOffset = 0; - buffer->mMapSize = descriptor->size; - - result.buffer = ToAPI(buffer); - - // Get the serialization size of the WriteHandle. - size_t handleCreateInfoLength = buffer->mWriteHandle->SerializeCreateSize(); - - DeviceCreateBufferMappedCmd cmd; cmd.device = ToAPI(device_); cmd.descriptor = descriptor; cmd.result = ObjectHandle{buffer->id, bufferObjectAndSerial->generation}; - cmd.handleCreateInfoLength = handleCreateInfoLength; + cmd.handleCreateInfoLength = writeHandleCreateInfoLength; cmd.handleCreateInfo = nullptr; - char* writeHandleSpace = - buffer->device->GetClient()->SerializeCommand(cmd, handleCreateInfoLength); + char* writeHandleSpace = wireClient->SerializeCommand(cmd, writeHandleCreateInfoLength); - // Serialize the WriteHandle into the space after the command. - buffer->mWriteHandle->SerializeCreate(writeHandleSpace); + if (descriptor->mappedAtCreation) { + // Serialize the WriteHandle into the space after the command. + writeHandle->SerializeCreate(writeHandleSpace); - return result; + // Set the buffer state for the mapping at creation. The buffer now owns the write + // handle.. + buffer->mWriteHandle = std::move(writeHandle); + buffer->mMappedData = writeData; + buffer->mMapOffset = 0; + buffer->mMapSize = buffer->mSize; + } + + return ToAPI(buffer); } // static diff --git a/src/dawn_wire/client/Buffer.h b/src/dawn_wire/client/Buffer.h index 79fcd858a4..1143779f99 100644 --- a/src/dawn_wire/client/Buffer.h +++ b/src/dawn_wire/client/Buffer.h @@ -29,8 +29,6 @@ namespace dawn_wire { namespace client { using ObjectBase::ObjectBase; static WGPUBuffer Create(Device* device, const WGPUBufferDescriptor* descriptor); - static WGPUCreateBufferMappedResult CreateMapped(Device* device, - const WGPUBufferDescriptor* descriptor); static WGPUBuffer CreateError(Device* device); ~Buffer(); diff --git a/src/dawn_wire/client/Device.cpp b/src/dawn_wire/client/Device.cpp index 411fd5cf2f..addc977359 100644 --- a/src/dawn_wire/client/Device.cpp +++ b/src/dawn_wire/client/Device.cpp @@ -146,16 +146,20 @@ namespace dawn_wire { namespace client { } WGPUBuffer Device::CreateBuffer(const WGPUBufferDescriptor* descriptor) { - if (descriptor->mappedAtCreation) { - return CreateBufferMapped(descriptor).buffer; - } else { - return Buffer::Create(this, descriptor); - } + return Buffer::Create(this, descriptor); } WGPUCreateBufferMappedResult Device::CreateBufferMapped( const WGPUBufferDescriptor* descriptor) { - return Buffer::CreateMapped(this, descriptor); + WGPUBufferDescriptor descMappedAtCreation = *descriptor; + descMappedAtCreation.mappedAtCreation = true; + + WGPUCreateBufferMappedResult result; + result.buffer = CreateBuffer(&descMappedAtCreation); + result.data = FromAPI(result.buffer)->GetMappedRange(0, descriptor->size); + result.dataLength = result.data == nullptr ? 0 : descriptor->size; + + return result; } WGPUBuffer Device::CreateErrorBuffer() { diff --git a/src/dawn_wire/server/ServerBuffer.cpp b/src/dawn_wire/server/ServerBuffer.cpp index d0c82a2560..dd71638de9 100644 --- a/src/dawn_wire/server/ServerBuffer.cpp +++ b/src/dawn_wire/server/ServerBuffer.cpp @@ -118,50 +118,52 @@ namespace dawn_wire { namespace server { return true; } - bool Server::DoDeviceCreateBufferMapped(WGPUDevice device, - const WGPUBufferDescriptor* descriptor, - ObjectHandle bufferResult, - uint64_t handleCreateInfoLength, - const uint8_t* handleCreateInfo) { - if (handleCreateInfoLength > std::numeric_limits::max()) { - // This is the size of data deserialized from the command stream, which must be - // CPU-addressable. - return false; - } - + bool Server::DoDeviceCreateBuffer(WGPUDevice device, + const WGPUBufferDescriptor* descriptor, + ObjectHandle bufferResult, + uint64_t handleCreateInfoLength, + const uint8_t* handleCreateInfo) { + // Create and register the buffer object. auto* resultData = BufferObjects().Allocate(bufferResult.id); if (resultData == nullptr) { return false; } resultData->generation = bufferResult.generation; + resultData->handle = mProcs.deviceCreateBuffer(device, descriptor); - WGPUCreateBufferMappedResult result = mProcs.deviceCreateBufferMapped(device, descriptor); - ASSERT(result.buffer != nullptr); - if (result.data == nullptr && result.dataLength != 0) { - // Non-zero dataLength but null data is used to indicate an allocation error. - // Don't return false because this is not fatal. result.buffer is an ErrorBuffer - // and subsequent operations will be errors. - // This should only happen when fuzzing with the Null backend. - resultData->mapWriteState = BufferMapWriteState::MapError; - } else { - // Deserialize metadata produced from the client to create a companion server handle. - MemoryTransferService::WriteHandle* writeHandle = nullptr; - if (!mMemoryTransferService->DeserializeWriteHandle( - handleCreateInfo, static_cast(handleCreateInfoLength), &writeHandle)) { - return false; - } - ASSERT(writeHandle != nullptr); - - // Set the target of the WriteHandle to the mapped GPU memory. - writeHandle->SetTarget(result.data, result.dataLength); - - // The buffer is mapped and has a valid mappedData pointer. - // The buffer may still be an error with fake staging data. - resultData->mapWriteState = BufferMapWriteState::Mapped; - resultData->writeHandle = - std::unique_ptr(writeHandle); + // If the buffer isn't mapped at creation, we are done. + if (!descriptor->mappedAtCreation) { + return handleCreateInfoLength == 0; } - resultData->handle = result.buffer; + + // This is the size of data deserialized from the command stream to create the write handle, + // which must be CPU-addressable. + if (handleCreateInfoLength > std::numeric_limits::max()) { + return false; + } + + void* mapping = mProcs.bufferGetMappedRange(resultData->handle, 0, descriptor->size); + if (mapping == nullptr) { + // A zero mapping is used to indicate an allocation error of an error buffer. This is a + // valid case and isn't fatal. Remember the buffer is an error so as to skip subsequent + // mapping operations. + resultData->mapWriteState = BufferMapWriteState::MapError; + return true; + } + + // Deserialize metadata produced from the client to create a companion server handle. + MemoryTransferService::WriteHandle* writeHandle = nullptr; + if (!mMemoryTransferService->DeserializeWriteHandle( + handleCreateInfo, static_cast(handleCreateInfoLength), &writeHandle)) { + return false; + } + + // Set the target of the WriteHandle to the mapped GPU memory. + ASSERT(writeHandle != nullptr); + writeHandle->SetTarget(mapping, descriptor->size); + + resultData->mapWriteState = BufferMapWriteState::Mapped; + resultData->writeHandle.reset(writeHandle); return true; } diff --git a/src/tests/unittests/wire/WireBufferMappingTests.cpp b/src/tests/unittests/wire/WireBufferMappingTests.cpp index c8902c1fe3..8729643347 100644 --- a/src/tests/unittests/wire/WireBufferMappingTests.cpp +++ b/src/tests/unittests/wire/WireBufferMappingTests.cpp @@ -477,84 +477,72 @@ TEST_F(WireBufferMappingTests, DestroyInsideMapWriteCallback) { FlushClient(); } -// Test successful CreateBufferMapped -TEST_F(WireBufferMappingTests, CreateBufferMappedSuccess) { +// Test successful buffer creation with mappedAtCreation=true +TEST_F(WireBufferMappingTests, MappedAtCreationSuccess) { WGPUBufferDescriptor descriptor = {}; descriptor.size = 4; + descriptor.mappedAtCreation = true; WGPUBuffer apiBuffer = api.GetNewBuffer(); - WGPUCreateBufferMappedResult apiResult; uint32_t apiBufferData = 1234; - apiResult.buffer = apiBuffer; - apiResult.data = reinterpret_cast(&apiBufferData); - apiResult.dataLength = 4; - WGPUCreateBufferMappedResult result = wgpuDeviceCreateBufferMapped(device, &descriptor); + WGPUBuffer buffer = wgpuDeviceCreateBuffer(device, &descriptor); - EXPECT_CALL(api, DeviceCreateBufferMapped(apiDevice, _)) - .WillOnce(Return(apiResult)) - .RetiresOnSaturation(); + EXPECT_CALL(api, DeviceCreateBuffer(apiDevice, _)).WillOnce(Return(apiBuffer)); + EXPECT_CALL(api, BufferGetMappedRange(apiBuffer, 0, 4)).WillOnce(Return(&apiBufferData)); FlushClient(); - wgpuBufferUnmap(result.buffer); + wgpuBufferUnmap(buffer); EXPECT_CALL(api, BufferUnmap(apiBuffer)).Times(1); FlushClient(); } -// Test that releasing after CreateBufferMapped does not call Unmap -TEST_F(WireBufferMappingTests, ReleaseAfterCreateBufferMapped) { +// Test that releasing a buffer mapped at creation does not call Unmap +TEST_F(WireBufferMappingTests, MappedAtCreationReleaseBeforeUnmap) { WGPUBufferDescriptor descriptor = {}; descriptor.size = 4; + descriptor.mappedAtCreation = true; WGPUBuffer apiBuffer = api.GetNewBuffer(); - WGPUCreateBufferMappedResult apiResult; uint32_t apiBufferData = 1234; - apiResult.buffer = apiBuffer; - apiResult.data = reinterpret_cast(&apiBufferData); - apiResult.dataLength = 4; - WGPUCreateBufferMappedResult result = wgpuDeviceCreateBufferMapped(device, &descriptor); + WGPUBuffer buffer = wgpuDeviceCreateBuffer(device, &descriptor); - EXPECT_CALL(api, DeviceCreateBufferMapped(apiDevice, _)) - .WillOnce(Return(apiResult)) - .RetiresOnSaturation(); + EXPECT_CALL(api, DeviceCreateBuffer(apiDevice, _)).WillOnce(Return(apiBuffer)); + EXPECT_CALL(api, BufferGetMappedRange(apiBuffer, 0, 4)).WillOnce(Return(&apiBufferData)); FlushClient(); - wgpuBufferRelease(result.buffer); + wgpuBufferRelease(buffer); EXPECT_CALL(api, BufferRelease(apiBuffer)).Times(1); FlushClient(); } -// Test that it is valid to map a buffer after CreateBufferMapped and Unmap -TEST_F(WireBufferMappingTests, CreateBufferMappedThenMapSuccess) { +// Test that it is valid to map a buffer after it is mapped at creation and unmapped +TEST_F(WireBufferMappingTests, MappedAtCreationThenMapSuccess) { WGPUBufferDescriptor descriptor = {}; descriptor.size = 4; + descriptor.mappedAtCreation = true; WGPUBuffer apiBuffer = api.GetNewBuffer(); - WGPUCreateBufferMappedResult apiResult; - uint32_t apiBufferData = 9863; - apiResult.buffer = apiBuffer; - apiResult.data = reinterpret_cast(&apiBufferData); - apiResult.dataLength = 4; + uint32_t apiBufferData = 1234; - WGPUCreateBufferMappedResult result = wgpuDeviceCreateBufferMapped(device, &descriptor); + WGPUBuffer buffer = wgpuDeviceCreateBuffer(device, &descriptor); - EXPECT_CALL(api, DeviceCreateBufferMapped(apiDevice, _)) - .WillOnce(Return(apiResult)) - .RetiresOnSaturation(); + EXPECT_CALL(api, DeviceCreateBuffer(apiDevice, _)).WillOnce(Return(apiBuffer)); + EXPECT_CALL(api, BufferGetMappedRange(apiBuffer, 0, 4)).WillOnce(Return(&apiBufferData)); FlushClient(); - wgpuBufferUnmap(result.buffer); + wgpuBufferUnmap(buffer); EXPECT_CALL(api, BufferUnmap(apiBuffer)).Times(1); FlushClient(); - wgpuBufferMapWriteAsync(result.buffer, ToMockBufferMapWriteCallback, nullptr); + wgpuBufferMapWriteAsync(buffer, ToMockBufferMapWriteCallback, nullptr); uint32_t zero = 0; EXPECT_CALL(api, OnBufferMapAsyncCallback(apiBuffer, _, _)).WillOnce(InvokeWithoutArgs([&]() { @@ -572,27 +560,23 @@ TEST_F(WireBufferMappingTests, CreateBufferMappedThenMapSuccess) { FlushServer(); } -// Test that it is invalid to map a buffer after CreateBufferMapped before Unmap -TEST_F(WireBufferMappingTests, CreateBufferMappedThenMapFailure) { +// Test that it is invalid to map a buffer after mappedAtCreation but before Unmap +TEST_F(WireBufferMappingTests, MappedAtCreationThenMapFailure) { WGPUBufferDescriptor descriptor = {}; descriptor.size = 4; + descriptor.mappedAtCreation = true; WGPUBuffer apiBuffer = api.GetNewBuffer(); - WGPUCreateBufferMappedResult apiResult; - uint32_t apiBufferData = 9863; - apiResult.buffer = apiBuffer; - apiResult.data = reinterpret_cast(&apiBufferData); - apiResult.dataLength = 4; + uint32_t apiBufferData = 1234; - WGPUCreateBufferMappedResult result = wgpuDeviceCreateBufferMapped(device, &descriptor); + WGPUBuffer buffer = wgpuDeviceCreateBuffer(device, &descriptor); - EXPECT_CALL(api, DeviceCreateBufferMapped(apiDevice, _)) - .WillOnce(Return(apiResult)) - .RetiresOnSaturation(); + EXPECT_CALL(api, DeviceCreateBuffer(apiDevice, _)).WillOnce(Return(apiBuffer)); + EXPECT_CALL(api, BufferGetMappedRange(apiBuffer, 0, 4)).WillOnce(Return(&apiBufferData)); FlushClient(); - wgpuBufferMapWriteAsync(result.buffer, ToMockBufferMapWriteCallback, nullptr); + wgpuBufferMapWriteAsync(buffer, ToMockBufferMapWriteCallback, nullptr); EXPECT_CALL(api, OnBufferMapAsyncCallback(apiBuffer, _, _)).WillOnce(InvokeWithoutArgs([&]() { api.CallMapAsyncCallback(apiBuffer, WGPUBufferMapAsyncStatus_Error); @@ -605,7 +589,7 @@ TEST_F(WireBufferMappingTests, CreateBufferMappedThenMapFailure) { FlushServer(); - wgpuBufferUnmap(result.buffer); + wgpuBufferUnmap(buffer); EXPECT_CALL(api, BufferUnmap(apiBuffer)).Times(1); FlushClient(); diff --git a/src/tests/unittests/wire/WireMemoryTransferServiceTests.cpp b/src/tests/unittests/wire/WireMemoryTransferServiceTests.cpp index 6c8179b8b5..dd986e42c6 100644 --- a/src/tests/unittests/wire/WireMemoryTransferServiceTests.cpp +++ b/src/tests/unittests/wire/WireMemoryTransferServiceTests.cpp @@ -155,24 +155,20 @@ class WireMemoryTransferServiceTests : public WireTest { return std::make_pair(apiBuffer, buffer); } - std::pair CreateBufferMapped() { + std::pair CreateBufferMapped() { WGPUBufferDescriptor descriptor = {}; descriptor.size = sizeof(mBufferContent); + descriptor.mappedAtCreation = true; WGPUBuffer apiBuffer = api.GetNewBuffer(); - WGPUCreateBufferMappedResult apiResult; - apiResult.buffer = apiBuffer; - apiResult.data = reinterpret_cast(&mMappedBufferContent); - apiResult.dataLength = sizeof(mMappedBufferContent); + WGPUBuffer buffer = wgpuDeviceCreateBuffer(device, &descriptor); - WGPUCreateBufferMappedResult result = wgpuDeviceCreateBufferMapped(device, &descriptor); + EXPECT_CALL(api, DeviceCreateBuffer(apiDevice, _)).WillOnce(Return(apiBuffer)); + EXPECT_CALL(api, BufferGetMappedRange(apiBuffer, 0, sizeof(mBufferContent))) + .WillOnce(Return(&mMappedBufferContent)); - EXPECT_CALL(api, DeviceCreateBufferMapped(apiDevice, _)) - .WillOnce(Return(apiResult)) - .RetiresOnSaturation(); - - return std::make_pair(apiResult, result); + return std::make_pair(apiBuffer, buffer); } ClientReadHandle* ExpectReadHandleCreation() { @@ -891,8 +887,8 @@ TEST_F(WireMemoryTransferServiceTests, BufferMapWriteDestroyBeforeUnmap) { } } -// Test successful CreateBufferMapped. -TEST_F(WireMemoryTransferServiceTests, CreateBufferMappedSuccess) { +// Test successful buffer creation with mappedAtCreation = true. +TEST_F(WireMemoryTransferServiceTests, MappedAtCreationSuccess) { // The client should create and serialize a WriteHandle on createBufferMapped. ClientWriteHandle* clientHandle = ExpectWriteHandleCreation(); @@ -904,9 +900,9 @@ TEST_F(WireMemoryTransferServiceTests, CreateBufferMappedSuccess) { // The server should then deserialize the WriteHandle from the client. ServerWriteHandle* serverHandle = ExpectServerWriteHandleDeserialization(); - WGPUCreateBufferMappedResult result; - WGPUCreateBufferMappedResult apiResult; - std::tie(apiResult, result) = CreateBufferMapped(); + WGPUBuffer buffer; + WGPUBuffer apiBuffer; + std::tie(apiBuffer, buffer) = CreateBufferMapped(); FlushClient(); // Update the mapped contents. @@ -916,35 +912,33 @@ TEST_F(WireMemoryTransferServiceTests, CreateBufferMappedSuccess) { ExpectClientWriteHandleSerializeFlush(clientHandle); EXPECT_CALL(clientMemoryTransferService, OnWriteHandleDestroy(clientHandle)).Times(1); - wgpuBufferUnmap(result.buffer); + wgpuBufferUnmap(buffer); // The server deserializes the Flush message. ExpectServerWriteHandleDeserializeFlush(serverHandle, mUpdatedBufferContent); // After the handle is updated it can be destroyed. EXPECT_CALL(serverMemoryTransferService, OnWriteHandleDestroy(serverHandle)).Times(1); - EXPECT_CALL(api, BufferUnmap(apiResult.buffer)).Times(1); + EXPECT_CALL(api, BufferUnmap(apiBuffer)).Times(1); FlushClient(); } -// Test CreateBufferMapped WriteHandle creation failure. -TEST_F(WireMemoryTransferServiceTests, CreateBufferMappedWriteHandleCreationFailure) { +// Test buffer creation with mappedAtCreation WriteHandle creation failure. +TEST_F(WireMemoryTransferServiceTests, MappedAtCreationWriteHandleCreationFailure) { // Mock a WriteHandle creation failure MockWriteHandleCreationFailure(); WGPUBufferDescriptor descriptor = {}; descriptor.size = sizeof(mBufferContent); + descriptor.mappedAtCreation = true; - WGPUCreateBufferMappedResult result = wgpuDeviceCreateBufferMapped(device, &descriptor); - - // TODO(enga): Check that the client generated a context lost. - EXPECT_EQ(result.data, nullptr); - EXPECT_EQ(result.dataLength, 0u); + WGPUBuffer buffer = wgpuDeviceCreateBuffer(device, &descriptor); + EXPECT_EQ(nullptr, wgpuBufferGetMappedRange(buffer, 0, sizeof(mBufferContent))); } -// Test CreateBufferMapped DeserializeWriteHandle failure. -TEST_F(WireMemoryTransferServiceTests, CreateBufferMappedDeserializeWriteHandleFailure) { +// Test buffer creation with mappedAtCreation DeserializeWriteHandle failure. +TEST_F(WireMemoryTransferServiceTests, MappedAtCreationDeserializeWriteHandleFailure) { // The client should create and serialize a WriteHandle on createBufferMapped. ClientWriteHandle* clientHandle = ExpectWriteHandleCreation(); @@ -956,16 +950,16 @@ TEST_F(WireMemoryTransferServiceTests, CreateBufferMappedDeserializeWriteHandleF // The server should then deserialize the WriteHandle from the client. MockServerWriteHandleDeserializeFailure(); - WGPUCreateBufferMappedResult result; - WGPUCreateBufferMappedResult apiResult; - std::tie(apiResult, result) = CreateBufferMapped(); + WGPUBuffer buffer; + WGPUBuffer apiBuffer; + std::tie(apiBuffer, buffer) = CreateBufferMapped(); FlushClient(false); EXPECT_CALL(clientMemoryTransferService, OnWriteHandleDestroy(clientHandle)).Times(1); } -// Test CreateBufferMapped handle Open failure. -TEST_F(WireMemoryTransferServiceTests, CreateBufferMappedHandleOpenFailure) { +// Test buffer creation with mappedAtCreation handle Open failure. +TEST_F(WireMemoryTransferServiceTests, MappedAtCreationHandleOpenFailure) { // The client should create a WriteHandle on createBufferMapped. ClientWriteHandle* clientHandle = ExpectWriteHandleCreation(); @@ -979,16 +973,14 @@ TEST_F(WireMemoryTransferServiceTests, CreateBufferMappedHandleOpenFailure) { WGPUBufferDescriptor descriptor = {}; descriptor.size = sizeof(mBufferContent); + descriptor.mappedAtCreation = true; - WGPUCreateBufferMappedResult result = wgpuDeviceCreateBufferMapped(device, &descriptor); - - // TODO(enga): Check that the client generated a context lost. - EXPECT_EQ(result.data, nullptr); - EXPECT_EQ(result.dataLength, 0u); + WGPUBuffer buffer = wgpuDeviceCreateBuffer(device, &descriptor); + EXPECT_EQ(nullptr, wgpuBufferGetMappedRange(buffer, 0, sizeof(mBufferContent))); } -// Test CreateBufferMapped DeserializeFlush failure. -TEST_F(WireMemoryTransferServiceTests, CreateBufferMappedDeserializeFlushFailure) { +// Test buffer creation with mappedAtCreation = true DeserializeFlush failure. +TEST_F(WireMemoryTransferServiceTests, MappedAtCreationDeserializeFlushFailure) { // The client should create and serialize a WriteHandle on createBufferMapped. ClientWriteHandle* clientHandle = ExpectWriteHandleCreation(); @@ -1000,9 +992,9 @@ TEST_F(WireMemoryTransferServiceTests, CreateBufferMappedDeserializeFlushFailure // The server should then deserialize the WriteHandle from the client. ServerWriteHandle* serverHandle = ExpectServerWriteHandleDeserialization(); - WGPUCreateBufferMappedResult result; - WGPUCreateBufferMappedResult apiResult; - std::tie(apiResult, result) = CreateBufferMapped(); + WGPUBuffer buffer; + WGPUBuffer apiBuffer; + std::tie(apiBuffer, buffer) = CreateBufferMapped(); FlushClient(); // Update the mapped contents. @@ -1012,7 +1004,7 @@ TEST_F(WireMemoryTransferServiceTests, CreateBufferMappedDeserializeFlushFailure ExpectClientWriteHandleSerializeFlush(clientHandle); EXPECT_CALL(clientMemoryTransferService, OnWriteHandleDestroy(clientHandle)).Times(1); - wgpuBufferUnmap(result.buffer); + wgpuBufferUnmap(buffer); // The server deserializes the Flush message. Mock a deserialization failure. MockServerWriteHandleDeserializeFlushFailure(serverHandle); @@ -1022,8 +1014,8 @@ TEST_F(WireMemoryTransferServiceTests, CreateBufferMappedDeserializeFlushFailure EXPECT_CALL(serverMemoryTransferService, OnWriteHandleDestroy(serverHandle)).Times(1); } -// Test CreateBufferMapped destroying the buffer before unmapping on the client side. -TEST_F(WireMemoryTransferServiceTests, CreateBufferMappedDestroyBeforeUnmap) { +// Test mappedAtCreation=true destroying the buffer before unmapping on the client side. +TEST_F(WireMemoryTransferServiceTests, MappedAtCreationDestroyBeforeUnmap) { // The client should create and serialize a WriteHandle on createBufferMapped. ClientWriteHandle* clientHandle = ExpectWriteHandleCreation(); @@ -1035,9 +1027,9 @@ TEST_F(WireMemoryTransferServiceTests, CreateBufferMappedDestroyBeforeUnmap) { // The server should then deserialize the WriteHandle from the client. ServerWriteHandle* serverHandle = ExpectServerWriteHandleDeserialization(); - WGPUCreateBufferMappedResult result; - WGPUCreateBufferMappedResult apiResult; - std::tie(apiResult, result) = CreateBufferMapped(); + WGPUBuffer buffer; + WGPUBuffer apiBuffer; + std::tie(apiBuffer, buffer) = CreateBufferMapped(); FlushClient(); // Update the mapped contents. @@ -1047,16 +1039,16 @@ TEST_F(WireMemoryTransferServiceTests, CreateBufferMappedDestroyBeforeUnmap) { // immediately, both in the client and server side. { EXPECT_CALL(clientMemoryTransferService, OnWriteHandleDestroy(clientHandle)).Times(1); - wgpuBufferDestroy(result.buffer); + wgpuBufferDestroy(buffer); EXPECT_CALL(serverMemoryTransferService, OnWriteHandleDestroy(serverHandle)).Times(1); - EXPECT_CALL(api, BufferDestroy(apiResult.buffer)).Times(1); + EXPECT_CALL(api, BufferDestroy(apiBuffer)).Times(1); FlushClient(); // The handle is already destroyed so unmap only results in a server unmap call. - wgpuBufferUnmap(result.buffer); + wgpuBufferUnmap(buffer); - EXPECT_CALL(api, BufferUnmap(apiResult.buffer)).Times(1); + EXPECT_CALL(api, BufferUnmap(apiBuffer)).Times(1); FlushClient(); } }