• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ganesh/GrCaps.h"
9 #include "src/gpu/ganesh/GrGpu.h"
10 #include "src/gpu/ganesh/GrGpuBuffer.h"
11 
GrGpuBuffer(GrGpu * gpu,size_t sizeInBytes,GrGpuBufferType type,GrAccessPattern pattern,std::string_view label)12 GrGpuBuffer::GrGpuBuffer(GrGpu* gpu, size_t sizeInBytes, GrGpuBufferType type,
13                          GrAccessPattern pattern,
14                          std::string_view label)
15         : GrGpuResource(gpu, label)
16         , fMapPtr(nullptr)
17         , fSizeInBytes(sizeInBytes)
18         , fAccessPattern(pattern)
19         , fIntendedType(type) {}
20 
map()21 void* GrGpuBuffer::map() {
22     if (this->wasDestroyed()) {
23         return nullptr;
24     }
25     if (!fMapPtr) {
26         this->onMap(this->mapType());
27     }
28     return fMapPtr;
29 }
30 
unmap()31 void GrGpuBuffer::unmap() {
32     if (this->wasDestroyed()) {
33         return;
34     }
35     SkASSERT(fMapPtr);
36     this->onUnmap(this->mapType());
37     fMapPtr = nullptr;
38 }
39 
isMapped() const40 bool GrGpuBuffer::isMapped() const { return SkToBool(fMapPtr); }
41 
clearToZero()42 bool GrGpuBuffer::clearToZero() {
43     SkASSERT(!this->isMapped());
44 
45     if (this->wasDestroyed()) {
46         return false;
47     }
48 
49     if (this->intendedType()  == GrGpuBufferType::kXferGpuToCpu) {
50         return false;
51     }
52 
53     return this->onClearToZero();
54 }
55 
updateData(const void * src,size_t offset,size_t size,bool preserve)56 bool GrGpuBuffer::updateData(const void* src, size_t offset, size_t size, bool preserve) {
57     SkASSERT(!this->isMapped());
58     SkASSERT(size > 0 && offset + size <= fSizeInBytes);
59     SkASSERT(src);
60 
61     if (this->wasDestroyed()) {
62         return false;
63     }
64 
65     if (preserve) {
66         size_t a = this->getGpu()->caps()->bufferUpdateDataPreserveAlignment();
67         if (SkAlignTo(offset, a) != offset || SkAlignTo(size, a) != size) {
68             return false;
69         }
70     }
71 
72     if (this->intendedType() == GrGpuBufferType::kXferGpuToCpu) {
73         return false;
74     }
75 
76     return this->onUpdateData(src, offset, size, preserve);
77 }
78 
ComputeScratchKeyForDynamicBuffer(size_t size,GrGpuBufferType intendedType,skgpu::ScratchKey * key)79 void GrGpuBuffer::ComputeScratchKeyForDynamicBuffer(size_t size,
80                                                     GrGpuBufferType intendedType,
81                                                     skgpu::ScratchKey* key) {
82     static const skgpu::ScratchKey::ResourceType kType = skgpu::ScratchKey::GenerateResourceType();
83     skgpu::ScratchKey::Builder builder(key, kType, 1 + (sizeof(size_t) + 3) / 4);
84     builder[0] = SkToU32(intendedType);
85     builder[1] = (uint32_t)size;
86     if (sizeof(size_t) > 4) {
87         builder[2] = (uint32_t)((uint64_t)size >> 32);
88     }
89 }
90 
computeScratchKey(skgpu::ScratchKey * key) const91 void GrGpuBuffer::computeScratchKey(skgpu::ScratchKey* key) const {
92     if (SkIsPow2(fSizeInBytes) && kDynamic_GrAccessPattern == fAccessPattern) {
93         ComputeScratchKeyForDynamicBuffer(fSizeInBytes, fIntendedType, key);
94     }
95 }
96