| 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 |
#include "src/gpu/ganesh/ops/TriangulatingPathRenderer.h" |
| 8 |
|
| 9 |
#include "include/core/SkData.h" |
| 10 |
#include "include/core/SkMatrix.h" |
| 11 |
#include "include/core/SkPath.h" |
| 12 |
#include "include/core/SkPoint.h" |
| 13 |
#include "include/core/SkRect.h" |
| 14 |
#include "include/core/SkRefCnt.h" |
| 15 |
#include "include/core/SkScalar.h" |
| 16 |
#include "include/core/SkString.h" |
| 17 |
#include "include/core/SkSurfaceProps.h" |
| 18 |
#include "include/core/SkTypes.h" |
| 19 |
#include "include/gpu/ganesh/GrRecordingContext.h" |
| 20 |
#include "include/private/SkIDChangeListener.h" |
| 21 |
#include "include/private/base/SkMalloc.h" |
| 22 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 23 |
#include "src/core/SkColorData.h" |
| 24 |
#include "src/core/SkMessageBus.h" |
| 25 |
#include "src/core/SkTraceEvent.h" |
| 26 |
#include "src/gpu/ResourceKey.h" |
| 27 |
#include "src/gpu/ganesh/GrAppliedClip.h" |
| 28 |
#include "src/gpu/ganesh/GrAuditTrail.h" |
| 29 |
#include "src/gpu/ganesh/GrBuffer.h" |
| 30 |
#include "src/gpu/ganesh/GrCaps.h" |
| 31 |
#include "src/gpu/ganesh/GrDefaultGeoProcFactory.h" |
| 32 |
#include "src/gpu/ganesh/GrDrawOpTest.h" |
| 33 |
#include "src/gpu/ganesh/GrEagerVertexAllocator.h" |
| 34 |
#include "src/gpu/ganesh/GrGeometryProcessor.h" |
| 35 |
#include "src/gpu/ganesh/GrGpuBuffer.h" |
| 36 |
#include "src/gpu/ganesh/GrMeshDrawTarget.h" |
| 37 |
#include "src/gpu/ganesh/GrOpFlushState.h" |
| 38 |
#include "src/gpu/ganesh/GrPaint.h" |
| 39 |
#include "src/gpu/ganesh/GrProcessorAnalysis.h" |
| 40 |
#include "src/gpu/ganesh/GrProcessorSet.h" |
| 41 |
#include "src/gpu/ganesh/GrProgramInfo.h" |
| 42 |
#include "src/gpu/ganesh/GrRecordingContextPriv.h" |
| 43 |
#include "src/gpu/ganesh/GrResourceProvider.h" |
| 44 |
#include "src/gpu/ganesh/GrSimpleMesh.h" |
| 45 |
#include "src/gpu/ganesh/GrStyle.h" |
| 46 |
#include "src/gpu/ganesh/GrThreadSafeCache.h" |
| 47 |
#include "src/gpu/ganesh/SurfaceDrawContext.h" |
| 48 |
#include "src/gpu/ganesh/geometry/GrAATriangulator.h" |
| 49 |
#include "src/gpu/ganesh/geometry/GrPathUtils.h" |
| 50 |
#include "src/gpu/ganesh/geometry/GrStyledShape.h" |
| 51 |
#include "src/gpu/ganesh/geometry/GrTriangulator.h" |
| 52 |
#include "src/gpu/ganesh/ops/GrMeshDrawOp.h" |
| 53 |
#include "src/gpu/ganesh/ops/GrOp.h" |
| 54 |
#include "src/gpu/ganesh/ops/GrSimpleMeshDrawOpHelperWithStencil.h" |
| 55 |
|
| 56 |
#if defined(GPU_TEST_UTILS) |
| 57 |
#include "src/base/SkRandom.h" |
| 58 |
#include "src/gpu/ganesh/GrTestUtils.h" |
| 59 |
#endif |
| 60 |
|
| 61 |
#include <array> |
| 62 |
#include <cstdint> |
| 63 |
#include <cstring> |
| 64 |
#include <utility> |
| 65 |
|
| 66 |
class GrDstProxyView; |
| 67 |
class GrSurfaceProxyView; |
| 68 |
class SkArenaAlloc; |
| 69 |
enum class GrXferBarrierFlags; |
| 70 |
struct GrUserStencilSettings; |
| 71 |
|
| 72 |
#if !defined(SK_ENABLE_OPTIMIZE_SIZE) |
| 73 |
|
| 74 |
#ifndef GR_AA_TESSELLATOR_MAX_VERB_COUNT |
| 75 |
#define GR_AA_TESSELLATOR_MAX_VERB_COUNT 10 |
| 76 |
#endif |
| 77 |
|
| 78 |
/* |
| 79 |
* This path renderer linearizes and decomposes the path into triangles using GrTriangulator, |
| 80 |
* uploads the triangles to a vertex buffer, and renders them with a single draw call. It can do |
| 81 |
* screenspace antialiasing with a one-pixel coverage ramp. |
| 82 |
*/ |
| 83 |
namespace { |
| 84 |
|
| 85 |
// The TessInfo struct contains ancillary data not specifically required for the triangle |
| 86 |
// data (which is stored in a GrThreadSafeCache::VertexData object). |
| 87 |
// The 'fNumVertices' field is a temporary exception. It is still needed to support the |
| 88 |
// AA triangulated path case - which doesn't use the GrThreadSafeCache nor the VertexData object). |
| 89 |
// When there is an associated VertexData, its numVertices should always match the TessInfo's |
| 90 |
// value. |
| 91 |
struct TessInfo { |
| 92 |
int fNumVertices; |
| 93 |
bool fIsLinear; |
| 94 |
SkScalar fTolerance; |
| 95 |
}; |
| 96 |
|
| 97 |
static sk_sp<SkData> create_data(int numVertices, bool isLinear, SkScalar tol) { |
| 98 |
TessInfo info { numVertices, isLinear, tol }; |
| 99 |
return SkData::MakeWithCopy(&info, sizeof(info)); |
| 100 |
} |
| 101 |
|
| 102 |
bool cache_match(const SkData* data, SkScalar tol) { |
| 103 |
SkASSERT(data); |
| 104 |
|
| 105 |
const TessInfo* info = static_cast<const TessInfo*>(data->data()); |
| 106 |
|
| 107 |
return info->fIsLinear || info->fTolerance < 3.0f * tol; |
| 108 |
} |
| 109 |
|
| 110 |
// Should 'challenger' replace 'incumbent' in the cache if there is a collision? |
| 111 |
bool is_newer_better(SkData* incumbent, SkData* challenger) { |
| 112 |
const TessInfo* i = static_cast<const TessInfo*>(incumbent->data()); |
| 113 |
const TessInfo* c = static_cast<const TessInfo*>(challenger->data()); |
| 114 |
|
| 115 |
if (i->fIsLinear || i->fTolerance <= c->fTolerance) { |
| 116 |
return false; // prefer the incumbent |
| 117 |
} |
| 118 |
|
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
// When the SkPathRef genID changes, invalidate a corresponding GrResource described by key. |
| 123 |
class UniqueKeyInvalidator : public SkIDChangeListener { |
| 124 |
public: |
| 125 |
UniqueKeyInvalidator(const skgpu::UniqueKey& key, uint32_t contextUniqueID) |
| 126 |
: fMsg(key, contextUniqueID, /* inThreadSafeCache */ true) {} |
| 127 |
|
| 128 |
private: |
| 129 |
skgpu::UniqueKeyInvalidatedMessage fMsg; |
| 130 |
|
| 131 |
void changed() override { |
| 132 |
SkMessageBus<skgpu::UniqueKeyInvalidatedMessage, uint32_t>::Post(fMsg); |
| 133 |
} |
| 134 |
}; |
| 135 |
|
| 136 |
class StaticVertexAllocator : public GrEagerVertexAllocator { |
| 137 |
public: |
| 138 |
StaticVertexAllocator(GrResourceProvider* resourceProvider, bool canMapVB) |
| 139 |
: fResourceProvider(resourceProvider) |
| 140 |
, fCanMapVB(canMapVB) { |
| 141 |
} |
| 142 |
|
| 143 |
#ifdef SK_DEBUG |
| 144 |
~StaticVertexAllocator() override { |
| 145 |
SkASSERT(!fLockStride && !fVertices && !fVertexBuffer && !fVertexData); |
| 146 |
} |
| 147 |
#endif |
| 148 |
|
| 149 |
void* lock(size_t stride, int eagerCount) override { |
| 150 |
SkASSERT(!fLockStride && !fVertices && !fVertexBuffer && !fVertexData); |
| 151 |
SkASSERT(stride && eagerCount); |
| 152 |
|
| 153 |
size_t size = eagerCount * stride; |
| 154 |
fVertexBuffer = fResourceProvider->createBuffer(size, |
| 155 |
GrGpuBufferType::kVertex, |
| 156 |
kStatic_GrAccessPattern, |
| 157 |
GrResourceProvider::ZeroInit::kNo); |
| 158 |
if (!fVertexBuffer) { |
| 159 |
return nullptr; |
| 160 |
} |
| 161 |
if (fCanMapVB) { |
| 162 |
fVertices = fVertexBuffer->map(); |
| 163 |
} |
| 164 |
if (!fVertices) { |
| 165 |
fVertices = sk_malloc_throw(eagerCount * stride); |
| 166 |
fCanMapVB = false; |
| 167 |
} |
| 168 |
fLockStride = stride; |
| 169 |
return fVertices; |
| 170 |
} |
| 171 |
|
| 172 |
void unlock(int actualCount) override { |
| 173 |
SkASSERT(fLockStride && fVertices && fVertexBuffer && !fVertexData); |
| 174 |
|
| 175 |
if (fCanMapVB) { |
| 176 |
fVertexBuffer->unmap(); |
| 177 |
} else { |
| 178 |
fVertexBuffer->updateData(fVertices, |
| 179 |
/*offset=*/0, |
| 180 |
/*size=*/actualCount*fLockStride, |
| 181 |
/*preserve=*/false); |
| 182 |
sk_free(fVertices); |
| 183 |
} |
| 184 |
|
| 185 |
fVertexData = GrThreadSafeCache::MakeVertexData(std::move(fVertexBuffer), |
| 186 |
actualCount, fLockStride); |
| 187 |
|
| 188 |
fVertices = nullptr; |
| 189 |
fLockStride = 0; |
| 190 |
} |
| 191 |
|
| 192 |
sk_sp<GrThreadSafeCache::VertexData> detachVertexData() { |
| 193 |
SkASSERT(!fLockStride && !fVertices && !fVertexBuffer && fVertexData); |
| 194 |
|
| 195 |
return std::move(fVertexData); |
| 196 |
} |
| 197 |
|
| 198 |
private: |
| 199 |
sk_sp<GrThreadSafeCache::VertexData> fVertexData; |
| 200 |
sk_sp<GrGpuBuffer> fVertexBuffer; |
| 201 |
GrResourceProvider* fResourceProvider; |
| 202 |
bool fCanMapVB; |
| 203 |
void* fVertices = nullptr; |
| 204 |
size_t fLockStride = 0; |
| 205 |
}; |
| 206 |
|
| 207 |
class TriangulatingPathOp final : public GrMeshDrawOp { |
| 208 |
private: |
| 209 |
using Helper = GrSimpleMeshDrawOpHelperWithStencil; |
| 210 |
|
| 211 |
public: |
| 212 |
DEFINE_OP_CLASS_ID |
| 213 |
|
| 214 |
static GrOp::Owner Make(GrRecordingContext* context, |
| 215 |
GrPaint&& paint, |
| 216 |
const GrStyledShape& shape, |
| 217 |
const SkMatrix& viewMatrix, |
| 218 |
SkIRect devClipBounds, |
| 219 |
GrAAType aaType, |
| 220 |
const GrUserStencilSettings* stencilSettings) { |
| 221 |
return Helper::FactoryHelper<TriangulatingPathOp>(context, std::move(paint), shape, |
| 222 |
viewMatrix, devClipBounds, aaType, |
| 223 |
stencilSettings); |
| 224 |
} |
| 225 |
|
| 226 |
const char* name() const override { return "TriangulatingPathOp"; } |
| 227 |
|
| 228 |
void visitProxies(const GrVisitProxyFunc& func) const override { |
| 229 |
if (fProgramInfo) { |
| 230 |
fProgramInfo->visitFPProxies(func); |
| 231 |
} else { |
| 232 |
fHelper.visitProxies(func); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
TriangulatingPathOp(GrProcessorSet* processorSet, |
| 237 |
const SkPMColor4f& color, |
| 238 |
const GrStyledShape& shape, |
| 239 |
const SkMatrix& viewMatrix, |
| 240 |
const SkIRect& devClipBounds, |
| 241 |
GrAAType aaType, |
| 242 |
const GrUserStencilSettings* stencilSettings) |
| 243 |
: INHERITED(ClassID()) |
| 244 |
, fHelper(processorSet, aaType, stencilSettings) |
| 245 |
, fColor(color) |
| 246 |
, fShape(shape) |
| 247 |
, fViewMatrix(viewMatrix) |
| 248 |
, fDevClipBounds(devClipBounds) |
| 249 |
, fAntiAlias(GrAAType::kCoverage == aaType) { |
| 250 |
SkRect devBounds; |
| 251 |
viewMatrix.mapRect(&devBounds, shape.bounds()); |
| 252 |
if (shape.inverseFilled()) { |
| 253 |
// Because the clip bounds are used to add a contour for inverse fills, they must also |
| 254 |
// include the path bounds. |
| 255 |
devBounds.join(SkRect::Make(fDevClipBounds)); |
| 256 |
} |
| 257 |
this->setBounds(devBounds, HasAABloat(fAntiAlias), IsHairline::kNo); |
| 258 |
} |
| 259 |
|
| 260 |
FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 261 |
|
| 262 |
GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip, |
| 263 |
GrClampType clampType) override { |
| 264 |
GrProcessorAnalysisCoverage coverage = fAntiAlias |
| 265 |
? GrProcessorAnalysisCoverage::kSingleChannel |
| 266 |
: GrProcessorAnalysisCoverage::kNone; |
| 267 |
// This Op uses uniform (not vertex) color, so doesn't need to track wide color. |
| 268 |
return fHelper.finalizeProcessors(caps, clip, clampType, coverage, &fColor, nullptr); |
| 269 |
} |
| 270 |
|
| 271 |
private: |
| 272 |
SkPath getPath() const { |
| 273 |
SkASSERT(!fShape.style().applies()); |
| 274 |
return fShape.asPath(); |
| 275 |
} |
| 276 |
|
| 277 |
static void CreateKey(skgpu::UniqueKey* key, |
| 278 |
const GrStyledShape& shape, |
| 279 |
const SkIRect& devClipBounds) { |
| 280 |
static const skgpu::UniqueKey::Domain kDomain = skgpu::UniqueKey::GenerateDomain(); |
| 281 |
|
| 282 |
bool inverseFill = shape.inverseFilled(); |
| 283 |
|
| 284 |
static constexpr int kClipBoundsCnt = sizeof(devClipBounds) / sizeof(uint32_t); |
| 285 |
uint16_t shapeKeyDataCnt = shape.unstyledKeySize(); |
| 286 |
skgpu::UniqueKey::Builder builder(key, kDomain, shapeKeyDataCnt + kClipBoundsCnt, "Path"); |
| 287 |
shape.writeUnstyledKey(&builder[0]); |
| 288 |
// For inverse fills, the tessellation is dependent on clip bounds. |
| 289 |
if (inverseFill) { |
| 290 |
memcpy(&builder[shapeKeyDataCnt], &devClipBounds, sizeof(devClipBounds)); |
| 291 |
} else { |
| 292 |
memset(&builder[shapeKeyDataCnt], 0, sizeof(devClipBounds)); |
| 293 |
} |
| 294 |
|
| 295 |
builder.finish(); |
| 296 |
} |
| 297 |
|
| 298 |
// Triangulate the provided 'shape' in the shape's coordinate space. 'tol' should already |
| 299 |
// have been mapped back from device space. |
| 300 |
static int Triangulate(GrEagerVertexAllocator* allocator, |
| 301 |
const SkMatrix& viewMatrix, |
| 302 |
const GrStyledShape& shape, |
| 303 |
const SkIRect& devClipBounds, |
| 304 |
SkScalar tol, |
| 305 |
bool* isLinear) { |
| 306 |
SkRect clipBounds = SkRect::Make(devClipBounds); |
| 307 |
|
| 308 |
SkMatrix vmi; |
| 309 |
if (!viewMatrix.invert(&vmi)) { |
| 310 |
return 0; |
| 311 |
} |
| 312 |
vmi.mapRect(&clipBounds); |
| 313 |
|
| 314 |
SkASSERT(!shape.style().applies()); |
| 315 |
SkPath path = shape.asPath(); |
| 316 |
|
| 317 |
return GrTriangulator::PathToTriangles(path, tol, clipBounds, allocator, isLinear); |
| 318 |
} |
| 319 |
|
| 320 |
void createNonAAMesh(GrMeshDrawTarget* target) { |
| 321 |
SkASSERT(!fAntiAlias); |
| 322 |
GrResourceProvider* rp = target->resourceProvider(); |
| 323 |
auto threadSafeCache = target->threadSafeCache(); |
| 324 |
|
| 325 |
skgpu::UniqueKey key; |
| 326 |
CreateKey(&key, fShape, fDevClipBounds); |
| 327 |
|
| 328 |
SkScalar tol = GrPathUtils::scaleToleranceToSrc(GrPathUtils::kDefaultTolerance, |
| 329 |
fViewMatrix, fShape.bounds()); |
| 330 |
|
| 331 |
if (!fVertexData) { |
| 332 |
auto [cachedVerts, data] = threadSafeCache->findVertsWithData(key); |
| 333 |
if (cachedVerts && cache_match(data.get(), tol)) { |
| 334 |
fVertexData = std::move(cachedVerts); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
if (fVertexData) { |
| 339 |
if (!fVertexData->gpuBuffer()) { |
| 340 |
sk_sp<GrGpuBuffer> buffer = rp->createBuffer(fVertexData->vertices(), |
| 341 |
fVertexData->size(), |
| 342 |
GrGpuBufferType::kVertex, |
| 343 |
kStatic_GrAccessPattern); |
| 344 |
if (!buffer) { |
| 345 |
return; |
| 346 |
} |
| 347 |
|
| 348 |
// Since we have a direct context and a ref on 'fVertexData' we need not worry |
| 349 |
// about any threading issues in this call. |
| 350 |
fVertexData->setGpuBuffer(std::move(buffer)); |
| 351 |
} |
| 352 |
|
| 353 |
fMesh = CreateMesh(target, fVertexData->refGpuBuffer(), 0, fVertexData->numVertices()); |
| 354 |
return; |
| 355 |
} |
| 356 |
|
| 357 |
bool canMapVB = GrCaps::kNone_MapFlags != target->caps().mapBufferFlags(); |
| 358 |
StaticVertexAllocator allocator(rp, canMapVB); |
| 359 |
|
| 360 |
bool isLinear; |
| 361 |
int vertexCount = Triangulate(&allocator, fViewMatrix, fShape, fDevClipBounds, tol, |
| 362 |
&isLinear); |
| 363 |
if (vertexCount == 0) { |
| 364 |
return; |
| 365 |
} |
| 366 |
|
| 367 |
fVertexData = allocator.detachVertexData(); |
| 368 |
|
| 369 |
key.setCustomData(create_data(vertexCount, isLinear, tol)); |
| 370 |
|
| 371 |
auto [tmpV, tmpD] = threadSafeCache->addVertsWithData(key, fVertexData, is_newer_better); |
| 372 |
if (tmpV != fVertexData) { |
| 373 |
SkASSERT(!tmpV->gpuBuffer()); |
| 374 |
// In this case, although the different triangulation found in the cache is better, |
| 375 |
// we will continue on with the current triangulation since it is already on the gpu. |
| 376 |
} else { |
| 377 |
// This isn't perfect. The current triangulation is in the cache but it may have |
| 378 |
// replaced a pre-existing one. A duplicated listener is unlikely and not that |
| 379 |
// expensive so we just roll with it. |
| 380 |
fShape.addGenIDChangeListener( |
| 381 |
sk_make_sp<UniqueKeyInvalidator>(key, target->contextUniqueID())); |
| 382 |
} |
| 383 |
|
| 384 |
fMesh = CreateMesh(target, fVertexData->refGpuBuffer(), 0, fVertexData->numVertices()); |
| 385 |
} |
| 386 |
|
| 387 |
void createAAMesh(GrMeshDrawTarget* target) { |
| 388 |
SkASSERT(!fVertexData); |
| 389 |
SkASSERT(fAntiAlias); |
| 390 |
SkPath path = this->getPath().makeTransform(fViewMatrix); |
| 391 |
if (path.isEmpty()) { |
| 392 |
return; |
| 393 |
} |
| 394 |
SkRect clipBounds = SkRect::Make(fDevClipBounds); |
| 395 |
SkScalar tol = GrPathUtils::kDefaultTolerance; |
| 396 |
sk_sp<const GrBuffer> vertexBuffer; |
| 397 |
int firstVertex; |
| 398 |
GrEagerDynamicVertexAllocator allocator(target, &vertexBuffer, &firstVertex); |
| 399 |
int vertexCount = GrAATriangulator::PathToAATriangles(path, tol, clipBounds, &allocator); |
| 400 |
if (vertexCount == 0) { |
| 401 |
return; |
| 402 |
} |
| 403 |
fMesh = CreateMesh(target, std::move(vertexBuffer), firstVertex, vertexCount); |
| 404 |
} |
| 405 |
|
| 406 |
GrProgramInfo* programInfo() override { return fProgramInfo; } |
| 407 |
|
| 408 |
void onCreateProgramInfo(const GrCaps* caps, |
| 409 |
SkArenaAlloc* arena, |
| 410 |
const GrSurfaceProxyView& writeView, |
| 411 |
bool usesMSAASurface, |
| 412 |
GrAppliedClip&& appliedClip, |
| 413 |
const GrDstProxyView& dstProxyView, |
| 414 |
GrXferBarrierFlags renderPassXferBarriers, |
| 415 |
GrLoadOp colorLoadOp) override { |
| 416 |
GrGeometryProcessor* gp; |
| 417 |
{ |
| 418 |
using namespace GrDefaultGeoProcFactory; |
| 419 |
|
| 420 |
Color color(fColor); |
| 421 |
LocalCoords::Type localCoordsType = fHelper.usesLocalCoords() |
| 422 |
? LocalCoords::kUsePosition_Type |
| 423 |
: LocalCoords::kUnused_Type; |
| 424 |
Coverage::Type coverageType; |
| 425 |
if (fAntiAlias) { |
| 426 |
if (fHelper.compatibleWithCoverageAsAlpha()) { |
| 427 |
coverageType = Coverage::kAttributeTweakAlpha_Type; |
| 428 |
} else { |
| 429 |
coverageType = Coverage::kAttribute_Type; |
| 430 |
} |
| 431 |
} else { |
| 432 |
coverageType = Coverage::kSolid_Type; |
| 433 |
} |
| 434 |
if (fAntiAlias) { |
| 435 |
gp = GrDefaultGeoProcFactory::MakeForDeviceSpace(arena, color, coverageType, |
| 436 |
localCoordsType, fViewMatrix); |
| 437 |
} else { |
| 438 |
gp = GrDefaultGeoProcFactory::Make(arena, color, coverageType, localCoordsType, |
| 439 |
fViewMatrix); |
| 440 |
} |
| 441 |
} |
| 442 |
if (!gp) { |
| 443 |
return; |
| 444 |
} |
| 445 |
|
| 446 |
#ifdef SK_DEBUG |
| 447 |
auto vertexStride = sizeof(SkPoint); |
| 448 |
if (fAntiAlias) { |
| 449 |
vertexStride += sizeof(float); |
| 450 |
} |
| 451 |
SkASSERT(vertexStride == gp->vertexStride()); |
| 452 |
#endif |
| 453 |
|
| 454 |
GrPrimitiveType primitiveType = TRIANGULATOR_WIREFRAME ? GrPrimitiveType::kLines |
| 455 |
: GrPrimitiveType::kTriangles; |
| 456 |
|
| 457 |
fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView, |
| 458 |
usesMSAASurface, |
| 459 |
std::move(appliedClip), dstProxyView, |
| 460 |
gp, primitiveType, |
| 461 |
renderPassXferBarriers, colorLoadOp); |
| 462 |
} |
| 463 |
|
| 464 |
void onPrePrepareDraws(GrRecordingContext* rContext, |
| 465 |
const GrSurfaceProxyView& writeView, |
| 466 |
GrAppliedClip* clip, |
| 467 |
const GrDstProxyView& dstProxyView, |
| 468 |
GrXferBarrierFlags renderPassXferBarriers, |
| 469 |
GrLoadOp colorLoadOp) override { |
| 470 |
TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 471 |
|
| 472 |
INHERITED::onPrePrepareDraws(rContext, writeView, clip, dstProxyView, |
| 473 |
renderPassXferBarriers, colorLoadOp); |
| 474 |
|
| 475 |
if (fAntiAlias) { |
| 476 |
// TODO: pull the triangulation work forward to the recording thread for the AA case |
| 477 |
// too. |
| 478 |
return; |
| 479 |
} |
| 480 |
|
| 481 |
auto threadSafeViewCache = rContext->priv().threadSafeCache(); |
| 482 |
|
| 483 |
skgpu::UniqueKey key; |
| 484 |
CreateKey(&key, fShape, fDevClipBounds); |
| 485 |
|
| 486 |
SkScalar tol = GrPathUtils::scaleToleranceToSrc(GrPathUtils::kDefaultTolerance, |
| 487 |
fViewMatrix, fShape.bounds()); |
| 488 |
|
| 489 |
auto [cachedVerts, data] = threadSafeViewCache->findVertsWithData(key); |
| 490 |
if (cachedVerts && cache_match(data.get(), tol)) { |
| 491 |
fVertexData = std::move(cachedVerts); |
| 492 |
return; |
| 493 |
} |
| 494 |
|
| 495 |
GrCpuVertexAllocator allocator; |
| 496 |
|
| 497 |
bool isLinear; |
| 498 |
int vertexCount = Triangulate(&allocator, fViewMatrix, fShape, fDevClipBounds, tol, |
| 499 |
&isLinear); |
| 500 |
if (vertexCount == 0) { |
| 501 |
return; |
| 502 |
} |
| 503 |
|
| 504 |
fVertexData = allocator.detachVertexData(); |
| 505 |
|
| 506 |
key.setCustomData(create_data(vertexCount, isLinear, tol)); |
| 507 |
|
| 508 |
// If some other thread created and cached its own triangulation, the 'is_newer_better' |
| 509 |
// predicate will replace the version in the cache if 'fVertexData' is a more accurate |
| 510 |
// triangulation. This will leave some other recording threads using a poorer triangulation |
| 511 |
// but will result in a version with greater applicability being in the cache. |
| 512 |
auto [tmpV, tmpD] = threadSafeViewCache->addVertsWithData(key, fVertexData, |
| 513 |
is_newer_better); |
| 514 |
if (tmpV != fVertexData) { |
| 515 |
// Someone beat us to creating the triangulation (and it is better than ours) so |
| 516 |
// just go ahead and use it. |
| 517 |
SkASSERT(cache_match(tmpD.get(), tol)); |
| 518 |
fVertexData = std::move(tmpV); |
| 519 |
} else { |
| 520 |
// This isn't perfect. The current triangulation is in the cache but it may have |
| 521 |
// replaced a pre-existing one. A duplicated listener is unlikely and not that |
| 522 |
// expensive so we just roll with it. |
| 523 |
fShape.addGenIDChangeListener( |
| 524 |
sk_make_sp<UniqueKeyInvalidator>(key, rContext->priv().contextID())); |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
void onPrepareDraws(GrMeshDrawTarget* target) override { |
| 529 |
if (fAntiAlias) { |
| 530 |
this->createAAMesh(target); |
| 531 |
} else { |
| 532 |
this->createNonAAMesh(target); |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
static GrSimpleMesh* CreateMesh(GrMeshDrawTarget* target, |
| 537 |
sk_sp<const GrBuffer> vb, |
| 538 |
int firstVertex, |
| 539 |
int count) { |
| 540 |
auto mesh = target->allocMesh(); |
| 541 |
mesh->set(std::move(vb), count, firstVertex); |
| 542 |
return mesh; |
| 543 |
} |
| 544 |
|
| 545 |
void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
| 546 |
if (!fProgramInfo) { |
| 547 |
this->createProgramInfo(flushState); |
| 548 |
} |
| 549 |
|
| 550 |
if (!fProgramInfo || !fMesh) { |
| 551 |
return; |
| 552 |
} |
| 553 |
|
| 554 |
flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds); |
| 555 |
flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline()); |
| 556 |
flushState->drawMesh(*fMesh); |
| 557 |
} |
| 558 |
|
| 559 |
#if defined(GPU_TEST_UTILS) |
| 560 |
SkString onDumpInfo() const override { |
| 561 |
return SkStringPrintf("Color 0x%08x, aa: %d\n%s", |
| 562 |
fColor.toBytes_RGBA(), fAntiAlias, fHelper.dumpInfo().c_str()); |
| 563 |
} |
| 564 |
#endif |
| 565 |
|
| 566 |
Helper fHelper; |
| 567 |
SkPMColor4f fColor; |
| 568 |
GrStyledShape fShape; |
| 569 |
SkMatrix fViewMatrix; |
| 570 |
SkIRect fDevClipBounds; |
| 571 |
bool fAntiAlias; |
| 572 |
|
| 573 |
GrSimpleMesh* fMesh = nullptr; |
| 574 |
GrProgramInfo* fProgramInfo = nullptr; |
| 575 |
|
| 576 |
sk_sp<GrThreadSafeCache::VertexData> fVertexData; |
| 577 |
|
| 578 |
using INHERITED = GrMeshDrawOp; |
| 579 |
}; |
| 580 |
|
| 581 |
} // anonymous namespace |
| 582 |
|
| 583 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 584 |
|
| 585 |
#if defined(GPU_TEST_UTILS) |
| 586 |
|
| 587 |
GR_DRAW_OP_TEST_DEFINE(TriangulatingPathOp) { |
| 588 |
SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
| 589 |
const SkPath& path = GrTest::TestPath(random); |
| 590 |
SkIRect devClipBounds = SkIRect::MakeLTRB( |
| 591 |
random->nextU(), random->nextU(), random->nextU(), random->nextU()); |
| 592 |
devClipBounds.sort(); |
| 593 |
static constexpr GrAAType kAATypes[] = {GrAAType::kNone, GrAAType::kMSAA, GrAAType::kCoverage}; |
| 594 |
GrAAType aaType; |
| 595 |
do { |
| 596 |
aaType = kAATypes[random->nextULessThan(std::size(kAATypes))]; |
| 597 |
} while(GrAAType::kMSAA == aaType && numSamples <= 1); |
| 598 |
GrStyle style; |
| 599 |
do { |
| 600 |
GrTest::TestStyle(random, &style); |
| 601 |
} while (!style.isSimpleFill()); |
| 602 |
GrStyledShape shape(path, style); |
| 603 |
return TriangulatingPathOp::Make(context, std::move(paint), shape, viewMatrix, devClipBounds, |
| 604 |
aaType, GrGetRandomStencil(random, context)); |
| 605 |
} |
| 606 |
|
| 607 |
#endif |
| 608 |
|
| 609 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 610 |
|
| 611 |
namespace skgpu::ganesh { |
| 612 |
|
| 613 |
TriangulatingPathRenderer::TriangulatingPathRenderer() |
| 614 |
: fMaxVerbCount(GR_AA_TESSELLATOR_MAX_VERB_COUNT) { |
| 615 |
} |
| 616 |
|
| 617 |
PathRenderer::CanDrawPath TriangulatingPathRenderer::onCanDrawPath( |
| 618 |
const CanDrawPathArgs& args) const { |
| 619 |
// Don't use this path renderer with dynamic MSAA. DMSAA tries to not rely on caching. |
| 620 |
if (args.fSurfaceProps->flags() & SkSurfaceProps::kDynamicMSAA_Flag) { |
| 621 |
return CanDrawPath::kNo; |
| 622 |
} |
| 623 |
// This path renderer can draw fill styles, and can do screenspace antialiasing via a |
| 624 |
// one-pixel coverage ramp. It can do convex and concave paths, but we'll leave the convex |
| 625 |
// ones to simpler algorithms. We pass on paths that have styles, though they may come back |
| 626 |
// around after applying the styling information to the geometry to create a filled path. |
| 627 |
if (!args.fShape->style().isSimpleFill() || args.fShape->knownToBeConvex()) { |
| 628 |
return CanDrawPath::kNo; |
| 629 |
} |
| 630 |
|
| 631 |
SkPath path = args.fShape->asPath(); |
| 632 |
int verbCount = path.countVerbs(); |
| 633 |
// Don't use this path renderer if we exceed the max verb count. |
| 634 |
if (verbCount > kMaxGPUPathRendererVerbs) { |
| 635 |
return CanDrawPath::kNo; |
| 636 |
} |
| 637 |
|
| 638 |
switch (args.fAAType) { |
| 639 |
case GrAAType::kNone: |
| 640 |
case GrAAType::kMSAA: |
| 641 |
// Prefer MSAA, if any antialiasing. In the non-analytic-AA case, We skip paths that |
| 642 |
// don't have a key since the real advantage of this path renderer comes from caching |
| 643 |
// the tessellated geometry. |
| 644 |
if (!args.fShape->hasUnstyledKey()) { |
| 645 |
return CanDrawPath::kNo; |
| 646 |
} |
| 647 |
break; |
| 648 |
case GrAAType::kCoverage: |
| 649 |
// Use analytic AA if we don't have MSAA. In this case, we do not cache, so we accept |
| 650 |
// paths without keys. |
| 651 |
if (verbCount > fMaxVerbCount) { |
| 652 |
return CanDrawPath::kNo; |
| 653 |
} |
| 654 |
break; |
| 655 |
} |
| 656 |
return CanDrawPath::kYes; |
| 657 |
} |
| 658 |
|
| 659 |
bool TriangulatingPathRenderer::onDrawPath(const DrawPathArgs& args) { |
| 660 |
GR_AUDIT_TRAIL_AUTO_FRAME(args.fContext->priv().auditTrail(), |
| 661 |
"GrTriangulatingPathRenderer::onDrawPath"); |
| 662 |
|
| 663 |
GrOp::Owner op = TriangulatingPathOp::Make( |
| 664 |
args.fContext, std::move(args.fPaint), *args.fShape, *args.fViewMatrix, |
| 665 |
*args.fClipConservativeBounds, args.fAAType, args.fUserStencilSettings); |
| 666 |
args.fSurfaceDrawContext->addDrawOp(args.fClip, std::move(op)); |
| 667 |
return true; |
| 668 |
} |
| 669 |
|
| 670 |
} // namespace skgpu::ganesh |
| 671 |
|
| 672 |
#endif // SK_ENABLE_OPTIMIZE_SIZE |