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/GrCaps.h"
9 #include "src/gpu/GrGpu.h"
10 #include "src/gpu/GrGpuBuffer.h"
11
GrGpuBuffer(GrGpu * gpu,size_t sizeInBytes,GrGpuBufferType type,GrAccessPattern pattern)12 GrGpuBuffer::GrGpuBuffer(GrGpu* gpu, size_t sizeInBytes, GrGpuBufferType type,
13 GrAccessPattern pattern)
14 : GrGpuResource(gpu)
15 , fMapPtr(nullptr)
16 , fSizeInBytes(sizeInBytes)
17 , fAccessPattern(pattern)
18 , fIntendedType(type) {}
19
map()20 void* GrGpuBuffer::map() {
21 if (this->wasDestroyed()) {
22 return nullptr;
23 }
24 SkASSERT(!fHasWrittenToBuffer || fAccessPattern == kDynamic_GrAccessPattern);
25 if (!fMapPtr) {
26 this->onMap();
27 }
28 return fMapPtr;
29 }
30
unmap()31 void GrGpuBuffer::unmap() {
32 if (this->wasDestroyed()) {
33 return;
34 }
35 SkASSERT(fMapPtr);
36 this->onUnmap();
37 fMapPtr = nullptr;
38 #ifdef SK_DEBUG
39 fHasWrittenToBuffer = true;
40 #endif
41 }
42
isMapped() const43 bool GrGpuBuffer::isMapped() const { return SkToBool(fMapPtr); }
44
updateData(const void * src,size_t srcSizeInBytes)45 bool GrGpuBuffer::updateData(const void* src, size_t srcSizeInBytes) {
46 SkASSERT(!fHasWrittenToBuffer || fAccessPattern == kDynamic_GrAccessPattern);
47 SkASSERT(!this->isMapped());
48 SkASSERT(srcSizeInBytes <= fSizeInBytes);
49 if (this->intendedType() == GrGpuBufferType::kXferGpuToCpu) {
50 return false;
51 }
52 bool result = this->onUpdateData(src, srcSizeInBytes);
53 #ifdef SK_DEBUG
54 if (result) {
55 fHasWrittenToBuffer = true;
56 }
57 #endif
58 return result;
59 }
60
ComputeScratchKeyForDynamicBuffer(size_t size,GrGpuBufferType intendedType,GrScratchKey * key)61 void GrGpuBuffer::ComputeScratchKeyForDynamicBuffer(size_t size, GrGpuBufferType intendedType,
62 GrScratchKey* key) {
63 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
64 GrScratchKey::Builder builder(key, kType, 1 + (sizeof(size_t) + 3) / 4);
65 builder[0] = SkToU32(intendedType);
66 builder[1] = (uint32_t)size;
67 if (sizeof(size_t) > 4) {
68 builder[2] = (uint32_t)((uint64_t)size >> 32);
69 }
70 }
71
computeScratchKey(GrScratchKey * key) const72 void GrGpuBuffer::computeScratchKey(GrScratchKey* key) const {
73 if (SkIsPow2(fSizeInBytes) && kDynamic_GrAccessPattern == fAccessPattern) {
74 ComputeScratchKeyForDynamicBuffer(fSizeInBytes, fIntendedType, key);
75 }
76 }
77