| 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "src/gpu/ops/GrTriangulatingPathRenderer.h" |
| 9 | |
| 10 | #include "include/private/SkIDChangeListener.h" |
| 11 | #include "src/core/SkGeometry.h" |
| 12 | #include "src/gpu/GrAuditTrail.h" |
| 13 | #include "src/gpu/GrCaps.h" |
| 14 | #include "src/gpu/GrDefaultGeoProcFactory.h" |
| 15 | #include "src/gpu/GrDrawOpTest.h" |
| 16 | #include "src/gpu/GrEagerVertexAllocator.h" |
| 17 | #include "src/gpu/GrOpFlushState.h" |
| 18 | #include "src/gpu/GrProgramInfo.h" |
| 19 | #include "src/gpu/GrRenderTargetContext.h" |
| 20 | #include "src/gpu/GrResourceCache.h" |
| 21 | #include "src/gpu/GrResourceProvider.h" |
| 22 | #include "src/gpu/GrSimpleMesh.h" |
| 23 | #include "src/gpu/GrStyle.h" |
| 24 | #include "src/gpu/GrTriangulator.h" |
| 25 | #include "src/gpu/geometry/GrPathUtils.h" |
| 26 | #include "src/gpu/geometry/GrStyledShape.h" |
| 27 | #include "src/gpu/ops/GrMeshDrawOp.h" |
| 28 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelperWithStencil.h" |
| 29 | |
| 30 | #include <cstdio> |
| 31 | |
| 32 | #ifndef GR_AA_TESSELLATOR_MAX_VERB_COUNT |
| 33 | #define GR_AA_TESSELLATOR_MAX_VERB_COUNT 10 |
| 34 | #endif |
| 35 | |
| 36 | /* |
| 37 | * This path renderer linearizes and decomposes the path into triangles using GrTriangulator, |
| 38 | * uploads the triangles to a vertex buffer, and renders them with a single draw call. It can do |
| 39 | * screenspace antialiasing with a one-pixel coverage ramp. |
| 40 | */ |
| 41 | namespace { |
| 42 | |
| 43 | struct TessInfo { |
| 44 | SkScalar fTolerance; |
| 45 | int fCount; |
| 46 | }; |
| 47 | |
| 48 | // When the SkPathRef genID changes, invalidate a corresponding GrResource described by key. |
| 49 | class UniqueKeyInvalidator : public SkIDChangeListener { |
| 50 | public: |
| 51 | UniqueKeyInvalidator(const GrUniqueKey& key, uint32_t contextUniqueID) |
| 52 | : fMsg(key, contextUniqueID) {} |
| 53 | |
| 54 | private: |
| 55 | GrUniqueKeyInvalidatedMessage fMsg; |
| 56 | |
| 57 | void changed() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); } |
| 58 | }; |
| 59 | |
| 60 | bool cache_match(GrGpuBuffer* vertexBuffer, SkScalar tol, int* actualCount) { |
| 61 | if (!vertexBuffer) { |
| 62 | return false; |
| 63 | } |
| 64 | const SkData* data = vertexBuffer->getUniqueKey().getCustomData(); |
| 65 | SkASSERT(data); |
| 66 | const TessInfo* info = static_cast<const TessInfo*>(data->data()); |
| 67 | if (info->fTolerance == 0 || info->fTolerance < 3.0f * tol) { |
| 68 | *actualCount = info->fCount; |
| 69 | return true; |
| 70 | } |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | class StaticVertexAllocator : public GrEagerVertexAllocator { |
| 75 | public: |
| 76 | StaticVertexAllocator(GrResourceProvider* resourceProvider, bool canMapVB) |
| 77 | : fResourceProvider(resourceProvider) |
| 78 | , fCanMapVB(canMapVB) |
| 79 | , fVertices(nullptr) { |
| 80 | } |
| 81 | #ifdef SK_DEBUG |
| 82 | ~StaticVertexAllocator() override { |
| 83 | SkASSERT(!fLockStride); |
| 84 | } |
| 85 | #endif |
| 86 | void* lock(size_t stride, int eagerCount) override { |
| 87 | SkASSERT(!fLockStride); |
| 88 | SkASSERT(stride); |
| 89 | size_t size = eagerCount * stride; |
| 90 | fVertexBuffer = fResourceProvider->createBuffer(size, GrGpuBufferType::kVertex, |
| 91 | kStatic_GrAccessPattern); |
| 92 | if (!fVertexBuffer.get()) { |
| 93 | return nullptr; |
| 94 | } |
| 95 | if (fCanMapVB) { |
| 96 | fVertices = fVertexBuffer->map(); |
| 97 | } else { |
| 98 | fVertices = sk_malloc_throw(eagerCount * stride); |
| 99 | } |
| 100 | fLockStride = stride; |
| 101 | return fVertices; |
| 102 | } |
| 103 | void unlock(int actualCount) override { |
| 104 | SkASSERT(fLockStride); |
| 105 | if (fCanMapVB) { |
| 106 | fVertexBuffer->unmap(); |
| 107 | } else { |
| 108 | fVertexBuffer->updateData(fVertices, actualCount * fLockStride); |
| 109 | sk_free(fVertices); |
| 110 | } |
| 111 | fVertices = nullptr; |
| 112 | fLockStride = 0; |
| 113 | } |
| 114 | sk_sp<GrGpuBuffer> detachVertexBuffer() { return std::move(fVertexBuffer); } |
| 115 | |
| 116 | private: |
| 117 | sk_sp<GrGpuBuffer> fVertexBuffer; |
| 118 | GrResourceProvider* fResourceProvider; |
| 119 | bool fCanMapVB; |
| 120 | void* fVertices; |
| 121 | size_t fLockStride = 0; |
| 122 | }; |
| 123 | |
| 124 | } // namespace |
| 125 | |
| 126 | GrTriangulatingPathRenderer::GrTriangulatingPathRenderer() |
| 127 | : fMaxVerbCount(GR_AA_TESSELLATOR_MAX_VERB_COUNT) { |
| 128 | } |
| 129 | |
| 130 | GrPathRenderer::CanDrawPath |
| 131 | GrTriangulatingPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
| 132 | // This path renderer can draw fill styles, and can do screenspace antialiasing via a |
| 133 | // one-pixel coverage ramp. It can do convex and concave paths, but we'll leave the convex |
| 134 | // ones to simpler algorithms. We pass on paths that have styles, though they may come back |
| 135 | // around after applying the styling information to the geometry to create a filled path. |
| 136 | if (!args.fShape->style().isSimpleFill() || args.fShape->knownToBeConvex()) { |
| 137 | return CanDrawPath::kNo; |
| 138 | } |
| 139 | switch (args.fAAType) { |
| 140 | case GrAAType::kNone: |
| 141 | case GrAAType::kMSAA: |
| 142 | // Prefer MSAA, if any antialiasing. In the non-analytic-AA case, We skip paths that |
| 143 | // don't have a key since the real advantage of this path renderer comes from caching |
| 144 | // the tessellated geometry. |
| 145 | if (!args.fShape->hasUnstyledKey()) { |
| 146 | return CanDrawPath::kNo; |
| 147 | } |
| 148 | break; |
| 149 | case GrAAType::kCoverage: |
| 150 | // Use analytic AA if we don't have MSAA. In this case, we do not cache, so we accept |
| 151 | // paths without keys. |
| 152 | SkPath path; |
| 153 | args.fShape->asPath(&path); |
| 154 | if (path.countVerbs() > fMaxVerbCount) { |
| 155 | return CanDrawPath::kNo; |
| 156 | } |
| 157 | break; |
| 158 | } |
| 159 | return CanDrawPath::kYes; |
| 160 | } |
| 161 | |
| 162 | namespace { |
| 163 | |
| 164 | class TriangulatingPathOp final : public GrMeshDrawOp { |
| 165 | private: |
| 166 | using Helper = GrSimpleMeshDrawOpHelperWithStencil; |
| 167 | |
| 168 | public: |
| 169 | DEFINE_OP_CLASS_ID |
| 170 | |
| 171 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
| 172 | GrPaint&& paint, |
| 173 | const GrStyledShape& shape, |
| 174 | const SkMatrix& viewMatrix, |
| 175 | SkIRect devClipBounds, |
| 176 | GrAAType aaType, |
| 177 | const GrUserStencilSettings* stencilSettings) { |
| 178 | return Helper::FactoryHelper<TriangulatingPathOp>(context, std::move(paint), shape, |
| 179 | viewMatrix, devClipBounds, aaType, |
| 180 | stencilSettings); |
| 181 | } |
| 182 | |
| 183 | const char* name() const override { return "TriangulatingPathOp" ; } |
| 184 | |
| 185 | void visitProxies(const VisitProxyFunc& func) const override { |
| 186 | if (fProgramInfo) { |
| 187 | fProgramInfo->visitFPProxies(func); |
| 188 | } else { |
| 189 | fHelper.visitProxies(func); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | TriangulatingPathOp(Helper::MakeArgs helperArgs, |
| 194 | const SkPMColor4f& color, |
| 195 | const GrStyledShape& shape, |
| 196 | const SkMatrix& viewMatrix, |
| 197 | const SkIRect& devClipBounds, |
| 198 | GrAAType aaType, |
| 199 | const GrUserStencilSettings* stencilSettings) |
| 200 | : INHERITED(ClassID()) |
| 201 | , fHelper(helperArgs, aaType, stencilSettings) |
| 202 | , fColor(color) |
| 203 | , fShape(shape) |
| 204 | , fViewMatrix(viewMatrix) |
| 205 | , fDevClipBounds(devClipBounds) |
| 206 | , fAntiAlias(GrAAType::kCoverage == aaType) { |
| 207 | SkRect devBounds; |
| 208 | viewMatrix.mapRect(&devBounds, shape.bounds()); |
| 209 | if (shape.inverseFilled()) { |
| 210 | // Because the clip bounds are used to add a contour for inverse fills, they must also |
| 211 | // include the path bounds. |
| 212 | devBounds.join(SkRect::Make(fDevClipBounds)); |
| 213 | } |
| 214 | this->setBounds(devBounds, HasAABloat::kNo, IsHairline::kNo); |
| 215 | } |
| 216 | |
| 217 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 218 | |
| 219 | GrProcessorSet::Analysis finalize( |
| 220 | const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage, |
| 221 | GrClampType clampType) override { |
| 222 | GrProcessorAnalysisCoverage coverage = fAntiAlias |
| 223 | ? GrProcessorAnalysisCoverage::kSingleChannel |
| 224 | : GrProcessorAnalysisCoverage::kNone; |
| 225 | // This Op uses uniform (not vertex) color, so doesn't need to track wide color. |
| 226 | return fHelper.finalizeProcessors( |
| 227 | caps, clip, hasMixedSampledCoverage, clampType, coverage, &fColor, nullptr); |
| 228 | } |
| 229 | |
| 230 | private: |
| 231 | SkPath getPath() const { |
| 232 | SkASSERT(!fShape.style().applies()); |
| 233 | SkPath path; |
| 234 | fShape.asPath(&path); |
| 235 | return path; |
| 236 | } |
| 237 | |
| 238 | void draw(Target* target) { |
| 239 | SkASSERT(!fAntiAlias); |
| 240 | GrResourceProvider* rp = target->resourceProvider(); |
| 241 | bool inverseFill = fShape.inverseFilled(); |
| 242 | // construct a cache key from the path's genID and the view matrix |
| 243 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 244 | GrUniqueKey key; |
| 245 | static constexpr int kClipBoundsCnt = sizeof(fDevClipBounds) / sizeof(uint32_t); |
| 246 | int shapeKeyDataCnt = fShape.unstyledKeySize(); |
| 247 | SkASSERT(shapeKeyDataCnt >= 0); |
| 248 | GrUniqueKey::Builder builder(&key, kDomain, shapeKeyDataCnt + kClipBoundsCnt, "Path" ); |
| 249 | fShape.writeUnstyledKey(&builder[0]); |
| 250 | // For inverse fills, the tessellation is dependent on clip bounds. |
| 251 | if (inverseFill) { |
| 252 | memcpy(&builder[shapeKeyDataCnt], &fDevClipBounds, sizeof(fDevClipBounds)); |
| 253 | } else { |
| 254 | memset(&builder[shapeKeyDataCnt], 0, sizeof(fDevClipBounds)); |
| 255 | } |
| 256 | builder.finish(); |
| 257 | sk_sp<GrGpuBuffer> cachedVertexBuffer(rp->findByUniqueKey<GrGpuBuffer>(key)); |
| 258 | int actualCount; |
| 259 | SkScalar tol = GrPathUtils::kDefaultTolerance; |
| 260 | tol = GrPathUtils::scaleToleranceToSrc(tol, fViewMatrix, fShape.bounds()); |
| 261 | if (cache_match(cachedVertexBuffer.get(), tol, &actualCount)) { |
| 262 | this->createMesh(target, std::move(cachedVertexBuffer), 0, actualCount); |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | SkRect clipBounds = SkRect::Make(fDevClipBounds); |
| 267 | |
| 268 | SkMatrix vmi; |
| 269 | if (!fViewMatrix.invert(&vmi)) { |
| 270 | return; |
| 271 | } |
| 272 | vmi.mapRect(&clipBounds); |
| 273 | int numCountedCurves; |
| 274 | bool canMapVB = GrCaps::kNone_MapFlags != target->caps().mapBufferFlags(); |
| 275 | StaticVertexAllocator allocator(rp, canMapVB); |
| 276 | int vertexCount = GrTriangulator::PathToTriangles(getPath(), tol, clipBounds, &allocator, |
| 277 | GrTriangulator::Mode::kNormal, |
| 278 | &numCountedCurves); |
| 279 | if (vertexCount == 0) { |
| 280 | return; |
| 281 | } |
| 282 | sk_sp<GrGpuBuffer> vb = allocator.detachVertexBuffer(); |
| 283 | TessInfo info; |
| 284 | info.fTolerance = (numCountedCurves == 0) ? 0 : tol; |
| 285 | info.fCount = vertexCount; |
| 286 | fShape.addGenIDChangeListener( |
| 287 | sk_make_sp<UniqueKeyInvalidator>(key, target->contextUniqueID())); |
| 288 | key.setCustomData(SkData::MakeWithCopy(&info, sizeof(info))); |
| 289 | rp->assignUniqueKeyToResource(key, vb.get()); |
| 290 | |
| 291 | this->createMesh(target, std::move(vb), 0, vertexCount); |
| 292 | } |
| 293 | |
| 294 | void drawAA(Target* target) { |
| 295 | SkASSERT(fAntiAlias); |
| 296 | SkPath path = getPath(); |
| 297 | if (path.isEmpty()) { |
| 298 | return; |
| 299 | } |
| 300 | SkRect clipBounds = SkRect::Make(fDevClipBounds); |
| 301 | path.transform(fViewMatrix); |
| 302 | SkScalar tol = GrPathUtils::kDefaultTolerance; |
| 303 | sk_sp<const GrBuffer> vertexBuffer; |
| 304 | int firstVertex; |
| 305 | int numCountedCurves; |
| 306 | GrEagerDynamicVertexAllocator allocator(target, &vertexBuffer, &firstVertex); |
| 307 | int vertexCount = GrTriangulator::PathToTriangles(path, tol, clipBounds, &allocator, |
| 308 | GrTriangulator::Mode::kEdgeAntialias, |
| 309 | &numCountedCurves); |
| 310 | if (vertexCount == 0) { |
| 311 | return; |
| 312 | } |
| 313 | this->createMesh(target, std::move(vertexBuffer), firstVertex, vertexCount); |
| 314 | } |
| 315 | |
| 316 | GrProgramInfo* programInfo() override { return fProgramInfo; } |
| 317 | |
| 318 | void onCreateProgramInfo(const GrCaps* caps, |
| 319 | SkArenaAlloc* arena, |
| 320 | const GrSurfaceProxyView* writeView, |
| 321 | GrAppliedClip&& appliedClip, |
| 322 | const GrXferProcessor::DstProxyView& dstProxyView) override { |
| 323 | GrGeometryProcessor* gp; |
| 324 | { |
| 325 | using namespace GrDefaultGeoProcFactory; |
| 326 | |
| 327 | Color color(fColor); |
| 328 | LocalCoords::Type localCoordsType = fHelper.usesLocalCoords() |
| 329 | ? LocalCoords::kUsePosition_Type |
| 330 | : LocalCoords::kUnused_Type; |
| 331 | Coverage::Type coverageType; |
| 332 | if (fAntiAlias) { |
| 333 | if (fHelper.compatibleWithCoverageAsAlpha()) { |
| 334 | coverageType = Coverage::kAttributeTweakAlpha_Type; |
| 335 | } else { |
| 336 | coverageType = Coverage::kAttribute_Type; |
| 337 | } |
| 338 | } else { |
| 339 | coverageType = Coverage::kSolid_Type; |
| 340 | } |
| 341 | if (fAntiAlias) { |
| 342 | gp = GrDefaultGeoProcFactory::MakeForDeviceSpace(arena, color, coverageType, |
| 343 | localCoordsType, fViewMatrix); |
| 344 | } else { |
| 345 | gp = GrDefaultGeoProcFactory::Make(arena, color, coverageType, localCoordsType, |
| 346 | fViewMatrix); |
| 347 | } |
| 348 | } |
| 349 | if (!gp) { |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | #ifdef SK_DEBUG |
| 354 | auto mode = (fAntiAlias) ? GrTriangulator::Mode::kEdgeAntialias |
| 355 | : GrTriangulator::Mode::kNormal; |
| 356 | SkASSERT(GrTriangulator::GetVertexStride(mode) == gp->vertexStride()); |
| 357 | #endif |
| 358 | |
| 359 | GrPrimitiveType primitiveType = TRIANGULATOR_WIREFRAME ? GrPrimitiveType::kLines |
| 360 | : GrPrimitiveType::kTriangles; |
| 361 | |
| 362 | fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView, |
| 363 | std::move(appliedClip), dstProxyView, |
| 364 | gp, primitiveType); |
| 365 | } |
| 366 | |
| 367 | void onPrepareDraws(Target* target) override { |
| 368 | if (fAntiAlias) { |
| 369 | this->drawAA(target); |
| 370 | } else { |
| 371 | this->draw(target); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | void createMesh(Target* target, sk_sp<const GrBuffer> vb, int firstVertex, int count) { |
| 376 | fMesh = target->allocMesh(); |
| 377 | fMesh->set(std::move(vb), count, firstVertex); |
| 378 | } |
| 379 | |
| 380 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
| 381 | if (!fProgramInfo) { |
| 382 | this->createProgramInfo(flushState); |
| 383 | } |
| 384 | |
| 385 | if (!fProgramInfo || !fMesh) { |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds); |
| 390 | flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline()); |
| 391 | flushState->drawMesh(*fMesh); |
| 392 | } |
| 393 | |
| 394 | #if GR_TEST_UTILS |
| 395 | SkString onDumpInfo() const override { |
| 396 | return SkStringPrintf("Color 0x%08x, aa: %d\n%s" , |
| 397 | fColor.toBytes_RGBA(), fAntiAlias, fHelper.dumpInfo().c_str()); |
| 398 | } |
| 399 | #endif |
| 400 | |
| 401 | Helper fHelper; |
| 402 | SkPMColor4f fColor; |
| 403 | GrStyledShape fShape; |
| 404 | SkMatrix fViewMatrix; |
| 405 | SkIRect fDevClipBounds; |
| 406 | bool fAntiAlias; |
| 407 | |
| 408 | GrSimpleMesh* fMesh = nullptr; |
| 409 | GrProgramInfo* fProgramInfo = nullptr; |
| 410 | |
| 411 | typedef GrMeshDrawOp INHERITED; |
| 412 | }; |
| 413 | |
| 414 | } // anonymous namespace |
| 415 | |
| 416 | bool GrTriangulatingPathRenderer::onDrawPath(const DrawPathArgs& args) { |
| 417 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
| 418 | "GrTriangulatingPathRenderer::onDrawPath" ); |
| 419 | |
| 420 | std::unique_ptr<GrDrawOp> op = TriangulatingPathOp::Make( |
| 421 | args.fContext, std::move(args.fPaint), *args.fShape, *args.fViewMatrix, |
| 422 | *args.fClipConservativeBounds, args.fAAType, args.fUserStencilSettings); |
| 423 | args.fRenderTargetContext->addDrawOp(args.fClip, std::move(op)); |
| 424 | return true; |
| 425 | } |
| 426 | |
| 427 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 428 | |
| 429 | #if GR_TEST_UTILS |
| 430 | |
| 431 | GR_DRAW_OP_TEST_DEFINE(TriangulatingPathOp) { |
| 432 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
| 433 | const SkPath& path = GrTest::TestPath(random); |
| 434 | SkIRect devClipBounds = SkIRect::MakeLTRB( |
| 435 | random->nextU(), random->nextU(), random->nextU(), random->nextU()); |
| 436 | devClipBounds.sort(); |
| 437 | static constexpr GrAAType kAATypes[] = {GrAAType::kNone, GrAAType::kMSAA, GrAAType::kCoverage}; |
| 438 | GrAAType aaType; |
| 439 | do { |
| 440 | aaType = kAATypes[random->nextULessThan(SK_ARRAY_COUNT(kAATypes))]; |
| 441 | } while(GrAAType::kMSAA == aaType && numSamples <= 1); |
| 442 | GrStyle style; |
| 443 | do { |
| 444 | GrTest::TestStyle(random, &style); |
| 445 | } while (!style.isSimpleFill()); |
| 446 | GrStyledShape shape(path, style); |
| 447 | return TriangulatingPathOp::Make(context, std::move(paint), shape, viewMatrix, devClipBounds, |
| 448 | aaType, GrGetRandomStencil(random, context)); |
| 449 | } |
| 450 | |
| 451 | #endif |
| 452 | |