Flutter Engine
The Flutter Engine
GrVertexChunkArray.h
Go to the documentation of this file.
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
17
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.
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.
38
39// Builds a GrVertexChunkArray. The provided Target must not be used externally throughout the
40// entire lifetime of this object.
42public:
44 size_t stride, int minVerticesPerChunk)
45 : fTarget(target)
46 , fChunks(chunks)
47 , fStride(stride)
48 , fMinVerticesPerChunk(minVerticesPerChunk) {
49 SkASSERT(fMinVerticesPerChunk > 0);
50 }
51
53
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.
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().
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
82private:
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
int count
Definition: FontMgrTest.cpp:50
#define SkASSERT(cond)
Definition: SkAssert.h:116
#define SK_ALWAYS_INLINE
Definition: SkAttributes.h:30
SkDEBUGCODE(SK_SPI) SkThreadID SkGetThreadID()
void popVertices(int count)
GrVertexChunkBuilder(GrMeshDrawTarget *target, GrVertexChunkArray *chunks, size_t stride, int minVerticesPerChunk)
SK_ALWAYS_INLINE skgpu::VertexWriter appendVertices(int count)
uint8_t value
uint32_t * target
sk_sp< const GrBuffer > fBuffer
std::true_type sk_is_trivially_relocatable
VertexWriter makeOffset(size_t offsetInBytes) const
Definition: BufferWriter.h:142