Coverage Report

Created: 2021-08-22 09:07

/src/skia/src/gpu/GrVertexChunkArray.h
Line
Count
Source (jump to first uncovered line)
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/private/SkNoncopyable.h"
12
#include "include/private/SkTArray.h"
13
#include "src/gpu/GrBuffer.h"
14
#include "src/gpu/GrVertexWriter.h"
15
16
class GrMeshDrawTarget;
17
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.
21
struct GrVertexChunk {
22
    sk_sp<const GrBuffer> fBuffer;
23
    int fCount = 0;
24
    int fBase;  // baseVertex or baseInstance, depending on the use case.
25
};
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.
31
using 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.
35
class GrVertexChunkBuilder : SkNoncopyable {
36
public:
37
    GrVertexChunkBuilder(GrMeshDrawTarget* target, GrVertexChunkArray* chunks, size_t stride,
38
                         int minVerticesPerChunk)
39
            : fTarget(target)
40
            , fChunks(chunks)
41
            , fStride(stride)
42
0
            , fMinVerticesPerChunk(minVerticesPerChunk) {
43
0
        SkASSERT(fMinVerticesPerChunk > 0);
44
0
    }
Unexecuted instantiation: GrVertexChunkBuilder::GrVertexChunkBuilder(GrMeshDrawTarget*, SkSTArray<1, GrVertexChunk, false>*, unsigned long, int)
Unexecuted instantiation: GrVertexChunkBuilder::GrVertexChunkBuilder(GrMeshDrawTarget*, SkSTArray<1, GrVertexChunk, false>*, unsigned long, int)
45
46
    ~GrVertexChunkBuilder();
47
48
    // Appends 'count' contiguous vertices. These vertices are not guaranteed to be contiguous with
49
    // previous or future calls to appendVertices.
50
0
    SK_ALWAYS_INLINE GrVertexWriter appendVertices(int count) {
51
0
        SkASSERT(count > 0);
52
0
        if (fCurrChunkVertexCount + count > fCurrChunkVertexCapacity && !this->allocChunk(count)) {
53
0
            SkDEBUGCODE(fLastAppendAmount = 0;)
54
0
            return {nullptr};
55
0
        }
56
0
        SkASSERT(fCurrChunkVertexCount + count <= fCurrChunkVertexCapacity);
57
0
        fCurrChunkVertexCount += count;
58
0
        SkDEBUGCODE(fLastAppendAmount = count;)
59
0
        return std::exchange(fCurrChunkVertexWriter,
60
0
                             fCurrChunkVertexWriter.makeOffset(fStride * count));
61
0
    }
Unexecuted instantiation: GrVertexChunkBuilder::appendVertices(int)
Unexecuted instantiation: GrVertexChunkBuilder::appendVertices(int)
62
63
0
    SK_ALWAYS_INLINE GrVertexWriter appendVertex() { return this->appendVertices(1); }
64
65
    // 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
0
    void popVertices(int count) {
68
0
        SkASSERT(count <= fLastAppendAmount);
69
0
        SkASSERT(fLastAppendAmount <= fCurrChunkVertexCount);
70
0
        SkASSERT(count >= 0);
71
0
        fCurrChunkVertexCount -= count;
72
0
        fCurrChunkVertexWriter = fCurrChunkVertexWriter.makeOffset(fStride * -count);
73
0
        SkDEBUGCODE(fLastAppendAmount -= count;)
74
0
    }
Unexecuted instantiation: GrVertexChunkBuilder::popVertices(int)
Unexecuted instantiation: GrVertexChunkBuilder::popVertices(int)
75
76
private:
77
    bool allocChunk(int minCount);
78
79
    GrMeshDrawTarget* const fTarget;
80
    GrVertexChunkArray* const fChunks;
81
    const size_t fStride;
82
    int fMinVerticesPerChunk;
83
84
    GrVertexWriter fCurrChunkVertexWriter;
85
    int fCurrChunkVertexCount = 0;
86
    int fCurrChunkVertexCapacity = 0;
87
88
    SkDEBUGCODE(int fLastAppendAmount = 0;)
89
};
90
91
#endif