bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/ops/GrDefaultPathRenderer.h" |
Robert Phillips | be9aff2 | 2019-02-15 11:33:22 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkString.h" |
| 11 | #include "include/core/SkStrokeRec.h" |
| 12 | #include "src/core/SkGeometry.h" |
| 13 | #include "src/core/SkTLazy.h" |
| 14 | #include "src/core/SkTraceEvent.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrAuditTrail.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/gpu/GrCaps.h" |
| 17 | #include "src/gpu/GrDefaultGeoProcFactory.h" |
| 18 | #include "src/gpu/GrDrawOpTest.h" |
| 19 | #include "src/gpu/GrFixedClip.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/gpu/GrOpFlushState.h" |
Robert Phillips | 9028bac | 2020-03-10 16:19:27 -0400 | [diff] [blame] | 21 | #include "src/gpu/GrProgramInfo.h" |
Michael Ludwig | aa1b6b3 | 2019-05-29 14:43:13 -0400 | [diff] [blame] | 22 | #include "src/gpu/GrRenderTargetContextPriv.h" |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 23 | #include "src/gpu/GrSimpleMesh.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "src/gpu/GrStyle.h" |
| 25 | #include "src/gpu/GrSurfaceContextPriv.h" |
Michael Ludwig | 663afe5 | 2019-06-03 16:46:19 -0400 | [diff] [blame] | 26 | #include "src/gpu/geometry/GrPathUtils.h" |
| 27 | #include "src/gpu/geometry/GrShape.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | #include "src/gpu/ops/GrMeshDrawOp.h" |
Robert Phillips | 55f681f | 2020-02-28 08:58:15 -0500 | [diff] [blame] | 29 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelperWithStencil.h" |
joshualitt | 7441782 | 2015-08-07 11:42:16 -0700 | [diff] [blame] | 30 | |
Brian Salomon | 15b2509 | 2017-05-08 11:10:53 -0400 | [diff] [blame] | 31 | GrDefaultPathRenderer::GrDefaultPathRenderer() { |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 32 | } |
| 33 | |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 34 | //////////////////////////////////////////////////////////////////////////////// |
| 35 | // Helpers for drawPath |
| 36 | |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 37 | #define STENCIL_OFF 0 // Always disable stencil (even when needed) |
| 38 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 39 | static inline bool single_pass_shape(const GrShape& shape) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 40 | #if STENCIL_OFF |
| 41 | return true; |
| 42 | #else |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 43 | // Inverse fill is always two pass. |
| 44 | if (shape.inverseFilled()) { |
| 45 | return false; |
| 46 | } |
| 47 | // This path renderer only accepts simple fill paths or stroke paths that are either hairline |
| 48 | // or have a stroke width small enough to treat as hairline. Hairline paths are always single |
| 49 | // pass. Filled paths are single pass if they're convex. |
| 50 | if (shape.style().isSimpleFill()) { |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 51 | return shape.knownToBeConvex(); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 52 | } |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 53 | return true; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 54 | #endif |
| 55 | } |
| 56 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 57 | GrPathRenderer::StencilSupport |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 58 | GrDefaultPathRenderer::onGetStencilSupport(const GrShape& shape) const { |
| 59 | if (single_pass_shape(shape)) { |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 60 | return GrPathRenderer::kNoRestriction_StencilSupport; |
| 61 | } else { |
| 62 | return GrPathRenderer::kStencilOnly_StencilSupport; |
| 63 | } |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Brian Salomon | ee3e0ba | 2017-07-13 16:40:46 -0400 | [diff] [blame] | 66 | namespace { |
| 67 | |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 68 | class PathGeoBuilder { |
| 69 | public: |
Robert Phillips | 9028bac | 2020-03-10 16:19:27 -0400 | [diff] [blame] | 70 | PathGeoBuilder(GrPrimitiveType primitiveType, |
| 71 | GrMeshDrawOp::Target* target, |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 72 | SkTDArray<GrSimpleMesh*>* meshes) |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 73 | : fPrimitiveType(primitiveType) |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 74 | , fTarget(target) |
| 75 | , fVertexStride(sizeof(SkPoint)) |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 76 | , fFirstIndex(0) |
| 77 | , fIndicesInChunk(0) |
Robert Phillips | 9028bac | 2020-03-10 16:19:27 -0400 | [diff] [blame] | 78 | , fIndices(nullptr) |
| 79 | , fMeshes(meshes) { |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 80 | this->allocNewBuffers(); |
Brian Osman | eb86b70 | 2017-06-07 11:38:31 -0400 | [diff] [blame] | 81 | } |
| 82 | |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 83 | ~PathGeoBuilder() { |
Robert Phillips | 9028bac | 2020-03-10 16:19:27 -0400 | [diff] [blame] | 84 | this->createMeshAndPutBackReserve(); |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Path verbs |
| 89 | */ |
| 90 | void moveTo(const SkPoint& p) { |
| 91 | needSpace(1); |
| 92 | |
| 93 | fSubpathIndexStart = this->currentIndex(); |
| 94 | *(fCurVert++) = p; |
| 95 | } |
| 96 | |
| 97 | void addLine(const SkPoint& p) { |
| 98 | needSpace(1, this->indexScale()); |
| 99 | |
Brian Salomon | 763abf0 | 2018-05-01 18:49:38 +0000 | [diff] [blame] | 100 | if (this->isIndexed()) { |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 101 | uint16_t prevIdx = this->currentIndex() - 1; |
| 102 | appendCountourEdgeIndices(prevIdx); |
| 103 | } |
| 104 | *(fCurVert++) = p; |
| 105 | } |
| 106 | |
| 107 | void addQuad(const SkPoint pts[], SkScalar srcSpaceTolSqd, SkScalar srcSpaceTol) { |
| 108 | this->needSpace(GrPathUtils::kMaxPointsPerCurve, |
| 109 | GrPathUtils::kMaxPointsPerCurve * this->indexScale()); |
| 110 | |
| 111 | // First pt of quad is the pt we ended on in previous step |
| 112 | uint16_t firstQPtIdx = this->currentIndex() - 1; |
| 113 | uint16_t numPts = (uint16_t)GrPathUtils::generateQuadraticPoints( |
| 114 | pts[0], pts[1], pts[2], srcSpaceTolSqd, &fCurVert, |
| 115 | GrPathUtils::quadraticPointCount(pts, srcSpaceTol)); |
Brian Salomon | 763abf0 | 2018-05-01 18:49:38 +0000 | [diff] [blame] | 116 | if (this->isIndexed()) { |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 117 | for (uint16_t i = 0; i < numPts; ++i) { |
| 118 | appendCountourEdgeIndices(firstQPtIdx + i); |
| 119 | } |
egdaniel | af18a09 | 2015-01-05 10:22:28 -0800 | [diff] [blame] | 120 | } |
| 121 | } |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 122 | |
| 123 | void addConic(SkScalar weight, const SkPoint pts[], SkScalar srcSpaceTolSqd, |
| 124 | SkScalar srcSpaceTol) { |
| 125 | SkAutoConicToQuads converter; |
| 126 | const SkPoint* quadPts = converter.computeQuads(pts, weight, srcSpaceTol); |
| 127 | for (int i = 0; i < converter.countQuads(); ++i) { |
| 128 | this->addQuad(quadPts + i * 2, srcSpaceTolSqd, srcSpaceTol); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void addCubic(const SkPoint pts[], SkScalar srcSpaceTolSqd, SkScalar srcSpaceTol) { |
| 133 | this->needSpace(GrPathUtils::kMaxPointsPerCurve, |
| 134 | GrPathUtils::kMaxPointsPerCurve * this->indexScale()); |
| 135 | |
| 136 | // First pt of cubic is the pt we ended on in previous step |
| 137 | uint16_t firstCPtIdx = this->currentIndex() - 1; |
| 138 | uint16_t numPts = (uint16_t) GrPathUtils::generateCubicPoints( |
| 139 | pts[0], pts[1], pts[2], pts[3], srcSpaceTolSqd, &fCurVert, |
| 140 | GrPathUtils::cubicPointCount(pts, srcSpaceTol)); |
Brian Salomon | 763abf0 | 2018-05-01 18:49:38 +0000 | [diff] [blame] | 141 | if (this->isIndexed()) { |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 142 | for (uint16_t i = 0; i < numPts; ++i) { |
| 143 | appendCountourEdgeIndices(firstCPtIdx + i); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | void addPath(const SkPath& path, SkScalar srcSpaceTol) { |
| 149 | SkScalar srcSpaceTolSqd = srcSpaceTol * srcSpaceTol; |
| 150 | |
| 151 | SkPath::Iter iter(path, false); |
| 152 | SkPoint pts[4]; |
| 153 | |
| 154 | bool done = false; |
| 155 | while (!done) { |
Mike Reed | ba7e9a6 | 2019-08-16 13:30:34 -0400 | [diff] [blame] | 156 | SkPath::Verb verb = iter.next(pts); |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 157 | switch (verb) { |
| 158 | case SkPath::kMove_Verb: |
| 159 | this->moveTo(pts[0]); |
| 160 | break; |
| 161 | case SkPath::kLine_Verb: |
| 162 | this->addLine(pts[1]); |
| 163 | break; |
| 164 | case SkPath::kConic_Verb: |
| 165 | this->addConic(iter.conicWeight(), pts, srcSpaceTolSqd, srcSpaceTol); |
| 166 | break; |
| 167 | case SkPath::kQuad_Verb: |
| 168 | this->addQuad(pts, srcSpaceTolSqd, srcSpaceTol); |
| 169 | break; |
| 170 | case SkPath::kCubic_Verb: |
| 171 | this->addCubic(pts, srcSpaceTolSqd, srcSpaceTol); |
| 172 | break; |
| 173 | case SkPath::kClose_Verb: |
| 174 | break; |
| 175 | case SkPath::kDone_Verb: |
| 176 | done = true; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | static bool PathHasMultipleSubpaths(const SkPath& path) { |
| 182 | bool first = true; |
| 183 | |
| 184 | SkPath::Iter iter(path, false); |
| 185 | SkPath::Verb verb; |
| 186 | |
| 187 | SkPoint pts[4]; |
Mike Reed | ba7e9a6 | 2019-08-16 13:30:34 -0400 | [diff] [blame] | 188 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 189 | if (SkPath::kMove_Verb == verb && !first) { |
| 190 | return true; |
| 191 | } |
| 192 | first = false; |
| 193 | } |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | private: |
| 198 | /** |
| 199 | * Derived properties |
| 200 | * TODO: Cache some of these for better performance, rather than re-computing? |
| 201 | */ |
Brian Salomon | 763abf0 | 2018-05-01 18:49:38 +0000 | [diff] [blame] | 202 | bool isIndexed() const { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 203 | return GrPrimitiveType::kLines == fPrimitiveType || |
| 204 | GrPrimitiveType::kTriangles == fPrimitiveType; |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 205 | } |
| 206 | bool isHairline() const { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 207 | return GrPrimitiveType::kLines == fPrimitiveType || |
| 208 | GrPrimitiveType::kLineStrip == fPrimitiveType; |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 209 | } |
| 210 | int indexScale() const { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 211 | switch (fPrimitiveType) { |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 212 | case GrPrimitiveType::kLines: |
| 213 | return 2; |
| 214 | case GrPrimitiveType::kTriangles: |
| 215 | return 3; |
| 216 | default: |
| 217 | return 0; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | uint16_t currentIndex() const { return fCurVert - fVertices; } |
| 222 | |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 223 | // Allocate vertex and (possibly) index buffers |
| 224 | void allocNewBuffers() { |
| 225 | // Ensure that we always get enough verts for a worst-case quad/cubic, plus leftover points |
| 226 | // from previous mesh piece (up to two verts to continue fanning). If we can't get that |
| 227 | // many, ask for a much larger number. This needs to be fairly big to handle quads/cubics, |
| 228 | // which have a worst-case of 1k points. |
| 229 | static const int kMinVerticesPerChunk = GrPathUtils::kMaxPointsPerCurve + 2; |
| 230 | static const int kFallbackVerticesPerChunk = 16384; |
| 231 | |
| 232 | fVertices = static_cast<SkPoint*>(fTarget->makeVertexSpaceAtLeast(fVertexStride, |
| 233 | kMinVerticesPerChunk, |
| 234 | kFallbackVerticesPerChunk, |
| 235 | &fVertexBuffer, |
| 236 | &fFirstVertex, |
| 237 | &fVerticesInChunk)); |
| 238 | |
Brian Salomon | 763abf0 | 2018-05-01 18:49:38 +0000 | [diff] [blame] | 239 | if (this->isIndexed()) { |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 240 | // Similar to above: Ensure we get enough indices for one worst-case quad/cubic. |
| 241 | // No extra indices are needed for stitching, though. If we can't get that many, ask |
| 242 | // for enough to match our large vertex request. |
| 243 | const int kMinIndicesPerChunk = GrPathUtils::kMaxPointsPerCurve * this->indexScale(); |
| 244 | const int kFallbackIndicesPerChunk = kFallbackVerticesPerChunk * this->indexScale(); |
| 245 | |
| 246 | fIndices = fTarget->makeIndexSpaceAtLeast(kMinIndicesPerChunk, kFallbackIndicesPerChunk, |
| 247 | &fIndexBuffer, &fFirstIndex, |
| 248 | &fIndicesInChunk); |
| 249 | } |
Brian Osman | 3902d40 | 2017-06-20 15:36:31 -0400 | [diff] [blame] | 250 | |
| 251 | fCurVert = fVertices; |
| 252 | fCurIdx = fIndices; |
| 253 | fSubpathIndexStart = 0; |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | void appendCountourEdgeIndices(uint16_t edgeV0Idx) { |
| 257 | // When drawing lines we're appending line segments along the countour. When applying the |
| 258 | // other fill rules we're drawing triangle fans around the start of the current (sub)path. |
| 259 | if (!this->isHairline()) { |
| 260 | *(fCurIdx++) = fSubpathIndexStart; |
| 261 | } |
| 262 | *(fCurIdx++) = edgeV0Idx; |
| 263 | *(fCurIdx++) = edgeV0Idx + 1; |
| 264 | } |
| 265 | |
| 266 | // Emits a single draw with all accumulated vertex/index data |
Robert Phillips | 9028bac | 2020-03-10 16:19:27 -0400 | [diff] [blame] | 267 | void createMeshAndPutBackReserve() { |
Brian Osman | 3902d40 | 2017-06-20 15:36:31 -0400 | [diff] [blame] | 268 | int vertexCount = fCurVert - fVertices; |
| 269 | int indexCount = fCurIdx - fIndices; |
| 270 | SkASSERT(vertexCount <= fVerticesInChunk); |
| 271 | SkASSERT(indexCount <= fIndicesInChunk); |
| 272 | |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 273 | GrSimpleMesh* mesh = nullptr; |
Brian Salomon | 763abf0 | 2018-05-01 18:49:38 +0000 | [diff] [blame] | 274 | if (this->isIndexed() ? SkToBool(indexCount) : SkToBool(vertexCount)) { |
Robert Phillips | 9028bac | 2020-03-10 16:19:27 -0400 | [diff] [blame] | 275 | mesh = fTarget->allocMesh(); |
Brian Salomon | 763abf0 | 2018-05-01 18:49:38 +0000 | [diff] [blame] | 276 | if (!this->isIndexed()) { |
Chris Dalton | 37c7bdd | 2020-03-13 09:21:12 -0600 | [diff] [blame] | 277 | mesh->set(std::move(fVertexBuffer), vertexCount, fFirstVertex); |
Brian Salomon | 763abf0 | 2018-05-01 18:49:38 +0000 | [diff] [blame] | 278 | } else { |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 279 | mesh->setIndexed(std::move(fIndexBuffer), indexCount, fFirstIndex, 0, |
Chris Dalton | 37c7bdd | 2020-03-13 09:21:12 -0600 | [diff] [blame] | 280 | vertexCount - 1, GrPrimitiveRestart::kNo, std::move(fVertexBuffer), |
| 281 | fFirstVertex); |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 282 | } |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 283 | } |
Brian Osman | 3902d40 | 2017-06-20 15:36:31 -0400 | [diff] [blame] | 284 | |
| 285 | fTarget->putBackIndices((size_t)(fIndicesInChunk - indexCount)); |
| 286 | fTarget->putBackVertices((size_t)(fVerticesInChunk - vertexCount), fVertexStride); |
Robert Phillips | 9028bac | 2020-03-10 16:19:27 -0400 | [diff] [blame] | 287 | |
| 288 | if (mesh) { |
| 289 | fMeshes->push_back(mesh); |
| 290 | } |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | void needSpace(int vertsNeeded, int indicesNeeded = 0) { |
| 294 | if (fCurVert + vertsNeeded > fVertices + fVerticesInChunk || |
| 295 | fCurIdx + indicesNeeded > fIndices + fIndicesInChunk) { |
| 296 | // We are about to run out of space (possibly) |
| 297 | |
| 298 | // To maintain continuity, we need to remember one or two points from the current mesh. |
| 299 | // Lines only need the last point, fills need the first point from the current contour. |
| 300 | // We always grab both here, and append the ones we need at the end of this process. |
| 301 | SkPoint lastPt = *(fCurVert - 1); |
Brian Osman | 3902d40 | 2017-06-20 15:36:31 -0400 | [diff] [blame] | 302 | SkASSERT(fSubpathIndexStart < fVerticesInChunk); |
| 303 | SkPoint subpathStartPt = fVertices[fSubpathIndexStart]; |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 304 | |
Brian Osman | 3902d40 | 2017-06-20 15:36:31 -0400 | [diff] [blame] | 305 | // Draw the mesh we've accumulated, and put back any unused space |
Robert Phillips | 9028bac | 2020-03-10 16:19:27 -0400 | [diff] [blame] | 306 | this->createMeshAndPutBackReserve(); |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 307 | |
Brian Osman | 3902d40 | 2017-06-20 15:36:31 -0400 | [diff] [blame] | 308 | // Get new buffers |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 309 | this->allocNewBuffers(); |
| 310 | |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 311 | // Append copies of the points we saved so the two meshes will weld properly |
| 312 | if (!this->isHairline()) { |
| 313 | *(fCurVert++) = subpathStartPt; |
| 314 | } |
| 315 | *(fCurVert++) = lastPt; |
| 316 | } |
| 317 | } |
| 318 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 319 | GrPrimitiveType fPrimitiveType; |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 320 | GrMeshDrawOp::Target* fTarget; |
| 321 | size_t fVertexStride; |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 322 | |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 323 | sk_sp<const GrBuffer> fVertexBuffer; |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 324 | int fFirstVertex; |
| 325 | int fVerticesInChunk; |
| 326 | SkPoint* fVertices; |
| 327 | SkPoint* fCurVert; |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 328 | |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 329 | sk_sp<const GrBuffer> fIndexBuffer; |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 330 | int fFirstIndex; |
| 331 | int fIndicesInChunk; |
| 332 | uint16_t* fIndices; |
| 333 | uint16_t* fCurIdx; |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 334 | uint16_t fSubpathIndexStart; |
Robert Phillips | 9028bac | 2020-03-10 16:19:27 -0400 | [diff] [blame] | 335 | |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 336 | SkTDArray<GrSimpleMesh*>* fMeshes; |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 337 | }; |
egdaniel | af18a09 | 2015-01-05 10:22:28 -0800 | [diff] [blame] | 338 | |
Brian Salomon | ee3e0ba | 2017-07-13 16:40:46 -0400 | [diff] [blame] | 339 | class DefaultPathOp final : public GrMeshDrawOp { |
| 340 | private: |
| 341 | using Helper = GrSimpleMeshDrawOpHelperWithStencil; |
| 342 | |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 343 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 344 | DEFINE_OP_CLASS_ID |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 345 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 346 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 347 | GrPaint&& paint, |
| 348 | const SkPath& path, |
| 349 | SkScalar tolerance, |
| 350 | uint8_t coverage, |
| 351 | const SkMatrix& viewMatrix, |
| 352 | bool isHairline, |
| 353 | GrAAType aaType, |
| 354 | const SkRect& devBounds, |
Brian Salomon | ee3e0ba | 2017-07-13 16:40:46 -0400 | [diff] [blame] | 355 | const GrUserStencilSettings* stencilSettings) { |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 356 | return Helper::FactoryHelper<DefaultPathOp>(context, std::move(paint), path, tolerance, |
| 357 | coverage, viewMatrix, isHairline, aaType, |
| 358 | devBounds, stencilSettings); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 361 | const char* name() const override { return "DefaultPathOp"; } |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 362 | |
Chris Dalton | 1706cbf | 2019-05-21 19:35:29 -0600 | [diff] [blame] | 363 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | 9028bac | 2020-03-10 16:19:27 -0400 | [diff] [blame] | 364 | if (fProgramInfo) { |
Chris Dalton | be45742 | 2020-03-16 18:05:03 -0600 | [diff] [blame^] | 365 | fProgramInfo->visitFPProxies(func); |
Robert Phillips | 9028bac | 2020-03-10 16:19:27 -0400 | [diff] [blame] | 366 | } else { |
| 367 | fHelper.visitProxies(func); |
| 368 | } |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 369 | } |
| 370 | |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 371 | #ifdef SK_DEBUG |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 372 | SkString dumpInfo() const override { |
| 373 | SkString string; |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 374 | string.appendf("Color: 0x%08x Count: %d\n", fColor.toBytes_RGBA(), fPaths.count()); |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 375 | for (const auto& path : fPaths) { |
| 376 | string.appendf("Tolerance: %.2f\n", path.fTolerance); |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 377 | } |
Brian Salomon | ee3e0ba | 2017-07-13 16:40:46 -0400 | [diff] [blame] | 378 | string += fHelper.dumpInfo(); |
| 379 | string += INHERITED::dumpInfo(); |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 380 | return string; |
| 381 | } |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 382 | #endif |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 383 | |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 384 | DefaultPathOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color, const SkPath& path, |
Brian Salomon | ee3e0ba | 2017-07-13 16:40:46 -0400 | [diff] [blame] | 385 | SkScalar tolerance, uint8_t coverage, const SkMatrix& viewMatrix, bool isHairline, |
| 386 | GrAAType aaType, const SkRect& devBounds, |
| 387 | const GrUserStencilSettings* stencilSettings) |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 388 | : INHERITED(ClassID()) |
Brian Salomon | ee3e0ba | 2017-07-13 16:40:46 -0400 | [diff] [blame] | 389 | , fHelper(helperArgs, aaType, stencilSettings) |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 390 | , fColor(color) |
| 391 | , fCoverage(coverage) |
| 392 | , fViewMatrix(viewMatrix) |
| 393 | , fIsHairline(isHairline) { |
| 394 | fPaths.emplace_back(PathData{path, tolerance}); |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 395 | |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 396 | HasAABloat aaBloat = (aaType == GrAAType::kNone) ? HasAABloat ::kNo : HasAABloat::kYes; |
| 397 | this->setBounds(devBounds, aaBloat, |
| 398 | isHairline ? IsHairline::kYes : IsHairline::kNo); |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 399 | } |
| 400 | |
Brian Salomon | ee3e0ba | 2017-07-13 16:40:46 -0400 | [diff] [blame] | 401 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 402 | |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 403 | GrProcessorSet::Analysis finalize( |
| 404 | const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage, |
| 405 | GrClampType clampType) override { |
Brian Salomon | ee3e0ba | 2017-07-13 16:40:46 -0400 | [diff] [blame] | 406 | GrProcessorAnalysisCoverage gpCoverage = |
| 407 | this->coverage() == 0xFF ? GrProcessorAnalysisCoverage::kNone |
| 408 | : GrProcessorAnalysisCoverage::kSingleChannel; |
Brian Osman | 8fa7ab4 | 2019-03-18 10:22:42 -0400 | [diff] [blame] | 409 | // This Op uses uniform (not vertex) color, so doesn't need to track wide color. |
| 410 | return fHelper.finalizeProcessors( |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 411 | caps, clip, hasMixedSampledCoverage, clampType, gpCoverage, &fColor, nullptr); |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 412 | } |
| 413 | |
Brian Salomon | ee3e0ba | 2017-07-13 16:40:46 -0400 | [diff] [blame] | 414 | private: |
Robert Phillips | 9028bac | 2020-03-10 16:19:27 -0400 | [diff] [blame] | 415 | GrPrimitiveType primType() const { |
| 416 | if (this->isHairline()) { |
| 417 | int instanceCount = fPaths.count(); |
| 418 | |
| 419 | // We avoid indices when we have a single hairline contour. |
| 420 | bool isIndexed = instanceCount > 1 || |
| 421 | PathGeoBuilder::PathHasMultipleSubpaths(fPaths[0].fPath); |
| 422 | |
| 423 | return isIndexed ? GrPrimitiveType::kLines : GrPrimitiveType::kLineStrip; |
| 424 | } |
| 425 | |
| 426 | return GrPrimitiveType::kTriangles; |
| 427 | } |
| 428 | |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 429 | GrProgramInfo* programInfo() override { return fProgramInfo; } |
| 430 | |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 431 | void onCreateProgramInfo(const GrCaps* caps, |
| 432 | SkArenaAlloc* arena, |
| 433 | const GrSurfaceProxyView* outputView, |
| 434 | GrAppliedClip&& appliedClip, |
| 435 | const GrXferProcessor::DstProxyView& dstProxyView) override { |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 436 | GrGeometryProcessor* gp; |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 437 | { |
| 438 | using namespace GrDefaultGeoProcFactory; |
| 439 | Color color(this->color()); |
| 440 | Coverage coverage(this->coverage()); |
Brian Salomon | ee3e0ba | 2017-07-13 16:40:46 -0400 | [diff] [blame] | 441 | LocalCoords localCoords(fHelper.usesLocalCoords() ? LocalCoords::kUsePosition_Type |
| 442 | : LocalCoords::kUnused_Type); |
Robert Phillips | 9028bac | 2020-03-10 16:19:27 -0400 | [diff] [blame] | 443 | gp = GrDefaultGeoProcFactory::Make(arena, |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 444 | color, |
| 445 | coverage, |
| 446 | localCoords, |
| 447 | this->viewMatrix()); |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 448 | } |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 449 | |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 450 | SkASSERT(gp->vertexStride() == sizeof(SkPoint)); |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 451 | |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 452 | fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, outputView, |
| 453 | std::move(appliedClip), |
| 454 | dstProxyView, gp, this->primType()); |
Brian Salomon | 763abf0 | 2018-05-01 18:49:38 +0000 | [diff] [blame] | 455 | |
Robert Phillips | 9028bac | 2020-03-10 16:19:27 -0400 | [diff] [blame] | 456 | } |
Brian Osman | 7f95dfc | 2017-06-09 14:43:49 +0000 | [diff] [blame] | 457 | |
Robert Phillips | 9028bac | 2020-03-10 16:19:27 -0400 | [diff] [blame] | 458 | void onPrepareDraws(Target* target) override { |
| 459 | PathGeoBuilder pathGeoBuilder(this->primType(), target, &fMeshes); |
Brian Osman | 7f95dfc | 2017-06-09 14:43:49 +0000 | [diff] [blame] | 460 | |
| 461 | // fill buffers |
Robert Phillips | 9028bac | 2020-03-10 16:19:27 -0400 | [diff] [blame] | 462 | for (int i = 0; i < fPaths.count(); i++) { |
Brian Salomon | 763abf0 | 2018-05-01 18:49:38 +0000 | [diff] [blame] | 463 | const PathData& args = fPaths[i]; |
| 464 | pathGeoBuilder.addPath(args.fPath, args.fTolerance); |
Brian Osman | 7f95dfc | 2017-06-09 14:43:49 +0000 | [diff] [blame] | 465 | } |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 466 | } |
| 467 | |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 468 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
Robert Phillips | 9028bac | 2020-03-10 16:19:27 -0400 | [diff] [blame] | 469 | if (!fProgramInfo) { |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 470 | this->createProgramInfo(flushState); |
Robert Phillips | 9028bac | 2020-03-10 16:19:27 -0400 | [diff] [blame] | 471 | } |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 472 | |
Robert Phillips | 9028bac | 2020-03-10 16:19:27 -0400 | [diff] [blame] | 473 | if (!fProgramInfo || !fMeshes.count()) { |
| 474 | return; |
| 475 | } |
| 476 | |
Chris Dalton | 765ed36 | 2020-03-16 17:34:44 -0600 | [diff] [blame] | 477 | flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds); |
| 478 | flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline()); |
Robert Phillips | 9028bac | 2020-03-10 16:19:27 -0400 | [diff] [blame] | 479 | for (int i = 0; i < fMeshes.count(); ++i) { |
Chris Dalton | 765ed36 | 2020-03-16 17:34:44 -0600 | [diff] [blame] | 480 | flushState->drawMesh(*fMeshes[i]); |
Robert Phillips | 9028bac | 2020-03-10 16:19:27 -0400 | [diff] [blame] | 481 | } |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Michael Ludwig | 28b0c5d | 2019-12-19 14:51:00 -0500 | [diff] [blame] | 484 | CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*, |
| 485 | const GrCaps& caps) override { |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 486 | DefaultPathOp* that = t->cast<DefaultPathOp>(); |
Brian Salomon | ee3e0ba | 2017-07-13 16:40:46 -0400 | [diff] [blame] | 487 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 488 | return CombineResult::kCannotCombine; |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 489 | } |
| 490 | |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 491 | if (this->color() != that->color()) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 492 | return CombineResult::kCannotCombine; |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | if (this->coverage() != that->coverage()) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 496 | return CombineResult::kCannotCombine; |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Mike Reed | 2c38315 | 2019-12-18 16:47:47 -0500 | [diff] [blame] | 499 | if (!SkMatrixPriv::CheapEqual(this->viewMatrix(), that->viewMatrix())) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 500 | return CombineResult::kCannotCombine; |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | if (this->isHairline() != that->isHairline()) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 504 | return CombineResult::kCannotCombine; |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 505 | } |
| 506 | |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 507 | fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin()); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 508 | return CombineResult::kMerged; |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 509 | } |
| 510 | |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 511 | const SkPMColor4f& color() const { return fColor; } |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 512 | uint8_t coverage() const { return fCoverage; } |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 513 | const SkMatrix& viewMatrix() const { return fViewMatrix; } |
| 514 | bool isHairline() const { return fIsHairline; } |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 515 | |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 516 | struct PathData { |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 517 | SkPath fPath; |
| 518 | SkScalar fTolerance; |
| 519 | }; |
| 520 | |
Brian Salomon | ee3e0ba | 2017-07-13 16:40:46 -0400 | [diff] [blame] | 521 | SkSTArray<1, PathData, true> fPaths; |
| 522 | Helper fHelper; |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 523 | SkPMColor4f fColor; |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 524 | uint8_t fCoverage; |
| 525 | SkMatrix fViewMatrix; |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 526 | bool fIsHairline; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 527 | |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 528 | SkTDArray<GrSimpleMesh*> fMeshes; |
| 529 | GrProgramInfo* fProgramInfo = nullptr; |
Robert Phillips | 9028bac | 2020-03-10 16:19:27 -0400 | [diff] [blame] | 530 | |
Brian Salomon | ee3e0ba | 2017-07-13 16:40:46 -0400 | [diff] [blame] | 531 | typedef GrMeshDrawOp INHERITED; |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 532 | }; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 533 | |
Brian Salomon | ee3e0ba | 2017-07-13 16:40:46 -0400 | [diff] [blame] | 534 | } // anonymous namespace |
| 535 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 536 | bool GrDefaultPathRenderer::internalDrawPath(GrRenderTargetContext* renderTargetContext, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 537 | GrPaint&& paint, |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 538 | GrAAType aaType, |
robertphillips | d2b6d64 | 2016-07-21 08:55:08 -0700 | [diff] [blame] | 539 | const GrUserStencilSettings& userStencilSettings, |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 540 | const GrClip& clip, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 541 | const SkMatrix& viewMatrix, |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 542 | const GrShape& shape, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 543 | bool stencilOnly) { |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 544 | auto context = renderTargetContext->surfPriv().getContext(); |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 545 | |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 546 | SkASSERT(GrAAType::kCoverage != aaType); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 547 | SkPath path; |
| 548 | shape.asPath(&path); |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 549 | |
| 550 | SkScalar hairlineCoverage; |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 551 | uint8_t newCoverage = 0xff; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 552 | bool isHairline = false; |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 553 | if (IsStrokeHairlineOrEquivalent(shape.style(), viewMatrix, &hairlineCoverage)) { |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 554 | newCoverage = SkScalarRoundToInt(hairlineCoverage * 0xff); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 555 | isHairline = true; |
| 556 | } else { |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 557 | SkASSERT(shape.style().isSimpleFill()); |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 558 | } |
| 559 | |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 560 | int passCount = 0; |
Brian Salomon | f086167 | 2017-05-08 11:10:10 -0400 | [diff] [blame] | 561 | const GrUserStencilSettings* passes[2]; |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 562 | bool reverse = false; |
| 563 | bool lastPassIsBounds; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 564 | |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 565 | if (isHairline) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 566 | passCount = 1; |
| 567 | if (stencilOnly) { |
| 568 | passes[0] = &gDirectToStencil; |
| 569 | } else { |
robertphillips | d2b6d64 | 2016-07-21 08:55:08 -0700 | [diff] [blame] | 570 | passes[0] = &userStencilSettings; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 571 | } |
| 572 | lastPassIsBounds = false; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 573 | } else { |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 574 | if (single_pass_shape(shape)) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 575 | passCount = 1; |
| 576 | if (stencilOnly) { |
| 577 | passes[0] = &gDirectToStencil; |
| 578 | } else { |
robertphillips | d2b6d64 | 2016-07-21 08:55:08 -0700 | [diff] [blame] | 579 | passes[0] = &userStencilSettings; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 580 | } |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 581 | lastPassIsBounds = false; |
| 582 | } else { |
Mike Reed | cf0e3c6 | 2019-12-03 16:26:15 -0500 | [diff] [blame] | 583 | switch (path.getFillType()) { |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 584 | case SkPathFillType::kInverseEvenOdd: |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 585 | reverse = true; |
| 586 | // fallthrough |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 587 | case SkPathFillType::kEvenOdd: |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 588 | passes[0] = &gEOStencilPass; |
| 589 | if (stencilOnly) { |
| 590 | passCount = 1; |
| 591 | lastPassIsBounds = false; |
| 592 | } else { |
| 593 | passCount = 2; |
| 594 | lastPassIsBounds = true; |
| 595 | if (reverse) { |
| 596 | passes[1] = &gInvEOColorPass; |
| 597 | } else { |
| 598 | passes[1] = &gEOColorPass; |
| 599 | } |
| 600 | } |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 601 | break; |
| 602 | |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 603 | case SkPathFillType::kInverseWinding: |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 604 | reverse = true; |
| 605 | // fallthrough |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 606 | case SkPathFillType::kWinding: |
Brian Salomon | 15b2509 | 2017-05-08 11:10:53 -0400 | [diff] [blame] | 607 | passes[0] = &gWindStencilPass; |
Brian Salomon | f086167 | 2017-05-08 11:10:10 -0400 | [diff] [blame] | 608 | passCount = 2; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 609 | if (stencilOnly) { |
| 610 | lastPassIsBounds = false; |
| 611 | --passCount; |
| 612 | } else { |
| 613 | lastPassIsBounds = true; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 614 | if (reverse) { |
| 615 | passes[passCount-1] = &gInvWindColorPass; |
| 616 | } else { |
| 617 | passes[passCount-1] = &gWindColorPass; |
| 618 | } |
| 619 | } |
| 620 | break; |
| 621 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 622 | SkDEBUGFAIL("Unknown path fFill!"); |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 623 | return false; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | } |
| 627 | |
senorblanco | 2b4bb07 | 2015-04-22 13:45:18 -0700 | [diff] [blame] | 628 | SkScalar tol = GrPathUtils::kDefaultTolerance; |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 629 | SkScalar srcSpaceTol = GrPathUtils::scaleToleranceToSrc(tol, viewMatrix, path.getBounds()); |
| 630 | |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 631 | SkRect devBounds; |
Robert Phillips | e9462dd | 2019-10-23 12:41:29 -0400 | [diff] [blame] | 632 | GetPathDevBounds(path, renderTargetContext->asRenderTargetProxy()->backingStoreDimensions(), |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 633 | viewMatrix, &devBounds); |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 634 | |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 635 | for (int p = 0; p < passCount; ++p) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 636 | if (lastPassIsBounds && (p == passCount-1)) { |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 637 | SkRect bounds; |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 638 | SkMatrix localMatrix = SkMatrix::I(); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 639 | if (reverse) { |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 640 | // draw over the dev bounds (which will be the whole dst surface for inv fill). |
| 641 | bounds = devBounds; |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 642 | SkMatrix vmi; |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 643 | // mapRect through persp matrix may not be correct |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 644 | if (!viewMatrix.hasPerspective() && viewMatrix.invert(&vmi)) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 645 | vmi.mapRect(&bounds); |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 646 | } else { |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 647 | if (!viewMatrix.invert(&localMatrix)) { |
| 648 | return false; |
| 649 | } |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 650 | } |
| 651 | } else { |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 652 | bounds = path.getBounds(); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 653 | } |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 654 | const SkMatrix& viewM = (reverse && viewMatrix.hasPerspective()) ? SkMatrix::I() : |
| 655 | viewMatrix; |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 656 | // This is a non-coverage aa rect op since we assert aaType != kCoverage at the start |
Mike Klein | 1688507 | 2018-12-11 09:54:31 -0500 | [diff] [blame] | 657 | assert_alive(paint); |
Michael Ludwig | aa1b6b3 | 2019-05-29 14:43:13 -0400 | [diff] [blame] | 658 | renderTargetContext->priv().stencilRect(clip, passes[p], std::move(paint), |
| 659 | GrAA(aaType == GrAAType::kMSAA), viewM, bounds, &localMatrix); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 660 | } else { |
Brian Salomon | d4652ca | 2017-01-13 12:11:36 -0500 | [diff] [blame] | 661 | bool stencilPass = stencilOnly || passCount > 1; |
Brian Salomon | b74ef03 | 2017-08-10 12:46:01 -0400 | [diff] [blame] | 662 | std::unique_ptr<GrDrawOp> op; |
Brian Salomon | d4652ca | 2017-01-13 12:11:36 -0500 | [diff] [blame] | 663 | if (stencilPass) { |
Brian Salomon | b74ef03 | 2017-08-10 12:46:01 -0400 | [diff] [blame] | 664 | GrPaint stencilPaint; |
| 665 | stencilPaint.setXPFactory(GrDisableColorXPFactory::Get()); |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 666 | op = DefaultPathOp::Make(context, std::move(stencilPaint), path, srcSpaceTol, |
| 667 | newCoverage, viewMatrix, isHairline, aaType, devBounds, |
| 668 | passes[p]); |
Brian Salomon | b74ef03 | 2017-08-10 12:46:01 -0400 | [diff] [blame] | 669 | } else { |
Mike Klein | 1688507 | 2018-12-11 09:54:31 -0500 | [diff] [blame] | 670 | assert_alive(paint); |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 671 | op = DefaultPathOp::Make(context, std::move(paint), path, srcSpaceTol, newCoverage, |
Brian Salomon | b74ef03 | 2017-08-10 12:46:01 -0400 | [diff] [blame] | 672 | viewMatrix, isHairline, aaType, devBounds, passes[p]); |
Brian Salomon | d4652ca | 2017-01-13 12:11:36 -0500 | [diff] [blame] | 673 | } |
Brian Salomon | ee3e0ba | 2017-07-13 16:40:46 -0400 | [diff] [blame] | 674 | renderTargetContext->addDrawOp(clip, std::move(op)); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 675 | } |
| 676 | } |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 677 | return true; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 680 | GrPathRenderer::CanDrawPath |
| 681 | GrDefaultPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
Chris Dalton | 09e5689 | 2019-03-13 00:22:01 -0600 | [diff] [blame] | 682 | bool isHairline = IsStrokeHairlineOrEquivalent( |
| 683 | args.fShape->style(), *args.fViewMatrix, nullptr); |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 684 | // If we aren't a single_pass_shape or hairline, we require stencil buffers. |
Greg Daniel | be7fc46 | 2019-01-03 16:40:42 -0500 | [diff] [blame] | 685 | if (!(single_pass_shape(*args.fShape) || isHairline) && |
| 686 | (args.fCaps->avoidStencilBuffers() || args.fTargetIsWrappedVkSecondaryCB)) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 687 | return CanDrawPath::kNo; |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 688 | } |
Chris Dalton | 09e5689 | 2019-03-13 00:22:01 -0600 | [diff] [blame] | 689 | // If antialiasing is required, we only support MSAA. |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 690 | if (GrAAType::kNone != args.fAAType && GrAAType::kMSAA != args.fAAType) { |
Chris Dalton | 09e5689 | 2019-03-13 00:22:01 -0600 | [diff] [blame] | 691 | return CanDrawPath::kNo; |
| 692 | } |
| 693 | // This can draw any path with any simple fill style. |
| 694 | if (!args.fShape->style().isSimpleFill() && !isHairline) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 695 | return CanDrawPath::kNo; |
| 696 | } |
| 697 | // This is the fallback renderer for when a path is too complicated for the others to draw. |
| 698 | return CanDrawPath::kAsBackup; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 699 | } |
| 700 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 701 | bool GrDefaultPathRenderer::onDrawPath(const DrawPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 702 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 703 | "GrDefaultPathRenderer::onDrawPath"); |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 704 | GrAAType aaType = (GrAAType::kNone != args.fAAType) ? GrAAType::kMSAA : GrAAType::kNone; |
Chris Dalton | 09e5689 | 2019-03-13 00:22:01 -0600 | [diff] [blame] | 705 | |
| 706 | return this->internalDrawPath( |
| 707 | args.fRenderTargetContext, std::move(args.fPaint), aaType, *args.fUserStencilSettings, |
| 708 | *args.fClip, *args.fViewMatrix, *args.fShape, false); |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 709 | } |
| 710 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 711 | void GrDefaultPathRenderer::onStencilPath(const StencilPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 712 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 713 | "GrDefaultPathRenderer::onStencilPath"); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 714 | SkASSERT(!args.fShape->inverseFilled()); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 715 | |
| 716 | GrPaint paint; |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 717 | paint.setXPFactory(GrDisableColorXPFactory::Get()); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 718 | |
Chris Dalton | 09e5689 | 2019-03-13 00:22:01 -0600 | [diff] [blame] | 719 | auto aaType = (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone; |
| 720 | |
| 721 | this->internalDrawPath( |
| 722 | args.fRenderTargetContext, std::move(paint), aaType, GrUserStencilSettings::kUnused, |
| 723 | *args.fClip, *args.fViewMatrix, *args.fShape, true); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 724 | } |
joshualitt | 622d3ad | 2015-05-07 08:13:11 -0700 | [diff] [blame] | 725 | |
| 726 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 727 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 728 | #if GR_TEST_UTILS |
joshualitt | 622d3ad | 2015-05-07 08:13:11 -0700 | [diff] [blame] | 729 | |
Brian Salomon | ee3e0ba | 2017-07-13 16:40:46 -0400 | [diff] [blame] | 730 | GR_DRAW_OP_TEST_DEFINE(DefaultPathOp) { |
joshualitt | 622d3ad | 2015-05-07 08:13:11 -0700 | [diff] [blame] | 731 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
| 732 | |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 733 | // For now just hairlines because the other types of draws require two ops. |
| 734 | // TODO we should figure out a way to combine the stencil and cover steps into one op. |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 735 | GrStyle style(SkStrokeRec::kHairline_InitStyle); |
joshualitt | 622d3ad | 2015-05-07 08:13:11 -0700 | [diff] [blame] | 736 | SkPath path = GrTest::TestPath(random); |
| 737 | |
| 738 | // Compute srcSpaceTol |
| 739 | SkRect bounds = path.getBounds(); |
| 740 | SkScalar tol = GrPathUtils::kDefaultTolerance; |
| 741 | SkScalar srcSpaceTol = GrPathUtils::scaleToleranceToSrc(tol, viewMatrix, bounds); |
| 742 | |
joshualitt | 622d3ad | 2015-05-07 08:13:11 -0700 | [diff] [blame] | 743 | viewMatrix.mapRect(&bounds); |
| 744 | uint8_t coverage = GrRandomCoverage(random); |
Brian Salomon | ee3e0ba | 2017-07-13 16:40:46 -0400 | [diff] [blame] | 745 | GrAAType aaType = GrAAType::kNone; |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 746 | if (numSamples > 1 && random->nextBool()) { |
Brian Salomon | ee3e0ba | 2017-07-13 16:40:46 -0400 | [diff] [blame] | 747 | aaType = GrAAType::kMSAA; |
| 748 | } |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 749 | return DefaultPathOp::Make(context, std::move(paint), path, srcSpaceTol, coverage, viewMatrix, |
| 750 | true, aaType, bounds, GrGetRandomStencil(random, context)); |
joshualitt | 622d3ad | 2015-05-07 08:13:11 -0700 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | #endif |