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