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 | |
| 8 | #include "GrCCDrawPathsOp.h" |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 9 | |
Brian Salomon | 653f42f | 2018-07-10 10:07:31 -0400 | [diff] [blame] | 10 | #include "GrContext.h" |
| 11 | #include "GrContextPriv.h" |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 12 | #include "GrMemoryPool.h" |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 13 | #include "GrOpFlushState.h" |
Chris Dalton | a2b5b64 | 2018-06-24 13:08:57 -0600 | [diff] [blame] | 14 | #include "ccpr/GrCCPathCache.h" |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 15 | #include "ccpr/GrCCPerFlushResources.h" |
| 16 | #include "ccpr/GrCoverageCountingPathRenderer.h" |
| 17 | |
Chris Dalton | 1c54894 | 2018-05-22 13:09:48 -0600 | [diff] [blame] | 18 | static bool has_coord_transforms(const GrPaint& paint) { |
| 19 | GrFragmentProcessor::Iter iter(paint); |
| 20 | while (const GrFragmentProcessor* fp = iter.next()) { |
| 21 | if (!fp->coordTransforms().empty()) { |
| 22 | return true; |
| 23 | } |
| 24 | } |
| 25 | return false; |
| 26 | } |
| 27 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 28 | std::unique_ptr<GrCCDrawPathsOp> GrCCDrawPathsOp::Make( |
| 29 | GrContext* context, const SkIRect& clipIBounds, const SkMatrix& m, const GrShape& shape, |
| 30 | GrPaint&& paint) { |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 31 | SkRect conservativeDevBounds; |
| 32 | m.mapRect(&conservativeDevBounds, shape.bounds()); |
| 33 | |
| 34 | const SkStrokeRec& stroke = shape.style().strokeRec(); |
| 35 | float strokeDevWidth = 0; |
| 36 | float conservativeInflationRadius = 0; |
| 37 | if (!stroke.isFillStyle()) { |
Chris Dalton | 82de18f | 2018-09-12 17:24:09 -0600 | [diff] [blame] | 38 | strokeDevWidth = GrCoverageCountingPathRenderer::GetStrokeDevWidth( |
| 39 | m, stroke, &conservativeInflationRadius); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 40 | conservativeDevBounds.outset(conservativeInflationRadius, conservativeInflationRadius); |
| 41 | } |
| 42 | |
| 43 | std::unique_ptr<GrCCDrawPathsOp> op; |
| 44 | float conservativeSize = SkTMax(conservativeDevBounds.height(), conservativeDevBounds.width()); |
Chris Dalton | 82de18f | 2018-09-12 17:24:09 -0600 | [diff] [blame] | 45 | if (conservativeSize > GrCoverageCountingPathRenderer::kPathCropThreshold) { |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 46 | // The path is too large. Crop it or analytic AA can run out of fp32 precision. |
| 47 | SkPath croppedDevPath; |
| 48 | shape.asPath(&croppedDevPath); |
| 49 | croppedDevPath.transform(m, &croppedDevPath); |
| 50 | |
| 51 | SkIRect cropBox = clipIBounds; |
| 52 | GrShape croppedDevShape; |
| 53 | if (stroke.isFillStyle()) { |
| 54 | GrCoverageCountingPathRenderer::CropPath(croppedDevPath, cropBox, &croppedDevPath); |
| 55 | croppedDevShape = GrShape(croppedDevPath); |
| 56 | conservativeDevBounds = croppedDevShape.bounds(); |
| 57 | } else { |
| 58 | int r = SkScalarCeilToInt(conservativeInflationRadius); |
| 59 | cropBox.outset(r, r); |
| 60 | GrCoverageCountingPathRenderer::CropPath(croppedDevPath, cropBox, &croppedDevPath); |
| 61 | SkStrokeRec devStroke = stroke; |
| 62 | devStroke.setStrokeStyle(strokeDevWidth); |
| 63 | croppedDevShape = GrShape(croppedDevPath, GrStyle(devStroke, nullptr)); |
| 64 | conservativeDevBounds = croppedDevPath.getBounds(); |
| 65 | conservativeDevBounds.outset(conservativeInflationRadius, conservativeInflationRadius); |
| 66 | } |
| 67 | |
| 68 | // FIXME: This breaks local coords: http://skbug.com/8003 |
| 69 | return InternalMake(context, clipIBounds, SkMatrix::I(), croppedDevShape, strokeDevWidth, |
| 70 | conservativeDevBounds, std::move(paint)); |
| 71 | } |
| 72 | |
| 73 | return InternalMake(context, clipIBounds, m, shape, strokeDevWidth, conservativeDevBounds, |
| 74 | std::move(paint)); |
| 75 | } |
| 76 | |
| 77 | std::unique_ptr<GrCCDrawPathsOp> GrCCDrawPathsOp::InternalMake( |
| 78 | GrContext* context, const SkIRect& clipIBounds, const SkMatrix& m, const GrShape& shape, |
| 79 | float strokeDevWidth, const SkRect& conservativeDevBounds, GrPaint&& paint) { |
Chris Dalton | 82de18f | 2018-09-12 17:24:09 -0600 | [diff] [blame] | 80 | // The path itself should have been cropped if larger than kPathCropThreshold. If it had a |
| 81 | // stroke, that would have further inflated its draw bounds. |
| 82 | SkASSERT(SkTMax(conservativeDevBounds.height(), conservativeDevBounds.width()) < |
| 83 | GrCoverageCountingPathRenderer::kPathCropThreshold + |
| 84 | GrCoverageCountingPathRenderer::kMaxBoundsInflationFromStroke*2 + 1); |
| 85 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 86 | SkIRect shapeConservativeIBounds; |
| 87 | conservativeDevBounds.roundOut(&shapeConservativeIBounds); |
Chris Dalton | a8429cf | 2018-06-22 11:43:31 -0600 | [diff] [blame] | 88 | |
| 89 | SkIRect maskDevIBounds; |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 90 | if (!maskDevIBounds.intersect(clipIBounds, shapeConservativeIBounds)) { |
| 91 | return nullptr; |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 92 | } |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 93 | |
| 94 | GrOpMemoryPool* pool = context->contextPriv().opMemoryPool(); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 95 | return pool->allocate<GrCCDrawPathsOp>(m, shape, strokeDevWidth, shapeConservativeIBounds, |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 96 | maskDevIBounds, conservativeDevBounds, std::move(paint)); |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 97 | } |
| 98 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 99 | GrCCDrawPathsOp::GrCCDrawPathsOp(const SkMatrix& m, const GrShape& shape, float strokeDevWidth, |
| 100 | const SkIRect& shapeConservativeIBounds, |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 101 | const SkIRect& maskDevIBounds, const SkRect& conservativeDevBounds, |
| 102 | GrPaint&& paint) |
Chris Dalton | 4bfb50b | 2018-05-21 09:10:53 -0600 | [diff] [blame] | 103 | : GrDrawOp(ClassID()) |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 104 | , fViewMatrixIfUsingLocalCoords(has_coord_transforms(paint) ? m : SkMatrix::I()) |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 105 | , fDraws(m, shape, strokeDevWidth, shapeConservativeIBounds, maskDevIBounds, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 106 | paint.getColor4f()) |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 107 | , 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] | 108 | SkDEBUGCODE(fBaseInstance = -1); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 109 | // FIXME: intersect with clip bounds to (hopefully) improve batching. |
| 110 | // (This is nontrivial due to assumptions in generating the octagon cover geometry.) |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 111 | this->setBounds(conservativeDevBounds, GrOp::HasAABloat::kYes, GrOp::IsZeroArea::kNo); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | GrCCDrawPathsOp::~GrCCDrawPathsOp() { |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 115 | if (fOwningPerOpListPaths) { |
Chris Dalton | b68bcc4 | 2018-09-14 00:44:22 -0600 | [diff] [blame] | 116 | // Remove the list's dangling pointer to this Op before deleting it. |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 117 | fOwningPerOpListPaths->fDrawOps.remove(this); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 118 | } |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 119 | } |
| 120 | |
Chris Dalton | a8429cf | 2018-06-22 11:43:31 -0600 | [diff] [blame] | 121 | GrCCDrawPathsOp::SingleDraw::SingleDraw(const SkMatrix& m, const GrShape& shape, |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 122 | float strokeDevWidth, |
| 123 | const SkIRect& shapeConservativeIBounds, |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 124 | const SkIRect& maskDevIBounds, const SkPMColor4f& color) |
Chris Dalton | a8429cf | 2018-06-22 11:43:31 -0600 | [diff] [blame] | 125 | : fMatrix(m) |
Chris Dalton | 644341a | 2018-06-18 19:14:16 -0600 | [diff] [blame] | 126 | , fShape(shape) |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 127 | , fStrokeDevWidth(strokeDevWidth) |
| 128 | , fShapeConservativeIBounds(shapeConservativeIBounds) |
Chris Dalton | a8429cf | 2018-06-22 11:43:31 -0600 | [diff] [blame] | 129 | , fMaskDevIBounds(maskDevIBounds) |
Chris Dalton | a8429cf | 2018-06-22 11:43:31 -0600 | [diff] [blame] | 130 | , fColor(color) { |
Chris Dalton | 644341a | 2018-06-18 19:14:16 -0600 | [diff] [blame] | 131 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 132 | if (fShape.hasUnstyledKey()) { |
| 133 | // On AOSP we round view matrix translates to integer values for cachable paths. We do this |
| 134 | // to match HWUI's cache hit ratio, which doesn't consider the matrix when caching paths. |
| 135 | fMatrix.setTranslateX(SkScalarRoundToScalar(fMatrix.getTranslateX())); |
| 136 | fMatrix.setTranslateY(SkScalarRoundToScalar(fMatrix.getTranslateY())); |
| 137 | } |
| 138 | #endif |
| 139 | } |
| 140 | |
Chris Dalton | 4b62aed | 2019-01-15 11:53:00 -0700 | [diff] [blame^] | 141 | GrProcessorSet::Analysis GrCCDrawPathsOp::finalize(const GrCaps& caps, const GrAppliedClip* clip) { |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 142 | SkASSERT(1 == fNumDraws); // There should only be one single path draw in this Op right now. |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 143 | return fDraws.head().finalize(caps, clip, &fProcessors); |
| 144 | } |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 145 | |
Chris Dalton | 4b62aed | 2019-01-15 11:53:00 -0700 | [diff] [blame^] | 146 | GrProcessorSet::Analysis GrCCDrawPathsOp::SingleDraw::finalize( |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 147 | const GrCaps& caps, const GrAppliedClip* clip, GrProcessorSet* processors) { |
| 148 | const GrProcessorSet::Analysis& analysis = processors->finalize( |
| 149 | fColor, GrProcessorAnalysisCoverage::kSingleChannel, clip, false, caps, |
| 150 | &fColor); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 151 | |
| 152 | // Lines start looking jagged when they get thinner than 1px. For thin strokes it looks better |
| 153 | // if we can convert them to hairline (i.e., inflate the stroke width to 1px), and instead |
| 154 | // reduce the opacity to create the illusion of thin-ness. This strategy also helps reduce |
| 155 | // artifacts from coverage dilation when there are self intersections. |
| 156 | if (analysis.isCompatibleWithCoverageAsAlpha() && |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 157 | !fShape.style().strokeRec().isFillStyle() && fStrokeDevWidth < 1) { |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 158 | // Modifying the shape affects its cache key. The draw can't have a cache entry yet or else |
| 159 | // our next step would invalidate it. |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 160 | SkASSERT(!fCacheEntry); |
| 161 | SkASSERT(SkStrokeRec::kStroke_Style == fShape.style().strokeRec().getStyle()); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 162 | |
| 163 | SkPath path; |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 164 | fShape.asPath(&path); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 165 | |
| 166 | // Create a hairline version of our stroke. |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 167 | SkStrokeRec hairlineStroke = fShape.style().strokeRec(); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 168 | hairlineStroke.setStrokeStyle(0); |
| 169 | |
| 170 | // 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] | 171 | float coverage = fStrokeDevWidth; |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 172 | |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 173 | fShape = GrShape(path, GrStyle(hairlineStroke, nullptr)); |
| 174 | fStrokeDevWidth = 1; |
Brian Osman | 1be2b7c | 2018-10-29 16:07:15 -0400 | [diff] [blame] | 175 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 176 | // fShapeConservativeIBounds already accounted for this possibility of inflating the stroke. |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 177 | fColor = fColor * coverage; |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 178 | } |
| 179 | |
Chris Dalton | 4b62aed | 2019-01-15 11:53:00 -0700 | [diff] [blame^] | 180 | return analysis; |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 181 | } |
| 182 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 183 | GrOp::CombineResult GrCCDrawPathsOp::onCombineIfPossible(GrOp* op, const GrCaps&) { |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 184 | GrCCDrawPathsOp* that = op->cast<GrCCDrawPathsOp>(); |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 185 | SkASSERT(fOwningPerOpListPaths); |
Chris Dalton | 4bfb50b | 2018-05-21 09:10:53 -0600 | [diff] [blame] | 186 | SkASSERT(fNumDraws); |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 187 | SkASSERT(!that->fOwningPerOpListPaths || that->fOwningPerOpListPaths == fOwningPerOpListPaths); |
Chris Dalton | 4bfb50b | 2018-05-21 09:10:53 -0600 | [diff] [blame] | 188 | SkASSERT(that->fNumDraws); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 189 | |
Brian Osman | 9aa30c6 | 2018-07-02 15:21:46 -0400 | [diff] [blame] | 190 | if (fProcessors != that->fProcessors || |
Chris Dalton | 1c54894 | 2018-05-22 13:09:48 -0600 | [diff] [blame] | 191 | fViewMatrixIfUsingLocalCoords != that->fViewMatrixIfUsingLocalCoords) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 192 | return CombineResult::kCannotCombine; |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 193 | } |
| 194 | |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 195 | fDraws.append(std::move(that->fDraws), &fOwningPerOpListPaths->fAllocator); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 196 | |
Chris Dalton | 4bfb50b | 2018-05-21 09:10:53 -0600 | [diff] [blame] | 197 | SkDEBUGCODE(fNumDraws += that->fNumDraws); |
| 198 | SkDEBUGCODE(that->fNumDraws = 0); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 199 | return CombineResult::kMerged; |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 200 | } |
| 201 | |
Brian Salomon | 348a037 | 2018-10-31 10:42:18 -0400 | [diff] [blame] | 202 | void GrCCDrawPathsOp::addToOwningPerOpListPaths(sk_sp<GrCCPerOpListPaths> owningPerOpListPaths) { |
Chris Dalton | f104fec | 2018-05-22 16:17:48 -0600 | [diff] [blame] | 203 | SkASSERT(1 == fNumDraws); |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 204 | SkASSERT(!fOwningPerOpListPaths); |
Chris Dalton | b68bcc4 | 2018-09-14 00:44:22 -0600 | [diff] [blame] | 205 | fOwningPerOpListPaths = std::move(owningPerOpListPaths); |
| 206 | fOwningPerOpListPaths->fDrawOps.addToTail(this); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 207 | } |
| 208 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 209 | void GrCCDrawPathsOp::accountForOwnPaths(GrCCPathCache* pathCache, |
| 210 | GrOnFlushResourceProvider* onFlushRP, |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 211 | GrCCPerFlushResourceSpecs* specs) { |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 212 | for (SingleDraw& draw : fDraws) { |
| 213 | draw.accountForOwnPath(pathCache, onFlushRP, specs); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | void GrCCDrawPathsOp::SingleDraw::accountForOwnPath( |
| 218 | GrCCPathCache* pathCache, GrOnFlushResourceProvider* onFlushRP, |
| 219 | GrCCPerFlushResourceSpecs* specs) { |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 220 | using CoverageType = GrCCAtlas::CoverageType; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 221 | |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 222 | SkPath path; |
| 223 | fShape.asPath(&path); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 224 | |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 225 | SkASSERT(!fCacheEntry); |
Chris Dalton | a2b5b64 | 2018-06-24 13:08:57 -0600 | [diff] [blame] | 226 | |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 227 | if (pathCache) { |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 228 | fCacheEntry = |
| 229 | pathCache->find(onFlushRP, fShape, fMaskDevIBounds, fMatrix, &fCachedMaskShift); |
Chris Dalton | 4bfb50b | 2018-05-21 09:10:53 -0600 | [diff] [blame] | 230 | } |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 231 | |
| 232 | if (fCacheEntry) { |
| 233 | if (const GrCCCachedAtlas* cachedAtlas = fCacheEntry->cachedAtlas()) { |
| 234 | SkASSERT(cachedAtlas->getOnFlushProxy()); |
| 235 | if (CoverageType::kA8_LiteralCoverage == cachedAtlas->coverageType()) { |
| 236 | ++specs->fNumCachedPaths; |
| 237 | } else { |
| 238 | // Suggest that this path be copied to a literal coverage atlas, to save memory. |
| 239 | // (The client may decline this copy via DoCopiesToA8Coverage::kNo.) |
| 240 | int idx = (fShape.style().strokeRec().isFillStyle()) |
| 241 | ? GrCCPerFlushResourceSpecs::kFillIdx |
| 242 | : GrCCPerFlushResourceSpecs::kStrokeIdx; |
| 243 | ++specs->fNumCopiedPaths[idx]; |
| 244 | specs->fCopyPathStats[idx].statPath(path); |
| 245 | specs->fCopyAtlasSpecs.accountForSpace(fCacheEntry->width(), fCacheEntry->height()); |
| 246 | fDoCopyToA8Coverage = true; |
| 247 | } |
| 248 | return; |
| 249 | } |
| 250 | |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 251 | if (this->shouldCachePathMask(onFlushRP->caps()->maxRenderTargetSize())) { |
| 252 | fDoCachePathMask = true; |
| 253 | // We don't cache partial masks; ensure the bounds include the entire path. |
| 254 | fMaskDevIBounds = fShapeConservativeIBounds; |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 258 | // Plan on rendering this path in a new atlas. |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 259 | int idx = (fShape.style().strokeRec().isFillStyle()) |
| 260 | ? GrCCPerFlushResourceSpecs::kFillIdx |
| 261 | : GrCCPerFlushResourceSpecs::kStrokeIdx; |
| 262 | ++specs->fNumRenderedPaths[idx]; |
| 263 | specs->fRenderedPathStats[idx].statPath(path); |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 264 | specs->fRenderedAtlasSpecs.accountForSpace(fMaskDevIBounds.width(), fMaskDevIBounds.height()); |
| 265 | } |
| 266 | |
| 267 | bool GrCCDrawPathsOp::SingleDraw::shouldCachePathMask(int maxRenderTargetSize) const { |
| 268 | SkASSERT(fCacheEntry); |
| 269 | SkASSERT(!fCacheEntry->cachedAtlas()); |
| 270 | if (fCacheEntry->hitCount() <= 1) { |
| 271 | return false; // Don't cache a path mask until at least its second hit. |
| 272 | } |
| 273 | |
| 274 | int shapeMaxDimension = SkTMax(fShapeConservativeIBounds.height(), |
| 275 | fShapeConservativeIBounds.width()); |
| 276 | if (shapeMaxDimension > maxRenderTargetSize) { |
| 277 | return false; // This path isn't cachable. |
| 278 | } |
| 279 | |
| 280 | int64_t shapeArea = sk_64_mul(fShapeConservativeIBounds.height(), |
| 281 | fShapeConservativeIBounds.width()); |
| 282 | if (shapeArea < 100*100) { |
| 283 | // If a path is small enough, we might as well try to render and cache the entire thing, no |
| 284 | // matter how much of it is actually visible. |
| 285 | return true; |
| 286 | } |
| 287 | |
| 288 | // The hitRect should already be contained within the shape's bounds, but we still intersect it |
| 289 | // because it's possible for edges very near pixel boundaries (e.g., 0.999999), to round out |
| 290 | // inconsistently, depending on the integer translation values and fp32 precision. |
| 291 | SkIRect hitRect = fCacheEntry->hitRect().makeOffset(fCachedMaskShift.x(), fCachedMaskShift.y()); |
| 292 | hitRect.intersect(fShapeConservativeIBounds); |
| 293 | |
| 294 | // Render and cache the entire path mask if we see enough of it to justify rendering all the |
| 295 | // pixels. Our criteria for "enough" is that we must have seen at least 50% of the path in the |
| 296 | // past, and in this particular draw we must see at least 10% of it. |
| 297 | int64_t hitArea = sk_64_mul(hitRect.height(), hitRect.width()); |
| 298 | int64_t drawArea = sk_64_mul(fMaskDevIBounds.height(), fMaskDevIBounds.width()); |
| 299 | return hitArea*2 >= shapeArea && drawArea*10 >= shapeArea; |
Chris Dalton | 4bfb50b | 2018-05-21 09:10:53 -0600 | [diff] [blame] | 300 | } |
| 301 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 302 | void GrCCDrawPathsOp::setupResources( |
| 303 | GrCCPathCache* pathCache, GrOnFlushResourceProvider* onFlushRP, |
| 304 | GrCCPerFlushResources* resources, DoCopiesToA8Coverage doCopies) { |
Chris Dalton | 4bfb50b | 2018-05-21 09:10:53 -0600 | [diff] [blame] | 305 | SkASSERT(fNumDraws > 0); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 306 | SkASSERT(-1 == fBaseInstance); |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 307 | fBaseInstance = resources->nextPathInstanceIdx(); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 308 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 309 | for (SingleDraw& draw : fDraws) { |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 310 | draw.setupResources(pathCache, onFlushRP, resources, doCopies, this); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 311 | } |
| 312 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 313 | if (!fInstanceRanges.empty()) { |
| 314 | fInstanceRanges.back().fEndInstanceIdx = resources->nextPathInstanceIdx(); |
| 315 | } |
| 316 | } |
| 317 | |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 318 | void GrCCDrawPathsOp::SingleDraw::setupResources( |
| 319 | GrCCPathCache* pathCache, GrOnFlushResourceProvider* onFlushRP, |
| 320 | GrCCPerFlushResources* resources, DoCopiesToA8Coverage doCopies, GrCCDrawPathsOp* op) { |
| 321 | using DoEvenOddFill = GrCCPathProcessor::DoEvenOddFill; |
| 322 | |
| 323 | SkPath path; |
| 324 | fShape.asPath(&path); |
| 325 | |
| 326 | auto doEvenOddFill = DoEvenOddFill(fShape.style().strokeRec().isFillStyle() && |
| 327 | SkPath::kEvenOdd_FillType == path.getFillType()); |
| 328 | SkASSERT(SkPath::kEvenOdd_FillType == path.getFillType() || |
| 329 | SkPath::kWinding_FillType == path.getFillType()); |
| 330 | |
| 331 | if (fCacheEntry) { |
| 332 | // Does the path already exist in a cached atlas texture? |
| 333 | if (fCacheEntry->cachedAtlas()) { |
| 334 | SkASSERT(fCacheEntry->cachedAtlas()->getOnFlushProxy()); |
| 335 | if (DoCopiesToA8Coverage::kYes == doCopies && fDoCopyToA8Coverage) { |
| 336 | resources->upgradeEntryToLiteralCoverageAtlas(pathCache, onFlushRP, |
| 337 | fCacheEntry.get(), doEvenOddFill); |
| 338 | SkASSERT(fCacheEntry->cachedAtlas()); |
| 339 | SkASSERT(GrCCAtlas::CoverageType::kA8_LiteralCoverage |
| 340 | == fCacheEntry->cachedAtlas()->coverageType()); |
| 341 | SkASSERT(fCacheEntry->cachedAtlas()->getOnFlushProxy()); |
| 342 | } |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 343 | #if 0 |
| 344 | // Simple color manipulation to visualize cached paths. |
| 345 | fColor = (GrCCAtlas::CoverageType::kA8_LiteralCoverage |
| 346 | == fCacheEntry->cachedAtlas()->coverageType()) |
| 347 | ? SkPMColor4f{0,0,.25,.25} : SkPMColor4f{0,.25,0,.25}; |
| 348 | #endif |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 349 | op->recordInstance(fCacheEntry->cachedAtlas()->getOnFlushProxy(), |
| 350 | resources->nextPathInstanceIdx()); |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 351 | resources->appendDrawPathInstance().set(*fCacheEntry, fCachedMaskShift, |
Brian Osman | c6444d2 | 2019-01-09 16:30:12 -0500 | [diff] [blame] | 352 | SkPMColor4f_toFP16(fColor)); |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 353 | return; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | // Render the raw path into a coverage count atlas. renderShapeInAtlas() gives us two tight |
| 358 | // bounding boxes: One in device space, as well as a second one rotated an additional 45 |
| 359 | // degrees. The path vertex shader uses these two bounding boxes to generate an octagon that |
| 360 | // circumscribes the path. |
| 361 | SkRect devBounds, devBounds45; |
| 362 | SkIRect devIBounds; |
| 363 | SkIVector devToAtlasOffset; |
| 364 | if (auto atlas = resources->renderShapeInAtlas( |
| 365 | fMaskDevIBounds, fMatrix, fShape, fStrokeDevWidth, &devBounds, &devBounds45, |
| 366 | &devIBounds, &devToAtlasOffset)) { |
| 367 | op->recordInstance(atlas->textureProxy(), resources->nextPathInstanceIdx()); |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 368 | resources->appendDrawPathInstance().set(devBounds, devBounds45, devToAtlasOffset, |
Brian Osman | c6444d2 | 2019-01-09 16:30:12 -0500 | [diff] [blame] | 369 | SkPMColor4f_toFP16(fColor), doEvenOddFill); |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 370 | |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 371 | if (fDoCachePathMask) { |
| 372 | SkASSERT(fCacheEntry); |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 373 | SkASSERT(!fCacheEntry->cachedAtlas()); |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 374 | SkASSERT(fShapeConservativeIBounds == fMaskDevIBounds); |
Chris Dalton | a13078c | 2019-01-07 09:34:05 -0700 | [diff] [blame] | 375 | fCacheEntry->setCoverageCountAtlas(onFlushRP, atlas, devToAtlasOffset, devBounds, |
| 376 | devBounds45, devIBounds, fCachedMaskShift); |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 381 | inline void GrCCDrawPathsOp::recordInstance(GrTextureProxy* atlasProxy, int instanceIdx) { |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 382 | if (fInstanceRanges.empty()) { |
| 383 | fInstanceRanges.push_back({atlasProxy, instanceIdx}); |
| 384 | return; |
| 385 | } |
| 386 | if (fInstanceRanges.back().fAtlasProxy != atlasProxy) { |
| 387 | fInstanceRanges.back().fEndInstanceIdx = instanceIdx; |
| 388 | fInstanceRanges.push_back({atlasProxy, instanceIdx}); |
| 389 | return; |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 393 | void GrCCDrawPathsOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) { |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 394 | SkASSERT(fOwningPerOpListPaths); |
Chris Dalton | f104fec | 2018-05-22 16:17:48 -0600 | [diff] [blame] | 395 | |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 396 | const GrCCPerFlushResources* resources = fOwningPerOpListPaths->fFlushResources.get(); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 397 | if (!resources) { |
| 398 | return; // Setup failed. |
| 399 | } |
| 400 | |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 401 | GrPipeline::InitArgs initArgs; |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 402 | initArgs.fProxy = flushState->drawOpArgs().fProxy; |
| 403 | initArgs.fCaps = &flushState->caps(); |
| 404 | initArgs.fResourceProvider = flushState->resourceProvider(); |
| 405 | initArgs.fDstProxy = flushState->drawOpArgs().fDstProxy; |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 406 | auto clip = flushState->detachAppliedClip(); |
| 407 | GrPipeline::FixedDynamicState fixedDynamicState(clip.scissorState().rect()); |
| 408 | GrPipeline pipeline(initArgs, std::move(fProcessors), std::move(clip)); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 409 | |
| 410 | int baseInstance = fBaseInstance; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 411 | SkASSERT(baseInstance >= 0); // Make sure setupResources() has been called. |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 412 | |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 413 | for (const InstanceRange& range : fInstanceRanges) { |
| 414 | SkASSERT(range.fEndInstanceIdx > baseInstance); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 415 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 416 | GrCCPathProcessor pathProc(range.fAtlasProxy, fViewMatrixIfUsingLocalCoords); |
| 417 | GrTextureProxy* atlasProxy = range.fAtlasProxy; |
| 418 | fixedDynamicState.fPrimitiveProcessorTextures = &atlasProxy; |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 419 | pathProc.drawPaths(flushState, pipeline, &fixedDynamicState, *resources, baseInstance, |
| 420 | range.fEndInstanceIdx, this->bounds()); |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 421 | |
| 422 | baseInstance = range.fEndInstanceIdx; |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 423 | } |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 424 | } |