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