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 "SkCanvas.h"
|
|
|
|
#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 16:44:25 +00:00
|
|
|
* An immutable set of vertex data that can be used with SkCanvas::drawVertices. Clients are
|
|
|
|
* encouraged to provide a bounds on the vertex positions if they can compute one more cheaply than
|
|
|
|
* looping over the positions.
|
2017-02-06 14:41:10 +00:00
|
|
|
*/
|
|
|
|
class SkVertices : public SkNVRefCnt<SkVertices> {
|
|
|
|
public:
|
2017-03-16 16:44:25 +00:00
|
|
|
~SkVertices() { sk_free((void*)fPositions); }
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
static sk_sp<SkVertices> MakeCopy(SkCanvas::VertexMode mode, int vertexCount,
|
|
|
|
const SkPoint positions[],
|
|
|
|
const SkPoint texs[],
|
|
|
|
const SkColor colors[],
|
|
|
|
int indexCount,
|
|
|
|
const uint16_t indices[]);
|
2017-02-06 14:41:10 +00:00
|
|
|
|
2017-03-14 16:04:16 +00:00
|
|
|
static sk_sp<SkVertices> MakeCopy(SkCanvas::VertexMode mode, int vertexCount,
|
|
|
|
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 16:44:25 +00:00
|
|
|
enum Flags {
|
|
|
|
kHasTexs_Flag = 1 << 0,
|
|
|
|
kHasColors_Flag = 1 << 1,
|
2017-03-14 16:04:16 +00:00
|
|
|
};
|
|
|
|
class Builder {
|
|
|
|
public:
|
|
|
|
Builder(SkCanvas::VertexMode mode, int vertexCount, int indexCount, uint32_t flags);
|
2017-03-16 16:44:25 +00:00
|
|
|
~Builder();
|
2017-03-14 16:04:16 +00:00
|
|
|
|
2017-03-16 16:44:25 +00:00
|
|
|
bool isValid() const { return fPositions != nullptr; }
|
2017-03-14 16:04:16 +00:00
|
|
|
|
2017-03-16 16:44:25 +00:00
|
|
|
int vertexCount() const { return fVertexCnt; }
|
|
|
|
int indexCount() const { return fIndexCnt; }
|
|
|
|
SkPoint* positions() { return fPositions; }
|
|
|
|
SkPoint* texCoords() { return fTexs; }
|
|
|
|
SkColor* colors() { return fColors; }
|
|
|
|
uint16_t* indices() { return fIndices; }
|
2017-03-14 16:04:16 +00:00
|
|
|
|
|
|
|
sk_sp<SkVertices> detach();
|
|
|
|
|
|
|
|
private:
|
2017-03-16 16:44:25 +00:00
|
|
|
SkPoint* fPositions; // owner of storage, use sk_free
|
|
|
|
SkPoint* fTexs;
|
|
|
|
SkColor* fColors;
|
|
|
|
uint16_t* fIndices;
|
|
|
|
int fVertexCnt;
|
|
|
|
int fIndexCnt;
|
|
|
|
SkCanvas::VertexMode fMode;
|
2017-03-14 16:04:16 +00:00
|
|
|
};
|
2017-02-06 14:41:10 +00:00
|
|
|
|
|
|
|
SkCanvas::VertexMode mode() const { return fMode; }
|
|
|
|
|
2017-03-16 16:44:25 +00:00
|
|
|
uint32_t uniqueID() const { return fUniqueID; }
|
2017-02-06 14:41:10 +00:00
|
|
|
int vertexCount() const { return fVertexCnt; }
|
2017-03-16 16:44:25 +00:00
|
|
|
bool hasColors() const { return SkToBool(fColors); }
|
|
|
|
bool hasTexCoords() const { return SkToBool(fTexs); }
|
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
|
|
|
|
2017-03-16 16:44:25 +00:00
|
|
|
bool isIndexed() const { return SkToBool(fIndexCnt); }
|
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 16:44:25 +00:00
|
|
|
size_t size() const {
|
|
|
|
return fVertexCnt * (sizeof(SkPoint) * (this->hasTexCoords() ? 2 : 1) + sizeof(SkColor)) +
|
|
|
|
fIndexCnt * sizeof(uint16_t);
|
|
|
|
}
|
2017-02-06 14:41:10 +00:00
|
|
|
|
2017-03-16 16:44:25 +00:00
|
|
|
const SkRect& bounds() const { return fBounds; }
|
2017-02-06 14:41:10 +00:00
|
|
|
|
2017-03-16 16:44:25 +00:00
|
|
|
static sk_sp<SkVertices> Decode(const void*, size_t);
|
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 16:44:25 +00:00
|
|
|
const SkPoint* fPositions; // owner of storage, use sk_free
|
|
|
|
const SkPoint* fTexs;
|
|
|
|
const SkColor* fColors;
|
|
|
|
const uint16_t* fIndices;
|
|
|
|
SkRect fBounds;
|
2017-03-15 01:05:17 +00:00
|
|
|
uint32_t fUniqueID;
|
2017-03-16 16:44:25 +00:00
|
|
|
int fVertexCnt;
|
|
|
|
int fIndexCnt;
|
2017-03-14 16:04:16 +00:00
|
|
|
SkCanvas::VertexMode fMode;
|
2017-02-06 14:41:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|