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
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkData.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
2017-02-06 14:41:10 +00:00
|
|
|
|
|
|
|
/**
|
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
|
|
|
*/
|
2018-10-30 15:23:00 +00:00
|
|
|
class SK_API SkVertices : public SkNVRefCnt<SkVertices> {
|
2020-03-04 14:48:08 +00:00
|
|
|
struct Desc;
|
|
|
|
struct Sizes;
|
2017-02-06 14:41:10 +00:00
|
|
|
public:
|
2020-03-02 19:57:09 +00:00
|
|
|
// DEPRECATED -- remove when we've updated canvas virtuals to not mention bones
|
|
|
|
struct Bone { float values[6]; };
|
2018-06-29 18:32:21 +00:00
|
|
|
|
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
|
|
|
/**
|
2020-03-02 19:57:09 +00:00
|
|
|
* Create a vertices by copying the specified arrays. texs, colors may be nullptr,
|
|
|
|
* and indices is ignored if indexCount == 0.
|
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[],
|
|
|
|
int indexCount,
|
2018-07-09 18:16:56 +00:00
|
|
|
const uint16_t indices[],
|
|
|
|
bool isVolatile = true);
|
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[],
|
2018-07-09 18:16:56 +00:00
|
|
|
bool isVolatile = true) {
|
2018-06-29 18:32:21 +00:00
|
|
|
return MakeCopy(mode,
|
|
|
|
vertexCount,
|
|
|
|
positions,
|
|
|
|
texs,
|
|
|
|
colors,
|
|
|
|
0,
|
2018-07-09 18:16:56 +00:00
|
|
|
nullptr,
|
|
|
|
isVolatile);
|
2018-06-29 18:32:21 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 18:38:48 +00:00
|
|
|
enum BuilderFlags {
|
|
|
|
kHasTexCoords_BuilderFlag = 1 << 0,
|
|
|
|
kHasColors_BuilderFlag = 1 << 1,
|
2020-03-02 19:57:09 +00:00
|
|
|
kIsNonVolatile_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);
|
2020-03-05 18:01:00 +00:00
|
|
|
Builder(VertexMode mode, int vertexCount, int indexCount, int perVertexDataCount,
|
|
|
|
bool isVolatile);
|
2017-04-03 15:11:09 +00:00
|
|
|
|
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;
|
2020-03-05 18:01:00 +00:00
|
|
|
int perVertexDataCount() const;
|
2018-07-09 18:16:56 +00:00
|
|
|
bool isVolatile() const;
|
2017-03-16 18:38:48 +00:00
|
|
|
SkPoint* positions();
|
2020-03-05 18:01:00 +00:00
|
|
|
uint16_t* indices(); // returns null if there are no indices
|
|
|
|
|
|
|
|
// if we have texCoords or colors, this will always be null
|
|
|
|
float* perVertexData(); // return null if there is no perVertexData
|
|
|
|
|
|
|
|
// If we have per-vertex-data, these will always be null
|
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
|
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:
|
2020-03-04 14:48:08 +00:00
|
|
|
Builder(const Desc&);
|
2017-03-16 18:38:48 +00:00
|
|
|
|
2020-03-04 14:48:08 +00:00
|
|
|
void init(const Desc&);
|
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; }
|
|
|
|
|
2020-03-05 18:01:00 +00:00
|
|
|
bool hasPerVertexData() const { return SkToBool(this->perVertexData()); }
|
2017-03-16 18:38:48 +00:00
|
|
|
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
|
|
|
|
2020-03-04 14:48:08 +00:00
|
|
|
int vertexCount() const { return fVertexCount; }
|
2020-03-05 18:01:00 +00:00
|
|
|
int indexCount() const { return fIndexCount; }
|
|
|
|
int perVertexDataCount() const { return fPerVertexDataCount; }
|
|
|
|
|
2017-03-14 16:04:16 +00:00
|
|
|
const SkPoint* positions() const { return fPositions; }
|
2020-03-05 18:01:00 +00:00
|
|
|
const float* perVertexData() const { return fPerVertexData; }
|
2017-03-14 16:04:16 +00:00
|
|
|
const SkPoint* texCoords() const { return fTexs; }
|
|
|
|
const SkColor* colors() const { return fColors; }
|
|
|
|
const uint16_t* indices() const { return fIndices; }
|
2017-02-06 14:41:10 +00:00
|
|
|
|
2018-07-09 18:16:56 +00:00
|
|
|
bool isVolatile() const { return fIsVolatile; }
|
|
|
|
|
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)
|
2018-10-30 15:23:00 +00:00
|
|
|
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);
|
|
|
|
|
2020-03-04 14:48:08 +00:00
|
|
|
Sizes getSizes() const;
|
|
|
|
|
2017-03-16 18:38:48 +00:00
|
|
|
// 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"
|
2020-03-05 18:01:00 +00:00
|
|
|
SkPoint* fPositions; // [vertexCount]
|
|
|
|
uint16_t* fIndices; // [indexCount] or null
|
|
|
|
float* fPerVertexData; // [perVertexDataCount * vertexCount] or null
|
|
|
|
SkPoint* fTexs; // [vertexCount] or null
|
|
|
|
SkColor* fColors; // [vertexCount] or null
|
2017-03-16 18:38:48 +00:00
|
|
|
|
|
|
|
SkRect fBounds; // computed to be the union of the fPositions[]
|
2020-03-04 14:48:08 +00:00
|
|
|
int fVertexCount;
|
|
|
|
int fIndexCount;
|
2020-03-05 18:01:00 +00:00
|
|
|
int fPerVertexDataCount;
|
2017-03-16 18:38:48 +00:00
|
|
|
|
2018-07-09 18:16:56 +00:00
|
|
|
bool fIsVolatile;
|
|
|
|
|
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
|