Stephen White | 5048a6a | 2019-07-29 17:07:44 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "src/gpu/dawn/GrDawnBuffer.h" |
| 9 | |
| 10 | #include "src/gpu/dawn/GrDawnGpu.h" |
| 11 | |
| 12 | namespace { |
| 13 | dawn::BufferUsageBit GrGpuBufferTypeToDawnUsageBit(GrGpuBufferType type) { |
| 14 | switch (type) { |
| 15 | case GrGpuBufferType::kVertex: |
| 16 | return dawn::BufferUsageBit::Vertex; |
| 17 | case GrGpuBufferType::kIndex: |
| 18 | return dawn::BufferUsageBit::Index; |
| 19 | case GrGpuBufferType::kXferCpuToGpu: |
| 20 | return dawn::BufferUsageBit::CopySrc; |
| 21 | case GrGpuBufferType::kXferGpuToCpu: |
| 22 | return dawn::BufferUsageBit::CopyDst; |
| 23 | default: |
| 24 | SkASSERT(!"buffer type not supported by Dawn"); |
| 25 | return dawn::BufferUsageBit::Vertex; |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | GrDawnBuffer::GrDawnBuffer(GrDawnGpu* gpu, size_t sizeInBytes, GrGpuBufferType type, |
| 31 | GrAccessPattern pattern) |
| 32 | : INHERITED(gpu, sizeInBytes, type, pattern) |
Stephen White | 7fba36b | 2019-09-10 13:05:22 -0400 | [diff] [blame] | 33 | , fStagingBuffer(nullptr) { |
Stephen White | 5048a6a | 2019-07-29 17:07:44 -0400 | [diff] [blame] | 34 | dawn::BufferDescriptor bufferDesc; |
| 35 | bufferDesc.size = sizeInBytes; |
| 36 | bufferDesc.usage = GrGpuBufferTypeToDawnUsageBit(type) | dawn::BufferUsageBit::CopyDst; |
| 37 | fBuffer = this->getDawnGpu()->device().CreateBuffer(&bufferDesc); |
| 38 | this->registerWithCache(SkBudgeted::kYes); |
| 39 | } |
| 40 | |
| 41 | GrDawnBuffer::~GrDawnBuffer() { |
Stephen White | 5048a6a | 2019-07-29 17:07:44 -0400 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void GrDawnBuffer::onMap() { |
| 45 | if (this->wasDestroyed()) { |
| 46 | return; |
| 47 | } |
Stephen White | 7fba36b | 2019-09-10 13:05:22 -0400 | [diff] [blame] | 48 | fStagingBuffer = getDawnGpu()->getStagingBuffer(this->size()); |
| 49 | fMapPtr = fStagingBuffer->fData; |
Stephen White | 5048a6a | 2019-07-29 17:07:44 -0400 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void GrDawnBuffer::onUnmap() { |
| 53 | if (this->wasDestroyed()) { |
| 54 | return; |
| 55 | } |
Stephen White | 7fba36b | 2019-09-10 13:05:22 -0400 | [diff] [blame] | 56 | fStagingBuffer->fBuffer.Unmap(); |
| 57 | fMapPtr = nullptr; |
| 58 | getDawnGpu()->getCopyEncoder() |
| 59 | .CopyBufferToBuffer(fStagingBuffer->fBuffer, 0, fBuffer, 0, this->size()); |
Stephen White | 5048a6a | 2019-07-29 17:07:44 -0400 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | bool GrDawnBuffer::onUpdateData(const void* src, size_t srcSizeInBytes) { |
| 63 | if (this->wasDestroyed()) { |
| 64 | return false; |
| 65 | } |
Stephen White | 7fba36b | 2019-09-10 13:05:22 -0400 | [diff] [blame] | 66 | this->onMap(); |
| 67 | memcpy(fStagingBuffer->fData, src, srcSizeInBytes); |
| 68 | this->onUnmap(); |
Stephen White | 5048a6a | 2019-07-29 17:07:44 -0400 | [diff] [blame] | 69 | return true; |
| 70 | } |
| 71 | |
| 72 | GrDawnGpu* GrDawnBuffer::getDawnGpu() const { |
| 73 | SkASSERT(!this->wasDestroyed()); |
| 74 | return static_cast<GrDawnGpu*>(this->getGpu()); |
| 75 | } |