| 1 |
/* |
| 2 |
* Copyright 2011 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/DefaultPathRenderer.h" |
| 8 |
|
| 9 |
#include "include/core/SkMatrix.h" |
| 10 |
#include "include/core/SkPath.h" |
| 11 |
#include "include/core/SkPathTypes.h" |
| 12 |
#include "include/core/SkRect.h" |
| 13 |
#include "include/core/SkRefCnt.h" |
| 14 |
#include "include/core/SkScalar.h" |
| 15 |
#include "include/core/SkString.h" |
| 16 |
#include "include/core/SkStrokeRec.h" |
| 17 |
#include "include/gpu/ganesh/GrRecordingContext.h" |
| 18 |
#include "include/private/base/SkAssert.h" |
| 19 |
#include "include/private/base/SkDebug.h" |
| 20 |
#include "include/private/base/SkPoint_impl.h" |
| 21 |
#include "include/private/base/SkTArray.h" |
| 22 |
#include "include/private/base/SkTDArray.h" |
| 23 |
#include "include/private/base/SkTo.h" |
| 24 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 25 |
#include "src/core/SkColorData.h" |
| 26 |
#include "src/core/SkGeometry.h" |
| 27 |
#include "src/core/SkMatrixPriv.h" |
| 28 |
#include "src/gpu/ganesh/GrAppliedClip.h" |
| 29 |
#include "src/gpu/ganesh/GrAuditTrail.h" |
| 30 |
#include "src/gpu/ganesh/GrBuffer.h" |
| 31 |
#include "src/gpu/ganesh/GrCaps.h" |
| 32 |
#include "src/gpu/ganesh/GrClip.h" |
| 33 |
#include "src/gpu/ganesh/GrDefaultGeoProcFactory.h" |
| 34 |
#include "src/gpu/ganesh/GrGeometryProcessor.h" |
| 35 |
#include "src/gpu/ganesh/GrMeshDrawTarget.h" |
| 36 |
#include "src/gpu/ganesh/GrOpFlushState.h" |
| 37 |
#include "src/gpu/ganesh/GrPaint.h" |
| 38 |
#include "src/gpu/ganesh/GrProcessorAnalysis.h" |
| 39 |
#include "src/gpu/ganesh/GrProcessorSet.h" |
| 40 |
#include "src/gpu/ganesh/GrProgramInfo.h" |
| 41 |
#include "src/gpu/ganesh/GrRecordingContextPriv.h" |
| 42 |
#include "src/gpu/ganesh/GrRenderTargetProxy.h" |
| 43 |
#include "src/gpu/ganesh/GrSimpleMesh.h" |
| 44 |
#include "src/gpu/ganesh/GrStyle.h" |
| 45 |
#include "src/gpu/ganesh/GrTestUtils.h" |
| 46 |
#include "src/gpu/ganesh/GrUserStencilSettings.h" |
| 47 |
#include "src/gpu/ganesh/GrUtil.h" |
| 48 |
#include "src/gpu/ganesh/SurfaceDrawContext.h" |
| 49 |
#include "src/gpu/ganesh/effects/GrDisableColorXP.h" |
| 50 |
#include "src/gpu/ganesh/geometry/GrPathUtils.h" |
| 51 |
#include "src/gpu/ganesh/geometry/GrStyledShape.h" |
| 52 |
#include "src/gpu/ganesh/ops/GrMeshDrawOp.h" |
| 53 |
#include "src/gpu/ganesh/ops/GrOp.h" |
| 54 |
#include "src/gpu/ganesh/ops/GrPathStencilSettings.h" |
| 55 |
#include "src/gpu/ganesh/ops/GrSimpleMeshDrawOpHelperWithStencil.h" |
| 56 |
|
| 57 |
#if defined(GPU_TEST_UTILS) |
| 58 |
#include "src/base/SkRandom.h" |
| 59 |
#include "src/gpu/ganesh/GrDrawOpTest.h" |
| 60 |
#endif |
| 61 |
|
| 62 |
#include <cstddef> |
| 63 |
#include <cstdint> |
| 64 |
#include <utility> |
| 65 |
|
| 66 |
class GrDstProxyView; |
| 67 |
class GrSurfaceProxyView; |
| 68 |
class SkArenaAlloc; |
| 69 |
enum class GrXferBarrierFlags; |
| 70 |
|
| 71 |
using namespace skia_private; |
| 72 |
|
| 73 |
//////////////////////////////////////////////////////////////////////////////// |
| 74 |
// Helpers for drawPath |
| 75 |
|
| 76 |
namespace { |
| 77 |
|
| 78 |
#define STENCIL_OFF 0 // Always disable stencil (even when needed) |
| 79 |
|
| 80 |
inline bool single_pass_shape(const GrStyledShape& shape) { |
| 81 |
#if STENCIL_OFF |
| 82 |
return true; |
| 83 |
#else |
| 84 |
// Inverse fill is always two pass. |
| 85 |
if (shape.inverseFilled()) { |
| 86 |
return false; |
| 87 |
} |
| 88 |
// This path renderer only accepts simple fill paths or stroke paths that are either hairline |
| 89 |
// or have a stroke width small enough to treat as hairline. Hairline paths are always single |
| 90 |
// pass. Filled paths are single pass if they're convex. |
| 91 |
if (shape.style().isSimpleFill()) { |
| 92 |
return shape.knownToBeConvex(); |
| 93 |
} |
| 94 |
return true; |
| 95 |
#endif |
| 96 |
} |
| 97 |
|
| 98 |
class PathGeoBuilder { |
| 99 |
public: |
| 100 |
PathGeoBuilder(GrPrimitiveType primitiveType, |
| 101 |
GrMeshDrawTarget* target, |
| 102 |
SkTDArray<GrSimpleMesh*>* meshes) |
| 103 |
: fPrimitiveType(primitiveType) |
| 104 |
, fTarget(target) |
| 105 |
, fVertexStride(sizeof(SkPoint)) |
| 106 |
, fFirstIndex(0) |
| 107 |
, fIndicesInChunk(0) |
| 108 |
, fIndices(nullptr) |
| 109 |
, fMeshes(meshes) { |
| 110 |
this->allocNewBuffers(); |
| 111 |
} |
| 112 |
|
| 113 |
~PathGeoBuilder() { |
| 114 |
this->createMeshAndPutBackReserve(); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Path verbs |
| 119 |
*/ |
| 120 |
void moveTo(const SkPoint& p) { |
| 121 |
if (!this->ensureSpace(1)) { |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
if (!this->isHairline()) { |
| 126 |
fSubpathIndexStart = this->currentIndex(); |
| 127 |
fSubpathStartPoint = p; |
| 128 |
} |
| 129 |
*(fCurVert++) = p; |
| 130 |
} |
| 131 |
|
| 132 |
void addLine(const SkPoint pts[]) { |
| 133 |
if (!this->ensureSpace(1, this->indexScale(), &pts[0])) { |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
if (this->isIndexed()) { |
| 138 |
uint16_t prevIdx = this->currentIndex() - 1; |
| 139 |
this->appendCountourEdgeIndices(prevIdx); |
| 140 |
} |
| 141 |
*(fCurVert++) = pts[1]; |
| 142 |
} |
| 143 |
|
| 144 |
void addQuad(const SkPoint pts[], SkScalar srcSpaceTolSqd, SkScalar srcSpaceTol) { |
| 145 |
if (!this->ensureSpace(GrPathUtils::kMaxPointsPerCurve, |
| 146 |
GrPathUtils::kMaxPointsPerCurve * this->indexScale(), |
| 147 |
&pts[0])) { |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
// First pt of quad is the pt we ended on in previous step |
| 152 |
uint16_t firstQPtIdx = this->currentIndex() - 1; |
| 153 |
uint16_t numPts = (uint16_t)GrPathUtils::generateQuadraticPoints( |
| 154 |
pts[0], pts[1], pts[2], srcSpaceTolSqd, &fCurVert, |
| 155 |
GrPathUtils::quadraticPointCount(pts, srcSpaceTol)); |
| 156 |
if (this->isIndexed()) { |
| 157 |
for (uint16_t i = 0; i < numPts; ++i) { |
| 158 |
this->appendCountourEdgeIndices(firstQPtIdx + i); |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
void addConic(SkScalar weight, const SkPoint pts[], SkScalar srcSpaceTolSqd, |
| 164 |
SkScalar srcSpaceTol) { |
| 165 |
SkAutoConicToQuads converter; |
| 166 |
const SkPoint* quadPts = converter.computeQuads(pts, weight, srcSpaceTol); |
| 167 |
for (int i = 0; i < converter.countQuads(); ++i) { |
| 168 |
this->addQuad(quadPts + i * 2, srcSpaceTolSqd, srcSpaceTol); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
void addCubic(const SkPoint pts[], SkScalar srcSpaceTolSqd, SkScalar srcSpaceTol) { |
| 173 |
if (!this->ensureSpace(GrPathUtils::kMaxPointsPerCurve, |
| 174 |
GrPathUtils::kMaxPointsPerCurve * this->indexScale(), |
| 175 |
&pts[0])) { |
| 176 |
return; |
| 177 |
} |
| 178 |
|
| 179 |
// First pt of cubic is the pt we ended on in previous step |
| 180 |
uint16_t firstCPtIdx = this->currentIndex() - 1; |
| 181 |
uint16_t numPts = (uint16_t) GrPathUtils::generateCubicPoints( |
| 182 |
pts[0], pts[1], pts[2], pts[3], srcSpaceTolSqd, &fCurVert, |
| 183 |
GrPathUtils::cubicPointCount(pts, srcSpaceTol)); |
| 184 |
if (this->isIndexed()) { |
| 185 |
for (uint16_t i = 0; i < numPts; ++i) { |
| 186 |
this->appendCountourEdgeIndices(firstCPtIdx + i); |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
void addPath(const SkPath& path, SkScalar srcSpaceTol) { |
| 192 |
SkScalar srcSpaceTolSqd = srcSpaceTol * srcSpaceTol; |
| 193 |
|
| 194 |
SkPath::Iter iter(path, false); |
| 195 |
while (auto rec = iter.next()) { |
| 196 |
const SkPoint* pts = rec->fPoints.data(); |
| 197 |
switch (rec->fVerb) { |
| 198 |
case SkPathVerb::kMove: |
| 199 |
this->moveTo(pts[0]); |
| 200 |
break; |
| 201 |
case SkPathVerb::kLine: |
| 202 |
this->addLine(pts); |
| 203 |
break; |
| 204 |
case SkPathVerb::kConic: |
| 205 |
this->addConic(rec->conicWeight(), pts, srcSpaceTolSqd, srcSpaceTol); |
| 206 |
break; |
| 207 |
case SkPathVerb::kQuad: |
| 208 |
this->addQuad(pts, srcSpaceTolSqd, srcSpaceTol); |
| 209 |
break; |
| 210 |
case SkPathVerb::kCubic: |
| 211 |
this->addCubic(pts, srcSpaceTolSqd, srcSpaceTol); |
| 212 |
break; |
| 213 |
case SkPathVerb::kClose: |
| 214 |
break; |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
static bool PathHasMultipleSubpaths(const SkPath& path) { |
| 220 |
bool first = true; |
| 221 |
|
| 222 |
SkPath::Iter iter(path, false); |
| 223 |
while (auto rec = iter.next()) { |
| 224 |
if (SkPathVerb::kMove == rec->fVerb && !first) { |
| 225 |
return true; |
| 226 |
} |
| 227 |
first = false; |
| 228 |
} |
| 229 |
return false; |
| 230 |
} |
| 231 |
|
| 232 |
private: |
| 233 |
/** |
| 234 |
* Derived properties |
| 235 |
* TODO: Cache some of these for better performance, rather than re-computing? |
| 236 |
*/ |
| 237 |
bool isIndexed() const { |
| 238 |
return GrPrimitiveType::kLines == fPrimitiveType || |
| 239 |
GrPrimitiveType::kTriangles == fPrimitiveType; |
| 240 |
} |
| 241 |
bool isHairline() const { |
| 242 |
return GrPrimitiveType::kLines == fPrimitiveType || |
| 243 |
GrPrimitiveType::kLineStrip == fPrimitiveType; |
| 244 |
} |
| 245 |
int indexScale() const { |
| 246 |
switch (fPrimitiveType) { |
| 247 |
case GrPrimitiveType::kLines: |
| 248 |
return 2; |
| 249 |
case GrPrimitiveType::kTriangles: |
| 250 |
return 3; |
| 251 |
default: |
| 252 |
return 0; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
uint16_t currentIndex() const { return fCurVert - fVertices; } |
| 257 |
|
| 258 |
// Allocate vertex and (possibly) index buffers |
| 259 |
void allocNewBuffers() { |
| 260 |
SkASSERT(fValid); |
| 261 |
|
| 262 |
// Ensure that we always get enough verts for a worst-case quad/cubic, plus leftover points |
| 263 |
// from previous mesh piece (up to two verts to continue fanning). If we can't get that |
| 264 |
// many, ask for a much larger number. This needs to be fairly big to handle quads/cubics, |
| 265 |
// which have a worst-case of 1k points. |
| 266 |
static const int kMinVerticesPerChunk = GrPathUtils::kMaxPointsPerCurve + 2; |
| 267 |
static const int kFallbackVerticesPerChunk = 16384; |
| 268 |
|
| 269 |
fVertices = static_cast<SkPoint*>(fTarget->makeVertexSpaceAtLeast(fVertexStride, |
| 270 |
kMinVerticesPerChunk, |
| 271 |
kFallbackVerticesPerChunk, |
| 272 |
&fVertexBuffer, |
| 273 |
&fFirstVertex, |
| 274 |
&fVerticesInChunk)); |
| 275 |
if (!fVertices) { |
| 276 |
SkDebugf("WARNING: Failed to allocate vertex buffer for DefaultPathRenderer.\n"); |
| 277 |
fCurVert = nullptr; |
| 278 |
fCurIdx = fIndices = nullptr; |
| 279 |
fSubpathIndexStart = 0; |
| 280 |
fValid = false; |
| 281 |
return; |
| 282 |
} |
| 283 |
|
| 284 |
if (this->isIndexed()) { |
| 285 |
// Similar to above: Ensure we get enough indices for one worst-case quad/cubic. |
| 286 |
// No extra indices are needed for stitching, though. If we can't get that many, ask |
| 287 |
// for enough to match our large vertex request. |
| 288 |
const int kMinIndicesPerChunk = GrPathUtils::kMaxPointsPerCurve * this->indexScale(); |
| 289 |
const int kFallbackIndicesPerChunk = kFallbackVerticesPerChunk * this->indexScale(); |
| 290 |
|
| 291 |
fIndices = fTarget->makeIndexSpaceAtLeast(kMinIndicesPerChunk, kFallbackIndicesPerChunk, |
| 292 |
&fIndexBuffer, &fFirstIndex, |
| 293 |
&fIndicesInChunk); |
| 294 |
if (!fIndices) { |
| 295 |
SkDebugf("WARNING: Failed to allocate index buffer for DefaultPathRenderer.\n"); |
| 296 |
fVertices = nullptr; |
| 297 |
fValid = false; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
fCurVert = fVertices; |
| 302 |
fCurIdx = fIndices; |
| 303 |
fSubpathIndexStart = 0; |
| 304 |
} |
| 305 |
|
| 306 |
void appendCountourEdgeIndices(uint16_t edgeV0Idx) { |
| 307 |
SkASSERT(fCurIdx); |
| 308 |
|
| 309 |
// When drawing lines we're appending line segments along the countour. When applying the |
| 310 |
// other fill rules we're drawing triangle fans around the start of the current (sub)path. |
| 311 |
if (!this->isHairline()) { |
| 312 |
*(fCurIdx++) = fSubpathIndexStart; |
| 313 |
} |
| 314 |
*(fCurIdx++) = edgeV0Idx; |
| 315 |
*(fCurIdx++) = edgeV0Idx + 1; |
| 316 |
} |
| 317 |
|
| 318 |
// Emits a single draw with all accumulated vertex/index data |
| 319 |
void createMeshAndPutBackReserve() { |
| 320 |
if (!fValid) { |
| 321 |
return; |
| 322 |
} |
| 323 |
|
| 324 |
int vertexCount = fCurVert - fVertices; |
| 325 |
int indexCount = fCurIdx - fIndices; |
| 326 |
SkASSERT(vertexCount <= fVerticesInChunk); |
| 327 |
SkASSERT(indexCount <= fIndicesInChunk); |
| 328 |
|
| 329 |
GrSimpleMesh* mesh = nullptr; |
| 330 |
if (this->isIndexed() ? SkToBool(indexCount) : SkToBool(vertexCount)) { |
| 331 |
mesh = fTarget->allocMesh(); |
| 332 |
if (!this->isIndexed()) { |
| 333 |
mesh->set(std::move(fVertexBuffer), vertexCount, fFirstVertex); |
| 334 |
} else { |
| 335 |
mesh->setIndexed(std::move(fIndexBuffer), indexCount, fFirstIndex, 0, |
| 336 |
vertexCount - 1, GrPrimitiveRestart::kNo, std::move(fVertexBuffer), |
| 337 |
fFirstVertex); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
fTarget->putBackIndices((size_t)(fIndicesInChunk - indexCount)); |
| 342 |
fTarget->putBackVertices((size_t)(fVerticesInChunk - vertexCount), fVertexStride); |
| 343 |
|
| 344 |
if (mesh) { |
| 345 |
fMeshes->push_back(mesh); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
bool ensureSpace(int vertsNeeded, int indicesNeeded = 0, const SkPoint* lastPoint = nullptr) { |
| 350 |
if (!fValid) { |
| 351 |
return false; |
| 352 |
} |
| 353 |
|
| 354 |
if (fCurVert + vertsNeeded > fVertices + fVerticesInChunk || |
| 355 |
fCurIdx + indicesNeeded > fIndices + fIndicesInChunk) { |
| 356 |
// We are about to run out of space (possibly) |
| 357 |
|
| 358 |
#ifdef SK_DEBUG |
| 359 |
// To maintain continuity, we need to remember one or two points from the current mesh. |
| 360 |
// Lines only need the last point, fills need the first point from the current contour. |
| 361 |
// We always grab both here, and append the ones we need at the end of this process. |
| 362 |
SkASSERT(fSubpathIndexStart < fVerticesInChunk); |
| 363 |
// This assert is reading from the gpu buffer fVertices and will be slow, but for debug |
| 364 |
// that is okay. |
| 365 |
if (!this->isHairline()) { |
| 366 |
SkASSERT(fSubpathStartPoint == fVertices[fSubpathIndexStart]); |
| 367 |
} |
| 368 |
if (lastPoint) { |
| 369 |
SkASSERT(*(fCurVert - 1) == *lastPoint); |
| 370 |
} |
| 371 |
#endif |
| 372 |
|
| 373 |
// Draw the mesh we've accumulated, and put back any unused space |
| 374 |
this->createMeshAndPutBackReserve(); |
| 375 |
|
| 376 |
// Get new buffers |
| 377 |
this->allocNewBuffers(); |
| 378 |
if (!fValid) { |
| 379 |
return false; |
| 380 |
} |
| 381 |
|
| 382 |
// On moves we don't need to copy over any points to the new buffer and we pass in a |
| 383 |
// null lastPoint. |
| 384 |
if (lastPoint) { |
| 385 |
// Append copies of the points we saved so the two meshes will weld properly |
| 386 |
if (!this->isHairline()) { |
| 387 |
*(fCurVert++) = fSubpathStartPoint; |
| 388 |
} |
| 389 |
*(fCurVert++) = *lastPoint; |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
return true; |
| 394 |
} |
| 395 |
|
| 396 |
GrPrimitiveType fPrimitiveType; |
| 397 |
GrMeshDrawTarget* fTarget; |
| 398 |
size_t fVertexStride; |
| 399 |
|
| 400 |
sk_sp<const GrBuffer> fVertexBuffer; |
| 401 |
int fFirstVertex; |
| 402 |
int fVerticesInChunk; |
| 403 |
SkPoint* fVertices; |
| 404 |
SkPoint* fCurVert; |
| 405 |
|
| 406 |
sk_sp<const GrBuffer> fIndexBuffer; |
| 407 |
int fFirstIndex; |
| 408 |
int fIndicesInChunk; |
| 409 |
uint16_t* fIndices; |
| 410 |
uint16_t* fCurIdx; |
| 411 |
uint16_t fSubpathIndexStart; |
| 412 |
SkPoint fSubpathStartPoint; |
| 413 |
|
| 414 |
bool fValid = true; |
| 415 |
SkTDArray<GrSimpleMesh*>* fMeshes; |
| 416 |
}; |
| 417 |
|
| 418 |
class DefaultPathOp final : public GrMeshDrawOp { |
| 419 |
private: |
| 420 |
using Helper = GrSimpleMeshDrawOpHelperWithStencil; |
| 421 |
|
| 422 |
public: |
| 423 |
DEFINE_OP_CLASS_ID |
| 424 |
|
| 425 |
static GrOp::Owner Make(GrRecordingContext* context, |
| 426 |
GrPaint&& paint, |
| 427 |
const SkPath& path, |
| 428 |
SkScalar tolerance, |
| 429 |
uint8_t coverage, |
| 430 |
const SkMatrix& viewMatrix, |
| 431 |
bool isHairline, |
| 432 |
GrAAType aaType, |
| 433 |
const SkRect& devBounds, |
| 434 |
const GrUserStencilSettings* stencilSettings) { |
| 435 |
return Helper::FactoryHelper<DefaultPathOp>(context, std::move(paint), path, tolerance, |
| 436 |
coverage, viewMatrix, isHairline, aaType, |
| 437 |
devBounds, stencilSettings); |
| 438 |
} |
| 439 |
|
| 440 |
const char* name() const override { return "DefaultPathOp"; } |
| 441 |
|
| 442 |
void visitProxies(const GrVisitProxyFunc& func) const override { |
| 443 |
if (fProgramInfo) { |
| 444 |
fProgramInfo->visitFPProxies(func); |
| 445 |
} else { |
| 446 |
fHelper.visitProxies(func); |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
DefaultPathOp(GrProcessorSet* processorSet, const SkPMColor4f& color, const SkPath& path, |
| 451 |
SkScalar tolerance, uint8_t coverage, const SkMatrix& viewMatrix, bool isHairline, |
| 452 |
GrAAType aaType, const SkRect& devBounds, |
| 453 |
const GrUserStencilSettings* stencilSettings) |
| 454 |
: INHERITED(ClassID()) |
| 455 |
, fHelper(processorSet, aaType, stencilSettings) |
| 456 |
, fColor(color) |
| 457 |
, fCoverage(coverage) |
| 458 |
, fViewMatrix(viewMatrix) |
| 459 |
, fIsHairline(isHairline) { |
| 460 |
fPaths.emplace_back(PathData{path, tolerance}); |
| 461 |
|
| 462 |
HasAABloat aaBloat = (aaType == GrAAType::kNone) ? HasAABloat ::kNo : HasAABloat::kYes; |
| 463 |
this->setBounds(devBounds, aaBloat, |
| 464 |
isHairline ? IsHairline::kYes : IsHairline::kNo); |
| 465 |
} |
| 466 |
|
| 467 |
FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 468 |
|
| 469 |
GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip, |
| 470 |
GrClampType clampType) override { |
| 471 |
GrProcessorAnalysisCoverage gpCoverage = |
| 472 |
this->coverage() == 0xFF ? GrProcessorAnalysisCoverage::kNone |
| 473 |
: GrProcessorAnalysisCoverage::kSingleChannel; |
| 474 |
// This Op uses uniform (not vertex) color, so doesn't need to track wide color. |
| 475 |
return fHelper.finalizeProcessors(caps, clip, clampType, gpCoverage, &fColor, nullptr); |
| 476 |
} |
| 477 |
|
| 478 |
private: |
| 479 |
GrPrimitiveType primType() const { |
| 480 |
if (this->isHairline()) { |
| 481 |
int instanceCount = fPaths.size(); |
| 482 |
|
| 483 |
// We avoid indices when we have a single hairline contour. |
| 484 |
bool isIndexed = instanceCount > 1 || |
| 485 |
PathGeoBuilder::PathHasMultipleSubpaths(fPaths[0].fPath); |
| 486 |
|
| 487 |
return isIndexed ? GrPrimitiveType::kLines : GrPrimitiveType::kLineStrip; |
| 488 |
} |
| 489 |
|
| 490 |
return GrPrimitiveType::kTriangles; |
| 491 |
} |
| 492 |
|
| 493 |
GrProgramInfo* programInfo() override { return fProgramInfo; } |
| 494 |
|
| 495 |
void onCreateProgramInfo(const GrCaps* caps, |
| 496 |
SkArenaAlloc* arena, |
| 497 |
const GrSurfaceProxyView& writeView, |
| 498 |
bool usesMSAASurface, |
| 499 |
GrAppliedClip&& appliedClip, |
| 500 |
const GrDstProxyView& dstProxyView, |
| 501 |
GrXferBarrierFlags renderPassXferBarriers, |
| 502 |
GrLoadOp colorLoadOp) override { |
| 503 |
GrGeometryProcessor* gp; |
| 504 |
{ |
| 505 |
using namespace GrDefaultGeoProcFactory; |
| 506 |
Color color(this->color()); |
| 507 |
Coverage coverage(this->coverage()); |
| 508 |
LocalCoords localCoords(fHelper.usesLocalCoords() ? LocalCoords::kUsePosition_Type |
| 509 |
: LocalCoords::kUnused_Type); |
| 510 |
gp = GrDefaultGeoProcFactory::Make(arena, |
| 511 |
color, |
| 512 |
coverage, |
| 513 |
localCoords, |
| 514 |
this->viewMatrix()); |
| 515 |
} |
| 516 |
|
| 517 |
SkASSERT(gp->vertexStride() == sizeof(SkPoint)); |
| 518 |
|
| 519 |
fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView, |
| 520 |
usesMSAASurface, |
| 521 |
std::move(appliedClip), dstProxyView, |
| 522 |
gp, this->primType(), |
| 523 |
renderPassXferBarriers, colorLoadOp); |
| 524 |
|
| 525 |
} |
| 526 |
|
| 527 |
void onPrepareDraws(GrMeshDrawTarget* target) override { |
| 528 |
PathGeoBuilder pathGeoBuilder(this->primType(), target, &fMeshes); |
| 529 |
|
| 530 |
// fill buffers |
| 531 |
for (int i = 0; i < fPaths.size(); i++) { |
| 532 |
const PathData& args = fPaths[i]; |
| 533 |
pathGeoBuilder.addPath(args.fPath, args.fTolerance); |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
| 538 |
if (!fProgramInfo) { |
| 539 |
this->createProgramInfo(flushState); |
| 540 |
} |
| 541 |
|
| 542 |
if (!fProgramInfo || fMeshes.empty()) { |
| 543 |
return; |
| 544 |
} |
| 545 |
|
| 546 |
flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds); |
| 547 |
flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline()); |
| 548 |
for (int i = 0; i < fMeshes.size(); ++i) { |
| 549 |
flushState->drawMesh(*fMeshes[i]); |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override { |
| 554 |
DefaultPathOp* that = t->cast<DefaultPathOp>(); |
| 555 |
if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
| 556 |
return CombineResult::kCannotCombine; |
| 557 |
} |
| 558 |
|
| 559 |
if (this->color() != that->color()) { |
| 560 |
return CombineResult::kCannotCombine; |
| 561 |
} |
| 562 |
|
| 563 |
if (this->coverage() != that->coverage()) { |
| 564 |
return CombineResult::kCannotCombine; |
| 565 |
} |
| 566 |
|
| 567 |
if (!SkMatrixPriv::CheapEqual(this->viewMatrix(), that->viewMatrix())) { |
| 568 |
return CombineResult::kCannotCombine; |
| 569 |
} |
| 570 |
|
| 571 |
if (this->isHairline() != that->isHairline()) { |
| 572 |
return CombineResult::kCannotCombine; |
| 573 |
} |
| 574 |
|
| 575 |
fPaths.push_back_n(that->fPaths.size(), that->fPaths.begin()); |
| 576 |
return CombineResult::kMerged; |
| 577 |
} |
| 578 |
|
| 579 |
#if defined(GPU_TEST_UTILS) |
| 580 |
SkString onDumpInfo() const override { |
| 581 |
SkString string = SkStringPrintf("Color: 0x%08x Count: %d\n", |
| 582 |
fColor.toBytes_RGBA(), fPaths.size()); |
| 583 |
for (const auto& path : fPaths) { |
| 584 |
string.appendf("Tolerance: %.2f\n", path.fTolerance); |
| 585 |
} |
| 586 |
string += fHelper.dumpInfo(); |
| 587 |
return string; |
| 588 |
} |
| 589 |
#endif |
| 590 |
|
| 591 |
const SkPMColor4f& color() const { return fColor; } |
| 592 |
uint8_t coverage() const { return fCoverage; } |
| 593 |
const SkMatrix& viewMatrix() const { return fViewMatrix; } |
| 594 |
bool isHairline() const { return fIsHairline; } |
| 595 |
|
| 596 |
struct PathData { |
| 597 |
SkPath fPath; |
| 598 |
SkScalar fTolerance; |
| 599 |
}; |
| 600 |
|
| 601 |
STArray<1, PathData, true> fPaths; |
| 602 |
Helper fHelper; |
| 603 |
SkPMColor4f fColor; |
| 604 |
uint8_t fCoverage; |
| 605 |
SkMatrix fViewMatrix; |
| 606 |
bool fIsHairline; |
| 607 |
|
| 608 |
SkTDArray<GrSimpleMesh*> fMeshes; |
| 609 |
GrProgramInfo* fProgramInfo = nullptr; |
| 610 |
|
| 611 |
using INHERITED = GrMeshDrawOp; |
| 612 |
}; |
| 613 |
|
| 614 |
} // anonymous namespace |
| 615 |
|
| 616 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 617 |
|
| 618 |
#if defined(GPU_TEST_UTILS) |
| 619 |
|
| 620 |
GR_DRAW_OP_TEST_DEFINE(DefaultPathOp) { |
| 621 |
SkMatrix viewMatrix = GrTest::TestMatrix(random); |
| 622 |
|
| 623 |
// For now just hairlines because the other types of draws require two ops. |
| 624 |
// TODO we should figure out a way to combine the stencil and cover steps into one op. |
| 625 |
GrStyle style(SkStrokeRec::kHairline_InitStyle); |
| 626 |
const SkPath& path = GrTest::TestPath(random); |
| 627 |
|
| 628 |
// Compute srcSpaceTol |
| 629 |
SkRect bounds = path.getBounds(); |
| 630 |
SkScalar tol = GrPathUtils::kDefaultTolerance; |
| 631 |
SkScalar srcSpaceTol = GrPathUtils::scaleToleranceToSrc(tol, viewMatrix, bounds); |
| 632 |
|
| 633 |
viewMatrix.mapRect(&bounds); |
| 634 |
uint8_t coverage = GrTest::RandomCoverage(random); |
| 635 |
GrAAType aaType = GrAAType::kNone; |
| 636 |
if (numSamples > 1 && random->nextBool()) { |
| 637 |
aaType = GrAAType::kMSAA; |
| 638 |
} |
| 639 |
return DefaultPathOp::Make(context, std::move(paint), path, srcSpaceTol, coverage, viewMatrix, |
| 640 |
true, aaType, bounds, GrGetRandomStencil(random, context)); |
| 641 |
} |
| 642 |
|
| 643 |
#endif |
| 644 |
|
| 645 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 646 |
|
| 647 |
namespace skgpu::ganesh { |
| 648 |
|
| 649 |
bool DefaultPathRenderer::internalDrawPath(skgpu::ganesh::SurfaceDrawContext* sdc, |
| 650 |
GrPaint&& paint, |
| 651 |
GrAAType aaType, |
| 652 |
const GrUserStencilSettings& userStencilSettings, |
| 653 |
const GrClip* clip, |
| 654 |
const SkMatrix& viewMatrix, |
| 655 |
const GrStyledShape& shape, |
| 656 |
bool stencilOnly) { |
| 657 |
auto context = sdc->recordingContext(); |
| 658 |
|
| 659 |
SkASSERT(GrAAType::kCoverage != aaType); |
| 660 |
SkPath path = shape.asPath(); |
| 661 |
|
| 662 |
SkScalar hairlineCoverage; |
| 663 |
uint8_t newCoverage = 0xff; |
| 664 |
bool isHairline = false; |
| 665 |
if (GrIsStrokeHairlineOrEquivalent(shape.style(), viewMatrix, &hairlineCoverage)) { |
| 666 |
newCoverage = SkScalarRoundToInt(hairlineCoverage * 0xff); |
| 667 |
isHairline = true; |
| 668 |
} else { |
| 669 |
SkASSERT(shape.style().isSimpleFill()); |
| 670 |
} |
| 671 |
|
| 672 |
int passCount = 0; |
| 673 |
const GrUserStencilSettings* passes[2]; |
| 674 |
bool reverse = false; |
| 675 |
bool lastPassIsBounds; |
| 676 |
|
| 677 |
if (isHairline) { |
| 678 |
passCount = 1; |
| 679 |
if (stencilOnly) { |
| 680 |
passes[0] = &gDirectToStencil; |
| 681 |
} else { |
| 682 |
passes[0] = &userStencilSettings; |
| 683 |
} |
| 684 |
lastPassIsBounds = false; |
| 685 |
} else { |
| 686 |
if (single_pass_shape(shape)) { |
| 687 |
passCount = 1; |
| 688 |
if (stencilOnly) { |
| 689 |
passes[0] = &gDirectToStencil; |
| 690 |
} else { |
| 691 |
passes[0] = &userStencilSettings; |
| 692 |
} |
| 693 |
lastPassIsBounds = false; |
| 694 |
} else { |
| 695 |
switch (path.getFillType()) { |
| 696 |
case SkPathFillType::kInverseEvenOdd: |
| 697 |
reverse = true; |
| 698 |
[[fallthrough]]; |
| 699 |
case SkPathFillType::kEvenOdd: |
| 700 |
passes[0] = &gEOStencilPass; |
| 701 |
if (stencilOnly) { |
| 702 |
passCount = 1; |
| 703 |
lastPassIsBounds = false; |
| 704 |
} else { |
| 705 |
passCount = 2; |
| 706 |
lastPassIsBounds = true; |
| 707 |
if (reverse) { |
| 708 |
passes[1] = &gInvEOColorPass; |
| 709 |
} else { |
| 710 |
passes[1] = &gEOColorPass; |
| 711 |
} |
| 712 |
} |
| 713 |
break; |
| 714 |
|
| 715 |
case SkPathFillType::kInverseWinding: |
| 716 |
reverse = true; |
| 717 |
[[fallthrough]]; |
| 718 |
case SkPathFillType::kWinding: |
| 719 |
passes[0] = &gWindStencilPass; |
| 720 |
passCount = 2; |
| 721 |
if (stencilOnly) { |
| 722 |
lastPassIsBounds = false; |
| 723 |
--passCount; |
| 724 |
} else { |
| 725 |
lastPassIsBounds = true; |
| 726 |
if (reverse) { |
| 727 |
passes[passCount-1] = &gInvWindColorPass; |
| 728 |
} else { |
| 729 |
passes[passCount-1] = &gWindColorPass; |
| 730 |
} |
| 731 |
} |
| 732 |
break; |
| 733 |
default: |
| 734 |
SkDEBUGFAIL("Unknown path fFill!"); |
| 735 |
return false; |
| 736 |
} |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
SkScalar tol = GrPathUtils::kDefaultTolerance; |
| 741 |
SkScalar srcSpaceTol = GrPathUtils::scaleToleranceToSrc(tol, viewMatrix, path.getBounds()); |
| 742 |
|
| 743 |
SkRect devBounds; |
| 744 |
GetPathDevBounds(path, sdc->asRenderTargetProxy()->backingStoreDimensions(), |
| 745 |
viewMatrix, &devBounds); |
| 746 |
|
| 747 |
for (int p = 0; p < passCount; ++p) { |
| 748 |
if (lastPassIsBounds && (p == passCount-1)) { |
| 749 |
SkRect bounds; |
| 750 |
SkMatrix localMatrix = SkMatrix::I(); |
| 751 |
if (reverse) { |
| 752 |
// draw over the dev bounds (which will be the whole dst surface for inv fill). |
| 753 |
bounds = devBounds; |
| 754 |
SkMatrix vmi; |
| 755 |
// mapRect through persp matrix may not be correct |
| 756 |
if (!viewMatrix.hasPerspective() && viewMatrix.invert(&vmi)) { |
| 757 |
vmi.mapRect(&bounds); |
| 758 |
} else { |
| 759 |
if (!viewMatrix.invert(&localMatrix)) { |
| 760 |
return false; |
| 761 |
} |
| 762 |
} |
| 763 |
} else { |
| 764 |
bounds = path.getBounds(); |
| 765 |
} |
| 766 |
const SkMatrix& viewM = (reverse && viewMatrix.hasPerspective()) ? SkMatrix::I() : |
| 767 |
viewMatrix; |
| 768 |
// This is a non-coverage aa rect op since we assert aaType != kCoverage at the start |
| 769 |
assert_alive(paint); |
| 770 |
sdc->stencilRect(clip, passes[p], std::move(paint), |
| 771 |
GrAA(aaType == GrAAType::kMSAA), viewM, bounds, |
| 772 |
&localMatrix); |
| 773 |
} else { |
| 774 |
bool stencilPass = stencilOnly || passCount > 1; |
| 775 |
GrOp::Owner op; |
| 776 |
if (stencilPass) { |
| 777 |
GrPaint stencilPaint; |
| 778 |
stencilPaint.setXPFactory(GrDisableColorXPFactory::Get()); |
| 779 |
op = DefaultPathOp::Make(context, std::move(stencilPaint), path, srcSpaceTol, |
| 780 |
newCoverage, viewMatrix, isHairline, aaType, devBounds, |
| 781 |
passes[p]); |
| 782 |
} else { |
| 783 |
assert_alive(paint); |
| 784 |
op = DefaultPathOp::Make(context, std::move(paint), path, srcSpaceTol, newCoverage, |
| 785 |
viewMatrix, isHairline, aaType, devBounds, passes[p]); |
| 786 |
} |
| 787 |
sdc->addDrawOp(clip, std::move(op)); |
| 788 |
} |
| 789 |
} |
| 790 |
return true; |
| 791 |
} |
| 792 |
|
| 793 |
PathRenderer::StencilSupport |
| 794 |
DefaultPathRenderer::onGetStencilSupport(const GrStyledShape& shape) const { |
| 795 |
if (single_pass_shape(shape)) { |
| 796 |
return kNoRestriction_StencilSupport; |
| 797 |
} else { |
| 798 |
return kStencilOnly_StencilSupport; |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
PathRenderer::CanDrawPath DefaultPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
| 803 |
bool isHairline = GrIsStrokeHairlineOrEquivalent( |
| 804 |
args.fShape->style(), *args.fViewMatrix, nullptr); |
| 805 |
// If we aren't a single_pass_shape or hairline, we require stencil buffers. |
| 806 |
if (!(single_pass_shape(*args.fShape) || isHairline) && |
| 807 |
!args.fProxy->canUseStencil(*args.fCaps)) { |
| 808 |
return CanDrawPath::kNo; |
| 809 |
} |
| 810 |
// If antialiasing is required, we only support MSAA. |
| 811 |
if (GrAAType::kNone != args.fAAType && GrAAType::kMSAA != args.fAAType) { |
| 812 |
return CanDrawPath::kNo; |
| 813 |
} |
| 814 |
// This can draw any path with any simple fill style. |
| 815 |
if (!args.fShape->style().isSimpleFill() && !isHairline) { |
| 816 |
return CanDrawPath::kNo; |
| 817 |
} |
| 818 |
// Don't try to draw hairlines with DefaultPathRenderer if avoidLineDraws is true. |
| 819 |
// Alternatively, we could try to implement hairline draws without line primitives in |
| 820 |
// DefaultPathRenderer, but this is simpler. |
| 821 |
if (args.fCaps->avoidLineDraws() && isHairline) { |
| 822 |
return CanDrawPath::kNo; |
| 823 |
} |
| 824 |
|
| 825 |
SkPath path = args.fShape->asPath(); |
| 826 |
int verbCount = path.countVerbs(); |
| 827 |
// Don't use this path renderer if we exceed the max verb count. |
| 828 |
if (verbCount > kMaxGPUPathRendererVerbs) { |
| 829 |
return CanDrawPath::kNo; |
| 830 |
} |
| 831 |
|
| 832 |
// This is the fallback renderer for when a path is too complicated for the others to draw. |
| 833 |
return CanDrawPath::kAsBackup; |
| 834 |
} |
| 835 |
|
| 836 |
bool DefaultPathRenderer::onDrawPath(const DrawPathArgs& args) { |
| 837 |
GR_AUDIT_TRAIL_AUTO_FRAME(args.fContext->priv().auditTrail(), |
| 838 |
"DefaultPathRenderer::onDrawPath"); |
| 839 |
GrAAType aaType = (GrAAType::kNone != args.fAAType) ? GrAAType::kMSAA : GrAAType::kNone; |
| 840 |
|
| 841 |
return this->internalDrawPath( |
| 842 |
args.fSurfaceDrawContext, std::move(args.fPaint), aaType, *args.fUserStencilSettings, |
| 843 |
args.fClip, *args.fViewMatrix, *args.fShape, false); |
| 844 |
} |
| 845 |
|
| 846 |
void DefaultPathRenderer::onStencilPath(const StencilPathArgs& args) { |
| 847 |
GR_AUDIT_TRAIL_AUTO_FRAME(args.fContext->priv().auditTrail(), |
| 848 |
"DefaultPathRenderer::onStencilPath"); |
| 849 |
SkASSERT(!args.fShape->inverseFilled()); |
| 850 |
|
| 851 |
GrPaint paint; |
| 852 |
paint.setXPFactory(GrDisableColorXPFactory::Get()); |
| 853 |
|
| 854 |
auto aaType = (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone; |
| 855 |
|
| 856 |
this->internalDrawPath( |
| 857 |
args.fSurfaceDrawContext, std::move(paint), aaType, GrUserStencilSettings::kUnused, |
| 858 |
args.fClip, *args.fViewMatrix, *args.fShape, true); |
| 859 |
} |
| 860 |
|
| 861 |
} // namespace skgpu::ganesh |