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/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
2017-02-06 14:41:10 +00:00
|
|
|
|
2020-03-31 14:38:31 +00:00
|
|
|
class SkData;
|
|
|
|
struct SkPoint;
|
|
|
|
class SkVerticesPriv;
|
|
|
|
|
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:
|
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,
|
2020-03-11 16:26:56 +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[],
|
2020-03-11 16:26:56 +00:00
|
|
|
const SkColor colors[]) {
|
2018-06-29 18:32:21 +00:00
|
|
|
return MakeCopy(mode,
|
|
|
|
vertexCount,
|
|
|
|
positions,
|
|
|
|
texs,
|
|
|
|
colors,
|
|
|
|
0,
|
2020-03-11 16:26:56 +00:00
|
|
|
nullptr);
|
2018-06-29 18:32:21 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 13:57:53 +00:00
|
|
|
static constexpr int kMaxCustomAttributes = 8;
|
|
|
|
|
2020-04-24 16:02:25 +00:00
|
|
|
/**
|
|
|
|
* EXPERIMENTAL - An SkVertices object can be constructed with a custom collection of vertex
|
|
|
|
* attributes. Each attribute is described by a single Attribute struct. Type defines the CPU
|
|
|
|
* type of the data. Usage determines what transformation (if any) is applied to that data in
|
|
|
|
* the vertex shader. For positions or vectors, markerName identifies what matrix is used in
|
|
|
|
* the vertex shader to transform the data. Those names should match a named transform on the
|
|
|
|
* CTM stack, created by calling SkCanvas::markCTM().
|
2020-05-15 19:47:25 +00:00
|
|
|
*
|
|
|
|
* For attributes with a usage of kVector, kNormalVector, or kPosition, a null markerName
|
|
|
|
* will transform the attribute by the canvas CTM matrix.
|
2020-04-24 16:02:25 +00:00
|
|
|
*/
|
2020-03-30 13:57:53 +00:00
|
|
|
struct Attribute {
|
|
|
|
enum class Type : uint8_t {
|
|
|
|
kFloat,
|
|
|
|
kFloat2,
|
|
|
|
kFloat3,
|
|
|
|
kFloat4,
|
|
|
|
kByte4_unorm,
|
|
|
|
};
|
|
|
|
|
2020-04-02 20:49:59 +00:00
|
|
|
enum class Usage : uint8_t {
|
|
|
|
// Raw values passed directly to effect
|
|
|
|
kRaw,
|
2020-03-30 13:57:53 +00:00
|
|
|
|
2020-04-07 20:04:24 +00:00
|
|
|
// sRGB unpremul colors, transformed to destination color space (3 or 4 channels)
|
|
|
|
// Colors are always assumed to be in RGBA order, and are automatically premultiplied.
|
|
|
|
kColor,
|
2020-04-02 20:49:59 +00:00
|
|
|
|
2020-04-09 20:24:23 +00:00
|
|
|
// Local vector, transformed via marker (2 or 3 channels)
|
2020-04-02 20:49:59 +00:00
|
|
|
kVector,
|
|
|
|
|
2020-04-09 20:24:23 +00:00
|
|
|
// Normal vector (or any other bivector), transformed via marker (2 or 3 channels)
|
2020-04-02 20:49:59 +00:00
|
|
|
kNormalVector,
|
|
|
|
|
2020-04-09 20:24:23 +00:00
|
|
|
// Local position, transformed via marker (2 or 3 channels)
|
2020-04-02 20:49:59 +00:00
|
|
|
kPosition,
|
|
|
|
};
|
|
|
|
|
2020-04-24 16:02:25 +00:00
|
|
|
/**
|
|
|
|
* markerName is not copied by the Attribute, so it must outlive this struct.
|
|
|
|
* It is copied when this Attribute is passed to the Builder constructor.
|
|
|
|
*/
|
|
|
|
Attribute(Type t = Type::kFloat, Usage u = Usage::kRaw, const char* markerName = nullptr);
|
2020-04-02 20:49:59 +00:00
|
|
|
|
|
|
|
bool operator==(const Attribute& that) const {
|
2020-04-09 20:24:23 +00:00
|
|
|
return fType == that.fType && fUsage == that.fUsage && fMarkerID == that.fMarkerID;
|
2020-04-02 20:49:59 +00:00
|
|
|
}
|
2020-03-30 13:57:53 +00:00
|
|
|
bool operator!=(const Attribute& that) const { return !(*this == that); }
|
|
|
|
|
2020-04-02 20:49:59 +00:00
|
|
|
// Number of channels that will be produced for the SkRuntimeEffect to consume.
|
|
|
|
// May not match the number of channels in fType. For example, kVector Attributes always
|
|
|
|
// produce three channels, even if the input is kFloat2.
|
2020-03-30 13:57:53 +00:00
|
|
|
int channelCount() const;
|
|
|
|
size_t bytesPerVertex() const;
|
2020-04-02 20:49:59 +00:00
|
|
|
bool isValid() const;
|
2020-03-30 13:57:53 +00:00
|
|
|
|
2020-04-24 16:02:25 +00:00
|
|
|
Type fType;
|
|
|
|
Usage fUsage;
|
|
|
|
uint32_t fMarkerID;
|
|
|
|
const char* fMarkerName; // Preserved for serialization and debugging
|
2020-03-30 13:57:53 +00:00
|
|
|
};
|
2020-03-11 16:26:56 +00:00
|
|
|
|
2017-03-16 18:38:48 +00:00
|
|
|
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);
|
2020-03-06 17:52:52 +00:00
|
|
|
|
|
|
|
// EXPERIMENTAL -- do not call if you care what happens
|
2020-03-30 13:57:53 +00:00
|
|
|
Builder(VertexMode mode,
|
|
|
|
int vertexCount,
|
|
|
|
int indexCount,
|
|
|
|
const Attribute* attrs,
|
|
|
|
int attrCount);
|
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
|
|
|
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
|
2020-03-30 13:57:53 +00:00
|
|
|
void* customData(); // returns null if there are no custom attributes
|
2020-03-05 18:01:00 +00:00
|
|
|
|
2020-03-30 13:57:53 +00:00
|
|
|
// If we have custom attributes, 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;
|
2020-06-01 20:33:28 +00:00
|
|
|
friend class SkVerticesPriv;
|
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; }
|
|
|
|
const SkRect& bounds() const { return fBounds; }
|
|
|
|
|
|
|
|
// returns approximate byte size of the vertices object
|
|
|
|
size_t approximateSize() const;
|
2017-02-06 14:41:10 +00:00
|
|
|
|
2020-03-31 14:38:31 +00:00
|
|
|
// Provides access to functions that aren't part of the public API.
|
|
|
|
SkVerticesPriv priv();
|
2020-08-07 17:05:14 +00:00
|
|
|
const SkVerticesPriv priv() const; // NOLINT(readability-const-return-type)
|
2020-03-13 00:33:21 +00:00
|
|
|
|
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
|
|
|
|
2020-03-13 00:33:21 +00:00
|
|
|
friend class SkVerticesPriv;
|
|
|
|
|
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
|
|
|
|
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-04-24 16:02:25 +00:00
|
|
|
Attribute* fAttributes; // [attributeCount] or null
|
2020-03-05 18:01:00 +00:00
|
|
|
SkPoint* fPositions; // [vertexCount]
|
|
|
|
uint16_t* fIndices; // [indexCount] or null
|
2020-03-30 13:57:53 +00:00
|
|
|
void* fCustomData; // [customDataSize * vertexCount] or null
|
2020-03-05 18:01:00 +00:00
|
|
|
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-04-24 16:02:25 +00:00
|
|
|
int fAttributeCount;
|
2017-03-16 18:38:48 +00:00
|
|
|
|
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
|