blob: 51389ecddbd6ae06a410c8fc85ba2b89a7dcdd5f [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
bsalomon8acedde2016-06-24 10:42:16 -070034static inline bool single_pass_shape(const GrShape& shape) {
35 if (!shape.inverseFilled()) {
36 return shape.knownToBeConvex();
ethannicholas6536ae52016-05-02 12:16:49 -070037 }
38 return false;
39}
40
bsalomon8acedde2016-06-24 10:42:16 -070041GrPathRenderer::StencilSupport GrMSAAPathRenderer::onGetStencilSupport(const GrShape& shape) const {
42 if (single_pass_shape(shape)) {
ethannicholas6536ae52016-05-02 12:16:49 -070043 return GrPathRenderer::kNoRestriction_StencilSupport;
44 } else {
45 return GrPathRenderer::kStencilOnly_StencilSupport;
46 }
47}
48
49struct MSAALineVertices {
50 struct Vertex {
51 SkPoint fPosition;
52 SkColor fColor;
53 };
54 Vertex* vertices;
55 Vertex* nextVertex;
56#ifdef SK_DEBUG
57 Vertex* verticesEnd;
58#endif
59 uint16_t* indices;
60 uint16_t* nextIndex;
61};
62
63struct MSAAQuadVertices {
64 struct Vertex {
65 SkPoint fPosition;
66 SkPoint fUV;
67 SkColor fColor;
68 };
69 Vertex* vertices;
70 Vertex* nextVertex;
71#ifdef SK_DEBUG
72 Vertex* verticesEnd;
73#endif
74 uint16_t* indices;
75 uint16_t* nextIndex;
76};
77
78static inline void append_contour_edge_indices(uint16_t fanCenterIdx,
79 uint16_t edgeV0Idx,
80 MSAALineVertices& lines) {
81 *(lines.nextIndex++) = fanCenterIdx;
82 *(lines.nextIndex++) = edgeV0Idx;
83 *(lines.nextIndex++) = edgeV0Idx + 1;
84}
85
bungeman06ca8ec2016-06-09 08:01:03 -070086static inline void add_quad(MSAALineVertices& lines, MSAAQuadVertices& quads, const SkPoint pts[],
ethannicholas6536ae52016-05-02 12:16:49 -070087 SkColor color, bool indexed, uint16_t subpathLineIdxStart) {
88 SkASSERT(lines.nextVertex < lines.verticesEnd);
89 *lines.nextVertex = { pts[2], color };
90 if (indexed) {
91 int prevIdx = (uint16_t) (lines.nextVertex - lines.vertices - 1);
92 if (prevIdx > subpathLineIdxStart) {
93 append_contour_edge_indices(subpathLineIdxStart, prevIdx, lines);
94 }
95 }
96 lines.nextVertex++;
97
98 SkASSERT(quads.nextVertex + 2 < quads.verticesEnd);
99 // the texture coordinates are drawn from the Loop-Blinn rendering algorithm
100 *(quads.nextVertex++) = { pts[0], SkPoint::Make(0.0, 0.0), color };
101 *(quads.nextVertex++) = { pts[1], SkPoint::Make(0.5, 0.0), color };
102 *(quads.nextVertex++) = { pts[2], SkPoint::Make(1.0, 1.0), color };
103 if (indexed) {
104 uint16_t offset = (uint16_t) (quads.nextVertex - quads.vertices) - 3;
105 *(quads.nextIndex++) = offset++;
106 *(quads.nextIndex++) = offset++;
107 *(quads.nextIndex++) = offset++;
108 }
109}
110
111class MSAAQuadProcessor : public GrGeometryProcessor {
112public:
113 static GrGeometryProcessor* Create(const SkMatrix& viewMatrix) {
114 return new MSAAQuadProcessor(viewMatrix);
115 }
116
117 virtual ~MSAAQuadProcessor() {}
118
119 const char* name() const override { return "MSAAQuadProcessor"; }
120
121 const Attribute* inPosition() const { return fInPosition; }
122 const Attribute* inUV() const { return fInUV; }
123 const Attribute* inColor() const { return fInColor; }
124 const SkMatrix& viewMatrix() const { return fViewMatrix; }
125 const SkMatrix& localMatrix() const { return SkMatrix::I(); }
126
127 class GLSLProcessor : public GrGLSLGeometryProcessor {
128 public:
129 GLSLProcessor(const GrGeometryProcessor& qpr) {}
130
131 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
132 const MSAAQuadProcessor& qp = args.fGP.cast<MSAAQuadProcessor>();
133 GrGLSLVertexBuilder* vsBuilder = args.fVertBuilder;
134 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
135 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
136
137 // emit attributes
138 varyingHandler->emitAttributes(qp);
139 varyingHandler->addPassThroughAttribute(qp.inColor(), args.fOutputColor);
140
141 GrGLSLVertToFrag uv(kVec2f_GrSLType);
142 varyingHandler->addVarying("uv", &uv, kHigh_GrSLPrecision);
143 vsBuilder->codeAppendf("%s = %s;", uv.vsOut(), qp.inUV()->fName);
144
145 // Setup position
bungeman06ca8ec2016-06-09 08:01:03 -0700146 this->setupPosition(vsBuilder, uniformHandler, gpArgs, qp.inPosition()->fName,
ethannicholas6536ae52016-05-02 12:16:49 -0700147 qp.viewMatrix(), &fViewMatrixUniform);
148
149 // emit transforms
bungeman06ca8ec2016-06-09 08:01:03 -0700150 this->emitTransforms(vsBuilder, varyingHandler, uniformHandler, gpArgs->fPositionVar,
151 qp.inPosition()->fName, SkMatrix::I(), args.fTransformsIn,
ethannicholas6536ae52016-05-02 12:16:49 -0700152 args.fTransformsOut);
153
154 GrGLSLPPFragmentBuilder* fsBuilder = args.fFragBuilder;
bungeman06ca8ec2016-06-09 08:01:03 -0700155 fsBuilder->codeAppendf("if (%s.x * %s.x >= %s.y) discard;", uv.fsIn(), uv.fsIn(),
ethannicholas6536ae52016-05-02 12:16:49 -0700156 uv.fsIn());
157 fsBuilder->codeAppendf("%s = vec4(1.0);", args.fOutputCoverage);
158 }
159
160 static inline void GenKey(const GrGeometryProcessor& gp,
161 const GrGLSLCaps&,
162 GrProcessorKeyBuilder* b) {
163 const MSAAQuadProcessor& qp = gp.cast<MSAAQuadProcessor>();
164 uint32_t key = 0;
165 key |= qp.viewMatrix().hasPerspective() ? 0x1 : 0x0;
166 key |= qp.viewMatrix().isIdentity() ? 0x2: 0x0;
167 b->add32(key);
168 }
169
170 virtual void setData(const GrGLSLProgramDataManager& pdman,
171 const GrPrimitiveProcessor& gp) override {
172 const MSAAQuadProcessor& qp = gp.cast<MSAAQuadProcessor>();
173 if (!qp.viewMatrix().isIdentity()) {
174 float viewMatrix[3 * 3];
175 GrGLSLGetMatrix<3>(viewMatrix, qp.viewMatrix());
176 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
177 }
178 }
179
180 void setTransformData(const GrPrimitiveProcessor& primProc,
181 const GrGLSLProgramDataManager& pdman,
182 int index,
183 const SkTArray<const GrCoordTransform*, true>& transforms) override {
184 this->setTransformDataHelper<MSAAQuadProcessor>(primProc, pdman, index, transforms);
185 }
186
187 private:
188 typedef GrGLSLGeometryProcessor INHERITED;
189
190 UniformHandle fViewMatrixUniform;
191 };
192
193 virtual void getGLSLProcessorKey(const GrGLSLCaps& caps,
194 GrProcessorKeyBuilder* b) const override {
195 GLSLProcessor::GenKey(*this, caps, b);
196 }
197
198 virtual GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) const override {
199 return new GLSLProcessor(*this);
200 }
201
202private:
203 MSAAQuadProcessor(const SkMatrix& viewMatrix)
204 : fViewMatrix(viewMatrix) {
205 this->initClassID<MSAAQuadProcessor>();
bungeman06ca8ec2016-06-09 08:01:03 -0700206 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType,
ethannicholas6536ae52016-05-02 12:16:49 -0700207 kHigh_GrSLPrecision));
bungeman06ca8ec2016-06-09 08:01:03 -0700208 fInUV = &this->addVertexAttrib(Attribute("inUV", kVec2f_GrVertexAttribType,
ethannicholas6536ae52016-05-02 12:16:49 -0700209 kHigh_GrSLPrecision));
210 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType));
211 this->setSampleShading(1.0f);
212 }
213
214 const Attribute* fInPosition;
215 const Attribute* fInUV;
216 const Attribute* fInColor;
217 SkMatrix fViewMatrix;
bungeman06ca8ec2016-06-09 08:01:03 -0700218
ethannicholas6536ae52016-05-02 12:16:49 -0700219 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
220
221 typedef GrGeometryProcessor INHERITED;
222};
223
224class MSAAPathBatch : public GrVertexBatch {
225public:
226 DEFINE_BATCH_CLASS_ID
227
bsalomon88cf17d2016-07-08 06:40:56 -0700228 MSAAPathBatch(GrColor color, const SkPath& path, const SkMatrix& viewMatrix)
bsalomon50c56a32016-06-30 12:05:32 -0700229 : INHERITED(ClassID())
230 , fViewMatrix(viewMatrix) {
231 fPaths.emplace_back(PathInfo{color, path});
bsalomon88cf17d2016-07-08 06:40:56 -0700232 this->setTransformedBounds(path.getBounds(), viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
bsalomon50c56a32016-06-30 12:05:32 -0700233 int contourCount;
234 this->computeWorstCasePointCount(path, &contourCount, &fMaxLineVertices, &fMaxQuadVertices);
235 fMaxLineIndices = fMaxLineVertices * 3;
236 fMaxQuadIndices = fMaxQuadVertices * 3;
237 fIsIndexed = contourCount > 1;
ethannicholas6536ae52016-05-02 12:16:49 -0700238 }
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 {
bsalomon50c56a32016-06-30 12:05:32 -0700245 // When this is called on a batch, there is only one path
246 color->setKnownFourComponents(fPaths[0].fColor);
ethannicholas6536ae52016-05-02 12:16:49 -0700247 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()) {
bsalomon50c56a32016-06-30 12:05:32 -0700258 fPaths[0].fColor = GrColor_ILLEGAL;
ethannicholas6536ae52016-05-02 12:16:49 -0700259 }
bsalomon50c56a32016-06-30 12:05:32 -0700260 overrides.getOverrideColorIfSet(&fPaths[0].fColor);
ethannicholas6536ae52016-05-02 12:16:49 -0700261 }
262
bsalomon50c56a32016-06-30 12:05:32 -0700263 void computeWorstCasePointCount(const SkPath& path, int* subpaths, int* outLinePointCount,
264 int* outQuadPointCount) const {
ethannicholas6536ae52016-05-02 12:16:49 -0700265 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
bsalomon50c56a32016-06-30 12:05:32 -0700374 for (int i = 0; i < fPaths.count(); i++) {
375 const PathInfo& pathInfo = fPaths[i];
ethannicholas6536ae52016-05-02 12:16:49 -0700376
377 if (!this->createGeom(lines,
378 quads,
bsalomon50c56a32016-06-30 12:05:32 -0700379 pathInfo.fPath,
ethannicholas6536ae52016-05-02 12:16:49 -0700380 fViewMatrix,
bsalomon50c56a32016-06-30 12:05:32 -0700381 pathInfo.fColor,
ethannicholas6536ae52016-05-02 12:16:49 -0700382 fIsIndexed)) {
383 return;
384 }
385 }
386 int lineVertexOffset = (int) (lines.nextVertex - lines.vertices);
387 int lineIndexOffset = (int) (lines.nextIndex - lines.indices);
388 SkASSERT(lineVertexOffset <= fMaxLineVertices && lineIndexOffset <= fMaxLineIndices);
389 int quadVertexOffset = (int) (quads.nextVertex - quads.vertices);
390 int quadIndexOffset = (int) (quads.nextIndex - quads.indices);
391 SkASSERT(quadVertexOffset <= fMaxQuadVertices && quadIndexOffset <= fMaxQuadIndices);
392
393 if (lineVertexOffset) {
bungeman06ca8ec2016-06-09 08:01:03 -0700394 sk_sp<GrGeometryProcessor> lineGP;
ethannicholas6536ae52016-05-02 12:16:49 -0700395 {
396 using namespace GrDefaultGeoProcFactory;
bungeman06ca8ec2016-06-09 08:01:03 -0700397 lineGP = GrDefaultGeoProcFactory::Make(Color(Color::kAttribute_Type),
398 Coverage(255),
399 LocalCoords(LocalCoords::kUnused_Type),
400 fViewMatrix);
ethannicholas6536ae52016-05-02 12:16:49 -0700401 }
402 SkASSERT(lineVertexStride == lineGP->getVertexStride());
403
404 GrMesh lineMeshes;
405 if (fIsIndexed) {
bungeman06ca8ec2016-06-09 08:01:03 -0700406 lineMeshes.initIndexed(primitiveType, lineVertexBuffer, lineIndexBuffer,
407 firstLineVertex, firstLineIndex, lineVertexOffset,
ethannicholas6536ae52016-05-02 12:16:49 -0700408 lineIndexOffset);
409 } else {
bungeman06ca8ec2016-06-09 08:01:03 -0700410 lineMeshes.init(primitiveType, lineVertexBuffer, firstLineVertex,
ethannicholas6536ae52016-05-02 12:16:49 -0700411 lineVertexOffset);
412 }
bungeman06ca8ec2016-06-09 08:01:03 -0700413 target->draw(lineGP.get(), lineMeshes);
ethannicholas6536ae52016-05-02 12:16:49 -0700414 }
415
416 if (quadVertexOffset) {
417 SkAutoTUnref<const GrGeometryProcessor> quadGP(MSAAQuadProcessor::Create(fViewMatrix));
418 SkASSERT(quadVertexStride == quadGP->getVertexStride());
419
420 const GrBuffer* quadVertexBuffer;
421 int firstQuadVertex;
bungeman06ca8ec2016-06-09 08:01:03 -0700422 MSAAQuadVertices::Vertex* quadVertices = (MSAAQuadVertices::Vertex*)
ethannicholas6536ae52016-05-02 12:16:49 -0700423 target->makeVertexSpace(quadVertexStride, quadVertexOffset, &quadVertexBuffer,
424 &firstQuadVertex);
425 memcpy(quadVertices, quads.vertices, quadVertexStride * quadVertexOffset);
426 GrMesh quadMeshes;
427 if (fIsIndexed) {
428 const GrBuffer* quadIndexBuffer;
429 int firstQuadIndex;
bungeman06ca8ec2016-06-09 08:01:03 -0700430 uint16_t* quadIndices = (uint16_t*) target->makeIndexSpace(quadIndexOffset,
431 &quadIndexBuffer,
ethannicholas6536ae52016-05-02 12:16:49 -0700432 &firstQuadIndex);
433 memcpy(quadIndices, quads.indices, sizeof(uint16_t) * quadIndexOffset);
bungeman06ca8ec2016-06-09 08:01:03 -0700434 quadMeshes.initIndexed(kTriangles_GrPrimitiveType, quadVertexBuffer,
435 quadIndexBuffer, firstQuadVertex, firstQuadIndex,
ethannicholas6536ae52016-05-02 12:16:49 -0700436 quadVertexOffset, quadIndexOffset);
437 } else {
bungeman06ca8ec2016-06-09 08:01:03 -0700438 quadMeshes.init(kTriangles_GrPrimitiveType, quadVertexBuffer, firstQuadVertex,
ethannicholas6536ae52016-05-02 12:16:49 -0700439 quadVertexOffset);
440 }
441 target->draw(quadGP, quadMeshes);
442 }
443 }
444
ethannicholas6536ae52016-05-02 12:16:49 -0700445 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
446 MSAAPathBatch* that = t->cast<MSAAPathBatch>();
447 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
448 that->bounds(), caps)) {
449 return false;
450 }
451
452 if (!fViewMatrix.cheapEqualTo(that->fViewMatrix)) {
453 return false;
454 }
455
bungeman06ca8ec2016-06-09 08:01:03 -0700456 if ((fMaxLineIndices + that->fMaxLineIndices > SK_MaxU16) ||
ethannicholas6536ae52016-05-02 12:16:49 -0700457 (fMaxQuadIndices + that->fMaxQuadIndices > SK_MaxU16)) {
458 return false;
459 }
460
bsalomon50c56a32016-06-30 12:05:32 -0700461 fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700462 this->joinBounds(*that);
ethannicholas6536ae52016-05-02 12:16:49 -0700463 fIsIndexed = true;
464 fMaxLineVertices += that->fMaxLineVertices;
465 fMaxQuadVertices += that->fMaxQuadVertices;
466 fMaxLineIndices += that->fMaxLineIndices;
467 fMaxQuadIndices += that->fMaxQuadIndices;
468 return true;
469 }
470
471 bool createGeom(MSAALineVertices& lines,
472 MSAAQuadVertices& quads,
473 const SkPath& path,
ethannicholas6536ae52016-05-02 12:16:49 -0700474 const SkMatrix& m,
475 SkColor color,
476 bool isIndexed) const {
477 {
478 uint16_t subpathIdxStart = (uint16_t) (lines.nextVertex - lines.vertices);
479
480 SkPoint pts[4];
481
482 bool first = true;
483 SkPath::Iter iter(path, false);
484
485 bool done = false;
486 while (!done) {
487 SkPath::Verb verb = iter.next(pts);
488 switch (verb) {
489 case SkPath::kMove_Verb:
490 if (!first) {
491 uint16_t currIdx = (uint16_t) (lines.nextVertex - lines.vertices);
492 subpathIdxStart = currIdx;
493 }
494 SkASSERT(lines.nextVertex < lines.verticesEnd);
495 *(lines.nextVertex++) = { pts[0], color };
496 break;
497 case SkPath::kLine_Verb:
498 if (isIndexed) {
499 uint16_t prevIdx = (uint16_t) (lines.nextVertex - lines.vertices - 1);
500 if (prevIdx > subpathIdxStart) {
501 append_contour_edge_indices(subpathIdxStart, prevIdx, lines);
502 }
503 }
504 SkASSERT(lines.nextVertex < lines.verticesEnd);
505 *(lines.nextVertex++) = { pts[1], color };
506 break;
507 case SkPath::kConic_Verb: {
508 SkScalar weight = iter.conicWeight();
509 SkAutoConicToQuads converter;
bsalomon50c56a32016-06-30 12:05:32 -0700510 const SkPoint* quadPts = converter.computeQuads(pts, weight, kTolerance);
ethannicholas6536ae52016-05-02 12:16:49 -0700511 for (int i = 0; i < converter.countQuads(); ++i) {
bungeman06ca8ec2016-06-09 08:01:03 -0700512 add_quad(lines, quads, quadPts + i * 2, color, isIndexed,
ethannicholas6536ae52016-05-02 12:16:49 -0700513 subpathIdxStart);
514 }
515 break;
516 }
517 case SkPath::kQuad_Verb: {
518 add_quad(lines, quads, pts, color, isIndexed, subpathIdxStart);
bungeman06ca8ec2016-06-09 08:01:03 -0700519 break;
ethannicholas6536ae52016-05-02 12:16:49 -0700520 }
521 case SkPath::kCubic_Verb: {
522 SkSTArray<15, SkPoint, true> quadPts;
523 GrPathUtils::convertCubicToQuads(pts, kTolerance, &quadPts);
524 int count = quadPts.count();
525 for (int i = 0; i < count; i += 3) {
526 add_quad(lines, quads, &quadPts[i], color, isIndexed, subpathIdxStart);
527 }
528 break;
529 }
530 case SkPath::kClose_Verb:
531 break;
532 case SkPath::kDone_Verb:
533 done = true;
534 }
535 first = false;
536 }
537 }
538 return true;
539 }
540
bsalomon50c56a32016-06-30 12:05:32 -0700541 struct PathInfo {
542 GrColor fColor;
543 SkPath fPath;
544 };
545
546 SkSTArray<1, PathInfo, true> fPaths;
ethannicholas6536ae52016-05-02 12:16:49 -0700547
548 SkMatrix fViewMatrix;
549 int fMaxLineVertices;
550 int fMaxQuadVertices;
551 int fMaxLineIndices;
552 int fMaxQuadIndices;
553 bool fIsIndexed;
554
555 typedef GrVertexBatch INHERITED;
556};
557
robertphillips976f5f02016-06-03 10:59:20 -0700558bool GrMSAAPathRenderer::internalDrawPath(GrDrawContext* drawContext,
559 const GrPaint& paint,
560 const GrUserStencilSettings* userStencilSettings,
cdalton862cff32016-05-12 15:09:48 -0700561 const GrClip& clip,
ethannicholas6536ae52016-05-02 12:16:49 -0700562 const SkMatrix& viewMatrix,
bsalomon8acedde2016-06-24 10:42:16 -0700563 const GrShape& shape,
ethannicholas6536ae52016-05-02 12:16:49 -0700564 bool stencilOnly) {
bsalomon8acedde2016-06-24 10:42:16 -0700565 SkASSERT(shape.style().isSimpleFill());
566 SkPath path;
567 shape.asPath(&path);
568
cdalton93a379b2016-05-11 13:58:08 -0700569 int passCount = 0;
570 const GrUserStencilSettings* passes[3];
571 GrPipelineBuilder::DrawFace drawFace[3];
572 bool reverse = false;
573 bool lastPassIsBounds;
ethannicholas6536ae52016-05-02 12:16:49 -0700574
bsalomon8acedde2016-06-24 10:42:16 -0700575 if (single_pass_shape(shape)) {
ethannicholas6536ae52016-05-02 12:16:49 -0700576 passCount = 1;
577 if (stencilOnly) {
578 passes[0] = &gDirectToStencil;
579 } else {
580 passes[0] = nullptr;
581 }
582 drawFace[0] = GrPipelineBuilder::kBoth_DrawFace;
583 lastPassIsBounds = false;
584 } else {
585 switch (path.getFillType()) {
586 case SkPath::kInverseEvenOdd_FillType:
587 reverse = true;
588 // fallthrough
589 case SkPath::kEvenOdd_FillType:
590 passes[0] = &gEOStencilPass;
591 if (stencilOnly) {
592 passCount = 1;
593 lastPassIsBounds = false;
594 } else {
595 passCount = 2;
596 lastPassIsBounds = true;
597 if (reverse) {
598 passes[1] = &gInvEOColorPass;
599 } else {
600 passes[1] = &gEOColorPass;
601 }
602 }
603 drawFace[0] = drawFace[1] = GrPipelineBuilder::kBoth_DrawFace;
604 break;
605
606 case SkPath::kInverseWinding_FillType:
607 reverse = true;
608 // fallthrough
609 case SkPath::kWinding_FillType:
610 passes[0] = &gWindStencilSeparateWithWrap;
611 passCount = 2;
612 drawFace[0] = GrPipelineBuilder::kBoth_DrawFace;
613 if (stencilOnly) {
614 lastPassIsBounds = false;
615 --passCount;
616 } else {
617 lastPassIsBounds = true;
618 drawFace[passCount-1] = GrPipelineBuilder::kBoth_DrawFace;
619 if (reverse) {
620 passes[passCount-1] = &gInvWindColorPass;
621 } else {
622 passes[passCount-1] = &gWindColorPass;
623 }
624 }
625 break;
626 default:
627 SkDEBUGFAIL("Unknown path fFill!");
628 return false;
629 }
630 }
631
632 SkRect devBounds;
robertphillips976f5f02016-06-03 10:59:20 -0700633 GetPathDevBounds(path, drawContext->width(), drawContext->height(), viewMatrix, &devBounds);
ethannicholas6536ae52016-05-02 12:16:49 -0700634
635 for (int p = 0; p < passCount; ++p) {
ethannicholas6536ae52016-05-02 12:16:49 -0700636 if (lastPassIsBounds && (p == passCount-1)) {
ethannicholas6536ae52016-05-02 12:16:49 -0700637 SkRect bounds;
638 SkMatrix localMatrix = SkMatrix::I();
639 if (reverse) {
ethannicholas6536ae52016-05-02 12:16:49 -0700640 // draw over the dev bounds (which will be the whole dst surface for inv fill).
641 bounds = devBounds;
642 SkMatrix vmi;
643 // mapRect through persp matrix may not be correct
644 if (!viewMatrix.hasPerspective() && viewMatrix.invert(&vmi)) {
645 vmi.mapRect(&bounds);
646 } else {
647 if (!viewMatrix.invert(&localMatrix)) {
648 return false;
649 }
650 }
651 } else {
652 bounds = path.getBounds();
653 }
654 const SkMatrix& viewM = (reverse && viewMatrix.hasPerspective()) ? SkMatrix::I() :
655 viewMatrix;
656 SkAutoTUnref<GrDrawBatch> batch(
robertphillips3950f0d2016-07-07 07:33:13 -0700657 GrRectBatchFactory::CreateNonAAFill(paint.getColor(), viewM, bounds, nullptr,
ethannicholas6536ae52016-05-02 12:16:49 -0700658 &localMatrix));
robertphillips976f5f02016-06-03 10:59:20 -0700659
csmartdaltonecbc12b2016-06-08 10:08:43 -0700660 GrPipelineBuilder pipelineBuilder(paint, drawContext->mustUseHWAA(paint));
robertphillips976f5f02016-06-03 10:59:20 -0700661 pipelineBuilder.setDrawFace(drawFace[p]);
662 if (passes[p]) {
663 pipelineBuilder.setUserStencil(passes[p]);
664 } else {
665 pipelineBuilder.setUserStencil(userStencilSettings);
ethannicholas6536ae52016-05-02 12:16:49 -0700666 }
667
robertphillips976f5f02016-06-03 10:59:20 -0700668 drawContext->drawBatch(pipelineBuilder, clip, batch);
669 } else {
bsalomon88cf17d2016-07-08 06:40:56 -0700670 SkAutoTUnref<MSAAPathBatch> batch(new MSAAPathBatch(paint.getColor(), path,
671 viewMatrix));
robertphillips976f5f02016-06-03 10:59:20 -0700672 if (!batch->isValid()) {
ethannicholas6536ae52016-05-02 12:16:49 -0700673 return false;
674 }
robertphillips976f5f02016-06-03 10:59:20 -0700675
csmartdaltonecbc12b2016-06-08 10:08:43 -0700676 GrPipelineBuilder pipelineBuilder(paint, drawContext->mustUseHWAA(paint));
robertphillips976f5f02016-06-03 10:59:20 -0700677 pipelineBuilder.setDrawFace(drawFace[p]);
678 if (passes[p]) {
679 pipelineBuilder.setUserStencil(passes[p]);
680 } else {
681 pipelineBuilder.setUserStencil(userStencilSettings);
682 }
683 if (passCount > 1) {
684 pipelineBuilder.setDisableColorXPFactory();
685 }
686
687 drawContext->drawBatch(pipelineBuilder, clip, batch);
ethannicholas6536ae52016-05-02 12:16:49 -0700688 }
689 }
690 return true;
691}
692
693bool GrMSAAPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
bsalomonee432412016-06-27 07:18:18 -0700694 // This path renderer only fills and relies on MSAA for antialiasing. Stroked shapes are
695 // handled by passing on the original shape and letting the caller compute the stroked shape
696 // which will have a fill style.
697 return args.fShape->style().isSimpleFill() && !args.fAntiAlias;
ethannicholas6536ae52016-05-02 12:16:49 -0700698}
699
700bool GrMSAAPathRenderer::onDrawPath(const DrawPathArgs& args) {
robertphillips976f5f02016-06-03 10:59:20 -0700701 GR_AUDIT_TRAIL_AUTO_FRAME(args.fDrawContext->auditTrail(),
702 "GrMSAAPathRenderer::onDrawPath");
bsalomon8acedde2016-06-24 10:42:16 -0700703 SkTLazy<GrShape> tmpShape;
704 const GrShape* shape = args.fShape;
705 if (shape->style().applies()) {
bsalomon6663acf2016-05-10 09:14:17 -0700706 SkScalar styleScale = GrStyle::MatrixToScaleFactor(*args.fViewMatrix);
bsalomon8acedde2016-06-24 10:42:16 -0700707 tmpShape.init(args.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale));
708 shape = tmpShape.get();
ethannicholas6536ae52016-05-02 12:16:49 -0700709 }
robertphillips976f5f02016-06-03 10:59:20 -0700710 return this->internalDrawPath(args.fDrawContext,
711 *args.fPaint,
712 args.fUserStencilSettings,
cdalton862cff32016-05-12 15:09:48 -0700713 *args.fClip,
ethannicholas6536ae52016-05-02 12:16:49 -0700714 *args.fViewMatrix,
bsalomon8acedde2016-06-24 10:42:16 -0700715 *shape,
ethannicholas6536ae52016-05-02 12:16:49 -0700716 false);
717}
718
719void GrMSAAPathRenderer::onStencilPath(const StencilPathArgs& args) {
robertphillips976f5f02016-06-03 10:59:20 -0700720 GR_AUDIT_TRAIL_AUTO_FRAME(args.fDrawContext->auditTrail(),
721 "GrMSAAPathRenderer::onStencilPath");
bsalomon8acedde2016-06-24 10:42:16 -0700722 SkASSERT(args.fShape->style().isSimpleFill());
723 SkASSERT(!args.fShape->mayBeInverseFilledAfterStyling());
robertphillips976f5f02016-06-03 10:59:20 -0700724
725 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700726 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700727 paint.setAntiAlias(args.fIsAA);
728
bsalomon8acedde2016-06-24 10:42:16 -0700729 this->internalDrawPath(args.fDrawContext, paint, &GrUserStencilSettings::kUnused, *args.fClip,
robertphillips3950f0d2016-07-07 07:33:13 -0700730 *args.fViewMatrix, *args.fShape, true);
ethannicholas6536ae52016-05-02 12:16:49 -0700731}
732
733///////////////////////////////////////////////////////////////////////////////////////////////////