senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GrTessellatingPathRenderer.h" |
Brian Salomon | 653f42f | 2018-07-10 10:07:31 -0400 | [diff] [blame] | 9 | #include <stdio.h> |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 10 | #include "GrAuditTrail.h" |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 11 | #include "GrClip.h" |
senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 12 | #include "GrDefaultGeoProcFactory.h" |
Brian Salomon | 5ec9def | 2016-12-20 15:34:05 -0500 | [diff] [blame] | 13 | #include "GrDrawOpTest.h" |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 14 | #include "GrMesh.h" |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 15 | #include "GrOpFlushState.h" |
senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 16 | #include "GrPathUtils.h" |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 17 | #include "GrResourceCache.h" |
| 18 | #include "GrResourceProvider.h" |
Brian Salomon | 653f42f | 2018-07-10 10:07:31 -0400 | [diff] [blame] | 19 | #include "GrShape.h" |
| 20 | #include "GrSimpleMeshDrawOpHelper.h" |
| 21 | #include "GrStyle.h" |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 22 | #include "GrTessellator.h" |
senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 23 | #include "SkGeometry.h" |
Brian Salomon | 8952743 | 2016-12-16 09:52:16 -0500 | [diff] [blame] | 24 | #include "ops/GrMeshDrawOp.h" |
joshualitt | 7441782 | 2015-08-07 11:42:16 -0700 | [diff] [blame] | 25 | |
Stephen White | a7701e0 | 2018-01-23 15:35:05 -0500 | [diff] [blame] | 26 | #ifndef GR_AA_TESSELLATOR_MAX_VERB_COUNT |
| 27 | #define GR_AA_TESSELLATOR_MAX_VERB_COUNT 10 |
| 28 | #endif |
| 29 | |
senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 30 | /* |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 31 | * This path renderer tessellates the path into triangles using GrTessellator, uploads the |
| 32 | * triangles to a vertex buffer, and renders them with a single draw call. It can do screenspace |
| 33 | * antialiasing with a one-pixel coverage ramp. |
senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 34 | */ |
senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 35 | namespace { |
| 36 | |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 37 | struct TessInfo { |
| 38 | SkScalar fTolerance; |
senorblanco | 06f989a | 2015-09-02 09:05:17 -0700 | [diff] [blame] | 39 | int fCount; |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 42 | // When the SkPathRef genID changes, invalidate a corresponding GrResource described by key. |
| 43 | class PathInvalidator : public SkPathRef::GenIDChangeListener { |
| 44 | public: |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 45 | PathInvalidator(const GrUniqueKey& key, uint32_t contextUniqueID) |
| 46 | : fMsg(key, contextUniqueID) {} |
| 47 | |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 48 | private: |
| 49 | GrUniqueKeyInvalidatedMessage fMsg; |
| 50 | |
| 51 | void onChange() override { |
| 52 | SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); |
| 53 | } |
| 54 | }; |
| 55 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 56 | bool cache_match(GrBuffer* vertexBuffer, SkScalar tol, int* actualCount) { |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 57 | if (!vertexBuffer) { |
| 58 | return false; |
| 59 | } |
| 60 | const SkData* data = vertexBuffer->getUniqueKey().getCustomData(); |
| 61 | SkASSERT(data); |
| 62 | const TessInfo* info = static_cast<const TessInfo*>(data->data()); |
| 63 | if (info->fTolerance == 0 || info->fTolerance < 3.0f * tol) { |
senorblanco | 06f989a | 2015-09-02 09:05:17 -0700 | [diff] [blame] | 64 | *actualCount = info->fCount; |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 65 | return true; |
| 66 | } |
| 67 | return false; |
| 68 | } |
| 69 | |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 70 | class StaticVertexAllocator : public GrTessellator::VertexAllocator { |
| 71 | public: |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 72 | StaticVertexAllocator(size_t stride, GrResourceProvider* resourceProvider, bool canMapVB) |
| 73 | : VertexAllocator(stride) |
| 74 | , fResourceProvider(resourceProvider) |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 75 | , fCanMapVB(canMapVB) |
| 76 | , fVertices(nullptr) { |
| 77 | } |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 78 | void* lock(int vertexCount) override { |
| 79 | size_t size = vertexCount * stride(); |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 80 | fVertexBuffer.reset(fResourceProvider->createBuffer( |
Chris Dalton | d004e0b | 2018-09-27 09:28:03 -0600 | [diff] [blame] | 81 | size, kVertex_GrBufferType, kStatic_GrAccessPattern, GrResourceProvider::Flags::kNone)); |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 82 | if (!fVertexBuffer.get()) { |
| 83 | return nullptr; |
| 84 | } |
| 85 | if (fCanMapVB) { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 86 | fVertices = fVertexBuffer->map(); |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 87 | } else { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 88 | fVertices = sk_malloc_throw(vertexCount * stride()); |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 89 | } |
| 90 | return fVertices; |
| 91 | } |
| 92 | void unlock(int actualCount) override { |
| 93 | if (fCanMapVB) { |
| 94 | fVertexBuffer->unmap(); |
| 95 | } else { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 96 | fVertexBuffer->updateData(fVertices, actualCount * stride()); |
| 97 | sk_free(fVertices); |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 98 | } |
| 99 | fVertices = nullptr; |
| 100 | } |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 101 | GrBuffer* vertexBuffer() { return fVertexBuffer.get(); } |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 102 | private: |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 103 | sk_sp<GrBuffer> fVertexBuffer; |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 104 | GrResourceProvider* fResourceProvider; |
| 105 | bool fCanMapVB; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 106 | void* fVertices; |
| 107 | }; |
| 108 | |
| 109 | class DynamicVertexAllocator : public GrTessellator::VertexAllocator { |
| 110 | public: |
Brian Salomon | e5b399e | 2017-07-19 13:50:54 -0400 | [diff] [blame] | 111 | DynamicVertexAllocator(size_t stride, GrMeshDrawOp::Target* target) |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 112 | : VertexAllocator(stride) |
| 113 | , fTarget(target) |
| 114 | , fVertexBuffer(nullptr) |
| 115 | , fVertices(nullptr) {} |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 116 | void* lock(int vertexCount) override { |
| 117 | fVertexCount = vertexCount; |
| 118 | fVertices = fTarget->makeVertexSpace(stride(), vertexCount, &fVertexBuffer, &fFirstVertex); |
| 119 | return fVertices; |
| 120 | } |
| 121 | void unlock(int actualCount) override { |
| 122 | fTarget->putBackVertices(fVertexCount - actualCount, stride()); |
| 123 | fVertices = nullptr; |
| 124 | } |
| 125 | const GrBuffer* vertexBuffer() const { return fVertexBuffer; } |
| 126 | int firstVertex() const { return fFirstVertex; } |
| 127 | private: |
Brian Salomon | e5b399e | 2017-07-19 13:50:54 -0400 | [diff] [blame] | 128 | GrMeshDrawOp::Target* fTarget; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 129 | const GrBuffer* fVertexBuffer; |
| 130 | int fVertexCount; |
| 131 | int fFirstVertex; |
| 132 | void* fVertices; |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 133 | }; |
| 134 | |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 135 | } // namespace |
senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 136 | |
| 137 | GrTessellatingPathRenderer::GrTessellatingPathRenderer() { |
| 138 | } |
| 139 | |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 140 | GrPathRenderer::CanDrawPath |
| 141 | GrTessellatingPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
Chris Dalton | e5ede4b | 2017-09-07 18:33:08 +0000 | [diff] [blame] | 142 | // This path renderer can draw fill styles, and can do screenspace antialiasing via a |
| 143 | // one-pixel coverage ramp. It can do convex and concave paths, but we'll leave the convex |
| 144 | // ones to simpler algorithms. We pass on paths that have styles, though they may come back |
| 145 | // around after applying the styling information to the geometry to create a filled path. In |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 146 | // the non-AA case, We skip paths that don't have a key since the real advantage of this path |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 147 | // renderer comes from caching the tessellated geometry. In the AA case, we do not cache, so we |
| 148 | // accept paths without keys. |
Chris Dalton | e5ede4b | 2017-09-07 18:33:08 +0000 | [diff] [blame] | 149 | if (!args.fShape->style().isSimpleFill() || args.fShape->knownToBeConvex()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 150 | return CanDrawPath::kNo; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 151 | } |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 152 | if (GrAAType::kCoverage == args.fAAType) { |
Stephan White | 021f927 | 2017-01-03 21:06:16 +0000 | [diff] [blame] | 153 | SkPath path; |
| 154 | args.fShape->asPath(&path); |
Stephen White | a7701e0 | 2018-01-23 15:35:05 -0500 | [diff] [blame] | 155 | if (path.countVerbs() > GR_AA_TESSELLATOR_MAX_VERB_COUNT) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 156 | return CanDrawPath::kNo; |
Stephan White | 021f927 | 2017-01-03 21:06:16 +0000 | [diff] [blame] | 157 | } |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 158 | } else if (!args.fShape->hasUnstyledKey()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 159 | return CanDrawPath::kNo; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 160 | } |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 161 | return CanDrawPath::kYes; |
senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Brian Salomon | 9530f7e | 2017-07-11 09:03:10 -0400 | [diff] [blame] | 164 | namespace { |
| 165 | |
| 166 | class TessellatingPathOp final : public GrMeshDrawOp { |
| 167 | private: |
| 168 | using Helper = GrSimpleMeshDrawOpHelperWithStencil; |
| 169 | |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 170 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 171 | DEFINE_OP_CLASS_ID |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 172 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 173 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, |
| 174 | GrPaint&& paint, |
Brian Salomon | 9530f7e | 2017-07-11 09:03:10 -0400 | [diff] [blame] | 175 | const GrShape& shape, |
| 176 | const SkMatrix& viewMatrix, |
| 177 | SkIRect devClipBounds, |
| 178 | GrAAType aaType, |
| 179 | const GrUserStencilSettings* stencilSettings) { |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 180 | return Helper::FactoryHelper<TessellatingPathOp>(context, std::move(paint), shape, |
| 181 | viewMatrix, devClipBounds, |
| 182 | aaType, stencilSettings); |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Brian Salomon | a6aa590 | 2016-12-16 09:32:00 -0500 | [diff] [blame] | 185 | const char* name() const override { return "TessellatingPathOp"; } |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 186 | |
Brian Salomon | 7d94bb5 | 2018-10-12 14:37:19 -0400 | [diff] [blame] | 187 | void visitProxies(const VisitProxyFunc& func, VisitorType) const override { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 188 | fHelper.visitProxies(func); |
| 189 | } |
| 190 | |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 191 | #ifdef SK_DEBUG |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 192 | SkString dumpInfo() const override { |
| 193 | SkString string; |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 194 | string.appendf("Color 0x%08x, aa: %d\n", fColor.toBytes_RGBA(), fAntiAlias); |
Brian Salomon | 9530f7e | 2017-07-11 09:03:10 -0400 | [diff] [blame] | 195 | string += fHelper.dumpInfo(); |
| 196 | string += INHERITED::dumpInfo(); |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 197 | return string; |
| 198 | } |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 199 | #endif |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 200 | |
Brian Salomon | 9530f7e | 2017-07-11 09:03:10 -0400 | [diff] [blame] | 201 | TessellatingPathOp(Helper::MakeArgs helperArgs, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 202 | const SkPMColor4f& color, |
Brian Salomon | 9530f7e | 2017-07-11 09:03:10 -0400 | [diff] [blame] | 203 | const GrShape& shape, |
| 204 | const SkMatrix& viewMatrix, |
| 205 | const SkIRect& devClipBounds, |
| 206 | GrAAType aaType, |
| 207 | const GrUserStencilSettings* stencilSettings) |
| 208 | : INHERITED(ClassID()) |
| 209 | , fHelper(helperArgs, aaType, stencilSettings) |
| 210 | , fColor(color) |
| 211 | , fShape(shape) |
| 212 | , fViewMatrix(viewMatrix) |
| 213 | , fDevClipBounds(devClipBounds) |
| 214 | , fAntiAlias(GrAAType::kCoverage == aaType) { |
| 215 | SkRect devBounds; |
| 216 | viewMatrix.mapRect(&devBounds, shape.bounds()); |
| 217 | if (shape.inverseFilled()) { |
| 218 | // Because the clip bounds are used to add a contour for inverse fills, they must also |
| 219 | // include the path bounds. |
| 220 | devBounds.join(SkRect::Make(fDevClipBounds)); |
| 221 | } |
| 222 | this->setBounds(devBounds, HasAABloat::kNo, IsZeroArea::kNo); |
| 223 | } |
| 224 | |
| 225 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 226 | |
Brian Osman | 532b3f9 | 2018-07-11 10:02:07 -0400 | [diff] [blame] | 227 | RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override { |
Brian Salomon | 9530f7e | 2017-07-11 09:03:10 -0400 | [diff] [blame] | 228 | GrProcessorAnalysisCoverage coverage = fAntiAlias |
| 229 | ? GrProcessorAnalysisCoverage::kSingleChannel |
| 230 | : GrProcessorAnalysisCoverage::kNone; |
Brian Osman | 532b3f9 | 2018-07-11 10:02:07 -0400 | [diff] [blame] | 231 | return fHelper.xpRequiresDstTexture(caps, clip, coverage, &fColor); |
Brian Salomon | 9530f7e | 2017-07-11 09:03:10 -0400 | [diff] [blame] | 232 | } |
| 233 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 234 | private: |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 235 | SkPath getPath() const { |
| 236 | SkASSERT(!fShape.style().applies()); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 237 | SkPath path; |
| 238 | fShape.asPath(&path); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 239 | return path; |
| 240 | } |
| 241 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 242 | void draw(Target* target, sk_sp<const GrGeometryProcessor> gp, size_t vertexStride) { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 243 | SkASSERT(!fAntiAlias); |
| 244 | GrResourceProvider* rp = target->resourceProvider(); |
| 245 | bool inverseFill = fShape.inverseFilled(); |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 246 | // construct a cache key from the path's genID and the view matrix |
| 247 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 248 | GrUniqueKey key; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 249 | static constexpr int kClipBoundsCnt = sizeof(fDevClipBounds) / sizeof(uint32_t); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 250 | int shapeKeyDataCnt = fShape.unstyledKeySize(); |
| 251 | SkASSERT(shapeKeyDataCnt >= 0); |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame] | 252 | GrUniqueKey::Builder builder(&key, kDomain, shapeKeyDataCnt + kClipBoundsCnt, "Path"); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 253 | fShape.writeUnstyledKey(&builder[0]); |
| 254 | // For inverse fills, the tessellation is dependent on clip bounds. |
| 255 | if (inverseFill) { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 256 | memcpy(&builder[shapeKeyDataCnt], &fDevClipBounds, sizeof(fDevClipBounds)); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 257 | } else { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 258 | memset(&builder[shapeKeyDataCnt], 0, sizeof(fDevClipBounds)); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 259 | } |
| 260 | builder.finish(); |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 261 | sk_sp<GrBuffer> cachedVertexBuffer(rp->findByUniqueKey<GrBuffer>(key)); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 262 | int actualCount; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 263 | SkScalar tol = GrPathUtils::kDefaultTolerance; |
| 264 | tol = GrPathUtils::scaleToleranceToSrc(tol, fViewMatrix, fShape.bounds()); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 265 | if (cache_match(cachedVertexBuffer.get(), tol, &actualCount)) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 266 | this->drawVertices(target, std::move(gp), cachedVertexBuffer.get(), 0, actualCount); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 267 | return; |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 268 | } |
| 269 | |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 270 | SkRect clipBounds = SkRect::Make(fDevClipBounds); |
| 271 | |
| 272 | SkMatrix vmi; |
| 273 | if (!fViewMatrix.invert(&vmi)) { |
| 274 | return; |
| 275 | } |
| 276 | vmi.mapRect(&clipBounds); |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 277 | bool isLinear; |
| 278 | bool canMapVB = GrCaps::kNone_MapFlags != target->caps().mapBufferFlags(); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 279 | StaticVertexAllocator allocator(vertexStride, rp, canMapVB); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 280 | int count = GrTessellator::PathToTriangles(getPath(), tol, clipBounds, &allocator, |
| 281 | false, GrColor(), false, &isLinear); |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 282 | if (count == 0) { |
| 283 | return; |
| 284 | } |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 285 | this->drawVertices(target, std::move(gp), allocator.vertexBuffer(), 0, count); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 286 | TessInfo info; |
| 287 | info.fTolerance = isLinear ? 0 : tol; |
| 288 | info.fCount = count; |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 289 | key.setCustomData(SkData::MakeWithCopy(&info, sizeof(info))); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 290 | rp->assignUniqueKeyToResource(key, allocator.vertexBuffer()); |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 291 | fShape.addGenIDChangeListener(sk_make_sp<PathInvalidator>(key, target->contextUniqueID())); |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 294 | void drawAA(Target* target, sk_sp<const GrGeometryProcessor> gp, size_t vertexStride) { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 295 | SkASSERT(fAntiAlias); |
| 296 | SkPath path = getPath(); |
| 297 | if (path.isEmpty()) { |
| 298 | return; |
| 299 | } |
| 300 | SkRect clipBounds = SkRect::Make(fDevClipBounds); |
| 301 | path.transform(fViewMatrix); |
| 302 | SkScalar tol = GrPathUtils::kDefaultTolerance; |
| 303 | bool isLinear; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 304 | DynamicVertexAllocator allocator(vertexStride, target); |
Brian Osman | 1be2b7c | 2018-10-29 16:07:15 -0400 | [diff] [blame] | 305 | // TODO4F: Preserve float colors |
Brian Salomon | 9530f7e | 2017-07-11 09:03:10 -0400 | [diff] [blame] | 306 | int count = |
Brian Osman | 1be2b7c | 2018-10-29 16:07:15 -0400 | [diff] [blame] | 307 | GrTessellator::PathToTriangles(path, tol, clipBounds, &allocator, true, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 308 | fColor.toBytes_RGBA(), |
Brian Salomon | 9530f7e | 2017-07-11 09:03:10 -0400 | [diff] [blame] | 309 | fHelper.compatibleWithAlphaAsCoverage(), &isLinear); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 310 | if (count == 0) { |
| 311 | return; |
| 312 | } |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 313 | this->drawVertices(target, std::move(gp), allocator.vertexBuffer(), allocator.firstVertex(), |
| 314 | count); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 317 | void onPrepareDraws(Target* target) override { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 318 | sk_sp<GrGeometryProcessor> gp; |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 319 | { |
| 320 | using namespace GrDefaultGeoProcFactory; |
| 321 | |
| 322 | Color color(fColor); |
Brian Salomon | 9530f7e | 2017-07-11 09:03:10 -0400 | [diff] [blame] | 323 | LocalCoords::Type localCoordsType = fHelper.usesLocalCoords() |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 324 | ? LocalCoords::kUsePosition_Type |
| 325 | : LocalCoords::kUnused_Type; |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 326 | Coverage::Type coverageType; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 327 | if (fAntiAlias) { |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 328 | color = Color(Color::kPremulGrColorAttribute_Type); |
Brian Salomon | 9530f7e | 2017-07-11 09:03:10 -0400 | [diff] [blame] | 329 | if (fHelper.compatibleWithAlphaAsCoverage()) { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 330 | coverageType = Coverage::kSolid_Type; |
| 331 | } else { |
| 332 | coverageType = Coverage::kAttribute_Type; |
| 333 | } |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 334 | } else { |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 335 | coverageType = Coverage::kSolid_Type; |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 336 | } |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 337 | if (fAntiAlias) { |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 338 | gp = GrDefaultGeoProcFactory::MakeForDeviceSpace(target->caps().shaderCaps(), |
| 339 | color, coverageType, |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 340 | localCoordsType, fViewMatrix); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 341 | } else { |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 342 | gp = GrDefaultGeoProcFactory::Make(target->caps().shaderCaps(), |
| 343 | color, coverageType, localCoordsType, |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 344 | fViewMatrix); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 345 | } |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 346 | } |
Stephen White | cc70083 | 2017-02-15 11:45:16 -0500 | [diff] [blame] | 347 | if (!gp.get()) { |
| 348 | return; |
| 349 | } |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 350 | size_t vertexStride = gp->vertexStride(); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 351 | if (fAntiAlias) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 352 | this->drawAA(target, std::move(gp), vertexStride); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 353 | } else { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 354 | this->draw(target, std::move(gp), vertexStride); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 355 | } |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 356 | } |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 357 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 358 | void drawVertices(Target* target, sk_sp<const GrGeometryProcessor> gp, const GrBuffer* vb, |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 359 | int firstVertex, int count) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 360 | GrMesh* mesh = target->allocMesh(TESSELLATOR_WIREFRAME ? GrPrimitiveType::kLines |
| 361 | : GrPrimitiveType::kTriangles); |
| 362 | mesh->setNonIndexedNonInstanced(count); |
| 363 | mesh->setVertexData(vb, firstVertex); |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 364 | auto pipe = fHelper.makePipeline(target); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 365 | target->draw(std::move(gp), pipe.fPipeline, pipe.fFixedDynamicState, mesh); |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Brian Salomon | 9530f7e | 2017-07-11 09:03:10 -0400 | [diff] [blame] | 368 | Helper fHelper; |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 369 | SkPMColor4f fColor; |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 370 | GrShape fShape; |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 371 | SkMatrix fViewMatrix; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 372 | SkIRect fDevClipBounds; |
| 373 | bool fAntiAlias; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 374 | |
Brian Salomon | 9530f7e | 2017-07-11 09:03:10 -0400 | [diff] [blame] | 375 | typedef GrMeshDrawOp INHERITED; |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 376 | }; |
| 377 | |
Brian Salomon | 9530f7e | 2017-07-11 09:03:10 -0400 | [diff] [blame] | 378 | } // anonymous namespace |
| 379 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 380 | bool GrTessellatingPathRenderer::onDrawPath(const DrawPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 381 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
joshualitt | de83b41 | 2016-01-14 09:58:36 -0800 | [diff] [blame] | 382 | "GrTessellatingPathRenderer::onDrawPath"); |
senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 383 | SkIRect clipBoundsI; |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 384 | args.fClip->getConservativeBounds(args.fRenderTargetContext->width(), |
| 385 | args.fRenderTargetContext->height(), |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 386 | &clipBoundsI); |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 387 | std::unique_ptr<GrDrawOp> op = TessellatingPathOp::Make(args.fContext, |
| 388 | std::move(args.fPaint), |
Brian Salomon | 9530f7e | 2017-07-11 09:03:10 -0400 | [diff] [blame] | 389 | *args.fShape, |
| 390 | *args.fViewMatrix, |
| 391 | clipBoundsI, |
| 392 | args.fAAType, |
| 393 | args.fUserStencilSettings); |
| 394 | args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op)); |
senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 395 | return true; |
| 396 | } |
joshualitt | 2fbd406 | 2015-05-07 13:06:41 -0700 | [diff] [blame] | 397 | |
| 398 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 399 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 400 | #if GR_TEST_UTILS |
joshualitt | 2fbd406 | 2015-05-07 13:06:41 -0700 | [diff] [blame] | 401 | |
Brian Salomon | 9530f7e | 2017-07-11 09:03:10 -0400 | [diff] [blame] | 402 | GR_DRAW_OP_TEST_DEFINE(TesselatingPathOp) { |
joshualitt | 2fbd406 | 2015-05-07 13:06:41 -0700 | [diff] [blame] | 403 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
| 404 | SkPath path = GrTest::TestPath(random); |
bsalomon | d3030ac | 2016-09-01 07:20:29 -0700 | [diff] [blame] | 405 | SkIRect devClipBounds = SkIRect::MakeLTRB( |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 406 | random->nextU(), random->nextU(), random->nextU(), random->nextU()); |
bsalomon | d3030ac | 2016-09-01 07:20:29 -0700 | [diff] [blame] | 407 | devClipBounds.sort(); |
Brian Salomon | 9530f7e | 2017-07-11 09:03:10 -0400 | [diff] [blame] | 408 | static constexpr GrAAType kAATypes[] = {GrAAType::kNone, GrAAType::kMSAA, GrAAType::kCoverage}; |
| 409 | GrAAType aaType; |
| 410 | do { |
| 411 | aaType = kAATypes[random->nextULessThan(SK_ARRAY_COUNT(kAATypes))]; |
| 412 | } while(GrAAType::kMSAA == aaType && GrFSAAType::kUnifiedMSAA != fsaaType); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 413 | GrStyle style; |
| 414 | do { |
| 415 | GrTest::TestStyle(random, &style); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 416 | } while (!style.isSimpleFill()); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 417 | GrShape shape(path, style); |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 418 | return TessellatingPathOp::Make(context, std::move(paint), shape, viewMatrix, devClipBounds, |
| 419 | aaType, GrGetRandomStencil(random, context)); |
joshualitt | 2fbd406 | 2015-05-07 13:06:41 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | #endif |