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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
|
/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "GrMSAAPathRenderer.h"
#include "GrAuditTrail.h"
#include "GrBatchFlushState.h"
#include "GrClip.h"
#include "GrDefaultGeoProcFactory.h"
#include "GrPathStencilSettings.h"
#include "GrPathUtils.h"
#include "GrPipelineBuilder.h"
#include "GrMesh.h"
#include "SkGeometry.h"
#include "SkTraceEvent.h"
#include "glsl/GrGLSLGeometryProcessor.h"
#include "glsl/GrGLSLFragmentShaderBuilder.h"
#include "glsl/GrGLSLVertexShaderBuilder.h"
#include "glsl/GrGLSLProgramDataManager.h"
#include "glsl/GrGLSLUtil.h"
#include "gl/GrGLVaryingHandler.h"
#include "batches/GrRectBatchFactory.h"
#include "batches/GrVertexBatch.h"
static const float kTolerance = 0.5f;
////////////////////////////////////////////////////////////////////////////////
// Helpers for drawPath
static inline bool single_pass_shape(const GrShape& shape) {
if (!shape.inverseFilled()) {
return shape.knownToBeConvex();
}
return false;
}
GrPathRenderer::StencilSupport GrMSAAPathRenderer::onGetStencilSupport(const GrShape& shape) const {
if (single_pass_shape(shape)) {
return GrPathRenderer::kNoRestriction_StencilSupport;
} else {
return GrPathRenderer::kStencilOnly_StencilSupport;
}
}
struct MSAALineVertices {
struct Vertex {
SkPoint fPosition;
SkColor fColor;
};
Vertex* vertices;
Vertex* nextVertex;
#ifdef SK_DEBUG
Vertex* verticesEnd;
#endif
uint16_t* indices;
uint16_t* nextIndex;
};
struct MSAAQuadVertices {
struct Vertex {
SkPoint fPosition;
SkPoint fUV;
SkColor fColor;
};
Vertex* vertices;
Vertex* nextVertex;
#ifdef SK_DEBUG
Vertex* verticesEnd;
#endif
uint16_t* indices;
uint16_t* nextIndex;
};
static inline void append_contour_edge_indices(uint16_t fanCenterIdx,
uint16_t edgeV0Idx,
MSAALineVertices& lines) {
*(lines.nextIndex++) = fanCenterIdx;
*(lines.nextIndex++) = edgeV0Idx;
*(lines.nextIndex++) = edgeV0Idx + 1;
}
static inline void add_quad(MSAALineVertices& lines, MSAAQuadVertices& quads, const SkPoint pts[],
SkColor color, bool indexed, uint16_t subpathLineIdxStart) {
SkASSERT(lines.nextVertex < lines.verticesEnd);
*lines.nextVertex = { pts[2], color };
if (indexed) {
int prevIdx = (uint16_t) (lines.nextVertex - lines.vertices - 1);
if (prevIdx > subpathLineIdxStart) {
append_contour_edge_indices(subpathLineIdxStart, prevIdx, lines);
}
}
lines.nextVertex++;
SkASSERT(quads.nextVertex + 2 < quads.verticesEnd);
// the texture coordinates are drawn from the Loop-Blinn rendering algorithm
*(quads.nextVertex++) = { pts[0], SkPoint::Make(0.0, 0.0), color };
*(quads.nextVertex++) = { pts[1], SkPoint::Make(0.5, 0.0), color };
*(quads.nextVertex++) = { pts[2], SkPoint::Make(1.0, 1.0), color };
if (indexed) {
uint16_t offset = (uint16_t) (quads.nextVertex - quads.vertices) - 3;
*(quads.nextIndex++) = offset++;
*(quads.nextIndex++) = offset++;
*(quads.nextIndex++) = offset++;
}
}
class MSAAQuadProcessor : public GrGeometryProcessor {
public:
static GrGeometryProcessor* Create(const SkMatrix& viewMatrix) {
return new MSAAQuadProcessor(viewMatrix);
}
virtual ~MSAAQuadProcessor() {}
const char* name() const override { return "MSAAQuadProcessor"; }
const Attribute* inPosition() const { return fInPosition; }
const Attribute* inUV() const { return fInUV; }
const Attribute* inColor() const { return fInColor; }
const SkMatrix& viewMatrix() const { return fViewMatrix; }
const SkMatrix& localMatrix() const { return SkMatrix::I(); }
class GLSLProcessor : public GrGLSLGeometryProcessor {
public:
GLSLProcessor(const GrGeometryProcessor& qpr) {}
void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
const MSAAQuadProcessor& qp = args.fGP.cast<MSAAQuadProcessor>();
GrGLSLVertexBuilder* vsBuilder = args.fVertBuilder;
GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
// emit attributes
varyingHandler->emitAttributes(qp);
varyingHandler->addPassThroughAttribute(qp.inColor(), args.fOutputColor);
GrGLSLVertToFrag uv(kVec2f_GrSLType);
varyingHandler->addVarying("uv", &uv, kHigh_GrSLPrecision);
vsBuilder->codeAppendf("%s = %s;", uv.vsOut(), qp.inUV()->fName);
// Setup position
this->setupPosition(vsBuilder, uniformHandler, gpArgs, qp.inPosition()->fName,
qp.viewMatrix(), &fViewMatrixUniform);
// emit transforms
this->emitTransforms(vsBuilder, varyingHandler, uniformHandler, gpArgs->fPositionVar,
qp.inPosition()->fName, SkMatrix::I(), args.fTransformsIn,
args.fTransformsOut);
GrGLSLPPFragmentBuilder* fsBuilder = args.fFragBuilder;
fsBuilder->codeAppendf("if (%s.x * %s.x >= %s.y) discard;", uv.fsIn(), uv.fsIn(),
uv.fsIn());
fsBuilder->codeAppendf("%s = vec4(1.0);", args.fOutputCoverage);
}
static inline void GenKey(const GrGeometryProcessor& gp,
const GrGLSLCaps&,
GrProcessorKeyBuilder* b) {
const MSAAQuadProcessor& qp = gp.cast<MSAAQuadProcessor>();
uint32_t key = 0;
key |= qp.viewMatrix().hasPerspective() ? 0x1 : 0x0;
key |= qp.viewMatrix().isIdentity() ? 0x2: 0x0;
b->add32(key);
}
virtual void setData(const GrGLSLProgramDataManager& pdman,
const GrPrimitiveProcessor& gp) override {
const MSAAQuadProcessor& qp = gp.cast<MSAAQuadProcessor>();
if (!qp.viewMatrix().isIdentity()) {
float viewMatrix[3 * 3];
GrGLSLGetMatrix<3>(viewMatrix, qp.viewMatrix());
pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
}
}
void setTransformData(const GrPrimitiveProcessor& primProc,
const GrGLSLProgramDataManager& pdman,
int index,
const SkTArray<const GrCoordTransform*, true>& transforms) override {
this->setTransformDataHelper<MSAAQuadProcessor>(primProc, pdman, index, transforms);
}
private:
typedef GrGLSLGeometryProcessor INHERITED;
UniformHandle fViewMatrixUniform;
};
virtual void getGLSLProcessorKey(const GrGLSLCaps& caps,
GrProcessorKeyBuilder* b) const override {
GLSLProcessor::GenKey(*this, caps, b);
}
virtual GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) const override {
return new GLSLProcessor(*this);
}
private:
MSAAQuadProcessor(const SkMatrix& viewMatrix)
: fViewMatrix(viewMatrix) {
this->initClassID<MSAAQuadProcessor>();
fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType,
kHigh_GrSLPrecision));
fInUV = &this->addVertexAttrib(Attribute("inUV", kVec2f_GrVertexAttribType,
kHigh_GrSLPrecision));
fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType));
this->setSampleShading(1.0f);
}
const Attribute* fInPosition;
const Attribute* fInUV;
const Attribute* fInColor;
SkMatrix fViewMatrix;
GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
typedef GrGeometryProcessor INHERITED;
};
class MSAAPathBatch : public GrVertexBatch {
public:
DEFINE_BATCH_CLASS_ID
MSAAPathBatch(GrColor color, const SkPath& path, const SkMatrix& viewMatrix,
const SkRect& devBounds)
: INHERITED(ClassID())
, fViewMatrix(viewMatrix) {
fPaths.emplace_back(PathInfo{color, path});
this->setBounds(devBounds, HasAABloat::kNo, IsZeroArea::kNo);
int contourCount;
this->computeWorstCasePointCount(path, &contourCount, &fMaxLineVertices, &fMaxQuadVertices);
fMaxLineIndices = fMaxLineVertices * 3;
fMaxQuadIndices = fMaxQuadVertices * 3;
fIsIndexed = contourCount > 1;
}
const char* name() const override { return "MSAAPathBatch"; }
void computePipelineOptimizations(GrInitInvariantOutput* color,
GrInitInvariantOutput* coverage,
GrBatchToXPOverrides* overrides) const override {
// When this is called on a batch, there is only one path
color->setKnownFourComponents(fPaths[0].fColor);
coverage->setKnownSingleComponent(0xff);
}
bool isValid() const {
return !fIsIndexed || fMaxLineIndices <= SK_MaxU16;
}
private:
void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
// Handle any color overrides
if (!overrides.readsColor()) {
fPaths[0].fColor = GrColor_ILLEGAL;
}
overrides.getOverrideColorIfSet(&fPaths[0].fColor);
}
void computeWorstCasePointCount(const SkPath& path, int* subpaths, int* outLinePointCount,
int* outQuadPointCount) const {
int linePointCount = 0;
int quadPointCount = 0;
*subpaths = 1;
bool first = true;
SkPath::Iter iter(path, false);
SkPath::Verb verb;
SkPoint pts[4];
while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
switch (verb) {
case SkPath::kLine_Verb:
linePointCount += 1;
break;
case SkPath::kConic_Verb: {
SkScalar weight = iter.conicWeight();
SkAutoConicToQuads converter;
converter.computeQuads(pts, weight, kTolerance);
int quadPts = converter.countQuads();
linePointCount += quadPts;
quadPointCount += 3 * quadPts;
}
case SkPath::kQuad_Verb:
linePointCount += 1;
quadPointCount += 3;
break;
case SkPath::kCubic_Verb: {
SkSTArray<15, SkPoint, true> quadPts;
GrPathUtils::convertCubicToQuads(pts, kTolerance, &quadPts);
int count = quadPts.count();
linePointCount += count / 3;
quadPointCount += count;
break;
}
case SkPath::kMove_Verb:
linePointCount += 1;
if (!first) {
++(*subpaths);
}
break;
default:
break;
}
first = false;
}
*outLinePointCount = linePointCount;
*outQuadPointCount = quadPointCount;
}
void onPrepareDraws(Target* target) const override {
SkASSERT(this->isValid());
if (fMaxLineVertices == 0) {
SkASSERT(fMaxQuadVertices == 0);
return;
}
GrPrimitiveType primitiveType = fIsIndexed ? kTriangles_GrPrimitiveType
: kTriangleFan_GrPrimitiveType;
// allocate vertex / index buffers
const GrBuffer* lineVertexBuffer;
int firstLineVertex;
MSAALineVertices lines;
size_t lineVertexStride = sizeof(MSAALineVertices::Vertex);
lines.vertices = (MSAALineVertices::Vertex*) target->makeVertexSpace(lineVertexStride,
fMaxLineVertices,
&lineVertexBuffer,
&firstLineVertex);
if (!lines.vertices) {
SkDebugf("Could not allocate vertices\n");
return;
}
lines.nextVertex = lines.vertices;
SkDEBUGCODE(lines.verticesEnd = lines.vertices + fMaxLineVertices;)
MSAAQuadVertices quads;
size_t quadVertexStride = sizeof(MSAAQuadVertices::Vertex);
SkAutoFree quadVertexPtr(sk_malloc_throw(fMaxQuadVertices * quadVertexStride));
quads.vertices = (MSAAQuadVertices::Vertex*) quadVertexPtr.get();
quads.nextVertex = quads.vertices;
SkDEBUGCODE(quads.verticesEnd = quads.vertices + fMaxQuadVertices;)
const GrBuffer* lineIndexBuffer = nullptr;
int firstLineIndex;
if (fIsIndexed) {
lines.indices = target->makeIndexSpace(fMaxLineIndices, &lineIndexBuffer,
&firstLineIndex);
if (!lines.indices) {
SkDebugf("Could not allocate indices\n");
return;
}
lines.nextIndex = lines.indices;
} else {
lines.indices = nullptr;
lines.nextIndex = nullptr;
}
SkAutoFree quadIndexPtr;
if (fIsIndexed) {
quads.indices = (uint16_t*) sk_malloc_throw(fMaxQuadIndices * sizeof(uint16_t));
quadIndexPtr.set(quads.indices);
quads.nextIndex = quads.indices;
} else {
quads.indices = nullptr;
quads.nextIndex = nullptr;
}
// fill buffers
for (int i = 0; i < fPaths.count(); i++) {
const PathInfo& pathInfo = fPaths[i];
if (!this->createGeom(lines,
quads,
pathInfo.fPath,
fViewMatrix,
pathInfo.fColor,
fIsIndexed)) {
return;
}
}
int lineVertexOffset = (int) (lines.nextVertex - lines.vertices);
int lineIndexOffset = (int) (lines.nextIndex - lines.indices);
SkASSERT(lineVertexOffset <= fMaxLineVertices && lineIndexOffset <= fMaxLineIndices);
int quadVertexOffset = (int) (quads.nextVertex - quads.vertices);
int quadIndexOffset = (int) (quads.nextIndex - quads.indices);
SkASSERT(quadVertexOffset <= fMaxQuadVertices && quadIndexOffset <= fMaxQuadIndices);
if (lineVertexOffset) {
sk_sp<GrGeometryProcessor> lineGP;
{
using namespace GrDefaultGeoProcFactory;
lineGP = GrDefaultGeoProcFactory::Make(Color(Color::kAttribute_Type),
Coverage(255),
LocalCoords(LocalCoords::kUnused_Type),
fViewMatrix);
}
SkASSERT(lineVertexStride == lineGP->getVertexStride());
GrMesh lineMeshes;
if (fIsIndexed) {
lineMeshes.initIndexed(primitiveType, lineVertexBuffer, lineIndexBuffer,
firstLineVertex, firstLineIndex, lineVertexOffset,
lineIndexOffset);
} else {
lineMeshes.init(primitiveType, lineVertexBuffer, firstLineVertex,
lineVertexOffset);
}
target->draw(lineGP.get(), lineMeshes);
}
if (quadVertexOffset) {
SkAutoTUnref<const GrGeometryProcessor> quadGP(MSAAQuadProcessor::Create(fViewMatrix));
SkASSERT(quadVertexStride == quadGP->getVertexStride());
const GrBuffer* quadVertexBuffer;
int firstQuadVertex;
MSAAQuadVertices::Vertex* quadVertices = (MSAAQuadVertices::Vertex*)
target->makeVertexSpace(quadVertexStride, quadVertexOffset, &quadVertexBuffer,
&firstQuadVertex);
memcpy(quadVertices, quads.vertices, quadVertexStride * quadVertexOffset);
GrMesh quadMeshes;
if (fIsIndexed) {
const GrBuffer* quadIndexBuffer;
int firstQuadIndex;
uint16_t* quadIndices = (uint16_t*) target->makeIndexSpace(quadIndexOffset,
&quadIndexBuffer,
&firstQuadIndex);
memcpy(quadIndices, quads.indices, sizeof(uint16_t) * quadIndexOffset);
quadMeshes.initIndexed(kTriangles_GrPrimitiveType, quadVertexBuffer,
quadIndexBuffer, firstQuadVertex, firstQuadIndex,
quadVertexOffset, quadIndexOffset);
} else {
quadMeshes.init(kTriangles_GrPrimitiveType, quadVertexBuffer, firstQuadVertex,
quadVertexOffset);
}
target->draw(quadGP, quadMeshes);
}
}
bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
MSAAPathBatch* that = t->cast<MSAAPathBatch>();
if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
that->bounds(), caps)) {
return false;
}
if (!fViewMatrix.cheapEqualTo(that->fViewMatrix)) {
return false;
}
if ((fMaxLineIndices + that->fMaxLineIndices > SK_MaxU16) ||
(fMaxQuadIndices + that->fMaxQuadIndices > SK_MaxU16)) {
return false;
}
fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
this->joinBounds(*that);
fIsIndexed = true;
fMaxLineVertices += that->fMaxLineVertices;
fMaxQuadVertices += that->fMaxQuadVertices;
fMaxLineIndices += that->fMaxLineIndices;
fMaxQuadIndices += that->fMaxQuadIndices;
return true;
}
bool createGeom(MSAALineVertices& lines,
MSAAQuadVertices& quads,
const SkPath& path,
const SkMatrix& m,
SkColor color,
bool isIndexed) const {
{
uint16_t subpathIdxStart = (uint16_t) (lines.nextVertex - lines.vertices);
SkPoint pts[4];
bool first = true;
SkPath::Iter iter(path, false);
bool done = false;
while (!done) {
SkPath::Verb verb = iter.next(pts);
switch (verb) {
case SkPath::kMove_Verb:
if (!first) {
uint16_t currIdx = (uint16_t) (lines.nextVertex - lines.vertices);
subpathIdxStart = currIdx;
}
SkASSERT(lines.nextVertex < lines.verticesEnd);
*(lines.nextVertex++) = { pts[0], color };
break;
case SkPath::kLine_Verb:
if (isIndexed) {
uint16_t prevIdx = (uint16_t) (lines.nextVertex - lines.vertices - 1);
if (prevIdx > subpathIdxStart) {
append_contour_edge_indices(subpathIdxStart, prevIdx, lines);
}
}
SkASSERT(lines.nextVertex < lines.verticesEnd);
*(lines.nextVertex++) = { pts[1], color };
break;
case SkPath::kConic_Verb: {
SkScalar weight = iter.conicWeight();
SkAutoConicToQuads converter;
const SkPoint* quadPts = converter.computeQuads(pts, weight, kTolerance);
for (int i = 0; i < converter.countQuads(); ++i) {
add_quad(lines, quads, quadPts + i * 2, color, isIndexed,
subpathIdxStart);
}
break;
}
case SkPath::kQuad_Verb: {
add_quad(lines, quads, pts, color, isIndexed, subpathIdxStart);
break;
}
case SkPath::kCubic_Verb: {
SkSTArray<15, SkPoint, true> quadPts;
GrPathUtils::convertCubicToQuads(pts, kTolerance, &quadPts);
int count = quadPts.count();
for (int i = 0; i < count; i += 3) {
add_quad(lines, quads, &quadPts[i], color, isIndexed, subpathIdxStart);
}
break;
}
case SkPath::kClose_Verb:
break;
case SkPath::kDone_Verb:
done = true;
}
first = false;
}
}
return true;
}
struct PathInfo {
GrColor fColor;
SkPath fPath;
};
SkSTArray<1, PathInfo, true> fPaths;
SkMatrix fViewMatrix;
int fMaxLineVertices;
int fMaxQuadVertices;
int fMaxLineIndices;
int fMaxQuadIndices;
bool fIsIndexed;
typedef GrVertexBatch INHERITED;
};
bool GrMSAAPathRenderer::internalDrawPath(GrDrawContext* drawContext,
const GrPaint& paint,
const GrUserStencilSettings* userStencilSettings,
const GrClip& clip,
const SkMatrix& viewMatrix,
const GrShape& shape,
bool stencilOnly) {
SkASSERT(shape.style().isSimpleFill());
SkPath path;
shape.asPath(&path);
static const int kMaxNumPasses = 2;
int passCount = 0;
const GrUserStencilSettings* passes[kMaxNumPasses];
bool reverse = false;
bool lastPassIsBounds;
if (single_pass_shape(shape)) {
passCount = 1;
if (stencilOnly) {
passes[0] = &gDirectToStencil;
} else {
passes[0] = userStencilSettings;
}
lastPassIsBounds = false;
} else {
switch (path.getFillType()) {
case SkPath::kInverseEvenOdd_FillType:
reverse = true;
// fallthrough
case SkPath::kEvenOdd_FillType:
passes[0] = &gEOStencilPass;
if (stencilOnly) {
passCount = 1;
lastPassIsBounds = false;
} else {
passCount = 2;
lastPassIsBounds = true;
if (reverse) {
passes[1] = &gInvEOColorPass;
} else {
passes[1] = &gEOColorPass;
}
}
break;
case SkPath::kInverseWinding_FillType:
reverse = true;
// fallthrough
case SkPath::kWinding_FillType:
passes[0] = &gWindStencilSeparateWithWrap;
passCount = 2;
if (stencilOnly) {
lastPassIsBounds = false;
passCount = 1;
} else {
lastPassIsBounds = true;
if (reverse) {
passes[1] = &gInvWindColorPass;
} else {
passes[1] = &gWindColorPass;
}
}
break;
default:
SkDEBUGFAIL("Unknown path fFill!");
return false;
}
}
SkRect devBounds;
GetPathDevBounds(path, drawContext->width(), drawContext->height(), viewMatrix, &devBounds);
SkASSERT(passCount <= kMaxNumPasses);
for (int p = 0; p < passCount; ++p) {
if (lastPassIsBounds && (p == passCount-1)) {
SkRect bounds;
SkMatrix localMatrix = SkMatrix::I();
if (reverse) {
// draw over the dev bounds (which will be the whole dst surface for inv fill).
bounds = devBounds;
SkMatrix vmi;
// mapRect through persp matrix may not be correct
if (!viewMatrix.hasPerspective() && viewMatrix.invert(&vmi)) {
vmi.mapRect(&bounds);
} else {
if (!viewMatrix.invert(&localMatrix)) {
return false;
}
}
} else {
bounds = path.getBounds();
}
const SkMatrix& viewM = (reverse && viewMatrix.hasPerspective()) ? SkMatrix::I() :
viewMatrix;
SkAutoTUnref<GrDrawBatch> batch(
GrRectBatchFactory::CreateNonAAFill(paint.getColor(), viewM, bounds, nullptr,
&localMatrix));
GrPipelineBuilder pipelineBuilder(paint, drawContext->mustUseHWAA(paint));
pipelineBuilder.setUserStencil(passes[p]);
drawContext->drawBatch(pipelineBuilder, clip, batch);
} else {
SkAutoTUnref<MSAAPathBatch> batch(new MSAAPathBatch(paint.getColor(), path,
viewMatrix, devBounds));
if (!batch->isValid()) {
return false;
}
GrPipelineBuilder pipelineBuilder(paint, drawContext->mustUseHWAA(paint));
pipelineBuilder.setUserStencil(passes[p]);
if (passCount > 1) {
pipelineBuilder.setDisableColorXPFactory();
}
drawContext->drawBatch(pipelineBuilder, clip, batch);
}
}
return true;
}
bool GrMSAAPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
// This path renderer only fills and relies on MSAA for antialiasing. Stroked shapes are
// handled by passing on the original shape and letting the caller compute the stroked shape
// which will have a fill style.
return args.fShape->style().isSimpleFill() && !args.fAntiAlias;
}
bool GrMSAAPathRenderer::onDrawPath(const DrawPathArgs& args) {
GR_AUDIT_TRAIL_AUTO_FRAME(args.fDrawContext->auditTrail(),
"GrMSAAPathRenderer::onDrawPath");
SkTLazy<GrShape> tmpShape;
const GrShape* shape = args.fShape;
if (shape->style().applies()) {
SkScalar styleScale = GrStyle::MatrixToScaleFactor(*args.fViewMatrix);
tmpShape.init(args.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale));
shape = tmpShape.get();
}
return this->internalDrawPath(args.fDrawContext,
*args.fPaint,
args.fUserStencilSettings,
*args.fClip,
*args.fViewMatrix,
*shape,
false);
}
void GrMSAAPathRenderer::onStencilPath(const StencilPathArgs& args) {
GR_AUDIT_TRAIL_AUTO_FRAME(args.fDrawContext->auditTrail(),
"GrMSAAPathRenderer::onStencilPath");
SkASSERT(args.fShape->style().isSimpleFill());
SkASSERT(!args.fShape->mayBeInverseFilledAfterStyling());
GrPaint paint;
paint.setXPFactory(GrDisableColorXPFactory::Make());
paint.setAntiAlias(args.fIsAA);
this->internalDrawPath(args.fDrawContext, paint, &GrUserStencilSettings::kUnused, *args.fClip,
*args.fViewMatrix, *args.fShape, true);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
|