blob: edc285e41132bc8248f4a6045cdcbf169d025f41 [file] [log] [blame]
ethannicholas6536ae52016-05-02 12:16:49 -07001/*
2 * Copyright 2016 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 "GrMSAAPathRenderer.h"
9
robertphillips976f5f02016-06-03 10:59:20 -070010#include "GrAuditTrail.h"
ethannicholas6536ae52016-05-02 12:16:49 -070011#include "GrBatchFlushState.h"
robertphillips976f5f02016-06-03 10:59:20 -070012#include "GrClip.h"
ethannicholas6536ae52016-05-02 12:16:49 -070013#include "GrDefaultGeoProcFactory.h"
14#include "GrPathStencilSettings.h"
15#include "GrPathUtils.h"
16#include "GrPipelineBuilder.h"
17#include "GrMesh.h"
18#include "SkGeometry.h"
19#include "SkTraceEvent.h"
20#include "glsl/GrGLSLGeometryProcessor.h"
21#include "glsl/GrGLSLFragmentShaderBuilder.h"
22#include "glsl/GrGLSLVertexShaderBuilder.h"
23#include "glsl/GrGLSLProgramDataManager.h"
24#include "glsl/GrGLSLUtil.h"
25#include "gl/GrGLVaryingHandler.h"
26#include "batches/GrRectBatchFactory.h"
27#include "batches/GrVertexBatch.h"
28
29static const float kTolerance = 0.5f;
30
31////////////////////////////////////////////////////////////////////////////////
32// Helpers for drawPath
33
bsalomon6663acf2016-05-10 09:14:17 -070034static inline bool single_pass_path(const SkPath& path) {
ethannicholas6536ae52016-05-02 12:16:49 -070035 if (!path.isInverseFillType()) {
36 return path.isConvex();
37 }
38 return false;
39}
40
41GrPathRenderer::StencilSupport
bsalomond6f25bf2016-05-05 09:26:21 -070042GrMSAAPathRenderer::onGetStencilSupport(const SkPath& path) const {
bsalomon6663acf2016-05-10 09:14:17 -070043 if (single_pass_path(path)) {
ethannicholas6536ae52016-05-02 12:16:49 -070044 return GrPathRenderer::kNoRestriction_StencilSupport;
45 } else {
46 return GrPathRenderer::kStencilOnly_StencilSupport;
47 }
48}
49
50struct MSAALineVertices {
51 struct Vertex {
52 SkPoint fPosition;
53 SkColor fColor;
54 };
55 Vertex* vertices;
56 Vertex* nextVertex;
57#ifdef SK_DEBUG
58 Vertex* verticesEnd;
59#endif
60 uint16_t* indices;
61 uint16_t* nextIndex;
62};
63
64struct MSAAQuadVertices {
65 struct Vertex {
66 SkPoint fPosition;
67 SkPoint fUV;
68 SkColor fColor;
69 };
70 Vertex* vertices;
71 Vertex* nextVertex;
72#ifdef SK_DEBUG
73 Vertex* verticesEnd;
74#endif
75 uint16_t* indices;
76 uint16_t* nextIndex;
77};
78
79static inline void append_contour_edge_indices(uint16_t fanCenterIdx,
80 uint16_t edgeV0Idx,
81 MSAALineVertices& lines) {
82 *(lines.nextIndex++) = fanCenterIdx;
83 *(lines.nextIndex++) = edgeV0Idx;
84 *(lines.nextIndex++) = edgeV0Idx + 1;
85}
86
bungeman06ca8ec2016-06-09 08:01:03 -070087static inline void add_quad(MSAALineVertices& lines, MSAAQuadVertices& quads, const SkPoint pts[],
ethannicholas6536ae52016-05-02 12:16:49 -070088 SkColor color, bool indexed, uint16_t subpathLineIdxStart) {
89 SkASSERT(lines.nextVertex < lines.verticesEnd);
90 *lines.nextVertex = { pts[2], color };
91 if (indexed) {
92 int prevIdx = (uint16_t) (lines.nextVertex - lines.vertices - 1);
93 if (prevIdx > subpathLineIdxStart) {
94 append_contour_edge_indices(subpathLineIdxStart, prevIdx, lines);
95 }
96 }
97 lines.nextVertex++;
98
99 SkASSERT(quads.nextVertex + 2 < quads.verticesEnd);
100 // the texture coordinates are drawn from the Loop-Blinn rendering algorithm
101 *(quads.nextVertex++) = { pts[0], SkPoint::Make(0.0, 0.0), color };
102 *(quads.nextVertex++) = { pts[1], SkPoint::Make(0.5, 0.0), color };
103 *(quads.nextVertex++) = { pts[2], SkPoint::Make(1.0, 1.0), color };
104 if (indexed) {
105 uint16_t offset = (uint16_t) (quads.nextVertex - quads.vertices) - 3;
106 *(quads.nextIndex++) = offset++;
107 *(quads.nextIndex++) = offset++;
108 *(quads.nextIndex++) = offset++;
109 }
110}
111
112class MSAAQuadProcessor : public GrGeometryProcessor {
113public:
114 static GrGeometryProcessor* Create(const SkMatrix& viewMatrix) {
115 return new MSAAQuadProcessor(viewMatrix);
116 }
117
118 virtual ~MSAAQuadProcessor() {}
119
120 const char* name() const override { return "MSAAQuadProcessor"; }
121
122 const Attribute* inPosition() const { return fInPosition; }
123 const Attribute* inUV() const { return fInUV; }
124 const Attribute* inColor() const { return fInColor; }
125 const SkMatrix& viewMatrix() const { return fViewMatrix; }
126 const SkMatrix& localMatrix() const { return SkMatrix::I(); }
127
128 class GLSLProcessor : public GrGLSLGeometryProcessor {
129 public:
130 GLSLProcessor(const GrGeometryProcessor& qpr) {}
131
132 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
133 const MSAAQuadProcessor& qp = args.fGP.cast<MSAAQuadProcessor>();
134 GrGLSLVertexBuilder* vsBuilder = args.fVertBuilder;
135 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
136 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
137
138 // emit attributes
139 varyingHandler->emitAttributes(qp);
140 varyingHandler->addPassThroughAttribute(qp.inColor(), args.fOutputColor);
141
142 GrGLSLVertToFrag uv(kVec2f_GrSLType);
143 varyingHandler->addVarying("uv", &uv, kHigh_GrSLPrecision);
144 vsBuilder->codeAppendf("%s = %s;", uv.vsOut(), qp.inUV()->fName);
145
146 // Setup position
bungeman06ca8ec2016-06-09 08:01:03 -0700147 this->setupPosition(vsBuilder, uniformHandler, gpArgs, qp.inPosition()->fName,
ethannicholas6536ae52016-05-02 12:16:49 -0700148 qp.viewMatrix(), &fViewMatrixUniform);
149
150 // emit transforms
bungeman06ca8ec2016-06-09 08:01:03 -0700151 this->emitTransforms(vsBuilder, varyingHandler, uniformHandler, gpArgs->fPositionVar,
152 qp.inPosition()->fName, SkMatrix::I(), args.fTransformsIn,
ethannicholas6536ae52016-05-02 12:16:49 -0700153 args.fTransformsOut);
154
155 GrGLSLPPFragmentBuilder* fsBuilder = args.fFragBuilder;
bungeman06ca8ec2016-06-09 08:01:03 -0700156 fsBuilder->codeAppendf("if (%s.x * %s.x >= %s.y) discard;", uv.fsIn(), uv.fsIn(),
ethannicholas6536ae52016-05-02 12:16:49 -0700157 uv.fsIn());
158 fsBuilder->codeAppendf("%s = vec4(1.0);", args.fOutputCoverage);
159 }
160
161 static inline void GenKey(const GrGeometryProcessor& gp,
162 const GrGLSLCaps&,
163 GrProcessorKeyBuilder* b) {
164 const MSAAQuadProcessor& qp = gp.cast<MSAAQuadProcessor>();
165 uint32_t key = 0;
166 key |= qp.viewMatrix().hasPerspective() ? 0x1 : 0x0;
167 key |= qp.viewMatrix().isIdentity() ? 0x2: 0x0;
168 b->add32(key);
169 }
170
171 virtual void setData(const GrGLSLProgramDataManager& pdman,
172 const GrPrimitiveProcessor& gp) override {
173 const MSAAQuadProcessor& qp = gp.cast<MSAAQuadProcessor>();
174 if (!qp.viewMatrix().isIdentity()) {
175 float viewMatrix[3 * 3];
176 GrGLSLGetMatrix<3>(viewMatrix, qp.viewMatrix());
177 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
178 }
179 }
180
181 void setTransformData(const GrPrimitiveProcessor& primProc,
182 const GrGLSLProgramDataManager& pdman,
183 int index,
184 const SkTArray<const GrCoordTransform*, true>& transforms) override {
185 this->setTransformDataHelper<MSAAQuadProcessor>(primProc, pdman, index, transforms);
186 }
187
188 private:
189 typedef GrGLSLGeometryProcessor INHERITED;
190
191 UniformHandle fViewMatrixUniform;
192 };
193
194 virtual void getGLSLProcessorKey(const GrGLSLCaps& caps,
195 GrProcessorKeyBuilder* b) const override {
196 GLSLProcessor::GenKey(*this, caps, b);
197 }
198
199 virtual GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) const override {
200 return new GLSLProcessor(*this);
201 }
202
203private:
204 MSAAQuadProcessor(const SkMatrix& viewMatrix)
205 : fViewMatrix(viewMatrix) {
206 this->initClassID<MSAAQuadProcessor>();
bungeman06ca8ec2016-06-09 08:01:03 -0700207 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType,
ethannicholas6536ae52016-05-02 12:16:49 -0700208 kHigh_GrSLPrecision));
bungeman06ca8ec2016-06-09 08:01:03 -0700209 fInUV = &this->addVertexAttrib(Attribute("inUV", kVec2f_GrVertexAttribType,
ethannicholas6536ae52016-05-02 12:16:49 -0700210 kHigh_GrSLPrecision));
211 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType));
212 this->setSampleShading(1.0f);
213 }
214
215 const Attribute* fInPosition;
216 const Attribute* fInUV;
217 const Attribute* fInColor;
218 SkMatrix fViewMatrix;
bungeman06ca8ec2016-06-09 08:01:03 -0700219
ethannicholas6536ae52016-05-02 12:16:49 -0700220 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
221
222 typedef GrGeometryProcessor INHERITED;
223};
224
225class MSAAPathBatch : public GrVertexBatch {
226public:
227 DEFINE_BATCH_CLASS_ID
228
229 struct Geometry {
230 GrColor fColor;
231 SkPath fPath;
232 SkScalar fTolerance;
233 };
234
bungeman06ca8ec2016-06-09 08:01:03 -0700235 static MSAAPathBatch* Create(const Geometry& geometry, const SkMatrix& viewMatrix,
ethannicholas6536ae52016-05-02 12:16:49 -0700236 const SkRect& devBounds) {
237 return new MSAAPathBatch(geometry, viewMatrix, devBounds);
238 }
239
240 const char* name() const override { return "MSAAPathBatch"; }
241
bungeman06ca8ec2016-06-09 08:01:03 -0700242 void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholas6536ae52016-05-02 12:16:49 -0700243 GrInitInvariantOutput* coverage,
244 GrBatchToXPOverrides* overrides) const override {
245 // When this is called on a batch, there is only one geometry bundle
246 color->setKnownFourComponents(fGeoData[0].fColor);
247 coverage->setKnownSingleComponent(0xff);
248 }
249
250 bool isValid() const {
251 return !fIsIndexed || fMaxLineIndices <= SK_MaxU16;
252 }
253
254private:
255 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
256 // Handle any color overrides
257 if (!overrides.readsColor()) {
258 fGeoData[0].fColor = GrColor_ILLEGAL;
259 }
260 overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
261 }
262
bungeman06ca8ec2016-06-09 08:01:03 -0700263 void computeWorstCasePointCount(const SkPath& path, int* subpaths, SkScalar tol,
ethannicholas6536ae52016-05-02 12:16:49 -0700264 int* outLinePointCount, int* outQuadPointCount) const {
265 int linePointCount = 0;
266 int quadPointCount = 0;
267 *subpaths = 1;
268
269 bool first = true;
270
271 SkPath::Iter iter(path, false);
272 SkPath::Verb verb;
273
274 SkPoint pts[4];
275 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
276 switch (verb) {
277 case SkPath::kLine_Verb:
278 linePointCount += 1;
279 break;
280 case SkPath::kConic_Verb: {
281 SkScalar weight = iter.conicWeight();
282 SkAutoConicToQuads converter;
283 converter.computeQuads(pts, weight, kTolerance);
284 int quadPts = converter.countQuads();
285 linePointCount += quadPts;
286 quadPointCount += 3 * quadPts;
287 }
288 case SkPath::kQuad_Verb:
289 linePointCount += 1;
290 quadPointCount += 3;
291 break;
292 case SkPath::kCubic_Verb: {
293 SkSTArray<15, SkPoint, true> quadPts;
294 GrPathUtils::convertCubicToQuads(pts, kTolerance, &quadPts);
295 int count = quadPts.count();
296 linePointCount += count / 3;
297 quadPointCount += count;
298 break;
299 }
300 case SkPath::kMove_Verb:
301 linePointCount += 1;
302 if (!first) {
303 ++(*subpaths);
304 }
305 break;
306 default:
307 break;
308 }
309 first = false;
310 }
311 *outLinePointCount = linePointCount;
312 *outQuadPointCount = quadPointCount;
313 }
314
315 void onPrepareDraws(Target* target) const override {
316 SkASSERT(this->isValid());
317 if (fMaxLineVertices == 0) {
318 SkASSERT(fMaxQuadVertices == 0);
319 return;
320 }
321
bungeman06ca8ec2016-06-09 08:01:03 -0700322 GrPrimitiveType primitiveType = fIsIndexed ? kTriangles_GrPrimitiveType
ethannicholas6536ae52016-05-02 12:16:49 -0700323 : kTriangleFan_GrPrimitiveType;
324
325 // allocate vertex / index buffers
326 const GrBuffer* lineVertexBuffer;
327 int firstLineVertex;
328 MSAALineVertices lines;
329 size_t lineVertexStride = sizeof(MSAALineVertices::Vertex);
bungeman06ca8ec2016-06-09 08:01:03 -0700330 lines.vertices = (MSAALineVertices::Vertex*) target->makeVertexSpace(lineVertexStride,
ethannicholas6536ae52016-05-02 12:16:49 -0700331 fMaxLineVertices,
bungeman06ca8ec2016-06-09 08:01:03 -0700332 &lineVertexBuffer,
ethannicholas6536ae52016-05-02 12:16:49 -0700333 &firstLineVertex);
334 if (!lines.vertices) {
335 SkDebugf("Could not allocate vertices\n");
336 return;
337 }
338 lines.nextVertex = lines.vertices;
339 SkDEBUGCODE(lines.verticesEnd = lines.vertices + fMaxLineVertices;)
340
341 MSAAQuadVertices quads;
342 size_t quadVertexStride = sizeof(MSAAQuadVertices::Vertex);
343 SkAutoFree quadVertexPtr(sk_malloc_throw(fMaxQuadVertices * quadVertexStride));
344 quads.vertices = (MSAAQuadVertices::Vertex*) quadVertexPtr.get();
345 quads.nextVertex = quads.vertices;
346 SkDEBUGCODE(quads.verticesEnd = quads.vertices + fMaxQuadVertices;)
347
348 const GrBuffer* lineIndexBuffer = nullptr;
349 int firstLineIndex;
350 if (fIsIndexed) {
bungeman06ca8ec2016-06-09 08:01:03 -0700351 lines.indices = target->makeIndexSpace(fMaxLineIndices, &lineIndexBuffer,
ethannicholas6536ae52016-05-02 12:16:49 -0700352 &firstLineIndex);
353 if (!lines.indices) {
354 SkDebugf("Could not allocate indices\n");
355 return;
356 }
357 lines.nextIndex = lines.indices;
358 } else {
359 lines.indices = nullptr;
360 lines.nextIndex = nullptr;
361 }
362
363 SkAutoFree quadIndexPtr;
364 if (fIsIndexed) {
365 quads.indices = (uint16_t*) sk_malloc_throw(fMaxQuadIndices * sizeof(uint16_t));
366 quadIndexPtr.set(quads.indices);
367 quads.nextIndex = quads.indices;
368 } else {
369 quads.indices = nullptr;
370 quads.nextIndex = nullptr;
371 }
372
373 // fill buffers
374 for (int i = 0; i < fGeoData.count(); i++) {
375 const Geometry& args = fGeoData[i];
376
377 if (!this->createGeom(lines,
378 quads,
379 args.fPath,
380 args.fTolerance,
381 fViewMatrix,
382 args.fColor,
383 fIsIndexed)) {
384 return;
385 }
386 }
387 int lineVertexOffset = (int) (lines.nextVertex - lines.vertices);
388 int lineIndexOffset = (int) (lines.nextIndex - lines.indices);
389 SkASSERT(lineVertexOffset <= fMaxLineVertices && lineIndexOffset <= fMaxLineIndices);
390 int quadVertexOffset = (int) (quads.nextVertex - quads.vertices);
391 int quadIndexOffset = (int) (quads.nextIndex - quads.indices);
392 SkASSERT(quadVertexOffset <= fMaxQuadVertices && quadIndexOffset <= fMaxQuadIndices);
393
394 if (lineVertexOffset) {
bungeman06ca8ec2016-06-09 08:01:03 -0700395 sk_sp<GrGeometryProcessor> lineGP;
ethannicholas6536ae52016-05-02 12:16:49 -0700396 {
397 using namespace GrDefaultGeoProcFactory;
bungeman06ca8ec2016-06-09 08:01:03 -0700398 lineGP = GrDefaultGeoProcFactory::Make(Color(Color::kAttribute_Type),
399 Coverage(255),
400 LocalCoords(LocalCoords::kUnused_Type),
401 fViewMatrix);
ethannicholas6536ae52016-05-02 12:16:49 -0700402 }
403 SkASSERT(lineVertexStride == lineGP->getVertexStride());
404
405 GrMesh lineMeshes;
406 if (fIsIndexed) {
bungeman06ca8ec2016-06-09 08:01:03 -0700407 lineMeshes.initIndexed(primitiveType, lineVertexBuffer, lineIndexBuffer,
408 firstLineVertex, firstLineIndex, lineVertexOffset,
ethannicholas6536ae52016-05-02 12:16:49 -0700409 lineIndexOffset);
410 } else {
bungeman06ca8ec2016-06-09 08:01:03 -0700411 lineMeshes.init(primitiveType, lineVertexBuffer, firstLineVertex,
ethannicholas6536ae52016-05-02 12:16:49 -0700412 lineVertexOffset);
413 }
bungeman06ca8ec2016-06-09 08:01:03 -0700414 target->draw(lineGP.get(), lineMeshes);
ethannicholas6536ae52016-05-02 12:16:49 -0700415 }
416
417 if (quadVertexOffset) {
418 SkAutoTUnref<const GrGeometryProcessor> quadGP(MSAAQuadProcessor::Create(fViewMatrix));
419 SkASSERT(quadVertexStride == quadGP->getVertexStride());
420
421 const GrBuffer* quadVertexBuffer;
422 int firstQuadVertex;
bungeman06ca8ec2016-06-09 08:01:03 -0700423 MSAAQuadVertices::Vertex* quadVertices = (MSAAQuadVertices::Vertex*)
ethannicholas6536ae52016-05-02 12:16:49 -0700424 target->makeVertexSpace(quadVertexStride, quadVertexOffset, &quadVertexBuffer,
425 &firstQuadVertex);
426 memcpy(quadVertices, quads.vertices, quadVertexStride * quadVertexOffset);
427 GrMesh quadMeshes;
428 if (fIsIndexed) {
429 const GrBuffer* quadIndexBuffer;
430 int firstQuadIndex;
bungeman06ca8ec2016-06-09 08:01:03 -0700431 uint16_t* quadIndices = (uint16_t*) target->makeIndexSpace(quadIndexOffset,
432 &quadIndexBuffer,
ethannicholas6536ae52016-05-02 12:16:49 -0700433 &firstQuadIndex);
434 memcpy(quadIndices, quads.indices, sizeof(uint16_t) * quadIndexOffset);
bungeman06ca8ec2016-06-09 08:01:03 -0700435 quadMeshes.initIndexed(kTriangles_GrPrimitiveType, quadVertexBuffer,
436 quadIndexBuffer, firstQuadVertex, firstQuadIndex,
ethannicholas6536ae52016-05-02 12:16:49 -0700437 quadVertexOffset, quadIndexOffset);
438 } else {
bungeman06ca8ec2016-06-09 08:01:03 -0700439 quadMeshes.init(kTriangles_GrPrimitiveType, quadVertexBuffer, firstQuadVertex,
ethannicholas6536ae52016-05-02 12:16:49 -0700440 quadVertexOffset);
441 }
442 target->draw(quadGP, quadMeshes);
443 }
444 }
445
446 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
447
448 MSAAPathBatch(const Geometry& geometry, const SkMatrix& viewMatrix, const SkRect& devBounds)
449 : INHERITED(ClassID())
450 , fViewMatrix(viewMatrix) {
451 fGeoData.push_back(geometry);
452 this->setBounds(devBounds);
453 int contourCount;
bungeman06ca8ec2016-06-09 08:01:03 -0700454 this->computeWorstCasePointCount(geometry.fPath, &contourCount, kTolerance,
ethannicholas6536ae52016-05-02 12:16:49 -0700455 &fMaxLineVertices, &fMaxQuadVertices);
456 fMaxLineIndices = fMaxLineVertices * 3;
457 fMaxQuadIndices = fMaxQuadVertices * 3;
458 fIsIndexed = contourCount > 1;
459 }
460
461 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
462 MSAAPathBatch* that = t->cast<MSAAPathBatch>();
463 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
464 that->bounds(), caps)) {
465 return false;
466 }
467
468 if (!fViewMatrix.cheapEqualTo(that->fViewMatrix)) {
469 return false;
470 }
471
bungeman06ca8ec2016-06-09 08:01:03 -0700472 if ((fMaxLineIndices + that->fMaxLineIndices > SK_MaxU16) ||
ethannicholas6536ae52016-05-02 12:16:49 -0700473 (fMaxQuadIndices + that->fMaxQuadIndices > SK_MaxU16)) {
474 return false;
475 }
476
477 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
478 this->joinBounds(that->bounds());
479 fIsIndexed = true;
480 fMaxLineVertices += that->fMaxLineVertices;
481 fMaxQuadVertices += that->fMaxQuadVertices;
482 fMaxLineIndices += that->fMaxLineIndices;
483 fMaxQuadIndices += that->fMaxQuadIndices;
484 return true;
485 }
486
487 bool createGeom(MSAALineVertices& lines,
488 MSAAQuadVertices& quads,
489 const SkPath& path,
490 SkScalar srcSpaceTol,
491 const SkMatrix& m,
492 SkColor color,
493 bool isIndexed) const {
494 {
495 uint16_t subpathIdxStart = (uint16_t) (lines.nextVertex - lines.vertices);
496
497 SkPoint pts[4];
498
499 bool first = true;
500 SkPath::Iter iter(path, false);
501
502 bool done = false;
503 while (!done) {
504 SkPath::Verb verb = iter.next(pts);
505 switch (verb) {
506 case SkPath::kMove_Verb:
507 if (!first) {
508 uint16_t currIdx = (uint16_t) (lines.nextVertex - lines.vertices);
509 subpathIdxStart = currIdx;
510 }
511 SkASSERT(lines.nextVertex < lines.verticesEnd);
512 *(lines.nextVertex++) = { pts[0], color };
513 break;
514 case SkPath::kLine_Verb:
515 if (isIndexed) {
516 uint16_t prevIdx = (uint16_t) (lines.nextVertex - lines.vertices - 1);
517 if (prevIdx > subpathIdxStart) {
518 append_contour_edge_indices(subpathIdxStart, prevIdx, lines);
519 }
520 }
521 SkASSERT(lines.nextVertex < lines.verticesEnd);
522 *(lines.nextVertex++) = { pts[1], color };
523 break;
524 case SkPath::kConic_Verb: {
525 SkScalar weight = iter.conicWeight();
526 SkAutoConicToQuads converter;
bungeman06ca8ec2016-06-09 08:01:03 -0700527 const SkPoint* quadPts = converter.computeQuads(pts, weight,
ethannicholas6536ae52016-05-02 12:16:49 -0700528 kTolerance);
529 for (int i = 0; i < converter.countQuads(); ++i) {
bungeman06ca8ec2016-06-09 08:01:03 -0700530 add_quad(lines, quads, quadPts + i * 2, color, isIndexed,
ethannicholas6536ae52016-05-02 12:16:49 -0700531 subpathIdxStart);
532 }
533 break;
534 }
535 case SkPath::kQuad_Verb: {
536 add_quad(lines, quads, pts, color, isIndexed, subpathIdxStart);
bungeman06ca8ec2016-06-09 08:01:03 -0700537 break;
ethannicholas6536ae52016-05-02 12:16:49 -0700538 }
539 case SkPath::kCubic_Verb: {
540 SkSTArray<15, SkPoint, true> quadPts;
541 GrPathUtils::convertCubicToQuads(pts, kTolerance, &quadPts);
542 int count = quadPts.count();
543 for (int i = 0; i < count; i += 3) {
544 add_quad(lines, quads, &quadPts[i], color, isIndexed, subpathIdxStart);
545 }
546 break;
547 }
548 case SkPath::kClose_Verb:
549 break;
550 case SkPath::kDone_Verb:
551 done = true;
552 }
553 first = false;
554 }
555 }
556 return true;
557 }
558
559 SkSTArray<1, Geometry, true> fGeoData;
560
561 SkMatrix fViewMatrix;
562 int fMaxLineVertices;
563 int fMaxQuadVertices;
564 int fMaxLineIndices;
565 int fMaxQuadIndices;
566 bool fIsIndexed;
567
568 typedef GrVertexBatch INHERITED;
569};
570
robertphillips976f5f02016-06-03 10:59:20 -0700571bool GrMSAAPathRenderer::internalDrawPath(GrDrawContext* drawContext,
572 const GrPaint& paint,
573 const GrUserStencilSettings* userStencilSettings,
cdalton862cff32016-05-12 15:09:48 -0700574 const GrClip& clip,
ethannicholas6536ae52016-05-02 12:16:49 -0700575 GrColor color,
576 const SkMatrix& viewMatrix,
577 const SkPath& path,
ethannicholas6536ae52016-05-02 12:16:49 -0700578 bool stencilOnly) {
cdalton93a379b2016-05-11 13:58:08 -0700579 int passCount = 0;
580 const GrUserStencilSettings* passes[3];
581 GrPipelineBuilder::DrawFace drawFace[3];
582 bool reverse = false;
583 bool lastPassIsBounds;
ethannicholas6536ae52016-05-02 12:16:49 -0700584
bsalomon6663acf2016-05-10 09:14:17 -0700585 if (single_pass_path(path)) {
ethannicholas6536ae52016-05-02 12:16:49 -0700586 passCount = 1;
587 if (stencilOnly) {
588 passes[0] = &gDirectToStencil;
589 } else {
590 passes[0] = nullptr;
591 }
592 drawFace[0] = GrPipelineBuilder::kBoth_DrawFace;
593 lastPassIsBounds = false;
594 } else {
595 switch (path.getFillType()) {
596 case SkPath::kInverseEvenOdd_FillType:
597 reverse = true;
598 // fallthrough
599 case SkPath::kEvenOdd_FillType:
600 passes[0] = &gEOStencilPass;
601 if (stencilOnly) {
602 passCount = 1;
603 lastPassIsBounds = false;
604 } else {
605 passCount = 2;
606 lastPassIsBounds = true;
607 if (reverse) {
608 passes[1] = &gInvEOColorPass;
609 } else {
610 passes[1] = &gEOColorPass;
611 }
612 }
613 drawFace[0] = drawFace[1] = GrPipelineBuilder::kBoth_DrawFace;
614 break;
615
616 case SkPath::kInverseWinding_FillType:
617 reverse = true;
618 // fallthrough
619 case SkPath::kWinding_FillType:
620 passes[0] = &gWindStencilSeparateWithWrap;
621 passCount = 2;
622 drawFace[0] = GrPipelineBuilder::kBoth_DrawFace;
623 if (stencilOnly) {
624 lastPassIsBounds = false;
625 --passCount;
626 } else {
627 lastPassIsBounds = true;
628 drawFace[passCount-1] = GrPipelineBuilder::kBoth_DrawFace;
629 if (reverse) {
630 passes[passCount-1] = &gInvWindColorPass;
631 } else {
632 passes[passCount-1] = &gWindColorPass;
633 }
634 }
635 break;
636 default:
637 SkDEBUGFAIL("Unknown path fFill!");
638 return false;
639 }
640 }
641
642 SkRect devBounds;
robertphillips976f5f02016-06-03 10:59:20 -0700643 GetPathDevBounds(path, drawContext->width(), drawContext->height(), viewMatrix, &devBounds);
ethannicholas6536ae52016-05-02 12:16:49 -0700644
645 for (int p = 0; p < passCount; ++p) {
ethannicholas6536ae52016-05-02 12:16:49 -0700646 if (lastPassIsBounds && (p == passCount-1)) {
ethannicholas6536ae52016-05-02 12:16:49 -0700647 SkRect bounds;
648 SkMatrix localMatrix = SkMatrix::I();
649 if (reverse) {
ethannicholas6536ae52016-05-02 12:16:49 -0700650 // draw over the dev bounds (which will be the whole dst surface for inv fill).
651 bounds = devBounds;
652 SkMatrix vmi;
653 // mapRect through persp matrix may not be correct
654 if (!viewMatrix.hasPerspective() && viewMatrix.invert(&vmi)) {
655 vmi.mapRect(&bounds);
656 } else {
657 if (!viewMatrix.invert(&localMatrix)) {
658 return false;
659 }
660 }
661 } else {
662 bounds = path.getBounds();
663 }
664 const SkMatrix& viewM = (reverse && viewMatrix.hasPerspective()) ? SkMatrix::I() :
665 viewMatrix;
666 SkAutoTUnref<GrDrawBatch> batch(
667 GrRectBatchFactory::CreateNonAAFill(color, viewM, bounds, nullptr,
668 &localMatrix));
robertphillips976f5f02016-06-03 10:59:20 -0700669
csmartdaltonecbc12b2016-06-08 10:08:43 -0700670 GrPipelineBuilder pipelineBuilder(paint, drawContext->mustUseHWAA(paint));
robertphillips976f5f02016-06-03 10:59:20 -0700671 pipelineBuilder.setDrawFace(drawFace[p]);
672 if (passes[p]) {
673 pipelineBuilder.setUserStencil(passes[p]);
674 } else {
675 pipelineBuilder.setUserStencil(userStencilSettings);
ethannicholas6536ae52016-05-02 12:16:49 -0700676 }
677
robertphillips976f5f02016-06-03 10:59:20 -0700678 drawContext->drawBatch(pipelineBuilder, clip, batch);
679 } else {
ethannicholas6536ae52016-05-02 12:16:49 -0700680 MSAAPathBatch::Geometry geometry;
681 geometry.fColor = color;
682 geometry.fPath = path;
683 geometry.fTolerance = kTolerance;
684
bungeman06ca8ec2016-06-09 08:01:03 -0700685 SkAutoTUnref<MSAAPathBatch> batch(MSAAPathBatch::Create(geometry, viewMatrix,
ethannicholas6536ae52016-05-02 12:16:49 -0700686 devBounds));
robertphillips976f5f02016-06-03 10:59:20 -0700687 if (!batch->isValid()) {
ethannicholas6536ae52016-05-02 12:16:49 -0700688 return false;
689 }
robertphillips976f5f02016-06-03 10:59:20 -0700690
csmartdaltonecbc12b2016-06-08 10:08:43 -0700691 GrPipelineBuilder pipelineBuilder(paint, drawContext->mustUseHWAA(paint));
robertphillips976f5f02016-06-03 10:59:20 -0700692 pipelineBuilder.setDrawFace(drawFace[p]);
693 if (passes[p]) {
694 pipelineBuilder.setUserStencil(passes[p]);
695 } else {
696 pipelineBuilder.setUserStencil(userStencilSettings);
697 }
698 if (passCount > 1) {
699 pipelineBuilder.setDisableColorXPFactory();
700 }
701
702 drawContext->drawBatch(pipelineBuilder, clip, batch);
ethannicholas6536ae52016-05-02 12:16:49 -0700703 }
704 }
705 return true;
706}
707
708bool GrMSAAPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
bsalomon6663acf2016-05-10 09:14:17 -0700709 // This path renderer does not support hairlines. We defer on anything that could be handled
710 // as a hairline by another path renderer. Also, arbitrary path effects could produce
711 // a hairline result.
712 return !IsStrokeHairlineOrEquivalent(*args.fStyle, *args.fViewMatrix, nullptr) &&
713 !args.fStyle->strokeRec().isHairlineStyle() &&
714 !args.fStyle->hasNonDashPathEffect() && !args.fAntiAlias;
ethannicholas6536ae52016-05-02 12:16:49 -0700715}
716
717bool GrMSAAPathRenderer::onDrawPath(const DrawPathArgs& args) {
robertphillips976f5f02016-06-03 10:59:20 -0700718 GR_AUDIT_TRAIL_AUTO_FRAME(args.fDrawContext->auditTrail(),
719 "GrMSAAPathRenderer::onDrawPath");
bsalomon6663acf2016-05-10 09:14:17 -0700720 SkPath tmpPath;
721 const SkPath* path;
722 if (args.fStyle->applies()) {
723 SkStrokeRec::InitStyle fill;
724 SkScalar styleScale = GrStyle::MatrixToScaleFactor(*args.fViewMatrix);
725 if (!args.fStyle->applyToPath(&tmpPath, &fill, *args.fPath, styleScale)) {
ethannicholas6536ae52016-05-02 12:16:49 -0700726 return false;
727 }
bsalomon6663acf2016-05-10 09:14:17 -0700728 // We don't accept styles that are hairlines or have path effects that could produce
729 // hairlines.
730 SkASSERT(SkStrokeRec::kFill_InitStyle == fill);
731 path = &tmpPath;
ethannicholas6536ae52016-05-02 12:16:49 -0700732 } else {
bsalomon6663acf2016-05-10 09:14:17 -0700733 path = args.fPath;
ethannicholas6536ae52016-05-02 12:16:49 -0700734 }
robertphillips976f5f02016-06-03 10:59:20 -0700735 return this->internalDrawPath(args.fDrawContext,
736 *args.fPaint,
737 args.fUserStencilSettings,
cdalton862cff32016-05-12 15:09:48 -0700738 *args.fClip,
ethannicholas6536ae52016-05-02 12:16:49 -0700739 args.fColor,
740 *args.fViewMatrix,
bsalomon6663acf2016-05-10 09:14:17 -0700741 *path,
ethannicholas6536ae52016-05-02 12:16:49 -0700742 false);
743}
744
745void GrMSAAPathRenderer::onStencilPath(const StencilPathArgs& args) {
robertphillips976f5f02016-06-03 10:59:20 -0700746 GR_AUDIT_TRAIL_AUTO_FRAME(args.fDrawContext->auditTrail(),
747 "GrMSAAPathRenderer::onStencilPath");
ethannicholas6536ae52016-05-02 12:16:49 -0700748 SkASSERT(SkPath::kInverseEvenOdd_FillType != args.fPath->getFillType());
749 SkASSERT(SkPath::kInverseWinding_FillType != args.fPath->getFillType());
robertphillips976f5f02016-06-03 10:59:20 -0700750
751 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700752 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700753 paint.setAntiAlias(args.fIsAA);
754
755 this->internalDrawPath(args.fDrawContext,
756 paint,
757 &GrUserStencilSettings::kUnused,
758 *args.fClip,
759 GrColor_WHITE,
760 *args.fViewMatrix,
761 *args.fPath,
762 true);
ethannicholas6536ae52016-05-02 12:16:49 -0700763}
764
765///////////////////////////////////////////////////////////////////////////////////////////////////