diff options
Diffstat (limited to 'samplecode/SampleCCPRGeometry.cpp')
| -rw-r--r-- | samplecode/SampleCCPRGeometry.cpp | 98 |
1 files changed, 43 insertions, 55 deletions
diff --git a/samplecode/SampleCCPRGeometry.cpp b/samplecode/SampleCCPRGeometry.cpp index 16041f5be7..a8f0d9e02c 100644 --- a/samplecode/SampleCCPRGeometry.cpp +++ b/samplecode/SampleCCPRGeometry.cpp @@ -26,15 +26,11 @@ #include "ops/GrDrawOp.h" using TriangleInstance = GrCCPRCoverageProcessor::TriangleInstance; -using CurveInstance = GrCCPRCoverageProcessor::CurveInstance; +using CubicInstance = GrCCPRCoverageProcessor::CubicInstance; using RenderPass = GrCCPRCoverageProcessor::RenderPass; static constexpr float kDebugBloat = 40; -static int num_points(RenderPass renderPass) { - return renderPass >= RenderPass::kSerpentineHulls ? 4 : 3; -} - static int is_quadratic(RenderPass renderPass) { return renderPass >= RenderPass::kQuadraticHulls && renderPass < RenderPass::kSerpentineHulls; } @@ -74,9 +70,8 @@ private: {100.05f, 300.95f} }; - SkTArray<SkPoint> fGpuPoints; - SkTArray<int32_t> fInstanceData; - int fInstanceCount; + SkTArray<TriangleInstance> fTriangleInstances; + SkTArray<CubicInstance> fCubicInstances; typedef SampleView INHERITED; }; @@ -135,7 +130,7 @@ void CCPRGeometryView::onDrawContent(SkCanvas* canvas) { SkPath outline; outline.moveTo(fPoints[0]); - if (4 == num_points(fRenderPass)) { + if (GrCCPRCoverageProcessor::RenderPassIsCubic(fRenderPass)) { outline.cubicTo(fPoints[1], fPoints[2], fPoints[3]); } else if (is_quadratic(fRenderPass)) { outline.quadTo(fPoints[1], fPoints[3]); @@ -179,7 +174,7 @@ void CCPRGeometryView::onDrawContent(SkCanvas* canvas) { pointsPaint.setStrokeWidth(8); pointsPaint.setAntiAlias(true); - if (4 == num_points(fRenderPass)) { + if (GrCCPRCoverageProcessor::RenderPassIsCubic(fRenderPass)) { int w = this->width(), h = this->height(); canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, fPoints, pointsPaint); draw_klm_line(w, h, canvas, &fCubicKLM[0], SK_ColorYELLOW); @@ -198,13 +193,10 @@ void CCPRGeometryView::onDrawContent(SkCanvas* canvas) { } void CCPRGeometryView::updateGpuData() { - int vertexCount = num_points(fRenderPass); + fTriangleInstances.reset(); + fCubicInstances.reset(); - fGpuPoints.reset(); - fInstanceData.reset(); - fInstanceCount = 0; - - if (4 == vertexCount) { + if (GrCCPRCoverageProcessor::RenderPassIsCubic(fRenderPass)) { double t[2], s[2]; SkCubicType type = GrPathUtils::getCubicKLM(fPoints, &fCubicKLM, t, s); if (RenderPass::kSerpentineHulls == fRenderPass && SkCubicType::kLoop == type) { @@ -224,7 +216,6 @@ void CCPRGeometryView::updateGpuData() { geometry.beginContour(fPoints[0]); geometry.cubicTo(fPoints[1], fPoints[2], fPoints[3], kDebugBloat/2, kDebugBloat/2); geometry.endContour(); - fGpuPoints.push_back_n(geometry.points().count(), geometry.points().begin()); int ptsIdx = 0; for (GrCCPRGeometry::Verb verb : geometry.verbs()) { switch (verb) { @@ -236,10 +227,8 @@ void CCPRGeometryView::updateGpuData() { continue; case GrCCPRGeometry::Verb::kMonotonicSerpentineTo: case GrCCPRGeometry::Verb::kMonotonicLoopTo: - fInstanceData.push_back(ptsIdx); - fInstanceData.push_back(0); // Atlas offset. + fCubicInstances.push_back().set(&geometry.points()[ptsIdx], 0, 0); ptsIdx += 3; - ++fInstanceCount; continue; default: continue; } @@ -249,7 +238,7 @@ void CCPRGeometryView::updateGpuData() { geometry.beginContour(fPoints[0]); geometry.quadraticTo(fPoints[1], fPoints[3]); geometry.endContour(); - fGpuPoints.push_back_n(geometry.points().count(), geometry.points().begin()); + int ptsIdx = 0; for (GrCCPRGeometry::Verb verb : geometry.verbs()) { if (GrCCPRGeometry::Verb::kBeginContour == verb || GrCCPRGeometry::Verb::kEndOpenContour == verb || @@ -257,59 +246,58 @@ void CCPRGeometryView::updateGpuData() { continue; } SkASSERT(GrCCPRGeometry::Verb::kMonotonicQuadraticTo == verb); - fInstanceData.push_back(2 * fInstanceCount++); // Pts idx. - fInstanceData.push_back(0); // Atlas offset. + fTriangleInstances.push_back().set(&geometry.points()[ptsIdx], Sk2f(0, 0)); + ptsIdx += 2; } } else { - fGpuPoints.push_back(fPoints[0]); - fGpuPoints.push_back(fPoints[1]); - fGpuPoints.push_back(fPoints[3]); - fInstanceData.push_back(0); - fInstanceData.push_back(1); - fInstanceData.push_back(2); - fInstanceData.push_back(0); // Atlas offset. - fInstanceCount = 1; + fTriangleInstances.push_back().set(fPoints[0], fPoints[1], fPoints[3], Sk2f(0, 0)); } } void CCPRGeometryView::Op::onExecute(GrOpFlushState* state) { - if (fView->fInstanceData.empty()) { - return; - } - GrResourceProvider* rp = state->resourceProvider(); GrContext* context = state->gpu()->getContext(); GrGLGpu* glGpu = kOpenGL_GrBackend == context->contextPriv().getBackend() ? static_cast<GrGLGpu*>(state->gpu()) : nullptr; - int vertexCount = num_points(fView->fRenderPass); - sk_sp<GrBuffer> pointsBuffer(rp->createBuffer(fView->fGpuPoints.count() * sizeof(SkPoint), - kTexel_GrBufferType, kDynamic_GrAccessPattern, + bool isCubic = GrCCPRCoverageProcessor::RenderPassIsCubic(fView->fRenderPass); + GrMesh mesh(isCubic ? GrPrimitiveType::kLinesAdjacency : GrPrimitiveType::kTriangles); + if (isCubic) { + if (fView->fCubicInstances.empty()) { + return; + } + sk_sp<GrBuffer> instBuff(rp->createBuffer(fView->fCubicInstances.count() * + sizeof(CubicInstance), kVertex_GrBufferType, + kDynamic_GrAccessPattern, GrResourceProvider::kNoPendingIO_Flag | GrResourceProvider::kRequireGpuMemory_Flag, - fView->fGpuPoints.begin())); - if (!pointsBuffer) { - return; - } - - sk_sp<GrBuffer> instanceBuffer(rp->createBuffer(fView->fInstanceData.count() * sizeof(int), - kVertex_GrBufferType, kDynamic_GrAccessPattern, - GrResourceProvider::kNoPendingIO_Flag | - GrResourceProvider::kRequireGpuMemory_Flag, - fView->fInstanceData.begin())); - if (!instanceBuffer) { - return; + fView->fCubicInstances.begin())); + if (!instBuff) { + return; + } + mesh.setInstanced(instBuff.get(), fView->fCubicInstances.count(), 0, 4); + } else { + if (fView->fTriangleInstances.empty()) { + return; + } + sk_sp<GrBuffer> instBuff(rp->createBuffer(fView->fTriangleInstances.count() * + sizeof(TriangleInstance), kVertex_GrBufferType, + kDynamic_GrAccessPattern, + GrResourceProvider::kNoPendingIO_Flag | + GrResourceProvider::kRequireGpuMemory_Flag, + fView->fTriangleInstances.begin())); + if (!instBuff) { + return; + } + mesh.setInstanced(instBuff.get(), fView->fTriangleInstances.count(), 0, 3); } GrPipeline pipeline(state->drawOpArgs().fProxy, GrPipeline::ScissorState::kDisabled, SkBlendMode::kSrcOver); - GrCCPRCoverageProcessor ccprProc(fView->fRenderPass, pointsBuffer.get()); + GrCCPRCoverageProcessor ccprProc(fView->fRenderPass); SkDEBUGCODE(ccprProc.enableDebugVisualizations(kDebugBloat);) - GrMesh mesh(4 == vertexCount ? GrPrimitiveType::kLinesAdjacency : GrPrimitiveType::kTriangles); - mesh.setInstanced(instanceBuffer.get(), fView->fInstanceCount, 0, vertexCount); - if (glGpu) { glGpu->handleDirtyContext(); GR_GL_CALL(glGpu->glInterface(), PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE)); @@ -348,7 +336,7 @@ private: SkView::Click* CCPRGeometryView::onFindClickHandler(SkScalar x, SkScalar y, unsigned) { for (int i = 0; i < 4; ++i) { - if (4 != num_points(fRenderPass) && 2 == i) { + if (!GrCCPRCoverageProcessor::RenderPassIsCubic(fRenderPass) && 2 == i) { continue; } if (fabs(x - fPoints[i].x()) < 20 && fabsf(y - fPoints[i].y()) < 20) { |
