/src/skia/include/core/SkVertices.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2017 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 SkVertices_DEFINED |
9 | | #define SkVertices_DEFINED |
10 | | |
11 | | #include "include/core/SkColor.h" |
12 | | #include "include/core/SkRect.h" |
13 | | #include "include/core/SkRefCnt.h" |
14 | | |
15 | | class SkData; |
16 | | struct SkPoint; |
17 | | class SkVerticesPriv; |
18 | | |
19 | | /** |
20 | | * An immutable set of vertex data that can be used with SkCanvas::drawVertices. |
21 | | */ |
22 | | class SK_API SkVertices : public SkNVRefCnt<SkVertices> { |
23 | | struct Desc; |
24 | | struct Sizes; |
25 | | public: |
26 | | enum VertexMode { |
27 | | kTriangles_VertexMode, |
28 | | kTriangleStrip_VertexMode, |
29 | | kTriangleFan_VertexMode, |
30 | | |
31 | | kLast_VertexMode = kTriangleFan_VertexMode, |
32 | | }; |
33 | | |
34 | | /** |
35 | | * Create a vertices by copying the specified arrays. texs, colors may be nullptr, |
36 | | * and indices is ignored if indexCount == 0. |
37 | | */ |
38 | | static sk_sp<SkVertices> MakeCopy(VertexMode mode, int vertexCount, |
39 | | const SkPoint positions[], |
40 | | const SkPoint texs[], |
41 | | const SkColor colors[], |
42 | | int indexCount, |
43 | | const uint16_t indices[]); |
44 | | |
45 | | static sk_sp<SkVertices> MakeCopy(VertexMode mode, int vertexCount, |
46 | | const SkPoint positions[], |
47 | | const SkPoint texs[], |
48 | 9 | const SkColor colors[]) { |
49 | 9 | return MakeCopy(mode, |
50 | 9 | vertexCount, |
51 | 9 | positions, |
52 | 9 | texs, |
53 | 9 | colors, |
54 | 9 | 0, |
55 | 9 | nullptr); |
56 | 9 | } |
57 | | |
58 | | enum BuilderFlags { |
59 | | kHasTexCoords_BuilderFlag = 1 << 0, |
60 | | kHasColors_BuilderFlag = 1 << 1, |
61 | | }; |
62 | | class Builder { |
63 | | public: |
64 | | Builder(VertexMode mode, int vertexCount, int indexCount, uint32_t flags); |
65 | | |
66 | 4.79k | bool isValid() const { return fVertices != nullptr; } |
67 | | |
68 | | SkPoint* positions(); |
69 | | uint16_t* indices(); // returns null if there are no indices |
70 | | |
71 | | // If we have custom attributes, these will always be null |
72 | | SkPoint* texCoords(); // returns null if there are no texCoords |
73 | | SkColor* colors(); // returns null if there are no colors |
74 | | |
75 | | // Detach the built vertices object. After the first call, this will always return null. |
76 | | sk_sp<SkVertices> detach(); |
77 | | |
78 | | private: |
79 | | Builder(const Desc&); |
80 | | |
81 | | void init(const Desc&); |
82 | | |
83 | | // holds a partially complete object. only completed in detach() |
84 | | sk_sp<SkVertices> fVertices; |
85 | | // Extra storage for intermediate vertices in the case where the client specifies indexed |
86 | | // triangle fans. These get converted to indexed triangles when the Builder is finalized. |
87 | | std::unique_ptr<uint8_t[]> fIntermediateFanIndices; |
88 | | |
89 | | friend class SkVertices; |
90 | | friend class SkVerticesPriv; |
91 | | }; |
92 | | |
93 | 0 | uint32_t uniqueID() const { return fUniqueID; } |
94 | 2.52k | const SkRect& bounds() const { return fBounds; } |
95 | | |
96 | | // returns approximate byte size of the vertices object |
97 | | size_t approximateSize() const; |
98 | | |
99 | | // Provides access to functions that aren't part of the public API. |
100 | | SkVerticesPriv priv(); |
101 | | const SkVerticesPriv priv() const; // NOLINT(readability-const-return-type) |
102 | | |
103 | | private: |
104 | 138k | SkVertices() {} |
105 | | |
106 | | friend class SkVerticesPriv; |
107 | | |
108 | | // these are needed since we've manually sized our allocation (see Builder::init) |
109 | | friend class SkNVRefCnt<SkVertices>; |
110 | | void operator delete(void* p); |
111 | | |
112 | | Sizes getSizes() const; |
113 | | |
114 | | // we store this first, to pair with the refcnt in our base-class, so we don't have an |
115 | | // unnecessary pad between it and the (possibly 8-byte aligned) ptrs. |
116 | | uint32_t fUniqueID; |
117 | | |
118 | | // these point inside our allocation, so none of these can be "freed" |
119 | | SkPoint* fPositions; // [vertexCount] |
120 | | uint16_t* fIndices; // [indexCount] or null |
121 | | SkPoint* fTexs; // [vertexCount] or null |
122 | | SkColor* fColors; // [vertexCount] or null |
123 | | |
124 | | SkRect fBounds; // computed to be the union of the fPositions[] |
125 | | int fVertexCount; |
126 | | int fIndexCount; |
127 | | |
128 | | VertexMode fMode; |
129 | | // below here is where the actual array data is stored. |
130 | | }; |
131 | | |
132 | | #endif |