BufferBase: Split validation in helper methods

This commit is contained in:
Corentin Wallez 2018-07-18 21:20:07 +02:00 committed by Corentin Wallez
parent 1ea205fb12
commit 7914958034
2 changed files with 38 additions and 22 deletions

View File

@ -78,13 +78,7 @@ namespace backend {
} }
void BufferBase::SetSubData(uint32_t start, uint32_t count, const uint8_t* data) { void BufferBase::SetSubData(uint32_t start, uint32_t count, const uint8_t* data) {
if (start + count > GetSize()) { if (mDevice->ConsumedError(ValidateSetSubData(start, count))) {
mDevice->HandleError("Buffer subdata out of range");
return;
}
if (!(mAllowedUsage & dawn::BufferUsageBit::TransferDst)) {
mDevice->HandleError("Buffer needs the transfer dst usage bit");
return; return;
} }
@ -95,7 +89,7 @@ namespace backend {
uint32_t size, uint32_t size,
dawnBufferMapReadCallback callback, dawnBufferMapReadCallback callback,
dawnCallbackUserdata userdata) { dawnCallbackUserdata userdata) {
if (!ValidateMapBase(start, size, dawn::BufferUsageBit::MapRead)) { if (mDevice->ConsumedError(ValidateMap(start, size, dawn::BufferUsageBit::MapRead))) {
callback(DAWN_BUFFER_MAP_ASYNC_STATUS_ERROR, nullptr, userdata); callback(DAWN_BUFFER_MAP_ASYNC_STATUS_ERROR, nullptr, userdata);
return; return;
} }
@ -115,7 +109,7 @@ namespace backend {
uint32_t size, uint32_t size,
dawnBufferMapWriteCallback callback, dawnBufferMapWriteCallback callback,
dawnCallbackUserdata userdata) { dawnCallbackUserdata userdata) {
if (!ValidateMapBase(start, size, dawn::BufferUsageBit::MapWrite)) { if (mDevice->ConsumedError(ValidateMap(start, size, dawn::BufferUsageBit::MapWrite))) {
callback(DAWN_BUFFER_MAP_ASYNC_STATUS_ERROR, nullptr, userdata); callback(DAWN_BUFFER_MAP_ASYNC_STATUS_ERROR, nullptr, userdata);
return; return;
} }
@ -132,8 +126,7 @@ namespace backend {
} }
void BufferBase::Unmap() { void BufferBase::Unmap() {
if (!mIsMapped) { if (mDevice->ConsumedError(ValidateUnmap())) {
mDevice->HandleError("Buffer wasn't mapped");
return; return;
} }
@ -148,26 +141,44 @@ namespace backend {
mMapUserdata = 0; mMapUserdata = 0;
} }
bool BufferBase::ValidateMapBase(uint32_t start, MaybeError BufferBase::ValidateSetSubData(uint32_t start, uint32_t count) const {
uint32_t size, // TODO(cwallez@chromium.org): check for overflows.
dawn::BufferUsageBit requiredUsage) { if (start + count > GetSize()) {
DAWN_RETURN_ERROR("Buffer subdata out of range");
}
if (!(mAllowedUsage & dawn::BufferUsageBit::TransferDst)) {
DAWN_RETURN_ERROR("Buffer needs the transfer dst usage bit");
}
return {};
}
MaybeError BufferBase::ValidateMap(uint32_t start,
uint32_t size,
dawn::BufferUsageBit requiredUsage) const {
// TODO(cwallez@chromium.org): check for overflows. // TODO(cwallez@chromium.org): check for overflows.
if (start + size > GetSize()) { if (start + size > GetSize()) {
mDevice->HandleError("Buffer map read out of range"); DAWN_RETURN_ERROR("Buffer map read out of range");
return false;
} }
if (mIsMapped) { if (mIsMapped) {
mDevice->HandleError("Buffer already mapped"); DAWN_RETURN_ERROR("Buffer already mapped");
return false;
} }
if (!(mAllowedUsage & requiredUsage)) { if (!(mAllowedUsage & requiredUsage)) {
mDevice->HandleError("Buffer needs the correct map usage bit"); DAWN_RETURN_ERROR("Buffer needs the correct map usage bit");
return false;
} }
return true; return {};
}
MaybeError BufferBase::ValidateUnmap() const {
if (!mIsMapped) {
DAWN_RETURN_ERROR("Buffer wasn't mapped");
}
return {};
} }
// BufferBuilder // BufferBuilder

View File

@ -16,6 +16,7 @@
#define BACKEND_BUFFER_H_ #define BACKEND_BUFFER_H_
#include "backend/Builder.h" #include "backend/Builder.h"
#include "backend/Error.h"
#include "backend/Forward.h" #include "backend/Forward.h"
#include "backend/RefCounted.h" #include "backend/RefCounted.h"
@ -66,7 +67,11 @@ namespace backend {
virtual void MapWriteAsyncImpl(uint32_t serial, uint32_t start, uint32_t size) = 0; virtual void MapWriteAsyncImpl(uint32_t serial, uint32_t start, uint32_t size) = 0;
virtual void UnmapImpl() = 0; virtual void UnmapImpl() = 0;
bool ValidateMapBase(uint32_t start, uint32_t size, dawn::BufferUsageBit requiredUsage); MaybeError ValidateSetSubData(uint32_t start, uint32_t count) const;
MaybeError ValidateMap(uint32_t start,
uint32_t size,
dawn::BufferUsageBit requiredUsage) const;
MaybeError ValidateUnmap() const;
DeviceBase* mDevice; DeviceBase* mDevice;
uint32_t mSize; uint32_t mSize;