blob: f49c8cd586b337e340ea87e6f9ed9b40b104b4ff [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"
robertphillips976f5f02016-06-03 10:59:20 -07009#include "GrAuditTrail.h"
robertphillips976f5f02016-06-03 10:59:20 -070010#include "GrClip.h"
ethannicholas6536ae52016-05-02 12:16:49 -070011#include "GrDefaultGeoProcFactory.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070012#include "GrFixedClip.h"
Brian Salomondad29232016-12-01 16:40:24 -050013#include "GrMesh.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050014#include "GrOpFlushState.h"
ethannicholas6536ae52016-05-02 12:16:49 -070015#include "GrPathStencilSettings.h"
16#include "GrPathUtils.h"
bsalomonbb243832016-07-22 07:10:19 -070017#include "GrPipelineBuilder.h"
Hal Canary95e3c052017-01-11 12:44:43 -050018#include "SkAutoMalloc.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"
Brian Salomonbaaf4392017-06-15 09:59:23 -040028#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
Brian Salomond3b65972017-03-22 12:05:03 -0400118 ~MSAAQuadProcessor() override {}
ethannicholas6536ae52016-05-02 12:16:49 -0700119
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 Salomond3ccb0a2017-04-03 10:38:00 -0400217class MSAAPathOp final : public GrLegacyMeshDrawOp {
ethannicholas6536ae52016-05-02 12:16:49 -0700218public:
Brian Salomon25a88092016-12-01 09:36:50 -0500219 DEFINE_OP_CLASS_ID
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400220 static std::unique_ptr<GrLegacyMeshDrawOp> Make(GrColor color, const SkPath& path,
221 const SkMatrix& viewMatrix,
222 const SkRect& devBounds) {
bsalomon50c56a32016-06-30 12:05:32 -0700223 int contourCount;
Brian Salomon780dad12016-12-15 18:08:40 -0500224 int maxLineVertices;
225 int maxQuadVertices;
Brian Salomon32ebaba2017-03-30 10:22:28 -0400226 ComputeWorstCasePointCount(path, viewMatrix, &contourCount, &maxLineVertices,
227 &maxQuadVertices);
Brian Salomon780dad12016-12-15 18:08:40 -0500228 bool isIndexed = contourCount > 1;
229 if (isIndexed &&
230 (maxLineVertices > kMaxIndexedVertexCnt || maxQuadVertices > kMaxIndexedVertexCnt)) {
231 return nullptr;
232 }
233
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400234 return std::unique_ptr<GrLegacyMeshDrawOp>(new MSAAPathOp(
Brian Salomonf8334782017-01-03 09:42:58 -0500235 color, path, viewMatrix, devBounds, maxLineVertices, maxQuadVertices, isIndexed));
ethannicholas6536ae52016-05-02 12:16:49 -0700236 }
237
Brian Salomon780dad12016-12-15 18:08:40 -0500238 const char* name() const override { return "MSAAPathOp"; }
ethannicholas6536ae52016-05-02 12:16:49 -0700239
Brian Salomon7c3e7182016-12-01 09:35:30 -0500240 SkString dumpInfo() const override {
241 SkString string;
242 string.appendf("Indexed: %d\n", fIsIndexed);
243 for (const auto& path : fPaths) {
244 string.appendf("Color: 0x%08x\n", path.fColor);
245 }
246 string.append(DumpPipelineInfo(*this->pipeline()));
247 string.append(INHERITED::dumpInfo());
248 return string;
249 }
250
Brian Salomon780dad12016-12-15 18:08:40 -0500251private:
252 MSAAPathOp(GrColor color, const SkPath& path, const SkMatrix& viewMatrix,
253 const SkRect& devBounds, int maxLineVertices, int maxQuadVertices, bool isIndexed)
254 : INHERITED(ClassID())
255 , fViewMatrix(viewMatrix)
256 , fMaxLineVertices(maxLineVertices)
257 , fMaxQuadVertices(maxQuadVertices)
258 , fIsIndexed(isIndexed) {
259 fPaths.emplace_back(PathInfo{color, path});
260 this->setBounds(devBounds, HasAABloat::kNo, IsZeroArea::kNo);
ethannicholas6536ae52016-05-02 12:16:49 -0700261 }
262
Brian Salomona811b122017-03-30 08:21:32 -0400263 void getProcessorAnalysisInputs(GrProcessorAnalysisColor* color,
264 GrProcessorAnalysisCoverage* coverage) const override {
Brian Salomonc0b642c2017-03-27 13:09:36 -0400265 color->setToConstant(fPaths[0].fColor);
Brian Salomona811b122017-03-30 08:21:32 -0400266 *coverage = GrProcessorAnalysisCoverage::kNone;
Brian Salomon92aee3d2016-12-21 09:20:25 -0500267 }
268
Brian Salomone7d30482017-03-29 12:09:15 -0400269 void applyPipelineOptimizations(const PipelineOptimizations& optimizations) override {
Brian Salomon92aee3d2016-12-21 09:20:25 -0500270 optimizations.getOverrideColorIfSet(&fPaths[0].fColor);
ethannicholas6536ae52016-05-02 12:16:49 -0700271 }
272
Brian Salomon32ebaba2017-03-30 10:22:28 -0400273 static void ComputeWorstCasePointCount(const SkPath& path, const SkMatrix& m, int* subpaths,
Brian Salomon780dad12016-12-15 18:08:40 -0500274 int* outLinePointCount, int* outQuadPointCount) {
Brian Salomon32ebaba2017-03-30 10:22:28 -0400275 SkScalar tolerance = GrPathUtils::scaleToleranceToSrc(kTolerance, m, path.getBounds());
ethannicholas6536ae52016-05-02 12:16:49 -0700276 int linePointCount = 0;
277 int quadPointCount = 0;
278 *subpaths = 1;
279
280 bool first = true;
281
bsalomon8eb43e52016-09-21 07:47:34 -0700282 SkPath::Iter iter(path, true);
ethannicholas6536ae52016-05-02 12:16:49 -0700283 SkPath::Verb verb;
284
285 SkPoint pts[4];
286 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
287 switch (verb) {
288 case SkPath::kLine_Verb:
289 linePointCount += 1;
290 break;
291 case SkPath::kConic_Verb: {
292 SkScalar weight = iter.conicWeight();
293 SkAutoConicToQuads converter;
Brian Salomon32ebaba2017-03-30 10:22:28 -0400294 converter.computeQuads(pts, weight, tolerance);
ethannicholas6536ae52016-05-02 12:16:49 -0700295 int quadPts = converter.countQuads();
296 linePointCount += quadPts;
297 quadPointCount += 3 * quadPts;
298 }
299 case SkPath::kQuad_Verb:
300 linePointCount += 1;
301 quadPointCount += 3;
302 break;
303 case SkPath::kCubic_Verb: {
304 SkSTArray<15, SkPoint, true> quadPts;
Brian Salomon32ebaba2017-03-30 10:22:28 -0400305 GrPathUtils::convertCubicToQuads(pts, tolerance, &quadPts);
ethannicholas6536ae52016-05-02 12:16:49 -0700306 int count = quadPts.count();
307 linePointCount += count / 3;
308 quadPointCount += count;
309 break;
310 }
311 case SkPath::kMove_Verb:
312 linePointCount += 1;
313 if (!first) {
314 ++(*subpaths);
315 }
316 break;
317 default:
318 break;
319 }
320 first = false;
321 }
322 *outLinePointCount = linePointCount;
323 *outQuadPointCount = quadPointCount;
324 }
325
326 void onPrepareDraws(Target* target) const override {
ethannicholas6536ae52016-05-02 12:16:49 -0700327 if (fMaxLineVertices == 0) {
328 SkASSERT(fMaxQuadVertices == 0);
329 return;
330 }
331
Chris Dalton3809bab2017-06-13 10:55:06 -0600332 GrPrimitiveType primitiveType = fIsIndexed ? GrPrimitiveType::kTriangles
333 : GrPrimitiveType::kTriangleFan;
ethannicholas6536ae52016-05-02 12:16:49 -0700334
335 // allocate vertex / index buffers
336 const GrBuffer* lineVertexBuffer;
337 int firstLineVertex;
338 MSAALineVertices lines;
Chris Dalton1d616352017-05-31 12:51:23 -0600339 int lineVertexStride = sizeof(MSAALineVertices::Vertex);
bungeman06ca8ec2016-06-09 08:01:03 -0700340 lines.vertices = (MSAALineVertices::Vertex*) target->makeVertexSpace(lineVertexStride,
ethannicholas6536ae52016-05-02 12:16:49 -0700341 fMaxLineVertices,
bungeman06ca8ec2016-06-09 08:01:03 -0700342 &lineVertexBuffer,
ethannicholas6536ae52016-05-02 12:16:49 -0700343 &firstLineVertex);
344 if (!lines.vertices) {
345 SkDebugf("Could not allocate vertices\n");
346 return;
347 }
348 lines.nextVertex = lines.vertices;
349 SkDEBUGCODE(lines.verticesEnd = lines.vertices + fMaxLineVertices;)
350
351 MSAAQuadVertices quads;
Chris Dalton1d616352017-05-31 12:51:23 -0600352 int quadVertexStride = sizeof(MSAAQuadVertices::Vertex);
Hal Canary95e3c052017-01-11 12:44:43 -0500353 SkAutoMalloc quadVertexPtr(fMaxQuadVertices * quadVertexStride);
ethannicholas6536ae52016-05-02 12:16:49 -0700354 quads.vertices = (MSAAQuadVertices::Vertex*) quadVertexPtr.get();
355 quads.nextVertex = quads.vertices;
356 SkDEBUGCODE(quads.verticesEnd = quads.vertices + fMaxQuadVertices;)
357
358 const GrBuffer* lineIndexBuffer = nullptr;
359 int firstLineIndex;
360 if (fIsIndexed) {
Brian Salomon780dad12016-12-15 18:08:40 -0500361 lines.indices =
362 target->makeIndexSpace(3 * fMaxLineVertices, &lineIndexBuffer, &firstLineIndex);
ethannicholas6536ae52016-05-02 12:16:49 -0700363 if (!lines.indices) {
364 SkDebugf("Could not allocate indices\n");
365 return;
366 }
367 lines.nextIndex = lines.indices;
368 } else {
369 lines.indices = nullptr;
370 lines.nextIndex = nullptr;
371 }
372
373 SkAutoFree quadIndexPtr;
374 if (fIsIndexed) {
Brian Salomon780dad12016-12-15 18:08:40 -0500375 quads.indices = (uint16_t*)sk_malloc_throw(3 * fMaxQuadVertices * sizeof(uint16_t));
Hal Canary95e3c052017-01-11 12:44:43 -0500376 quadIndexPtr.reset(quads.indices);
ethannicholas6536ae52016-05-02 12:16:49 -0700377 quads.nextIndex = quads.indices;
378 } else {
379 quads.indices = nullptr;
380 quads.nextIndex = nullptr;
381 }
ethannicholas6536ae52016-05-02 12:16:49 -0700382 // 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 if (!this->createGeom(lines,
386 quads,
bsalomon50c56a32016-06-30 12:05:32 -0700387 pathInfo.fPath,
ethannicholas6536ae52016-05-02 12:16:49 -0700388 fViewMatrix,
bsalomon50c56a32016-06-30 12:05:32 -0700389 pathInfo.fColor,
ethannicholas6536ae52016-05-02 12:16:49 -0700390 fIsIndexed)) {
391 return;
392 }
393 }
394 int lineVertexOffset = (int) (lines.nextVertex - lines.vertices);
395 int lineIndexOffset = (int) (lines.nextIndex - lines.indices);
Brian Salomon780dad12016-12-15 18:08:40 -0500396 SkASSERT(lineVertexOffset <= fMaxLineVertices && lineIndexOffset <= 3 * fMaxLineVertices);
ethannicholas6536ae52016-05-02 12:16:49 -0700397 int quadVertexOffset = (int) (quads.nextVertex - quads.vertices);
398 int quadIndexOffset = (int) (quads.nextIndex - quads.indices);
Brian Salomon780dad12016-12-15 18:08:40 -0500399 SkASSERT(quadVertexOffset <= fMaxQuadVertices && quadIndexOffset <= 3 * fMaxQuadVertices);
ethannicholas6536ae52016-05-02 12:16:49 -0700400
401 if (lineVertexOffset) {
bungeman06ca8ec2016-06-09 08:01:03 -0700402 sk_sp<GrGeometryProcessor> lineGP;
ethannicholas6536ae52016-05-02 12:16:49 -0700403 {
404 using namespace GrDefaultGeoProcFactory;
Brian Salomon3de0aee2017-01-29 09:34:17 -0500405 lineGP = GrDefaultGeoProcFactory::Make(Color(Color::kPremulGrColorAttribute_Type),
Brian Salomon8c852be2017-01-04 10:44:42 -0500406 Coverage::kSolid_Type,
bungeman06ca8ec2016-06-09 08:01:03 -0700407 LocalCoords(LocalCoords::kUnused_Type),
408 fViewMatrix);
ethannicholas6536ae52016-05-02 12:16:49 -0700409 }
410 SkASSERT(lineVertexStride == lineGP->getVertexStride());
411
Chris Daltonbca46e22017-05-15 11:03:26 -0600412 GrMesh lineMeshes(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -0600413 if (!fIsIndexed) {
Chris Dalton1d616352017-05-31 12:51:23 -0600414 lineMeshes.setNonIndexedNonInstanced(lineVertexOffset);
Chris Dalton114a3c02017-05-26 15:17:19 -0600415 } else {
416 lineMeshes.setIndexed(lineIndexBuffer, lineIndexOffset, firstLineIndex,
417 0, lineVertexOffset - 1);
ethannicholas6536ae52016-05-02 12:16:49 -0700418 }
Chris Dalton114a3c02017-05-26 15:17:19 -0600419 lineMeshes.setVertexData(lineVertexBuffer, firstLineVertex);
Chris Daltonff926502017-05-03 14:36:54 -0400420
Brian Salomon2a55c8e2017-05-09 09:57:19 -0400421 // We can get line vertices from path moveTos with no actual segments and thus no index
422 // count. We assert that indexed draws contain a positive index count, so bail here in
423 // that case.
424 if (!fIsIndexed || lineIndexOffset) {
425 target->draw(lineGP.get(), this->pipeline(), lineMeshes);
426 }
ethannicholas6536ae52016-05-02 12:16:49 -0700427 }
428
429 if (quadVertexOffset) {
Hal Canary144caf52016-11-07 17:57:18 -0500430 sk_sp<const GrGeometryProcessor> quadGP(MSAAQuadProcessor::Create(fViewMatrix));
ethannicholas6536ae52016-05-02 12:16:49 -0700431 SkASSERT(quadVertexStride == quadGP->getVertexStride());
432
433 const GrBuffer* quadVertexBuffer;
434 int firstQuadVertex;
bungeman06ca8ec2016-06-09 08:01:03 -0700435 MSAAQuadVertices::Vertex* quadVertices = (MSAAQuadVertices::Vertex*)
ethannicholas6536ae52016-05-02 12:16:49 -0700436 target->makeVertexSpace(quadVertexStride, quadVertexOffset, &quadVertexBuffer,
437 &firstQuadVertex);
438 memcpy(quadVertices, quads.vertices, quadVertexStride * quadVertexOffset);
Chris Dalton3809bab2017-06-13 10:55:06 -0600439 GrMesh quadMeshes(GrPrimitiveType::kTriangles);
Chris Dalton114a3c02017-05-26 15:17:19 -0600440 if (!fIsIndexed) {
Chris Dalton1d616352017-05-31 12:51:23 -0600441 quadMeshes.setNonIndexedNonInstanced(quadVertexOffset);
Chris Dalton114a3c02017-05-26 15:17:19 -0600442 } else {
ethannicholas6536ae52016-05-02 12:16:49 -0700443 const GrBuffer* quadIndexBuffer;
Chris Daltonbca46e22017-05-15 11:03:26 -0600444 int firstQuadIndex;
bungeman06ca8ec2016-06-09 08:01:03 -0700445 uint16_t* quadIndices = (uint16_t*) target->makeIndexSpace(quadIndexOffset,
446 &quadIndexBuffer,
Chris Daltonbca46e22017-05-15 11:03:26 -0600447 &firstQuadIndex);
ethannicholas6536ae52016-05-02 12:16:49 -0700448 memcpy(quadIndices, quads.indices, sizeof(uint16_t) * quadIndexOffset);
Chris Dalton114a3c02017-05-26 15:17:19 -0600449 quadMeshes.setIndexed(quadIndexBuffer, quadIndexOffset, firstQuadIndex,
450 0, quadVertexOffset - 1);
ethannicholas6536ae52016-05-02 12:16:49 -0700451 }
Chris Dalton114a3c02017-05-26 15:17:19 -0600452 quadMeshes.setVertexData(quadVertexBuffer, firstQuadVertex);
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400453 target->draw(quadGP.get(), this->pipeline(), quadMeshes);
ethannicholas6536ae52016-05-02 12:16:49 -0700454 }
455 }
456
Brian Salomon25a88092016-12-01 09:36:50 -0500457 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomon780dad12016-12-15 18:08:40 -0500458 MSAAPathOp* that = t->cast<MSAAPathOp>();
ethannicholas6536ae52016-05-02 12:16:49 -0700459 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
Brian Salomon9e50f7b2017-03-06 12:02:34 -0500460 that->bounds(), caps)) {
ethannicholas6536ae52016-05-02 12:16:49 -0700461 return false;
462 }
463
Jim Van Verth9d01fbc2017-02-22 14:50:52 -0500464 if (this->bounds().intersects(that->bounds())) {
465 return false;
466 }
467
ethannicholas6536ae52016-05-02 12:16:49 -0700468 if (!fViewMatrix.cheapEqualTo(that->fViewMatrix)) {
469 return false;
470 }
471
Brian Salomon780dad12016-12-15 18:08:40 -0500472 // If we grow to include 2+ paths we will be indexed.
473 if (((fMaxLineVertices + that->fMaxLineVertices) > kMaxIndexedVertexCnt) ||
474 ((fMaxQuadVertices + that->fMaxQuadVertices) > kMaxIndexedVertexCnt)) {
ethannicholas6536ae52016-05-02 12:16:49 -0700475 return false;
476 }
477
bsalomon50c56a32016-06-30 12:05:32 -0700478 fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700479 this->joinBounds(*that);
ethannicholas6536ae52016-05-02 12:16:49 -0700480 fIsIndexed = true;
481 fMaxLineVertices += that->fMaxLineVertices;
482 fMaxQuadVertices += that->fMaxQuadVertices;
ethannicholas6536ae52016-05-02 12:16:49 -0700483 return true;
484 }
485
486 bool createGeom(MSAALineVertices& lines,
487 MSAAQuadVertices& quads,
488 const SkPath& path,
ethannicholas6536ae52016-05-02 12:16:49 -0700489 const SkMatrix& m,
490 SkColor color,
491 bool isIndexed) const {
492 {
Brian Salomon32ebaba2017-03-30 10:22:28 -0400493 const SkScalar tolerance = GrPathUtils::scaleToleranceToSrc(kTolerance, m,
494 path.getBounds());
ethannicholas6536ae52016-05-02 12:16:49 -0700495 uint16_t subpathIdxStart = (uint16_t) (lines.nextVertex - lines.vertices);
496
497 SkPoint pts[4];
498
499 bool first = true;
bsalomon8eb43e52016-09-21 07:47:34 -0700500 SkPath::Iter iter(path, true);
ethannicholas6536ae52016-05-02 12:16:49 -0700501
502 bool done = false;
503 while (!done) {
504 SkPath::Verb verb = iter.next(pts);
505 switch (verb) {
506 case SkPath::kMove_Verb:
507 if (!first) {
508 uint16_t currIdx = (uint16_t) (lines.nextVertex - lines.vertices);
509 subpathIdxStart = currIdx;
510 }
511 SkASSERT(lines.nextVertex < lines.verticesEnd);
512 *(lines.nextVertex++) = { pts[0], color };
513 break;
514 case SkPath::kLine_Verb:
515 if (isIndexed) {
516 uint16_t prevIdx = (uint16_t) (lines.nextVertex - lines.vertices - 1);
517 if (prevIdx > subpathIdxStart) {
518 append_contour_edge_indices(subpathIdxStart, prevIdx, lines);
519 }
520 }
521 SkASSERT(lines.nextVertex < lines.verticesEnd);
522 *(lines.nextVertex++) = { pts[1], color };
523 break;
524 case SkPath::kConic_Verb: {
525 SkScalar weight = iter.conicWeight();
526 SkAutoConicToQuads converter;
Brian Salomon32ebaba2017-03-30 10:22:28 -0400527 const SkPoint* quadPts = converter.computeQuads(pts, weight, tolerance);
ethannicholas6536ae52016-05-02 12:16:49 -0700528 for (int i = 0; i < converter.countQuads(); ++i) {
bungeman06ca8ec2016-06-09 08:01:03 -0700529 add_quad(lines, quads, quadPts + i * 2, color, isIndexed,
ethannicholas6536ae52016-05-02 12:16:49 -0700530 subpathIdxStart);
531 }
532 break;
533 }
534 case SkPath::kQuad_Verb: {
535 add_quad(lines, quads, pts, color, isIndexed, subpathIdxStart);
bungeman06ca8ec2016-06-09 08:01:03 -0700536 break;
ethannicholas6536ae52016-05-02 12:16:49 -0700537 }
538 case SkPath::kCubic_Verb: {
539 SkSTArray<15, SkPoint, true> quadPts;
Brian Salomon32ebaba2017-03-30 10:22:28 -0400540 GrPathUtils::convertCubicToQuads(pts, tolerance, &quadPts);
ethannicholas6536ae52016-05-02 12:16:49 -0700541 int count = quadPts.count();
542 for (int i = 0; i < count; i += 3) {
543 add_quad(lines, quads, &quadPts[i], color, isIndexed, subpathIdxStart);
544 }
545 break;
546 }
547 case SkPath::kClose_Verb:
548 break;
549 case SkPath::kDone_Verb:
550 done = true;
551 }
552 first = false;
553 }
554 }
555 return true;
556 }
557
Brian Salomon780dad12016-12-15 18:08:40 -0500558 // Lines and quads may render with an index buffer. However, we don't have any support for
559 // overflowing the max index.
560 static constexpr int kMaxIndexedVertexCnt = SK_MaxU16 / 3;
bsalomon50c56a32016-06-30 12:05:32 -0700561 struct PathInfo {
562 GrColor fColor;
563 SkPath fPath;
564 };
565
566 SkSTArray<1, PathInfo, true> fPaths;
ethannicholas6536ae52016-05-02 12:16:49 -0700567
568 SkMatrix fViewMatrix;
569 int fMaxLineVertices;
570 int fMaxQuadVertices;
ethannicholas6536ae52016-05-02 12:16:49 -0700571 bool fIsIndexed;
572
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400573 typedef GrLegacyMeshDrawOp INHERITED;
ethannicholas6536ae52016-05-02 12:16:49 -0700574};
575
Brian Osman11052242016-10-27 14:47:55 -0400576bool GrMSAAPathRenderer::internalDrawPath(GrRenderTargetContext* renderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -0500577 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500578 GrAAType aaType,
robertphillipsd2b6d642016-07-21 08:55:08 -0700579 const GrUserStencilSettings& userStencilSettings,
cdalton862cff32016-05-12 15:09:48 -0700580 const GrClip& clip,
ethannicholas6536ae52016-05-02 12:16:49 -0700581 const SkMatrix& viewMatrix,
bsalomon8acedde2016-06-24 10:42:16 -0700582 const GrShape& shape,
ethannicholas6536ae52016-05-02 12:16:49 -0700583 bool stencilOnly) {
bsalomon8acedde2016-06-24 10:42:16 -0700584 SkASSERT(shape.style().isSimpleFill());
585 SkPath path;
586 shape.asPath(&path);
587
Brian Salomon82f44312017-01-11 13:42:54 -0500588 const GrUserStencilSettings* passes[2] = {nullptr, nullptr};
cdalton93a379b2016-05-11 13:58:08 -0700589 bool reverse = false;
ethannicholas6536ae52016-05-02 12:16:49 -0700590
bsalomon8acedde2016-06-24 10:42:16 -0700591 if (single_pass_shape(shape)) {
ethannicholas6536ae52016-05-02 12:16:49 -0700592 if (stencilOnly) {
593 passes[0] = &gDirectToStencil;
594 } else {
robertphillipsd2b6d642016-07-21 08:55:08 -0700595 passes[0] = &userStencilSettings;
ethannicholas6536ae52016-05-02 12:16:49 -0700596 }
ethannicholas6536ae52016-05-02 12:16:49 -0700597 } else {
598 switch (path.getFillType()) {
599 case SkPath::kInverseEvenOdd_FillType:
600 reverse = true;
601 // fallthrough
602 case SkPath::kEvenOdd_FillType:
603 passes[0] = &gEOStencilPass;
Brian Salomon82f44312017-01-11 13:42:54 -0500604 if (!stencilOnly) {
605 passes[1] = reverse ? &gInvEOColorPass : &gEOColorPass;
ethannicholas6536ae52016-05-02 12:16:49 -0700606 }
ethannicholas6536ae52016-05-02 12:16:49 -0700607 break;
608
609 case SkPath::kInverseWinding_FillType:
610 reverse = true;
611 // fallthrough
612 case SkPath::kWinding_FillType:
Brian Salomon15b25092017-05-08 11:10:53 -0400613 passes[0] = &gWindStencilPass;
Brian Salomon82f44312017-01-11 13:42:54 -0500614 if (!stencilOnly) {
615 passes[1] = reverse ? &gInvWindColorPass : &gWindColorPass;
ethannicholas6536ae52016-05-02 12:16:49 -0700616 }
617 break;
618 default:
619 SkDEBUGFAIL("Unknown path fFill!");
620 return false;
621 }
622 }
623
624 SkRect devBounds;
Brian Osman11052242016-10-27 14:47:55 -0400625 GetPathDevBounds(path, renderTargetContext->width(), renderTargetContext->height(), viewMatrix,
626 &devBounds);
ethannicholas6536ae52016-05-02 12:16:49 -0700627
Brian Salomond4652ca2017-01-13 12:11:36 -0500628 SkASSERT(passes[0]);
629 { // First pass
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400630 std::unique_ptr<GrLegacyMeshDrawOp> op =
Brian Salomond4652ca2017-01-13 12:11:36 -0500631 MSAAPathOp::Make(paint.getColor(), path, viewMatrix, devBounds);
632 if (!op) {
633 return false;
634 }
635 bool firstPassIsStencil = stencilOnly || passes[1];
636 // If we have a cover pass then we ignore the paint in the first pass and apply it in the
637 // second.
638 GrPaint::MoveOrNew firstPassPaint(paint, firstPassIsStencil);
639 if (firstPassIsStencil) {
640 firstPassPaint.paint().setXPFactory(GrDisableColorXPFactory::Get());
641 }
642 GrPipelineBuilder pipelineBuilder(std::move(firstPassPaint), aaType);
643 pipelineBuilder.setUserStencil(passes[0]);
Brian Salomone14bd802017-04-04 15:13:25 -0400644 renderTargetContext->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
Brian Salomon82f44312017-01-11 13:42:54 -0500645 }
robertphillips8e375302016-07-11 10:43:58 -0700646
Brian Salomon82f44312017-01-11 13:42:54 -0500647 if (passes[1]) {
648 SkRect bounds;
649 SkMatrix localMatrix = SkMatrix::I();
650 if (reverse) {
651 // draw over the dev bounds (which will be the whole dst surface for inv fill).
652 bounds = devBounds;
653 SkMatrix vmi;
654 // mapRect through persp matrix may not be correct
655 if (!viewMatrix.hasPerspective() && viewMatrix.invert(&vmi)) {
656 vmi.mapRect(&bounds);
ethannicholas6536ae52016-05-02 12:16:49 -0700657 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500658 if (!viewMatrix.invert(&localMatrix)) {
659 return false;
660 }
ethannicholas6536ae52016-05-02 12:16:49 -0700661 }
robertphillips976f5f02016-06-03 10:59:20 -0700662 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500663 bounds = path.getBounds();
ethannicholas6536ae52016-05-02 12:16:49 -0700664 }
Brian Salomon82f44312017-01-11 13:42:54 -0500665 const SkMatrix& viewM =
666 (reverse && viewMatrix.hasPerspective()) ? SkMatrix::I() : viewMatrix;
Brian Salomonac70f842017-05-08 10:43:33 -0400667 renderTargetContext->addDrawOp(
668 clip,
Brian Salomonbaaf4392017-06-15 09:59:23 -0400669 GrRectOpFactory::MakeNonAAFillWithLocalMatrix(std::move(paint), viewM, localMatrix,
670 bounds, aaType, passes[1]));
ethannicholas6536ae52016-05-02 12:16:49 -0700671 }
672 return true;
673}
674
675bool GrMSAAPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Eric Karl5c779752017-05-08 12:02:07 -0700676 // If we aren't a single_pass_shape, we require stencil buffers.
677 if (!single_pass_shape(*args.fShape) && args.fCaps->avoidStencilBuffers()) {
678 return false;
679 }
bsalomonee432412016-06-27 07:18:18 -0700680 // This path renderer only fills and relies on MSAA for antialiasing. Stroked shapes are
681 // handled by passing on the original shape and letting the caller compute the stroked shape
682 // which will have a fill style.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500683 return args.fShape->style().isSimpleFill() && (GrAAType::kCoverage != args.fAAType);
ethannicholas6536ae52016-05-02 12:16:49 -0700684}
685
686bool GrMSAAPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400687 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700688 "GrMSAAPathRenderer::onDrawPath");
bsalomon8acedde2016-06-24 10:42:16 -0700689 SkTLazy<GrShape> tmpShape;
690 const GrShape* shape = args.fShape;
691 if (shape->style().applies()) {
bsalomon6663acf2016-05-10 09:14:17 -0700692 SkScalar styleScale = GrStyle::MatrixToScaleFactor(*args.fViewMatrix);
bsalomon8acedde2016-06-24 10:42:16 -0700693 tmpShape.init(args.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale));
694 shape = tmpShape.get();
ethannicholas6536ae52016-05-02 12:16:49 -0700695 }
Brian Osman11052242016-10-27 14:47:55 -0400696 return this->internalDrawPath(args.fRenderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -0500697 std::move(args.fPaint),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500698 args.fAAType,
robertphillipsd2b6d642016-07-21 08:55:08 -0700699 *args.fUserStencilSettings,
cdalton862cff32016-05-12 15:09:48 -0700700 *args.fClip,
ethannicholas6536ae52016-05-02 12:16:49 -0700701 *args.fViewMatrix,
bsalomon8acedde2016-06-24 10:42:16 -0700702 *shape,
ethannicholas6536ae52016-05-02 12:16:49 -0700703 false);
704}
705
706void GrMSAAPathRenderer::onStencilPath(const StencilPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400707 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700708 "GrMSAAPathRenderer::onStencilPath");
bsalomon8acedde2016-06-24 10:42:16 -0700709 SkASSERT(args.fShape->style().isSimpleFill());
710 SkASSERT(!args.fShape->mayBeInverseFilledAfterStyling());
robertphillips976f5f02016-06-03 10:59:20 -0700711
712 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500713 paint.setXPFactory(GrDisableColorXPFactory::Get());
robertphillips976f5f02016-06-03 10:59:20 -0700714
Brian Salomon82f44312017-01-11 13:42:54 -0500715 this->internalDrawPath(args.fRenderTargetContext, std::move(paint), args.fAAType,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500716 GrUserStencilSettings::kUnused, *args.fClip, *args.fViewMatrix,
717 *args.fShape, true);
ethannicholas6536ae52016-05-02 12:16:49 -0700718}
719
720///////////////////////////////////////////////////////////////////////////////////////////////////