diff options
author | 2017-03-14 12:04:16 -0400 | |
---|---|---|
committer | 2017-03-14 16:57:49 +0000 | |
commit | 97eb4feb112967ba7fcb00d6995adda1002873c2 (patch) | |
tree | 4fb8569365aabaac2b2ef0d3cee88e4b9e7a4fdf /include/core/SkVertices.h | |
parent | 8f7d9b9784fc22b809ef1d2fa301b7b95efd2a90 (diff) |
add SkVertices::Builder
Possible next iterations:
- remove another allocation use the SkData trick to share
the object and its (trailing) data
- store a bit that tells use to free each pointer, allowing
the builder to "adopt" some allocations instead of copy.
Larger idea:
- merge with drawPoints to have a single object for both.
BUG=skia:6366
Change-Id: Iec33239aa2ad5d00b36469ca0b88934ddf6f22eb
Reviewed-on: https://skia-review.googlesource.com/9604
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Mike Reed <reed@google.com>
Diffstat (limited to 'include/core/SkVertices.h')
-rw-r--r-- | include/core/SkVertices.h | 146 |
1 files changed, 56 insertions, 90 deletions
diff --git a/include/core/SkVertices.h b/include/core/SkVertices.h index 9b94372d7c..2e4f09ff8c 100644 --- a/include/core/SkVertices.h +++ b/include/core/SkVertices.h @@ -21,74 +21,68 @@ */ class SkVertices : public SkNVRefCnt<SkVertices> { public: - static sk_sp<SkVertices> Make(SkCanvas::VertexMode mode, - std::unique_ptr<const SkPoint[]> positions, - std::unique_ptr<const SkColor[]> colors, /* optional */ - std::unique_ptr<const SkPoint[]> texs, /* optional */ - int vertexCnt) { - if (!positions || vertexCnt <= 0) { - return nullptr; - } - return sk_sp<SkVertices>(new SkVertices(mode, std::move(positions), std::move(colors), - std::move(texs), vertexCnt, nullptr, 0, nullptr)); - } + ~SkVertices() { sk_free((void*)fPositions); } - static sk_sp<SkVertices> Make(SkCanvas::VertexMode mode, - std::unique_ptr<const SkPoint[]> positions, - std::unique_ptr<const SkColor[]> colors, /* optional */ - std::unique_ptr<const SkPoint[]> texs, /* optional */ - int vertexCnt, - const SkRect& bounds) { - if (!positions || vertexCnt <= 0) { - return nullptr; - } - return sk_sp<SkVertices>(new SkVertices(mode, std::move(positions), std::move(colors), - std::move(texs), vertexCnt, nullptr, 0, &bounds)); - } + /** + * 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[]); - static sk_sp<SkVertices> MakeIndexed(SkCanvas::VertexMode mode, - std::unique_ptr<const SkPoint[]> positions, - std::unique_ptr<const SkColor[]> colors, /* optional */ - std::unique_ptr<const SkPoint[]> texs, /* optional */ - int vertexCnt, - std::unique_ptr<const uint16_t[]> indices, - int indexCnt) { - if (!positions || !indices || vertexCnt <= 0 || indexCnt <= 0) { - return nullptr; - } - return sk_sp<SkVertices>(new SkVertices(mode, std::move(positions), std::move(colors), - std::move(texs), vertexCnt, std::move(indices), - indexCnt, nullptr)); + 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); } - static sk_sp<SkVertices> MakeIndexed(SkCanvas::VertexMode mode, - std::unique_ptr<const SkPoint[]> positions, - std::unique_ptr<const SkColor[]> colors, /* optional */ - std::unique_ptr<const SkPoint[]> texs, /* optional */ - int vertexCnt, - std::unique_ptr<const uint16_t[]> indices, - int indexCnt, - const SkRect& bounds) { - if (!positions || !indices || vertexCnt <= 0 || indexCnt <= 0) { - return nullptr; - } - return sk_sp<SkVertices>(new SkVertices(mode, std::move(positions), std::move(colors), - std::move(texs), vertexCnt, std::move(indices), - indexCnt, &bounds)); - } + enum Flags { + kHasTexs_Flag = 1 << 0, + kHasColors_Flag = 1 << 1, + }; + class Builder { + public: + Builder(SkCanvas::VertexMode mode, int vertexCount, int indexCount, uint32_t flags); + ~Builder(); + + bool isValid() const { return fPositions != nullptr; } + + 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; } + + sk_sp<SkVertices> detach(); + + private: + SkPoint* fPositions; // owner of storage, use sk_free + SkPoint* fTexs; + SkColor* fColors; + uint16_t* fIndices; + int fVertexCnt; + int fIndexCnt; + SkCanvas::VertexMode fMode; + }; SkCanvas::VertexMode mode() const { return fMode; } int vertexCount() const { return fVertexCnt; } bool hasColors() const { return SkToBool(fColors); } bool hasTexCoords() const { return SkToBool(fTexs); } - const SkPoint* positions() const { return fPositions.get(); } - const SkPoint* texCoords() const { return fTexs.get(); } - const SkColor* colors() const { return fColors.get(); } + const SkPoint* positions() const { return fPositions; } + const SkPoint* texCoords() const { return fTexs; } + const SkColor* colors() const { return fColors; } bool isIndexed() const { return SkToBool(fIndexCnt); } int indexCount() const { return fIndexCnt; } - const uint16_t* indices() const { return fIndices.get(); } + const uint16_t* indices() const { return fIndices; } size_t size() const { return fVertexCnt * (sizeof(SkPoint) * (this->hasTexCoords() ? 2 : 1) + sizeof(SkColor)) + @@ -98,44 +92,16 @@ public: const SkRect& bounds() const { return fBounds; } private: - SkVertices(SkCanvas::VertexMode mode, std::unique_ptr<const SkPoint[]> positions, - std::unique_ptr<const SkColor[]> colors, std::unique_ptr<const SkPoint[]> texs, - int vertexCnt, std::unique_ptr<const uint16_t[]> indices, int indexCnt, - const SkRect* bounds) - : fMode(mode) - , fVertexCnt(vertexCnt) - , fIndexCnt(indexCnt) - , fPositions(std::move(positions)) - , fColors(std::move(colors)) - , fTexs(std::move(texs)) - , fIndices(std::move(indices)) { - SkASSERT(SkToBool(fPositions) && SkToBool(fVertexCnt)); - SkASSERT(SkToBool(fIndices) == SkToBool(fIndexCnt)); - if (bounds) { -#ifdef SK_DEBUG - fBounds.setBounds(fPositions.get(), fVertexCnt); - SkASSERT(bounds->fLeft <= fBounds.fLeft && bounds->fRight >= fBounds.fRight && - bounds->fTop <= fBounds.fTop && bounds->fBottom >= fBounds.fBottom); -#endif - fBounds = *bounds; - } else { - fBounds.setBounds(fPositions.get(), fVertexCnt); - } -#ifdef SK_DEBUG - for (int i = 0; i < fIndexCnt; ++i) { - SkASSERT(fIndices[i] < fVertexCnt); - } -#endif - } + SkVertices() {} - SkCanvas::VertexMode fMode; + const SkPoint* fPositions; // owner of storage, use sk_free + const SkPoint* fTexs; + const SkColor* fColors; + const uint16_t* fIndices; + SkRect fBounds; int fVertexCnt; int fIndexCnt; - std::unique_ptr<const SkPoint[]> fPositions; - std::unique_ptr<const SkColor[]> fColors; - std::unique_ptr<const SkPoint[]> fTexs; - std::unique_ptr<const uint16_t[]> fIndices; - SkRect fBounds; + SkCanvas::VertexMode fMode; }; #endif |