blob: a45e6fad15a35815b985f4fbea440444568d344c [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 "gl/GrGLVaryingHandler.h"
ethannicholas6536ae52016-05-02 12:16:49 -070022#include "glsl/GrGLSLFragmentShaderBuilder.h"
Brian Salomondad29232016-12-01 16:40:24 -050023#include "glsl/GrGLSLGeometryProcessor.h"
ethannicholas6536ae52016-05-02 12:16:49 -070024#include "glsl/GrGLSLProgramDataManager.h"
25#include "glsl/GrGLSLUtil.h"
Brian Salomondad29232016-12-01 16:40:24 -050026#include "glsl/GrGLSLVertexShaderBuilder.h"
Brian Salomon89527432016-12-16 09:52:16 -050027#include "ops/GrMeshDrawOp.h"
28#include "ops/GrRectOpFactory.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 Salomon780dad12016-12-15 18:08:40 -0500217class MSAAPathOp final : public GrMeshDrawOp {
ethannicholas6536ae52016-05-02 12:16:49 -0700218public:
Brian Salomon25a88092016-12-01 09:36:50 -0500219 DEFINE_OP_CLASS_ID
Brian Salomonf8334782017-01-03 09:42:58 -0500220 static std::unique_ptr<GrDrawOp> Make(GrColor color, const SkPath& path,
221 const SkMatrix& viewMatrix, const SkRect& devBounds) {
bsalomon50c56a32016-06-30 12:05:32 -0700222 int contourCount;
Brian Salomon780dad12016-12-15 18:08:40 -0500223 int maxLineVertices;
224 int maxQuadVertices;
225 ComputeWorstCasePointCount(path, &contourCount, &maxLineVertices, &maxQuadVertices);
226 bool isIndexed = contourCount > 1;
227 if (isIndexed &&
228 (maxLineVertices > kMaxIndexedVertexCnt || maxQuadVertices > kMaxIndexedVertexCnt)) {
229 return nullptr;
230 }
231
Brian Salomonf8334782017-01-03 09:42:58 -0500232 return std::unique_ptr<GrDrawOp>(new MSAAPathOp(
233 color, path, viewMatrix, devBounds, maxLineVertices, maxQuadVertices, isIndexed));
ethannicholas6536ae52016-05-02 12:16:49 -0700234 }
235
Brian Salomon780dad12016-12-15 18:08:40 -0500236 const char* name() const override { return "MSAAPathOp"; }
ethannicholas6536ae52016-05-02 12:16:49 -0700237
Brian Salomon7c3e7182016-12-01 09:35:30 -0500238 SkString dumpInfo() const override {
239 SkString string;
240 string.appendf("Indexed: %d\n", fIsIndexed);
241 for (const auto& path : fPaths) {
242 string.appendf("Color: 0x%08x\n", path.fColor);
243 }
244 string.append(DumpPipelineInfo(*this->pipeline()));
245 string.append(INHERITED::dumpInfo());
246 return string;
247 }
248
Brian Salomon780dad12016-12-15 18:08:40 -0500249private:
250 MSAAPathOp(GrColor color, const SkPath& path, const SkMatrix& viewMatrix,
251 const SkRect& devBounds, int maxLineVertices, int maxQuadVertices, bool isIndexed)
252 : INHERITED(ClassID())
253 , fViewMatrix(viewMatrix)
254 , fMaxLineVertices(maxLineVertices)
255 , fMaxQuadVertices(maxQuadVertices)
256 , fIsIndexed(isIndexed) {
257 fPaths.emplace_back(PathInfo{color, path});
258 this->setBounds(devBounds, HasAABloat::kNo, IsZeroArea::kNo);
ethannicholas6536ae52016-05-02 12:16:49 -0700259 }
260
Brian Salomon92aee3d2016-12-21 09:20:25 -0500261 void getPipelineAnalysisInput(GrPipelineAnalysisDrawOpInput* input) const override {
262 input->pipelineColorInput()->setKnownFourComponents(fPaths[0].fColor);
263 input->pipelineCoverageInput()->setKnownSingleComponent(0xff);
264 }
265
266 void applyPipelineOptimizations(const GrPipelineOptimizations& optimizations) override {
267 if (!optimizations.readsColor()) {
bsalomon50c56a32016-06-30 12:05:32 -0700268 fPaths[0].fColor = GrColor_ILLEGAL;
ethannicholas6536ae52016-05-02 12:16:49 -0700269 }
Brian Salomon92aee3d2016-12-21 09:20:25 -0500270 optimizations.getOverrideColorIfSet(&fPaths[0].fColor);
ethannicholas6536ae52016-05-02 12:16:49 -0700271 }
272
Brian Salomon780dad12016-12-15 18:08:40 -0500273 static void ComputeWorstCasePointCount(const SkPath& path, int* subpaths,
274 int* outLinePointCount, int* outQuadPointCount) {
ethannicholas6536ae52016-05-02 12:16:49 -0700275 int linePointCount = 0;
276 int quadPointCount = 0;
277 *subpaths = 1;
278
279 bool first = true;
280
bsalomon8eb43e52016-09-21 07:47:34 -0700281 SkPath::Iter iter(path, true);
ethannicholas6536ae52016-05-02 12:16:49 -0700282 SkPath::Verb verb;
283
284 SkPoint pts[4];
285 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
286 switch (verb) {
287 case SkPath::kLine_Verb:
288 linePointCount += 1;
289 break;
290 case SkPath::kConic_Verb: {
291 SkScalar weight = iter.conicWeight();
292 SkAutoConicToQuads converter;
293 converter.computeQuads(pts, weight, kTolerance);
294 int quadPts = converter.countQuads();
295 linePointCount += quadPts;
296 quadPointCount += 3 * quadPts;
297 }
298 case SkPath::kQuad_Verb:
299 linePointCount += 1;
300 quadPointCount += 3;
301 break;
302 case SkPath::kCubic_Verb: {
303 SkSTArray<15, SkPoint, true> quadPts;
304 GrPathUtils::convertCubicToQuads(pts, kTolerance, &quadPts);
305 int count = quadPts.count();
306 linePointCount += count / 3;
307 quadPointCount += count;
308 break;
309 }
310 case SkPath::kMove_Verb:
311 linePointCount += 1;
312 if (!first) {
313 ++(*subpaths);
314 }
315 break;
316 default:
317 break;
318 }
319 first = false;
320 }
321 *outLinePointCount = linePointCount;
322 *outQuadPointCount = quadPointCount;
323 }
324
325 void onPrepareDraws(Target* target) const override {
ethannicholas6536ae52016-05-02 12:16:49 -0700326 if (fMaxLineVertices == 0) {
327 SkASSERT(fMaxQuadVertices == 0);
328 return;
329 }
330
bungeman06ca8ec2016-06-09 08:01:03 -0700331 GrPrimitiveType primitiveType = fIsIndexed ? kTriangles_GrPrimitiveType
ethannicholas6536ae52016-05-02 12:16:49 -0700332 : kTriangleFan_GrPrimitiveType;
333
334 // allocate vertex / index buffers
335 const GrBuffer* lineVertexBuffer;
336 int firstLineVertex;
337 MSAALineVertices lines;
338 size_t lineVertexStride = sizeof(MSAALineVertices::Vertex);
bungeman06ca8ec2016-06-09 08:01:03 -0700339 lines.vertices = (MSAALineVertices::Vertex*) target->makeVertexSpace(lineVertexStride,
ethannicholas6536ae52016-05-02 12:16:49 -0700340 fMaxLineVertices,
bungeman06ca8ec2016-06-09 08:01:03 -0700341 &lineVertexBuffer,
ethannicholas6536ae52016-05-02 12:16:49 -0700342 &firstLineVertex);
343 if (!lines.vertices) {
344 SkDebugf("Could not allocate vertices\n");
345 return;
346 }
347 lines.nextVertex = lines.vertices;
348 SkDEBUGCODE(lines.verticesEnd = lines.vertices + fMaxLineVertices;)
349
350 MSAAQuadVertices quads;
351 size_t quadVertexStride = sizeof(MSAAQuadVertices::Vertex);
352 SkAutoFree quadVertexPtr(sk_malloc_throw(fMaxQuadVertices * quadVertexStride));
353 quads.vertices = (MSAAQuadVertices::Vertex*) quadVertexPtr.get();
354 quads.nextVertex = quads.vertices;
355 SkDEBUGCODE(quads.verticesEnd = quads.vertices + fMaxQuadVertices;)
356
357 const GrBuffer* lineIndexBuffer = nullptr;
358 int firstLineIndex;
359 if (fIsIndexed) {
Brian Salomon780dad12016-12-15 18:08:40 -0500360 lines.indices =
361 target->makeIndexSpace(3 * fMaxLineVertices, &lineIndexBuffer, &firstLineIndex);
ethannicholas6536ae52016-05-02 12:16:49 -0700362 if (!lines.indices) {
363 SkDebugf("Could not allocate indices\n");
364 return;
365 }
366 lines.nextIndex = lines.indices;
367 } else {
368 lines.indices = nullptr;
369 lines.nextIndex = nullptr;
370 }
371
372 SkAutoFree quadIndexPtr;
373 if (fIsIndexed) {
Brian Salomon780dad12016-12-15 18:08:40 -0500374 quads.indices = (uint16_t*)sk_malloc_throw(3 * fMaxQuadVertices * sizeof(uint16_t));
ethannicholas6536ae52016-05-02 12:16:49 -0700375 quadIndexPtr.set(quads.indices);
376 quads.nextIndex = quads.indices;
377 } else {
378 quads.indices = nullptr;
379 quads.nextIndex = nullptr;
380 }
381
382 // fill buffers
bsalomon50c56a32016-06-30 12:05:32 -0700383 for (int i = 0; i < fPaths.count(); i++) {
384 const PathInfo& pathInfo = fPaths[i];
ethannicholas6536ae52016-05-02 12:16:49 -0700385
386 if (!this->createGeom(lines,
387 quads,
bsalomon50c56a32016-06-30 12:05:32 -0700388 pathInfo.fPath,
ethannicholas6536ae52016-05-02 12:16:49 -0700389 fViewMatrix,
bsalomon50c56a32016-06-30 12:05:32 -0700390 pathInfo.fColor,
ethannicholas6536ae52016-05-02 12:16:49 -0700391 fIsIndexed)) {
392 return;
393 }
394 }
395 int lineVertexOffset = (int) (lines.nextVertex - lines.vertices);
396 int lineIndexOffset = (int) (lines.nextIndex - lines.indices);
Brian Salomon780dad12016-12-15 18:08:40 -0500397 SkASSERT(lineVertexOffset <= fMaxLineVertices && lineIndexOffset <= 3 * fMaxLineVertices);
ethannicholas6536ae52016-05-02 12:16:49 -0700398 int quadVertexOffset = (int) (quads.nextVertex - quads.vertices);
399 int quadIndexOffset = (int) (quads.nextIndex - quads.indices);
Brian Salomon780dad12016-12-15 18:08:40 -0500400 SkASSERT(quadVertexOffset <= fMaxQuadVertices && quadIndexOffset <= 3 * fMaxQuadVertices);
ethannicholas6536ae52016-05-02 12:16:49 -0700401
402 if (lineVertexOffset) {
bungeman06ca8ec2016-06-09 08:01:03 -0700403 sk_sp<GrGeometryProcessor> lineGP;
ethannicholas6536ae52016-05-02 12:16:49 -0700404 {
405 using namespace GrDefaultGeoProcFactory;
bungeman06ca8ec2016-06-09 08:01:03 -0700406 lineGP = GrDefaultGeoProcFactory::Make(Color(Color::kAttribute_Type),
Brian Salomon8c852be2017-01-04 10:44:42 -0500407 Coverage::kSolid_Type,
bungeman06ca8ec2016-06-09 08:01:03 -0700408 LocalCoords(LocalCoords::kUnused_Type),
409 fViewMatrix);
ethannicholas6536ae52016-05-02 12:16:49 -0700410 }
411 SkASSERT(lineVertexStride == lineGP->getVertexStride());
412
413 GrMesh lineMeshes;
414 if (fIsIndexed) {
bungeman06ca8ec2016-06-09 08:01:03 -0700415 lineMeshes.initIndexed(primitiveType, lineVertexBuffer, lineIndexBuffer,
416 firstLineVertex, firstLineIndex, lineVertexOffset,
ethannicholas6536ae52016-05-02 12:16:49 -0700417 lineIndexOffset);
418 } else {
bungeman06ca8ec2016-06-09 08:01:03 -0700419 lineMeshes.init(primitiveType, lineVertexBuffer, firstLineVertex,
ethannicholas6536ae52016-05-02 12:16:49 -0700420 lineVertexOffset);
421 }
bungeman06ca8ec2016-06-09 08:01:03 -0700422 target->draw(lineGP.get(), lineMeshes);
ethannicholas6536ae52016-05-02 12:16:49 -0700423 }
424
425 if (quadVertexOffset) {
Hal Canary144caf52016-11-07 17:57:18 -0500426 sk_sp<const GrGeometryProcessor> quadGP(MSAAQuadProcessor::Create(fViewMatrix));
ethannicholas6536ae52016-05-02 12:16:49 -0700427 SkASSERT(quadVertexStride == quadGP->getVertexStride());
428
429 const GrBuffer* quadVertexBuffer;
430 int firstQuadVertex;
bungeman06ca8ec2016-06-09 08:01:03 -0700431 MSAAQuadVertices::Vertex* quadVertices = (MSAAQuadVertices::Vertex*)
ethannicholas6536ae52016-05-02 12:16:49 -0700432 target->makeVertexSpace(quadVertexStride, quadVertexOffset, &quadVertexBuffer,
433 &firstQuadVertex);
434 memcpy(quadVertices, quads.vertices, quadVertexStride * quadVertexOffset);
435 GrMesh quadMeshes;
436 if (fIsIndexed) {
437 const GrBuffer* quadIndexBuffer;
438 int firstQuadIndex;
bungeman06ca8ec2016-06-09 08:01:03 -0700439 uint16_t* quadIndices = (uint16_t*) target->makeIndexSpace(quadIndexOffset,
440 &quadIndexBuffer,
ethannicholas6536ae52016-05-02 12:16:49 -0700441 &firstQuadIndex);
442 memcpy(quadIndices, quads.indices, sizeof(uint16_t) * quadIndexOffset);
bungeman06ca8ec2016-06-09 08:01:03 -0700443 quadMeshes.initIndexed(kTriangles_GrPrimitiveType, quadVertexBuffer,
444 quadIndexBuffer, firstQuadVertex, firstQuadIndex,
ethannicholas6536ae52016-05-02 12:16:49 -0700445 quadVertexOffset, quadIndexOffset);
446 } else {
bungeman06ca8ec2016-06-09 08:01:03 -0700447 quadMeshes.init(kTriangles_GrPrimitiveType, quadVertexBuffer, firstQuadVertex,
ethannicholas6536ae52016-05-02 12:16:49 -0700448 quadVertexOffset);
449 }
Hal Canary144caf52016-11-07 17:57:18 -0500450 target->draw(quadGP.get(), quadMeshes);
ethannicholas6536ae52016-05-02 12:16:49 -0700451 }
452 }
453
Brian Salomon25a88092016-12-01 09:36:50 -0500454 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomon780dad12016-12-15 18:08:40 -0500455 MSAAPathOp* that = t->cast<MSAAPathOp>();
ethannicholas6536ae52016-05-02 12:16:49 -0700456 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
457 that->bounds(), caps)) {
458 return false;
459 }
460
461 if (!fViewMatrix.cheapEqualTo(that->fViewMatrix)) {
462 return false;
463 }
464
Brian Salomon780dad12016-12-15 18:08:40 -0500465 // If we grow to include 2+ paths we will be indexed.
466 if (((fMaxLineVertices + that->fMaxLineVertices) > kMaxIndexedVertexCnt) ||
467 ((fMaxQuadVertices + that->fMaxQuadVertices) > kMaxIndexedVertexCnt)) {
ethannicholas6536ae52016-05-02 12:16:49 -0700468 return false;
469 }
470
bsalomon50c56a32016-06-30 12:05:32 -0700471 fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700472 this->joinBounds(*that);
ethannicholas6536ae52016-05-02 12:16:49 -0700473 fIsIndexed = true;
474 fMaxLineVertices += that->fMaxLineVertices;
475 fMaxQuadVertices += that->fMaxQuadVertices;
ethannicholas6536ae52016-05-02 12:16:49 -0700476 return true;
477 }
478
479 bool createGeom(MSAALineVertices& lines,
480 MSAAQuadVertices& quads,
481 const SkPath& path,
ethannicholas6536ae52016-05-02 12:16:49 -0700482 const SkMatrix& m,
483 SkColor color,
484 bool isIndexed) const {
485 {
486 uint16_t subpathIdxStart = (uint16_t) (lines.nextVertex - lines.vertices);
487
488 SkPoint pts[4];
489
490 bool first = true;
bsalomon8eb43e52016-09-21 07:47:34 -0700491 SkPath::Iter iter(path, true);
ethannicholas6536ae52016-05-02 12:16:49 -0700492
493 bool done = false;
494 while (!done) {
495 SkPath::Verb verb = iter.next(pts);
496 switch (verb) {
497 case SkPath::kMove_Verb:
498 if (!first) {
499 uint16_t currIdx = (uint16_t) (lines.nextVertex - lines.vertices);
500 subpathIdxStart = currIdx;
501 }
502 SkASSERT(lines.nextVertex < lines.verticesEnd);
503 *(lines.nextVertex++) = { pts[0], color };
504 break;
505 case SkPath::kLine_Verb:
506 if (isIndexed) {
507 uint16_t prevIdx = (uint16_t) (lines.nextVertex - lines.vertices - 1);
508 if (prevIdx > subpathIdxStart) {
509 append_contour_edge_indices(subpathIdxStart, prevIdx, lines);
510 }
511 }
512 SkASSERT(lines.nextVertex < lines.verticesEnd);
513 *(lines.nextVertex++) = { pts[1], color };
514 break;
515 case SkPath::kConic_Verb: {
516 SkScalar weight = iter.conicWeight();
517 SkAutoConicToQuads converter;
bsalomon50c56a32016-06-30 12:05:32 -0700518 const SkPoint* quadPts = converter.computeQuads(pts, weight, kTolerance);
ethannicholas6536ae52016-05-02 12:16:49 -0700519 for (int i = 0; i < converter.countQuads(); ++i) {
bungeman06ca8ec2016-06-09 08:01:03 -0700520 add_quad(lines, quads, quadPts + i * 2, color, isIndexed,
ethannicholas6536ae52016-05-02 12:16:49 -0700521 subpathIdxStart);
522 }
523 break;
524 }
525 case SkPath::kQuad_Verb: {
526 add_quad(lines, quads, pts, color, isIndexed, subpathIdxStart);
bungeman06ca8ec2016-06-09 08:01:03 -0700527 break;
ethannicholas6536ae52016-05-02 12:16:49 -0700528 }
529 case SkPath::kCubic_Verb: {
530 SkSTArray<15, SkPoint, true> quadPts;
531 GrPathUtils::convertCubicToQuads(pts, kTolerance, &quadPts);
532 int count = quadPts.count();
533 for (int i = 0; i < count; i += 3) {
534 add_quad(lines, quads, &quadPts[i], color, isIndexed, subpathIdxStart);
535 }
536 break;
537 }
538 case SkPath::kClose_Verb:
539 break;
540 case SkPath::kDone_Verb:
541 done = true;
542 }
543 first = false;
544 }
545 }
546 return true;
547 }
548
Brian Salomon780dad12016-12-15 18:08:40 -0500549 // Lines and quads may render with an index buffer. However, we don't have any support for
550 // overflowing the max index.
551 static constexpr int kMaxIndexedVertexCnt = SK_MaxU16 / 3;
bsalomon50c56a32016-06-30 12:05:32 -0700552 struct PathInfo {
553 GrColor fColor;
554 SkPath fPath;
555 };
556
557 SkSTArray<1, PathInfo, true> fPaths;
ethannicholas6536ae52016-05-02 12:16:49 -0700558
559 SkMatrix fViewMatrix;
560 int fMaxLineVertices;
561 int fMaxQuadVertices;
ethannicholas6536ae52016-05-02 12:16:49 -0700562 bool fIsIndexed;
563
Brian Salomondad29232016-12-01 16:40:24 -0500564 typedef GrMeshDrawOp INHERITED;
ethannicholas6536ae52016-05-02 12:16:49 -0700565};
566
Brian Osman11052242016-10-27 14:47:55 -0400567bool GrMSAAPathRenderer::internalDrawPath(GrRenderTargetContext* renderTargetContext,
robertphillips976f5f02016-06-03 10:59:20 -0700568 const GrPaint& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500569 GrAAType aaType,
robertphillipsd2b6d642016-07-21 08:55:08 -0700570 const GrUserStencilSettings& userStencilSettings,
cdalton862cff32016-05-12 15:09:48 -0700571 const GrClip& clip,
ethannicholas6536ae52016-05-02 12:16:49 -0700572 const SkMatrix& viewMatrix,
bsalomon8acedde2016-06-24 10:42:16 -0700573 const GrShape& shape,
ethannicholas6536ae52016-05-02 12:16:49 -0700574 bool stencilOnly) {
bsalomon8acedde2016-06-24 10:42:16 -0700575 SkASSERT(shape.style().isSimpleFill());
576 SkPath path;
577 shape.asPath(&path);
578
robertphillips8e375302016-07-11 10:43:58 -0700579 static const int kMaxNumPasses = 2;
580
cdalton93a379b2016-05-11 13:58:08 -0700581 int passCount = 0;
robertphillips8e375302016-07-11 10:43:58 -0700582 const GrUserStencilSettings* passes[kMaxNumPasses];
cdalton93a379b2016-05-11 13:58:08 -0700583 bool reverse = false;
584 bool lastPassIsBounds;
ethannicholas6536ae52016-05-02 12:16:49 -0700585
bsalomon8acedde2016-06-24 10:42:16 -0700586 if (single_pass_shape(shape)) {
ethannicholas6536ae52016-05-02 12:16:49 -0700587 passCount = 1;
588 if (stencilOnly) {
589 passes[0] = &gDirectToStencil;
590 } else {
robertphillipsd2b6d642016-07-21 08:55:08 -0700591 passes[0] = &userStencilSettings;
ethannicholas6536ae52016-05-02 12:16:49 -0700592 }
ethannicholas6536ae52016-05-02 12:16:49 -0700593 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 }
ethannicholas6536ae52016-05-02 12:16:49 -0700613 break;
614
615 case SkPath::kInverseWinding_FillType:
616 reverse = true;
617 // fallthrough
618 case SkPath::kWinding_FillType:
619 passes[0] = &gWindStencilSeparateWithWrap;
620 passCount = 2;
ethannicholas6536ae52016-05-02 12:16:49 -0700621 if (stencilOnly) {
622 lastPassIsBounds = false;
robertphillips8e375302016-07-11 10:43:58 -0700623 passCount = 1;
ethannicholas6536ae52016-05-02 12:16:49 -0700624 } else {
625 lastPassIsBounds = true;
ethannicholas6536ae52016-05-02 12:16:49 -0700626 if (reverse) {
robertphillips8e375302016-07-11 10:43:58 -0700627 passes[1] = &gInvWindColorPass;
ethannicholas6536ae52016-05-02 12:16:49 -0700628 } else {
robertphillips8e375302016-07-11 10:43:58 -0700629 passes[1] = &gWindColorPass;
ethannicholas6536ae52016-05-02 12:16:49 -0700630 }
631 }
632 break;
633 default:
634 SkDEBUGFAIL("Unknown path fFill!");
635 return false;
636 }
637 }
638
639 SkRect devBounds;
Brian Osman11052242016-10-27 14:47:55 -0400640 GetPathDevBounds(path, renderTargetContext->width(), renderTargetContext->height(), viewMatrix,
641 &devBounds);
ethannicholas6536ae52016-05-02 12:16:49 -0700642
robertphillips8e375302016-07-11 10:43:58 -0700643 SkASSERT(passCount <= kMaxNumPasses);
644
ethannicholas6536ae52016-05-02 12:16:49 -0700645 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;
Brian Salomonf8334782017-01-03 09:42:58 -0500666 std::unique_ptr<GrDrawOp> op(GrRectOpFactory::MakeNonAAFill(
667 paint.getColor(), viewM, bounds, nullptr, &localMatrix));
robertphillips976f5f02016-06-03 10:59:20 -0700668
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500669 GrPipelineBuilder pipelineBuilder(paint, aaType);
bsalomonbb243832016-07-22 07:10:19 -0700670 pipelineBuilder.setUserStencil(passes[p]);
671
Brian Salomon24f19782016-12-13 15:10:11 -0500672 renderTargetContext->addDrawOp(pipelineBuilder, clip, std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -0700673 } else {
Brian Salomonf8334782017-01-03 09:42:58 -0500674 std::unique_ptr<GrDrawOp> op =
675 MSAAPathOp::Make(paint.getColor(), path, viewMatrix, devBounds);
Brian Salomon780dad12016-12-15 18:08:40 -0500676 if (!op) {
ethannicholas6536ae52016-05-02 12:16:49 -0700677 return false;
678 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500679 GrPipelineBuilder pipelineBuilder(paint, aaType);
bsalomonbb243832016-07-22 07:10:19 -0700680 pipelineBuilder.setUserStencil(passes[p]);
robertphillips976f5f02016-06-03 10:59:20 -0700681 if (passCount > 1) {
bsalomonbb243832016-07-22 07:10:19 -0700682 pipelineBuilder.setDisableColorXPFactory();
robertphillips976f5f02016-06-03 10:59:20 -0700683 }
Brian Salomon24f19782016-12-13 15:10:11 -0500684 renderTargetContext->addDrawOp(pipelineBuilder, clip, std::move(op));
ethannicholas6536ae52016-05-02 12:16:49 -0700685 }
686 }
687 return true;
688}
689
690bool GrMSAAPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
bsalomonee432412016-06-27 07:18:18 -0700691 // This path renderer only fills and relies on MSAA for antialiasing. Stroked shapes are
692 // handled by passing on the original shape and letting the caller compute the stroked shape
693 // which will have a fill style.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500694 return args.fShape->style().isSimpleFill() && (GrAAType::kCoverage != args.fAAType);
ethannicholas6536ae52016-05-02 12:16:49 -0700695}
696
697bool GrMSAAPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400698 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700699 "GrMSAAPathRenderer::onDrawPath");
bsalomon8acedde2016-06-24 10:42:16 -0700700 SkTLazy<GrShape> tmpShape;
701 const GrShape* shape = args.fShape;
702 if (shape->style().applies()) {
bsalomon6663acf2016-05-10 09:14:17 -0700703 SkScalar styleScale = GrStyle::MatrixToScaleFactor(*args.fViewMatrix);
bsalomon8acedde2016-06-24 10:42:16 -0700704 tmpShape.init(args.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale));
705 shape = tmpShape.get();
ethannicholas6536ae52016-05-02 12:16:49 -0700706 }
Brian Osman11052242016-10-27 14:47:55 -0400707 return this->internalDrawPath(args.fRenderTargetContext,
robertphillips976f5f02016-06-03 10:59:20 -0700708 *args.fPaint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500709 args.fAAType,
robertphillipsd2b6d642016-07-21 08:55:08 -0700710 *args.fUserStencilSettings,
cdalton862cff32016-05-12 15:09:48 -0700711 *args.fClip,
ethannicholas6536ae52016-05-02 12:16:49 -0700712 *args.fViewMatrix,
bsalomon8acedde2016-06-24 10:42:16 -0700713 *shape,
ethannicholas6536ae52016-05-02 12:16:49 -0700714 false);
715}
716
717void GrMSAAPathRenderer::onStencilPath(const StencilPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400718 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700719 "GrMSAAPathRenderer::onStencilPath");
bsalomon8acedde2016-06-24 10:42:16 -0700720 SkASSERT(args.fShape->style().isSimpleFill());
721 SkASSERT(!args.fShape->mayBeInverseFilledAfterStyling());
robertphillips976f5f02016-06-03 10:59:20 -0700722
723 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700724 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700725
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500726 this->internalDrawPath(args.fRenderTargetContext, paint, args.fAAType,
727 GrUserStencilSettings::kUnused, *args.fClip, *args.fViewMatrix,
728 *args.fShape, true);
ethannicholas6536ae52016-05-02 12:16:49 -0700729}
730
731///////////////////////////////////////////////////////////////////////////////////////////////////