blob: c3a5440fc3c8ea583fb33f8ad34d782c876c2e4a [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"
Hal Canary95e3c052017-01-11 12:44:43 -050019#include "SkAutoMalloc.h"
ethannicholas6536ae52016-05-02 12:16:49 -070020#include "SkGeometry.h"
21#include "SkTraceEvent.h"
Brian Salomondad29232016-12-01 16:40:24 -050022#include "gl/GrGLVaryingHandler.h"
ethannicholas6536ae52016-05-02 12:16:49 -070023#include "glsl/GrGLSLFragmentShaderBuilder.h"
Brian Salomondad29232016-12-01 16:40:24 -050024#include "glsl/GrGLSLGeometryProcessor.h"
ethannicholas6536ae52016-05-02 12:16:49 -070025#include "glsl/GrGLSLProgramDataManager.h"
26#include "glsl/GrGLSLUtil.h"
Brian Salomondad29232016-12-01 16:40:24 -050027#include "glsl/GrGLSLVertexShaderBuilder.h"
Brian Salomon89527432016-12-16 09:52:16 -050028#include "ops/GrMeshDrawOp.h"
Brian Salomonac70f842017-05-08 10:43:33 -040029#include "ops/GrNonAAFillRectOp.h"
ethannicholas6536ae52016-05-02 12:16:49 -070030
31static const float kTolerance = 0.5f;
32
33////////////////////////////////////////////////////////////////////////////////
34// Helpers for drawPath
35
bsalomon8acedde2016-06-24 10:42:16 -070036static inline bool single_pass_shape(const GrShape& shape) {
37 if (!shape.inverseFilled()) {
38 return shape.knownToBeConvex();
ethannicholas6536ae52016-05-02 12:16:49 -070039 }
40 return false;
41}
42
bsalomon8acedde2016-06-24 10:42:16 -070043GrPathRenderer::StencilSupport GrMSAAPathRenderer::onGetStencilSupport(const GrShape& shape) const {
44 if (single_pass_shape(shape)) {
ethannicholas6536ae52016-05-02 12:16:49 -070045 return GrPathRenderer::kNoRestriction_StencilSupport;
46 } else {
47 return GrPathRenderer::kStencilOnly_StencilSupport;
48 }
49}
50
51struct MSAALineVertices {
52 struct Vertex {
53 SkPoint fPosition;
54 SkColor fColor;
55 };
56 Vertex* vertices;
57 Vertex* nextVertex;
58#ifdef SK_DEBUG
59 Vertex* verticesEnd;
60#endif
61 uint16_t* indices;
62 uint16_t* nextIndex;
63};
64
65struct MSAAQuadVertices {
66 struct Vertex {
67 SkPoint fPosition;
68 SkPoint fUV;
69 SkColor fColor;
70 };
71 Vertex* vertices;
72 Vertex* nextVertex;
73#ifdef SK_DEBUG
74 Vertex* verticesEnd;
75#endif
76 uint16_t* indices;
77 uint16_t* nextIndex;
78};
79
80static inline void append_contour_edge_indices(uint16_t fanCenterIdx,
81 uint16_t edgeV0Idx,
82 MSAALineVertices& lines) {
83 *(lines.nextIndex++) = fanCenterIdx;
84 *(lines.nextIndex++) = edgeV0Idx;
85 *(lines.nextIndex++) = edgeV0Idx + 1;
86}
87
bungeman06ca8ec2016-06-09 08:01:03 -070088static inline void add_quad(MSAALineVertices& lines, MSAAQuadVertices& quads, const SkPoint pts[],
ethannicholas6536ae52016-05-02 12:16:49 -070089 SkColor color, bool indexed, uint16_t subpathLineIdxStart) {
90 SkASSERT(lines.nextVertex < lines.verticesEnd);
91 *lines.nextVertex = { pts[2], color };
92 if (indexed) {
93 int prevIdx = (uint16_t) (lines.nextVertex - lines.vertices - 1);
94 if (prevIdx > subpathLineIdxStart) {
95 append_contour_edge_indices(subpathLineIdxStart, prevIdx, lines);
96 }
97 }
98 lines.nextVertex++;
99
100 SkASSERT(quads.nextVertex + 2 < quads.verticesEnd);
101 // the texture coordinates are drawn from the Loop-Blinn rendering algorithm
102 *(quads.nextVertex++) = { pts[0], SkPoint::Make(0.0, 0.0), color };
103 *(quads.nextVertex++) = { pts[1], SkPoint::Make(0.5, 0.0), color };
104 *(quads.nextVertex++) = { pts[2], SkPoint::Make(1.0, 1.0), color };
105 if (indexed) {
106 uint16_t offset = (uint16_t) (quads.nextVertex - quads.vertices) - 3;
107 *(quads.nextIndex++) = offset++;
108 *(quads.nextIndex++) = offset++;
109 *(quads.nextIndex++) = offset++;
110 }
111}
112
113class MSAAQuadProcessor : public GrGeometryProcessor {
114public:
115 static GrGeometryProcessor* Create(const SkMatrix& viewMatrix) {
116 return new MSAAQuadProcessor(viewMatrix);
117 }
118
Brian Salomond3b65972017-03-22 12:05:03 -0400119 ~MSAAQuadProcessor() override {}
ethannicholas6536ae52016-05-02 12:16:49 -0700120
121 const char* name() const override { return "MSAAQuadProcessor"; }
122
123 const Attribute* inPosition() const { return fInPosition; }
124 const Attribute* inUV() const { return fInUV; }
125 const Attribute* inColor() const { return fInColor; }
126 const SkMatrix& viewMatrix() const { return fViewMatrix; }
ethannicholas6536ae52016-05-02 12:16:49 -0700127
128 class GLSLProcessor : public GrGLSLGeometryProcessor {
129 public:
130 GLSLProcessor(const GrGeometryProcessor& qpr) {}
131
132 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
133 const MSAAQuadProcessor& qp = args.fGP.cast<MSAAQuadProcessor>();
134 GrGLSLVertexBuilder* vsBuilder = args.fVertBuilder;
135 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
136 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
137
138 // emit attributes
139 varyingHandler->emitAttributes(qp);
140 varyingHandler->addPassThroughAttribute(qp.inColor(), args.fOutputColor);
141
142 GrGLSLVertToFrag uv(kVec2f_GrSLType);
143 varyingHandler->addVarying("uv", &uv, kHigh_GrSLPrecision);
144 vsBuilder->codeAppendf("%s = %s;", uv.vsOut(), qp.inUV()->fName);
145
146 // Setup position
bungeman06ca8ec2016-06-09 08:01:03 -0700147 this->setupPosition(vsBuilder, uniformHandler, gpArgs, qp.inPosition()->fName,
ethannicholas6536ae52016-05-02 12:16:49 -0700148 qp.viewMatrix(), &fViewMatrixUniform);
149
150 // emit transforms
bungeman06ca8ec2016-06-09 08:01:03 -0700151 this->emitTransforms(vsBuilder, varyingHandler, uniformHandler, gpArgs->fPositionVar,
bsalomona624bf32016-09-20 09:12:47 -0700152 qp.inPosition()->fName, SkMatrix::I(),
153 args.fFPCoordTransformHandler);
ethannicholas6536ae52016-05-02 12:16:49 -0700154
155 GrGLSLPPFragmentBuilder* fsBuilder = args.fFragBuilder;
bungeman06ca8ec2016-06-09 08:01:03 -0700156 fsBuilder->codeAppendf("if (%s.x * %s.x >= %s.y) discard;", uv.fsIn(), uv.fsIn(),
ethannicholas6536ae52016-05-02 12:16:49 -0700157 uv.fsIn());
158 fsBuilder->codeAppendf("%s = vec4(1.0);", args.fOutputCoverage);
159 }
160
161 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500162 const GrShaderCaps&,
ethannicholas6536ae52016-05-02 12:16:49 -0700163 GrProcessorKeyBuilder* b) {
164 const MSAAQuadProcessor& qp = gp.cast<MSAAQuadProcessor>();
165 uint32_t key = 0;
166 key |= qp.viewMatrix().hasPerspective() ? 0x1 : 0x0;
167 key |= qp.viewMatrix().isIdentity() ? 0x2: 0x0;
168 b->add32(key);
169 }
170
bsalomona624bf32016-09-20 09:12:47 -0700171 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& gp,
172 FPCoordTransformIter&& transformIter) override {
ethannicholas6536ae52016-05-02 12:16:49 -0700173 const MSAAQuadProcessor& qp = gp.cast<MSAAQuadProcessor>();
174 if (!qp.viewMatrix().isIdentity()) {
175 float viewMatrix[3 * 3];
176 GrGLSLGetMatrix<3>(viewMatrix, qp.viewMatrix());
177 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
178 }
bsalomona624bf32016-09-20 09:12:47 -0700179 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
ethannicholas6536ae52016-05-02 12:16:49 -0700180 }
181
ethannicholas6536ae52016-05-02 12:16:49 -0700182 private:
183 typedef GrGLSLGeometryProcessor INHERITED;
184
185 UniformHandle fViewMatrixUniform;
186 };
187
Brian Salomon94efbf52016-11-29 13:43:05 -0500188 virtual void getGLSLProcessorKey(const GrShaderCaps& caps,
ethannicholas6536ae52016-05-02 12:16:49 -0700189 GrProcessorKeyBuilder* b) const override {
190 GLSLProcessor::GenKey(*this, caps, b);
191 }
192
Brian Salomon94efbf52016-11-29 13:43:05 -0500193 virtual GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override {
ethannicholas6536ae52016-05-02 12:16:49 -0700194 return new GLSLProcessor(*this);
195 }
196
197private:
198 MSAAQuadProcessor(const SkMatrix& viewMatrix)
199 : fViewMatrix(viewMatrix) {
200 this->initClassID<MSAAQuadProcessor>();
bsalomon6cb807b2016-08-17 11:33:39 -0700201 fInPosition = &this->addVertexAttrib("inPosition", kVec2f_GrVertexAttribType,
202 kHigh_GrSLPrecision);
203 fInUV = &this->addVertexAttrib("inUV", kVec2f_GrVertexAttribType, kHigh_GrSLPrecision);
204 fInColor = &this->addVertexAttrib("inColor", kVec4ub_GrVertexAttribType);
ethannicholas6536ae52016-05-02 12:16:49 -0700205 this->setSampleShading(1.0f);
206 }
207
208 const Attribute* fInPosition;
209 const Attribute* fInUV;
210 const Attribute* fInColor;
211 SkMatrix fViewMatrix;
bungeman06ca8ec2016-06-09 08:01:03 -0700212
ethannicholas6536ae52016-05-02 12:16:49 -0700213 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
214
215 typedef GrGeometryProcessor INHERITED;
216};
217
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400218class MSAAPathOp final : public GrLegacyMeshDrawOp {
ethannicholas6536ae52016-05-02 12:16:49 -0700219public:
Brian Salomon25a88092016-12-01 09:36:50 -0500220 DEFINE_OP_CLASS_ID
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400221 static std::unique_ptr<GrLegacyMeshDrawOp> Make(GrColor color, const SkPath& path,
222 const SkMatrix& viewMatrix,
223 const SkRect& devBounds) {
bsalomon50c56a32016-06-30 12:05:32 -0700224 int contourCount;
Brian Salomon780dad12016-12-15 18:08:40 -0500225 int maxLineVertices;
226 int maxQuadVertices;
Brian Salomon32ebaba2017-03-30 10:22:28 -0400227 ComputeWorstCasePointCount(path, viewMatrix, &contourCount, &maxLineVertices,
228 &maxQuadVertices);
Brian Salomon780dad12016-12-15 18:08:40 -0500229 bool isIndexed = contourCount > 1;
230 if (isIndexed &&
231 (maxLineVertices > kMaxIndexedVertexCnt || maxQuadVertices > kMaxIndexedVertexCnt)) {
232 return nullptr;
233 }
234
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400235 return std::unique_ptr<GrLegacyMeshDrawOp>(new MSAAPathOp(
Brian Salomonf8334782017-01-03 09:42:58 -0500236 color, path, viewMatrix, devBounds, maxLineVertices, maxQuadVertices, isIndexed));
ethannicholas6536ae52016-05-02 12:16:49 -0700237 }
238
Brian Salomon780dad12016-12-15 18:08:40 -0500239 const char* name() const override { return "MSAAPathOp"; }
ethannicholas6536ae52016-05-02 12:16:49 -0700240
Brian Salomon7c3e7182016-12-01 09:35:30 -0500241 SkString dumpInfo() const override {
242 SkString string;
243 string.appendf("Indexed: %d\n", fIsIndexed);
244 for (const auto& path : fPaths) {
245 string.appendf("Color: 0x%08x\n", path.fColor);
246 }
247 string.append(DumpPipelineInfo(*this->pipeline()));
248 string.append(INHERITED::dumpInfo());
249 return string;
250 }
251
Brian Salomon780dad12016-12-15 18:08:40 -0500252private:
253 MSAAPathOp(GrColor color, const SkPath& path, const SkMatrix& viewMatrix,
254 const SkRect& devBounds, int maxLineVertices, int maxQuadVertices, bool isIndexed)
255 : INHERITED(ClassID())
256 , fViewMatrix(viewMatrix)
257 , fMaxLineVertices(maxLineVertices)
258 , fMaxQuadVertices(maxQuadVertices)
259 , fIsIndexed(isIndexed) {
260 fPaths.emplace_back(PathInfo{color, path});
261 this->setBounds(devBounds, HasAABloat::kNo, IsZeroArea::kNo);
ethannicholas6536ae52016-05-02 12:16:49 -0700262 }
263
Brian Salomona811b122017-03-30 08:21:32 -0400264 void getProcessorAnalysisInputs(GrProcessorAnalysisColor* color,
265 GrProcessorAnalysisCoverage* coverage) const override {
Brian Salomonc0b642c2017-03-27 13:09:36 -0400266 color->setToConstant(fPaths[0].fColor);
Brian Salomona811b122017-03-30 08:21:32 -0400267 *coverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomon92aee3d2016-12-21 09:20:25 -0500268 }
269
Brian Salomone7d30482017-03-29 12:09:15 -0400270 void applyPipelineOptimizations(const PipelineOptimizations& optimizations) override {
Brian Salomon92aee3d2016-12-21 09:20:25 -0500271 optimizations.getOverrideColorIfSet(&fPaths[0].fColor);
ethannicholas6536ae52016-05-02 12:16:49 -0700272 }
273
Brian Salomon32ebaba2017-03-30 10:22:28 -0400274 static void ComputeWorstCasePointCount(const SkPath& path, const SkMatrix& m, int* subpaths,
Brian Salomon780dad12016-12-15 18:08:40 -0500275 int* outLinePointCount, int* outQuadPointCount) {
Brian Salomon32ebaba2017-03-30 10:22:28 -0400276 SkScalar tolerance = GrPathUtils::scaleToleranceToSrc(kTolerance, m, path.getBounds());
ethannicholas6536ae52016-05-02 12:16:49 -0700277 int linePointCount = 0;
278 int quadPointCount = 0;
279 *subpaths = 1;
280
281 bool first = true;
282
bsalomon8eb43e52016-09-21 07:47:34 -0700283 SkPath::Iter iter(path, true);
ethannicholas6536ae52016-05-02 12:16:49 -0700284 SkPath::Verb verb;
285
286 SkPoint pts[4];
287 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
288 switch (verb) {
289 case SkPath::kLine_Verb:
290 linePointCount += 1;
291 break;
292 case SkPath::kConic_Verb: {
293 SkScalar weight = iter.conicWeight();
294 SkAutoConicToQuads converter;
Brian Salomon32ebaba2017-03-30 10:22:28 -0400295 converter.computeQuads(pts, weight, tolerance);
ethannicholas6536ae52016-05-02 12:16:49 -0700296 int quadPts = converter.countQuads();
297 linePointCount += quadPts;
298 quadPointCount += 3 * quadPts;
299 }
300 case SkPath::kQuad_Verb:
301 linePointCount += 1;
302 quadPointCount += 3;
303 break;
304 case SkPath::kCubic_Verb: {
305 SkSTArray<15, SkPoint, true> quadPts;
Brian Salomon32ebaba2017-03-30 10:22:28 -0400306 GrPathUtils::convertCubicToQuads(pts, tolerance, &quadPts);
ethannicholas6536ae52016-05-02 12:16:49 -0700307 int count = quadPts.count();
308 linePointCount += count / 3;
309 quadPointCount += count;
310 break;
311 }
312 case SkPath::kMove_Verb:
313 linePointCount += 1;
314 if (!first) {
315 ++(*subpaths);
316 }
317 break;
318 default:
319 break;
320 }
321 first = false;
322 }
323 *outLinePointCount = linePointCount;
324 *outQuadPointCount = quadPointCount;
325 }
326
327 void onPrepareDraws(Target* target) const override {
ethannicholas6536ae52016-05-02 12:16:49 -0700328 if (fMaxLineVertices == 0) {
329 SkASSERT(fMaxQuadVertices == 0);
330 return;
331 }
332
bungeman06ca8ec2016-06-09 08:01:03 -0700333 GrPrimitiveType primitiveType = fIsIndexed ? kTriangles_GrPrimitiveType
ethannicholas6536ae52016-05-02 12:16:49 -0700334 : kTriangleFan_GrPrimitiveType;
335
336 // allocate vertex / index buffers
337 const GrBuffer* lineVertexBuffer;
338 int firstLineVertex;
339 MSAALineVertices lines;
340 size_t lineVertexStride = sizeof(MSAALineVertices::Vertex);
bungeman06ca8ec2016-06-09 08:01:03 -0700341 lines.vertices = (MSAALineVertices::Vertex*) target->makeVertexSpace(lineVertexStride,
ethannicholas6536ae52016-05-02 12:16:49 -0700342 fMaxLineVertices,
bungeman06ca8ec2016-06-09 08:01:03 -0700343 &lineVertexBuffer,
ethannicholas6536ae52016-05-02 12:16:49 -0700344 &firstLineVertex);
345 if (!lines.vertices) {
346 SkDebugf("Could not allocate vertices\n");
347 return;
348 }
349 lines.nextVertex = lines.vertices;
350 SkDEBUGCODE(lines.verticesEnd = lines.vertices + fMaxLineVertices;)
351
352 MSAAQuadVertices quads;
353 size_t quadVertexStride = sizeof(MSAAQuadVertices::Vertex);
Hal Canary95e3c052017-01-11 12:44:43 -0500354 SkAutoMalloc quadVertexPtr(fMaxQuadVertices * quadVertexStride);
ethannicholas6536ae52016-05-02 12:16:49 -0700355 quads.vertices = (MSAAQuadVertices::Vertex*) quadVertexPtr.get();
356 quads.nextVertex = quads.vertices;
357 SkDEBUGCODE(quads.verticesEnd = quads.vertices + fMaxQuadVertices;)
358
359 const GrBuffer* lineIndexBuffer = nullptr;
360 int firstLineIndex;
361 if (fIsIndexed) {
Brian Salomon780dad12016-12-15 18:08:40 -0500362 lines.indices =
363 target->makeIndexSpace(3 * fMaxLineVertices, &lineIndexBuffer, &firstLineIndex);
ethannicholas6536ae52016-05-02 12:16:49 -0700364 if (!lines.indices) {
365 SkDebugf("Could not allocate indices\n");
366 return;
367 }
368 lines.nextIndex = lines.indices;
369 } else {
370 lines.indices = nullptr;
371 lines.nextIndex = nullptr;
372 }
373
374 SkAutoFree quadIndexPtr;
375 if (fIsIndexed) {
Brian Salomon780dad12016-12-15 18:08:40 -0500376 quads.indices = (uint16_t*)sk_malloc_throw(3 * fMaxQuadVertices * sizeof(uint16_t));
Hal Canary95e3c052017-01-11 12:44:43 -0500377 quadIndexPtr.reset(quads.indices);
ethannicholas6536ae52016-05-02 12:16:49 -0700378 quads.nextIndex = quads.indices;
379 } else {
380 quads.indices = nullptr;
381 quads.nextIndex = nullptr;
382 }
ethannicholas6536ae52016-05-02 12:16:49 -0700383 // fill buffers
bsalomon50c56a32016-06-30 12:05:32 -0700384 for (int i = 0; i < fPaths.count(); i++) {
385 const PathInfo& pathInfo = fPaths[i];
ethannicholas6536ae52016-05-02 12:16:49 -0700386 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;
Brian Salomon3de0aee2017-01-29 09:34:17 -0500406 lineGP = GrDefaultGeoProcFactory::Make(Color(Color::kPremulGrColorAttribute_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
Chris Daltonbca46e22017-05-15 11:03:26 -0600413 GrMesh lineMeshes(primitiveType);
ethannicholas6536ae52016-05-02 12:16:49 -0700414 if (fIsIndexed) {
Chris Daltonbca46e22017-05-15 11:03:26 -0600415 lineMeshes.setIndexed(lineIndexBuffer, lineIndexOffset, firstLineIndex);
ethannicholas6536ae52016-05-02 12:16:49 -0700416 }
Chris Daltonbca46e22017-05-15 11:03:26 -0600417 lineMeshes.setVertices(lineVertexBuffer, lineVertexOffset, firstLineVertex);
Chris Daltonff926502017-05-03 14:36:54 -0400418
Brian Salomon2a55c8e2017-05-09 09:57:19 -0400419 // We can get line vertices from path moveTos with no actual segments and thus no index
420 // count. We assert that indexed draws contain a positive index count, so bail here in
421 // that case.
422 if (!fIsIndexed || lineIndexOffset) {
423 target->draw(lineGP.get(), this->pipeline(), lineMeshes);
424 }
ethannicholas6536ae52016-05-02 12:16:49 -0700425 }
426
427 if (quadVertexOffset) {
Hal Canary144caf52016-11-07 17:57:18 -0500428 sk_sp<const GrGeometryProcessor> quadGP(MSAAQuadProcessor::Create(fViewMatrix));
ethannicholas6536ae52016-05-02 12:16:49 -0700429 SkASSERT(quadVertexStride == quadGP->getVertexStride());
430
431 const GrBuffer* quadVertexBuffer;
432 int firstQuadVertex;
bungeman06ca8ec2016-06-09 08:01:03 -0700433 MSAAQuadVertices::Vertex* quadVertices = (MSAAQuadVertices::Vertex*)
ethannicholas6536ae52016-05-02 12:16:49 -0700434 target->makeVertexSpace(quadVertexStride, quadVertexOffset, &quadVertexBuffer,
435 &firstQuadVertex);
436 memcpy(quadVertices, quads.vertices, quadVertexStride * quadVertexOffset);
Chris Daltonbca46e22017-05-15 11:03:26 -0600437 GrMesh quadMeshes(kTriangles_GrPrimitiveType);
ethannicholas6536ae52016-05-02 12:16:49 -0700438 if (fIsIndexed) {
439 const GrBuffer* quadIndexBuffer;
Chris Daltonbca46e22017-05-15 11:03:26 -0600440 int firstQuadIndex;
bungeman06ca8ec2016-06-09 08:01:03 -0700441 uint16_t* quadIndices = (uint16_t*) target->makeIndexSpace(quadIndexOffset,
442 &quadIndexBuffer,
Chris Daltonbca46e22017-05-15 11:03:26 -0600443 &firstQuadIndex);
ethannicholas6536ae52016-05-02 12:16:49 -0700444 memcpy(quadIndices, quads.indices, sizeof(uint16_t) * quadIndexOffset);
Chris Daltonbca46e22017-05-15 11:03:26 -0600445 quadMeshes.setIndexed(quadIndexBuffer, quadIndexOffset, firstQuadIndex);
ethannicholas6536ae52016-05-02 12:16:49 -0700446 }
Chris Daltonbca46e22017-05-15 11:03:26 -0600447 quadMeshes.setVertices(quadVertexBuffer, quadVertexOffset, firstQuadVertex);
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400448 target->draw(quadGP.get(), this->pipeline(), quadMeshes);
ethannicholas6536ae52016-05-02 12:16:49 -0700449 }
450 }
451
Brian Salomon25a88092016-12-01 09:36:50 -0500452 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomon780dad12016-12-15 18:08:40 -0500453 MSAAPathOp* that = t->cast<MSAAPathOp>();
ethannicholas6536ae52016-05-02 12:16:49 -0700454 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
Brian Salomon9e50f7b2017-03-06 12:02:34 -0500455 that->bounds(), caps)) {
ethannicholas6536ae52016-05-02 12:16:49 -0700456 return false;
457 }
458
Jim Van Verth9d01fbc2017-02-22 14:50:52 -0500459 if (this->bounds().intersects(that->bounds())) {
460 return false;
461 }
462
ethannicholas6536ae52016-05-02 12:16:49 -0700463 if (!fViewMatrix.cheapEqualTo(that->fViewMatrix)) {
464 return false;
465 }
466
Brian Salomon780dad12016-12-15 18:08:40 -0500467 // If we grow to include 2+ paths we will be indexed.
468 if (((fMaxLineVertices + that->fMaxLineVertices) > kMaxIndexedVertexCnt) ||
469 ((fMaxQuadVertices + that->fMaxQuadVertices) > kMaxIndexedVertexCnt)) {
ethannicholas6536ae52016-05-02 12:16:49 -0700470 return false;
471 }
472
bsalomon50c56a32016-06-30 12:05:32 -0700473 fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700474 this->joinBounds(*that);
ethannicholas6536ae52016-05-02 12:16:49 -0700475 fIsIndexed = true;
476 fMaxLineVertices += that->fMaxLineVertices;
477 fMaxQuadVertices += that->fMaxQuadVertices;
ethannicholas6536ae52016-05-02 12:16:49 -0700478 return true;
479 }
480
481 bool createGeom(MSAALineVertices& lines,
482 MSAAQuadVertices& quads,
483 const SkPath& path,
ethannicholas6536ae52016-05-02 12:16:49 -0700484 const SkMatrix& m,
485 SkColor color,
486 bool isIndexed) const {
487 {
Brian Salomon32ebaba2017-03-30 10:22:28 -0400488 const SkScalar tolerance = GrPathUtils::scaleToleranceToSrc(kTolerance, m,
489 path.getBounds());
ethannicholas6536ae52016-05-02 12:16:49 -0700490 uint16_t subpathIdxStart = (uint16_t) (lines.nextVertex - lines.vertices);
491
492 SkPoint pts[4];
493
494 bool first = true;
bsalomon8eb43e52016-09-21 07:47:34 -0700495 SkPath::Iter iter(path, true);
ethannicholas6536ae52016-05-02 12:16:49 -0700496
497 bool done = false;
498 while (!done) {
499 SkPath::Verb verb = iter.next(pts);
500 switch (verb) {
501 case SkPath::kMove_Verb:
502 if (!first) {
503 uint16_t currIdx = (uint16_t) (lines.nextVertex - lines.vertices);
504 subpathIdxStart = currIdx;
505 }
506 SkASSERT(lines.nextVertex < lines.verticesEnd);
507 *(lines.nextVertex++) = { pts[0], color };
508 break;
509 case SkPath::kLine_Verb:
510 if (isIndexed) {
511 uint16_t prevIdx = (uint16_t) (lines.nextVertex - lines.vertices - 1);
512 if (prevIdx > subpathIdxStart) {
513 append_contour_edge_indices(subpathIdxStart, prevIdx, lines);
514 }
515 }
516 SkASSERT(lines.nextVertex < lines.verticesEnd);
517 *(lines.nextVertex++) = { pts[1], color };
518 break;
519 case SkPath::kConic_Verb: {
520 SkScalar weight = iter.conicWeight();
521 SkAutoConicToQuads converter;
Brian Salomon32ebaba2017-03-30 10:22:28 -0400522 const SkPoint* quadPts = converter.computeQuads(pts, weight, tolerance);
ethannicholas6536ae52016-05-02 12:16:49 -0700523 for (int i = 0; i < converter.countQuads(); ++i) {
bungeman06ca8ec2016-06-09 08:01:03 -0700524 add_quad(lines, quads, quadPts + i * 2, color, isIndexed,
ethannicholas6536ae52016-05-02 12:16:49 -0700525 subpathIdxStart);
526 }
527 break;
528 }
529 case SkPath::kQuad_Verb: {
530 add_quad(lines, quads, pts, color, isIndexed, subpathIdxStart);
bungeman06ca8ec2016-06-09 08:01:03 -0700531 break;
ethannicholas6536ae52016-05-02 12:16:49 -0700532 }
533 case SkPath::kCubic_Verb: {
534 SkSTArray<15, SkPoint, true> quadPts;
Brian Salomon32ebaba2017-03-30 10:22:28 -0400535 GrPathUtils::convertCubicToQuads(pts, tolerance, &quadPts);
ethannicholas6536ae52016-05-02 12:16:49 -0700536 int count = quadPts.count();
537 for (int i = 0; i < count; i += 3) {
538 add_quad(lines, quads, &quadPts[i], color, isIndexed, subpathIdxStart);
539 }
540 break;
541 }
542 case SkPath::kClose_Verb:
543 break;
544 case SkPath::kDone_Verb:
545 done = true;
546 }
547 first = false;
548 }
549 }
550 return true;
551 }
552
Brian Salomon780dad12016-12-15 18:08:40 -0500553 // Lines and quads may render with an index buffer. However, we don't have any support for
554 // overflowing the max index.
555 static constexpr int kMaxIndexedVertexCnt = SK_MaxU16 / 3;
bsalomon50c56a32016-06-30 12:05:32 -0700556 struct PathInfo {
557 GrColor fColor;
558 SkPath fPath;
559 };
560
561 SkSTArray<1, PathInfo, true> fPaths;
ethannicholas6536ae52016-05-02 12:16:49 -0700562
563 SkMatrix fViewMatrix;
564 int fMaxLineVertices;
565 int fMaxQuadVertices;
ethannicholas6536ae52016-05-02 12:16:49 -0700566 bool fIsIndexed;
567
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400568 typedef GrLegacyMeshDrawOp INHERITED;
ethannicholas6536ae52016-05-02 12:16:49 -0700569};
570
Brian Osman11052242016-10-27 14:47:55 -0400571bool GrMSAAPathRenderer::internalDrawPath(GrRenderTargetContext* renderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -0500572 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500573 GrAAType aaType,
robertphillipsd2b6d642016-07-21 08:55:08 -0700574 const GrUserStencilSettings& userStencilSettings,
cdalton862cff32016-05-12 15:09:48 -0700575 const GrClip& clip,
ethannicholas6536ae52016-05-02 12:16:49 -0700576 const SkMatrix& viewMatrix,
bsalomon8acedde2016-06-24 10:42:16 -0700577 const GrShape& shape,
ethannicholas6536ae52016-05-02 12:16:49 -0700578 bool stencilOnly) {
bsalomon8acedde2016-06-24 10:42:16 -0700579 SkASSERT(shape.style().isSimpleFill());
580 SkPath path;
581 shape.asPath(&path);
582
Brian Salomon82f44312017-01-11 13:42:54 -0500583 const GrUserStencilSettings* passes[2] = {nullptr, nullptr};
cdalton93a379b2016-05-11 13:58:08 -0700584 bool reverse = false;
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 if (stencilOnly) {
588 passes[0] = &gDirectToStencil;
589 } else {
robertphillipsd2b6d642016-07-21 08:55:08 -0700590 passes[0] = &userStencilSettings;
ethannicholas6536ae52016-05-02 12:16:49 -0700591 }
ethannicholas6536ae52016-05-02 12:16:49 -0700592 } else {
593 switch (path.getFillType()) {
594 case SkPath::kInverseEvenOdd_FillType:
595 reverse = true;
596 // fallthrough
597 case SkPath::kEvenOdd_FillType:
598 passes[0] = &gEOStencilPass;
Brian Salomon82f44312017-01-11 13:42:54 -0500599 if (!stencilOnly) {
600 passes[1] = reverse ? &gInvEOColorPass : &gEOColorPass;
ethannicholas6536ae52016-05-02 12:16:49 -0700601 }
ethannicholas6536ae52016-05-02 12:16:49 -0700602 break;
603
604 case SkPath::kInverseWinding_FillType:
605 reverse = true;
606 // fallthrough
607 case SkPath::kWinding_FillType:
Brian Salomon15b25092017-05-08 11:10:53 -0400608 passes[0] = &gWindStencilPass;
Brian Salomon82f44312017-01-11 13:42:54 -0500609 if (!stencilOnly) {
610 passes[1] = reverse ? &gInvWindColorPass : &gWindColorPass;
ethannicholas6536ae52016-05-02 12:16:49 -0700611 }
612 break;
613 default:
614 SkDEBUGFAIL("Unknown path fFill!");
615 return false;
616 }
617 }
618
619 SkRect devBounds;
Brian Osman11052242016-10-27 14:47:55 -0400620 GetPathDevBounds(path, renderTargetContext->width(), renderTargetContext->height(), viewMatrix,
621 &devBounds);
ethannicholas6536ae52016-05-02 12:16:49 -0700622
Brian Salomond4652ca2017-01-13 12:11:36 -0500623 SkASSERT(passes[0]);
624 { // First pass
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400625 std::unique_ptr<GrLegacyMeshDrawOp> op =
Brian Salomond4652ca2017-01-13 12:11:36 -0500626 MSAAPathOp::Make(paint.getColor(), path, viewMatrix, devBounds);
627 if (!op) {
628 return false;
629 }
630 bool firstPassIsStencil = stencilOnly || passes[1];
631 // If we have a cover pass then we ignore the paint in the first pass and apply it in the
632 // second.
633 GrPaint::MoveOrNew firstPassPaint(paint, firstPassIsStencil);
634 if (firstPassIsStencil) {
635 firstPassPaint.paint().setXPFactory(GrDisableColorXPFactory::Get());
636 }
637 GrPipelineBuilder pipelineBuilder(std::move(firstPassPaint), aaType);
638 pipelineBuilder.setUserStencil(passes[0]);
Brian Salomone14bd802017-04-04 15:13:25 -0400639 renderTargetContext->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
Brian Salomon82f44312017-01-11 13:42:54 -0500640 }
robertphillips8e375302016-07-11 10:43:58 -0700641
Brian Salomon82f44312017-01-11 13:42:54 -0500642 if (passes[1]) {
643 SkRect bounds;
644 SkMatrix localMatrix = SkMatrix::I();
645 if (reverse) {
646 // 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);
ethannicholas6536ae52016-05-02 12:16:49 -0700652 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500653 if (!viewMatrix.invert(&localMatrix)) {
654 return false;
655 }
ethannicholas6536ae52016-05-02 12:16:49 -0700656 }
robertphillips976f5f02016-06-03 10:59:20 -0700657 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500658 bounds = path.getBounds();
ethannicholas6536ae52016-05-02 12:16:49 -0700659 }
Brian Salomon82f44312017-01-11 13:42:54 -0500660 const SkMatrix& viewM =
661 (reverse && viewMatrix.hasPerspective()) ? SkMatrix::I() : viewMatrix;
Brian Salomonac70f842017-05-08 10:43:33 -0400662 renderTargetContext->addDrawOp(
663 clip,
664 GrNonAAFillRectOp::Make(std::move(paint), viewM, bounds, nullptr, &localMatrix,
665 aaType, passes[1]));
ethannicholas6536ae52016-05-02 12:16:49 -0700666 }
667 return true;
668}
669
670bool GrMSAAPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Eric Karl5c779752017-05-08 12:02:07 -0700671 // If we aren't a single_pass_shape, we require stencil buffers.
672 if (!single_pass_shape(*args.fShape) && args.fCaps->avoidStencilBuffers()) {
673 return false;
674 }
bsalomonee432412016-06-27 07:18:18 -0700675 // This path renderer only fills and relies on MSAA for antialiasing. Stroked shapes are
676 // handled by passing on the original shape and letting the caller compute the stroked shape
677 // which will have a fill style.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500678 return args.fShape->style().isSimpleFill() && (GrAAType::kCoverage != args.fAAType);
ethannicholas6536ae52016-05-02 12:16:49 -0700679}
680
681bool GrMSAAPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400682 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700683 "GrMSAAPathRenderer::onDrawPath");
bsalomon8acedde2016-06-24 10:42:16 -0700684 SkTLazy<GrShape> tmpShape;
685 const GrShape* shape = args.fShape;
686 if (shape->style().applies()) {
bsalomon6663acf2016-05-10 09:14:17 -0700687 SkScalar styleScale = GrStyle::MatrixToScaleFactor(*args.fViewMatrix);
bsalomon8acedde2016-06-24 10:42:16 -0700688 tmpShape.init(args.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale));
689 shape = tmpShape.get();
ethannicholas6536ae52016-05-02 12:16:49 -0700690 }
Brian Osman11052242016-10-27 14:47:55 -0400691 return this->internalDrawPath(args.fRenderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -0500692 std::move(args.fPaint),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500693 args.fAAType,
robertphillipsd2b6d642016-07-21 08:55:08 -0700694 *args.fUserStencilSettings,
cdalton862cff32016-05-12 15:09:48 -0700695 *args.fClip,
ethannicholas6536ae52016-05-02 12:16:49 -0700696 *args.fViewMatrix,
bsalomon8acedde2016-06-24 10:42:16 -0700697 *shape,
ethannicholas6536ae52016-05-02 12:16:49 -0700698 false);
699}
700
701void GrMSAAPathRenderer::onStencilPath(const StencilPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400702 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700703 "GrMSAAPathRenderer::onStencilPath");
bsalomon8acedde2016-06-24 10:42:16 -0700704 SkASSERT(args.fShape->style().isSimpleFill());
705 SkASSERT(!args.fShape->mayBeInverseFilledAfterStyling());
robertphillips976f5f02016-06-03 10:59:20 -0700706
707 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500708 paint.setXPFactory(GrDisableColorXPFactory::Get());
robertphillips976f5f02016-06-03 10:59:20 -0700709
Brian Salomon82f44312017-01-11 13:42:54 -0500710 this->internalDrawPath(args.fRenderTargetContext, std::move(paint), args.fAAType,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500711 GrUserStencilSettings::kUnused, *args.fClip, *args.fViewMatrix,
712 *args.fShape, true);
ethannicholas6536ae52016-05-02 12:16:49 -0700713}
714
715///////////////////////////////////////////////////////////////////////////////////////////////////