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 #ifndef GrGpuBuffer_DEFINED 9 #define GrGpuBuffer_DEFINED 10 11 #include "src/gpu/ganesh/GrBuffer.h" 12 #include "src/gpu/ganesh/GrGpuResource.h" 13 14 class GrGpu; 15 16 class GrGpuBuffer : public GrGpuResource, public GrBuffer { 17 public: 18 /** 19 * Computes a scratch key for a GPU-side buffer with a "dynamic" access pattern. (Buffers with 20 * "static" and "stream" patterns are disqualified by nature from being cached and reused.) 21 */ 22 static void ComputeScratchKeyForDynamicBuffer(size_t size, GrGpuBufferType, skgpu::ScratchKey*); 23 accessPattern()24 GrAccessPattern accessPattern() const { return fAccessPattern; } 25 size()26 size_t size() const final { return fSizeInBytes; } 27 ref()28 void ref() const final { GrGpuResource::ref(); } 29 unref()30 void unref() const final { GrGpuResource::unref(); } 31 32 /** 33 * Maps the buffer to be read or written by the CPU. 34 * 35 * It is an error to draw from the buffer while it is mapped or transfer to/from the buffer. It 36 * may fail if the backend doesn't support mapping the buffer. Once a buffer is mapped, 37 * subsequent calls to map() trivially succeed. No matter how many times map() is called, 38 * umap() will unmap the buffer on the first call if it is mapped. 39 * 40 * If the buffer is of type GrGpuBufferType::kXferGpuToCpu then it is mapped for reading only. 41 * Otherwise it is mapped writing only. Writing to a buffer that is mapped for reading or vice 42 * versa produces undefined results. If the buffer is mapped for writing then the buffer's 43 * previous contents are invalidated. 44 * 45 * @return a pointer to the data or nullptr if the map fails. 46 */ 47 void* map(); 48 49 /** 50 * Unmaps the buffer if it is mapped. 51 * 52 * The pointer returned by the previous map call will no longer be valid. 53 */ 54 void unmap(); 55 56 /** 57 * Queries whether the buffer has been mapped. 58 * 59 * @return true if the buffer is mapped, false otherwise. 60 */ 61 bool isMapped() const; 62 isCpuBuffer()63 bool isCpuBuffer() const final { return false; } 64 65 /** 66 * Overwrites the buffer with zero bytes. Always fails for GrGpuBufferType::kXferGpuToCpu 67 * buffers. The buffer must not currently be mapped. 68 */ 69 bool clearToZero(); 70 71 /** 72 * Updates the buffer data. 73 * 74 * The size of the buffer will be preserved. The src data will be 75 * placed at offset. If preserve is false then any remaining content 76 * before/after the range [offset, offset+size) becomes undefined. 77 * Preserving updates will fail if the size and offset are not aligned 78 * to GrCaps::bufferUpdateDataPreserveAlignment(). 79 * 80 * The buffer must not be mapped. 81 * 82 * Fails for GrGpuBufferType::kXferGpuToCpu. 83 * 84 * Note that buffer updates do not go through GrContext and therefore are 85 * not serialized with other operations. 86 * 87 * @return returns true if the update succeeds, false otherwise. 88 */ 89 bool updateData(const void* src, size_t offset, size_t size, bool preserve); 90 intendedType()91 GrGpuBufferType intendedType() const { return fIntendedType; } 92 93 protected: 94 GrGpuBuffer(GrGpu*, 95 size_t sizeInBytes, 96 GrGpuBufferType, 97 GrAccessPattern, 98 std::string_view label); 99 100 enum class MapType { 101 /** Maps for reading. The effect of writes is undefined. */ 102 kRead, 103 /** 104 * Maps for writing. The existing contents are discarded and the initial contents of the 105 * buffer. Reads (even after overwriting initial contents) should be avoided for performance 106 * reasons as the memory may not be cached. 107 */ 108 kWriteDiscard, 109 }; 110 111 void* fMapPtr; 112 113 private: 114 /** Currently MapType is determined entirely by the buffer type, as documented in map(). */ mapType()115 MapType mapType() const { 116 return this->intendedType() == GrGpuBufferType::kXferGpuToCpu ? MapType::kRead 117 : MapType::kWriteDiscard; 118 } 119 120 virtual void onMap(MapType) = 0; 121 virtual void onUnmap(MapType) = 0; 122 virtual bool onClearToZero() = 0; 123 virtual bool onUpdateData(const void* src, size_t offset, size_t size, bool preserve) = 0; 124 onGpuMemorySize()125 size_t onGpuMemorySize() const override { return fSizeInBytes; } onSetLabel()126 void onSetLabel() override{} getResourceType()127 const char* getResourceType() const override { return "Buffer Object"; } 128 void computeScratchKey(skgpu::ScratchKey* key) const override; 129 130 size_t fSizeInBytes; 131 GrAccessPattern fAccessPattern; 132 GrGpuBufferType fIntendedType; 133 }; 134 135 #endif 136