1 /* 2 * Copyright 2021 Google LLC. 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 GrVertexChunkArray_DEFINED 9 #define GrVertexChunkArray_DEFINED 10 11 #include "include/core/SkRefCnt.h" 12 #include "include/private/base/SkNoncopyable.h" 13 #include "include/private/base/SkTArray.h" 14 #include "include/private/base/SkTypeTraits.h" 15 #include "src/gpu/BufferWriter.h" 16 #include "src/gpu/ganesh/GrBuffer.h" 17 18 class GrMeshDrawTarget; 19 20 // Represents a chunk of vertex data. Use with GrVertexChunkArray and GrVertexChunkBuilder. We write 21 // the data out in chunks when we don't start out knowing exactly how many vertices (or instances) 22 // we will end up writing. 23 struct GrVertexChunk { 24 sk_sp<const GrBuffer> fBuffer; 25 int fCount = 0; 26 int fBase; // baseVertex or baseInstance, depending on the use case. 27 28 static_assert(::sk_is_trivially_relocatable<decltype(fBuffer)>::value); 29 30 using sk_is_trivially_relocatable = std::true_type; 31 }; 32 33 // Represents an array of GrVertexChunks. 34 // 35 // We only preallocate 1 chunk because if the array needs to grow, then we're also allocating a 36 // brand new GPU buffer anyway. 37 using GrVertexChunkArray = SkSTArray<1, GrVertexChunk>; 38 39 // Builds a GrVertexChunkArray. The provided Target must not be used externally throughout the 40 // entire lifetime of this object. 41 class GrVertexChunkBuilder : SkNoncopyable { 42 public: GrVertexChunkBuilder(GrMeshDrawTarget * target,GrVertexChunkArray * chunks,size_t stride,int minVerticesPerChunk)43 GrVertexChunkBuilder(GrMeshDrawTarget* target, GrVertexChunkArray* chunks, 44 size_t stride, int minVerticesPerChunk) 45 : fTarget(target) 46 , fChunks(chunks) 47 , fStride(stride) 48 , fMinVerticesPerChunk(minVerticesPerChunk) { 49 SkASSERT(fMinVerticesPerChunk > 0); 50 } 51 52 ~GrVertexChunkBuilder(); 53 stride()54 size_t stride() const { return fStride; } 55 56 // Appends 'count' contiguous vertices. These vertices are not guaranteed to be contiguous with 57 // previous or future calls to appendVertices. appendVertices(int count)58 SK_ALWAYS_INLINE skgpu::VertexWriter appendVertices(int count) { 59 SkASSERT(count > 0); 60 if (fCurrChunkVertexCount + count > fCurrChunkVertexCapacity && !this->allocChunk(count)) { 61 SkDEBUGCODE(fLastAppendAmount = 0;) 62 return {}; 63 } 64 SkASSERT(fCurrChunkVertexCount + count <= fCurrChunkVertexCapacity); 65 fCurrChunkVertexCount += count; 66 SkDEBUGCODE(fLastAppendAmount = count;) 67 return std::exchange(fCurrChunkVertexWriter, 68 fCurrChunkVertexWriter.makeOffset(fStride * count)); 69 } 70 71 // Pops the most recent 'count' contiguous vertices. Since there is no guarantee of contiguity 72 // between appends, 'count' may be no larger than the most recent call to appendVertices(). popVertices(int count)73 void popVertices(int count) { 74 SkASSERT(count <= fLastAppendAmount); 75 SkASSERT(fLastAppendAmount <= fCurrChunkVertexCount); 76 SkASSERT(count >= 0); 77 fCurrChunkVertexCount -= count; 78 fCurrChunkVertexWriter = fCurrChunkVertexWriter.makeOffset(fStride * -count); 79 SkDEBUGCODE(fLastAppendAmount -= count;) 80 } 81 82 private: 83 bool allocChunk(int minCount); 84 85 GrMeshDrawTarget* const fTarget; 86 GrVertexChunkArray* const fChunks; 87 const size_t fStride; 88 int fMinVerticesPerChunk; 89 90 skgpu::VertexWriter fCurrChunkVertexWriter; 91 int fCurrChunkVertexCount = 0; 92 int fCurrChunkVertexCapacity = 0; 93 94 SkDEBUGCODE(int fLastAppendAmount = 0;) 95 }; 96 97 #endif 98