Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | Private Member Functions | List of all members
GrGpuBuffer Class Referenceabstract

#include <GrGpuBuffer.h>

Inheritance diagram for GrGpuBuffer:
GrGpuResource GrBuffer GrIORef< GrGpuResource > SkNoncopyable GrD3DBuffer GrGLBuffer GrMockBuffer GrMtlBuffer GrVkBuffer

Public Member Functions

GrAccessPattern accessPattern () const
 
size_t size () const final
 
void ref () const final
 
void unref () const final
 
void * map ()
 
void unmap ()
 
bool isMapped () const
 
bool isCpuBuffer () const final
 
bool clearToZero ()
 
bool updateData (const void *src, size_t offset, size_t size, bool preserve)
 
GrGpuBufferType intendedType () const
 
- Public Member Functions inherited from GrGpuResource
bool wasDestroyed () const
 
const GrDirectContextgetContext () const
 
GrDirectContextgetContext ()
 
size_t gpuMemorySize () const
 
UniqueID uniqueID () const
 
const skgpu::UniqueKeygetUniqueKey () const
 
std::string getLabel () const
 
void setLabel (std::string_view label)
 
CacheAccess cacheAccess ()
 
const CacheAccess cacheAccess () const
 
ProxyAccess proxyAccess ()
 
ResourcePriv resourcePriv ()
 
const ResourcePriv resourcePriv () const
 
virtual void dumpMemoryStatistics (SkTraceMemoryDump *traceMemoryDump) const
 
- Public Member Functions inherited from GrIORef< GrGpuResource >
bool unique () const
 
void ref () const
 
void unref () const
 
void refCommandBuffer () const
 
void unrefCommandBuffer () const
 
- Public Member Functions inherited from SkNoncopyable
 SkNoncopyable ()=default
 
 SkNoncopyable (SkNoncopyable &&)=default
 
SkNoncopyableoperator= (SkNoncopyable &&)=default
 
- Public Member Functions inherited from GrBuffer
 GrBuffer (const GrBuffer &)=delete
 
GrBufferoperator= (const GrBuffer &)=delete
 
virtual ~GrBuffer ()=default
 

Static Public Member Functions

static void ComputeScratchKeyForDynamicBuffer (size_t size, GrGpuBufferType, skgpu::ScratchKey *)
 
- Static Public Member Functions inherited from GrGpuResource
static uint32_t CreateUniqueID ()
 

Protected Types

enum class  MapType { kRead , kWriteDiscard }
 

Protected Member Functions

 GrGpuBuffer (GrGpu *, size_t sizeInBytes, GrGpuBufferType, GrAccessPattern, std::string_view label)
 
- Protected Member Functions inherited from GrGpuResource
void registerWithCache (skgpu::Budgeted)
 
void registerWithCacheWrapped (GrWrapCacheable)
 
 GrGpuResource (GrGpu *, std::string_view label)
 
virtual ~GrGpuResource ()
 
GrGpugetGpu () const
 
virtual void onRelease ()
 
virtual void onAbandon ()
 
virtual void setMemoryBacking (SkTraceMemoryDump *, const SkString &) const
 
SkString getResourceName () const
 
void dumpMemoryStatisticsPriv (SkTraceMemoryDump *traceMemoryDump, const SkString &resourceName, const char *type, size_t size) const
 
- Protected Member Functions inherited from GrIORef< GrGpuResource >
 GrIORef ()
 
bool internalHasRef () const
 
bool internalHasNoCommandBufferUsages () const
 
void addInitialRef () const
 
- Protected Member Functions inherited from GrBuffer
 GrBuffer ()=default
 

Protected Attributes

void * fMapPtr
 

Private Member Functions

virtual void onMap (MapType)=0
 
virtual void onUnmap (MapType)=0
 
virtual bool onClearToZero ()=0
 
virtual bool onUpdateData (const void *src, size_t offset, size_t size, bool preserve)=0
 
size_t onGpuMemorySize () const override
 
void onSetLabel () override
 
const char * getResourceType () const override
 
void computeScratchKey (skgpu::ScratchKey *key) const override
 

Additional Inherited Members

- Public Types inherited from GrIORef< GrGpuResource >
enum  LastRemovedRef
 

Detailed Description

Definition at line 24 of file GrGpuBuffer.h.

Member Enumeration Documentation

◆ MapType

enum class GrGpuBuffer::MapType
strongprotected
Enumerator
kRead 

Maps for reading. The effect of writes is undefined.

kWriteDiscard 

Maps for writing. The existing contents are discarded and the initial contents of the buffer. Reads (even after overwriting initial contents) should be avoided for performance reasons as the memory may not be cached.

Definition at line 108 of file GrGpuBuffer.h.

108 {
109 /** Maps for reading. The effect of writes is undefined. */
110 kRead,
111 /**
112 * Maps for writing. The existing contents are discarded and the initial contents of the
113 * buffer. Reads (even after overwriting initial contents) should be avoided for performance
114 * reasons as the memory may not be cached.
115 */
117 };

