blob: 242b992288b827b6faedb141db189624a5eb302b [file] [log] [blame]
Chris Dalton1a325d22017-07-14 15:17:41 -06001/*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
Chris Dalton1a325d22017-07-14 15:17:41 -06009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/pathops/SkPathOps.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrCaps.h"
12#include "src/gpu/GrClip.h"
13#include "src/gpu/GrProxyProvider.h"
14#include "src/gpu/ccpr/GrCCClipProcessor.h"
15#include "src/gpu/ccpr/GrCCDrawPathsOp.h"
16#include "src/gpu/ccpr/GrCCPathCache.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060017
Chris Dalton5ba36ba2018-05-09 01:08:38 -060018using PathInstance = GrCCPathProcessor::Instance;
Chris Daltona32a3c32017-12-05 10:05:21 -070019
Chris Daltonc3318f02019-07-19 14:20:53 -060020bool GrCoverageCountingPathRenderer::IsSupported(const GrCaps& caps, CoverageType* coverageType) {
Chris Dalton1a325d22017-07-14 15:17:41 -060021 const GrShaderCaps& shaderCaps = *caps.shaderCaps();
Greg Daniel0258c902019-08-01 13:08:33 -040022 GrBackendFormat defaultA8Format = caps.getDefaultBackendFormat(GrColorType::kAlpha_8,
23 GrRenderable::kYes);
Chris Daltonc3318f02019-07-19 14:20:53 -060024 if (caps.driverBlacklistCCPR() || !shaderCaps.integerSupport() ||
Chris Daltona77cdee2020-04-03 14:50:43 -060025 !caps.drawInstancedSupport() || !shaderCaps.floatIs32Bits() ||
Greg Daniel0258c902019-08-01 13:08:33 -040026 !defaultA8Format.isValid() || // This checks both texturable and renderable
Chris Daltona8fbeba2019-03-30 00:31:23 -060027 !caps.halfFloatVertexAttributeSupport()) {
28 return false;
29 }
Chris Daltonc3318f02019-07-19 14:20:53 -060030
Greg Daniel0258c902019-08-01 13:08:33 -040031 GrBackendFormat defaultAHalfFormat = caps.getDefaultBackendFormat(GrColorType::kAlpha_F16,
32 GrRenderable::kYes);
Chris Daltonc3318f02019-07-19 14:20:53 -060033 if (caps.allowCoverageCounting() &&
Greg Daniel0258c902019-08-01 13:08:33 -040034 defaultAHalfFormat.isValid()) { // This checks both texturable and renderable
Chris Daltonc3318f02019-07-19 14:20:53 -060035 if (coverageType) {
36 *coverageType = CoverageType::kFP16_CoverageCount;
37 }
38 return true;
39 }
40
Chris Dalton7c012082019-07-22 00:45:52 -040041 if (!caps.driverBlacklistMSAACCPR() &&
Greg Danieleadfac92019-08-02 09:03:53 -040042 caps.internalMultisampleCount(defaultA8Format) > 1 &&
Chris Daltonc3318f02019-07-19 14:20:53 -060043 caps.sampleLocationsSupport() &&
Chris Dalton8a64a442019-10-29 18:54:58 -060044 shaderCaps.sampleMaskSupport()) {
Chris Daltonc3318f02019-07-19 14:20:53 -060045 if (coverageType) {
46 *coverageType = CoverageType::kA8_Multisample;
47 }
48 return true;
49 }
50
51 return false;
Chris Dalton1a325d22017-07-14 15:17:41 -060052}
53
Chris Dalton383a2ef2018-01-08 17:21:41 -050054sk_sp<GrCoverageCountingPathRenderer> GrCoverageCountingPathRenderer::CreateIfSupported(
Chris Dalton351e80c2019-01-06 22:51:00 -070055 const GrCaps& caps, AllowCaching allowCaching, uint32_t contextUniqueID) {
Chris Daltonc3318f02019-07-19 14:20:53 -060056 CoverageType coverageType;
57 if (IsSupported(caps, &coverageType)) {
58 return sk_sp<GrCoverageCountingPathRenderer>(new GrCoverageCountingPathRenderer(
59 coverageType, allowCaching, contextUniqueID));
60 }
61 return nullptr;
Chris Daltona2b5b642018-06-24 13:08:57 -060062}
63
Chris Daltonc3318f02019-07-19 14:20:53 -060064GrCoverageCountingPathRenderer::GrCoverageCountingPathRenderer(
65 CoverageType coverageType, AllowCaching allowCaching, uint32_t contextUniqueID)
66 : fCoverageType(coverageType) {
Chris Daltona2b5b642018-06-24 13:08:57 -060067 if (AllowCaching::kYes == allowCaching) {
Mike Kleinf46d5ca2019-12-11 10:45:01 -050068 fPathCache = std::make_unique<GrCCPathCache>(contextUniqueID);
Chris Daltona2b5b642018-06-24 13:08:57 -060069 }
70}
71
Greg Danielf41b2bd2019-08-22 16:19:24 -040072GrCCPerOpsTaskPaths* GrCoverageCountingPathRenderer::lookupPendingPaths(uint32_t opsTaskID) {
73 auto it = fPendingPaths.find(opsTaskID);
Chris Daltond7e22272018-05-23 10:17:17 -060074 if (fPendingPaths.end() == it) {
Greg Danielf41b2bd2019-08-22 16:19:24 -040075 sk_sp<GrCCPerOpsTaskPaths> paths = sk_make_sp<GrCCPerOpsTaskPaths>();
76 it = fPendingPaths.insert(std::make_pair(opsTaskID, std::move(paths))).first;
Chris Daltond7e22272018-05-23 10:17:17 -060077 }
78 return it->second.get();
Chris Dalton5ba36ba2018-05-09 01:08:38 -060079}
80
Chris Dalton383a2ef2018-01-08 17:21:41 -050081GrPathRenderer::CanDrawPath GrCoverageCountingPathRenderer::onCanDrawPath(
82 const CanDrawPathArgs& args) const {
Chris Dalton09a7bb22018-08-31 19:53:15 +080083 const GrShape& shape = *args.fShape;
Chris Daltonc3318f02019-07-19 14:20:53 -060084 // We use "kCoverage", or analytic AA, no mater what the coverage type of our atlas: Even if the
85 // atlas is multisampled, that resolves into analytic coverage before we draw the path to the
86 // main canvas.
Chris Dalton6ce447a2019-06-23 18:07:38 -060087 if (GrAAType::kCoverage != args.fAAType || shape.style().hasPathEffect() ||
Chris Dalton09a7bb22018-08-31 19:53:15 +080088 args.fViewMatrix->hasPerspective() || shape.inverseFilled()) {
Chris Dalton5ed44232017-09-07 13:22:46 -060089 return CanDrawPath::kNo;
Chris Dalton1a325d22017-07-14 15:17:41 -060090 }
91
92 SkPath path;
Chris Dalton09a7bb22018-08-31 19:53:15 +080093 shape.asPath(&path);
Chris Daltona2b5b642018-06-24 13:08:57 -060094
Chris Dalton82de18f2018-09-12 17:24:09 -060095 const SkStrokeRec& stroke = shape.style().strokeRec();
96 switch (stroke.getStyle()) {
Chris Dalton09a7bb22018-08-31 19:53:15 +080097 case SkStrokeRec::kFill_Style: {
98 SkRect devBounds;
99 args.fViewMatrix->mapRect(&devBounds, path.getBounds());
Chris Daltona2b5b642018-06-24 13:08:57 -0600100
Chris Dalton09a7bb22018-08-31 19:53:15 +0800101 SkIRect clippedIBounds;
102 devBounds.roundOut(&clippedIBounds);
103 if (!clippedIBounds.intersect(*args.fClipConservativeBounds)) {
104 // The path is completely clipped away. Our code will eventually notice this before
105 // doing any real work.
106 return CanDrawPath::kYes;
107 }
108
109 int64_t numPixels = sk_64_mul(clippedIBounds.height(), clippedIBounds.width());
110 if (path.countVerbs() > 1000 && path.countPoints() > numPixels) {
111 // This is a complicated path that has more vertices than pixels! Let's let the SW
112 // renderer have this one: It will probably be faster and a bitmap will require less
113 // total memory on the GPU than CCPR instance buffers would for the raw path data.
114 return CanDrawPath::kNo;
115 }
116
117 if (numPixels > 256 * 256) {
118 // Large paths can blow up the atlas fast. And they are not ideal for a two-pass
119 // rendering algorithm. Give the simpler direct renderers a chance before we commit
120 // to drawing it.
121 return CanDrawPath::kAsBackup;
122 }
123
124 if (args.fShape->hasUnstyledKey() && path.countVerbs() > 50) {
125 // Complex paths do better cached in an SDF, if the renderer will accept them.
126 return CanDrawPath::kAsBackup;
127 }
128
129 return CanDrawPath::kYes;
130 }
131
132 case SkStrokeRec::kStroke_Style:
133 if (!args.fViewMatrix->isSimilarity()) {
134 // The stroker currently only supports rigid-body transfoms for the stroke lines
135 // themselves. This limitation doesn't affect hairlines since their stroke lines are
136 // defined relative to device space.
137 return CanDrawPath::kNo;
138 }
139 // fallthru
Chris Dalton82de18f2018-09-12 17:24:09 -0600140 case SkStrokeRec::kHairline_Style: {
Chris Daltonc3318f02019-07-19 14:20:53 -0600141 if (CoverageType::kFP16_CoverageCount != fCoverageType) {
142 // Stroking is not yet supported in MSAA atlas mode.
143 return CanDrawPath::kNo;
144 }
Chris Dalton82de18f2018-09-12 17:24:09 -0600145 float inflationRadius;
146 GetStrokeDevWidth(*args.fViewMatrix, stroke, &inflationRadius);
147 if (!(inflationRadius <= kMaxBoundsInflationFromStroke)) {
148 // Let extremely wide strokes be converted to fill paths and drawn by the CCPR
149 // filler instead. (Cast the logic negatively in order to also catch r=NaN.)
150 return CanDrawPath::kNo;
151 }
152 SkASSERT(!SkScalarIsNaN(inflationRadius));
153 if (SkPathPriv::ConicWeightCnt(path)) {
154 // The stroker does not support conics yet.
155 return CanDrawPath::kNo;
156 }
157 return CanDrawPath::kYes;
158 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800159
160 case SkStrokeRec::kStrokeAndFill_Style:
161 return CanDrawPath::kNo;
Chris Daltondb91c6e2017-09-08 16:25:08 -0600162 }
163
Chris Dalton09a7bb22018-08-31 19:53:15 +0800164 SK_ABORT("Invalid stroke style.");
Chris Dalton1a325d22017-07-14 15:17:41 -0600165}
166
167bool GrCoverageCountingPathRenderer::onDrawPath(const DrawPathArgs& args) {
168 SkASSERT(!fFlushing);
Chris Dalton1a325d22017-07-14 15:17:41 -0600169
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600170 SkIRect clipIBounds;
171 GrRenderTargetContext* rtc = args.fRenderTargetContext;
172 args.fClip->getConservativeBounds(rtc->width(), rtc->height(), &clipIBounds, nullptr);
173
Chris Dalton09a7bb22018-08-31 19:53:15 +0800174 auto op = GrCCDrawPathsOp::Make(args.fContext, clipIBounds, *args.fViewMatrix, *args.fShape,
175 std::move(args.fPaint));
Chris Dalton42c21152018-06-13 15:28:19 -0600176 this->recordOp(std::move(op), args);
Chris Dalton1a325d22017-07-14 15:17:41 -0600177 return true;
178}
179
Brian Salomon348a0372018-10-31 10:42:18 -0400180void GrCoverageCountingPathRenderer::recordOp(std::unique_ptr<GrCCDrawPathsOp> op,
Chris Dalton42c21152018-06-13 15:28:19 -0600181 const DrawPathArgs& args) {
Brian Salomon348a0372018-10-31 10:42:18 -0400182 if (op) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400183 auto addToOwningPerOpsTaskPaths = [this](GrOp* op, uint32_t opsTaskID) {
184 op->cast<GrCCDrawPathsOp>()->addToOwningPerOpsTaskPaths(
185 sk_ref_sp(this->lookupPendingPaths(opsTaskID)));
Brian Salomon348a0372018-10-31 10:42:18 -0400186 };
Greg Danielf41b2bd2019-08-22 16:19:24 -0400187 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op),
188 addToOwningPerOpsTaskPaths);
Chris Dalton42c21152018-06-13 15:28:19 -0600189 }
190}
191
Chris Dalton383a2ef2018-01-08 17:21:41 -0500192std::unique_ptr<GrFragmentProcessor> GrCoverageCountingPathRenderer::makeClipProcessor(
Greg Danielf41b2bd2019-08-22 16:19:24 -0400193 uint32_t opsTaskID, const SkPath& deviceSpacePath, const SkIRect& accessRect,
Chris Daltonc3318f02019-07-19 14:20:53 -0600194 const GrCaps& caps) {
Chris Daltona32a3c32017-12-05 10:05:21 -0700195 SkASSERT(!fFlushing);
Chris Daltona32a3c32017-12-05 10:05:21 -0700196
Chris Daltonc3318f02019-07-19 14:20:53 -0600197 uint32_t key = deviceSpacePath.getGenerationID();
198 if (CoverageType::kA8_Multisample == fCoverageType) {
199 // We only need to consider fill rule in MSAA mode. In coverage count mode Even/Odd and
200 // Nonzero both reference the same coverage count mask.
201 key = (key << 1) | (uint32_t)GrFillRuleForSkPath(deviceSpacePath);
202 }
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600203 GrCCClipPath& clipPath =
Greg Danielf41b2bd2019-08-22 16:19:24 -0400204 this->lookupPendingPaths(opsTaskID)->fClipPaths[key];
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600205 if (!clipPath.isInitialized()) {
Chris Daltona32a3c32017-12-05 10:05:21 -0700206 // This ClipPath was just created during lookup. Initialize it.
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600207 const SkRect& pathDevBounds = deviceSpacePath.getBounds();
Brian Osman788b9162020-02-07 10:36:46 -0500208 if (std::max(pathDevBounds.height(), pathDevBounds.width()) > kPathCropThreshold) {
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600209 // The path is too large. Crop it or analytic AA can run out of fp32 precision.
210 SkPath croppedPath;
Chris Dalton4c458b12018-06-16 17:22:59 -0600211 int maxRTSize = caps.maxRenderTargetSize();
Chris Dalton09a7bb22018-08-31 19:53:15 +0800212 CropPath(deviceSpacePath, SkIRect::MakeWH(maxRTSize, maxRTSize), &croppedPath);
Chris Daltonc3318f02019-07-19 14:20:53 -0600213 clipPath.init(croppedPath, accessRect, fCoverageType, caps);
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600214 } else {
Chris Daltonc3318f02019-07-19 14:20:53 -0600215 clipPath.init(deviceSpacePath, accessRect, fCoverageType, caps);
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600216 }
Chris Daltona32a3c32017-12-05 10:05:21 -0700217 } else {
218 clipPath.addAccess(accessRect);
219 }
220
Chris Daltonc3318f02019-07-19 14:20:53 -0600221 auto isCoverageCount = GrCCClipProcessor::IsCoverageCount(
222 CoverageType::kFP16_CoverageCount == fCoverageType);
223 auto mustCheckBounds = GrCCClipProcessor::MustCheckBounds(
224 !clipPath.pathDevIBounds().contains(accessRect));
Greg Daniele810d832020-02-07 17:20:56 -0500225 return std::make_unique<GrCCClipProcessor>(caps, &clipPath, isCoverageCount, mustCheckBounds);
Chris Daltona32a3c32017-12-05 10:05:21 -0700226}
227
Brian Salomonbf6b9792019-08-21 09:38:10 -0400228void GrCoverageCountingPathRenderer::preFlush(
Chris Daltonc4b47352019-08-23 10:10:36 -0600229 GrOnFlushResourceProvider* onFlushRP, const uint32_t* opsTaskIDs, int numOpsTaskIDs) {
Chris Dalton351e80c2019-01-06 22:51:00 -0700230 using DoCopiesToA8Coverage = GrCCDrawPathsOp::DoCopiesToA8Coverage;
Chris Daltona32a3c32017-12-05 10:05:21 -0700231 SkASSERT(!fFlushing);
Chris Daltond7e22272018-05-23 10:17:17 -0600232 SkASSERT(fFlushingPaths.empty());
Chris Dalton383a2ef2018-01-08 17:21:41 -0500233 SkDEBUGCODE(fFlushing = true);
Chris Daltona32a3c32017-12-05 10:05:21 -0700234
Chris Dalton351e80c2019-01-06 22:51:00 -0700235 if (fPathCache) {
236 fPathCache->doPreFlushProcessing();
Chris Dalton4da70192018-06-18 09:51:36 -0600237 }
238
Chris Daltond7e22272018-05-23 10:17:17 -0600239 if (fPendingPaths.empty()) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500240 return; // Nothing to draw.
Chris Daltona32a3c32017-12-05 10:05:21 -0700241 }
Chris Daltonc1e59632017-09-05 00:30:07 -0600242
Chris Dalton4da70192018-06-18 09:51:36 -0600243 GrCCPerFlushResourceSpecs specs;
Chris Dalton42c21152018-06-13 15:28:19 -0600244 int maxPreferredRTSize = onFlushRP->caps()->maxPreferredRenderTargetSize();
Brian Osman788b9162020-02-07 10:36:46 -0500245 specs.fCopyAtlasSpecs.fMaxPreferredTextureSize = std::min(2048, maxPreferredRTSize);
Chris Dalton4da70192018-06-18 09:51:36 -0600246 SkASSERT(0 == specs.fCopyAtlasSpecs.fMinTextureSize);
247 specs.fRenderedAtlasSpecs.fMaxPreferredTextureSize = maxPreferredRTSize;
Brian Osman788b9162020-02-07 10:36:46 -0500248 specs.fRenderedAtlasSpecs.fMinTextureSize = std::min(512, maxPreferredRTSize);
Chris Dalton42c21152018-06-13 15:28:19 -0600249
Greg Danielf41b2bd2019-08-22 16:19:24 -0400250 // Move the per-opsTask paths that are about to be flushed from fPendingPaths to fFlushingPaths,
Chris Dalton42c21152018-06-13 15:28:19 -0600251 // and count them up so we can preallocate buffers.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400252 fFlushingPaths.reserve(numOpsTaskIDs);
253 for (int i = 0; i < numOpsTaskIDs; ++i) {
254 auto iter = fPendingPaths.find(opsTaskIDs[i]);
Chris Daltond7e22272018-05-23 10:17:17 -0600255 if (fPendingPaths.end() == iter) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400256 continue; // No paths on this opsTask.
Chris Dalton1a325d22017-07-14 15:17:41 -0600257 }
Chris Daltona32a3c32017-12-05 10:05:21 -0700258
tzikbdb49562018-05-28 14:58:00 +0900259 fFlushingPaths.push_back(std::move(iter->second));
Chris Daltond7e22272018-05-23 10:17:17 -0600260 fPendingPaths.erase(iter);
261
Chris Dalton4da70192018-06-18 09:51:36 -0600262 for (GrCCDrawPathsOp* op : fFlushingPaths.back()->fDrawOps) {
Chris Dalton351e80c2019-01-06 22:51:00 -0700263 op->accountForOwnPaths(fPathCache.get(), onFlushRP, &specs);
Chris Dalton080baa42017-11-06 14:19:19 -0700264 }
Chris Daltond7e22272018-05-23 10:17:17 -0600265 for (const auto& clipsIter : fFlushingPaths.back()->fClipPaths) {
Chris Dalton4da70192018-06-18 09:51:36 -0600266 clipsIter.second.accountForOwnPath(&specs);
Chris Daltona32a3c32017-12-05 10:05:21 -0700267 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600268 }
269
Chris Dalton4da70192018-06-18 09:51:36 -0600270 if (specs.isEmpty()) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500271 return; // Nothing to draw.
Chris Dalton1a325d22017-07-14 15:17:41 -0600272 }
273
Chris Dalton4da70192018-06-18 09:51:36 -0600274 // Determine if there are enough reusable paths from last flush for it to be worth our time to
275 // copy them to cached atlas(es).
Chris Dalton09a7bb22018-08-31 19:53:15 +0800276 int numCopies = specs.fNumCopiedPaths[GrCCPerFlushResourceSpecs::kFillIdx] +
277 specs.fNumCopiedPaths[GrCCPerFlushResourceSpecs::kStrokeIdx];
Chris Dalton351e80c2019-01-06 22:51:00 -0700278 auto doCopies = DoCopiesToA8Coverage(numCopies > 100 ||
279 specs.fCopyAtlasSpecs.fApproxNumPixels > 256 * 256);
280 if (numCopies && DoCopiesToA8Coverage::kNo == doCopies) {
281 specs.cancelCopies();
Chris Dalton4da70192018-06-18 09:51:36 -0600282 }
283
Chris Daltonc3318f02019-07-19 14:20:53 -0600284 auto resources = sk_make_sp<GrCCPerFlushResources>(onFlushRP, fCoverageType, specs);
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600285 if (!resources->isMapped()) {
286 return; // Some allocation failed.
Chris Dalton1a325d22017-07-14 15:17:41 -0600287 }
288
Chris Dalton4da70192018-06-18 09:51:36 -0600289 // Layout the atlas(es) and parse paths.
Chris Daltond7e22272018-05-23 10:17:17 -0600290 for (const auto& flushingPaths : fFlushingPaths) {
291 for (GrCCDrawPathsOp* op : flushingPaths->fDrawOps) {
Chris Dalton351e80c2019-01-06 22:51:00 -0700292 op->setupResources(fPathCache.get(), onFlushRP, resources.get(), doCopies);
Chris Dalton1a325d22017-07-14 15:17:41 -0600293 }
Chris Daltond7e22272018-05-23 10:17:17 -0600294 for (auto& clipsIter : flushingPaths->fClipPaths) {
Chris Daltondaef06a2018-05-23 17:11:09 -0600295 clipsIter.second.renderPathInAtlas(resources.get(), onFlushRP);
Chris Daltonc1e59632017-09-05 00:30:07 -0600296 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600297 }
298
Chris Dalton351e80c2019-01-06 22:51:00 -0700299 if (fPathCache) {
300 // Purge invalidated textures from previous atlases *before* calling finalize(). That way,
301 // the underlying textures objects can be freed up and reused for the next atlases.
302 fPathCache->purgeInvalidatedAtlasTextures(onFlushRP);
Chris Daltond6fa4542019-01-04 13:23:51 -0700303 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600304
Chris Dalton351e80c2019-01-06 22:51:00 -0700305 // Allocate resources and then render the atlas(es).
Chris Daltonc4b47352019-08-23 10:10:36 -0600306 if (!resources->finalize(onFlushRP)) {
Chris Dalton351e80c2019-01-06 22:51:00 -0700307 return;
308 }
Chris Dalton2e825a32019-01-04 22:14:27 +0000309
Chris Daltond7e22272018-05-23 10:17:17 -0600310 // Commit flushing paths to the resources once they are successfully completed.
311 for (auto& flushingPaths : fFlushingPaths) {
Robert Phillips774168e2018-05-31 12:43:27 -0400312 SkASSERT(!flushingPaths->fFlushResources);
Chris Daltond7e22272018-05-23 10:17:17 -0600313 flushingPaths->fFlushResources = resources;
314 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600315}
316
Greg Danielf41b2bd2019-08-22 16:19:24 -0400317void GrCoverageCountingPathRenderer::postFlush(GrDeferredUploadToken, const uint32_t* opsTaskIDs,
318 int numOpsTaskIDs) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600319 SkASSERT(fFlushing);
Robert Phillips774168e2018-05-31 12:43:27 -0400320
Chris Dalton4da70192018-06-18 09:51:36 -0600321 if (!fFlushingPaths.empty()) {
Chris Dalton4da70192018-06-18 09:51:36 -0600322 // In DDL mode these aren't guaranteed to be deleted so we must clear out the perFlush
323 // resources manually.
324 for (auto& flushingPaths : fFlushingPaths) {
325 flushingPaths->fFlushResources = nullptr;
326 }
327
328 // We wait to erase these until after flush, once Ops and FPs are done accessing their data.
329 fFlushingPaths.reset();
Robert Phillips774168e2018-05-31 12:43:27 -0400330 }
331
Chris Dalton383a2ef2018-01-08 17:21:41 -0500332 SkDEBUGCODE(fFlushing = false);
Chris Dalton1a325d22017-07-14 15:17:41 -0600333}
Chris Dalton09a7bb22018-08-31 19:53:15 +0800334
Chris Dalton6c3879d2018-11-01 11:13:19 -0600335void GrCoverageCountingPathRenderer::purgeCacheEntriesOlderThan(
Chris Dalton351e80c2019-01-06 22:51:00 -0700336 GrProxyProvider* proxyProvider, const GrStdSteadyClock::time_point& purgeTime) {
Chris Dalton6c3879d2018-11-01 11:13:19 -0600337 if (fPathCache) {
Chris Dalton351e80c2019-01-06 22:51:00 -0700338 fPathCache->purgeEntriesOlderThan(proxyProvider, purgeTime);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600339 }
340}
341
Chris Dalton09a7bb22018-08-31 19:53:15 +0800342void GrCoverageCountingPathRenderer::CropPath(const SkPath& path, const SkIRect& cropbox,
343 SkPath* out) {
344 SkPath cropboxPath;
345 cropboxPath.addRect(SkRect::Make(cropbox));
346 if (!Op(cropboxPath, path, kIntersect_SkPathOp, out)) {
347 // This can fail if the PathOps encounter NaN or infinities.
348 out->reset();
349 }
350 out->setIsVolatile(true);
351}
Chris Dalton82de18f2018-09-12 17:24:09 -0600352
353float GrCoverageCountingPathRenderer::GetStrokeDevWidth(const SkMatrix& m,
354 const SkStrokeRec& stroke,
355 float* inflationRadius) {
356 float strokeDevWidth;
357 if (stroke.isHairlineStyle()) {
358 strokeDevWidth = 1;
359 } else {
360 SkASSERT(SkStrokeRec::kStroke_Style == stroke.getStyle());
361 SkASSERT(m.isSimilarity()); // Otherwise matrixScaleFactor = m.getMaxScale().
362 float matrixScaleFactor = SkVector::Length(m.getScaleX(), m.getSkewY());
363 strokeDevWidth = stroke.getWidth() * matrixScaleFactor;
364 }
365 if (inflationRadius) {
366 // Inflate for a minimum stroke width of 1. In some cases when the stroke is less than 1px
367 // wide, we may inflate it to 1px and instead reduce the opacity.
368 *inflationRadius = SkStrokeRec::GetInflationRadius(
Brian Osman788b9162020-02-07 10:36:46 -0500369 stroke.getJoin(), stroke.getMiter(), stroke.getCap(), std::max(strokeDevWidth, 1.f));
Chris Dalton82de18f2018-09-12 17:24:09 -0600370 }
371 return strokeDevWidth;
372}