2017-02-06 14:41:10 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkVertices_DEFINED
|
|
|
|
#define SkVertices_DEFINED
|
|
|
|
|
|
|
|
#include "SkColor.h"
|
2017-03-14 18:10:54 +00:00
|
|
|
#include "SkData.h"
|
2017-02-06 14:41:10 +00:00
|
|
|
#include "SkPoint.h"
|
|
|
|
#include "SkRect.h"
|
|
|
|
#include "SkRefCnt.h"
|
|
|
|
|
|
|
|
/**
|
2017-03-16 18:38:48 +00:00
|
|
|
* An immutable set of vertex data that can be used with SkCanvas::drawVertices.
|
2017-02-06 14:41:10 +00:00
|
|
|
*/
|
2017-09-20 19:51:08 +00:00
|
|
|
class SK_API SkVertices : public SkNVRefCnt<SkVertices> {
|
2017-02-06 14:41:10 +00:00
|
|
|
public:
|
2017-04-03 15:11:09 +00:00
|
|
|
enum VertexMode {
|
|
|
|
kTriangles_VertexMode,
|
|
|
|
kTriangleStrip_VertexMode,
|
|
|
|
kTriangleFan_VertexMode,
|
2018-01-23 16:50:25 +00:00
|
|
|
|
|
|
|
kLast_VertexMode = kTriangleFan_VertexMode,
|
2017-04-03 15:11:09 +00:00
|
|
|
};
|
|
|
|
|
2017-03-14 16:04:16 +00:00
|
|
|
/**
|
|
|
|
* Create a vertices by copying the specified arrays. texs and colors may be nullptr,
|
|
|
|
* and indices is ignored if indexCount == 0.
|
|
|
|
*/
|
2017-04-03 15:11:09 +00:00
|
|
|
static sk_sp<SkVertices> MakeCopy(VertexMode mode, int vertexCount,
|
2017-03-14 16:04:16 +00:00
|
|
|
const SkPoint positions[],
|
|
|
|
const SkPoint texs[],
|
|
|
|
const SkColor colors[],
|
|
|
|
int indexCount,
|
|
|
|
const uint16_t indices[]);
|
2017-02-06 14:41:10 +00:00
|
|
|
|
2017-04-03 15:11:09 +00:00
|
|
|
static sk_sp<SkVertices> MakeCopy(VertexMode mode, int vertexCount,
|
2017-03-14 16:04:16 +00:00
|
|
|
const SkPoint positions[],
|
|
|
|
const SkPoint texs[],
|
|
|
|
const SkColor colors[]) {
|
|
|
|
return MakeCopy(mode, vertexCount, positions, texs, colors, 0, nullptr);
|
2017-02-06 14:41:10 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 18:38:48 +00:00
|
|
|
struct Sizes;
|
|
|
|
|
|
|
|
enum BuilderFlags {
|
|
|
|
kHasTexCoords_BuilderFlag = 1 << 0,
|
|
|
|
kHasColors_BuilderFlag = 1 << 1,
|
2017-03-14 16:04:16 +00:00
|
|
|
};
|
|
|
|
class Builder {
|
|
|
|
public:
|
2017-04-03 15:11:09 +00:00
|
|
|
Builder(VertexMode mode, int vertexCount, int indexCount, uint32_t flags);
|
|
|
|
|
2017-03-16 18:38:48 +00:00
|
|
|
bool isValid() const { return fVertices != nullptr; }
|
2017-03-14 16:04:16 +00:00
|
|
|
|
2017-03-16 18:38:48 +00:00
|
|
|
// if the builder is invalid, these will return 0
|
|
|
|
int vertexCount() const;
|
|
|
|
int indexCount() const;
|
|
|
|
SkPoint* positions();
|
|
|
|
SkPoint* texCoords(); // returns null if there are no texCoords
|
|
|
|
SkColor* colors(); // returns null if there are no colors
|
|
|
|
uint16_t* indices(); // returns null if there are no indices
|
2017-03-14 16:04:16 +00:00
|
|
|
|
2017-03-16 18:38:48 +00:00
|
|
|
// Detach the built vertices object. After the first call, this will always return null.
|
2017-03-14 16:04:16 +00:00
|
|
|
sk_sp<SkVertices> detach();
|
|
|
|
|
|
|
|
private:
|
2017-04-03 15:11:09 +00:00
|
|
|
Builder(VertexMode mode, int vertexCount, int indexCount, const Sizes&);
|
2017-03-16 18:38:48 +00:00
|
|
|
|
2017-04-03 15:11:09 +00:00
|
|
|
void init(VertexMode mode, int vertexCount, int indexCount, const Sizes&);
|
2017-03-16 18:38:48 +00:00
|
|
|
|
|
|
|
// holds a partially complete object. only completed in detach()
|
|
|
|
sk_sp<SkVertices> fVertices;
|
2018-04-28 20:13:08 +00:00
|
|
|
// Extra storage for intermediate vertices in the case where the client specifies indexed
|
|
|
|
// triangle fans. These get converted to indexed triangles when the Builder is finalized.
|
|
|
|
std::unique_ptr<uint8_t[]> fIntermediateFanIndices;
|
2017-03-16 18:38:48 +00:00
|
|
|
|
|
|
|
friend class SkVertices;
|
2017-03-14 16:04:16 +00:00
|
|
|
};
|
2017-02-06 14:41:10 +00:00
|
|
|
|
2017-03-16 18:38:48 +00:00
|
|
|
uint32_t uniqueID() const { return fUniqueID; }
|
2017-04-03 15:11:09 +00:00
|
|
|
VertexMode mode() const { return fMode; }
|
2017-03-16 18:38:48 +00:00
|
|
|
const SkRect& bounds() const { return fBounds; }
|
|
|
|
|
|
|
|
bool hasColors() const { return SkToBool(this->colors()); }
|
|
|
|
bool hasTexCoords() const { return SkToBool(this->texCoords()); }
|
|
|
|
bool hasIndices() const { return SkToBool(this->indices()); }
|
2017-02-06 14:41:10 +00:00
|
|
|
|
|
|
|
int vertexCount() const { return fVertexCnt; }
|
2017-03-14 16:04:16 +00:00
|
|
|
const SkPoint* positions() const { return fPositions; }
|
|
|
|
const SkPoint* texCoords() const { return fTexs; }
|
|
|
|
const SkColor* colors() const { return fColors; }
|
2017-02-06 14:41:10 +00:00
|
|
|
|
|
|
|
int indexCount() const { return fIndexCnt; }
|
2017-03-14 16:04:16 +00:00
|
|
|
const uint16_t* indices() const { return fIndices; }
|
2017-02-06 14:41:10 +00:00
|
|
|
|
2017-03-16 18:38:48 +00:00
|
|
|
// returns approximate byte size of the vertices object
|
|
|
|
size_t approximateSize() const;
|
2017-02-06 14:41:10 +00:00
|
|
|
|
2017-03-16 18:38:48 +00:00
|
|
|
/**
|
|
|
|
* Recreate a vertices from a buffer previously created by calling encode().
|
|
|
|
* Returns null if the data is corrupt or the length is incorrect for the contents.
|
|
|
|
*/
|
|
|
|
static sk_sp<SkVertices> Decode(const void* buffer, size_t length);
|
2017-02-06 14:41:10 +00:00
|
|
|
|
2017-03-16 18:38:48 +00:00
|
|
|
/**
|
|
|
|
* Pack the vertices object into a byte buffer. This can be used to recreate the vertices
|
|
|
|
* by calling Decode() with the buffer.
|
|
|
|
*/
|
2017-03-14 18:10:54 +00:00
|
|
|
sk_sp<SkData> encode() const;
|
|
|
|
|
2017-02-06 14:41:10 +00:00
|
|
|
private:
|
2017-03-14 16:04:16 +00:00
|
|
|
SkVertices() {}
|
2017-02-06 14:41:10 +00:00
|
|
|
|
2017-03-16 18:38:48 +00:00
|
|
|
// these are needed since we've manually sized our allocation (see Builder::init)
|
|
|
|
friend class SkNVRefCnt<SkVertices>;
|
2018-04-20 12:44:43 +00:00
|
|
|
void operator delete(void* p);
|
2017-03-16 18:38:48 +00:00
|
|
|
|
|
|
|
static sk_sp<SkVertices> Alloc(int vCount, int iCount, uint32_t builderFlags,
|
|
|
|
size_t* arraySize);
|
|
|
|
|
|
|
|
// we store this first, to pair with the refcnt in our base-class, so we don't have an
|
|
|
|
// unnecessary pad between it and the (possibly 8-byte aligned) ptrs.
|
2017-03-15 01:05:17 +00:00
|
|
|
uint32_t fUniqueID;
|
2017-03-16 18:38:48 +00:00
|
|
|
|
|
|
|
// these point inside our allocation, so none of these can be "freed"
|
|
|
|
SkPoint* fPositions;
|
|
|
|
SkPoint* fTexs;
|
|
|
|
SkColor* fColors;
|
|
|
|
uint16_t* fIndices;
|
|
|
|
|
|
|
|
SkRect fBounds; // computed to be the union of the fPositions[]
|
|
|
|
int fVertexCnt;
|
|
|
|
int fIndexCnt;
|
|
|
|
|
2017-04-03 15:11:09 +00:00
|
|
|
VertexMode fMode;
|
2017-03-16 18:38:48 +00:00
|
|
|
// below here is where the actual array data is stored.
|
2017-02-06 14:41:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|