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" |
| 16 | #include "GrOpFlushState.h" |
| 17 | #include "GrRenderTargetOpList.h" |
| 18 | #include "GrStyle.h" |
| 19 | #include "ccpr/GrCCPRPathProcessor.h" |
| 20 | |
| 21 | using DrawPathsOp = GrCoverageCountingPathRenderer::DrawPathsOp; |
| 22 | using ScissorMode = GrCCPRCoverageOpsBuilder::ScissorMode; |
| 23 | |
| 24 | bool GrCoverageCountingPathRenderer::IsSupported(const GrCaps& caps) { |
| 25 | const GrShaderCaps& shaderCaps = *caps.shaderCaps(); |
| 26 | return shaderCaps.geometryShaderSupport() && |
| 27 | shaderCaps.texelBufferSupport() && |
| 28 | shaderCaps.integerSupport() && |
| 29 | shaderCaps.flatInterpolationSupport() && |
| 30 | shaderCaps.maxVertexSamplers() >= 1 && |
| 31 | caps.instanceAttribSupport() && |
| 32 | caps.isConfigTexturable(kAlpha_half_GrPixelConfig) && |
| 33 | caps.isConfigRenderable(kAlpha_half_GrPixelConfig, /*withMSAA=*/false); |
| 34 | } |
| 35 | |
| 36 | sk_sp<GrCoverageCountingPathRenderer> |
| 37 | GrCoverageCountingPathRenderer::CreateIfSupported(const GrCaps& caps) { |
| 38 | return sk_sp<GrCoverageCountingPathRenderer>(IsSupported(caps) ? |
| 39 | new GrCoverageCountingPathRenderer : nullptr); |
| 40 | } |
| 41 | |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 42 | GrPathRenderer::CanDrawPath |
| 43 | GrCoverageCountingPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 44 | if (!args.fShape->style().isSimpleFill() || |
| 45 | args.fShape->inverseFilled() || |
| 46 | args.fViewMatrix->hasPerspective() || |
| 47 | GrAAType::kCoverage != args.fAAType) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 48 | return CanDrawPath::kNo; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | SkPath path; |
| 52 | args.fShape->asPath(&path); |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 53 | if (SkPathPriv::ConicWeightCnt(path)) { |
| 54 | return CanDrawPath::kNo; |
| 55 | } |
| 56 | |
Chris Dalton | db91c6e | 2017-09-08 16:25:08 -0600 | [diff] [blame^] | 57 | SkRect devBounds; |
| 58 | SkIRect devIBounds; |
| 59 | args.fViewMatrix->mapRect(&devBounds, path.getBounds()); |
| 60 | devBounds.roundOut(&devIBounds); |
| 61 | if (!devIBounds.intersect(*args.fClipConservativeBounds)) { |
| 62 | // Path is completely clipped away. Our code will eventually notice this before doing any |
| 63 | // real work. |
| 64 | return CanDrawPath::kYes; |
| 65 | } |
| 66 | |
| 67 | if (devIBounds.height() * devIBounds.width() > 256 * 256) { |
| 68 | // Large paths can blow up the atlas fast. And they are not ideal for a two-pass rendering |
| 69 | // algorithm. Give the simpler direct renderers a chance before we commit to drawing it. |
| 70 | return CanDrawPath::kAsBackup; |
| 71 | } |
| 72 | |
| 73 | if (args.fShape->hasUnstyledKey() && path.countVerbs() > 50) { |
| 74 | // Complex paths do better cached in an SDF, if the renderer will accept them. |
| 75 | return CanDrawPath::kAsBackup; |
| 76 | } |
| 77 | |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 78 | return CanDrawPath::kYes; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | bool GrCoverageCountingPathRenderer::onDrawPath(const DrawPathArgs& args) { |
| 82 | SkASSERT(!fFlushing); |
| 83 | SkASSERT(!args.fShape->isEmpty()); |
| 84 | |
| 85 | auto op = skstd::make_unique<DrawPathsOp>(this, args, args.fPaint.getColor()); |
| 86 | args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op)); |
| 87 | |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | GrCoverageCountingPathRenderer::DrawPathsOp::DrawPathsOp(GrCoverageCountingPathRenderer* ccpr, |
| 92 | const DrawPathArgs& args, GrColor color) |
| 93 | : INHERITED(ClassID()) |
| 94 | , fCCPR(ccpr) |
| 95 | , fSRGBFlags(GrPipeline::SRGBFlagsFromPaint(args.fPaint)) |
| 96 | , fProcessors(std::move(args.fPaint)) |
| 97 | , fTailDraw(&fHeadDraw) |
| 98 | , fOwningRTPendingOps(nullptr) { |
| 99 | SkDEBUGCODE(fBaseInstance = -1); |
| 100 | SkDEBUGCODE(fDebugInstanceCount = 1;) |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 101 | SkDEBUGCODE(fDebugSkippedInstances = 0;) |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 102 | |
| 103 | GrRenderTargetContext* const rtc = args.fRenderTargetContext; |
| 104 | |
| 105 | SkRect devBounds; |
| 106 | args.fViewMatrix->mapRect(&devBounds, args.fShape->bounds()); |
| 107 | |
| 108 | args.fClip->getConservativeBounds(rtc->width(), rtc->height(), &fHeadDraw.fClipBounds, nullptr); |
| 109 | fHeadDraw.fScissorMode = fHeadDraw.fClipBounds.contains(devBounds) ? |
| 110 | ScissorMode::kNonScissored : ScissorMode::kScissored; |
| 111 | fHeadDraw.fMatrix = *args.fViewMatrix; |
| 112 | args.fShape->asPath(&fHeadDraw.fPath); |
| 113 | fHeadDraw.fColor = color; // Can't call args.fPaint.getColor() because it has been std::move'd. |
| 114 | |
| 115 | // FIXME: intersect with clip bounds to (hopefully) improve batching. |
| 116 | // (This is nontrivial due to assumptions in generating the octagon cover geometry.) |
| 117 | this->setBounds(devBounds, GrOp::HasAABloat::kYes, GrOp::IsZeroArea::kNo); |
| 118 | } |
| 119 | |
| 120 | GrDrawOp::RequiresDstTexture DrawPathsOp::finalize(const GrCaps& caps, const GrAppliedClip* clip) { |
| 121 | SingleDraw& onlyDraw = this->getOnlyPathDraw(); |
| 122 | GrProcessorSet::Analysis analysis = fProcessors.finalize(onlyDraw.fColor, |
| 123 | GrProcessorAnalysisCoverage::kSingleChannel, |
| 124 | clip, false, caps, &onlyDraw.fColor); |
| 125 | return analysis.requiresDstTexture() ? RequiresDstTexture::kYes : RequiresDstTexture::kNo; |
| 126 | } |
| 127 | |
| 128 | bool DrawPathsOp::onCombineIfPossible(GrOp* op, const GrCaps& caps) { |
| 129 | DrawPathsOp* that = op->cast<DrawPathsOp>(); |
| 130 | SkASSERT(fCCPR == that->fCCPR); |
| 131 | SkASSERT(fOwningRTPendingOps); |
| 132 | SkASSERT(fDebugInstanceCount); |
| 133 | SkASSERT(that->fDebugInstanceCount); |
| 134 | |
| 135 | if (this->getFillType() != that->getFillType() || |
| 136 | fSRGBFlags != that->fSRGBFlags || |
| 137 | fProcessors != that->fProcessors) { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | if (RTPendingOps* owningRTPendingOps = that->fOwningRTPendingOps) { |
| 142 | SkASSERT(owningRTPendingOps == fOwningRTPendingOps); |
| 143 | owningRTPendingOps->fOpList.remove(that); |
| 144 | } else { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 145 | // The Op is being combined immediately after creation, before a call to wasRecorded. In |
| 146 | // this case wasRecorded will not be called. So we count its path here instead. |
| 147 | const SingleDraw& onlyDraw = that->getOnlyPathDraw(); |
| 148 | ++fOwningRTPendingOps->fNumTotalPaths; |
| 149 | fOwningRTPendingOps->fNumSkPoints += onlyDraw.fPath.countPoints(); |
| 150 | fOwningRTPendingOps->fNumSkVerbs += onlyDraw.fPath.countVerbs(); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | fTailDraw->fNext = &fOwningRTPendingOps->fDrawsAllocator.push_back(that->fHeadDraw); |
| 154 | fTailDraw = that->fTailDraw == &that->fHeadDraw ? fTailDraw->fNext : that->fTailDraw; |
| 155 | |
| 156 | this->joinBounds(*that); |
| 157 | |
| 158 | SkDEBUGCODE(fDebugInstanceCount += that->fDebugInstanceCount;) |
| 159 | SkDEBUGCODE(that->fDebugInstanceCount = 0); |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | void DrawPathsOp::wasRecorded(GrRenderTargetOpList* opList) { |
| 164 | SkASSERT(!fOwningRTPendingOps); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 165 | const SingleDraw& onlyDraw = this->getOnlyPathDraw(); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 166 | fOwningRTPendingOps = &fCCPR->fRTPendingOpsMap[opList->uniqueID()]; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 167 | ++fOwningRTPendingOps->fNumTotalPaths; |
| 168 | fOwningRTPendingOps->fNumSkPoints += onlyDraw.fPath.countPoints(); |
| 169 | fOwningRTPendingOps->fNumSkVerbs += onlyDraw.fPath.countVerbs(); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 170 | fOwningRTPendingOps->fOpList.addToTail(this); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void GrCoverageCountingPathRenderer::preFlush(GrOnFlushResourceProvider* onFlushRP, |
| 174 | const uint32_t* opListIDs, int numOpListIDs, |
| 175 | SkTArray<sk_sp<GrRenderTargetContext>>* results) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 176 | SkASSERT(!fFlushing); |
| 177 | SkDEBUGCODE(fFlushing = true;) |
| 178 | |
| 179 | if (fRTPendingOpsMap.empty()) { |
| 180 | return; // Nothing to draw. |
| 181 | } |
| 182 | |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 183 | this->setupPerFlushResources(onFlushRP, opListIDs, numOpListIDs, results); |
| 184 | |
| 185 | // Erase these last, once we are done accessing data from the SingleDraw allocators. |
| 186 | for (int i = 0; i < numOpListIDs; ++i) { |
| 187 | fRTPendingOpsMap.erase(opListIDs[i]); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | void GrCoverageCountingPathRenderer::setupPerFlushResources(GrOnFlushResourceProvider* onFlushRP, |
| 192 | const uint32_t* opListIDs, |
| 193 | int numOpListIDs, |
| 194 | SkTArray<sk_sp<GrRenderTargetContext>>* results) { |
| 195 | using PathInstance = GrCCPRPathProcessor::Instance; |
| 196 | |
| 197 | SkASSERT(!fPerFlushIndexBuffer); |
| 198 | SkASSERT(!fPerFlushVertexBuffer); |
| 199 | SkASSERT(!fPerFlushInstanceBuffer); |
| 200 | SkASSERT(fPerFlushAtlases.empty()); |
| 201 | |
| 202 | fPerFlushResourcesAreValid = false; |
| 203 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 204 | SkTInternalLList<DrawPathsOp> flushingOps; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 205 | int maxTotalPaths = 0, numSkPoints = 0, numSkVerbs = 0; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 206 | |
| 207 | for (int i = 0; i < numOpListIDs; ++i) { |
| 208 | auto it = fRTPendingOpsMap.find(opListIDs[i]); |
| 209 | if (fRTPendingOpsMap.end() != it) { |
| 210 | RTPendingOps& rtPendingOps = it->second; |
| 211 | SkASSERT(!rtPendingOps.fOpList.isEmpty()); |
| 212 | flushingOps.concat(std::move(rtPendingOps.fOpList)); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 213 | maxTotalPaths += rtPendingOps.fNumTotalPaths; |
| 214 | numSkPoints += rtPendingOps.fNumSkPoints; |
| 215 | numSkVerbs += rtPendingOps.fNumSkVerbs; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 219 | SkASSERT(flushingOps.isEmpty() == !maxTotalPaths); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 220 | if (flushingOps.isEmpty()) { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 221 | return; // Nothing to draw. |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | fPerFlushIndexBuffer = GrCCPRPathProcessor::FindOrMakeIndexBuffer(onFlushRP); |
| 225 | if (!fPerFlushIndexBuffer) { |
| 226 | SkDebugf("WARNING: failed to allocate ccpr path index buffer.\n"); |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | fPerFlushVertexBuffer = GrCCPRPathProcessor::FindOrMakeVertexBuffer(onFlushRP); |
| 231 | if (!fPerFlushVertexBuffer) { |
| 232 | SkDebugf("WARNING: failed to allocate ccpr path vertex buffer.\n"); |
| 233 | return; |
| 234 | } |
| 235 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 236 | fPerFlushInstanceBuffer = onFlushRP->makeBuffer(kVertex_GrBufferType, |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 237 | maxTotalPaths * sizeof(PathInstance)); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 238 | if (!fPerFlushInstanceBuffer) { |
| 239 | SkDebugf("WARNING: failed to allocate path instance buffer. No paths will be drawn.\n"); |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | PathInstance* pathInstanceData = static_cast<PathInstance*>(fPerFlushInstanceBuffer->map()); |
| 244 | SkASSERT(pathInstanceData); |
| 245 | int pathInstanceIdx = 0; |
| 246 | |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 247 | GrCCPRCoverageOpsBuilder atlasOpsBuilder(maxTotalPaths, numSkPoints, numSkVerbs); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 248 | GrCCPRAtlas* atlas = nullptr; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 249 | SkDEBUGCODE(int skippedTotalPaths = 0;) |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 250 | |
| 251 | SkTInternalLList<DrawPathsOp>::Iter iter; |
| 252 | iter.init(flushingOps, SkTInternalLList<DrawPathsOp>::Iter::kHead_IterStart); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 253 | while (DrawPathsOp* drawPathOp = iter.get()) { |
| 254 | SkASSERT(drawPathOp->fDebugInstanceCount > 0); |
| 255 | SkASSERT(-1 == drawPathOp->fBaseInstance); |
| 256 | drawPathOp->fBaseInstance = pathInstanceIdx; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 257 | |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 258 | for (const auto* draw = &drawPathOp->fHeadDraw; draw; draw = draw->fNext) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 259 | // parsePath gives us two tight bounding boxes: one in device space, as well as a second |
| 260 | // one rotated an additional 45 degrees. The path vertex shader uses these two bounding |
| 261 | // boxes to generate an octagon that circumscribes the path. |
| 262 | SkRect devBounds, devBounds45; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 263 | atlasOpsBuilder.parsePath(draw->fMatrix, draw->fPath, &devBounds, &devBounds45); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 264 | |
| 265 | SkRect clippedDevBounds = devBounds; |
| 266 | if (ScissorMode::kScissored == draw->fScissorMode && |
| 267 | !clippedDevBounds.intersect(devBounds, SkRect::Make(draw->fClipBounds))) { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 268 | SkDEBUGCODE(++drawPathOp->fDebugSkippedInstances); |
| 269 | atlasOpsBuilder.discardParsedPath(); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 270 | continue; |
| 271 | } |
| 272 | |
| 273 | SkIRect clippedDevIBounds; |
| 274 | clippedDevBounds.roundOut(&clippedDevIBounds); |
| 275 | const int h = clippedDevIBounds.height(), w = clippedDevIBounds.width(); |
| 276 | |
| 277 | SkIPoint16 atlasLocation; |
| 278 | if (atlas && !atlas->addRect(w, h, &atlasLocation)) { |
| 279 | // The atlas is out of room and can't grow any bigger. |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 280 | atlasOpsBuilder.emitOp(atlas->drawBounds()); |
| 281 | if (pathInstanceIdx > drawPathOp->fBaseInstance) { |
| 282 | drawPathOp->addAtlasBatch(atlas, pathInstanceIdx); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 283 | } |
| 284 | atlas = nullptr; |
| 285 | } |
| 286 | |
| 287 | if (!atlas) { |
| 288 | atlas = &fPerFlushAtlases.emplace_back(*onFlushRP->caps(), w, h); |
| 289 | SkAssertResult(atlas->addRect(w, h, &atlasLocation)); |
| 290 | } |
| 291 | |
| 292 | const SkMatrix& m = draw->fMatrix; |
| 293 | const int16_t offsetX = atlasLocation.x() - static_cast<int16_t>(clippedDevIBounds.x()), |
| 294 | offsetY = atlasLocation.y() - static_cast<int16_t>(clippedDevIBounds.y()); |
| 295 | |
| 296 | pathInstanceData[pathInstanceIdx++] = { |
| 297 | devBounds, |
| 298 | devBounds45, |
| 299 | {{m.getScaleX(), m.getSkewY(), m.getSkewX(), m.getScaleY()}}, |
| 300 | {{m.getTranslateX(), m.getTranslateY()}}, |
| 301 | {{offsetX, offsetY}}, |
| 302 | draw->fColor |
| 303 | }; |
| 304 | |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 305 | atlasOpsBuilder.saveParsedPath(draw->fScissorMode, clippedDevIBounds, offsetX, offsetY); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 306 | } |
| 307 | |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 308 | SkASSERT(pathInstanceIdx == drawPathOp->fBaseInstance + drawPathOp->fDebugInstanceCount - |
| 309 | drawPathOp->fDebugSkippedInstances); |
| 310 | if (pathInstanceIdx > drawPathOp->fBaseInstance) { |
| 311 | drawPathOp->addAtlasBatch(atlas, pathInstanceIdx); |
| 312 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 313 | |
| 314 | iter.next(); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 315 | SkDEBUGCODE(skippedTotalPaths += drawPathOp->fDebugSkippedInstances;) |
| 316 | } |
| 317 | SkASSERT(pathInstanceIdx == maxTotalPaths - skippedTotalPaths); |
| 318 | |
| 319 | if (atlas) { |
| 320 | atlasOpsBuilder.emitOp(atlas->drawBounds()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 321 | } |
| 322 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 323 | fPerFlushInstanceBuffer->unmap(); |
| 324 | |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 325 | // Draw the coverage ops into their respective atlases. |
| 326 | SkSTArray<4, std::unique_ptr<GrCCPRCoverageOp>> atlasOps(fPerFlushAtlases.count()); |
| 327 | if (!atlasOpsBuilder.finalize(onFlushRP, &atlasOps)) { |
| 328 | SkDebugf("WARNING: failed to allocate ccpr atlas buffers. No paths will be drawn.\n"); |
| 329 | return; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 330 | } |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 331 | SkASSERT(atlasOps.count() == fPerFlushAtlases.count()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 332 | |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 333 | GrTAllocator<GrCCPRAtlas>::Iter atlasIter(&fPerFlushAtlases); |
| 334 | for (std::unique_ptr<GrCCPRCoverageOp>& atlasOp : atlasOps) { |
| 335 | SkAssertResult(atlasIter.next()); |
| 336 | GrCCPRAtlas* atlas = atlasIter.get(); |
| 337 | SkASSERT(atlasOp->bounds() == SkRect::MakeIWH(atlas->drawBounds().width(), |
| 338 | atlas->drawBounds().height())); |
| 339 | if (auto rtc = atlas->finalize(onFlushRP, std::move(atlasOp))) { |
| 340 | results->push_back(std::move(rtc)); |
| 341 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 342 | } |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 343 | SkASSERT(!atlasIter.next()); |
| 344 | |
| 345 | fPerFlushResourcesAreValid = true; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | void DrawPathsOp::onExecute(GrOpFlushState* flushState) { |
| 349 | SkASSERT(fCCPR->fFlushing); |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 350 | SkASSERT(flushState->rtCommandBuffer()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 351 | |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 352 | if (!fCCPR->fPerFlushResourcesAreValid) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 353 | return; // Setup failed. |
| 354 | } |
| 355 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 356 | GrPipeline::InitArgs args; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 357 | args.fCaps = &flushState->caps(); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 358 | args.fFlags = fSRGBFlags; |
Robert Phillips | 2890fbf | 2017-07-26 15:48:41 -0400 | [diff] [blame] | 359 | args.fProxy = flushState->drawOpArgs().fProxy; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 360 | args.fDstProxy = flushState->drawOpArgs().fDstProxy; |
Brian Salomon | bfd18cd | 2017-08-09 16:27:09 -0400 | [diff] [blame] | 361 | GrPipeline pipeline(args, std::move(fProcessors), flushState->detachAppliedClip()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 362 | |
| 363 | int baseInstance = fBaseInstance; |
| 364 | |
| 365 | for (int i = 0; i < fAtlasBatches.count(); baseInstance = fAtlasBatches[i++].fEndInstanceIdx) { |
| 366 | const AtlasBatch& batch = fAtlasBatches[i]; |
| 367 | SkASSERT(batch.fEndInstanceIdx > baseInstance); |
| 368 | |
| 369 | if (!batch.fAtlas->textureProxy()) { |
| 370 | continue; // Atlas failed to allocate. |
| 371 | } |
| 372 | |
| 373 | GrCCPRPathProcessor coverProc(flushState->resourceProvider(), batch.fAtlas->textureProxy(), |
| 374 | this->getFillType(), *flushState->gpu()->caps()->shaderCaps()); |
| 375 | |
| 376 | GrMesh mesh(GrPrimitiveType::kTriangles); |
| 377 | mesh.setIndexedInstanced(fCCPR->fPerFlushIndexBuffer.get(), |
| 378 | GrCCPRPathProcessor::kPerInstanceIndexCount, |
| 379 | fCCPR->fPerFlushInstanceBuffer.get(), |
| 380 | batch.fEndInstanceIdx - baseInstance, baseInstance); |
| 381 | mesh.setVertexData(fCCPR->fPerFlushVertexBuffer.get()); |
| 382 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 383 | flushState->rtCommandBuffer()->draw(pipeline, coverProc, &mesh, nullptr, 1, this->bounds()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 384 | } |
| 385 | |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 386 | SkASSERT(baseInstance == fBaseInstance + fDebugInstanceCount - fDebugSkippedInstances); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | void GrCoverageCountingPathRenderer::postFlush() { |
| 390 | SkASSERT(fFlushing); |
| 391 | fPerFlushAtlases.reset(); |
| 392 | fPerFlushInstanceBuffer.reset(); |
| 393 | fPerFlushVertexBuffer.reset(); |
| 394 | fPerFlushIndexBuffer.reset(); |
| 395 | SkDEBUGCODE(fFlushing = false;) |
| 396 | } |