blob: 3d570d8f29427036b8c9c1c1d814261f2fc96adc [file] [log] [blame]
Chris Dalton8ed7a8d2021-03-31 10:40:29 -06001/*
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/private/SkNoncopyable.h"
12#include "include/private/SkTArray.h"
13#include "src/gpu/GrBuffer.h"
14#include "src/gpu/GrVertexWriter.h"
Robert Phillips71143952021-06-17 14:55:07 -040015
16class GrMeshDrawTarget;
Chris Dalton8ed7a8d2021-03-31 10:40:29 -060017
18// Represents a chunk of vertex data. Use with GrVertexChunkArray and GrVertexChunkBuilder. We write
19// the data out in chunks when we don't start out knowing exactly how many vertices (or instances)
20// we will end up writing.
21struct GrVertexChunk {
22 sk_sp<const GrBuffer> fBuffer;
Chris Dalton82007f52021-04-20 00:45:50 -060023 int fCount = 0;
24 int fBase; // baseVertex or baseInstance, depending on the use case.
Chris Dalton8ed7a8d2021-03-31 10:40:29 -060025};
26
27// Represents an array of GrVertexChunks.
28//
29// We only preallocate 1 chunk because if the array needs to grow, then we're also allocating a
30// brand new GPU buffer anyway.
31using GrVertexChunkArray = SkSTArray<1, GrVertexChunk>;
32
33// Builds a GrVertexChunkArray. The provided Target must not be used externally throughout the
34// entire lifetime of this object.
35class GrVertexChunkBuilder : SkNoncopyable {
36public:
Robert Phillips71143952021-06-17 14:55:07 -040037 GrVertexChunkBuilder(GrMeshDrawTarget* target, GrVertexChunkArray* chunks, size_t stride,
Chris Dalton8ed7a8d2021-03-31 10:40:29 -060038 int minVerticesPerChunk)
39 : fTarget(target)
40 , fChunks(chunks)
41 , fStride(stride)
42 , fMinVerticesPerChunk(minVerticesPerChunk) {
43 SkASSERT(fMinVerticesPerChunk > 0);
44 }
45
Robert Phillips71143952021-06-17 14:55:07 -040046 ~GrVertexChunkBuilder();
Chris Dalton8ed7a8d2021-03-31 10:40:29 -060047
48 // Appends 'count' contiguous vertices. These vertices are not guaranteed to be contiguous with
49 // previous or future calls to appendVertices.
50 SK_ALWAYS_INLINE GrVertexWriter appendVertices(int count) {
51 SkASSERT(count > 0);
52 if (fCurrChunkVertexCount + count > fCurrChunkVertexCapacity && !this->allocChunk(count)) {
Chris Dalton8447f132021-05-21 15:54:23 -060053 SkDEBUGCODE(fLastAppendAmount = 0;)
Chris Dalton8ed7a8d2021-03-31 10:40:29 -060054 return {nullptr};
55 }
56 SkASSERT(fCurrChunkVertexCount + count <= fCurrChunkVertexCapacity);
57 fCurrChunkVertexCount += count;
Chris Dalton8447f132021-05-21 15:54:23 -060058 SkDEBUGCODE(fLastAppendAmount = count;)
Chris Dalton8ed7a8d2021-03-31 10:40:29 -060059 return std::exchange(fCurrChunkVertexWriter,
60 fCurrChunkVertexWriter.makeOffset(fStride * count));
61 }
62
63 SK_ALWAYS_INLINE GrVertexWriter appendVertex() { return this->appendVertices(1); }
64
Chris Dalton8447f132021-05-21 15:54:23 -060065 // Pops the most recent 'count' contiguous vertices. Since there is no guarantee of contiguity
66 // between appends, 'count' may be no larger than the most recent call to appendVertices().
67 void popVertices(int count) {
68 SkASSERT(count <= fLastAppendAmount);
69 SkASSERT(fLastAppendAmount <= fCurrChunkVertexCount);
70 SkASSERT(count >= 0);
71 fCurrChunkVertexCount -= count;
72 fCurrChunkVertexWriter = fCurrChunkVertexWriter.makeOffset(fStride * -count);
73 SkDEBUGCODE(fLastAppendAmount -= count;)
74 }
75
Chris Dalton8ed7a8d2021-03-31 10:40:29 -060076private:
Robert Phillips71143952021-06-17 14:55:07 -040077 bool allocChunk(int minCount);
Chris Dalton8ed7a8d2021-03-31 10:40:29 -060078
Robert Phillips71143952021-06-17 14:55:07 -040079 GrMeshDrawTarget* const fTarget;
Chris Dalton8ed7a8d2021-03-31 10:40:29 -060080 GrVertexChunkArray* const fChunks;
81 const size_t fStride;
Chris Dalton1c933f82021-05-20 19:25:53 -060082 int fMinVerticesPerChunk;
Chris Dalton8ed7a8d2021-03-31 10:40:29 -060083
84 GrVertexWriter fCurrChunkVertexWriter;
85 int fCurrChunkVertexCount = 0;
86 int fCurrChunkVertexCapacity = 0;
Chris Dalton8447f132021-05-21 15:54:23 -060087
88 SkDEBUGCODE(int fLastAppendAmount = 0;)
Chris Dalton8ed7a8d2021-03-31 10:40:29 -060089};
90
91#endif