blob: be5290e4fe568cd7c60dc836e4cf94f0d78c0953 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/ccpr/GrCCDrawPathsOp.h"
Chris Dalton351e80c2019-01-06 22:51:00 -07009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/private/GrRecordingContext.h"
11#include "src/gpu/GrMemoryPool.h"
12#include "src/gpu/GrOpFlushState.h"
13#include "src/gpu/GrRecordingContextPriv.h"
14#include "src/gpu/ccpr/GrCCPathCache.h"
15#include "src/gpu/ccpr/GrCCPerFlushResources.h"
16#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
Chris Dalton8610e9c2019-05-09 11:07:10 -060017#include "src/gpu/ccpr/GrOctoBounds.h"
Chris Dalton5ba36ba2018-05-09 01:08:38 -060018
Chris Dalton1c548942018-05-22 13:09:48 -060019static bool has_coord_transforms(const GrPaint& paint) {
20 GrFragmentProcessor::Iter iter(paint);
21 while (const GrFragmentProcessor* fp = iter.next()) {
22 if (!fp->coordTransforms().empty()) {
23 return true;
24 }
25 }
26 return false;
27}
28
Chris Dalton09a7bb22018-08-31 19:53:15 +080029std::unique_ptr<GrCCDrawPathsOp> GrCCDrawPathsOp::Make(
Robert Phillips6f0e02f2019-02-13 11:02:28 -050030 GrRecordingContext* context, const SkIRect& clipIBounds, const SkMatrix& m,
31 const GrShape& shape, GrPaint&& paint) {
Chris Dalton09a7bb22018-08-31 19:53:15 +080032 SkRect conservativeDevBounds;
33 m.mapRect(&conservativeDevBounds, shape.bounds());
34
35 const SkStrokeRec& stroke = shape.style().strokeRec();
36 float strokeDevWidth = 0;
37 float conservativeInflationRadius = 0;
38 if (!stroke.isFillStyle()) {
Chris Dalton82de18f2018-09-12 17:24:09 -060039 strokeDevWidth = GrCoverageCountingPathRenderer::GetStrokeDevWidth(
40 m, stroke, &conservativeInflationRadius);
Chris Dalton09a7bb22018-08-31 19:53:15 +080041 conservativeDevBounds.outset(conservativeInflationRadius, conservativeInflationRadius);
42 }
43
44 std::unique_ptr<GrCCDrawPathsOp> op;
45 float conservativeSize = SkTMax(conservativeDevBounds.height(), conservativeDevBounds.width());
Chris Dalton82de18f2018-09-12 17:24:09 -060046 if (conservativeSize > GrCoverageCountingPathRenderer::kPathCropThreshold) {
Chris Dalton09a7bb22018-08-31 19:53:15 +080047 // The path is too large. Crop it or analytic AA can run out of fp32 precision.
48 SkPath croppedDevPath;
49 shape.asPath(&croppedDevPath);
50 croppedDevPath.transform(m, &croppedDevPath);
51
52 SkIRect cropBox = clipIBounds;
53 GrShape croppedDevShape;
54 if (stroke.isFillStyle()) {
55 GrCoverageCountingPathRenderer::CropPath(croppedDevPath, cropBox, &croppedDevPath);
56 croppedDevShape = GrShape(croppedDevPath);
57 conservativeDevBounds = croppedDevShape.bounds();
58 } else {
59 int r = SkScalarCeilToInt(conservativeInflationRadius);
60 cropBox.outset(r, r);
61 GrCoverageCountingPathRenderer::CropPath(croppedDevPath, cropBox, &croppedDevPath);
62 SkStrokeRec devStroke = stroke;
63 devStroke.setStrokeStyle(strokeDevWidth);
64 croppedDevShape = GrShape(croppedDevPath, GrStyle(devStroke, nullptr));
65 conservativeDevBounds = croppedDevPath.getBounds();
66 conservativeDevBounds.outset(conservativeInflationRadius, conservativeInflationRadius);
67 }
68
69 // FIXME: This breaks local coords: http://skbug.com/8003
70 return InternalMake(context, clipIBounds, SkMatrix::I(), croppedDevShape, strokeDevWidth,
71 conservativeDevBounds, std::move(paint));
72 }
73
74 return InternalMake(context, clipIBounds, m, shape, strokeDevWidth, conservativeDevBounds,
75 std::move(paint));
76}
77
78std::unique_ptr<GrCCDrawPathsOp> GrCCDrawPathsOp::InternalMake(
Robert Phillips6f0e02f2019-02-13 11:02:28 -050079 GrRecordingContext* context, const SkIRect& clipIBounds, const SkMatrix& m,
80 const GrShape& shape, float strokeDevWidth, const SkRect& conservativeDevBounds,
81 GrPaint&& paint) {
Chris Dalton82de18f2018-09-12 17:24:09 -060082 // The path itself should have been cropped if larger than kPathCropThreshold. If it had a
83 // stroke, that would have further inflated its draw bounds.
84 SkASSERT(SkTMax(conservativeDevBounds.height(), conservativeDevBounds.width()) <
85 GrCoverageCountingPathRenderer::kPathCropThreshold +
86 GrCoverageCountingPathRenderer::kMaxBoundsInflationFromStroke*2 + 1);
87
Chris Dalton09a7bb22018-08-31 19:53:15 +080088 SkIRect shapeConservativeIBounds;
89 conservativeDevBounds.roundOut(&shapeConservativeIBounds);
Chris Daltona8429cf2018-06-22 11:43:31 -060090
91 SkIRect maskDevIBounds;
Chris Daltonaaa77c12019-01-07 17:45:36 -070092 if (!maskDevIBounds.intersect(clipIBounds, shapeConservativeIBounds)) {
93 return nullptr;
Chris Dalton42c21152018-06-13 15:28:19 -060094 }
Robert Phillipsc994a932018-06-19 13:09:54 -040095
Robert Phillips9da87e02019-02-04 13:26:26 -050096 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Chris Dalton09a7bb22018-08-31 19:53:15 +080097 return pool->allocate<GrCCDrawPathsOp>(m, shape, strokeDevWidth, shapeConservativeIBounds,
Chris Daltonaaa77c12019-01-07 17:45:36 -070098 maskDevIBounds, conservativeDevBounds, std::move(paint));
Chris Dalton42c21152018-06-13 15:28:19 -060099}
100
Chris Dalton09a7bb22018-08-31 19:53:15 +0800101GrCCDrawPathsOp::GrCCDrawPathsOp(const SkMatrix& m, const GrShape& shape, float strokeDevWidth,
102 const SkIRect& shapeConservativeIBounds,
Chris Daltonaaa77c12019-01-07 17:45:36 -0700103 const SkIRect& maskDevIBounds, const SkRect& conservativeDevBounds,
104 GrPaint&& paint)
Chris Dalton4bfb50b2018-05-21 09:10:53 -0600105 : GrDrawOp(ClassID())
Chris Daltond7e22272018-05-23 10:17:17 -0600106 , fViewMatrixIfUsingLocalCoords(has_coord_transforms(paint) ? m : SkMatrix::I())
Chris Daltonaaa77c12019-01-07 17:45:36 -0700107 , fDraws(m, shape, strokeDevWidth, shapeConservativeIBounds, maskDevIBounds,
Brian Osmancf860852018-10-31 14:04:39 -0400108 paint.getColor4f())
Chris Dalton42c21152018-06-13 15:28:19 -0600109 , fProcessors(std::move(paint)) { // Paint must be moved after fetching its color above.
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600110 SkDEBUGCODE(fBaseInstance = -1);
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600111 // FIXME: intersect with clip bounds to (hopefully) improve batching.
112 // (This is nontrivial due to assumptions in generating the octagon cover geometry.)
Chris Dalton09a7bb22018-08-31 19:53:15 +0800113 this->setBounds(conservativeDevBounds, GrOp::HasAABloat::kYes, GrOp::IsZeroArea::kNo);
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600114}
115
116GrCCDrawPathsOp::~GrCCDrawPathsOp() {
Chris Daltond7e22272018-05-23 10:17:17 -0600117 if (fOwningPerOpListPaths) {
Chris Daltonb68bcc42018-09-14 00:44:22 -0600118 // Remove the list's dangling pointer to this Op before deleting it.
Chris Daltond7e22272018-05-23 10:17:17 -0600119 fOwningPerOpListPaths->fDrawOps.remove(this);
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600120 }
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600121}
122
Chris Daltona8429cf2018-06-22 11:43:31 -0600123GrCCDrawPathsOp::SingleDraw::SingleDraw(const SkMatrix& m, const GrShape& shape,
Chris Dalton09a7bb22018-08-31 19:53:15 +0800124 float strokeDevWidth,
125 const SkIRect& shapeConservativeIBounds,
Chris Daltonaaa77c12019-01-07 17:45:36 -0700126 const SkIRect& maskDevIBounds, const SkPMColor4f& color)
Chris Daltona8429cf2018-06-22 11:43:31 -0600127 : fMatrix(m)
Chris Dalton644341a2018-06-18 19:14:16 -0600128 , fShape(shape)
Chris Dalton09a7bb22018-08-31 19:53:15 +0800129 , fStrokeDevWidth(strokeDevWidth)
130 , fShapeConservativeIBounds(shapeConservativeIBounds)
Chris Daltona8429cf2018-06-22 11:43:31 -0600131 , fMaskDevIBounds(maskDevIBounds)
Chris Daltona8429cf2018-06-22 11:43:31 -0600132 , fColor(color) {
Chris Dalton644341a2018-06-18 19:14:16 -0600133#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
134 if (fShape.hasUnstyledKey()) {
135 // On AOSP we round view matrix translates to integer values for cachable paths. We do this
136 // to match HWUI's cache hit ratio, which doesn't consider the matrix when caching paths.
137 fMatrix.setTranslateX(SkScalarRoundToScalar(fMatrix.getTranslateX()));
138 fMatrix.setTranslateY(SkScalarRoundToScalar(fMatrix.getTranslateY()));
139 }
140#endif
141}
142
Brian Osman5ced0bf2019-03-15 10:15:29 -0400143GrProcessorSet::Analysis GrCCDrawPathsOp::finalize(const GrCaps& caps, const GrAppliedClip* clip,
144 GrFSAAType fsaaType, GrClampType clampType) {
Chris Dalton4da70192018-06-18 09:51:36 -0600145 SkASSERT(1 == fNumDraws); // There should only be one single path draw in this Op right now.
Brian Osman5ced0bf2019-03-15 10:15:29 -0400146 return fDraws.head().finalize(caps, clip, fsaaType, clampType, &fProcessors);
Chris Daltona13078c2019-01-07 09:34:05 -0700147}
Chris Dalton09a7bb22018-08-31 19:53:15 +0800148
Chris Dalton4b62aed2019-01-15 11:53:00 -0700149GrProcessorSet::Analysis GrCCDrawPathsOp::SingleDraw::finalize(
Brian Osman5ced0bf2019-03-15 10:15:29 -0400150 const GrCaps& caps, const GrAppliedClip* clip, GrFSAAType fsaaType, GrClampType clampType,
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700151 GrProcessorSet* processors) {
Chris Daltona13078c2019-01-07 09:34:05 -0700152 const GrProcessorSet::Analysis& analysis = processors->finalize(
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700153 fColor, GrProcessorAnalysisCoverage::kSingleChannel, clip,
Brian Osman5ced0bf2019-03-15 10:15:29 -0400154 &GrUserStencilSettings::kUnused, fsaaType, caps, clampType, &fColor);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800155
156 // Lines start looking jagged when they get thinner than 1px. For thin strokes it looks better
157 // if we can convert them to hairline (i.e., inflate the stroke width to 1px), and instead
158 // reduce the opacity to create the illusion of thin-ness. This strategy also helps reduce
159 // artifacts from coverage dilation when there are self intersections.
160 if (analysis.isCompatibleWithCoverageAsAlpha() &&
Chris Daltona13078c2019-01-07 09:34:05 -0700161 !fShape.style().strokeRec().isFillStyle() && fStrokeDevWidth < 1) {
Chris Dalton09a7bb22018-08-31 19:53:15 +0800162 // Modifying the shape affects its cache key. The draw can't have a cache entry yet or else
163 // our next step would invalidate it.
Chris Daltona13078c2019-01-07 09:34:05 -0700164 SkASSERT(!fCacheEntry);
165 SkASSERT(SkStrokeRec::kStroke_Style == fShape.style().strokeRec().getStyle());
Chris Dalton09a7bb22018-08-31 19:53:15 +0800166
167 SkPath path;
Chris Daltona13078c2019-01-07 09:34:05 -0700168 fShape.asPath(&path);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800169
170 // Create a hairline version of our stroke.
Chris Daltona13078c2019-01-07 09:34:05 -0700171 SkStrokeRec hairlineStroke = fShape.style().strokeRec();
Chris Dalton09a7bb22018-08-31 19:53:15 +0800172 hairlineStroke.setStrokeStyle(0);
173
174 // 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 -0700175 float coverage = fStrokeDevWidth;
Chris Dalton09a7bb22018-08-31 19:53:15 +0800176
Chris Daltona13078c2019-01-07 09:34:05 -0700177 fShape = GrShape(path, GrStyle(hairlineStroke, nullptr));
178 fStrokeDevWidth = 1;
Brian Osman1be2b7c2018-10-29 16:07:15 -0400179
Chris Dalton09a7bb22018-08-31 19:53:15 +0800180 // fShapeConservativeIBounds already accounted for this possibility of inflating the stroke.
Chris Daltona13078c2019-01-07 09:34:05 -0700181 fColor = fColor * coverage;
Chris Dalton09a7bb22018-08-31 19:53:15 +0800182 }
183
Chris Dalton4b62aed2019-01-15 11:53:00 -0700184 return analysis;
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600185}
186
Brian Salomon7eae3e02018-08-07 14:02:38 +0000187GrOp::CombineResult GrCCDrawPathsOp::onCombineIfPossible(GrOp* op, const GrCaps&) {
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600188 GrCCDrawPathsOp* that = op->cast<GrCCDrawPathsOp>();
Chris Daltond7e22272018-05-23 10:17:17 -0600189 SkASSERT(fOwningPerOpListPaths);
Chris Dalton4bfb50b2018-05-21 09:10:53 -0600190 SkASSERT(fNumDraws);
Chris Daltond7e22272018-05-23 10:17:17 -0600191 SkASSERT(!that->fOwningPerOpListPaths || that->fOwningPerOpListPaths == fOwningPerOpListPaths);
Chris Dalton4bfb50b2018-05-21 09:10:53 -0600192 SkASSERT(that->fNumDraws);
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600193
Brian Osman9aa30c62018-07-02 15:21:46 -0400194 if (fProcessors != that->fProcessors ||
Chris Dalton1c548942018-05-22 13:09:48 -0600195 fViewMatrixIfUsingLocalCoords != that->fViewMatrixIfUsingLocalCoords) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000196 return CombineResult::kCannotCombine;
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600197 }
198
Chris Daltond7e22272018-05-23 10:17:17 -0600199 fDraws.append(std::move(that->fDraws), &fOwningPerOpListPaths->fAllocator);
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600200
Chris Dalton4bfb50b2018-05-21 09:10:53 -0600201 SkDEBUGCODE(fNumDraws += that->fNumDraws);
202 SkDEBUGCODE(that->fNumDraws = 0);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000203 return CombineResult::kMerged;
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600204}
205
Brian Salomon348a0372018-10-31 10:42:18 -0400206void GrCCDrawPathsOp::addToOwningPerOpListPaths(sk_sp<GrCCPerOpListPaths> owningPerOpListPaths) {
Chris Daltonf104fec2018-05-22 16:17:48 -0600207 SkASSERT(1 == fNumDraws);
Chris Daltond7e22272018-05-23 10:17:17 -0600208 SkASSERT(!fOwningPerOpListPaths);
Chris Daltonb68bcc42018-09-14 00:44:22 -0600209 fOwningPerOpListPaths = std::move(owningPerOpListPaths);
210 fOwningPerOpListPaths->fDrawOps.addToTail(this);
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600211}
212
Chris Dalton4da70192018-06-18 09:51:36 -0600213void GrCCDrawPathsOp::accountForOwnPaths(GrCCPathCache* pathCache,
214 GrOnFlushResourceProvider* onFlushRP,
Chris Dalton4da70192018-06-18 09:51:36 -0600215 GrCCPerFlushResourceSpecs* specs) {
Chris Daltona13078c2019-01-07 09:34:05 -0700216 for (SingleDraw& draw : fDraws) {
217 draw.accountForOwnPath(pathCache, onFlushRP, specs);
218 }
219}
220
221void GrCCDrawPathsOp::SingleDraw::accountForOwnPath(
222 GrCCPathCache* pathCache, GrOnFlushResourceProvider* onFlushRP,
223 GrCCPerFlushResourceSpecs* specs) {
Chris Dalton351e80c2019-01-06 22:51:00 -0700224 using CoverageType = GrCCAtlas::CoverageType;
Chris Dalton4da70192018-06-18 09:51:36 -0600225
Chris Daltona13078c2019-01-07 09:34:05 -0700226 SkPath path;
227 fShape.asPath(&path);
Chris Dalton4da70192018-06-18 09:51:36 -0600228
Chris Daltona13078c2019-01-07 09:34:05 -0700229 SkASSERT(!fCacheEntry);
Chris Daltona2b5b642018-06-24 13:08:57 -0600230
Chris Daltona13078c2019-01-07 09:34:05 -0700231 if (pathCache) {
Chris Daltonaaa77c12019-01-07 17:45:36 -0700232 fCacheEntry =
233 pathCache->find(onFlushRP, fShape, fMaskDevIBounds, fMatrix, &fCachedMaskShift);
Chris Dalton4bfb50b2018-05-21 09:10:53 -0600234 }
Chris Daltona13078c2019-01-07 09:34:05 -0700235
236 if (fCacheEntry) {
237 if (const GrCCCachedAtlas* cachedAtlas = fCacheEntry->cachedAtlas()) {
238 SkASSERT(cachedAtlas->getOnFlushProxy());
239 if (CoverageType::kA8_LiteralCoverage == cachedAtlas->coverageType()) {
240 ++specs->fNumCachedPaths;
241 } else {
242 // Suggest that this path be copied to a literal coverage atlas, to save memory.
243 // (The client may decline this copy via DoCopiesToA8Coverage::kNo.)
244 int idx = (fShape.style().strokeRec().isFillStyle())
245 ? GrCCPerFlushResourceSpecs::kFillIdx
246 : GrCCPerFlushResourceSpecs::kStrokeIdx;
247 ++specs->fNumCopiedPaths[idx];
248 specs->fCopyPathStats[idx].statPath(path);
249 specs->fCopyAtlasSpecs.accountForSpace(fCacheEntry->width(), fCacheEntry->height());
250 fDoCopyToA8Coverage = true;
251 }
252 return;
253 }
254
Chris Daltonaaa77c12019-01-07 17:45:36 -0700255 if (this->shouldCachePathMask(onFlushRP->caps()->maxRenderTargetSize())) {
256 fDoCachePathMask = true;
257 // We don't cache partial masks; ensure the bounds include the entire path.
258 fMaskDevIBounds = fShapeConservativeIBounds;
Chris Daltona13078c2019-01-07 09:34:05 -0700259 }
260 }
261
Chris Daltonaaa77c12019-01-07 17:45:36 -0700262 // Plan on rendering this path in a new atlas.
Chris Daltona13078c2019-01-07 09:34:05 -0700263 int idx = (fShape.style().strokeRec().isFillStyle())
264 ? GrCCPerFlushResourceSpecs::kFillIdx
265 : GrCCPerFlushResourceSpecs::kStrokeIdx;
266 ++specs->fNumRenderedPaths[idx];
267 specs->fRenderedPathStats[idx].statPath(path);
Chris Daltonaaa77c12019-01-07 17:45:36 -0700268 specs->fRenderedAtlasSpecs.accountForSpace(fMaskDevIBounds.width(), fMaskDevIBounds.height());
269}
270
271bool GrCCDrawPathsOp::SingleDraw::shouldCachePathMask(int maxRenderTargetSize) const {
272 SkASSERT(fCacheEntry);
273 SkASSERT(!fCacheEntry->cachedAtlas());
274 if (fCacheEntry->hitCount() <= 1) {
275 return false; // Don't cache a path mask until at least its second hit.
276 }
277
278 int shapeMaxDimension = SkTMax(fShapeConservativeIBounds.height(),
279 fShapeConservativeIBounds.width());
280 if (shapeMaxDimension > maxRenderTargetSize) {
281 return false; // This path isn't cachable.
282 }
283
284 int64_t shapeArea = sk_64_mul(fShapeConservativeIBounds.height(),
285 fShapeConservativeIBounds.width());
286 if (shapeArea < 100*100) {
287 // If a path is small enough, we might as well try to render and cache the entire thing, no
288 // matter how much of it is actually visible.
289 return true;
290 }
291
292 // The hitRect should already be contained within the shape's bounds, but we still intersect it
293 // because it's possible for edges very near pixel boundaries (e.g., 0.999999), to round out
294 // inconsistently, depending on the integer translation values and fp32 precision.
295 SkIRect hitRect = fCacheEntry->hitRect().makeOffset(fCachedMaskShift.x(), fCachedMaskShift.y());
296 hitRect.intersect(fShapeConservativeIBounds);
297
298 // Render and cache the entire path mask if we see enough of it to justify rendering all the
299 // pixels. Our criteria for "enough" is that we must have seen at least 50% of the path in the
300 // past, and in this particular draw we must see at least 10% of it.
301 int64_t hitArea = sk_64_mul(hitRect.height(), hitRect.width());
302 int64_t drawArea = sk_64_mul(fMaskDevIBounds.height(), fMaskDevIBounds.width());
303 return hitArea*2 >= shapeArea && drawArea*10 >= shapeArea;
Chris Dalton4bfb50b2018-05-21 09:10:53 -0600304}
305
Chris Dalton351e80c2019-01-06 22:51:00 -0700306void GrCCDrawPathsOp::setupResources(
307 GrCCPathCache* pathCache, GrOnFlushResourceProvider* onFlushRP,
308 GrCCPerFlushResources* resources, DoCopiesToA8Coverage doCopies) {
Chris Dalton4bfb50b2018-05-21 09:10:53 -0600309 SkASSERT(fNumDraws > 0);
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600310 SkASSERT(-1 == fBaseInstance);
Chris Daltondaef06a2018-05-23 17:11:09 -0600311 fBaseInstance = resources->nextPathInstanceIdx();
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600312
Chris Dalton4da70192018-06-18 09:51:36 -0600313 for (SingleDraw& draw : fDraws) {
Chris Daltona13078c2019-01-07 09:34:05 -0700314 draw.setupResources(pathCache, onFlushRP, resources, doCopies, this);
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600315 }
316
Chris Dalton4da70192018-06-18 09:51:36 -0600317 if (!fInstanceRanges.empty()) {
318 fInstanceRanges.back().fEndInstanceIdx = resources->nextPathInstanceIdx();
319 }
320}
321
Chris Daltona13078c2019-01-07 09:34:05 -0700322void GrCCDrawPathsOp::SingleDraw::setupResources(
323 GrCCPathCache* pathCache, GrOnFlushResourceProvider* onFlushRP,
324 GrCCPerFlushResources* resources, DoCopiesToA8Coverage doCopies, GrCCDrawPathsOp* op) {
325 using DoEvenOddFill = GrCCPathProcessor::DoEvenOddFill;
326
327 SkPath path;
328 fShape.asPath(&path);
329
330 auto doEvenOddFill = DoEvenOddFill(fShape.style().strokeRec().isFillStyle() &&
331 SkPath::kEvenOdd_FillType == path.getFillType());
332 SkASSERT(SkPath::kEvenOdd_FillType == path.getFillType() ||
333 SkPath::kWinding_FillType == path.getFillType());
334
335 if (fCacheEntry) {
336 // Does the path already exist in a cached atlas texture?
337 if (fCacheEntry->cachedAtlas()) {
338 SkASSERT(fCacheEntry->cachedAtlas()->getOnFlushProxy());
339 if (DoCopiesToA8Coverage::kYes == doCopies && fDoCopyToA8Coverage) {
340 resources->upgradeEntryToLiteralCoverageAtlas(pathCache, onFlushRP,
341 fCacheEntry.get(), doEvenOddFill);
342 SkASSERT(fCacheEntry->cachedAtlas());
343 SkASSERT(GrCCAtlas::CoverageType::kA8_LiteralCoverage
344 == fCacheEntry->cachedAtlas()->coverageType());
345 SkASSERT(fCacheEntry->cachedAtlas()->getOnFlushProxy());
346 }
Chris Daltonaaa77c12019-01-07 17:45:36 -0700347#if 0
348 // Simple color manipulation to visualize cached paths.
349 fColor = (GrCCAtlas::CoverageType::kA8_LiteralCoverage
350 == fCacheEntry->cachedAtlas()->coverageType())
351 ? SkPMColor4f{0,0,.25,.25} : SkPMColor4f{0,.25,0,.25};
352#endif
Chris Daltona13078c2019-01-07 09:34:05 -0700353 op->recordInstance(fCacheEntry->cachedAtlas()->getOnFlushProxy(),
354 resources->nextPathInstanceIdx());
Chris Daltona13078c2019-01-07 09:34:05 -0700355 resources->appendDrawPathInstance().set(*fCacheEntry, fCachedMaskShift,
Brian Osmanc6444d22019-01-09 16:30:12 -0500356 SkPMColor4f_toFP16(fColor));
Chris Daltona13078c2019-01-07 09:34:05 -0700357 return;
358 }
359 }
360
361 // Render the raw path into a coverage count atlas. renderShapeInAtlas() gives us two tight
362 // bounding boxes: One in device space, as well as a second one rotated an additional 45
363 // degrees. The path vertex shader uses these two bounding boxes to generate an octagon that
364 // circumscribes the path.
Chris Dalton8610e9c2019-05-09 11:07:10 -0600365 GrOctoBounds octoBounds;
Chris Daltona13078c2019-01-07 09:34:05 -0700366 SkIRect devIBounds;
367 SkIVector devToAtlasOffset;
368 if (auto atlas = resources->renderShapeInAtlas(
Chris Dalton8610e9c2019-05-09 11:07:10 -0600369 fMaskDevIBounds, fMatrix, fShape, fStrokeDevWidth, &octoBounds, &devIBounds,
370 &devToAtlasOffset)) {
Chris Daltona13078c2019-01-07 09:34:05 -0700371 op->recordInstance(atlas->textureProxy(), resources->nextPathInstanceIdx());
Chris Dalton8610e9c2019-05-09 11:07:10 -0600372 resources->appendDrawPathInstance().set(
373 octoBounds, devToAtlasOffset, SkPMColor4f_toFP16(fColor), doEvenOddFill);
Chris Daltona13078c2019-01-07 09:34:05 -0700374
Chris Daltonaaa77c12019-01-07 17:45:36 -0700375 if (fDoCachePathMask) {
376 SkASSERT(fCacheEntry);
Chris Daltona13078c2019-01-07 09:34:05 -0700377 SkASSERT(!fCacheEntry->cachedAtlas());
Chris Daltonaaa77c12019-01-07 17:45:36 -0700378 SkASSERT(fShapeConservativeIBounds == fMaskDevIBounds);
Chris Dalton8610e9c2019-05-09 11:07:10 -0600379 fCacheEntry->setCoverageCountAtlas(
380 onFlushRP, atlas, devToAtlasOffset, octoBounds, devIBounds, fCachedMaskShift);
Chris Daltona13078c2019-01-07 09:34:05 -0700381 }
382 }
383}
384
Brian Salomon7eae3e02018-08-07 14:02:38 +0000385inline void GrCCDrawPathsOp::recordInstance(GrTextureProxy* atlasProxy, int instanceIdx) {
Chris Dalton4da70192018-06-18 09:51:36 -0600386 if (fInstanceRanges.empty()) {
387 fInstanceRanges.push_back({atlasProxy, instanceIdx});
388 return;
389 }
390 if (fInstanceRanges.back().fAtlasProxy != atlasProxy) {
391 fInstanceRanges.back().fEndInstanceIdx = instanceIdx;
392 fInstanceRanges.push_back({atlasProxy, instanceIdx});
393 return;
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600394 }
395}
396
Brian Salomon588cec72018-11-14 13:56:37 -0500397void GrCCDrawPathsOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Chris Daltond7e22272018-05-23 10:17:17 -0600398 SkASSERT(fOwningPerOpListPaths);
Chris Daltonf104fec2018-05-22 16:17:48 -0600399
Chris Daltond7e22272018-05-23 10:17:17 -0600400 const GrCCPerFlushResources* resources = fOwningPerOpListPaths->fFlushResources.get();
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600401 if (!resources) {
402 return; // Setup failed.
403 }
404
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600405 GrPipeline::InitArgs initArgs;
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600406 initArgs.fCaps = &flushState->caps();
407 initArgs.fResourceProvider = flushState->resourceProvider();
408 initArgs.fDstProxy = flushState->drawOpArgs().fDstProxy;
Brian Salomon49348902018-06-26 09:12:38 -0400409 auto clip = flushState->detachAppliedClip();
410 GrPipeline::FixedDynamicState fixedDynamicState(clip.scissorState().rect());
411 GrPipeline pipeline(initArgs, std::move(fProcessors), std::move(clip));
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600412
413 int baseInstance = fBaseInstance;
Chris Dalton4da70192018-06-18 09:51:36 -0600414 SkASSERT(baseInstance >= 0); // Make sure setupResources() has been called.
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600415
Chris Dalton4c458b12018-06-16 17:22:59 -0600416 for (const InstanceRange& range : fInstanceRanges) {
417 SkASSERT(range.fEndInstanceIdx > baseInstance);
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600418
Chris Daltonf91b7552019-04-29 16:21:18 -0600419 const GrTextureProxy* atlas = range.fAtlasProxy;
420 SkASSERT(atlas->isInstantiated());
421
422 GrCCPathProcessor pathProc(
423 atlas->peekTexture(), atlas->origin(), fViewMatrixIfUsingLocalCoords);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000424 GrTextureProxy* atlasProxy = range.fAtlasProxy;
425 fixedDynamicState.fPrimitiveProcessorTextures = &atlasProxy;
Brian Salomon49348902018-06-26 09:12:38 -0400426 pathProc.drawPaths(flushState, pipeline, &fixedDynamicState, *resources, baseInstance,
427 range.fEndInstanceIdx, this->bounds());
Chris Dalton4c458b12018-06-16 17:22:59 -0600428
429 baseInstance = range.fEndInstanceIdx;
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600430 }
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600431}