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 | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 111 | // FIXME: intersect with clip bounds to (hopefully) improve batching. |
| 112 | // (This is nontrivial due to assumptions in generating the octagon cover geometry.) |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 113 | this->setBounds(conservativeDevBounds, GrOp::HasAABloat::kYes, GrOp::IsZeroArea::kNo); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | GrCCDrawPathsOp::~GrCCDrawPathsOp() { |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 117 | if (fOwningPerOpListPaths) { |
Chris Dalton | b68bcc4 | 2018-09-14 00:44:22 -0600 | [diff] [blame] | 118 | // Remove the list's dangling pointer to this Op before deleting it. |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 119 | fOwningPerOpListPaths->fDrawOps.remove(this); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 120 | } |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 121 | } |
| 122 | |
Chris Dalton | a8429cf | 2018-06-22 11:43:31 -0600 | [diff] [blame] | 123 | GrCCDrawPathsOp::SingleDraw::SingleDraw(const SkMatrix& m, const GrShape& shape, |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 124 | float strokeDevWidth, |
| 125 | const SkIRect& shapeConservativeIBounds, |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 126 | const SkIRect& maskDevIBounds, const SkPMColor4f& color) |
Chris Dalton | a8429cf | 2018-06-22 11:43:31 -0600 | [diff] [blame] | 127 | : fMatrix(m) |
Chris Dalton | 644341a | 2018-06-18 19:14:16 -0600 | [diff] [blame] | 128 | , fShape(shape) |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 129 | , fStrokeDevWidth(strokeDevWidth) |
| 130 | , fShapeConservativeIBounds(shapeConservativeIBounds) |
Chris Dalton | a8429cf | 2018-06-22 11:43:31 -0600 | [diff] [blame] | 131 | , fMaskDevIBounds(maskDevIBounds) |
Chris Dalton | a8429cf | 2018-06-22 11:43:31 -0600 | [diff] [blame] | 132 | , fColor(color) { |
Chris Dalton | 644341a | 2018-06-18 19:14:16 -0600 | [diff] [blame] | 133 | #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 Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 143 | GrProcessorSet::Analysis GrCCDrawPathsOp::finalize(const GrCaps& caps, const GrAppliedClip* clip, |
| 144 | GrFSAAType fsaaType, GrClampType clampType) { |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 145 | SkASSERT(1 == fNumDraws); // There should only be one single path draw in this Op right now. |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 146 | return fDraws.head().finalize(caps, clip, fsaaType, clampType, &fProcessors); |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 147 | } |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 148 | |
Chris Dalton | 4b62aed | 2019-01-15 11:53:00 -0700 | [diff] [blame] | 149 | GrProcessorSet::Analysis GrCCDrawPathsOp::SingleDraw::finalize( |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 150 | const GrCaps& caps, const GrAppliedClip* clip, GrFSAAType fsaaType, GrClampType clampType, |
Chris Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 151 | GrProcessorSet* processors) { |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 152 | const GrProcessorSet::Analysis& analysis = processors->finalize( |
Chris Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 153 | fColor, GrProcessorAnalysisCoverage::kSingleChannel, clip, |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 154 | &GrUserStencilSettings::kUnused, fsaaType, caps, clampType, &fColor); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 155 | |
| 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 Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 161 | !fShape.style().strokeRec().isFillStyle() && fStrokeDevWidth < 1) { |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 162 | // 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 Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 164 | SkASSERT(!fCacheEntry); |
| 165 | SkASSERT(SkStrokeRec::kStroke_Style == fShape.style().strokeRec().getStyle()); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 166 | |
| 167 | SkPath path; |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 168 | fShape.asPath(&path); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 169 | |
| 170 | // Create a hairline version of our stroke. |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 171 | SkStrokeRec hairlineStroke = fShape.style().strokeRec(); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 172 | 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 Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 175 | float coverage = fStrokeDevWidth; |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 176 | |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 177 | fShape = GrShape(path, GrStyle(hairlineStroke, nullptr)); |
| 178 | fStrokeDevWidth = 1; |
Brian Osman | 1be2b7c | 2018-10-29 16:07:15 -0400 | [diff] [blame] | 179 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 180 | // fShapeConservativeIBounds already accounted for this possibility of inflating the stroke. |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 181 | fColor = fColor * coverage; |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 182 | } |
| 183 | |
Chris Dalton | 4b62aed | 2019-01-15 11:53:00 -0700 | [diff] [blame] | 184 | return analysis; |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 185 | } |
| 186 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 187 | GrOp::CombineResult GrCCDrawPathsOp::onCombineIfPossible(GrOp* op, const GrCaps&) { |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 188 | GrCCDrawPathsOp* that = op->cast<GrCCDrawPathsOp>(); |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 189 | SkASSERT(fOwningPerOpListPaths); |
Chris Dalton | 4bfb50b | 2018-05-21 09:10:53 -0600 | [diff] [blame] | 190 | SkASSERT(fNumDraws); |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 191 | SkASSERT(!that->fOwningPerOpListPaths || that->fOwningPerOpListPaths == fOwningPerOpListPaths); |
Chris Dalton | 4bfb50b | 2018-05-21 09:10:53 -0600 | [diff] [blame] | 192 | SkASSERT(that->fNumDraws); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 193 | |
Brian Osman | 9aa30c6 | 2018-07-02 15:21:46 -0400 | [diff] [blame] | 194 | if (fProcessors != that->fProcessors || |
Chris Dalton | 1c54894 | 2018-05-22 13:09:48 -0600 | [diff] [blame] | 195 | fViewMatrixIfUsingLocalCoords != that->fViewMatrixIfUsingLocalCoords) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 196 | return CombineResult::kCannotCombine; |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 197 | } |
| 198 | |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 199 | fDraws.append(std::move(that->fDraws), &fOwningPerOpListPaths->fAllocator); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 200 | |
Chris Dalton | 4bfb50b | 2018-05-21 09:10:53 -0600 | [diff] [blame] | 201 | SkDEBUGCODE(fNumDraws += that->fNumDraws); |
| 202 | SkDEBUGCODE(that->fNumDraws = 0); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 203 | return CombineResult::kMerged; |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 204 | } |
| 205 | |
Brian Salomon | 348a037 | 2018-10-31 10:42:18 -0400 | [diff] [blame] | 206 | void GrCCDrawPathsOp::addToOwningPerOpListPaths(sk_sp<GrCCPerOpListPaths> owningPerOpListPaths) { |
Chris Dalton | f104fec | 2018-05-22 16:17:48 -0600 | [diff] [blame] | 207 | SkASSERT(1 == fNumDraws); |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 208 | SkASSERT(!fOwningPerOpListPaths); |
Chris Dalton | b68bcc4 | 2018-09-14 00:44:22 -0600 | [diff] [blame] | 209 | fOwningPerOpListPaths = std::move(owningPerOpListPaths); |
| 210 | fOwningPerOpListPaths->fDrawOps.addToTail(this); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 211 | } |
| 212 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 213 | void GrCCDrawPathsOp::accountForOwnPaths(GrCCPathCache* pathCache, |
| 214 | GrOnFlushResourceProvider* onFlushRP, |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 215 | GrCCPerFlushResourceSpecs* specs) { |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 216 | for (SingleDraw& draw : fDraws) { |
| 217 | draw.accountForOwnPath(pathCache, onFlushRP, specs); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | void GrCCDrawPathsOp::SingleDraw::accountForOwnPath( |
| 222 | GrCCPathCache* pathCache, GrOnFlushResourceProvider* onFlushRP, |
| 223 | GrCCPerFlushResourceSpecs* specs) { |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 224 | using CoverageType = GrCCAtlas::CoverageType; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 225 | |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 226 | SkPath path; |
| 227 | fShape.asPath(&path); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 228 | |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 229 | SkASSERT(!fCacheEntry); |
Chris Dalton | a2b5b64 | 2018-06-24 13:08:57 -0600 | [diff] [blame] | 230 | |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 231 | if (pathCache) { |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 232 | fCacheEntry = |
| 233 | pathCache->find(onFlushRP, fShape, fMaskDevIBounds, fMatrix, &fCachedMaskShift); |
Chris Dalton | 4bfb50b | 2018-05-21 09:10:53 -0600 | [diff] [blame] | 234 | } |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 235 | |
| 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 Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 255 | 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 Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 262 | // Plan on rendering this path in a new atlas. |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 263 | int idx = (fShape.style().strokeRec().isFillStyle()) |
| 264 | ? GrCCPerFlushResourceSpecs::kFillIdx |
| 265 | : GrCCPerFlushResourceSpecs::kStrokeIdx; |
| 266 | ++specs->fNumRenderedPaths[idx]; |
| 267 | specs->fRenderedPathStats[idx].statPath(path); |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 268 | specs->fRenderedAtlasSpecs.accountForSpace(fMaskDevIBounds.width(), fMaskDevIBounds.height()); |
| 269 | } |
| 270 | |
| 271 | bool 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 Dalton | 4bfb50b | 2018-05-21 09:10:53 -0600 | [diff] [blame] | 304 | } |
| 305 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 306 | void GrCCDrawPathsOp::setupResources( |
| 307 | GrCCPathCache* pathCache, GrOnFlushResourceProvider* onFlushRP, |
| 308 | GrCCPerFlushResources* resources, DoCopiesToA8Coverage doCopies) { |
Chris Dalton | 4bfb50b | 2018-05-21 09:10:53 -0600 | [diff] [blame] | 309 | SkASSERT(fNumDraws > 0); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 310 | SkASSERT(-1 == fBaseInstance); |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 311 | fBaseInstance = resources->nextPathInstanceIdx(); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 312 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 313 | for (SingleDraw& draw : fDraws) { |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 314 | draw.setupResources(pathCache, onFlushRP, resources, doCopies, this); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 315 | } |
| 316 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 317 | if (!fInstanceRanges.empty()) { |
| 318 | fInstanceRanges.back().fEndInstanceIdx = resources->nextPathInstanceIdx(); |
| 319 | } |
| 320 | } |
| 321 | |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 322 | void 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 Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 347 | #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 Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 353 | op->recordInstance(fCacheEntry->cachedAtlas()->getOnFlushProxy(), |
| 354 | resources->nextPathInstanceIdx()); |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 355 | resources->appendDrawPathInstance().set(*fCacheEntry, fCachedMaskShift, |
Brian Osman | c6444d2 | 2019-01-09 16:30:12 -0500 | [diff] [blame] | 356 | SkPMColor4f_toFP16(fColor)); |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 357 | 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 Dalton | 8610e9c | 2019-05-09 11:07:10 -0600 | [diff] [blame^] | 365 | GrOctoBounds octoBounds; |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 366 | SkIRect devIBounds; |
| 367 | SkIVector devToAtlasOffset; |
| 368 | if (auto atlas = resources->renderShapeInAtlas( |
Chris Dalton | 8610e9c | 2019-05-09 11:07:10 -0600 | [diff] [blame^] | 369 | fMaskDevIBounds, fMatrix, fShape, fStrokeDevWidth, &octoBounds, &devIBounds, |
| 370 | &devToAtlasOffset)) { |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 371 | op->recordInstance(atlas->textureProxy(), resources->nextPathInstanceIdx()); |
Chris Dalton | 8610e9c | 2019-05-09 11:07:10 -0600 | [diff] [blame^] | 372 | resources->appendDrawPathInstance().set( |
| 373 | octoBounds, devToAtlasOffset, SkPMColor4f_toFP16(fColor), doEvenOddFill); |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 374 | |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 375 | if (fDoCachePathMask) { |
| 376 | SkASSERT(fCacheEntry); |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 377 | SkASSERT(!fCacheEntry->cachedAtlas()); |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 378 | SkASSERT(fShapeConservativeIBounds == fMaskDevIBounds); |
Chris Dalton | 8610e9c | 2019-05-09 11:07:10 -0600 | [diff] [blame^] | 379 | fCacheEntry->setCoverageCountAtlas( |
| 380 | onFlushRP, atlas, devToAtlasOffset, octoBounds, devIBounds, fCachedMaskShift); |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 385 | inline void GrCCDrawPathsOp::recordInstance(GrTextureProxy* atlasProxy, int instanceIdx) { |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 386 | 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 Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 397 | void GrCCDrawPathsOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) { |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 398 | SkASSERT(fOwningPerOpListPaths); |
Chris Dalton | f104fec | 2018-05-22 16:17:48 -0600 | [diff] [blame] | 399 | |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 400 | const GrCCPerFlushResources* resources = fOwningPerOpListPaths->fFlushResources.get(); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 401 | if (!resources) { |
| 402 | return; // Setup failed. |
| 403 | } |
| 404 | |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 405 | GrPipeline::InitArgs initArgs; |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 406 | initArgs.fCaps = &flushState->caps(); |
| 407 | initArgs.fResourceProvider = flushState->resourceProvider(); |
| 408 | initArgs.fDstProxy = flushState->drawOpArgs().fDstProxy; |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 409 | auto clip = flushState->detachAppliedClip(); |
| 410 | GrPipeline::FixedDynamicState fixedDynamicState(clip.scissorState().rect()); |
| 411 | GrPipeline pipeline(initArgs, std::move(fProcessors), std::move(clip)); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 412 | |
| 413 | int baseInstance = fBaseInstance; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 414 | SkASSERT(baseInstance >= 0); // Make sure setupResources() has been called. |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 415 | |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 416 | for (const InstanceRange& range : fInstanceRanges) { |
| 417 | SkASSERT(range.fEndInstanceIdx > baseInstance); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 418 | |
Chris Dalton | f91b755 | 2019-04-29 16:21:18 -0600 | [diff] [blame] | 419 | const GrTextureProxy* atlas = range.fAtlasProxy; |
| 420 | SkASSERT(atlas->isInstantiated()); |
| 421 | |
| 422 | GrCCPathProcessor pathProc( |
| 423 | atlas->peekTexture(), atlas->origin(), fViewMatrixIfUsingLocalCoords); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 424 | GrTextureProxy* atlasProxy = range.fAtlasProxy; |
| 425 | fixedDynamicState.fPrimitiveProcessorTextures = &atlasProxy; |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 426 | pathProc.drawPaths(flushState, pipeline, &fixedDynamicState, *resources, baseInstance, |
| 427 | range.fEndInstanceIdx, this->bounds()); |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 428 | |
| 429 | baseInstance = range.fEndInstanceIdx; |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 430 | } |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 431 | } |