• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
MakeCopy(VertexMode mode,int vertexCount,const SkPoint positions[],const SkPoint texs[],const SkColor colors[])45     static sk_sp<SkVertices> MakeCopy(VertexMode mode, int vertexCount,
46                                       const SkPoint positions[],
47                                       const SkPoint texs[],
48                                       const SkColor colors[]) {
49         return MakeCopy(mode,
50                         vertexCount,
51                         positions,
52                         texs,
53                         colors,
54                         0,
55                         nullptr);
56     }
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 
isValid()66         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 
uniqueID()93     uint32_t uniqueID() const { return fUniqueID; }
bounds()94     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     /** Writes text representation of SkVertices to string.
104 
105         @param desc     the string storing a description of parameters.
106         @param depth    the number of tabs preceding each line.
107     */
108     void dump(std::string& desc, int depth) const;
109 
110 private:
SkVertices()111     SkVertices() {}
112 
113     friend class SkVerticesPriv;
114 
115     // these are needed since we've manually sized our allocation (see Builder::init)
116     friend class SkNVRefCnt<SkVertices>;
117     void operator delete(void* p);
118 
119     Sizes getSizes() const;
120 
121     // we store this first, to pair with the refcnt in our base-class, so we don't have an
122     // unnecessary pad between it and the (possibly 8-byte aligned) ptrs.
123     uint32_t fUniqueID;
124 
125     // these point inside our allocation, so none of these can be "freed"
126     SkPoint*     fPositions;        // [vertexCount]
127     uint16_t*    fIndices;          // [indexCount] or null
128     SkPoint*     fTexs;             // [vertexCount] or null
129     SkColor*     fColors;           // [vertexCount] or null
130 
131     SkRect  fBounds;    // computed to be the union of the fPositions[]
132     int     fVertexCount;
133     int     fIndexCount;
134 
135     VertexMode fMode;
136     // below here is where the actual array data is stored.
137 };
138 
139 #endif
140