blob: 825a3ce7996dcd160eeacacebdcbadbd7a6da90a [file] [log] [blame]
Stephen White5048a6a2019-07-29 17:07:44 -04001/*
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
12namespace {
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
30GrDawnBuffer::GrDawnBuffer(GrDawnGpu* gpu, size_t sizeInBytes, GrGpuBufferType type,
31 GrAccessPattern pattern)
32 : INHERITED(gpu, sizeInBytes, type, pattern)
Stephen White7fba36b2019-09-10 13:05:22 -040033 , fStagingBuffer(nullptr) {
Stephen White5048a6a2019-07-29 17:07:44 -040034 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
41GrDawnBuffer::~GrDawnBuffer() {
Stephen White5048a6a2019-07-29 17:07:44 -040042}
43
44void GrDawnBuffer::onMap() {
45 if (this->wasDestroyed()) {
46 return;
47 }
Stephen White7fba36b2019-09-10 13:05:22 -040048 fStagingBuffer = getDawnGpu()->getStagingBuffer(this->size());
49 fMapPtr = fStagingBuffer->fData;
Stephen White5048a6a2019-07-29 17:07:44 -040050}
51
52void GrDawnBuffer::onUnmap() {
53 if (this->wasDestroyed()) {
54 return;
55 }
Stephen White7fba36b2019-09-10 13:05:22 -040056 fStagingBuffer->fBuffer.Unmap();
57 fMapPtr = nullptr;
58 getDawnGpu()->getCopyEncoder()
59 .CopyBufferToBuffer(fStagingBuffer->fBuffer, 0, fBuffer, 0, this->size());
Stephen White5048a6a2019-07-29 17:07:44 -040060}
61
62bool GrDawnBuffer::onUpdateData(const void* src, size_t srcSizeInBytes) {
63 if (this->wasDestroyed()) {
64 return false;
65 }
Stephen White7fba36b2019-09-10 13:05:22 -040066 this->onMap();
67 memcpy(fStagingBuffer->fData, src, srcSizeInBytes);
68 this->onUnmap();
Stephen White5048a6a2019-07-29 17:07:44 -040069 return true;
70}
71
72GrDawnGpu* GrDawnBuffer::getDawnGpu() const {
73 SkASSERT(!this->wasDestroyed());
74 return static_cast<GrDawnGpu*>(this->getGpu());
75}