blob: 930f7f926aca0c78c14555cf28725731bee97c92 [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"
robertphillips976f5f02016-06-03 10:59:20 -070011#include "GrClip.h"
ethannicholas6536ae52016-05-02 12:16:49 -070012#include "GrDefaultGeoProcFactory.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070013#include "GrFixedClip.h"
Brian Salomondad29232016-12-01 16:40:24 -050014#include "GrMesh.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050015#include "GrOpFlushState.h"
ethannicholas6536ae52016-05-02 12:16:49 -070016#include "GrPathStencilSettings.h"
17#include "GrPathUtils.h"
bsalomonbb243832016-07-22 07:10:19 -070018#include "GrPipelineBuilder.h"
ethannicholas6536ae52016-05-02 12:16:49 -070019#include "SkGeometry.h"
20#include "SkTraceEvent.h"
Brian Salomondad29232016-12-01 16:40:24 -050021#include "batches/GrMeshDrawOp.h"
22#include "batches/GrRectBatchFactory.h"
23#include "gl/GrGLVaryingHandler.h"
ethannicholas6536ae52016-05-02 12:16:49 -070024#include "glsl/GrGLSLFragmentShaderBuilder.h"
Brian Salomondad29232016-12-01 16:40:24 -050025#include "glsl/GrGLSLGeometryProcessor.h"
ethannicholas6536ae52016-05-02 12:16:49 -070026#include "glsl/GrGLSLProgramDataManager.h"
27#include "glsl/GrGLSLUtil.h"
Brian Salomondad29232016-12-01 16:40:24 -050028#include "glsl/GrGLSLVertexShaderBuilder.h"
ethannicholas6536ae52016-05-02 12:16:49 -070029
30static const float kTolerance = 0.5f;
31
32////////////////////////////////////////////////////////////////////////////////
33// Helpers for drawPath
34
bsalomon8acedde2016-06-24 10:42:16 -070035static inline bool single_pass_shape(const GrShape& shape) {
36 if (!shape.inverseFilled()) {
37 return shape.knownToBeConvex();
ethannicholas6536ae52016-05-02 12:16:49 -070038 }
39 return false;
40}
41
bsalomon8acedde2016-06-24 10:42:16 -070042GrPathRenderer::StencilSupport GrMSAAPathRenderer::onGetStencilSupport(const GrShape& shape) const {
43 if (single_pass_shape(shape)) {
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; }
ethannicholas6536ae52016-05-02 12:16:49 -0700126
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,
bsalomona624bf32016-09-20 09:12:47 -0700151 qp.inPosition()->fName, SkMatrix::I(),
152 args.fFPCoordTransformHandler);
ethannicholas6536ae52016-05-02 12:16:49 -0700153
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,
Brian Salomon94efbf52016-11-29 13:43:05 -0500161 const GrShaderCaps&,
ethannicholas6536ae52016-05-02 12:16:49 -0700162 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
bsalomona624bf32016-09-20 09:12:47 -0700170 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& gp,
171 FPCoordTransformIter&& transformIter) override {
ethannicholas6536ae52016-05-02 12:16:49 -0700172 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 }
bsalomona624bf32016-09-20 09:12:47 -0700178 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
ethannicholas6536ae52016-05-02 12:16:49 -0700179 }
180
ethannicholas6536ae52016-05-02 12:16:49 -0700181 private:
182 typedef GrGLSLGeometryProcessor INHERITED;
183
184 UniformHandle fViewMatrixUniform;
185 };
186
Brian Salomon94efbf52016-11-29 13:43:05 -0500187 virtual void getGLSLProcessorKey(const GrShaderCaps& caps,
ethannicholas6536ae52016-05-02 12:16:49 -0700188 GrProcessorKeyBuilder* b) const override {
189 GLSLProcessor::GenKey(*this, caps, b);
190 }
191
Brian Salomon94efbf52016-11-29 13:43:05 -0500192 virtual GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override {
ethannicholas6536ae52016-05-02 12:16:49 -0700193 return new GLSLProcessor(*this);
194 }
195
196private:
197 MSAAQuadProcessor(const SkMatrix& viewMatrix)
198 : fViewMatrix(viewMatrix) {
199 this->initClassID<MSAAQuadProcessor>();
bsalomon6cb807b2016-08-17 11:33:39 -0700200 fInPosition = &this->addVertexAttrib("inPosition", kVec2f_GrVertexAttribType,
201 kHigh_GrSLPrecision);
202 fInUV = &this->addVertexAttrib("inUV", kVec2f_GrVertexAttribType, kHigh_GrSLPrecision);
203 fInColor = &this->addVertexAttrib("inColor", kVec4ub_GrVertexAttribType);
ethannicholas6536ae52016-05-02 12:16:49 -0700204 this->setSampleShading(1.0f);
205 }
206
207 const Attribute* fInPosition;
208 const Attribute* fInUV;
209 const Attribute* fInColor;
210 SkMatrix fViewMatrix;
bungeman06ca8ec2016-06-09 08:01:03 -0700211
ethannicholas6536ae52016-05-02 12:16:49 -0700212 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
213
214 typedef GrGeometryProcessor INHERITED;
215};
216
Brian Salomondad29232016-12-01 16:40:24 -0500217class MSAAPathBatch : public GrMeshDrawOp {
ethannicholas6536ae52016-05-02 12:16:49 -0700218public:
Brian Salomon25a88092016-12-01 09:36:50 -0500219 DEFINE_OP_CLASS_ID
ethannicholas6536ae52016-05-02 12:16:49 -0700220
bsalomon8e9e45a2016-07-08 09:14:07 -0700221 MSAAPathBatch(GrColor color, const SkPath& path, const SkMatrix& viewMatrix,
222 const SkRect& devBounds)
bsalomon50c56a32016-06-30 12:05:32 -0700223 : INHERITED(ClassID())
224 , fViewMatrix(viewMatrix) {
225 fPaths.emplace_back(PathInfo{color, path});
bsalomon8e9e45a2016-07-08 09:14:07 -0700226 this->setBounds(devBounds, HasAABloat::kNo, IsZeroArea::kNo);
bsalomon50c56a32016-06-30 12:05:32 -0700227 int contourCount;
228 this->computeWorstCasePointCount(path, &contourCount, &fMaxLineVertices, &fMaxQuadVertices);
229 fMaxLineIndices = fMaxLineVertices * 3;
230 fMaxQuadIndices = fMaxQuadVertices * 3;
231 fIsIndexed = contourCount > 1;
ethannicholas6536ae52016-05-02 12:16:49 -0700232 }
233
234 const char* name() const override { return "MSAAPathBatch"; }
235
Brian Salomon7c3e7182016-12-01 09:35:30 -0500236 SkString dumpInfo() const override {
237 SkString string;
238 string.appendf("Indexed: %d\n", fIsIndexed);
239 for (const auto& path : fPaths) {
240 string.appendf("Color: 0x%08x\n", path.fColor);
241 }
242 string.append(DumpPipelineInfo(*this->pipeline()));
243 string.append(INHERITED::dumpInfo());
244 return string;
245 }
246
bungeman06ca8ec2016-06-09 08:01:03 -0700247 void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholas6536ae52016-05-02 12:16:49 -0700248 GrInitInvariantOutput* coverage,
249 GrBatchToXPOverrides* overrides) const override {
bsalomon50c56a32016-06-30 12:05:32 -0700250 // When this is called on a batch, there is only one path
251 color->setKnownFourComponents(fPaths[0].fColor);
ethannicholas6536ae52016-05-02 12:16:49 -0700252 coverage->setKnownSingleComponent(0xff);
253 }
254
255 bool isValid() const {
256 return !fIsIndexed || fMaxLineIndices <= SK_MaxU16;
257 }
258
259private:
260 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
261 // Handle any color overrides
262 if (!overrides.readsColor()) {
bsalomon50c56a32016-06-30 12:05:32 -0700263 fPaths[0].fColor = GrColor_ILLEGAL;
ethannicholas6536ae52016-05-02 12:16:49 -0700264 }
bsalomon50c56a32016-06-30 12:05:32 -0700265 overrides.getOverrideColorIfSet(&fPaths[0].fColor);
ethannicholas6536ae52016-05-02 12:16:49 -0700266 }
267
bsalomon50c56a32016-06-30 12:05:32 -0700268 void computeWorstCasePointCount(const SkPath& path, int* subpaths, int* outLinePointCount,
269 int* outQuadPointCount) const {
ethannicholas6536ae52016-05-02 12:16:49 -0700270 int linePointCount = 0;
271 int quadPointCount = 0;
272 *subpaths = 1;
273
274 bool first = true;
275
bsalomon8eb43e52016-09-21 07:47:34 -0700276 SkPath::Iter iter(path, true);
ethannicholas6536ae52016-05-02 12:16:49 -0700277 SkPath::Verb verb;
278
279 SkPoint pts[4];
280 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
281 switch (verb) {
282 case SkPath::kLine_Verb:
283 linePointCount += 1;
284 break;
285 case SkPath::kConic_Verb: {
286 SkScalar weight = iter.conicWeight();
287 SkAutoConicToQuads converter;
288 converter.computeQuads(pts, weight, kTolerance);
289 int quadPts = converter.countQuads();
290 linePointCount += quadPts;
291 quadPointCount += 3 * quadPts;
292 }
293 case SkPath::kQuad_Verb:
294 linePointCount += 1;
295 quadPointCount += 3;
296 break;
297 case SkPath::kCubic_Verb: {
298 SkSTArray<15, SkPoint, true> quadPts;
299 GrPathUtils::convertCubicToQuads(pts, kTolerance, &quadPts);
300 int count = quadPts.count();
301 linePointCount += count / 3;
302 quadPointCount += count;
303 break;
304 }
305 case SkPath::kMove_Verb:
306 linePointCount += 1;
307 if (!first) {
308 ++(*subpaths);
309 }
310 break;
311 default:
312 break;
313 }
314 first = false;
315 }
316 *outLinePointCount = linePointCount;
317 *outQuadPointCount = quadPointCount;
318 }
319
320 void onPrepareDraws(Target* target) const override {
321 SkASSERT(this->isValid());
322 if (fMaxLineVertices == 0) {
323 SkASSERT(fMaxQuadVertices == 0);
324 return;
325 }
326
bungeman06ca8ec2016-06-09 08:01:03 -0700327 GrPrimitiveType primitiveType = fIsIndexed ? kTriangles_GrPrimitiveType
ethannicholas6536ae52016-05-02 12:16:49 -0700328 : kTriangleFan_GrPrimitiveType;
329
330 // allocate vertex / index buffers
331 const GrBuffer* lineVertexBuffer;
332 int firstLineVertex;
333 MSAALineVertices lines;
334 size_t lineVertexStride = sizeof(MSAALineVertices::Vertex);
bungeman06ca8ec2016-06-09 08:01:03 -0700335 lines.vertices = (MSAALineVertices::Vertex*) target->makeVertexSpace(lineVertexStride,
ethannicholas6536ae52016-05-02 12:16:49 -0700336 fMaxLineVertices,
bungeman06ca8ec2016-06-09 08:01:03 -0700337 &lineVertexBuffer,
ethannicholas6536ae52016-05-02 12:16:49 -0700338 &firstLineVertex);
339 if (!lines.vertices) {
340 SkDebugf("Could not allocate vertices\n");
341 return;
342 }
343 lines.nextVertex = lines.vertices;
344 SkDEBUGCODE(lines.verticesEnd = lines.vertices + fMaxLineVertices;)
345
346 MSAAQuadVertices quads;
347 size_t quadVertexStride = sizeof(MSAAQuadVertices::Vertex);
348 SkAutoFree quadVertexPtr(sk_malloc_throw(fMaxQuadVertices * quadVertexStride));
349 quads.vertices = (MSAAQuadVertices::Vertex*) quadVertexPtr.get();
350 quads.nextVertex = quads.vertices;
351 SkDEBUGCODE(quads.verticesEnd = quads.vertices + fMaxQuadVertices;)
352
353 const GrBuffer* lineIndexBuffer = nullptr;
354 int firstLineIndex;
355 if (fIsIndexed) {
bungeman06ca8ec2016-06-09 08:01:03 -0700356 lines.indices = target->makeIndexSpace(fMaxLineIndices, &lineIndexBuffer,
ethannicholas6536ae52016-05-02 12:16:49 -0700357 &firstLineIndex);
358 if (!lines.indices) {
359 SkDebugf("Could not allocate indices\n");
360 return;
361 }
362 lines.nextIndex = lines.indices;
363 } else {
364 lines.indices = nullptr;
365 lines.nextIndex = nullptr;
366 }
367
368 SkAutoFree quadIndexPtr;
369 if (fIsIndexed) {
370 quads.indices = (uint16_t*) sk_malloc_throw(fMaxQuadIndices * sizeof(uint16_t));
371 quadIndexPtr.set(quads.indices);
372 quads.nextIndex = quads.indices;
373 } else {
374 quads.indices = nullptr;
375 quads.nextIndex = nullptr;
376 }
377
378 // fill buffers
bsalomon50c56a32016-06-30 12:05:32 -0700379 for (int i = 0; i < fPaths.count(); i++) {
380 const PathInfo& pathInfo = fPaths[i];
ethannicholas6536ae52016-05-02 12:16:49 -0700381
382 if (!this->createGeom(lines,
383 quads,
bsalomon50c56a32016-06-30 12:05:32 -0700384 pathInfo.fPath,
ethannicholas6536ae52016-05-02 12:16:49 -0700385 fViewMatrix,
bsalomon50c56a32016-06-30 12:05:32 -0700386 pathInfo.fColor,
ethannicholas6536ae52016-05-02 12:16:49 -0700387 fIsIndexed)) {
388 return;
389 }
390 }
391 int lineVertexOffset = (int) (lines.nextVertex - lines.vertices);
392 int lineIndexOffset = (int) (lines.nextIndex - lines.indices);
393 SkASSERT(lineVertexOffset <= fMaxLineVertices && lineIndexOffset <= fMaxLineIndices);
394 int quadVertexOffset = (int) (quads.nextVertex - quads.vertices);
395 int quadIndexOffset = (int) (quads.nextIndex - quads.indices);
396 SkASSERT(quadVertexOffset <= fMaxQuadVertices && quadIndexOffset <= fMaxQuadIndices);
397
398 if (lineVertexOffset) {
bungeman06ca8ec2016-06-09 08:01:03 -0700399 sk_sp<GrGeometryProcessor> lineGP;
ethannicholas6536ae52016-05-02 12:16:49 -0700400 {
401 using namespace GrDefaultGeoProcFactory;
bungeman06ca8ec2016-06-09 08:01:03 -0700402 lineGP = GrDefaultGeoProcFactory::Make(Color(Color::kAttribute_Type),
403 Coverage(255),
404 LocalCoords(LocalCoords::kUnused_Type),
405 fViewMatrix);
ethannicholas6536ae52016-05-02 12:16:49 -0700406 }
407 SkASSERT(lineVertexStride == lineGP->getVertexStride());
408
409 GrMesh lineMeshes;
410 if (fIsIndexed) {
bungeman06ca8ec2016-06-09 08:01:03 -0700411 lineMeshes.initIndexed(primitiveType, lineVertexBuffer, lineIndexBuffer,
412 firstLineVertex, firstLineIndex, lineVertexOffset,
ethannicholas6536ae52016-05-02 12:16:49 -0700413 lineIndexOffset);
414 } else {
bungeman06ca8ec2016-06-09 08:01:03 -0700415 lineMeshes.init(primitiveType, lineVertexBuffer, firstLineVertex,
ethannicholas6536ae52016-05-02 12:16:49 -0700416 lineVertexOffset);
417 }
bungeman06ca8ec2016-06-09 08:01:03 -0700418 target->draw(lineGP.get(), lineMeshes);
ethannicholas6536ae52016-05-02 12:16:49 -0700419 }
420
421 if (quadVertexOffset) {
Hal Canary144caf52016-11-07 17:57:18 -0500422 sk_sp<const GrGeometryProcessor> quadGP(MSAAQuadProcessor::Create(fViewMatrix));
ethannicholas6536ae52016-05-02 12:16:49 -0700423 SkASSERT(quadVertexStride == quadGP->getVertexStride());
424
425 const GrBuffer* quadVertexBuffer;
426 int firstQuadVertex;
bungeman06ca8ec2016-06-09 08:01:03 -0700427 MSAAQuadVertices::Vertex* quadVertices = (MSAAQuadVertices::Vertex*)
ethannicholas6536ae52016-05-02 12:16:49 -0700428 target->makeVertexSpace(quadVertexStride, quadVertexOffset, &quadVertexBuffer,
429 &firstQuadVertex);
430 memcpy(quadVertices, quads.vertices, quadVertexStride * quadVertexOffset);
431 GrMesh quadMeshes;
432 if (fIsIndexed) {
433 const GrBuffer* quadIndexBuffer;
434 int firstQuadIndex;
bungeman06ca8ec2016-06-09 08:01:03 -0700435 uint16_t* quadIndices = (uint16_t*) target->makeIndexSpace(quadIndexOffset,
436 &quadIndexBuffer,
ethannicholas6536ae52016-05-02 12:16:49 -0700437 &firstQuadIndex);
438 memcpy(quadIndices, quads.indices, sizeof(uint16_t) * quadIndexOffset);
bungeman06ca8ec2016-06-09 08:01:03 -0700439 quadMeshes.initIndexed(kTriangles_GrPrimitiveType, quadVertexBuffer,
440 quadIndexBuffer, firstQuadVertex, firstQuadIndex,
ethannicholas6536ae52016-05-02 12:16:49 -0700441 quadVertexOffset, quadIndexOffset);
442 } else {
bungeman06ca8ec2016-06-09 08:01:03 -0700443 quadMeshes.init(kTriangles_GrPrimitiveType, quadVertexBuffer, firstQuadVertex,
ethannicholas6536ae52016-05-02 12:16:49 -0700444 quadVertexOffset);
445 }
Hal Canary144caf52016-11-07 17:57:18 -0500446 target->draw(quadGP.get(), quadMeshes);
ethannicholas6536ae52016-05-02 12:16:49 -0700447 }
448 }
449
Brian Salomon25a88092016-12-01 09:36:50 -0500450 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
ethannicholas6536ae52016-05-02 12:16:49 -0700451 MSAAPathBatch* that = t->cast<MSAAPathBatch>();
452 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
453 that->bounds(), caps)) {
454 return false;
455 }
456
457 if (!fViewMatrix.cheapEqualTo(that->fViewMatrix)) {
458 return false;
459 }
460
bungeman06ca8ec2016-06-09 08:01:03 -0700461 if ((fMaxLineIndices + that->fMaxLineIndices > SK_MaxU16) ||
ethannicholas6536ae52016-05-02 12:16:49 -0700462 (fMaxQuadIndices + that->fMaxQuadIndices > SK_MaxU16)) {
463 return false;
464 }
465
bsalomon50c56a32016-06-30 12:05:32 -0700466 fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700467 this->joinBounds(*that);
ethannicholas6536ae52016-05-02 12:16:49 -0700468 fIsIndexed = true;
469 fMaxLineVertices += that->fMaxLineVertices;
470 fMaxQuadVertices += that->fMaxQuadVertices;
471 fMaxLineIndices += that->fMaxLineIndices;
472 fMaxQuadIndices += that->fMaxQuadIndices;
473 return true;
474 }
475
476 bool createGeom(MSAALineVertices& lines,
477 MSAAQuadVertices& quads,
478 const SkPath& path,
ethannicholas6536ae52016-05-02 12:16:49 -0700479 const SkMatrix& m,
480 SkColor color,
481 bool isIndexed) const {
482 {
483 uint16_t subpathIdxStart = (uint16_t) (lines.nextVertex - lines.vertices);
484
485 SkPoint pts[4];
486
487 bool first = true;
bsalomon8eb43e52016-09-21 07:47:34 -0700488 SkPath::Iter iter(path, true);
ethannicholas6536ae52016-05-02 12:16:49 -0700489
490 bool done = false;
491 while (!done) {
492 SkPath::Verb verb = iter.next(pts);
493 switch (verb) {
494 case SkPath::kMove_Verb:
495 if (!first) {
496 uint16_t currIdx = (uint16_t) (lines.nextVertex - lines.vertices);
497 subpathIdxStart = currIdx;
498 }
499 SkASSERT(lines.nextVertex < lines.verticesEnd);
500 *(lines.nextVertex++) = { pts[0], color };
501 break;
502 case SkPath::kLine_Verb:
503 if (isIndexed) {
504 uint16_t prevIdx = (uint16_t) (lines.nextVertex - lines.vertices - 1);
505 if (prevIdx > subpathIdxStart) {
506 append_contour_edge_indices(subpathIdxStart, prevIdx, lines);
507 }
508 }
509 SkASSERT(lines.nextVertex < lines.verticesEnd);
510 *(lines.nextVertex++) = { pts[1], color };
511 break;
512 case SkPath::kConic_Verb: {
513 SkScalar weight = iter.conicWeight();
514 SkAutoConicToQuads converter;
bsalomon50c56a32016-06-30 12:05:32 -0700515 const SkPoint* quadPts = converter.computeQuads(pts, weight, kTolerance);
ethannicholas6536ae52016-05-02 12:16:49 -0700516 for (int i = 0; i < converter.countQuads(); ++i) {
bungeman06ca8ec2016-06-09 08:01:03 -0700517 add_quad(lines, quads, quadPts + i * 2, color, isIndexed,
ethannicholas6536ae52016-05-02 12:16:49 -0700518 subpathIdxStart);
519 }
520 break;
521 }
522 case SkPath::kQuad_Verb: {
523 add_quad(lines, quads, pts, color, isIndexed, subpathIdxStart);
bungeman06ca8ec2016-06-09 08:01:03 -0700524 break;
ethannicholas6536ae52016-05-02 12:16:49 -0700525 }
526 case SkPath::kCubic_Verb: {
527 SkSTArray<15, SkPoint, true> quadPts;
528 GrPathUtils::convertCubicToQuads(pts, kTolerance, &quadPts);
529 int count = quadPts.count();
530 for (int i = 0; i < count; i += 3) {
531 add_quad(lines, quads, &quadPts[i], color, isIndexed, subpathIdxStart);
532 }
533 break;
534 }
535 case SkPath::kClose_Verb:
536 break;
537 case SkPath::kDone_Verb:
538 done = true;
539 }
540 first = false;
541 }
542 }
543 return true;
544 }
545
bsalomon50c56a32016-06-30 12:05:32 -0700546 struct PathInfo {
547 GrColor fColor;
548 SkPath fPath;
549 };
550
551 SkSTArray<1, PathInfo, true> fPaths;
ethannicholas6536ae52016-05-02 12:16:49 -0700552
553 SkMatrix fViewMatrix;
554 int fMaxLineVertices;
555 int fMaxQuadVertices;
556 int fMaxLineIndices;
557 int fMaxQuadIndices;
558 bool fIsIndexed;
559
Brian Salomondad29232016-12-01 16:40:24 -0500560 typedef GrMeshDrawOp INHERITED;
ethannicholas6536ae52016-05-02 12:16:49 -0700561};
562
Brian Osman11052242016-10-27 14:47:55 -0400563bool GrMSAAPathRenderer::internalDrawPath(GrRenderTargetContext* renderTargetContext,
robertphillips976f5f02016-06-03 10:59:20 -0700564 const GrPaint& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500565 GrAAType aaType,
robertphillipsd2b6d642016-07-21 08:55:08 -0700566 const GrUserStencilSettings& userStencilSettings,
cdalton862cff32016-05-12 15:09:48 -0700567 const GrClip& clip,
ethannicholas6536ae52016-05-02 12:16:49 -0700568 const SkMatrix& viewMatrix,
bsalomon8acedde2016-06-24 10:42:16 -0700569 const GrShape& shape,
ethannicholas6536ae52016-05-02 12:16:49 -0700570 bool stencilOnly) {
bsalomon8acedde2016-06-24 10:42:16 -0700571 SkASSERT(shape.style().isSimpleFill());
572 SkPath path;
573 shape.asPath(&path);
574
robertphillips8e375302016-07-11 10:43:58 -0700575 static const int kMaxNumPasses = 2;
576
cdalton93a379b2016-05-11 13:58:08 -0700577 int passCount = 0;
robertphillips8e375302016-07-11 10:43:58 -0700578 const GrUserStencilSettings* passes[kMaxNumPasses];
cdalton93a379b2016-05-11 13:58:08 -0700579 bool reverse = false;
580 bool lastPassIsBounds;
ethannicholas6536ae52016-05-02 12:16:49 -0700581
bsalomon8acedde2016-06-24 10:42:16 -0700582 if (single_pass_shape(shape)) {
ethannicholas6536ae52016-05-02 12:16:49 -0700583 passCount = 1;
584 if (stencilOnly) {
585 passes[0] = &gDirectToStencil;
586 } else {
robertphillipsd2b6d642016-07-21 08:55:08 -0700587 passes[0] = &userStencilSettings;
ethannicholas6536ae52016-05-02 12:16:49 -0700588 }
ethannicholas6536ae52016-05-02 12:16:49 -0700589 lastPassIsBounds = false;
590 } else {
591 switch (path.getFillType()) {
592 case SkPath::kInverseEvenOdd_FillType:
593 reverse = true;
594 // fallthrough
595 case SkPath::kEvenOdd_FillType:
596 passes[0] = &gEOStencilPass;
597 if (stencilOnly) {
598 passCount = 1;
599 lastPassIsBounds = false;
600 } else {
601 passCount = 2;
602 lastPassIsBounds = true;
603 if (reverse) {
604 passes[1] = &gInvEOColorPass;
605 } else {
606 passes[1] = &gEOColorPass;
607 }
608 }
ethannicholas6536ae52016-05-02 12:16:49 -0700609 break;
610
611 case SkPath::kInverseWinding_FillType:
612 reverse = true;
613 // fallthrough
614 case SkPath::kWinding_FillType:
615 passes[0] = &gWindStencilSeparateWithWrap;
616 passCount = 2;
ethannicholas6536ae52016-05-02 12:16:49 -0700617 if (stencilOnly) {
618 lastPassIsBounds = false;
robertphillips8e375302016-07-11 10:43:58 -0700619 passCount = 1;
ethannicholas6536ae52016-05-02 12:16:49 -0700620 } else {
621 lastPassIsBounds = true;
ethannicholas6536ae52016-05-02 12:16:49 -0700622 if (reverse) {
robertphillips8e375302016-07-11 10:43:58 -0700623 passes[1] = &gInvWindColorPass;
ethannicholas6536ae52016-05-02 12:16:49 -0700624 } else {
robertphillips8e375302016-07-11 10:43:58 -0700625 passes[1] = &gWindColorPass;
ethannicholas6536ae52016-05-02 12:16:49 -0700626 }
627 }
628 break;
629 default:
630 SkDEBUGFAIL("Unknown path fFill!");
631 return false;
632 }
633 }
634
635 SkRect devBounds;
Brian Osman11052242016-10-27 14:47:55 -0400636 GetPathDevBounds(path, renderTargetContext->width(), renderTargetContext->height(), viewMatrix,
637 &devBounds);
ethannicholas6536ae52016-05-02 12:16:49 -0700638
robertphillips8e375302016-07-11 10:43:58 -0700639 SkASSERT(passCount <= kMaxNumPasses);
640
ethannicholas6536ae52016-05-02 12:16:49 -0700641 for (int p = 0; p < passCount; ++p) {
ethannicholas6536ae52016-05-02 12:16:49 -0700642 if (lastPassIsBounds && (p == passCount-1)) {
ethannicholas6536ae52016-05-02 12:16:49 -0700643 SkRect bounds;
644 SkMatrix localMatrix = SkMatrix::I();
645 if (reverse) {
ethannicholas6536ae52016-05-02 12:16:49 -0700646 // draw over the dev bounds (which will be the whole dst surface for inv fill).
647 bounds = devBounds;
648 SkMatrix vmi;
649 // mapRect through persp matrix may not be correct
650 if (!viewMatrix.hasPerspective() && viewMatrix.invert(&vmi)) {
651 vmi.mapRect(&bounds);
652 } else {
653 if (!viewMatrix.invert(&localMatrix)) {
654 return false;
655 }
656 }
657 } else {
658 bounds = path.getBounds();
659 }
660 const SkMatrix& viewM = (reverse && viewMatrix.hasPerspective()) ? SkMatrix::I() :
661 viewMatrix;
Brian Salomon9afd3712016-12-01 10:59:09 -0500662 sk_sp<GrDrawOp> batch(
robertphillips3950f0d2016-07-07 07:33:13 -0700663 GrRectBatchFactory::CreateNonAAFill(paint.getColor(), viewM, bounds, nullptr,
ethannicholas6536ae52016-05-02 12:16:49 -0700664 &localMatrix));
robertphillips976f5f02016-06-03 10:59:20 -0700665
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500666 GrPipelineBuilder pipelineBuilder(paint, aaType);
bsalomonbb243832016-07-22 07:10:19 -0700667 pipelineBuilder.setUserStencil(passes[p]);
668
Brian Salomon42521e82016-12-07 16:44:58 -0500669 renderTargetContext->addDrawOp(pipelineBuilder, clip, batch.get());
robertphillips976f5f02016-06-03 10:59:20 -0700670 } else {
Hal Canary144caf52016-11-07 17:57:18 -0500671 sk_sp<MSAAPathBatch> batch(new MSAAPathBatch(paint.getColor(), path,
672 viewMatrix, devBounds));
robertphillips976f5f02016-06-03 10:59:20 -0700673 if (!batch->isValid()) {
ethannicholas6536ae52016-05-02 12:16:49 -0700674 return false;
675 }
robertphillips976f5f02016-06-03 10:59:20 -0700676
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500677 GrPipelineBuilder pipelineBuilder(paint, aaType);
bsalomonbb243832016-07-22 07:10:19 -0700678 pipelineBuilder.setUserStencil(passes[p]);
robertphillips976f5f02016-06-03 10:59:20 -0700679 if (passCount > 1) {
bsalomonbb243832016-07-22 07:10:19 -0700680 pipelineBuilder.setDisableColorXPFactory();
robertphillips976f5f02016-06-03 10:59:20 -0700681 }
Brian Salomon42521e82016-12-07 16:44:58 -0500682 renderTargetContext->addDrawOp(pipelineBuilder, clip, batch.get());
ethannicholas6536ae52016-05-02 12:16:49 -0700683 }
684 }
685 return true;
686}
687
688bool GrMSAAPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
bsalomonee432412016-06-27 07:18:18 -0700689 // This path renderer only fills and relies on MSAA for antialiasing. Stroked shapes are
690 // handled by passing on the original shape and letting the caller compute the stroked shape
691 // which will have a fill style.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500692 return args.fShape->style().isSimpleFill() && (GrAAType::kCoverage != args.fAAType);
ethannicholas6536ae52016-05-02 12:16:49 -0700693}
694
695bool GrMSAAPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400696 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700697 "GrMSAAPathRenderer::onDrawPath");
bsalomon8acedde2016-06-24 10:42:16 -0700698 SkTLazy<GrShape> tmpShape;
699 const GrShape* shape = args.fShape;
700 if (shape->style().applies()) {
bsalomon6663acf2016-05-10 09:14:17 -0700701 SkScalar styleScale = GrStyle::MatrixToScaleFactor(*args.fViewMatrix);
bsalomon8acedde2016-06-24 10:42:16 -0700702 tmpShape.init(args.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale));
703 shape = tmpShape.get();
ethannicholas6536ae52016-05-02 12:16:49 -0700704 }
Brian Osman11052242016-10-27 14:47:55 -0400705 return this->internalDrawPath(args.fRenderTargetContext,
robertphillips976f5f02016-06-03 10:59:20 -0700706 *args.fPaint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500707 args.fAAType,
robertphillipsd2b6d642016-07-21 08:55:08 -0700708 *args.fUserStencilSettings,
cdalton862cff32016-05-12 15:09:48 -0700709 *args.fClip,
ethannicholas6536ae52016-05-02 12:16:49 -0700710 *args.fViewMatrix,
bsalomon8acedde2016-06-24 10:42:16 -0700711 *shape,
ethannicholas6536ae52016-05-02 12:16:49 -0700712 false);
713}
714
715void GrMSAAPathRenderer::onStencilPath(const StencilPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400716 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700717 "GrMSAAPathRenderer::onStencilPath");
bsalomon8acedde2016-06-24 10:42:16 -0700718 SkASSERT(args.fShape->style().isSimpleFill());
719 SkASSERT(!args.fShape->mayBeInverseFilledAfterStyling());
robertphillips976f5f02016-06-03 10:59:20 -0700720
721 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700722 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700723
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500724 this->internalDrawPath(args.fRenderTargetContext, paint, args.fAAType,
725 GrUserStencilSettings::kUnused, *args.fClip, *args.fViewMatrix,
726 *args.fShape, true);
ethannicholas6536ae52016-05-02 12:16:49 -0700727}
728
729///////////////////////////////////////////////////////////////////////////////////////////////////