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:
|
2018-06-29 18:32:21 +00:00
|
|
|
// BoneIndices indicates which (of a maximum of 4 bones) a given vertex will interpolate
|
|
|
|
// between. To indicate that a slot is not used, the convention is to assign the bone index
|
|
|
|
// to 0.
|
|
|
|
struct BoneIndices {
|
|
|
|
uint32_t indices[4];
|
|
|
|
};
|
|
|
|
|
|
|
|
// BoneWeights stores the interpolation weight for each of the (maximum of 4) bones a given
|
|
|
|
// vertex interpolates between. To indicate that a slot is not used, the weight for that
|
|
|
|
// slot should be 0.
|
|
|
|
struct BoneWeights {
|
|
|
|
float weights[4];
|
|
|
|
};
|
|
|
|
|
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
|
|
|
/**
|
2018-06-29 18:32:21 +00:00
|
|
|
* Create a vertices by copying the specified arrays. texs, colors, boneIndices, and
|
|
|
|
* boneWeights may be nullptr, and indices is ignored if indexCount == 0.
|
|
|
|
*
|
|
|
|
* boneIndices and boneWeights must either both be nullptr or both point to valid data.
|
|
|
|
* If specified, they must both contain 'vertexCount' entries.
|
2017-03-14 16:04:16 +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[],
|
2018-06-29 18:32:21 +00:00
|
|
|
const BoneIndices boneIndices[],
|
|
|
|
const BoneWeights boneWeights[],
|
2017-03-14 16:04:16 +00:00
|
|
|
int indexCount,
|
2018-07-03 21:18:07 +00:00
|
|
|
const uint16_t indices[]);
|
2017-02-06 14:41:10 +00:00
|
|
|
|
2018-06-29 18:32:21 +00:00
|
|
|
static sk_sp<SkVertices> MakeCopy(VertexMode mode, int vertexCount,
|
|
|
|
const SkPoint positions[],
|
|
|
|
const SkPoint texs[],
|
|
|
|
const SkColor colors[],
|
|
|
|
const BoneIndices boneIndices[],
|
2018-07-03 21:18:07 +00:00
|
|
|
const BoneWeights boneWeights[]) {
|
2018-06-29 18:32:21 +00:00
|
|
|
return MakeCopy(mode,
|
|
|
|
vertexCount,
|
|
|
|
positions,
|
|
|
|
texs,
|
|
|
|
colors,
|
|
|
|
boneIndices,
|
|
|
|
boneWeights,
|
|
|
|
0,
|
2018-07-03 21:18:07 +00:00
|
|
|
nullptr);
|
2018-06-29 18:32:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static sk_sp<SkVertices> MakeCopy(VertexMode mode, int vertexCount,
|
|
|
|
const SkPoint positions[],
|
|
|
|
const SkPoint texs[],
|
|
|
|
const SkColor colors[],
|
|
|
|
int indexCount,
|
2018-07-03 21:18:07 +00:00
|
|
|
const uint16_t indices[]) {
|
2018-06-29 18:32:21 +00:00
|
|
|
return MakeCopy(mode,
|
|
|
|
vertexCount,
|
|
|
|
positions,
|
|
|
|
texs,
|
|
|
|
colors,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
indexCount,
|
2018-07-03 21:18:07 +00:00
|
|
|
indices);
|
2018-06-29 18:32:21 +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[],
|
2018-07-03 21:18:07 +00:00
|
|
|
const SkColor colors[]) {
|
|
|
|
return MakeCopy(mode, vertexCount, positions, texs, colors, nullptr, 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,
|
2018-06-29 18:32:21 +00:00
|
|
|
kHasBones_BuilderFlag = 1 << 2,
|
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();
|
2018-06-29 18:32:21 +00:00
|
|
|
SkPoint* texCoords(); // returns null if there are no texCoords
|
|
|
|
SkColor* colors(); // returns null if there are no colors
|
|
|
|
BoneIndices* boneIndices(); // returns null if there are no bone indices
|
|
|
|
BoneWeights* boneWeights(); // returns null if there are no bone weights
|
|
|
|
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:
|
2018-07-03 21:18:07 +00:00
|
|
|
Builder(VertexMode mode, int vertexCount, int indexCount, const Sizes&);
|
2017-03-16 18:38:48 +00:00
|
|
|
|
2018-07-03 21:18:07 +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()); }
|
2018-06-29 18:32:21 +00:00
|
|
|
bool hasBones() const { return SkToBool(this->boneIndices()); }
|
2017-03-16 18:38:48 +00:00
|
|
|
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
|
|
|
|
2018-06-29 18:32:21 +00:00
|
|
|
const BoneIndices* boneIndices() const { return fBoneIndices; }
|
|
|
|
const BoneWeights* boneWeights() const { return fBoneWeights; }
|
|
|
|
|
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"
|
2018-06-29 18:32:21 +00:00
|
|
|
SkPoint* fPositions;
|
|
|
|
SkPoint* fTexs;
|
|
|
|
SkColor* fColors;
|
|
|
|
BoneIndices* fBoneIndices;
|
|
|
|
BoneWeights* fBoneWeights;
|
|
|
|
uint16_t* fIndices;
|
2017-03-16 18:38:48 +00:00
|
|
|
|
|
|
|
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
|