Constructor & Destructor Documentation

◆ GrGpuBuffer()

GrGpuBuffer::GrGpuBuffer ( GrGpu gpu,
size_t  sizeInBytes,
GrGpuBufferType  type,
GrAccessPattern  pattern,
std::string_view  label 
)
protected

Definition at line 19 of file GrGpuBuffer.cpp.

22 : GrGpuResource(gpu, label)
23 , fMapPtr(nullptr)
24 , fSizeInBytes(sizeInBytes)
25 , fAccessPattern(pattern)
26 , fIntendedType(type) {}
void * fMapPtr

Member Function Documentation

◆ accessPattern()

GrAccessPattern GrGpuBuffer::accessPattern ( ) const
inline

Definition at line 32 of file GrGpuBuffer.h.

32{ return fAccessPattern; }

◆ clearToZero()

bool GrGpuBuffer::clearToZero ( )

Overwrites the buffer with zero bytes. Always fails for GrGpuBufferType::kXferGpuToCpu buffers. The buffer must not currently be mapped.

Definition at line 49 of file GrGpuBuffer.cpp.

49 {
50 SkASSERT(!this->isMapped());
51
52 if (this->wasDestroyed()) {
53 return false;
54 }
55
57 return false;
58 }
59
60 return this->onClearToZero();
61}
#define SkASSERT(cond)
Definition SkAssert.h:116
GrGpuBufferType intendedType() const
Definition GrGpuBuffer.h:99
virtual bool onClearToZero()=0
bool isMapped() const
bool wasDestroyed() const

◆ computeScratchKey()

void GrGpuBuffer::computeScratchKey ( skgpu::ScratchKey ) const
overrideprivatevirtual

Called by the registerWithCache if the resource is available to be used as scratch. Resource subclasses should override this if the instances should be recycled as scratch resources and populate the scratchKey with the key. By default resources are not recycled as scratch.

Reimplemented from GrGpuResource.

Definition at line 98 of file GrGpuBuffer.cpp.

98 {
99 if (kDynamic_GrAccessPattern == fAccessPattern) {
100 ComputeScratchKeyForDynamicBuffer(fSizeInBytes, fIntendedType, key);
101 }
102}
@ kDynamic_GrAccessPattern
static void ComputeScratchKeyForDynamicBuffer(size_t size, GrGpuBufferType, skgpu::ScratchKey *)

◆ ComputeScratchKeyForDynamicBuffer()

void GrGpuBuffer::ComputeScratchKeyForDynamicBuffer ( size_t  size,
GrGpuBufferType  intendedType,
skgpu::ScratchKey key 
)
static

Computes a scratch key for a GPU-side buffer with a "dynamic" access pattern. (Buffers with "static" and "stream" patterns are disqualified by nature from being cached and reused.)

Definition at line 86 of file GrGpuBuffer.cpp.

88 {
90 skgpu::ScratchKey::Builder builder(key, kType, 1 + (sizeof(size_t) + 3) / 4);
92 builder[1] = (uint32_t)size;
93 if (sizeof(size_t) > 4) {
94 builder[2] = (uint32_t)((uint64_t)size >> 32);
95 }
96}
constexpr uint32_t SkToU32(S x)
Definition SkTo.h:26
size_t size() const final
Definition GrGpuBuffer.h:34
static ResourceType GenerateResourceType()
uint32_t ResourceType

◆ getResourceType()

const char * GrGpuBuffer::getResourceType ( ) const
inlineoverrideprivatevirtual

Describes the type of gpu resource that is represented by the implementing class (e.g. texture, buffer object, stencil). This data is used for diagnostic purposes by dumpMemoryStatistics().

The value returned is expected to be long lived and will not be copied by the caller.

Implements GrGpuResource.

Definition at line 135 of file GrGpuBuffer.h.

135{ return "Buffer Object"; }

◆ intendedType()

GrGpuBufferType GrGpuBuffer::intendedType ( ) const
inline

Definition at line 99 of file GrGpuBuffer.h.

99{ return fIntendedType; }

◆ isCpuBuffer()

bool GrGpuBuffer::isCpuBuffer ( ) const
inlinefinalvirtual

Is this an instance of GrCpuBuffer? Otherwise, an instance of GrGpuBuffer.

Implements GrBuffer.

Definition at line 71 of file GrGpuBuffer.h.

71{ return false; }

◆ isMapped()

bool GrGpuBuffer::isMapped ( ) const

Queries whether the buffer has been mapped.

Returns
true if the buffer is mapped, false otherwise.

Definition at line 47 of file GrGpuBuffer.cpp.

47{ return SkToBool(fMapPtr); }
static constexpr bool SkToBool(const T &x)
Definition SkTo.h:35

◆ map()

void * GrGpuBuffer::map ( )

Maps the buffer to be read or written by the CPU.

