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