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