blob: 79e318fd28aa0dfe6d031ea8f0fb365d3a8320cb [file] [log] [blame]
Chris Dalton5ba36ba2018-05-09 01:08:38 -06001/*
2 * Copyright 2018 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
8#include "GrCCDrawPathsOp.h"
Chris Dalton351e80c2019-01-06 22:51:00 -07009
Robert Phillips7c525e62018-06-12 10:11:12 -040010#include "GrMemoryPool.h"
Chris Dalton5ba36ba2018-05-09 01:08:38 -060011#include "GrOpFlushState.h"
Robert Phillips6f0e02f2019-02-13 11:02:28 -050012#include "GrRecordingContext.h"
13#include "GrRecordingContextPriv.h"
Chris Daltona2b5b642018-06-24 13:08:57 -060014#include "ccpr/GrCCPathCache.h"
Chris Dalton5ba36ba2018-05-09 01:08:38 -060015#include "ccpr/GrCCPerFlushResources.h"
16#include "ccpr/GrCoverageCountingPathRenderer.h"
17
Chris Dalton1c548942018-05-22 13:09:48 -060018static bool has_coord_transforms(const GrPaint& paint) {
19 GrFragmentProcessor::Iter iter(paint);
20 while (const GrFragmentProcessor* fp = iter.next()) {
21 if (!fp->coordTransforms().empty()) {
22 return true;
23 }
24 }
25 return false;
26}
27
Chris Dalton09a7bb22018-08-31 19:53:15 +080028std::unique_ptr<GrCCDrawPathsOp> GrCCDrawPathsOp::Make(
Robert Phillips6f0e02f2019-02-13 11:02:28 -050029 GrRecordingContext* context, const SkIRect& clipIBounds, const SkMatrix& m,
30 const GrShape& shape, GrPaint&& paint) {
Chris Dalton09a7bb22018-08-31 19:53:15 +080031 SkRect conservativeDevBounds;
32 m.mapRect(&conservativeDevBounds, shape.bounds());
33
34 const SkStrokeRec& stroke = shape.style().strokeRec();
35 float strokeDevWidth = 0;
36 float conservativeInflationRadius = 0;
37 if (!stroke.isFillStyle()) {
Chris Dalton82de18f2018-09-12 17:24:09 -060038 strokeDevWidth = GrCoverageCountingPathRenderer::GetStrokeDevWidth(
39 m, stroke, &conservativeInflationRadius);
Chris Dalton09a7bb22018-08-31 19:53:15 +080040 conservativeDevBounds.outset(conservativeInflationRadius, conservativeInflationRadius);
41 }
42
43 std::unique_ptr<GrCCDrawPathsOp> op;
44 float conservativeSize = SkTMax(conservativeDevBounds.height(), conservativeDevBounds.width());
Chris Dalton82de18f2018-09-12 17:24:09 -060045 if (conservativeSize > GrCoverageCountingPathRenderer::kPathCropThreshold) {
Chris Dalton09a7bb22018-08-31 19:53:15 +080046 // The path is too large. Crop it or analytic AA can run out of fp32 precision.
47 SkPath croppedDevPath;
48 shape.asPath(&croppedDevPath);
49 croppedDevPath.transform(m, &croppedDevPath);
50
51 SkIRect cropBox = clipIBounds;
52 GrShape croppedDevShape;
53 if (stroke.isFillStyle()) {
54 GrCoverageCountingPathRenderer::CropPath(croppedDevPath, cropBox, &croppedDevPath);
55 croppedDevShape = GrShape(croppedDevPath);
56 conservativeDevBounds = croppedDevShape.bounds();
57 } else {
58 int r = SkScalarCeilToInt(conservativeInflationRadius);
59 cropBox.outset(r, r);
60 GrCoverageCountingPathRenderer::CropPath(croppedDevPath, cropBox, &croppedDevPath);
61 SkStrokeRec devStroke = stroke;
62 devStroke.setStrokeStyle(strokeDevWidth);
63 croppedDevShape = GrShape(croppedDevPath, GrStyle(devStroke, nullptr));
64 conservativeDevBounds = croppedDevPath.getBounds();
65 conservativeDevBounds.outset(conservativeInflationRadius, conservativeInflationRadius);
66 }
67
68 // FIXME: This breaks local coords: http://skbug.com/8003
69 return InternalMake(context, clipIBounds, SkMatrix::I(), croppedDevShape, strokeDevWidth,
70 conservativeDevBounds, std::move(paint));
71 }
72
73 return InternalMake(context, clipIBounds, m, shape, strokeDevWidth, conservativeDevBounds,
74 std::move(paint));
75}
76
77std::unique_ptr<GrCCDrawPathsOp> GrCCDrawPathsOp::InternalMake(
Robert Phillips6f0e02f2019-02-13 11:02:28 -050078 GrRecordingContext* context, const SkIRect& clipIBounds, const SkMatrix& m,
79 const GrShape& shape, float strokeDevWidth, const SkRect& conservativeDevBounds,
80 GrPaint&& paint) {
Chris Dalton82de18f2018-09-12 17:24:09 -060081 // The path itself should have been cropped if larger than kPathCropThreshold. If it had a
82 // stroke, that would have further inflated its draw bounds.
83 SkASSERT(SkTMax(conservativeDevBounds.height(), conservativeDevBounds.width()) <
84 GrCoverageCountingPathRenderer::kPathCropThreshold +
85 GrCoverageCountingPathRenderer::kMaxBoundsInflationFromStroke*2 + 1);
86
Chris Dalton09a7bb22018-08-31 19:53:15 +080087 SkIRect shapeConservativeIBounds;
88 conservativeDevBounds.roundOut(&shapeConservativeIBounds);
Chris Daltona8429cf2018-06-22 11:43:31 -060089
90 SkIRect maskDevIBounds;
Chris Daltonaaa77c12019-01-07 17:45:36 -070091 if (!maskDevIBounds.intersect(clipIBounds, shapeConservativeIBounds)) {
92 return nullptr;
Chris Dalton42c21152018-06-13 15:28:19 -060093 }
Robert Phillipsc994a932018-06-19 13:09:54 -040094
Robert Phillips9da87e02019-02-04 13:26:26 -050095 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Chris Dalton09a7bb22018-08-31 19:53:15 +080096 return pool->allocate<GrCCDrawPathsOp>(m, shape, strokeDevWidth, shapeConservativeIBounds,
Chris Daltonaaa77c12019-01-07 17:45:36 -070097 maskDevIBounds, conservativeDevBounds, std::move(paint));
Chris Dalton42c21152018-06-13 15:28:19 -060098}
99
Chris Dalton09a7bb22018-08-31 19:53:15 +0800100GrCCDrawPathsOp::GrCCDrawPathsOp(const SkMatrix& m, const GrShape& shape, float strokeDevWidth,
101 const SkIRect& shapeConservativeIBounds,
Chris Daltonaaa77c12019-01-07 17:45:36 -0700102 const SkIRect& maskDevIBounds, const SkRect& conservativeDevBounds,
103 GrPaint&& paint)
Chris Dalton4bfb50b2018-05-21 09:10:53 -0600104 : GrDrawOp(ClassID())
Chris Daltond7e22272018-05-23 10:17:17 -0600105 , fViewMatrixIfUsingLocalCoords(has_coord_transforms(paint) ? m : SkMatrix::I())
Chris Daltonaaa77c12019-01-07 17:45:36 -0700106 , fDraws(m, shape, strokeDevWidth, shapeConservativeIBounds, maskDevIBounds,
Brian Osmancf860852018-10-31 14:04:39 -0400107 paint.getColor4f())
Chris Dalton42c21152018-06-13 15:28:19 -0600108 , fProcessors(std::move(paint)) { // Paint must be moved after fetching its color above.
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600109 SkDEBUGCODE(fBaseInstance = -1);
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600110 // FIXME: intersect with clip bounds to (hopefully) improve batching.
111 // (This is nontrivial due to assumptions in generating the octagon cover geometry.)
Chris Dalton09a7bb22018-08-31 19:53:15 +0800112 this->setBounds(conservativeDevBounds, GrOp::HasAABloat::kYes, GrOp::IsZeroArea::kNo);
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600113}
114
115GrCCDrawPathsOp::~GrCCDrawPathsOp() {
Chris Daltond7e22272018-05-23 10:17:17 -0600116 if (fOwningPerOpListPaths) {
Chris Daltonb68bcc42018-09-14 00:44:22 -0600117 // Remove the list's dangling pointer to this Op before deleting it.
Chris Daltond7e22272018-05-23 10:17:17 -0600118 fOwningPerOpListPaths->fDrawOps.remove(this);
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600119 }
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600120}
121
Chris Daltona8429cf2018-06-22 11:43:31 -0600122GrCCDrawPathsOp::SingleDraw::SingleDraw(const SkMatrix& m, const GrShape& shape,
Chris Dalton09a7bb22018-08-31 19:53:15 +0800123 float strokeDevWidth,
124 const SkIRect& shapeConservativeIBounds,
Chris Daltonaaa77c12019-01-07 17:45:36 -0700125 const SkIRect& maskDevIBounds, const SkPMColor4f& color)
Chris Daltona8429cf2018-06-22 11:43:31 -0600126 : fMatrix(m)
Chris Dalton644341a2018-06-18 19:14:16 -0600127 , fShape(shape)
Chris Dalton09a7bb22018-08-31 19:53:15 +0800128 , fStrokeDevWidth(strokeDevWidth)
129 , fShapeConservativeIBounds(shapeConservativeIBounds)
Chris Daltona8429cf2018-06-22 11:43:31 -0600130 , fMaskDevIBounds(maskDevIBounds)
Chris Daltona8429cf2018-06-22 11:43:31 -0600131 , fColor(color) {
Chris Dalton644341a2018-06-18 19:14:16 -0600132#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
133 if (fShape.hasUnstyledKey()) {
134 // On AOSP we round view matrix translates to integer values for cachable paths. We do this
135 // to match HWUI's cache hit ratio, which doesn't consider the matrix when caching paths.
136 fMatrix.setTranslateX(SkScalarRoundToScalar(fMatrix.getTranslateX()));
137 fMatrix.setTranslateY(SkScalarRoundToScalar(fMatrix.getTranslateY()));
138 }
139#endif
140}
141
Chris Dalton4b62aed2019-01-15 11:53:00 -0700142GrProcessorSet::Analysis GrCCDrawPathsOp::finalize(const GrCaps& caps, const GrAppliedClip* clip) {
Chris Dalton4da70192018-06-18 09:51:36 -0600143 SkASSERT(1 == fNumDraws); // There should only be one single path draw in this Op right now.
Chris Daltona13078c2019-01-07 09:34:05 -0700144 return fDraws.head().finalize(caps, clip, &fProcessors);
145}
Chris Dalton09a7bb22018-08-31 19:53:15 +0800146
Chris Dalton4b62aed2019-01-15 11:53:00 -0700147GrProcessorSet::Analysis GrCCDrawPathsOp::SingleDraw::finalize(
Chris Daltona13078c2019-01-07 09:34:05 -0700148 const GrCaps& caps, const GrAppliedClip* clip, GrProcessorSet* processors) {
149 const GrProcessorSet::Analysis& analysis = processors->finalize(
150 fColor, GrProcessorAnalysisCoverage::kSingleChannel, clip, false, caps,
151 &fColor);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800152
153 // Lines start looking jagged when they get thinner than 1px. For thin strokes it looks better
154 // if we can convert them to hairline (i.e., inflate the stroke width to 1px), and instead
155 // reduce the opacity to create the illusion of thin-ness. This strategy also helps reduce
156 // artifacts from coverage dilation when there are self intersections.
157 if (analysis.isCompatibleWithCoverageAsAlpha() &&
Chris Daltona13078c2019-01-07 09:34:05 -0700158 !fShape.style().strokeRec().isFillStyle() && fStrokeDevWidth < 1) {
Chris Dalton09a7bb22018-08-31 19:53:15 +0800159 // Modifying the shape affects its cache key. The draw can't have a cache entry yet or else
160 // our next step would invalidate it.
Chris Daltona13078c2019-01-07 09:34:05 -0700161 SkASSERT(!fCacheEntry);
162 SkASSERT(SkStrokeRec::kStroke_Style == fShape.style().strokeRec().getStyle());
Chris Dalton09a7bb22018-08-31 19:53:15 +0800163
164 SkPath path;
Chris Daltona13078c2019-01-07 09:34:05 -0700165 fShape.asPath(&path);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800166
167 // Create a hairline version of our stroke.
Chris Daltona13078c2019-01-07 09:34:05 -0700168 SkStrokeRec hairlineStroke = fShape.style().strokeRec();
Chris Dalton09a7bb22018-08-31 19:53:15 +0800169 hairlineStroke.setStrokeStyle(0);
170
171 // How transparent does a 1px stroke have to be in order to appear as thin as the real one?
Chris Daltona13078c2019-01-07 09:34:05 -0700172 float coverage = fStrokeDevWidth;
Chris Dalton09a7bb22018-08-31 19:53:15 +0800173
Chris Daltona13078c2019-01-07 09:34:05 -0700174 fShape = GrShape(path, GrStyle(hairlineStroke, nullptr));
175 fStrokeDevWidth = 1;
Brian Osman1be2b7c2018-10-29 16:07:15 -0400176
Chris Dalton09a7bb22018-08-31 19:53:15 +0800177 // fShapeConservativeIBounds already accounted for this possibility of inflating the stroke.
Chris Daltona13078c2019-01-07 09:34:05 -0700178 fColor = fColor * coverage;
Chris Dalton09a7bb22018-08-31 19:53:15 +0800179 }
180
Chris Dalton4b62aed2019-01-15 11:53:00 -0700181 return analysis;
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600182}
183
Brian Salomon7eae3e02018-08-07 14:02:38 +0000184GrOp::CombineResult GrCCDrawPathsOp::onCombineIfPossible(GrOp* op, const GrCaps&) {
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600185 GrCCDrawPathsOp* that = op->cast<GrCCDrawPathsOp>();
Chris Daltond7e22272018-05-23 10:17:17 -0600186 SkASSERT(fOwningPerOpListPaths);
Chris Dalton4bfb50b2018-05-21 09:10:53 -0600187 SkASSERT(fNumDraws);
Chris Daltond7e22272018-05-23 10:17:17 -0600188 SkASSERT(!that->fOwningPerOpListPaths || that->fOwningPerOpListPaths == fOwningPerOpListPaths);
Chris Dalton4bfb50b2018-05-21 09:10:53 -0600189 SkASSERT(that->fNumDraws);
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600190
Brian Osman9aa30c62018-07-02 15:21:46 -0400191 if (fProcessors != that->fProcessors ||
Chris Dalton1c548942018-05-22 13:09:48 -0600192 fViewMatrixIfUsingLocalCoords != that->fViewMatrixIfUsingLocalCoords) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000193 return CombineResult::kCannotCombine;
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600194 }
195
Chris Daltond7e22272018-05-23 10:17:17 -0600196 fDraws.append(std::move(that->fDraws), &fOwningPerOpListPaths->fAllocator);
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600197
Chris Dalton4bfb50b2018-05-21 09:10:53 -0600198 SkDEBUGCODE(fNumDraws += that->fNumDraws);
199 SkDEBUGCODE(that->fNumDraws = 0);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000200 return CombineResult::kMerged;
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600201}
202
Brian Salomon348a0372018-10-31 10:42:18 -0400203void GrCCDrawPathsOp::addToOwningPerOpListPaths(sk_sp<GrCCPerOpListPaths> owningPerOpListPaths) {
Chris Daltonf104fec2018-05-22 16:17:48 -0600204 SkASSERT(1 == fNumDraws);
Chris Daltond7e22272018-05-23 10:17:17 -0600205 SkASSERT(!fOwningPerOpListPaths);
Chris Daltonb68bcc42018-09-14 00:44:22 -0600206 fOwningPerOpListPaths = std::move(owningPerOpListPaths);
207 fOwningPerOpListPaths->fDrawOps.addToTail(this);
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600208}
209
Chris Dalton4da70192018-06-18 09:51:36 -0600210void GrCCDrawPathsOp::accountForOwnPaths(GrCCPathCache* pathCache,
211 GrOnFlushResourceProvider* onFlushRP,
Chris Dalton4da70192018-06-18 09:51:36 -0600212 GrCCPerFlushResourceSpecs* specs) {
Chris Daltona13078c2019-01-07 09:34:05 -0700213 for (SingleDraw& draw : fDraws) {
214 draw.accountForOwnPath(pathCache, onFlushRP, specs);
215 }
216}
217
218void GrCCDrawPathsOp::SingleDraw::accountForOwnPath(
219 GrCCPathCache* pathCache, GrOnFlushResourceProvider* onFlushRP,
220 GrCCPerFlushResourceSpecs* specs) {
Chris Dalton351e80c2019-01-06 22:51:00 -0700221 using CoverageType = GrCCAtlas::CoverageType;
Chris Dalton4da70192018-06-18 09:51:36 -0600222
Chris Daltona13078c2019-01-07 09:34:05 -0700223 SkPath path;
224 fShape.asPath(&path);
Chris Dalton4da70192018-06-18 09:51:36 -0600225
Chris Daltona13078c2019-01-07 09:34:05 -0700226 SkASSERT(!fCacheEntry);
Chris Daltona2b5b642018-06-24 13:08:57 -0600227
Chris Daltona13078c2019-01-07 09:34:05 -0700228 if (pathCache) {
Chris Daltonaaa77c12019-01-07 17:45:36 -0700229 fCacheEntry =
230 pathCache->find(onFlushRP, fShape, fMaskDevIBounds, fMatrix, &fCachedMaskShift);
Chris Dalton4bfb50b2018-05-21 09:10:53 -0600231 }
Chris Daltona13078c2019-01-07 09:34:05 -0700232
233 if (fCacheEntry) {
234 if (const GrCCCachedAtlas* cachedAtlas = fCacheEntry->cachedAtlas()) {
235 SkASSERT(cachedAtlas->getOnFlushProxy());
236 if (CoverageType::kA8_LiteralCoverage == cachedAtlas->coverageType()) {
237 ++specs->fNumCachedPaths;
238 } else {
239 // Suggest that this path be copied to a literal coverage atlas, to save memory.
240 // (The client may decline this copy via DoCopiesToA8Coverage::kNo.)
241 int idx = (fShape.style().strokeRec().isFillStyle())
242 ? GrCCPerFlushResourceSpecs::kFillIdx
243 : GrCCPerFlushResourceSpecs::kStrokeIdx;
244 ++specs->fNumCopiedPaths[idx];
245 specs->fCopyPathStats[idx].statPath(path);
246 specs->fCopyAtlasSpecs.accountForSpace(fCacheEntry->width(), fCacheEntry->height());
247 fDoCopyToA8Coverage = true;
248 }
249 return;
250 }
251
Chris Daltonaaa77c12019-01-07 17:45:36 -0700252 if (this->shouldCachePathMask(onFlushRP->caps()->maxRenderTargetSize())) {
253 fDoCachePathMask = true;
254 // We don't cache partial masks; ensure the bounds include the entire path.
255 fMaskDevIBounds = fShapeConservativeIBounds;
Chris Daltona13078c2019-01-07 09:34:05 -0700256 }
257 }
258
Chris Daltonaaa77c12019-01-07 17:45:36 -0700259 // Plan on rendering this path in a new atlas.
Chris Daltona13078c2019-01-07 09:34:05 -0700260 int idx = (fShape.style().strokeRec().isFillStyle())
261 ? GrCCPerFlushResourceSpecs::kFillIdx
262 : GrCCPerFlushResourceSpecs::kStrokeIdx;
263 ++specs->fNumRenderedPaths[idx];
264 specs->fRenderedPathStats[idx].statPath(path);
Chris Daltonaaa77c12019-01-07 17:45:36 -0700265 specs->fRenderedAtlasSpecs.accountForSpace(fMaskDevIBounds.width(), fMaskDevIBounds.height());
266}
267
268bool GrCCDrawPathsOp::SingleDraw::shouldCachePathMask(int maxRenderTargetSize) const {
269 SkASSERT(fCacheEntry);
270 SkASSERT(!fCacheEntry->cachedAtlas());
271 if (fCacheEntry->hitCount() <= 1) {
272 return false; // Don't cache a path mask until at least its second hit.
273 }
274
275 int shapeMaxDimension = SkTMax(fShapeConservativeIBounds.height(),
276 fShapeConservativeIBounds.width());
277 if (shapeMaxDimension > maxRenderTargetSize) {
278 return false; // This path isn't cachable.
279 }
280
281 int64_t shapeArea = sk_64_mul(fShapeConservativeIBounds.height(),
282 fShapeConservativeIBounds.width());
283 if (shapeArea < 100*100) {
284 // If a path is small enough, we might as well try to render and cache the entire thing, no
285 // matter how much of it is actually visible.
286 return true;
287 }
288
289 // The hitRect should already be contained within the shape's bounds, but we still intersect it
290 // because it's possible for edges very near pixel boundaries (e.g., 0.999999), to round out
291 // inconsistently, depending on the integer translation values and fp32 precision.
292 SkIRect hitRect = fCacheEntry->hitRect().makeOffset(fCachedMaskShift.x(), fCachedMaskShift.y());
293 hitRect.intersect(fShapeConservativeIBounds);
294
295 // Render and cache the entire path mask if we see enough of it to justify rendering all the
296 // pixels. Our criteria for "enough" is that we must have seen at least 50% of the path in the
297 // past, and in this particular draw we must see at least 10% of it.
298 int64_t hitArea = sk_64_mul(hitRect.height(), hitRect.width());
299 int64_t drawArea = sk_64_mul(fMaskDevIBounds.height(), fMaskDevIBounds.width());
300 return hitArea*2 >= shapeArea && drawArea*10 >= shapeArea;
Chris Dalton4bfb50b2018-05-21 09:10:53 -0600301}
302
Chris Dalton351e80c2019-01-06 22:51:00 -0700303void GrCCDrawPathsOp::setupResources(
304 GrCCPathCache* pathCache, GrOnFlushResourceProvider* onFlushRP,
305 GrCCPerFlushResources* resources, DoCopiesToA8Coverage doCopies) {
Chris Dalton4bfb50b2018-05-21 09:10:53 -0600306 SkASSERT(fNumDraws > 0);
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600307 SkASSERT(-1 == fBaseInstance);
Chris Daltondaef06a2018-05-23 17:11:09 -0600308 fBaseInstance = resources->nextPathInstanceIdx();
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600309
Chris Dalton4da70192018-06-18 09:51:36 -0600310 for (SingleDraw& draw : fDraws) {
Chris Daltona13078c2019-01-07 09:34:05 -0700311 draw.setupResources(pathCache, onFlushRP, resources, doCopies, this);
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600312 }
313
Chris Dalton4da70192018-06-18 09:51:36 -0600314 if (!fInstanceRanges.empty()) {
315 fInstanceRanges.back().fEndInstanceIdx = resources->nextPathInstanceIdx();
316 }
317}
318
Chris Daltona13078c2019-01-07 09:34:05 -0700319void GrCCDrawPathsOp::SingleDraw::setupResources(
320 GrCCPathCache* pathCache, GrOnFlushResourceProvider* onFlushRP,
321 GrCCPerFlushResources* resources, DoCopiesToA8Coverage doCopies, GrCCDrawPathsOp* op) {
322 using DoEvenOddFill = GrCCPathProcessor::DoEvenOddFill;
323
324 SkPath path;
325 fShape.asPath(&path);
326
327 auto doEvenOddFill = DoEvenOddFill(fShape.style().strokeRec().isFillStyle() &&
328 SkPath::kEvenOdd_FillType == path.getFillType());
329 SkASSERT(SkPath::kEvenOdd_FillType == path.getFillType() ||
330 SkPath::kWinding_FillType == path.getFillType());
331
332 if (fCacheEntry) {
333 // Does the path already exist in a cached atlas texture?
334 if (fCacheEntry->cachedAtlas()) {
335 SkASSERT(fCacheEntry->cachedAtlas()->getOnFlushProxy());
336 if (DoCopiesToA8Coverage::kYes == doCopies && fDoCopyToA8Coverage) {
337 resources->upgradeEntryToLiteralCoverageAtlas(pathCache, onFlushRP,
338 fCacheEntry.get(), doEvenOddFill);
339 SkASSERT(fCacheEntry->cachedAtlas());
340 SkASSERT(GrCCAtlas::CoverageType::kA8_LiteralCoverage
341 == fCacheEntry->cachedAtlas()->coverageType());
342 SkASSERT(fCacheEntry->cachedAtlas()->getOnFlushProxy());
343 }
Chris Daltonaaa77c12019-01-07 17:45:36 -0700344#if 0
345 // Simple color manipulation to visualize cached paths.
346 fColor = (GrCCAtlas::CoverageType::kA8_LiteralCoverage
347 == fCacheEntry->cachedAtlas()->coverageType())
348 ? SkPMColor4f{0,0,.25,.25} : SkPMColor4f{0,.25,0,.25};
349#endif
Chris Daltona13078c2019-01-07 09:34:05 -0700350 op->recordInstance(fCacheEntry->cachedAtlas()->getOnFlushProxy(),
351 resources->nextPathInstanceIdx());
Chris Daltona13078c2019-01-07 09:34:05 -0700352 resources->appendDrawPathInstance().set(*fCacheEntry, fCachedMaskShift,
Brian Osmanc6444d22019-01-09 16:30:12 -0500353 SkPMColor4f_toFP16(fColor));
Chris Daltona13078c2019-01-07 09:34:05 -0700354 return;
355 }
356 }
357
358 // Render the raw path into a coverage count atlas. renderShapeInAtlas() gives us two tight
359 // bounding boxes: One in device space, as well as a second one rotated an additional 45
360 // degrees. The path vertex shader uses these two bounding boxes to generate an octagon that
361 // circumscribes the path.
362 SkRect devBounds, devBounds45;
363 SkIRect devIBounds;
364 SkIVector devToAtlasOffset;
365 if (auto atlas = resources->renderShapeInAtlas(
366 fMaskDevIBounds, fMatrix, fShape, fStrokeDevWidth, &devBounds, &devBounds45,
367 &devIBounds, &devToAtlasOffset)) {
368 op->recordInstance(atlas->textureProxy(), resources->nextPathInstanceIdx());
Chris Daltona13078c2019-01-07 09:34:05 -0700369 resources->appendDrawPathInstance().set(devBounds, devBounds45, devToAtlasOffset,
Brian Osmanc6444d22019-01-09 16:30:12 -0500370 SkPMColor4f_toFP16(fColor), doEvenOddFill);
Chris Daltona13078c2019-01-07 09:34:05 -0700371
Chris Daltonaaa77c12019-01-07 17:45:36 -0700372 if (fDoCachePathMask) {
373 SkASSERT(fCacheEntry);
Chris Daltona13078c2019-01-07 09:34:05 -0700374 SkASSERT(!fCacheEntry->cachedAtlas());
Chris Daltonaaa77c12019-01-07 17:45:36 -0700375 SkASSERT(fShapeConservativeIBounds == fMaskDevIBounds);
Chris Daltona13078c2019-01-07 09:34:05 -0700376 fCacheEntry->setCoverageCountAtlas(onFlushRP, atlas, devToAtlasOffset, devBounds,
377 devBounds45, devIBounds, fCachedMaskShift);
378 }
379 }
380}
381
Brian Salomon7eae3e02018-08-07 14:02:38 +0000382inline void GrCCDrawPathsOp::recordInstance(GrTextureProxy* atlasProxy, int instanceIdx) {
Chris Dalton4da70192018-06-18 09:51:36 -0600383 if (fInstanceRanges.empty()) {
384 fInstanceRanges.push_back({atlasProxy, instanceIdx});
385 return;
386 }
387 if (fInstanceRanges.back().fAtlasProxy != atlasProxy) {
388 fInstanceRanges.back().fEndInstanceIdx = instanceIdx;
389 fInstanceRanges.push_back({atlasProxy, instanceIdx});
390 return;
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600391 }
392}
393
Brian Salomon588cec72018-11-14 13:56:37 -0500394void GrCCDrawPathsOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Chris Daltond7e22272018-05-23 10:17:17 -0600395 SkASSERT(fOwningPerOpListPaths);
Chris Daltonf104fec2018-05-22 16:17:48 -0600396
Chris Daltond7e22272018-05-23 10:17:17 -0600397 const GrCCPerFlushResources* resources = fOwningPerOpListPaths->fFlushResources.get();
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600398 if (!resources) {
399 return; // Setup failed.
400 }
401
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600402 GrPipeline::InitArgs initArgs;
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600403 initArgs.fCaps = &flushState->caps();
404 initArgs.fResourceProvider = flushState->resourceProvider();
405 initArgs.fDstProxy = flushState->drawOpArgs().fDstProxy;
Brian Salomon49348902018-06-26 09:12:38 -0400406 auto clip = flushState->detachAppliedClip();
407 GrPipeline::FixedDynamicState fixedDynamicState(clip.scissorState().rect());
408 GrPipeline pipeline(initArgs, std::move(fProcessors), std::move(clip));
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600409
410 int baseInstance = fBaseInstance;
Chris Dalton4da70192018-06-18 09:51:36 -0600411 SkASSERT(baseInstance >= 0); // Make sure setupResources() has been called.
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600412
Chris Dalton4c458b12018-06-16 17:22:59 -0600413 for (const InstanceRange& range : fInstanceRanges) {
414 SkASSERT(range.fEndInstanceIdx > baseInstance);
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600415
Brian Salomon7eae3e02018-08-07 14:02:38 +0000416 GrCCPathProcessor pathProc(range.fAtlasProxy, fViewMatrixIfUsingLocalCoords);
417 GrTextureProxy* atlasProxy = range.fAtlasProxy;
418 fixedDynamicState.fPrimitiveProcessorTextures = &atlasProxy;
Brian Salomon49348902018-06-26 09:12:38 -0400419 pathProc.drawPaths(flushState, pipeline, &fixedDynamicState, *resources, baseInstance,
420 range.fEndInstanceIdx, this->bounds());
Chris Dalton4c458b12018-06-16 17:22:59 -0600421
422 baseInstance = range.fEndInstanceIdx;
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600423 }
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600424}