mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-05-14 03:11:29 +00:00
Static_casts are prefered over reinterpret_casts for better type safety Change-Id: I190cbee293591ebf8ab8035e900c081848eb1f30 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/6921 Commit-Queue: Kai Ninomiya <kainino@chromium.org> Reviewed-by: Kai Ninomiya <kainino@chromium.org>
157 lines
5.7 KiB
C++
157 lines
5.7 KiB
C++
// Copyright 2019 The Dawn Authors
|
|
//
|
|
// 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.
|
|
|
|
#include "common/Assert.h"
|
|
#include "dawn_wire/server/Server.h"
|
|
|
|
#include <memory>
|
|
|
|
namespace dawn_wire { namespace server {
|
|
|
|
bool Server::PreHandleBufferUnmap(const BufferUnmapCmd& cmd) {
|
|
auto* selfData = BufferObjects().Get(cmd.selfId);
|
|
DAWN_ASSERT(selfData != nullptr);
|
|
|
|
selfData->mappedData = nullptr;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool Server::DoBufferMapAsync(ObjectId bufferId,
|
|
uint32_t requestSerial,
|
|
bool isWrite) {
|
|
// These requests are just forwarded to the buffer, with userdata containing what the
|
|
// client will require in the return command.
|
|
|
|
// The null object isn't valid as `self`
|
|
if (bufferId == 0) {
|
|
return false;
|
|
}
|
|
|
|
auto* buffer = BufferObjects().Get(bufferId);
|
|
if (buffer == nullptr) {
|
|
return false;
|
|
}
|
|
|
|
auto* data = new MapUserdata;
|
|
data->server = this;
|
|
data->buffer = ObjectHandle{bufferId, buffer->serial};
|
|
data->requestSerial = requestSerial;
|
|
data->isWrite = isWrite;
|
|
|
|
auto userdata = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(data));
|
|
|
|
if (isWrite) {
|
|
mProcs.bufferMapWriteAsync(buffer->handle, ForwardBufferMapWriteAsync, userdata);
|
|
} else {
|
|
mProcs.bufferMapReadAsync(buffer->handle, ForwardBufferMapReadAsync, userdata);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool Server::DoBufferUpdateMappedData(ObjectId bufferId, uint32_t count, const uint8_t* data) {
|
|
// The null object isn't valid as `self`
|
|
if (bufferId == 0) {
|
|
return false;
|
|
}
|
|
|
|
auto* buffer = BufferObjects().Get(bufferId);
|
|
if (buffer == nullptr || buffer->mappedData == nullptr || buffer->mappedDataSize != count) {
|
|
return false;
|
|
}
|
|
|
|
if (data == nullptr) {
|
|
return false;
|
|
}
|
|
|
|
memcpy(buffer->mappedData, data, count);
|
|
|
|
return true;
|
|
}
|
|
|
|
void Server::ForwardBufferMapReadAsync(DawnBufferMapAsyncStatus status,
|
|
const void* ptr,
|
|
uint64_t dataLength,
|
|
DawnCallbackUserdata userdata) {
|
|
auto data = reinterpret_cast<MapUserdata*>(static_cast<uintptr_t>(userdata));
|
|
data->server->OnBufferMapReadAsyncCallback(status, ptr, dataLength, data);
|
|
}
|
|
|
|
void Server::ForwardBufferMapWriteAsync(DawnBufferMapAsyncStatus status,
|
|
void* ptr,
|
|
uint64_t dataLength,
|
|
DawnCallbackUserdata userdata) {
|
|
auto data = reinterpret_cast<MapUserdata*>(static_cast<uintptr_t>(userdata));
|
|
data->server->OnBufferMapWriteAsyncCallback(status, ptr, dataLength, data);
|
|
}
|
|
|
|
void Server::OnBufferMapReadAsyncCallback(DawnBufferMapAsyncStatus status,
|
|
const void* ptr,
|
|
uint64_t dataLength,
|
|
MapUserdata* userdata) {
|
|
std::unique_ptr<MapUserdata> data(userdata);
|
|
|
|
// Skip sending the callback if the buffer has already been destroyed.
|
|
auto* bufferData = BufferObjects().Get(data->buffer.id);
|
|
if (bufferData == nullptr || bufferData->serial != data->buffer.serial) {
|
|
return;
|
|
}
|
|
|
|
ReturnBufferMapReadAsyncCallbackCmd cmd;
|
|
cmd.buffer = data->buffer;
|
|
cmd.requestSerial = data->requestSerial;
|
|
cmd.status = status;
|
|
cmd.dataLength = 0;
|
|
cmd.data = static_cast<const uint8_t*>(ptr);
|
|
|
|
if (status == DAWN_BUFFER_MAP_ASYNC_STATUS_SUCCESS) {
|
|
cmd.dataLength = dataLength;
|
|
}
|
|
|
|
size_t requiredSize = cmd.GetRequiredSize();
|
|
char* allocatedBuffer = static_cast<char*>(GetCmdSpace(requiredSize));
|
|
cmd.Serialize(allocatedBuffer);
|
|
}
|
|
|
|
void Server::OnBufferMapWriteAsyncCallback(DawnBufferMapAsyncStatus status,
|
|
void* ptr,
|
|
uint64_t dataLength,
|
|
MapUserdata* userdata) {
|
|
std::unique_ptr<MapUserdata> data(userdata);
|
|
|
|
// Skip sending the callback if the buffer has already been destroyed.
|
|
auto* bufferData = BufferObjects().Get(data->buffer.id);
|
|
if (bufferData == nullptr || bufferData->serial != data->buffer.serial) {
|
|
return;
|
|
}
|
|
|
|
ReturnBufferMapWriteAsyncCallbackCmd cmd;
|
|
cmd.buffer = data->buffer;
|
|
cmd.requestSerial = data->requestSerial;
|
|
cmd.status = status;
|
|
cmd.dataLength = dataLength;
|
|
|
|
size_t requiredSize = cmd.GetRequiredSize();
|
|
char* allocatedBuffer = static_cast<char*>(GetCmdSpace(requiredSize));
|
|
cmd.Serialize(allocatedBuffer);
|
|
|
|
if (status == DAWN_BUFFER_MAP_ASYNC_STATUS_SUCCESS) {
|
|
bufferData->mappedData = ptr;
|
|
bufferData->mappedDataSize = dataLength;
|
|
}
|
|
}
|
|
|
|
}} // namespace dawn_wire::server
|