/* * Copyright 2010 Google Inc. * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef GrVertexWriter_DEFINED #define GrVertexWriter_DEFINED #include "include/private/SkTemplates.h" #include "src/gpu/GrColor.h" #include "src/gpu/geometry/GrQuad.h" #include /** * Helper for writing vertex data to a buffer. Usage: * GrVertexWriter vertices{target->makeVertexSpace(...)}; * vertices.write(A0, B0, C0, ...); * vertices.write(A1, B1, C1, ...); * * Supports any number of arguments. Each argument must be POD (plain old data), or an array * thereof. */ struct GrVertexWriter { void* fPtr; GrVertexWriter() = default; GrVertexWriter(void* ptr) : fPtr(ptr) {} GrVertexWriter(const GrVertexWriter&) = delete; GrVertexWriter(GrVertexWriter&& that) { *this = std::move(that); } GrVertexWriter& operator=(const GrVertexWriter&) = delete; GrVertexWriter& operator=(GrVertexWriter&& that) { fPtr = that.fPtr; that.fPtr = nullptr; return *this; } bool operator==(const GrVertexWriter& that) const { return fPtr == that.fPtr; } operator bool() const { return fPtr != nullptr; } GrVertexWriter makeOffset(size_t offsetInBytes) const { return {SkTAddOffset(fPtr, offsetInBytes)}; } template class Conditional { public: explicit Conditional(bool condition, const T& value) : fCondition(condition), fValue(value) {} private: friend struct GrVertexWriter; bool fCondition; T fValue; }; template static Conditional If(bool condition, const T& value) { return Conditional(condition, value); } template struct Skip {}; template void write(const T& val, const Args&... remainder) { static_assert(std::is_pod::value, ""); // This assert is barely related to what we're trying to check - that our vertex data // matches our attribute layouts, where each attribute is aligned to four bytes. If this // becomes a problem, just remove it. static_assert(alignof(T) <= 4, ""); memcpy(fPtr, &val, sizeof(T)); fPtr = SkTAddOffset(fPtr, sizeof(T)); this->write(remainder...); } template void write(const T(&val)[N], const Args&... remainder) { static_assert(std::is_pod::value, ""); static_assert(alignof(T) <= 4, ""); memcpy(fPtr, val, N * sizeof(T)); fPtr = SkTAddOffset(fPtr, N * sizeof(T)); this->write(remainder...); } template void write(const GrVertexColor& color, const Args&... remainder) { this->write(color.fColor[0]); if (color.fWideColor) { this->write(color.fColor[1]); this->write(color.fColor[2]); this->write(color.fColor[3]); } this->write(remainder...); } template void write(const Conditional& val, const Args&... remainder) { if (val.fCondition) { this->write(val.fValue); } this->write(remainder...); } template void write(const Skip& val, const Args&... remainder) { fPtr = SkTAddOffset(fPtr, sizeof(T)); this->write(remainder...); } template void write(const Sk4f& vector, const Args&... remainder) { float buffer[4]; vector.store(buffer); this->write(buffer); this->write(remainder...); } template void writeArray(const T* array, int count) { static_assert(std::is_pod::value, ""); static_assert(alignof(T) <= 4, ""); memcpy(fPtr, array, count * sizeof(T)); fPtr = SkTAddOffset(fPtr, count * sizeof(T)); } template void fill(const T& val, int repeatCount) { for (int i = 0; i < repeatCount; ++i) { this->write(val); } } void writeRaw(const void* data, size_t size) { memcpy(fPtr, data, size); fPtr = SkTAddOffset(fPtr, size); } void write() {} /** * Specialized utility for writing a four-vertices, with some data being replicated at each * vertex, and other data being the appropriate 2-components from an SkRect to construct a * triangle strip. * * writeQuad(A, B, C, ...) is similar to write(A, B, C, ...), except that: * * - Four sets of data will be written * - For any arguments of type TriStrip, a unique SkPoint will be written at each vertex, * in this order: left-top, left-bottom, right-top, right-bottom. */ template struct TriStrip { T l, t, r, b; }; static TriStrip TriStripFromRect(const SkRect& r) { return { r.fLeft, r.fTop, r.fRight, r.fBottom }; } static TriStrip TriStripFromUVs(const std::array& rect) { return { rect[0], rect[1], rect[2], rect[3] }; } template struct TriFan { T l, t, r, b; }; static TriFan TriFanFromRect(const SkRect& r) { return { r.fLeft, r.fTop, r.fRight, r.fBottom }; } template void writeQuad(const Args&... remainder) { this->writeQuadVert<0>(remainder...); this->writeQuadVert<1>(remainder...); this->writeQuadVert<2>(remainder...); this->writeQuadVert<3>(remainder...); } private: template void writeQuadVert(const T& val, const Args&... remainder) { this->writeQuadValue(val); this->writeQuadVert(remainder...); } template void writeQuadVert() {} template void writeQuadValue(const T& val) { this->write(val); } template void writeQuadValue(const TriStrip& r) { switch (corner) { case 0: this->write(r.l, r.t); break; case 1: this->write(r.l, r.b); break; case 2: this->write(r.r, r.t); break; case 3: this->write(r.r, r.b); break; } } template void writeQuadValue(const TriFan& r) { switch (corner) { case 0: this->write(r.l, r.t); break; case 1: this->write(r.l, r.b); break; case 2: this->write(r.r, r.b); break; case 3: this->write(r.r, r.t); break; } } template void writeQuadValue(const GrQuad& q) { this->write(q.point(corner)); } }; #endif