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