It is an error to draw from the buffer while it is mapped or transfer to/from the buffer. It may fail if the backend doesn't support mapping the buffer. Once a buffer is mapped, subsequent calls to map() trivially succeed. No matter how many times map() is called, umap() will unmap the buffer on the first call if it is mapped.

If the buffer is of type GrGpuBufferType::kXferGpuToCpu then it is mapped for reading only. Otherwise it is mapped writing only. Writing to a buffer that is mapped for reading or vice versa produces undefined results. If the buffer is mapped for writing then the buffer's previous contents are invalidated.

Returns
a pointer to the data or nullptr if the map fails.

Definition at line 28 of file GrGpuBuffer.cpp.

28 {
29 if (this->wasDestroyed()) {
30 return nullptr;
31 }
32 if (!fMapPtr) {
33 this->onMap(this->mapType());
34 }
35 return fMapPtr;
36}
virtual void onMap(MapType)=0

◆ onClearToZero()

virtual bool GrGpuBuffer::onClearToZero ( )
privatepure virtual

◆ onGpuMemorySize()

size_t GrGpuBuffer::onGpuMemorySize ( ) const
inlineoverrideprivatevirtual

Implements GrGpuResource.

Definition at line 133 of file GrGpuBuffer.h.

133{ return fSizeInBytes; }

◆ onMap()

virtual void GrGpuBuffer::onMap ( MapType  )
privatepure virtual

◆ onSetLabel()

void GrGpuBuffer::onSetLabel ( )
inlineoverrideprivatevirtual

Implements GrGpuResource.

Reimplemented in GrMtlBuffer.

Definition at line 134 of file GrGpuBuffer.h.

134{}

◆ onUnmap()

virtual void GrGpuBuffer::onUnmap ( MapType  )
privatepure virtual

◆ onUpdateData()

virtual bool GrGpuBuffer::onUpdateData ( const void *  src,
size_t  offset,
size_t  size,
bool  preserve 
)
privatepure virtual

◆ ref()

void GrGpuBuffer::ref ( ) const
inlinefinalvirtual

Implements GrBuffer.

Definition at line 36 of file GrGpuBuffer.h.

◆ size()

size_t GrGpuBuffer::size ( ) const
inlinefinalvirtual

Size of the buffer in bytes.

Implements GrBuffer.

Definition at line 34 of file GrGpuBuffer.h.

34{ return fSizeInBytes; }

◆ unmap()

void GrGpuBuffer::unmap ( )

Unmaps the buffer if it is mapped.

The pointer returned by the previous map call will no longer be valid.

Definition at line 38 of file GrGpuBuffer.cpp.

38 {
39 if (this->wasDestroyed()) {
40 return;
41 }
43 this->onUnmap(this->mapType());
44 fMapPtr = nullptr;
45}
virtual void onUnmap(MapType)=0

◆ unref()

void GrGpuBuffer::unref ( ) const
inlinefinalvirtual

Implements GrBuffer.

Definition at line 38 of file GrGpuBuffer.h.

◆ updateData()

bool GrGpuBuffer::updateData ( const void *  src,
size_t  offset,
size_t  size,
bool  preserve 
)

Updates the buffer data.

The size of the buffer will be preserved. The src data will be placed at offset. If preserve is false then any remaining content before/after the range [offset, offset+size) becomes undefined. Preserving updates will fail if the size and offset are not aligned to GrCaps::bufferUpdateDataPreserveAlignment().

The buffer must not be mapped.

Fails for GrGpuBufferType::kXferGpuToCpu.

Note that buffer updates do not go through GrContext and therefore are not serialized with other operations.

Returns
returns true if the update succeeds, false otherwise.

Definition at line 63 of file GrGpuBuffer.cpp.

63 {
64 SkASSERT(!this->isMapped());
65 SkASSERT(size > 0 && offset + size <= fSizeInBytes);
66 SkASSERT(src);
67
68 if (this->wasDestroyed()) {
69 return false;
70 }
71
72 if (preserve) {
73 size_t a = this->getGpu()->caps()->bufferUpdateDataPreserveAlignment();
74 if (SkAlignTo(offset, a) != offset || SkAlignTo(size, a) != size) {
75 return false;
76 }
77 }
78
80 return false;
81 }
82
83 return this->onUpdateData(src, offset, size, preserve);
84}
static constexpr size_t SkAlignTo(size_t x, size_t alignment)
Definition SkAlign.h:33
size_t bufferUpdateDataPreserveAlignment() const
Definition GrCaps.h:250
virtual bool onUpdateData(const void *src, size_t offset, size_t size, bool preserve)=0
GrGpu * getGpu() const
const GrCaps * caps() const
Definition GrGpu.h:73
struct MyStruct a[10]
Point offset

Member Data Documentation

◆ fMapPtr

void* GrGpuBuffer::fMapPtr
protected

Definition at line 119 of file GrGpuBuffer.h.


The documentation for this class was generated from the following files: