Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/ccpr/GrCCDrawPathsOp.h" |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #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 Dalton | 8610e9c | 2019-05-09 11:07:10 -0600 | [diff] [blame] | 17 | #include "src/gpu/ccpr/GrOctoBounds.h" |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 18 | |
Chris Dalton | 1c54894 | 2018-05-22 13:09:48 -0600 | [diff] [blame] | 19 | static 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 Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 29 | std::unique_ptr<GrCCDrawPathsOp> GrCCDrawPathsOp::Make( |
Robert Phillips | 6f0e02f | 2019-02-13 11:02:28 -0500 | [diff] [blame] | 30 | GrRecordingContext* context, const SkIRect& clipIBounds, const SkMatrix& m, |
| 31 | const GrShape& shape, GrPaint&& paint) { |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 32 | 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 Dalton | 82de18f | 2018-09-12 17:24:09 -0600 | [diff] [blame] | 39 | strokeDevWidth = GrCoverageCountingPathRenderer::GetStrokeDevWidth( |
| 40 | m, stroke, &conservativeInflationRadius); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 41 | conservativeDevBounds.outset(conservativeInflationRadius, conservativeInflationRadius); |
| 42 | } |
| 43 | |
| 44 | std::unique_ptr<GrCCDrawPathsOp> op; |
| 45 | float conservativeSize = SkTMax(conservativeDevBounds.height(), conservativeDevBounds.width()); |
Chris Dalton | 82de18f | 2018-09-12 17:24:09 -0600 | [diff] [blame] | 46 | if (conservativeSize > GrCoverageCountingPathRenderer::kPathCropThreshold) { |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 47 | // 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 | |
| 78 | std::unique_ptr<GrCCDrawPathsOp> GrCCDrawPathsOp::InternalMake( |
Robert Phillips | 6f0e02f | 2019-02-13 11:02:28 -0500 | [diff] [blame] | 79 | GrRecordingContext* context, const SkIRect& clipIBounds, const SkMatrix& m, |
| 80 | const GrShape& shape, float strokeDevWidth, const SkRect& conservativeDevBounds, |
| 81 | GrPaint&& paint) { |
Chris Dalton | 82de18f | 2018-09-12 17:24:09 -0600 | [diff] [blame] | 82 | // 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 Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 88 | SkIRect shapeConservativeIBounds; |
| 89 | conservativeDevBounds.roundOut(&shapeConservativeIBounds); |
Chris Dalton | a8429cf | 2018-06-22 11:43:31 -0600 | [diff] [blame] | 90 | |
| 91 | SkIRect maskDevIBounds; |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 92 | if (!maskDevIBounds.intersect(clipIBounds, shapeConservativeIBounds)) { |
| 93 | return nullptr; |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 94 | } |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 95 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 96 | GrOpMemoryPool* pool = context->priv().opMemoryPool(); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 97 | return pool->allocate<GrCCDrawPathsOp>(m, shape, strokeDevWidth, shapeConservativeIBounds, |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 98 | maskDevIBounds, conservativeDevBounds, std::move(paint)); |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 99 | } |
| 100 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 101 | GrCCDrawPathsOp::GrCCDrawPathsOp(const SkMatrix& m, const GrShape& shape, float strokeDevWidth, |
| 102 | const SkIRect& shapeConservativeIBounds, |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 103 | const SkIRect& maskDevIBounds, const SkRect& conservativeDevBounds, |
| 104 | GrPaint&& paint) |
Chris Dalton | 4bfb50b | 2018-05-21 09:10:53 -0600 | [diff] [blame] | 105 | : GrDrawOp(ClassID()) |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 106 | , fViewMatrixIfUsingLocalCoords(has_coord_transforms(paint) ? m : SkMatrix::I()) |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 107 | , fDraws(m, shape, strokeDevWidth, shapeConservativeIBounds, maskDevIBounds, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 108 | paint.getColor4f()) |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 109 | , fProcessors(std::move(paint)) { // Paint must be moved after fetching its color above. |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 110 | SkDEBUGCODE(fBaseInstance = -1); |
Chris Dalton | a6fcb76 | 2019-05-13 08:57:53 -0600 | [diff] [blame] | 111 | // If the path is clipped, CCPR will only draw the visible portion. This helps improve batching, |
| 112 | // since it eliminates the need for scissor when drawing to the main canvas. |
| 113 | // FIXME: We should parse the path right here. It will provide a tighter bounding box for us to |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 114 | // give the opsTask, as well as enabling threaded parsing when using DDL. |
Chris Dalton | a6fcb76 | 2019-05-13 08:57:53 -0600 | [diff] [blame] | 115 | SkRect clippedDrawBounds; |
| 116 | if (!clippedDrawBounds.intersect(conservativeDevBounds, SkRect::Make(maskDevIBounds))) { |
| 117 | clippedDrawBounds.setEmpty(); |
| 118 | } |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 119 | // We always have AA bloat, even in MSAA atlas mode. This is because by the time this Op comes |
| 120 | // along and draws to the main canvas, the atlas has been resolved to analytic coverage. |
Chris Dalton | a6fcb76 | 2019-05-13 08:57:53 -0600 | [diff] [blame] | 121 | this->setBounds(clippedDrawBounds, GrOp::HasAABloat::kYes, GrOp::IsZeroArea::kNo); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | GrCCDrawPathsOp::~GrCCDrawPathsOp() { |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 125 | if (fOwningPerOpsTaskPaths) { |
Chris Dalton | b68bcc4 | 2018-09-14 00:44:22 -0600 | [diff] [blame] | 126 | // Remove the list's dangling pointer to this Op before deleting it. |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 127 | fOwningPerOpsTaskPaths->fDrawOps.remove(this); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 128 | } |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 129 | } |
| 130 | |
Chris Dalton | a8429cf | 2018-06-22 11:43:31 -0600 | [diff] [blame] | 131 | GrCCDrawPathsOp::SingleDraw::SingleDraw(const SkMatrix& m, const GrShape& shape, |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 132 | float strokeDevWidth, |
| 133 | const SkIRect& shapeConservativeIBounds, |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 134 | const SkIRect& maskDevIBounds, const SkPMColor4f& color) |
Chris Dalton | a8429cf | 2018-06-22 11:43:31 -0600 | [diff] [blame] | 135 | : fMatrix(m) |
Chris Dalton | 644341a | 2018-06-18 19:14:16 -0600 | [diff] [blame] | 136 | , fShape(shape) |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 137 | , fStrokeDevWidth(strokeDevWidth) |
| 138 | , fShapeConservativeIBounds(shapeConservativeIBounds) |
Chris Dalton | a8429cf | 2018-06-22 11:43:31 -0600 | [diff] [blame] | 139 | , fMaskDevIBounds(maskDevIBounds) |
Chris Dalton | a8429cf | 2018-06-22 11:43:31 -0600 | [diff] [blame] | 140 | , fColor(color) { |
Chris Dalton | 644341a | 2018-06-18 19:14:16 -0600 | [diff] [blame] | 141 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 142 | if (fShape.hasUnstyledKey()) { |
| 143 | // On AOSP we round view matrix translates to integer values for cachable paths. We do this |
| 144 | // to match HWUI's cache hit ratio, which doesn't consider the matrix when caching paths. |
| 145 | fMatrix.setTranslateX(SkScalarRoundToScalar(fMatrix.getTranslateX())); |
| 146 | fMatrix.setTranslateY(SkScalarRoundToScalar(fMatrix.getTranslateY())); |
| 147 | } |
| 148 | #endif |
| 149 | } |
| 150 | |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 151 | GrProcessorSet::Analysis GrCCDrawPathsOp::finalize( |
| 152 | const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage, |
| 153 | GrClampType clampType) { |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 154 | SkASSERT(1 == fNumDraws); // There should only be one single path draw in this Op right now. |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 155 | return fDraws.head().finalize(caps, clip, hasMixedSampledCoverage, clampType, &fProcessors); |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 156 | } |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 157 | |
Chris Dalton | 4b62aed | 2019-01-15 11:53:00 -0700 | [diff] [blame] | 158 | GrProcessorSet::Analysis GrCCDrawPathsOp::SingleDraw::finalize( |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 159 | const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage, GrClampType |
| 160 | clampType, GrProcessorSet* processors) { |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 161 | const GrProcessorSet::Analysis& analysis = processors->finalize( |
Chris Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 162 | fColor, GrProcessorAnalysisCoverage::kSingleChannel, clip, |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 163 | &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps, clampType, &fColor); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 164 | |
| 165 | // Lines start looking jagged when they get thinner than 1px. For thin strokes it looks better |
| 166 | // if we can convert them to hairline (i.e., inflate the stroke width to 1px), and instead |
| 167 | // reduce the opacity to create the illusion of thin-ness. This strategy also helps reduce |
| 168 | // artifacts from coverage dilation when there are self intersections. |
| 169 | if (analysis.isCompatibleWithCoverageAsAlpha() && |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 170 | !fShape.style().strokeRec().isFillStyle() && fStrokeDevWidth < 1) { |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 171 | // Modifying the shape affects its cache key. The draw can't have a cache entry yet or else |
| 172 | // our next step would invalidate it. |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 173 | SkASSERT(!fCacheEntry); |
| 174 | SkASSERT(SkStrokeRec::kStroke_Style == fShape.style().strokeRec().getStyle()); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 175 | |
| 176 | SkPath path; |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 177 | fShape.asPath(&path); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 178 | |
| 179 | // Create a hairline version of our stroke. |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 180 | SkStrokeRec hairlineStroke = fShape.style().strokeRec(); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 181 | hairlineStroke.setStrokeStyle(0); |
| 182 | |
| 183 | // How transparent does a 1px stroke have to be in order to appear as thin as the real one? |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 184 | float coverage = fStrokeDevWidth; |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 185 | |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 186 | fShape = GrShape(path, GrStyle(hairlineStroke, nullptr)); |
| 187 | fStrokeDevWidth = 1; |
Brian Osman | 1be2b7c | 2018-10-29 16:07:15 -0400 | [diff] [blame] | 188 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 189 | // fShapeConservativeIBounds already accounted for this possibility of inflating the stroke. |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 190 | fColor = fColor * coverage; |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 191 | } |
| 192 | |
Chris Dalton | 4b62aed | 2019-01-15 11:53:00 -0700 | [diff] [blame] | 193 | return analysis; |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 194 | } |
| 195 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 196 | GrOp::CombineResult GrCCDrawPathsOp::onCombineIfPossible(GrOp* op, const GrCaps&) { |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 197 | GrCCDrawPathsOp* that = op->cast<GrCCDrawPathsOp>(); |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 198 | SkASSERT(fOwningPerOpsTaskPaths); |
Chris Dalton | 4bfb50b | 2018-05-21 09:10:53 -0600 | [diff] [blame] | 199 | SkASSERT(fNumDraws); |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 200 | SkASSERT(!that->fOwningPerOpsTaskPaths || |
| 201 | that->fOwningPerOpsTaskPaths == fOwningPerOpsTaskPaths); |
Chris Dalton | 4bfb50b | 2018-05-21 09:10:53 -0600 | [diff] [blame] | 202 | SkASSERT(that->fNumDraws); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 203 | |
Brian Osman | 9aa30c6 | 2018-07-02 15:21:46 -0400 | [diff] [blame] | 204 | if (fProcessors != that->fProcessors || |
Chris Dalton | 1c54894 | 2018-05-22 13:09:48 -0600 | [diff] [blame] | 205 | fViewMatrixIfUsingLocalCoords != that->fViewMatrixIfUsingLocalCoords) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 206 | return CombineResult::kCannotCombine; |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 207 | } |
| 208 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 209 | fDraws.append(std::move(that->fDraws), &fOwningPerOpsTaskPaths->fAllocator); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 210 | |
Chris Dalton | 4bfb50b | 2018-05-21 09:10:53 -0600 | [diff] [blame] | 211 | SkDEBUGCODE(fNumDraws += that->fNumDraws); |
| 212 | SkDEBUGCODE(that->fNumDraws = 0); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 213 | return CombineResult::kMerged; |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 214 | } |
| 215 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 216 | void GrCCDrawPathsOp::addToOwningPerOpsTaskPaths(sk_sp<GrCCPerOpsTaskPaths> owningPerOpsTaskPaths) { |
Chris Dalton | f104fec | 2018-05-22 16:17:48 -0600 | [diff] [blame] | 217 | SkASSERT(1 == fNumDraws); |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 218 | SkASSERT(!fOwningPerOpsTaskPaths); |
| 219 | fOwningPerOpsTaskPaths = std::move(owningPerOpsTaskPaths); |
| 220 | fOwningPerOpsTaskPaths->fDrawOps.addToTail(this); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 221 | } |
| 222 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 223 | void GrCCDrawPathsOp::accountForOwnPaths(GrCCPathCache* pathCache, |
| 224 | GrOnFlushResourceProvider* onFlushRP, |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 225 | GrCCPerFlushResourceSpecs* specs) { |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 226 | for (SingleDraw& draw : fDraws) { |
| 227 | draw.accountForOwnPath(pathCache, onFlushRP, specs); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | void GrCCDrawPathsOp::SingleDraw::accountForOwnPath( |
| 232 | GrCCPathCache* pathCache, GrOnFlushResourceProvider* onFlushRP, |
| 233 | GrCCPerFlushResourceSpecs* specs) { |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 234 | using CoverageType = GrCCAtlas::CoverageType; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 235 | |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 236 | SkPath path; |
| 237 | fShape.asPath(&path); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 238 | |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 239 | SkASSERT(!fCacheEntry); |
Chris Dalton | a2b5b64 | 2018-06-24 13:08:57 -0600 | [diff] [blame] | 240 | |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 241 | if (pathCache) { |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 242 | fCacheEntry = pathCache->find( |
| 243 | onFlushRP, fShape, fMaskDevIBounds, fMatrix, &fCachedMaskShift); |
Chris Dalton | 4bfb50b | 2018-05-21 09:10:53 -0600 | [diff] [blame] | 244 | } |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 245 | |
| 246 | if (fCacheEntry) { |
| 247 | if (const GrCCCachedAtlas* cachedAtlas = fCacheEntry->cachedAtlas()) { |
| 248 | SkASSERT(cachedAtlas->getOnFlushProxy()); |
| 249 | if (CoverageType::kA8_LiteralCoverage == cachedAtlas->coverageType()) { |
| 250 | ++specs->fNumCachedPaths; |
| 251 | } else { |
| 252 | // Suggest that this path be copied to a literal coverage atlas, to save memory. |
| 253 | // (The client may decline this copy via DoCopiesToA8Coverage::kNo.) |
| 254 | int idx = (fShape.style().strokeRec().isFillStyle()) |
| 255 | ? GrCCPerFlushResourceSpecs::kFillIdx |
| 256 | : GrCCPerFlushResourceSpecs::kStrokeIdx; |
| 257 | ++specs->fNumCopiedPaths[idx]; |
| 258 | specs->fCopyPathStats[idx].statPath(path); |
| 259 | specs->fCopyAtlasSpecs.accountForSpace(fCacheEntry->width(), fCacheEntry->height()); |
| 260 | fDoCopyToA8Coverage = true; |
| 261 | } |
| 262 | return; |
| 263 | } |
| 264 | |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 265 | if (this->shouldCachePathMask(onFlushRP->caps()->maxRenderTargetSize())) { |
| 266 | fDoCachePathMask = true; |
| 267 | // We don't cache partial masks; ensure the bounds include the entire path. |
| 268 | fMaskDevIBounds = fShapeConservativeIBounds; |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 272 | // Plan on rendering this path in a new atlas. |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 273 | int idx = (fShape.style().strokeRec().isFillStyle()) |
| 274 | ? GrCCPerFlushResourceSpecs::kFillIdx |
| 275 | : GrCCPerFlushResourceSpecs::kStrokeIdx; |
| 276 | ++specs->fNumRenderedPaths[idx]; |
| 277 | specs->fRenderedPathStats[idx].statPath(path); |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 278 | specs->fRenderedAtlasSpecs.accountForSpace(fMaskDevIBounds.width(), fMaskDevIBounds.height()); |
Chris Dalton | 9bc450b | 2019-07-21 19:34:52 -0600 | [diff] [blame] | 279 | SkDEBUGCODE(fWasCountedAsRender = true); |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | bool GrCCDrawPathsOp::SingleDraw::shouldCachePathMask(int maxRenderTargetSize) const { |
| 283 | SkASSERT(fCacheEntry); |
| 284 | SkASSERT(!fCacheEntry->cachedAtlas()); |
| 285 | if (fCacheEntry->hitCount() <= 1) { |
| 286 | return false; // Don't cache a path mask until at least its second hit. |
| 287 | } |
| 288 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 289 | int shapeMaxDimension = SkTMax( |
| 290 | fShapeConservativeIBounds.height(), fShapeConservativeIBounds.width()); |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 291 | if (shapeMaxDimension > maxRenderTargetSize) { |
| 292 | return false; // This path isn't cachable. |
| 293 | } |
| 294 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 295 | int64_t shapeArea = sk_64_mul( |
| 296 | fShapeConservativeIBounds.height(), fShapeConservativeIBounds.width()); |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 297 | if (shapeArea < 100*100) { |
| 298 | // If a path is small enough, we might as well try to render and cache the entire thing, no |
| 299 | // matter how much of it is actually visible. |
| 300 | return true; |
| 301 | } |
| 302 | |
| 303 | // The hitRect should already be contained within the shape's bounds, but we still intersect it |
| 304 | // because it's possible for edges very near pixel boundaries (e.g., 0.999999), to round out |
| 305 | // inconsistently, depending on the integer translation values and fp32 precision. |
| 306 | SkIRect hitRect = fCacheEntry->hitRect().makeOffset(fCachedMaskShift.x(), fCachedMaskShift.y()); |
| 307 | hitRect.intersect(fShapeConservativeIBounds); |
| 308 | |
| 309 | // Render and cache the entire path mask if we see enough of it to justify rendering all the |
| 310 | // pixels. Our criteria for "enough" is that we must have seen at least 50% of the path in the |
| 311 | // past, and in this particular draw we must see at least 10% of it. |
| 312 | int64_t hitArea = sk_64_mul(hitRect.height(), hitRect.width()); |
| 313 | int64_t drawArea = sk_64_mul(fMaskDevIBounds.height(), fMaskDevIBounds.width()); |
| 314 | return hitArea*2 >= shapeArea && drawArea*10 >= shapeArea; |
Chris Dalton | 4bfb50b | 2018-05-21 09:10:53 -0600 | [diff] [blame] | 315 | } |
| 316 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 317 | void GrCCDrawPathsOp::setupResources( |
| 318 | GrCCPathCache* pathCache, GrOnFlushResourceProvider* onFlushRP, |
| 319 | GrCCPerFlushResources* resources, DoCopiesToA8Coverage doCopies) { |
Chris Dalton | 4bfb50b | 2018-05-21 09:10:53 -0600 | [diff] [blame] | 320 | SkASSERT(fNumDraws > 0); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 321 | SkASSERT(-1 == fBaseInstance); |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 322 | fBaseInstance = resources->nextPathInstanceIdx(); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 323 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 324 | for (SingleDraw& draw : fDraws) { |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 325 | draw.setupResources(pathCache, onFlushRP, resources, doCopies, this); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 326 | } |
| 327 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 328 | if (!fInstanceRanges.empty()) { |
| 329 | fInstanceRanges.back().fEndInstanceIdx = resources->nextPathInstanceIdx(); |
| 330 | } |
| 331 | } |
| 332 | |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 333 | void GrCCDrawPathsOp::SingleDraw::setupResources( |
| 334 | GrCCPathCache* pathCache, GrOnFlushResourceProvider* onFlushRP, |
| 335 | GrCCPerFlushResources* resources, DoCopiesToA8Coverage doCopies, GrCCDrawPathsOp* op) { |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 336 | SkPath path; |
| 337 | fShape.asPath(&path); |
| 338 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 339 | auto fillRule = (fShape.style().strokeRec().isFillStyle()) |
| 340 | ? GrFillRuleForSkPath(path) |
| 341 | : GrFillRule::kNonzero; |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 342 | |
| 343 | if (fCacheEntry) { |
| 344 | // Does the path already exist in a cached atlas texture? |
| 345 | if (fCacheEntry->cachedAtlas()) { |
| 346 | SkASSERT(fCacheEntry->cachedAtlas()->getOnFlushProxy()); |
| 347 | if (DoCopiesToA8Coverage::kYes == doCopies && fDoCopyToA8Coverage) { |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 348 | resources->upgradeEntryToLiteralCoverageAtlas( |
| 349 | pathCache, onFlushRP, fCacheEntry.get(), fillRule); |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 350 | SkASSERT(fCacheEntry->cachedAtlas()); |
| 351 | SkASSERT(GrCCAtlas::CoverageType::kA8_LiteralCoverage |
| 352 | == fCacheEntry->cachedAtlas()->coverageType()); |
| 353 | SkASSERT(fCacheEntry->cachedAtlas()->getOnFlushProxy()); |
| 354 | } |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 355 | #if 0 |
| 356 | // Simple color manipulation to visualize cached paths. |
| 357 | fColor = (GrCCAtlas::CoverageType::kA8_LiteralCoverage |
| 358 | == fCacheEntry->cachedAtlas()->coverageType()) |
| 359 | ? SkPMColor4f{0,0,.25,.25} : SkPMColor4f{0,.25,0,.25}; |
| 360 | #endif |
Chris Dalton | 6a5317a | 2019-07-12 09:55:52 -0600 | [diff] [blame] | 361 | auto coverageMode = GrCCPathProcessor::GetCoverageMode( |
| 362 | fCacheEntry->cachedAtlas()->coverageType()); |
| 363 | op->recordInstance(coverageMode, fCacheEntry->cachedAtlas()->getOnFlushProxy(), |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 364 | resources->nextPathInstanceIdx()); |
Chris Dalton | 5b5403e | 2019-06-05 11:54:39 -0600 | [diff] [blame] | 365 | resources->appendDrawPathInstance().set( |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 366 | *fCacheEntry, fCachedMaskShift, SkPMColor4f_toFP16(fColor), fillRule); |
| 367 | #ifdef SK_DEBUG |
Chris Dalton | 9bc450b | 2019-07-21 19:34:52 -0600 | [diff] [blame] | 368 | if (fWasCountedAsRender) { |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 369 | // A path mask didn't exist for this path at the beginning of flush, but we have one |
| 370 | // now. What this means is that we've drawn the same path multiple times this flush. |
| 371 | // Let the resources know that we reused one for their internal debug counters. |
| 372 | resources->debugOnly_didReuseRenderedPath(); |
| 373 | } |
| 374 | #endif |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 375 | return; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | // Render the raw path into a coverage count atlas. renderShapeInAtlas() gives us two tight |
| 380 | // bounding boxes: One in device space, as well as a second one rotated an additional 45 |
| 381 | // degrees. The path vertex shader uses these two bounding boxes to generate an octagon that |
| 382 | // circumscribes the path. |
Chris Dalton | 8610e9c | 2019-05-09 11:07:10 -0600 | [diff] [blame] | 383 | GrOctoBounds octoBounds; |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 384 | SkIRect devIBounds; |
| 385 | SkIVector devToAtlasOffset; |
| 386 | if (auto atlas = resources->renderShapeInAtlas( |
Chris Dalton | 8610e9c | 2019-05-09 11:07:10 -0600 | [diff] [blame] | 387 | fMaskDevIBounds, fMatrix, fShape, fStrokeDevWidth, &octoBounds, &devIBounds, |
| 388 | &devToAtlasOffset)) { |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 389 | auto coverageMode = GrCCPathProcessor::GetCoverageMode( |
| 390 | resources->renderedPathCoverageType()); |
| 391 | op->recordInstance(coverageMode, atlas->textureProxy(), resources->nextPathInstanceIdx()); |
Chris Dalton | 8610e9c | 2019-05-09 11:07:10 -0600 | [diff] [blame] | 392 | resources->appendDrawPathInstance().set( |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 393 | octoBounds, devToAtlasOffset, SkPMColor4f_toFP16(fColor), fillRule); |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 394 | |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 395 | if (fDoCachePathMask) { |
| 396 | SkASSERT(fCacheEntry); |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 397 | SkASSERT(!fCacheEntry->cachedAtlas()); |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 398 | SkASSERT(fShapeConservativeIBounds == fMaskDevIBounds); |
Chris Dalton | 8610e9c | 2019-05-09 11:07:10 -0600 | [diff] [blame] | 399 | fCacheEntry->setCoverageCountAtlas( |
| 400 | onFlushRP, atlas, devToAtlasOffset, octoBounds, devIBounds, fCachedMaskShift); |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
Chris Dalton | 6a5317a | 2019-07-12 09:55:52 -0600 | [diff] [blame] | 405 | inline void GrCCDrawPathsOp::recordInstance( |
| 406 | GrCCPathProcessor::CoverageMode coverageMode, GrTextureProxy* atlasProxy, int instanceIdx) { |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 407 | if (fInstanceRanges.empty()) { |
Chris Dalton | 6a5317a | 2019-07-12 09:55:52 -0600 | [diff] [blame] | 408 | fInstanceRanges.push_back({coverageMode, atlasProxy, instanceIdx}); |
| 409 | } else if (fInstanceRanges.back().fAtlasProxy != atlasProxy) { |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 410 | fInstanceRanges.back().fEndInstanceIdx = instanceIdx; |
Chris Dalton | 6a5317a | 2019-07-12 09:55:52 -0600 | [diff] [blame] | 411 | fInstanceRanges.push_back({coverageMode, atlasProxy, instanceIdx}); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 412 | } |
Chris Dalton | 6a5317a | 2019-07-12 09:55:52 -0600 | [diff] [blame] | 413 | SkASSERT(fInstanceRanges.back().fCoverageMode == coverageMode); |
| 414 | SkASSERT(fInstanceRanges.back().fAtlasProxy == atlasProxy); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 415 | } |
| 416 | |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 417 | void GrCCDrawPathsOp::onPrepare(GrOpFlushState* flushState) { |
| 418 | // The CCPR ops don't know their atlas textures until after the preFlush calls have been |
| 419 | // executed at the start GrDrawingManger::flush. Thus the proxies are not added during the |
| 420 | // normal visitProxies calls doing addDrawOp. Therefore, the atlas proxies are added now. |
| 421 | for (const InstanceRange& range : fInstanceRanges) { |
| 422 | flushState->sampledProxyArray()->push_back(range.fAtlasProxy); |
| 423 | } |
| 424 | } |
| 425 | |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 426 | void GrCCDrawPathsOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) { |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 427 | SkASSERT(fOwningPerOpsTaskPaths); |
Chris Dalton | f104fec | 2018-05-22 16:17:48 -0600 | [diff] [blame] | 428 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 429 | const GrCCPerFlushResources* resources = fOwningPerOpsTaskPaths->fFlushResources.get(); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 430 | if (!resources) { |
| 431 | return; // Setup failed. |
| 432 | } |
| 433 | |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 434 | GrPipeline::InitArgs initArgs; |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 435 | initArgs.fCaps = &flushState->caps(); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 436 | initArgs.fDstProxy = flushState->drawOpArgs().fDstProxy; |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 437 | initArgs.fOutputSwizzle = flushState->drawOpArgs().fOutputSwizzle; |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 438 | auto clip = flushState->detachAppliedClip(); |
| 439 | GrPipeline::FixedDynamicState fixedDynamicState(clip.scissorState().rect()); |
| 440 | GrPipeline pipeline(initArgs, std::move(fProcessors), std::move(clip)); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 441 | |
| 442 | int baseInstance = fBaseInstance; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 443 | SkASSERT(baseInstance >= 0); // Make sure setupResources() has been called. |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 444 | |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 445 | for (const InstanceRange& range : fInstanceRanges) { |
| 446 | SkASSERT(range.fEndInstanceIdx > baseInstance); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 447 | |
Chris Dalton | f91b755 | 2019-04-29 16:21:18 -0600 | [diff] [blame] | 448 | const GrTextureProxy* atlas = range.fAtlasProxy; |
| 449 | SkASSERT(atlas->isInstantiated()); |
| 450 | |
| 451 | GrCCPathProcessor pathProc( |
Chris Dalton | 6a5317a | 2019-07-12 09:55:52 -0600 | [diff] [blame] | 452 | range.fCoverageMode, atlas->peekTexture(), atlas->textureSwizzle(), atlas->origin(), |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 453 | fViewMatrixIfUsingLocalCoords); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 454 | GrTextureProxy* atlasProxy = range.fAtlasProxy; |
| 455 | fixedDynamicState.fPrimitiveProcessorTextures = &atlasProxy; |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 456 | pathProc.drawPaths(flushState, pipeline, &fixedDynamicState, *resources, baseInstance, |
| 457 | range.fEndInstanceIdx, this->bounds()); |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 458 | |
| 459 | baseInstance = range.fEndInstanceIdx; |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 460 | } |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 461 | } |