Skia
2D Graphics Library
SkVertices.h
Go to the documentation of this file.
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 
24 class SK_API SkVertices : public SkNVRefCnt<SkVertices> {
25  struct Desc;
26  struct Sizes;
27 public:
28  enum VertexMode {
32 
33  kLast_VertexMode = kTriangleFan_VertexMode,
34  };
35 
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 
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 SK_API Builder {
65  public:
66  Builder(VertexMode mode, int vertexCount, int indexCount, uint32_t flags);
67 
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.
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 
95  uint32_t uniqueID() const { return fUniqueID; }
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:
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
Types, consts, functions, and macros for colors.
uint32_t SkColor
32-bit ARGB color value, unpremultiplied.
Definition: SkColor.h:37
SkData holds an immutable data buffer.
Definition: SkData.h:25
Definition: SkRefCnt.h:160
Definition: SkVertices.h:64
SkPoint * positions()
sk_sp< SkVertices > detach()
uint16_t * indices()
SkPoint * texCoords()
bool isValid() const
Definition: SkVertices.h:68
Builder(VertexMode mode, int vertexCount, int indexCount, uint32_t flags)
SkColor * colors()
An immutable set of vertex data that can be used with SkCanvas::drawVertices.
Definition: SkVertices.h:24
BuilderFlags
Definition: SkVertices.h:60
static sk_sp< SkVertices > MakeCopy(VertexMode mode, int vertexCount, const SkPoint positions[], const SkPoint texs[], const SkColor colors[], int indexCount, const uint16_t indices[])
Create a vertices by copying the specified arrays.
const SkRect & bounds() const
Definition: SkVertices.h:96
uint32_t uniqueID() const
Definition: SkVertices.h:95
VertexMode
Definition: SkVertices.h:28
@ kTriangleStrip_VertexMode
Definition: SkVertices.h:30
@ kTriangleFan_VertexMode
Definition: SkVertices.h:31
@ kTriangles_VertexMode
Definition: SkVertices.h:29
static sk_sp< SkVertices > MakeCopy(VertexMode mode, int vertexCount, const SkPoint positions[], const SkPoint texs[], const SkColor colors[])
Definition: SkVertices.h:47
size_t approximateSize() const
SkVerticesPriv priv()
const SkVerticesPriv priv() const
SkRect holds four float coordinates describing the upper and lower bounds of a rectangle.
Definition: SkRect.h:582