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