Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 "GrCoverageCountingPathRenderer.h" |
| 9 | |
| 10 | #include "GrCaps.h" |
| 11 | #include "GrClip.h" |
| 12 | #include "GrGpu.h" |
| 13 | #include "GrGpuCommandBuffer.h" |
| 14 | #include "SkMakeUnique.h" |
| 15 | #include "SkMatrix.h" |
Chris Dalton | a039d3b | 2017-09-28 11:16:36 -0600 | [diff] [blame] | 16 | #include "SkPathOps.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 17 | #include "GrOpFlushState.h" |
| 18 | #include "GrRenderTargetOpList.h" |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 19 | #include "GrTexture.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 20 | #include "GrStyle.h" |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 21 | #include "ccpr/GrCCPRClipProcessor.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 22 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 23 | // Shorthand for keeping line lengths under control with nested classes... |
| 24 | using CCPR = GrCoverageCountingPathRenderer; |
| 25 | |
| 26 | // If a path spans more pixels than this, we need to crop it or else analytic AA can run out of fp32 |
| 27 | // precision. |
| 28 | static constexpr float kPathCropThreshold = 1 << 16; |
| 29 | |
| 30 | static void crop_path(const SkPath& path, const SkIRect& cropbox, SkPath* out) { |
| 31 | SkPath cropPath; |
| 32 | cropPath.addRect(SkRect::Make(cropbox)); |
| 33 | if (!Op(cropPath, path, kIntersect_SkPathOp, out)) { |
| 34 | // This can fail if the PathOps encounter NaN or infinities. |
| 35 | out->reset(); |
| 36 | } |
| 37 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 38 | |
| 39 | bool GrCoverageCountingPathRenderer::IsSupported(const GrCaps& caps) { |
| 40 | const GrShaderCaps& shaderCaps = *caps.shaderCaps(); |
| 41 | return shaderCaps.geometryShaderSupport() && |
| 42 | shaderCaps.texelBufferSupport() && |
| 43 | shaderCaps.integerSupport() && |
| 44 | shaderCaps.flatInterpolationSupport() && |
| 45 | shaderCaps.maxVertexSamplers() >= 1 && |
| 46 | caps.instanceAttribSupport() && |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame] | 47 | GrCaps::kNone_MapFlags != caps.mapBufferFlags() && |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 48 | caps.isConfigTexturable(kAlpha_half_GrPixelConfig) && |
Chris Dalton | e4679fa | 2017-09-29 13:58:26 -0600 | [diff] [blame] | 49 | caps.isConfigRenderable(kAlpha_half_GrPixelConfig, /*withMSAA=*/false) && |
Brian Salomon | 7f56d3d | 2017-10-09 13:02:49 -0400 | [diff] [blame] | 50 | GrCaps::kNone_MapFlags != caps.mapBufferFlags() && |
Chris Dalton | e4679fa | 2017-09-29 13:58:26 -0600 | [diff] [blame] | 51 | !caps.blacklistCoverageCounting(); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | sk_sp<GrCoverageCountingPathRenderer> |
Chris Dalton | a2ac30d | 2017-10-17 10:40:01 -0600 | [diff] [blame] | 55 | GrCoverageCountingPathRenderer::CreateIfSupported(const GrCaps& caps, bool drawCachablePaths) { |
| 56 | auto ccpr = IsSupported(caps) ? new GrCoverageCountingPathRenderer(drawCachablePaths) : nullptr; |
| 57 | return sk_sp<GrCoverageCountingPathRenderer>(ccpr); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 58 | } |
| 59 | |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 60 | GrPathRenderer::CanDrawPath |
| 61 | GrCoverageCountingPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
Chris Dalton | a2ac30d | 2017-10-17 10:40:01 -0600 | [diff] [blame] | 62 | if (args.fShape->hasUnstyledKey() && !fDrawCachablePaths) { |
| 63 | return CanDrawPath::kNo; |
| 64 | } |
| 65 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 66 | if (!args.fShape->style().isSimpleFill() || |
| 67 | args.fShape->inverseFilled() || |
| 68 | args.fViewMatrix->hasPerspective() || |
| 69 | GrAAType::kCoverage != args.fAAType) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 70 | return CanDrawPath::kNo; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | SkPath path; |
| 74 | args.fShape->asPath(&path); |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 75 | if (SkPathPriv::ConicWeightCnt(path)) { |
| 76 | return CanDrawPath::kNo; |
| 77 | } |
| 78 | |
Chris Dalton | db91c6e | 2017-09-08 16:25:08 -0600 | [diff] [blame] | 79 | SkRect devBounds; |
| 80 | SkIRect devIBounds; |
| 81 | args.fViewMatrix->mapRect(&devBounds, path.getBounds()); |
| 82 | devBounds.roundOut(&devIBounds); |
| 83 | if (!devIBounds.intersect(*args.fClipConservativeBounds)) { |
| 84 | // Path is completely clipped away. Our code will eventually notice this before doing any |
| 85 | // real work. |
| 86 | return CanDrawPath::kYes; |
| 87 | } |
| 88 | |
| 89 | if (devIBounds.height() * devIBounds.width() > 256 * 256) { |
| 90 | // Large paths can blow up the atlas fast. And they are not ideal for a two-pass rendering |
| 91 | // algorithm. Give the simpler direct renderers a chance before we commit to drawing it. |
| 92 | return CanDrawPath::kAsBackup; |
| 93 | } |
| 94 | |
| 95 | if (args.fShape->hasUnstyledKey() && path.countVerbs() > 50) { |
| 96 | // Complex paths do better cached in an SDF, if the renderer will accept them. |
| 97 | return CanDrawPath::kAsBackup; |
| 98 | } |
| 99 | |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 100 | return CanDrawPath::kYes; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | bool GrCoverageCountingPathRenderer::onDrawPath(const DrawPathArgs& args) { |
| 104 | SkASSERT(!fFlushing); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 105 | auto op = skstd::make_unique<DrawPathsOp>(this, args, args.fPaint.getColor()); |
| 106 | args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op)); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 107 | return true; |
| 108 | } |
| 109 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 110 | CCPR::DrawPathsOp::DrawPathsOp(GrCoverageCountingPathRenderer* ccpr, const DrawPathArgs& args, |
| 111 | GrColor color) |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 112 | : INHERITED(ClassID()) |
| 113 | , fCCPR(ccpr) |
| 114 | , fSRGBFlags(GrPipeline::SRGBFlagsFromPaint(args.fPaint)) |
| 115 | , fProcessors(std::move(args.fPaint)) |
| 116 | , fTailDraw(&fHeadDraw) |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 117 | , fOwningRTPendingPaths(nullptr) { |
Chris Dalton | 080baa4 | 2017-11-06 14:19:19 -0700 | [diff] [blame] | 118 | SkDEBUGCODE(++fCCPR->fPendingDrawOpsCount); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 119 | SkDEBUGCODE(fBaseInstance = -1); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 120 | SkDEBUGCODE(fInstanceCount = 1;) |
| 121 | SkDEBUGCODE(fNumSkippedInstances = 0;) |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 122 | GrRenderTargetContext* const rtc = args.fRenderTargetContext; |
| 123 | |
| 124 | SkRect devBounds; |
| 125 | args.fViewMatrix->mapRect(&devBounds, args.fShape->bounds()); |
Chris Dalton | c9c97b7 | 2017-11-27 15:34:26 -0700 | [diff] [blame] | 126 | args.fClip->getConservativeBounds(rtc->width(), rtc->height(), &fHeadDraw.fClipIBounds, |
| 127 | nullptr); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 128 | if (SkTMax(devBounds.height(), devBounds.width()) > kPathCropThreshold) { |
| 129 | // The path is too large. We need to crop it or analytic AA can run out of fp32 precision. |
| 130 | SkPath path; |
Chris Dalton | a039d3b | 2017-09-28 11:16:36 -0600 | [diff] [blame] | 131 | args.fShape->asPath(&path); |
| 132 | path.transform(*args.fViewMatrix); |
| 133 | fHeadDraw.fMatrix.setIdentity(); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 134 | crop_path(path, fHeadDraw.fClipIBounds, &fHeadDraw.fPath); |
Chris Dalton | a039d3b | 2017-09-28 11:16:36 -0600 | [diff] [blame] | 135 | devBounds = fHeadDraw.fPath.getBounds(); |
Chris Dalton | a039d3b | 2017-09-28 11:16:36 -0600 | [diff] [blame] | 136 | } else { |
| 137 | fHeadDraw.fMatrix = *args.fViewMatrix; |
| 138 | args.fShape->asPath(&fHeadDraw.fPath); |
Chris Dalton | a039d3b | 2017-09-28 11:16:36 -0600 | [diff] [blame] | 139 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 140 | fHeadDraw.fColor = color; // Can't call args.fPaint.getColor() because it has been std::move'd. |
| 141 | |
| 142 | // FIXME: intersect with clip bounds to (hopefully) improve batching. |
| 143 | // (This is nontrivial due to assumptions in generating the octagon cover geometry.) |
| 144 | this->setBounds(devBounds, GrOp::HasAABloat::kYes, GrOp::IsZeroArea::kNo); |
| 145 | } |
| 146 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 147 | CCPR::DrawPathsOp::~DrawPathsOp() { |
| 148 | if (fOwningRTPendingPaths) { |
Chris Dalton | 080baa4 | 2017-11-06 14:19:19 -0700 | [diff] [blame] | 149 | // Remove CCPR's dangling pointer to this Op before deleting it. |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 150 | fOwningRTPendingPaths->fDrawOps.remove(this); |
Chris Dalton | 080baa4 | 2017-11-06 14:19:19 -0700 | [diff] [blame] | 151 | } |
| 152 | SkDEBUGCODE(--fCCPR->fPendingDrawOpsCount); |
| 153 | } |
| 154 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 155 | GrDrawOp::RequiresDstTexture CCPR::DrawPathsOp::finalize(const GrCaps& caps, |
| 156 | const GrAppliedClip* clip, |
| 157 | GrPixelConfigIsClamped dstIsClamped) { |
Chris Dalton | 080baa4 | 2017-11-06 14:19:19 -0700 | [diff] [blame] | 158 | SkASSERT(!fCCPR->fFlushing); |
| 159 | // There should only be one single path draw in this Op right now. |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 160 | SkASSERT(1 == fInstanceCount); |
Chris Dalton | 080baa4 | 2017-11-06 14:19:19 -0700 | [diff] [blame] | 161 | SkASSERT(&fHeadDraw == fTailDraw); |
Brian Osman | 9a725dd | 2017-09-20 09:53:22 -0400 | [diff] [blame] | 162 | GrProcessorSet::Analysis analysis = fProcessors.finalize( |
Chris Dalton | 080baa4 | 2017-11-06 14:19:19 -0700 | [diff] [blame] | 163 | fHeadDraw.fColor, GrProcessorAnalysisCoverage::kSingleChannel, clip, false, caps, |
| 164 | dstIsClamped, &fHeadDraw.fColor); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 165 | return analysis.requiresDstTexture() ? RequiresDstTexture::kYes : RequiresDstTexture::kNo; |
| 166 | } |
| 167 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 168 | bool CCPR::DrawPathsOp::onCombineIfPossible(GrOp* op, const GrCaps& caps) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 169 | DrawPathsOp* that = op->cast<DrawPathsOp>(); |
| 170 | SkASSERT(fCCPR == that->fCCPR); |
Chris Dalton | 080baa4 | 2017-11-06 14:19:19 -0700 | [diff] [blame] | 171 | SkASSERT(!fCCPR->fFlushing); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 172 | SkASSERT(fOwningRTPendingPaths); |
| 173 | SkASSERT(fInstanceCount); |
| 174 | SkASSERT(!that->fOwningRTPendingPaths || that->fOwningRTPendingPaths == fOwningRTPendingPaths); |
| 175 | SkASSERT(that->fInstanceCount); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 176 | |
| 177 | if (this->getFillType() != that->getFillType() || |
| 178 | fSRGBFlags != that->fSRGBFlags || |
| 179 | fProcessors != that->fProcessors) { |
| 180 | return false; |
| 181 | } |
| 182 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 183 | fTailDraw->fNext = &fOwningRTPendingPaths->fDrawsAllocator.push_back(that->fHeadDraw); |
Chris Dalton | 080baa4 | 2017-11-06 14:19:19 -0700 | [diff] [blame] | 184 | fTailDraw = (that->fTailDraw == &that->fHeadDraw) ? fTailDraw->fNext : that->fTailDraw; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 185 | |
| 186 | this->joinBounds(*that); |
| 187 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 188 | SkDEBUGCODE(fInstanceCount += that->fInstanceCount;) |
| 189 | SkDEBUGCODE(that->fInstanceCount = 0); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 190 | return true; |
| 191 | } |
| 192 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 193 | void CCPR::DrawPathsOp::wasRecorded(GrRenderTargetOpList* opList) { |
Chris Dalton | 080baa4 | 2017-11-06 14:19:19 -0700 | [diff] [blame] | 194 | SkASSERT(!fCCPR->fFlushing); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 195 | SkASSERT(!fOwningRTPendingPaths); |
| 196 | fOwningRTPendingPaths = &fCCPR->fRTPendingPathsMap[opList->uniqueID()]; |
| 197 | fOwningRTPendingPaths->fDrawOps.addToTail(this); |
| 198 | } |
| 199 | |
| 200 | bool GrCoverageCountingPathRenderer::canMakeClipProcessor(const SkPath& deviceSpacePath) const { |
| 201 | if (!fDrawCachablePaths && !deviceSpacePath.isVolatile()) { |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | if (SkPathPriv::ConicWeightCnt(deviceSpacePath)) { |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | std::unique_ptr<GrFragmentProcessor> |
| 213 | GrCoverageCountingPathRenderer::makeClipProcessor(uint32_t opListID, const SkPath& deviceSpacePath, |
| 214 | const SkIRect& accessRect, int rtWidth, |
| 215 | int rtHeight) { |
| 216 | using MustCheckBounds = GrCCPRClipProcessor::MustCheckBounds; |
| 217 | |
| 218 | SkASSERT(!fFlushing); |
| 219 | SkASSERT(this->canMakeClipProcessor(deviceSpacePath)); |
| 220 | |
| 221 | ClipPath& clipPath = fRTPendingPathsMap[opListID].fClipPaths[deviceSpacePath.getGenerationID()]; |
| 222 | if (clipPath.isUninitialized()) { |
| 223 | // This ClipPath was just created during lookup. Initialize it. |
| 224 | clipPath.init(deviceSpacePath, accessRect, rtWidth, rtHeight); |
| 225 | } else { |
| 226 | clipPath.addAccess(accessRect); |
| 227 | } |
| 228 | |
| 229 | bool mustCheckBounds = !clipPath.pathDevIBounds().contains(accessRect); |
| 230 | return skstd::make_unique<GrCCPRClipProcessor>(&clipPath, MustCheckBounds(mustCheckBounds), |
| 231 | deviceSpacePath.getFillType()); |
| 232 | } |
| 233 | |
| 234 | void CCPR::ClipPath::init(const SkPath& deviceSpacePath, const SkIRect& accessRect, int rtWidth, |
| 235 | int rtHeight) { |
| 236 | SkASSERT(this->isUninitialized()); |
| 237 | |
| 238 | fAtlasLazyProxy = GrSurfaceProxy::MakeLazy([this](GrResourceProvider* resourceProvider, |
| 239 | GrSurfaceOrigin* outOrigin) { |
| 240 | SkASSERT(fHasAtlas); |
| 241 | SkASSERT(!fHasAtlasTransform); |
| 242 | |
| 243 | GrTextureProxy* textureProxy = fAtlas ? fAtlas->textureProxy() : nullptr; |
| 244 | if (!textureProxy || !textureProxy->instantiate(resourceProvider)) { |
| 245 | fAtlasScale = fAtlasTranslate = {0, 0}; |
| 246 | SkDEBUGCODE(fHasAtlasTransform = true); |
| 247 | return sk_sp<GrTexture>(); |
| 248 | } |
| 249 | |
| 250 | fAtlasScale = {1.f / textureProxy->width(), 1.f / textureProxy->height()}; |
| 251 | fAtlasTranslate = {fAtlasOffsetX * fAtlasScale.x(), fAtlasOffsetY * fAtlasScale.y()}; |
| 252 | if (kBottomLeft_GrSurfaceOrigin == textureProxy->origin()) { |
| 253 | fAtlasScale.fY = -fAtlasScale.y(); |
| 254 | fAtlasTranslate.fY = 1 - fAtlasTranslate.y(); |
| 255 | } |
| 256 | SkDEBUGCODE(fHasAtlasTransform = true); |
| 257 | |
| 258 | *outOrigin = textureProxy->origin(); |
| 259 | return sk_ref_sp(textureProxy->priv().peekTexture()); |
| 260 | }, GrSurfaceProxy::Renderable::kYes, kAlpha_half_GrPixelConfig); |
| 261 | |
| 262 | const SkRect& pathDevBounds = deviceSpacePath.getBounds(); |
| 263 | if (SkTMax(pathDevBounds.height(), pathDevBounds.width()) > kPathCropThreshold) { |
| 264 | // The path is too large. We need to crop it or analytic AA can run out of fp32 precision. |
| 265 | crop_path(deviceSpacePath, SkIRect::MakeWH(rtWidth, rtHeight), &fDeviceSpacePath); |
| 266 | } else { |
| 267 | fDeviceSpacePath = deviceSpacePath; |
| 268 | } |
| 269 | deviceSpacePath.getBounds().roundOut(&fPathDevIBounds); |
| 270 | fAccessRect = accessRect; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | void GrCoverageCountingPathRenderer::preFlush(GrOnFlushResourceProvider* onFlushRP, |
| 274 | const uint32_t* opListIDs, int numOpListIDs, |
| 275 | SkTArray<sk_sp<GrRenderTargetContext>>* results) { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 276 | using PathInstance = GrCCPRPathProcessor::Instance; |
| 277 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 278 | SkASSERT(!fFlushing); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 279 | SkASSERT(!fPerFlushIndexBuffer); |
| 280 | SkASSERT(!fPerFlushVertexBuffer); |
| 281 | SkASSERT(!fPerFlushInstanceBuffer); |
| 282 | SkASSERT(fPerFlushAtlases.empty()); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 283 | SkDEBUGCODE(fFlushing = true;) |
| 284 | |
| 285 | if (fRTPendingPathsMap.empty()) { |
| 286 | return; // Nothing to draw. |
| 287 | } |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 288 | |
| 289 | fPerFlushResourcesAreValid = false; |
| 290 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 291 | // Count the paths that are being flushed. |
Chris Dalton | c9c97b7 | 2017-11-27 15:34:26 -0700 | [diff] [blame] | 292 | int maxTotalPaths = 0, maxPathPoints = 0, numSkPoints = 0, numSkVerbs = 0; |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 293 | SkDEBUGCODE(int numClipPaths = 0;) |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 294 | for (int i = 0; i < numOpListIDs; ++i) { |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 295 | auto it = fRTPendingPathsMap.find(opListIDs[i]); |
| 296 | if (fRTPendingPathsMap.end() == it) { |
Chris Dalton | 080baa4 | 2017-11-06 14:19:19 -0700 | [diff] [blame] | 297 | continue; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 298 | } |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 299 | const RTPendingPaths& rtPendingPaths = it->second; |
| 300 | |
| 301 | SkTInternalLList<DrawPathsOp>::Iter drawOpsIter; |
| 302 | drawOpsIter.init(rtPendingPaths.fDrawOps, |
| 303 | SkTInternalLList<DrawPathsOp>::Iter::kHead_IterStart); |
| 304 | while (DrawPathsOp* op = drawOpsIter.get()) { |
| 305 | for (const DrawPathsOp::SingleDraw* draw = op->head(); draw; draw = draw->fNext) { |
Chris Dalton | 080baa4 | 2017-11-06 14:19:19 -0700 | [diff] [blame] | 306 | ++maxTotalPaths; |
Chris Dalton | c9c97b7 | 2017-11-27 15:34:26 -0700 | [diff] [blame] | 307 | maxPathPoints = SkTMax(draw->fPath.countPoints(), maxPathPoints); |
Chris Dalton | 080baa4 | 2017-11-06 14:19:19 -0700 | [diff] [blame] | 308 | numSkPoints += draw->fPath.countPoints(); |
| 309 | numSkVerbs += draw->fPath.countVerbs(); |
| 310 | } |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 311 | drawOpsIter.next(); |
Chris Dalton | 080baa4 | 2017-11-06 14:19:19 -0700 | [diff] [blame] | 312 | } |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 313 | |
| 314 | maxTotalPaths += rtPendingPaths.fClipPaths.size(); |
| 315 | SkDEBUGCODE(numClipPaths += rtPendingPaths.fClipPaths.size()); |
| 316 | for (const auto& clipsIter : rtPendingPaths.fClipPaths) { |
| 317 | const SkPath& path = clipsIter.second.deviceSpacePath(); |
| 318 | maxPathPoints = SkTMax(path.countPoints(), maxPathPoints); |
| 319 | numSkPoints += path.countPoints(); |
| 320 | numSkVerbs += path.countVerbs(); |
| 321 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 322 | } |
| 323 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 324 | if (!maxTotalPaths) { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 325 | return; // Nothing to draw. |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 326 | } |
| 327 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 328 | // Allocate GPU buffers. |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 329 | fPerFlushIndexBuffer = GrCCPRPathProcessor::FindOrMakeIndexBuffer(onFlushRP); |
| 330 | if (!fPerFlushIndexBuffer) { |
| 331 | SkDebugf("WARNING: failed to allocate ccpr path index buffer.\n"); |
| 332 | return; |
| 333 | } |
| 334 | |
| 335 | fPerFlushVertexBuffer = GrCCPRPathProcessor::FindOrMakeVertexBuffer(onFlushRP); |
| 336 | if (!fPerFlushVertexBuffer) { |
| 337 | SkDebugf("WARNING: failed to allocate ccpr path vertex buffer.\n"); |
| 338 | return; |
| 339 | } |
| 340 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 341 | fPerFlushInstanceBuffer = onFlushRP->makeBuffer(kVertex_GrBufferType, |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 342 | maxTotalPaths * sizeof(PathInstance)); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 343 | if (!fPerFlushInstanceBuffer) { |
| 344 | SkDebugf("WARNING: failed to allocate path instance buffer. No paths will be drawn.\n"); |
| 345 | return; |
| 346 | } |
| 347 | |
| 348 | PathInstance* pathInstanceData = static_cast<PathInstance*>(fPerFlushInstanceBuffer->map()); |
| 349 | SkASSERT(pathInstanceData); |
| 350 | int pathInstanceIdx = 0; |
| 351 | |
Chris Dalton | c9c97b7 | 2017-11-27 15:34:26 -0700 | [diff] [blame] | 352 | GrCCPRCoverageOpsBuilder atlasOpsBuilder(maxTotalPaths, maxPathPoints, numSkPoints, numSkVerbs); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 353 | SkDEBUGCODE(int skippedTotalPaths = 0;) |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 354 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 355 | // Allocate atlas(es) and fill out GPU instance buffers. |
| 356 | for (int i = 0; i < numOpListIDs; ++i) { |
| 357 | auto it = fRTPendingPathsMap.find(opListIDs[i]); |
| 358 | if (fRTPendingPathsMap.end() == it) { |
| 359 | continue; |
| 360 | } |
| 361 | RTPendingPaths& rtPendingPaths = it->second; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 362 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 363 | SkTInternalLList<DrawPathsOp>::Iter drawOpsIter; |
| 364 | drawOpsIter.init(rtPendingPaths.fDrawOps, |
| 365 | SkTInternalLList<DrawPathsOp>::Iter::kHead_IterStart); |
| 366 | while (DrawPathsOp* op = drawOpsIter.get()) { |
| 367 | pathInstanceIdx = op->setupResources(onFlushRP, &atlasOpsBuilder, pathInstanceData, |
| 368 | pathInstanceIdx); |
| 369 | drawOpsIter.next(); |
| 370 | SkDEBUGCODE(skippedTotalPaths += op->numSkippedInstances_debugOnly();) |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 371 | } |
| 372 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 373 | for (auto& clipsIter : rtPendingPaths.fClipPaths) { |
| 374 | clipsIter.second.placePathInAtlas(this, onFlushRP, &atlasOpsBuilder); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 375 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 376 | } |
| 377 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 378 | fPerFlushInstanceBuffer->unmap(); |
| 379 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 380 | SkASSERT(pathInstanceIdx == maxTotalPaths - skippedTotalPaths - numClipPaths); |
| 381 | |
| 382 | if (!fPerFlushAtlases.empty()) { |
| 383 | atlasOpsBuilder.emitOp(fPerFlushAtlases.back().drawBounds()); |
| 384 | } |
| 385 | |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 386 | SkSTArray<4, std::unique_ptr<GrCCPRCoverageOp>> atlasOps(fPerFlushAtlases.count()); |
| 387 | if (!atlasOpsBuilder.finalize(onFlushRP, &atlasOps)) { |
| 388 | SkDebugf("WARNING: failed to allocate ccpr atlas buffers. No paths will be drawn.\n"); |
| 389 | return; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 390 | } |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 391 | SkASSERT(atlasOps.count() == fPerFlushAtlases.count()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 392 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 393 | // Draw the coverage ops into their respective atlases. |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 394 | GrTAllocator<GrCCPRAtlas>::Iter atlasIter(&fPerFlushAtlases); |
| 395 | for (std::unique_ptr<GrCCPRCoverageOp>& atlasOp : atlasOps) { |
| 396 | SkAssertResult(atlasIter.next()); |
| 397 | GrCCPRAtlas* atlas = atlasIter.get(); |
| 398 | SkASSERT(atlasOp->bounds() == SkRect::MakeIWH(atlas->drawBounds().width(), |
| 399 | atlas->drawBounds().height())); |
| 400 | if (auto rtc = atlas->finalize(onFlushRP, std::move(atlasOp))) { |
| 401 | results->push_back(std::move(rtc)); |
| 402 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 403 | } |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 404 | SkASSERT(!atlasIter.next()); |
| 405 | |
| 406 | fPerFlushResourcesAreValid = true; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 407 | } |
| 408 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 409 | int CCPR::DrawPathsOp::setupResources(GrOnFlushResourceProvider* onFlushRP, |
| 410 | GrCCPRCoverageOpsBuilder* atlasOpsBuilder, |
| 411 | GrCCPRPathProcessor::Instance* pathInstanceData, |
| 412 | int pathInstanceIdx) { |
| 413 | const GrCCPRAtlas* currentAtlas = nullptr; |
| 414 | SkASSERT(fInstanceCount > 0); |
| 415 | SkASSERT(-1 == fBaseInstance); |
| 416 | fBaseInstance = pathInstanceIdx; |
| 417 | |
| 418 | for (const SingleDraw* draw = this->head(); draw; draw = draw->fNext) { |
| 419 | // parsePath gives us two tight bounding boxes: one in device space, as well as a second |
| 420 | // one rotated an additional 45 degrees. The path vertex shader uses these two bounding |
| 421 | // boxes to generate an octagon that circumscribes the path. |
| 422 | SkRect devBounds, devBounds45; |
| 423 | atlasOpsBuilder->parsePath(draw->fMatrix, draw->fPath, &devBounds, &devBounds45); |
| 424 | |
| 425 | SkIRect devIBounds; |
| 426 | devBounds.roundOut(&devIBounds); |
| 427 | |
| 428 | int16_t offsetX, offsetY; |
| 429 | GrCCPRAtlas* atlas = fCCPR->placeParsedPathInAtlas(onFlushRP, draw->fClipIBounds, |
| 430 | devIBounds, &offsetX, &offsetY, |
| 431 | atlasOpsBuilder); |
| 432 | if (!atlas) { |
| 433 | SkDEBUGCODE(++fNumSkippedInstances); |
| 434 | continue; |
| 435 | } |
| 436 | if (currentAtlas != atlas) { |
| 437 | if (currentAtlas) { |
| 438 | this->addAtlasBatch(currentAtlas, pathInstanceIdx); |
| 439 | } |
| 440 | currentAtlas = atlas; |
| 441 | } |
| 442 | |
| 443 | const SkMatrix& m = draw->fMatrix; |
| 444 | pathInstanceData[pathInstanceIdx++] = { |
| 445 | devBounds, |
| 446 | devBounds45, |
| 447 | {{m.getScaleX(), m.getSkewY(), m.getSkewX(), m.getScaleY()}}, |
| 448 | {{m.getTranslateX(), m.getTranslateY()}}, |
| 449 | {{offsetX, offsetY}}, |
| 450 | draw->fColor |
| 451 | }; |
| 452 | } |
| 453 | |
| 454 | SkASSERT(pathInstanceIdx == fBaseInstance + fInstanceCount - fNumSkippedInstances); |
| 455 | if (currentAtlas) { |
| 456 | this->addAtlasBatch(currentAtlas, pathInstanceIdx); |
| 457 | } |
| 458 | |
| 459 | return pathInstanceIdx; |
| 460 | } |
| 461 | |
| 462 | void CCPR::ClipPath::placePathInAtlas(GrCoverageCountingPathRenderer* ccpr, |
| 463 | GrOnFlushResourceProvider* onFlushRP, |
| 464 | GrCCPRCoverageOpsBuilder* atlasOpsBuilder) { |
| 465 | SkASSERT(!this->isUninitialized()); |
| 466 | SkASSERT(!fHasAtlas); |
| 467 | atlasOpsBuilder->parseDeviceSpacePath(fDeviceSpacePath); |
| 468 | fAtlas = ccpr->placeParsedPathInAtlas(onFlushRP, fAccessRect, fPathDevIBounds, &fAtlasOffsetX, |
| 469 | &fAtlasOffsetY, atlasOpsBuilder); |
| 470 | SkDEBUGCODE(fHasAtlas = true); |
| 471 | } |
| 472 | |
| 473 | GrCCPRAtlas* |
| 474 | GrCoverageCountingPathRenderer::placeParsedPathInAtlas(GrOnFlushResourceProvider* onFlushRP, |
| 475 | const SkIRect& clipIBounds, |
| 476 | const SkIRect& pathIBounds, |
| 477 | int16_t* atlasOffsetX, |
| 478 | int16_t* atlasOffsetY, |
| 479 | GrCCPRCoverageOpsBuilder* atlasOpsBuilder) { |
| 480 | using ScissorMode = GrCCPRCoverageOpsBuilder::ScissorMode; |
| 481 | |
| 482 | ScissorMode scissorMode; |
| 483 | SkIRect clippedPathIBounds; |
| 484 | if (clipIBounds.contains(pathIBounds)) { |
| 485 | clippedPathIBounds = pathIBounds; |
| 486 | scissorMode = ScissorMode::kNonScissored; |
| 487 | } else if (clippedPathIBounds.intersect(clipIBounds, pathIBounds)) { |
| 488 | scissorMode = ScissorMode::kScissored; |
| 489 | } else { |
| 490 | atlasOpsBuilder->discardParsedPath(); |
| 491 | return nullptr; |
| 492 | } |
| 493 | |
| 494 | SkIPoint16 atlasLocation; |
| 495 | const int h = clippedPathIBounds.height(), w = clippedPathIBounds.width(); |
| 496 | if (fPerFlushAtlases.empty() || !fPerFlushAtlases.back().addRect(w, h, &atlasLocation)) { |
| 497 | if (!fPerFlushAtlases.empty()) { |
| 498 | // The atlas is out of room and can't grow any bigger. |
| 499 | atlasOpsBuilder->emitOp(fPerFlushAtlases.back().drawBounds()); |
| 500 | } |
| 501 | fPerFlushAtlases.emplace_back(*onFlushRP->caps(), w, h).addRect(w, h, &atlasLocation); |
| 502 | } |
| 503 | |
| 504 | *atlasOffsetX = atlasLocation.x() - static_cast<int16_t>(clippedPathIBounds.left()); |
| 505 | *atlasOffsetY = atlasLocation.y() - static_cast<int16_t>(clippedPathIBounds.top()); |
| 506 | atlasOpsBuilder->saveParsedPath(scissorMode, clippedPathIBounds, *atlasOffsetX, *atlasOffsetY); |
| 507 | |
| 508 | return &fPerFlushAtlases.back(); |
| 509 | } |
| 510 | |
| 511 | void CCPR::DrawPathsOp::onExecute(GrOpFlushState* flushState) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 512 | SkASSERT(fCCPR->fFlushing); |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 513 | SkASSERT(flushState->rtCommandBuffer()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 514 | |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 515 | if (!fCCPR->fPerFlushResourcesAreValid) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 516 | return; // Setup failed. |
| 517 | } |
| 518 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 519 | SkASSERT(fBaseInstance >= 0); // Make sure setupResources has been called. |
Chris Dalton | 080baa4 | 2017-11-06 14:19:19 -0700 | [diff] [blame] | 520 | |
Chris Dalton | d151322 | 2017-10-06 08:30:46 -0600 | [diff] [blame] | 521 | GrPipeline::InitArgs initArgs; |
| 522 | initArgs.fFlags = fSRGBFlags; |
| 523 | initArgs.fProxy = flushState->drawOpArgs().fProxy; |
| 524 | initArgs.fCaps = &flushState->caps(); |
| 525 | initArgs.fResourceProvider = flushState->resourceProvider(); |
| 526 | initArgs.fDstProxy = flushState->drawOpArgs().fDstProxy; |
| 527 | GrPipeline pipeline(initArgs, std::move(fProcessors), flushState->detachAppliedClip()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 528 | |
| 529 | int baseInstance = fBaseInstance; |
| 530 | |
| 531 | for (int i = 0; i < fAtlasBatches.count(); baseInstance = fAtlasBatches[i++].fEndInstanceIdx) { |
| 532 | const AtlasBatch& batch = fAtlasBatches[i]; |
| 533 | SkASSERT(batch.fEndInstanceIdx > baseInstance); |
| 534 | |
| 535 | if (!batch.fAtlas->textureProxy()) { |
| 536 | continue; // Atlas failed to allocate. |
| 537 | } |
| 538 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 539 | GrCCPRPathProcessor coverProc(flushState->resourceProvider(), |
| 540 | sk_ref_sp(batch.fAtlas->textureProxy()), this->getFillType(), |
| 541 | *flushState->gpu()->caps()->shaderCaps()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 542 | |
| 543 | GrMesh mesh(GrPrimitiveType::kTriangles); |
| 544 | mesh.setIndexedInstanced(fCCPR->fPerFlushIndexBuffer.get(), |
| 545 | GrCCPRPathProcessor::kPerInstanceIndexCount, |
| 546 | fCCPR->fPerFlushInstanceBuffer.get(), |
| 547 | batch.fEndInstanceIdx - baseInstance, baseInstance); |
| 548 | mesh.setVertexData(fCCPR->fPerFlushVertexBuffer.get()); |
| 549 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 550 | flushState->rtCommandBuffer()->draw(pipeline, coverProc, &mesh, nullptr, 1, this->bounds()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 551 | } |
| 552 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 553 | SkASSERT(baseInstance == fBaseInstance + fInstanceCount - fNumSkippedInstances); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 554 | } |
| 555 | |
Chris Dalton | 3968ff9 | 2017-11-27 12:26:31 -0700 | [diff] [blame] | 556 | void GrCoverageCountingPathRenderer::postFlush(GrDeferredUploadToken, const uint32_t* opListIDs, |
| 557 | int numOpListIDs) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 558 | SkASSERT(fFlushing); |
| 559 | fPerFlushAtlases.reset(); |
| 560 | fPerFlushInstanceBuffer.reset(); |
| 561 | fPerFlushVertexBuffer.reset(); |
| 562 | fPerFlushIndexBuffer.reset(); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame^] | 563 | // We wait to erase these until after flush, once Ops and FPs are done accessing their data. |
| 564 | for (int i = 0; i < numOpListIDs; ++i) { |
| 565 | fRTPendingPathsMap.erase(opListIDs[i]); |
| 566 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 567 | SkDEBUGCODE(fFlushing = false;) |
| 568 | } |