blob: ee0dbde79934c7895ad10dceddb5bc6449bb5174 [file] [log] [blame]
Chris Dalton1a325d22017-07-14 15:17:41 -06001/*
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"
Robert Phillips777707b2018-01-17 11:40:14 -050012#include "GrProxyProvider.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060013#include "SkMakeUnique.h"
Chris Daltona039d3b2017-09-28 11:16:36 -060014#include "SkPathOps.h"
Chris Dalton383a2ef2018-01-08 17:21:41 -050015#include "ccpr/GrCCClipProcessor.h"
Chris Daltond7e22272018-05-23 10:17:17 -060016#include "ccpr/GrCCDrawPathsOp.h"
Chris Dalton5ba36ba2018-05-09 01:08:38 -060017#include "ccpr/GrCCPathParser.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060018
Chris Dalton5ba36ba2018-05-09 01:08:38 -060019using PathInstance = GrCCPathProcessor::Instance;
Chris Daltona32a3c32017-12-05 10:05:21 -070020
21// If a path spans more pixels than this, we need to crop it or else analytic AA can run out of fp32
22// precision.
23static constexpr float kPathCropThreshold = 1 << 16;
24
25static void crop_path(const SkPath& path, const SkIRect& cropbox, SkPath* out) {
Chris Dalton5ba36ba2018-05-09 01:08:38 -060026 SkPath cropboxPath;
27 cropboxPath.addRect(SkRect::Make(cropbox));
28 if (!Op(cropboxPath, path, kIntersect_SkPathOp, out)) {
Chris Daltona32a3c32017-12-05 10:05:21 -070029 // This can fail if the PathOps encounter NaN or infinities.
30 out->reset();
31 }
Chris Dalton5ba36ba2018-05-09 01:08:38 -060032 out->setIsVolatile(true);
Chris Daltona32a3c32017-12-05 10:05:21 -070033}
Chris Dalton1a325d22017-07-14 15:17:41 -060034
35bool GrCoverageCountingPathRenderer::IsSupported(const GrCaps& caps) {
36 const GrShaderCaps& shaderCaps = *caps.shaderCaps();
Chris Dalton383a2ef2018-01-08 17:21:41 -050037 return shaderCaps.integerSupport() && shaderCaps.flatInterpolationSupport() &&
38 caps.instanceAttribSupport() && GrCaps::kNone_MapFlags != caps.mapBufferFlags() &&
Chris Dalton1a325d22017-07-14 15:17:41 -060039 caps.isConfigTexturable(kAlpha_half_GrPixelConfig) &&
Brian Salomonbdecacf2018-02-02 20:32:49 -050040 caps.isConfigRenderable(kAlpha_half_GrPixelConfig) &&
Chris Daltone4679fa2017-09-29 13:58:26 -060041 !caps.blacklistCoverageCounting();
Chris Dalton1a325d22017-07-14 15:17:41 -060042}
43
Chris Dalton383a2ef2018-01-08 17:21:41 -050044sk_sp<GrCoverageCountingPathRenderer> GrCoverageCountingPathRenderer::CreateIfSupported(
45 const GrCaps& caps, bool drawCachablePaths) {
Chris Daltona2ac30d2017-10-17 10:40:01 -060046 auto ccpr = IsSupported(caps) ? new GrCoverageCountingPathRenderer(drawCachablePaths) : nullptr;
47 return sk_sp<GrCoverageCountingPathRenderer>(ccpr);
Chris Dalton1a325d22017-07-14 15:17:41 -060048}
49
Chris Daltond7e22272018-05-23 10:17:17 -060050GrCCPerOpListPaths* GrCoverageCountingPathRenderer::lookupPendingPaths(uint32_t opListID) {
51 auto it = fPendingPaths.find(opListID);
52 if (fPendingPaths.end() == it) {
53 auto paths = skstd::make_unique<GrCCPerOpListPaths>();
54 it = fPendingPaths.insert(std::make_pair(opListID, std::move(paths))).first;
55 }
56 return it->second.get();
Chris Dalton5ba36ba2018-05-09 01:08:38 -060057}
58
Chris Daltond7e22272018-05-23 10:17:17 -060059void GrCoverageCountingPathRenderer::adoptAndRecordOp(GrCCDrawPathsOp* op,
60 const DrawPathArgs& args) {
61 GrRenderTargetContext* rtc = args.fRenderTargetContext;
62 if (uint32_t opListID = rtc->addDrawOp(*args.fClip, std::unique_ptr<GrDrawOp>(op))) {
63 // If the Op wasn't dropped or combined, give it a pointer to its owning GrCCPerOpListPaths.
64 op->wasRecorded(this->lookupPendingPaths(opListID));
65 }
Chris Dalton5ba36ba2018-05-09 01:08:38 -060066}
67
Chris Dalton383a2ef2018-01-08 17:21:41 -050068GrPathRenderer::CanDrawPath GrCoverageCountingPathRenderer::onCanDrawPath(
69 const CanDrawPathArgs& args) const {
Chris Daltona2ac30d2017-10-17 10:40:01 -060070 if (args.fShape->hasUnstyledKey() && !fDrawCachablePaths) {
71 return CanDrawPath::kNo;
72 }
73
Chris Dalton383a2ef2018-01-08 17:21:41 -050074 if (!args.fShape->style().isSimpleFill() || args.fShape->inverseFilled() ||
75 args.fViewMatrix->hasPerspective() || GrAAType::kCoverage != args.fAAType) {
Chris Dalton5ed44232017-09-07 13:22:46 -060076 return CanDrawPath::kNo;
Chris Dalton1a325d22017-07-14 15:17:41 -060077 }
78
79 SkPath path;
80 args.fShape->asPath(&path);
Chris Daltondb91c6e2017-09-08 16:25:08 -060081 SkRect devBounds;
82 SkIRect devIBounds;
83 args.fViewMatrix->mapRect(&devBounds, path.getBounds());
84 devBounds.roundOut(&devIBounds);
85 if (!devIBounds.intersect(*args.fClipConservativeBounds)) {
86 // Path is completely clipped away. Our code will eventually notice this before doing any
87 // real work.
88 return CanDrawPath::kYes;
89 }
90
91 if (devIBounds.height() * devIBounds.width() > 256 * 256) {
92 // Large paths can blow up the atlas fast. And they are not ideal for a two-pass rendering
93 // algorithm. Give the simpler direct renderers a chance before we commit to drawing it.
94 return CanDrawPath::kAsBackup;
95 }
96
97 if (args.fShape->hasUnstyledKey() && path.countVerbs() > 50) {
98 // Complex paths do better cached in an SDF, if the renderer will accept them.
99 return CanDrawPath::kAsBackup;
100 }
101
Chris Dalton5ed44232017-09-07 13:22:46 -0600102 return CanDrawPath::kYes;
Chris Dalton1a325d22017-07-14 15:17:41 -0600103}
104
105bool GrCoverageCountingPathRenderer::onDrawPath(const DrawPathArgs& args) {
106 SkASSERT(!fFlushing);
Chris Dalton1a325d22017-07-14 15:17:41 -0600107
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600108 SkIRect clipIBounds;
109 GrRenderTargetContext* rtc = args.fRenderTargetContext;
110 args.fClip->getConservativeBounds(rtc->width(), rtc->height(), &clipIBounds, nullptr);
111
112 SkPath path;
113 args.fShape->asPath(&path);
Chris Dalton1a325d22017-07-14 15:17:41 -0600114
115 SkRect devBounds;
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600116 args.fViewMatrix->mapRect(&devBounds, path.getBounds());
117
Chris Daltona32a3c32017-12-05 10:05:21 -0700118 if (SkTMax(devBounds.height(), devBounds.width()) > kPathCropThreshold) {
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600119 // The path is too large. Crop it or analytic AA can run out of fp32 precision.
120 SkPath croppedPath;
121 path.transform(*args.fViewMatrix, &croppedPath);
122 crop_path(croppedPath, clipIBounds, &croppedPath);
Chris Daltond7e22272018-05-23 10:17:17 -0600123 this->adoptAndRecordOp(new GrCCDrawPathsOp(std::move(args.fPaint), clipIBounds,
Chris Daltonf104fec2018-05-22 16:17:48 -0600124 SkMatrix::I(), croppedPath,
125 croppedPath.getBounds()), args);
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600126 return true;
Chris Dalton1a325d22017-07-14 15:17:41 -0600127 }
128
Chris Daltond7e22272018-05-23 10:17:17 -0600129 this->adoptAndRecordOp(new GrCCDrawPathsOp(std::move(args.fPaint), clipIBounds,
Chris Daltonf104fec2018-05-22 16:17:48 -0600130 *args.fViewMatrix, path, devBounds), args);
Chris Dalton1a325d22017-07-14 15:17:41 -0600131 return true;
132}
133
Chris Dalton383a2ef2018-01-08 17:21:41 -0500134std::unique_ptr<GrFragmentProcessor> GrCoverageCountingPathRenderer::makeClipProcessor(
Robert Phillips777707b2018-01-17 11:40:14 -0500135 GrProxyProvider* proxyProvider,
136 uint32_t opListID, const SkPath& deviceSpacePath, const SkIRect& accessRect,
137 int rtWidth, int rtHeight) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500138 using MustCheckBounds = GrCCClipProcessor::MustCheckBounds;
Chris Daltona32a3c32017-12-05 10:05:21 -0700139
140 SkASSERT(!fFlushing);
Chris Daltona32a3c32017-12-05 10:05:21 -0700141
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600142 GrCCClipPath& clipPath =
Chris Daltond7e22272018-05-23 10:17:17 -0600143 this->lookupPendingPaths(opListID)->fClipPaths[deviceSpacePath.getGenerationID()];
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600144 if (!clipPath.isInitialized()) {
Chris Daltona32a3c32017-12-05 10:05:21 -0700145 // This ClipPath was just created during lookup. Initialize it.
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600146 const SkRect& pathDevBounds = deviceSpacePath.getBounds();
147 if (SkTMax(pathDevBounds.height(), pathDevBounds.width()) > kPathCropThreshold) {
148 // The path is too large. Crop it or analytic AA can run out of fp32 precision.
149 SkPath croppedPath;
150 int maxRTSize = proxyProvider->caps()->maxRenderTargetSize();
151 crop_path(deviceSpacePath, SkIRect::MakeWH(maxRTSize, maxRTSize), &croppedPath);
152 clipPath.init(proxyProvider, croppedPath, accessRect, rtWidth, rtHeight);
153 } else {
154 clipPath.init(proxyProvider, deviceSpacePath, accessRect, rtWidth, rtHeight);
155 }
Chris Daltona32a3c32017-12-05 10:05:21 -0700156 } else {
157 clipPath.addAccess(accessRect);
158 }
159
160 bool mustCheckBounds = !clipPath.pathDevIBounds().contains(accessRect);
Chris Dalton383a2ef2018-01-08 17:21:41 -0500161 return skstd::make_unique<GrCCClipProcessor>(&clipPath, MustCheckBounds(mustCheckBounds),
162 deviceSpacePath.getFillType());
Chris Daltona32a3c32017-12-05 10:05:21 -0700163}
164
Chris Dalton1a325d22017-07-14 15:17:41 -0600165void GrCoverageCountingPathRenderer::preFlush(GrOnFlushResourceProvider* onFlushRP,
166 const uint32_t* opListIDs, int numOpListIDs,
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600167 SkTArray<sk_sp<GrRenderTargetContext>>* atlasDraws) {
Chris Daltona32a3c32017-12-05 10:05:21 -0700168 SkASSERT(!fFlushing);
Chris Daltond7e22272018-05-23 10:17:17 -0600169 SkASSERT(fFlushingPaths.empty());
Chris Dalton383a2ef2018-01-08 17:21:41 -0500170 SkDEBUGCODE(fFlushing = true);
Chris Daltona32a3c32017-12-05 10:05:21 -0700171
Chris Daltond7e22272018-05-23 10:17:17 -0600172 if (fPendingPaths.empty()) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500173 return; // Nothing to draw.
Chris Daltona32a3c32017-12-05 10:05:21 -0700174 }
Chris Daltonc1e59632017-09-05 00:30:07 -0600175
Chris Daltond7e22272018-05-23 10:17:17 -0600176 // Move the per-opList paths that are about to be flushed from fPendingPaths to fFlushingPaths,
177 // and count up the paths about to be flushed so we can preallocate buffers.
Chris Dalton3917b1e2018-05-09 00:40:52 -0600178 int numPathDraws = 0;
179 int numClipPaths = 0;
180 GrCCPathParser::PathStats flushingPathStats;
Chris Daltond7e22272018-05-23 10:17:17 -0600181 fFlushingPaths.reserve(numOpListIDs);
Chris Dalton1a325d22017-07-14 15:17:41 -0600182 for (int i = 0; i < numOpListIDs; ++i) {
Chris Daltond7e22272018-05-23 10:17:17 -0600183 auto iter = fPendingPaths.find(opListIDs[i]);
184 if (fPendingPaths.end() == iter) {
185 continue; // No paths on this opList.
Chris Dalton1a325d22017-07-14 15:17:41 -0600186 }
Chris Daltona32a3c32017-12-05 10:05:21 -0700187
Chris Daltond7e22272018-05-23 10:17:17 -0600188 fFlushingPaths.push_back(std::move(iter->second)).get();
189 fPendingPaths.erase(iter);
190
191 for (const GrCCDrawPathsOp* op : fFlushingPaths.back()->fDrawOps) {
Chris Dalton4bfb50b2018-05-21 09:10:53 -0600192 numPathDraws += op->countPaths(&flushingPathStats);
Chris Dalton080baa42017-11-06 14:19:19 -0700193 }
Chris Daltond7e22272018-05-23 10:17:17 -0600194 for (const auto& clipsIter : fFlushingPaths.back()->fClipPaths) {
Chris Dalton3917b1e2018-05-09 00:40:52 -0600195 flushingPathStats.statPath(clipsIter.second.deviceSpacePath());
Chris Daltona32a3c32017-12-05 10:05:21 -0700196 }
Chris Daltond7e22272018-05-23 10:17:17 -0600197 numClipPaths += fFlushingPaths.back()->fClipPaths.size();
Chris Dalton1a325d22017-07-14 15:17:41 -0600198 }
199
Chris Dalton3917b1e2018-05-09 00:40:52 -0600200 if (0 == numPathDraws + numClipPaths) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500201 return; // Nothing to draw.
Chris Dalton1a325d22017-07-14 15:17:41 -0600202 }
203
Chris Daltond7e22272018-05-23 10:17:17 -0600204 auto resources = sk_make_sp<GrCCPerFlushResources>(onFlushRP, numPathDraws, numClipPaths,
205 flushingPathStats);
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600206 if (!resources->isMapped()) {
207 return; // Some allocation failed.
Chris Dalton1a325d22017-07-14 15:17:41 -0600208 }
209
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600210 // Layout atlas(es) and parse paths.
Chris Dalton3917b1e2018-05-09 00:40:52 -0600211 SkDEBUGCODE(int numSkippedPaths = 0);
Chris Daltond7e22272018-05-23 10:17:17 -0600212 for (const auto& flushingPaths : fFlushingPaths) {
213 for (GrCCDrawPathsOp* op : flushingPaths->fDrawOps) {
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600214 op->setupResources(resources.get(), onFlushRP);
Chris Dalton3917b1e2018-05-09 00:40:52 -0600215 SkDEBUGCODE(numSkippedPaths += op->numSkippedInstances_debugOnly());
Chris Dalton1a325d22017-07-14 15:17:41 -0600216 }
Chris Daltond7e22272018-05-23 10:17:17 -0600217 for (auto& clipsIter : flushingPaths->fClipPaths) {
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600218 clipsIter.second.placePathInAtlas(resources.get(), onFlushRP);
Chris Daltonc1e59632017-09-05 00:30:07 -0600219 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600220 }
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600221 SkASSERT(resources->pathInstanceCount() == numPathDraws - numSkippedPaths);
Chris Dalton1a325d22017-07-14 15:17:41 -0600222
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600223 // Allocate the atlases and create instance buffers to draw them.
224 if (!resources->finalize(onFlushRP, atlasDraws)) {
Chris Daltonc1e59632017-09-05 00:30:07 -0600225 return;
Chris Dalton1a325d22017-07-14 15:17:41 -0600226 }
227
Chris Daltond7e22272018-05-23 10:17:17 -0600228 // Commit flushing paths to the resources once they are successfully completed.
229 for (auto& flushingPaths : fFlushingPaths) {
230 flushingPaths->fFlushResources = resources;
231 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600232}
233
Chris Dalton3968ff92017-11-27 12:26:31 -0700234void GrCoverageCountingPathRenderer::postFlush(GrDeferredUploadToken, const uint32_t* opListIDs,
235 int numOpListIDs) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600236 SkASSERT(fFlushing);
Chris Daltona32a3c32017-12-05 10:05:21 -0700237 // We wait to erase these until after flush, once Ops and FPs are done accessing their data.
Chris Daltond7e22272018-05-23 10:17:17 -0600238 fFlushingPaths.reset();
Chris Dalton383a2ef2018-01-08 17:21:41 -0500239 SkDEBUGCODE(fFlushing = false);
Chris Dalton1a325d22017-07-14 15:17:41 -0600240}