diff --git a/bench/TessellateBench.cpp b/bench/TessellateBench.cpp index f0dddd2b8a..8b23ecbfd7 100644 --- a/bench/TessellateBench.cpp +++ b/bench/TessellateBench.cpp @@ -233,8 +233,8 @@ DEF_PATH_TESS_BENCH(middle_out_triangulation, SkMatrix::I()) { sk_sp buffer; int baseVertex; - VertexWriter vertexWriter = static_cast(fTarget->makeVertexSpace( - sizeof(SkPoint), kNumCubicsInChalkboard, &buffer, &baseVertex)); + VertexWriter vertexWriter = fTarget->makeVertexWriter( + sizeof(SkPoint), kNumCubicsInChalkboard, &buffer, &baseVertex); AffineMatrix m(gAlmostIdentity); for (PathMiddleOutFanIter it(fPath); !it.done();) { for (auto [p0, p1, p2] : it.nextStack()) { diff --git a/src/gpu/BufferWriter.h b/src/gpu/BufferWriter.h index b43ca9e3fc..c46f28ece0 100644 --- a/src/gpu/BufferWriter.h +++ b/src/gpu/BufferWriter.h @@ -22,7 +22,7 @@ public: protected: BufferWriter() = default; - BufferWriter(void* ptr) : fPtr(ptr) {} + explicit BufferWriter(void* ptr) : fPtr(ptr) {} BufferWriter& operator=(const BufferWriter&) = delete; BufferWriter& operator=(BufferWriter&& that) { @@ -47,7 +47,7 @@ struct VertexWriter : public BufferWriter { inline constexpr static uint32_t kIEEE_32_infinity = 0x7f800000; VertexWriter() = default; - VertexWriter(void* ptr) : BufferWriter(ptr) {} + explicit VertexWriter(void* ptr) : BufferWriter(ptr) {} VertexWriter(const VertexWriter&) = delete; VertexWriter(VertexWriter&& that) { *this = std::move(that); } @@ -65,7 +65,7 @@ struct VertexWriter : public BufferWriter { void* ptr() const { return fPtr; } VertexWriter makeOffset(ptrdiff_t offsetInBytes) const { - return {SkTAddOffset(fPtr, offsetInBytes)}; + return VertexWriter{SkTAddOffset(fPtr, offsetInBytes)}; } template @@ -297,7 +297,8 @@ SK_MAYBE_UNUSED inline VertexWriter& operator<<(VertexWriter& w, const VertexCol struct IndexWriter : public BufferWriter { IndexWriter() = default; - IndexWriter(void* ptr) : BufferWriter(ptr) {} + explicit IndexWriter(void* ptr) : BufferWriter(ptr) {} + IndexWriter(const IndexWriter&) = delete; IndexWriter(IndexWriter&& that) { *this = std::move(that); } @@ -308,7 +309,7 @@ struct IndexWriter : public BufferWriter { } IndexWriter makeAdvance(int numIndices) const { - return {SkTAddOffset(fPtr, numIndices * sizeof(uint16_t))}; + return IndexWriter{SkTAddOffset(fPtr, numIndices * sizeof(uint16_t))}; } void writeArray(const uint16_t* array, int count) { @@ -331,7 +332,8 @@ inline IndexWriter& operator<<(IndexWriter& w, int val) { return (w << SkToresourceProvider()->contextUniqueID(); } + +template +static W make_writer(void* p, int count, size_t elementSize) { + // TODO: This will do more when writers track their ending mark based on count*elementSize + return W{p}; +} + +skgpu::VertexWriter GrMeshDrawTarget::makeVertexWriter( + size_t vertexSize, int vertexCount, sk_sp* buffer, int* startVertex) { + void* p = this->makeVertexSpace(vertexSize, vertexCount, buffer, startVertex); + return make_writer(p, vertexCount, vertexSize); +} + +skgpu::IndexWriter GrMeshDrawTarget::makeIndexWriter( + int indexCount, sk_sp* buffer, int* startIndex) { + void* p = this->makeIndexSpace(indexCount, buffer, startIndex); + return make_writer(p, indexCount, sizeof(uint16_t)); +} + +skgpu::VertexWriter GrMeshDrawTarget::makeVertexWriterAtLeast( + size_t vertexSize, int minVertexCount, int fallbackVertexCount, + sk_sp* buffer, int* startVertex, int* actualVertexCount) { + void* p = this->makeVertexSpaceAtLeast(vertexSize, minVertexCount, fallbackVertexCount, + buffer, startVertex, actualVertexCount); + return make_writer(p, *actualVertexCount, vertexSize); +} + +skgpu::IndexWriter GrMeshDrawTarget::makeIndexWriterAtLeast( + int minIndexCount, int fallbackIndexCount, sk_sp* buffer, + int* startIndex, int* actualIndexCount) { + void* p = this->makeIndexSpaceAtLeast(minIndexCount, fallbackIndexCount, buffer, + startIndex, actualIndexCount); + return make_writer(p, *actualIndexCount, sizeof(uint16_t)); +} diff --git a/src/gpu/GrMeshDrawTarget.h b/src/gpu/GrMeshDrawTarget.h index ff7c9df89c..9ba4389b0d 100644 --- a/src/gpu/GrMeshDrawTarget.h +++ b/src/gpu/GrMeshDrawTarget.h @@ -15,7 +15,12 @@ class GrAtlasManager; class GrStrikeCache; class GrThreadSafeCache; -namespace skgpu { namespace v1 { class SmallPathAtlasMgr; }} +namespace skgpu { + namespace v1 { class SmallPathAtlasMgr; } + + struct IndexWriter; + struct VertexWriter; +} // namespace skgpu /* * Abstract interface that supports creating vertices, indices, and meshes, as well as @@ -94,6 +99,17 @@ public: sk_sp*, size_t* offsetInBytes) = 0; + /** Helpers for ops that only need to use the VertexWriter to fill the data directly. */ + skgpu::VertexWriter makeVertexWriter(size_t vertexSize, int vertexCount, + sk_sp*, int* startVertex); + skgpu::IndexWriter makeIndexWriter(int indexCount, sk_sp*, int* startIndex); + skgpu::VertexWriter makeVertexWriterAtLeast(size_t vertexSize, int minVertexCount, + int fallbackVertexCount, sk_sp*, + int* startVertex, int* actualVertexCount); + skgpu::IndexWriter makeIndexWriterAtLeast(int minIndexCount, int fallbackIndexCount, + sk_sp*, int* startIndex, + int* actualIndexCount); + /** Helpers for ops which over-allocate and then return excess data to the pool. */ virtual void putBackIndices(int indices) = 0; virtual void putBackVertices(int vertices, size_t vertexStride) = 0; diff --git a/src/gpu/GrResourceProvider.cpp b/src/gpu/GrResourceProvider.cpp index 8e11a302e9..757eaca831 100644 --- a/src/gpu/GrResourceProvider.cpp +++ b/src/gpu/GrResourceProvider.cpp @@ -437,11 +437,11 @@ sk_sp GrResourceProvider::findOrMakeStaticBuffer( buffer->resourcePriv().setUniqueKey(uniqueKey); // Map the buffer. Use a staging buffer on the heap if mapping isn't supported. - skgpu::VertexWriter vertexWriter = buffer->map(); + skgpu::VertexWriter vertexWriter{buffer->map()}; SkAutoTMalloc stagingBuffer; if (!vertexWriter) { SkASSERT(!buffer->isMapped()); - vertexWriter = stagingBuffer.reset(size); + vertexWriter = skgpu::VertexWriter{stagingBuffer.reset(size)}; } initializeBufferFn(std::move(vertexWriter), size); diff --git a/src/gpu/GrVertexChunkArray.cpp b/src/gpu/GrVertexChunkArray.cpp index 27490f6469..5a43227eb2 100644 --- a/src/gpu/GrVertexChunkArray.cpp +++ b/src/gpu/GrVertexChunkArray.cpp @@ -24,10 +24,10 @@ bool GrVertexChunkBuilder::allocChunk(int minCount) { fCurrChunkVertexCount = 0; GrVertexChunk* chunk = &fChunks->push_back(); int minAllocCount = std::max(minCount, fMinVerticesPerChunk); - fCurrChunkVertexWriter = {fTarget->makeVertexSpaceAtLeast(fStride, minAllocCount, - minAllocCount, &chunk->fBuffer, - &chunk->fBase, - &fCurrChunkVertexCapacity)}; + fCurrChunkVertexWriter = fTarget->makeVertexWriterAtLeast(fStride, minAllocCount, + minAllocCount, &chunk->fBuffer, + &chunk->fBase, + &fCurrChunkVertexCapacity); if (!fCurrChunkVertexWriter || !chunk->fBuffer || fCurrChunkVertexCapacity < minCount) { SkDebugf("WARNING: Failed to allocate vertex buffer for GrVertexChunk.\n"); fChunks->pop_back(); diff --git a/src/gpu/GrVertexChunkArray.h b/src/gpu/GrVertexChunkArray.h index 6ff36a64af..5de51f3005 100644 --- a/src/gpu/GrVertexChunkArray.h +++ b/src/gpu/GrVertexChunkArray.h @@ -54,7 +54,7 @@ public: SkASSERT(count > 0); if (fCurrChunkVertexCount + count > fCurrChunkVertexCapacity && !this->allocChunk(count)) { SkDEBUGCODE(fLastAppendAmount = 0;) - return {nullptr}; + return {}; } SkASSERT(fCurrChunkVertexCount + count <= fCurrChunkVertexCapacity); fCurrChunkVertexCount += count; diff --git a/src/gpu/ops/AAConvexPathRenderer.cpp b/src/gpu/ops/AAConvexPathRenderer.cpp index 20142fc9a0..bdabb608ff 100644 --- a/src/gpu/ops/AAConvexPathRenderer.cpp +++ b/src/gpu/ops/AAConvexPathRenderer.cpp @@ -803,10 +803,10 @@ private: sk_sp vertexBuffer; int firstVertex; - VertexWriter verts{target->makeVertexSpace(kVertexStride, - vertexCount, - &vertexBuffer, - &firstVertex)}; + VertexWriter verts = target->makeVertexWriter(kVertexStride, + vertexCount, + &vertexBuffer, + &firstVertex); if (!verts) { SkDebugf("Could not allocate vertices\n"); diff --git a/src/gpu/ops/DrawAtlasPathOp.cpp b/src/gpu/ops/DrawAtlasPathOp.cpp index 5dcd55ecf6..112644b097 100644 --- a/src/gpu/ops/DrawAtlasPathOp.cpp +++ b/src/gpu/ops/DrawAtlasPathOp.cpp @@ -192,7 +192,7 @@ void DrawAtlasPathOp::onPrepare(GrOpFlushState* flushState) { SkASSERT(fProgram); } - if (VertexWriter instanceWriter = flushState->makeVertexSpace( + if (VertexWriter instanceWriter = flushState->makeVertexWriter( fProgram->geomProc().instanceStride(), fInstanceCount, &fInstanceBuffer, &fBaseInstance)) { for (const Instance* i = fHeadInstance; i; i = i->fNext) { diff --git a/src/gpu/ops/DrawCustomMeshOp.cpp b/src/gpu/ops/DrawCustomMeshOp.cpp index 124a23b7b7..0a356ad566 100644 --- a/src/gpu/ops/DrawCustomMeshOp.cpp +++ b/src/gpu/ops/DrawCustomMeshOp.cpp @@ -697,10 +697,10 @@ void CustomMeshOp::onPrepareDraws(GrMeshDrawTarget* target) { size_t vertexStride = fSpecification->stride(); sk_sp vertexBuffer; int firstVertex = 0; - skgpu::VertexWriter verts{target->makeVertexSpace(vertexStride, - fVertexCount, - &vertexBuffer, - &firstVertex)}; + skgpu::VertexWriter verts = target->makeVertexWriter(vertexStride, + fVertexCount, + &vertexBuffer, + &firstVertex); if (!verts) { SkDebugf("Could not allocate vertices.\n"); return; diff --git a/src/gpu/ops/FillRRectOp.cpp b/src/gpu/ops/FillRRectOp.cpp index c28ae3c81c..dfba0ae630 100644 --- a/src/gpu/ops/FillRRectOp.cpp +++ b/src/gpu/ops/FillRRectOp.cpp @@ -520,9 +520,9 @@ void FillRRectOpImpl::onPrepareDraws(GrMeshDrawTarget* target) { size_t instanceStride = fProgramInfo->geomProc().instanceStride(); - if (VertexWriter instanceWrter = target->makeVertexSpace(instanceStride, fInstanceCount, - &fInstanceBuffer, &fBaseInstance)) { - SkDEBUGCODE(auto end = instanceWrter.makeOffset(instanceStride * fInstanceCount)); + if (VertexWriter instanceWriter = target->makeVertexWriter(instanceStride, fInstanceCount, + &fInstanceBuffer, &fBaseInstance)) { + SkDEBUGCODE(auto end = instanceWriter.makeOffset(instanceStride * fInstanceCount)); for (Instance* i = fHeadInstance; i; i = i->fNext) { auto [l, t, r, b] = i->fRRect.rect(); @@ -539,14 +539,14 @@ void FillRRectOpImpl::onPrepareDraws(GrMeshDrawTarget* target) { radiiX *= 2 / (r - l); radiiY *= 2 / (b - t); - instanceWrter << m.getScaleX() << m.getSkewX() << m.getSkewY() << m.getScaleY() - << m.getTranslateX() << m.getTranslateY() - << radiiX << radiiY - << VertexColor(i->fColor, fProcessorFlags & ProcessorFlags::kWideColor) - << VertexWriter::If(fProcessorFlags & ProcessorFlags::kHasLocalCoords, - i->fLocalRect); + instanceWriter << m.getScaleX() << m.getSkewX() << m.getSkewY() << m.getScaleY() + << m.getTranslateX() << m.getTranslateY() + << radiiX << radiiY + << VertexColor(i->fColor, fProcessorFlags & ProcessorFlags::kWideColor) + << VertexWriter::If(fProcessorFlags & ProcessorFlags::kHasLocalCoords, + i->fLocalRect); } - SkASSERT(instanceWrter == end); + SkASSERT(instanceWriter == end); } SKGPU_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey); diff --git a/src/gpu/ops/GrOvalOpFactory.cpp b/src/gpu/ops/GrOvalOpFactory.cpp index 461f595ad1..8c4f6c6d69 100644 --- a/src/gpu/ops/GrOvalOpFactory.cpp +++ b/src/gpu/ops/GrOvalOpFactory.cpp @@ -1281,8 +1281,8 @@ private: sk_sp vertexBuffer; int firstVertex; - VertexWriter vertices{target->makeVertexSpace(fProgramInfo->geomProc().vertexStride(), - fVertCount, &vertexBuffer, &firstVertex)}; + VertexWriter vertices = target->makeVertexWriter(fProgramInfo->geomProc().vertexStride(), + fVertCount, &vertexBuffer, &firstVertex); if (!vertices) { SkDebugf("Could not allocate vertices\n"); return; @@ -1653,8 +1653,8 @@ private: sk_sp vertexBuffer; int firstVertex; - VertexWriter vertices{target->makeVertexSpace(fProgramInfo->geomProc().vertexStride(), - fVertCount, &vertexBuffer, &firstVertex)}; + VertexWriter vertices = target->makeVertexWriter(fProgramInfo->geomProc().vertexStride(), + fVertCount, &vertexBuffer, &firstVertex); if (!vertices) { SkDebugf("Could not allocate vertices\n"); return; @@ -2684,8 +2684,8 @@ private: sk_sp vertexBuffer; int firstVertex; - VertexWriter verts{target->makeVertexSpace(fProgramInfo->geomProc().vertexStride(), - fVertCount, &vertexBuffer, &firstVertex)}; + VertexWriter verts = target->makeVertexWriter(fProgramInfo->geomProc().vertexStride(), + fVertCount, &vertexBuffer, &firstVertex); if (!verts) { SkDebugf("Could not allocate vertices\n"); return; diff --git a/src/gpu/ops/PathStencilCoverOp.cpp b/src/gpu/ops/PathStencilCoverOp.cpp index a7366216ca..c2e8f5e816 100644 --- a/src/gpu/ops/PathStencilCoverOp.cpp +++ b/src/gpu/ops/PathStencilCoverOp.cpp @@ -244,7 +244,8 @@ void PathStencilCoverOp::onPrepare(GrOpFlushState* flushState) { // edge count of n are fanned by strictly fewer triangles. int maxTrianglesInFans = std::max(maxCombinedFanEdges - 2, 0); int fanTriangleCount = 0; - if (VertexWriter triangleVertexWriter = vertexAlloc.lock(maxTrianglesInFans * 3)) { + if (VertexWriter triangleVertexWriter = + VertexWriter{vertexAlloc.lock(maxTrianglesInFans * 3)}) { for (auto [pathMatrix, path, color] : *fPathDrawList) { AffineMatrix m(pathMatrix); for (PathMiddleOutFanIter it(path); !it.done();) { @@ -270,10 +271,10 @@ void PathStencilCoverOp::onPrepare(GrOpFlushState* flushState) { if (fCoverBBoxProgram) { size_t instanceStride = fCoverBBoxProgram->geomProc().instanceStride(); - VertexWriter vertexWriter = flushState->makeVertexSpace(instanceStride, - fPathCount, - &fBBoxBuffer, - &fBBoxBaseInstance); + VertexWriter vertexWriter = flushState->makeVertexWriter(instanceStride, + fPathCount, + &fBBoxBuffer, + &fBBoxBaseInstance); SkDEBUGCODE(int pathCount = 0;) for (auto [pathMatrix, path, color] : *fPathDrawList) { SkDEBUGCODE(auto end = vertexWriter.makeOffset(instanceStride)); diff --git a/src/gpu/ops/SmallPathRenderer.cpp b/src/gpu/ops/SmallPathRenderer.cpp index 4d1da82d65..43d5feab3f 100644 --- a/src/gpu/ops/SmallPathRenderer.cpp +++ b/src/gpu/ops/SmallPathRenderer.cpp @@ -211,9 +211,9 @@ private: if (instanceCount > SK_MaxS32 / GrResourceProvider::NumVertsPerNonAAQuad()) { return; } - VertexWriter vertices{target->makeVertexSpace( + VertexWriter vertices = target->makeVertexWriter( kVertexStride, GrResourceProvider::NumVertsPerNonAAQuad() * instanceCount, - &flushInfo.fVertexBuffer, &flushInfo.fVertexOffset)}; + &flushInfo.fVertexBuffer, &flushInfo.fVertexOffset); flushInfo.fIndexBuffer = target->resourceProvider()->refNonAAQuadIndexBuffer(); if (!vertices || !flushInfo.fIndexBuffer) { diff --git a/src/gpu/tessellate/PatchWriter.h b/src/gpu/tessellate/PatchWriter.h index 52e2c88cde..e640becd45 100644 --- a/src/gpu/tessellate/PatchWriter.h +++ b/src/gpu/tessellate/PatchWriter.h @@ -142,7 +142,7 @@ private: if (!fFallbackPatchStorage) { fFallbackPatchStorage.reset(fChunker.stride()); } - vertexWriter = fFallbackPatchStorage.data(); + vertexWriter = VertexWriter{fFallbackPatchStorage.data()}; } return vertexWriter; }