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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
|
/*
* 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 "GrCCPRCoverageOpsBuilder.h"
#include "GrBuffer.h"
#include "GrGpuCommandBuffer.h"
#include "GrOnFlushResourceProvider.h"
#include "GrOpFlushState.h"
#include "SkGeometry.h"
#include "SkMakeUnique.h"
#include "SkMathPriv.h"
#include "SkPath.h"
#include "SkPathPriv.h"
#include "SkPoint.h"
#include "SkNx.h"
#include "ops/GrDrawOp.h"
#include "../pathops/SkPathOpsCubic.h"
#include <numeric>
class GrCCPRCoverageOpsBuilder::CoverageOp : public GrDrawOp {
public:
using PrimitiveTallies = GrCCPRCoverageOpsBuilder::PrimitiveTallies;
DEFINE_OP_CLASS_ID
CoverageOp(const SkISize& drawBounds, sk_sp<GrBuffer> pointsBuffer,
sk_sp<GrBuffer> trianglesBuffer,
const PrimitiveTallies baseInstances[kNumScissorModes],
const PrimitiveTallies endInstances[kNumScissorModes], SkTArray<ScissorBatch>&&);
// GrDrawOp interface.
const char* name() const override { return "GrCCPRCoverageOpsBuilder::CoverageOp"; }
FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*) override {
return RequiresDstTexture::kNo;
}
bool onCombineIfPossible(GrOp* other, const GrCaps& caps) override { return false; }
void onPrepare(GrOpFlushState*) override {}
void onExecute(GrOpFlushState*) override;
private:
void drawMaskPrimitives(GrOpFlushState*, const GrPipeline&, const GrCCPRCoverageProcessor::Mode,
GrPrimitiveType, int vertexCount,
int PrimitiveTallies::* instanceType) const;
const SkISize fDrawBounds;
const sk_sp<GrBuffer> fPointsBuffer;
const sk_sp<GrBuffer> fTrianglesBuffer;
const PrimitiveTallies fBaseInstances[GrCCPRCoverageOpsBuilder::kNumScissorModes];
const PrimitiveTallies fInstanceCounts[GrCCPRCoverageOpsBuilder::kNumScissorModes];
const SkTArray<ScissorBatch> fScissorBatches;
mutable SkTArray<GrMesh> fMeshesScratchBuffer;
mutable SkTArray<GrPipeline::DynamicState> fDynamicStatesScratchBuffer;
typedef GrDrawOp INHERITED;
};
/**
* This is a view matrix that accumulates two bounding boxes as it maps points: device-space bounds
* and "45 degree" device-space bounds (| 1 -1 | * devCoords).
* | 1 1 |
*/
class GrCCPRCoverageOpsBuilder::AccumulatingViewMatrix {
public:
AccumulatingViewMatrix(const SkMatrix& m, const SkPoint& initialPoint);
SkPoint transform(const SkPoint& pt);
void getAccumulatedBounds(SkRect* devBounds, SkRect* devBounds45) const;
private:
Sk4f fX;
Sk4f fY;
Sk4f fT;
Sk4f fTopLeft;
Sk4f fBottomRight;
};
static int num_pts(uint8_t verb) {
switch (verb) {
case SkPath::kClose_Verb:
case SkPath::kDone_Verb:
default:
SkFAIL("Path verb does not have an endpoint.");
return 0;
case SkPath::kMove_Verb:
case SkPath::kLine_Verb:
return 1;
case SkPath::kQuad_Verb:
return 2;
case SkPath::kConic_Verb:
return 2;
case SkPath::kCubic_Verb:
return 3;
}
}
static SkPoint to_skpoint(double x, double y) {
return {static_cast<SkScalar>(x), static_cast<SkScalar>(y)};
}
static SkPoint to_skpoint(const SkDPoint& dpoint) {
return to_skpoint(dpoint.fX, dpoint.fY);
}
bool GrCCPRCoverageOpsBuilder::init(GrOnFlushResourceProvider* onFlushRP,
const MaxBufferItems& maxBufferItems) {
const int maxPoints = maxBufferItems.fMaxFanPoints + maxBufferItems.fMaxControlPoints;
fPointsBuffer = onFlushRP->makeBuffer(kTexel_GrBufferType, maxPoints * 2 * sizeof(float));
if (!fPointsBuffer) {
return false;
}
const MaxPrimitives* const maxPrimitives = maxBufferItems.fMaxPrimitives;
const int maxInstances = (maxPrimitives[0].sum() + maxPrimitives[1].sum());
fInstanceBuffer = onFlushRP->makeBuffer(kVertex_GrBufferType, maxInstances * 4 * sizeof(int));
if (!fInstanceBuffer) {
fPointsBuffer.reset();
return false;
}
fFanPtsIdx = 0;
fControlPtsIdx = maxBufferItems.fMaxFanPoints;
SkDEBUGCODE(fMaxFanPoints = maxBufferItems.fMaxFanPoints);
SkDEBUGCODE(fMaxControlPoints = maxBufferItems.fMaxControlPoints);
int baseInstance = 0;
for (int i = 0; i < kNumScissorModes; ++i) {
fBaseInstances[i].fTriangles = baseInstance;
baseInstance += maxPrimitives[i].fMaxTriangles;
fBaseInstances[i].fQuadratics = baseInstance;
baseInstance += maxPrimitives[i].fMaxQuadratics;
fBaseInstances[i].fSerpentines = baseInstance;
baseInstance += maxPrimitives[i].fMaxCubics;
// Loops grow backwards.
fBaseInstances[i].fLoops = baseInstance;
fInstanceIndices[i] = fBaseInstances[i];
}
fPointsData = static_cast<SkPoint*>(fPointsBuffer->map());
SkASSERT(fPointsData);
GR_STATIC_ASSERT(SK_SCALAR_IS_FLOAT);
GR_STATIC_ASSERT(8 == sizeof(SkPoint));
fInstanceData = static_cast<PrimitiveInstance*>(fInstanceBuffer->map());
SkASSERT(fInstanceData);
return true;
}
void GrCCPRCoverageOpsBuilder::parsePath(ScissorMode scissorMode, const SkMatrix& viewMatrix,
const SkPath& path, SkRect* devBounds,
SkRect* devBounds45) {
// Make sure they haven't called finalize yet (or not called init).
SkASSERT(fPointsData);
SkASSERT(fInstanceData);
fCurrScissorMode = scissorMode;
fCurrPathIndices = fInstanceIndices[(int)fCurrScissorMode];
fCurrContourStartIdx = fFanPtsIdx;
const SkPoint* const pts = SkPathPriv::PointData(path);
int ptsIdx = 0;
SkASSERT(!path.isEmpty());
SkASSERT(path.countPoints() > 0);
AccumulatingViewMatrix m(viewMatrix, pts[0]);
for (SkPath::Verb verb : SkPathPriv::Verbs(path)) {
switch (verb) {
case SkPath::kMove_Verb:
this->startContour(m, pts[ptsIdx++]);
continue;
case SkPath::kClose_Verb:
this->closeContour();
continue;
case SkPath::kLine_Verb:
this->fanTo(m, pts[ptsIdx]);
break;
case SkPath::kQuad_Verb:
SkASSERT(ptsIdx >= 1); // SkPath should have inserted an implicit moveTo if needed.
this->quadraticTo(m, &pts[ptsIdx - 1]);
break;
case SkPath::kCubic_Verb:
SkASSERT(ptsIdx >= 1); // SkPath should have inserted an implicit moveTo if needed.
this->cubicTo(m, &pts[ptsIdx - 1]);
break;
case SkPath::kConic_Verb:
SkFAIL("Conics are not supported.");
default:
SkFAIL("Unexpected path verb.");
}
ptsIdx += num_pts(verb);
}
this->closeContour();
m.getAccumulatedBounds(devBounds, devBounds45);
SkDEBUGCODE(this->validate();)
}
void GrCCPRCoverageOpsBuilder::saveParsedPath(const SkIRect& clippedDevIBounds,
int16_t atlasOffsetX, int16_t atlasOffsetY) {
const PrimitiveTallies& baseIndices = fInstanceIndices[(int)fCurrScissorMode];
const int32_t packedAtlasOffset = (atlasOffsetY << 16) | (atlasOffsetX & 0xffff);
for (int i = baseIndices.fTriangles; i < fCurrPathIndices.fTriangles; ++i) {
fInstanceData[i].fPackedAtlasOffset = packedAtlasOffset;
}
for (int i = baseIndices.fQuadratics; i < fCurrPathIndices.fQuadratics; ++i) {
fInstanceData[i].fPackedAtlasOffset = packedAtlasOffset;
}
for (int i = baseIndices.fSerpentines; i < fCurrPathIndices.fSerpentines; ++i) {
fInstanceData[i].fPackedAtlasOffset = packedAtlasOffset;
}
for (int i = baseIndices.fLoops - 1; i >= fCurrPathIndices.fLoops; --i) {
fInstanceData[i].fPackedAtlasOffset = packedAtlasOffset;
}
if (ScissorMode::kScissored == fCurrScissorMode) {
fScissorBatches.push_back() = {
fCurrPathIndices - fInstanceIndices[(int)fCurrScissorMode],
clippedDevIBounds.makeOffset(atlasOffsetX, atlasOffsetY)
};
}
fInstanceIndices[(int)fCurrScissorMode] = fCurrPathIndices;
}
void GrCCPRCoverageOpsBuilder::startContour(AccumulatingViewMatrix& m, const SkPoint& anchorPoint) {
this->closeContour();
fCurrPathSpaceAnchorPoint = anchorPoint;
fPointsData[fFanPtsIdx++] = m.transform(anchorPoint);
SkASSERT(fCurrContourStartIdx == fFanPtsIdx - 1);
}
void GrCCPRCoverageOpsBuilder::fanTo(AccumulatingViewMatrix& m, const SkPoint& pt) {
SkASSERT(fCurrContourStartIdx < fFanPtsIdx);
if (pt == fCurrPathSpaceAnchorPoint) {
this->startContour(m, pt);
return;
}
fPointsData[fFanPtsIdx++] = m.transform(pt);
}
void GrCCPRCoverageOpsBuilder::quadraticTo(AccumulatingViewMatrix& m, const SkPoint P[3]) {
SkASSERT(fCurrPathIndices.fQuadratics < fBaseInstances[(int)fCurrScissorMode].fSerpentines);
this->fanTo(m, P[2]);
fPointsData[fControlPtsIdx++] = m.transform(P[1]);
fInstanceData[fCurrPathIndices.fQuadratics++].fQuadraticData = {
fControlPtsIdx - 1,
fFanPtsIdx - 2
};
}
void GrCCPRCoverageOpsBuilder::cubicTo(AccumulatingViewMatrix& m, const SkPoint P[4]) {
double t[2], s[2];
SkCubicType type = SkClassifyCubic(P, t, s);
if (SkCubicType::kLineOrPoint == type) {
this->fanTo(m, P[3]);
return;
}
if (SkCubicType::kQuadratic == type) {
SkScalar x1 = P[1].y() - P[0].y(), y1 = P[0].x() - P[1].x(),
k1 = x1 * P[0].x() + y1 * P[0].y();
SkScalar x2 = P[2].y() - P[3].y(), y2 = P[3].x() - P[2].x(),
k2 = x2 * P[3].x() + y2 * P[3].y();
SkScalar rdet = 1 / (x1*y2 - y1*x2);
SkPoint Q[3] = {P[0], {(y2*k1 - y1*k2) * rdet, (x1*k2 - x2*k1) * rdet}, P[3]};
this->quadraticTo(m, Q);
return;
}
SkDCubic C;
C.set(P);
for (int x = 0; x <= 1; ++x) {
if (t[x] * s[x] <= 0) { // This is equivalent to tx/sx <= 0.
// This technically also gets taken if tx/sx = infinity, but the code still does
// the right thing in that edge case.
continue; // Don't increment x0.
}
if (fabs(t[x]) >= fabs(s[x])) { // tx/sx >= 1.
break;
}
const double chopT = double(t[x]) / double(s[x]);
SkASSERT(chopT >= 0 && chopT <= 1);
if (chopT <= 0 || chopT >= 1) { // floating-point error.
continue;
}
SkDCubicPair chopped = C.chopAt(chopT);
// Ensure the double points are identical if this is a loop (more workarounds for FP error).
if (SkCubicType::kLoop == type && 0 == t[0]) {
chopped.pts[3] = chopped.pts[0];
}
// (This might put ts0/ts1 out of order, but it doesn't matter anymore at this point.)
this->emitCubicSegment(m, type, chopped.first(),
to_skpoint(t[1 - x], s[1 - x] * chopT), to_skpoint(1, 1));
t[x] = 0;
s[x] = 1;
const double r = s[1 - x] * chopT;
t[1 - x] -= r;
s[1 - x] -= r;
C = chopped.second();
}
this->emitCubicSegment(m, type, C, to_skpoint(t[0], s[0]), to_skpoint(t[1], s[1]));
}
void GrCCPRCoverageOpsBuilder::emitCubicSegment(AccumulatingViewMatrix& m,
SkCubicType type, const SkDCubic& C,
const SkPoint& ts0, const SkPoint& ts1) {
SkASSERT(fCurrPathIndices.fSerpentines < fCurrPathIndices.fLoops);
fPointsData[fControlPtsIdx++] = m.transform(to_skpoint(C[1]));
fPointsData[fControlPtsIdx++] = m.transform(to_skpoint(C[2]));
this->fanTo(m, to_skpoint(C[3]));
// Also emit the cubic's root t,s values as "control points".
fPointsData[fControlPtsIdx++] = ts0;
fPointsData[fControlPtsIdx++] = ts1;
// Serpentines grow up from the front, and loops grow down from the back.
fInstanceData[SkCubicType::kLoop != type ?
fCurrPathIndices.fSerpentines++ : --fCurrPathIndices.fLoops].fCubicData = {
fControlPtsIdx - 4,
fFanPtsIdx - 2
};
}
void GrCCPRCoverageOpsBuilder::closeContour() {
int fanSize = fFanPtsIdx - fCurrContourStartIdx;
if (fanSize >= 3) {
// Technically this can grow to fanSize + log3(fanSize), but we approximate with log2.
SkAutoSTMalloc<300, int32_t> indices(fanSize + SkNextLog2(fanSize));
std::iota(indices.get(), indices.get() + fanSize, fCurrContourStartIdx);
this->emitHierarchicalFan(indices, fanSize);
}
// Reset the current contour.
fCurrContourStartIdx = fFanPtsIdx;
}
void GrCCPRCoverageOpsBuilder::emitHierarchicalFan(int32_t indices[], int count) {
if (count < 3) {
return;
}
const int32_t oneThirdPt = count / 3;
const int32_t twoThirdsPt = (2 * count) / 3;
SkASSERT(fCurrPathIndices.fTriangles < fBaseInstances[(int)fCurrScissorMode].fQuadratics);
fInstanceData[fCurrPathIndices.fTriangles++].fTriangleData = {
indices[0],
indices[oneThirdPt],
indices[twoThirdsPt]
};
this->emitHierarchicalFan(indices, oneThirdPt + 1);
this->emitHierarchicalFan(&indices[oneThirdPt], twoThirdsPt - oneThirdPt + 1);
int32_t oldIndex = indices[count];
indices[count] = indices[0];
this->emitHierarchicalFan(&indices[twoThirdsPt], count - twoThirdsPt + 1);
indices[count] = oldIndex;
}
std::unique_ptr<GrDrawOp> GrCCPRCoverageOpsBuilder::createIntermediateOp(SkISize drawBounds) {
auto op = skstd::make_unique<CoverageOp>(drawBounds, fPointsBuffer, fInstanceBuffer,
fBaseInstances, fInstanceIndices,
std::move(fScissorBatches));
SkASSERT(fScissorBatches.empty());
fBaseInstances[0] = fInstanceIndices[0];
fBaseInstances[1] = fInstanceIndices[1];
return std::move(op);
}
std::unique_ptr<GrDrawOp> GrCCPRCoverageOpsBuilder::finalize(SkISize drawBounds) {
fPointsBuffer->unmap();
SkDEBUGCODE(fPointsData = nullptr);
fInstanceBuffer->unmap();
SkDEBUGCODE(fInstanceData = nullptr);
return skstd::make_unique<CoverageOp>(drawBounds, std::move(fPointsBuffer),
std::move(fInstanceBuffer), fBaseInstances,
fInstanceIndices, std::move(fScissorBatches));
}
#ifdef SK_DEBUG
void GrCCPRCoverageOpsBuilder::validate() {
SkASSERT(fFanPtsIdx <= fMaxFanPoints);
SkASSERT(fControlPtsIdx <= fMaxFanPoints + fMaxControlPoints);
for (int i = 0; i < kNumScissorModes; ++i) {
SkASSERT(fInstanceIndices[i].fTriangles <= fBaseInstances[i].fQuadratics);
SkASSERT(fInstanceIndices[i].fQuadratics <= fBaseInstances[i].fSerpentines);
SkASSERT(fInstanceIndices[i].fSerpentines <= fInstanceIndices[i].fLoops);
}
}
#endif
using MaxBufferItems = GrCCPRCoverageOpsBuilder::MaxBufferItems;
void MaxBufferItems::countPathItems(GrCCPRCoverageOpsBuilder::ScissorMode scissorMode,
const SkPath& path) {
MaxPrimitives& maxPrimitives = fMaxPrimitives[(int)scissorMode];
int currFanPts = 0;
for (SkPath::Verb verb : SkPathPriv::Verbs(path)) {
switch (verb) {
case SkPath::kMove_Verb:
case SkPath::kClose_Verb:
fMaxFanPoints += currFanPts;
maxPrimitives.fMaxTriangles += SkTMax(0, currFanPts - 2);
currFanPts = SkPath::kMove_Verb == verb ? 1 : 0;
continue;
case SkPath::kLine_Verb:
SkASSERT(currFanPts > 0);
++currFanPts;
continue;
case SkPath::kQuad_Verb:
SkASSERT(currFanPts > 0);
++currFanPts;
++fMaxControlPoints;
++maxPrimitives.fMaxQuadratics;
continue;
case SkPath::kCubic_Verb: {
SkASSERT(currFanPts > 0);
// Over-allocate for the worst case when the cubic is chopped into 3 segments.
static constexpr int kMaxSegments = 3;
currFanPts += kMaxSegments;
// Each cubic segment has two control points.
fMaxControlPoints += kMaxSegments * 2;
// Each cubic segment also emits two root t,s values as "control points".
fMaxControlPoints += kMaxSegments * 2;
maxPrimitives.fMaxCubics += kMaxSegments;
// The cubic may also turn out to be a quadratic. While we over-allocate by a fair
// amount, this is still a relatively small amount of space.
++maxPrimitives.fMaxQuadratics;
continue;
}
case SkPath::kConic_Verb:
SkASSERT(currFanPts > 0);
SkFAIL("Conics are not supported.");
default:
SkFAIL("Unexpected path verb.");
}
}
fMaxFanPoints += currFanPts;
maxPrimitives.fMaxTriangles += SkTMax(0, currFanPts - 2);
++fMaxPaths;
}
using CoverageOp = GrCCPRCoverageOpsBuilder::CoverageOp;
GrCCPRCoverageOpsBuilder::CoverageOp::CoverageOp(const SkISize& drawBounds,
sk_sp<GrBuffer> pointsBuffer,
sk_sp<GrBuffer> trianglesBuffer,
const PrimitiveTallies baseInstances[kNumScissorModes],
const PrimitiveTallies endInstances[kNumScissorModes],
SkTArray<ScissorBatch>&& scissorBatches)
: INHERITED(ClassID())
, fDrawBounds(drawBounds)
, fPointsBuffer(std::move(pointsBuffer))
, fTrianglesBuffer(std::move(trianglesBuffer))
, fBaseInstances{baseInstances[0], baseInstances[1]}
, fInstanceCounts{endInstances[0] - baseInstances[0], endInstances[1] - baseInstances[1]}
, fScissorBatches(std::move(scissorBatches)) {
SkASSERT(fPointsBuffer);
SkASSERT(fTrianglesBuffer);
this->setBounds(SkRect::MakeIWH(fDrawBounds.width(), fDrawBounds.height()),
GrOp::HasAABloat::kNo, GrOp::IsZeroArea::kNo);
}
void CoverageOp::onExecute(GrOpFlushState* flushState) {
using Mode = GrCCPRCoverageProcessor::Mode;
SkDEBUGCODE(GrCCPRCoverageProcessor::Validate(flushState->drawOpArgs().fProxy));
GrPipeline pipeline(flushState->drawOpArgs().fProxy, GrPipeline::ScissorState::kEnabled,
SkBlendMode::kPlus);
fMeshesScratchBuffer.reserve(1 + fScissorBatches.count());
fDynamicStatesScratchBuffer.reserve(1 + fScissorBatches.count());
// Triangles.
auto constexpr kTrianglesGrPrimitiveType = GrCCPRCoverageProcessor::kTrianglesGrPrimitiveType;
this->drawMaskPrimitives(flushState, pipeline, Mode::kCombinedTriangleHullsAndEdges,
kTrianglesGrPrimitiveType, 3, &PrimitiveTallies::fTriangles);
this->drawMaskPrimitives(flushState, pipeline, Mode::kTriangleCorners,
kTrianglesGrPrimitiveType, 3, &PrimitiveTallies::fTriangles);
// Quadratics.
auto constexpr kQuadraticsGrPrimitiveType = GrCCPRCoverageProcessor::kQuadraticsGrPrimitiveType;
this->drawMaskPrimitives(flushState, pipeline, Mode::kQuadraticHulls,
kQuadraticsGrPrimitiveType, 3, &PrimitiveTallies::fQuadratics);
this->drawMaskPrimitives(flushState, pipeline, Mode::kQuadraticFlatEdges,
kQuadraticsGrPrimitiveType, 3, &PrimitiveTallies::fQuadratics);
// Cubics.
auto constexpr kCubicsGrPrimitiveType = GrCCPRCoverageProcessor::kCubicsGrPrimitiveType;
this->drawMaskPrimitives(flushState, pipeline, Mode::kSerpentineInsets,
kCubicsGrPrimitiveType, 4, &PrimitiveTallies::fSerpentines);
this->drawMaskPrimitives(flushState, pipeline, Mode::kLoopInsets,
kCubicsGrPrimitiveType, 4, &PrimitiveTallies::fLoops);
this->drawMaskPrimitives(flushState, pipeline, Mode::kSerpentineBorders,
kCubicsGrPrimitiveType, 4, &PrimitiveTallies::fSerpentines);
this->drawMaskPrimitives(flushState, pipeline, Mode::kLoopBorders,
kCubicsGrPrimitiveType, 4, &PrimitiveTallies::fLoops);
}
void CoverageOp::drawMaskPrimitives(GrOpFlushState* flushState, const GrPipeline& pipeline,
GrCCPRCoverageProcessor::Mode mode, GrPrimitiveType primType,
int vertexCount, int PrimitiveTallies::* instanceType) const {
SkASSERT(pipeline.getScissorState().enabled());
fMeshesScratchBuffer.reset();
fDynamicStatesScratchBuffer.reset();
if (const int instanceCount = fInstanceCounts[(int)ScissorMode::kNonScissored].*instanceType) {
const int baseInstance = fBaseInstances[(int)ScissorMode::kNonScissored].*instanceType;
// Loops grow backwards, which is indicated by a negative instance count.
GrMesh& mesh = fMeshesScratchBuffer.emplace_back(primType);
mesh.setInstanced(fTrianglesBuffer.get(), abs(instanceCount),
baseInstance + SkTMin(instanceCount, 0), vertexCount);
fDynamicStatesScratchBuffer.push_back().fScissorRect.setXYWH(0, 0, fDrawBounds.width(),
fDrawBounds.height());
}
if (fInstanceCounts[(int)ScissorMode::kScissored].*instanceType) {
int baseInstance = fBaseInstances[(int)ScissorMode::kScissored].*instanceType;
for (const ScissorBatch& batch : fScissorBatches) {
SkASSERT(this->bounds().contains(batch.fScissor));
const int instanceCount = batch.fInstanceCounts.*instanceType;
if (!instanceCount) {
continue;
}
// Loops grow backwards, which is indicated by a negative instance count.
GrMesh& mesh = fMeshesScratchBuffer.emplace_back(primType);
mesh.setInstanced(fTrianglesBuffer.get(), abs(instanceCount),
baseInstance + SkTMin(instanceCount,0), vertexCount);
fDynamicStatesScratchBuffer.push_back().fScissorRect = batch.fScissor;
baseInstance += instanceCount;
}
}
SkASSERT(fMeshesScratchBuffer.count() == fDynamicStatesScratchBuffer.count());
if (!fMeshesScratchBuffer.empty()) {
GrCCPRCoverageProcessor proc(mode, fPointsBuffer.get());
flushState->commandBuffer()->draw(pipeline, proc, fMeshesScratchBuffer.begin(),
fDynamicStatesScratchBuffer.begin(),
fMeshesScratchBuffer.count(), this->bounds());
}
}
using PrimitiveTallies = CoverageOp::PrimitiveTallies;
inline PrimitiveTallies PrimitiveTallies::operator+(const PrimitiveTallies& b) const {
return {fTriangles + b.fTriangles,
fQuadratics + b.fQuadratics,
fSerpentines + b.fSerpentines,
fLoops + b.fLoops};
}
inline PrimitiveTallies PrimitiveTallies::operator-(const PrimitiveTallies& b) const {
return {fTriangles - b.fTriangles,
fQuadratics - b.fQuadratics,
fSerpentines - b.fSerpentines,
fLoops - b.fLoops};
}
inline int PrimitiveTallies::sum() const {
return fTriangles + fQuadratics + fSerpentines + fLoops;
}
using AccumulatingViewMatrix = GrCCPRCoverageOpsBuilder::AccumulatingViewMatrix;
inline AccumulatingViewMatrix::AccumulatingViewMatrix(const SkMatrix& m,
const SkPoint& initialPoint) {
// m45 transforms into 45 degree space in order to find the octagon's diagonals. We could
// use SK_ScalarRoot2Over2 if we wanted an orthonormal transform, but this is irrelevant as
// long as the shader uses the correct inverse when coming back to device space.
SkMatrix m45;
m45.setSinCos(1, 1);
m45.preConcat(m);
fX = Sk4f(m.getScaleX(), m.getSkewY(), m45.getScaleX(), m45.getSkewY());
fY = Sk4f(m.getSkewX(), m.getScaleY(), m45.getSkewX(), m45.getScaleY());
fT = Sk4f(m.getTranslateX(), m.getTranslateY(), m45.getTranslateX(), m45.getTranslateY());
Sk4f transformed = SkNx_fma(fY, Sk4f(initialPoint.y()), fT);
transformed = SkNx_fma(fX, Sk4f(initialPoint.x()), transformed);
fTopLeft = fBottomRight = transformed;
}
inline SkPoint AccumulatingViewMatrix::transform(const SkPoint& pt) {
Sk4f transformed = SkNx_fma(fY, Sk4f(pt.y()), fT);
transformed = SkNx_fma(fX, Sk4f(pt.x()), transformed);
fTopLeft = Sk4f::Min(fTopLeft, transformed);
fBottomRight = Sk4f::Max(fBottomRight, transformed);
// TODO: vst1_lane_f32? (Sk4f::storeLane?)
float data[4];
transformed.store(data);
return SkPoint::Make(data[0], data[1]);
}
inline void AccumulatingViewMatrix::getAccumulatedBounds(SkRect* devBounds,
SkRect* devBounds45) const {
float topLeft[4], bottomRight[4];
fTopLeft.store(topLeft);
fBottomRight.store(bottomRight);
devBounds->setLTRB(topLeft[0], topLeft[1], bottomRight[0], bottomRight[1]);
devBounds45->setLTRB(topLeft[2], topLeft[3], bottomRight[2], bottomRight[3]);
}
|