ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 1 | /* |
| 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" |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 9 | #include "GrAuditTrail.h" |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 10 | #include "GrClip.h" |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 11 | #include "GrDefaultGeoProcFactory.h" |
csmartdalton | 02fa32c | 2016-08-19 13:29:27 -0700 | [diff] [blame] | 12 | #include "GrFixedClip.h" |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 13 | #include "GrMesh.h" |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 14 | #include "GrOpFlushState.h" |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 15 | #include "GrPathStencilSettings.h" |
| 16 | #include "GrPathUtils.h" |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 17 | #include "GrPipelineBuilder.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 18 | #include "SkAutoMalloc.h" |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 19 | #include "SkGeometry.h" |
| 20 | #include "SkTraceEvent.h" |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 21 | #include "gl/GrGLVaryingHandler.h" |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 22 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 23 | #include "glsl/GrGLSLGeometryProcessor.h" |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 24 | #include "glsl/GrGLSLProgramDataManager.h" |
| 25 | #include "glsl/GrGLSLUtil.h" |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 26 | #include "glsl/GrGLSLVertexShaderBuilder.h" |
Brian Salomon | 8952743 | 2016-12-16 09:52:16 -0500 | [diff] [blame] | 27 | #include "ops/GrMeshDrawOp.h" |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 28 | #include "ops/GrRectOpFactory.h" |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 29 | |
| 30 | static const float kTolerance = 0.5f; |
| 31 | |
| 32 | //////////////////////////////////////////////////////////////////////////////// |
| 33 | // Helpers for drawPath |
| 34 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 35 | static inline bool single_pass_shape(const GrShape& shape) { |
| 36 | if (!shape.inverseFilled()) { |
| 37 | return shape.knownToBeConvex(); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 38 | } |
| 39 | return false; |
| 40 | } |
| 41 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 42 | GrPathRenderer::StencilSupport GrMSAAPathRenderer::onGetStencilSupport(const GrShape& shape) const { |
| 43 | if (single_pass_shape(shape)) { |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 44 | return GrPathRenderer::kNoRestriction_StencilSupport; |
| 45 | } else { |
| 46 | return GrPathRenderer::kStencilOnly_StencilSupport; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | struct 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 | |
| 64 | struct 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 | |
| 79 | static 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 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 87 | static inline void add_quad(MSAALineVertices& lines, MSAAQuadVertices& quads, const SkPoint pts[], |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 88 | 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 | |
| 112 | class MSAAQuadProcessor : public GrGeometryProcessor { |
| 113 | public: |
| 114 | static GrGeometryProcessor* Create(const SkMatrix& viewMatrix) { |
| 115 | return new MSAAQuadProcessor(viewMatrix); |
| 116 | } |
| 117 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 118 | ~MSAAQuadProcessor() override {} |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 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; } |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 126 | |
| 127 | class GLSLProcessor : public GrGLSLGeometryProcessor { |
| 128 | public: |
| 129 | GLSLProcessor(const GrGeometryProcessor& qpr) {} |
| 130 | |
| 131 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
| 132 | const MSAAQuadProcessor& qp = args.fGP.cast<MSAAQuadProcessor>(); |
| 133 | GrGLSLVertexBuilder* vsBuilder = args.fVertBuilder; |
| 134 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
| 135 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 136 | |
| 137 | // emit attributes |
| 138 | varyingHandler->emitAttributes(qp); |
| 139 | varyingHandler->addPassThroughAttribute(qp.inColor(), args.fOutputColor); |
| 140 | |
| 141 | GrGLSLVertToFrag uv(kVec2f_GrSLType); |
| 142 | varyingHandler->addVarying("uv", &uv, kHigh_GrSLPrecision); |
| 143 | vsBuilder->codeAppendf("%s = %s;", uv.vsOut(), qp.inUV()->fName); |
| 144 | |
| 145 | // Setup position |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 146 | this->setupPosition(vsBuilder, uniformHandler, gpArgs, qp.inPosition()->fName, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 147 | qp.viewMatrix(), &fViewMatrixUniform); |
| 148 | |
| 149 | // emit transforms |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 150 | this->emitTransforms(vsBuilder, varyingHandler, uniformHandler, gpArgs->fPositionVar, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 151 | qp.inPosition()->fName, SkMatrix::I(), |
| 152 | args.fFPCoordTransformHandler); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 153 | |
| 154 | GrGLSLPPFragmentBuilder* fsBuilder = args.fFragBuilder; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 155 | fsBuilder->codeAppendf("if (%s.x * %s.x >= %s.y) discard;", uv.fsIn(), uv.fsIn(), |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 156 | uv.fsIn()); |
| 157 | fsBuilder->codeAppendf("%s = vec4(1.0);", args.fOutputCoverage); |
| 158 | } |
| 159 | |
| 160 | static inline void GenKey(const GrGeometryProcessor& gp, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 161 | const GrShaderCaps&, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 162 | GrProcessorKeyBuilder* b) { |
| 163 | const MSAAQuadProcessor& qp = gp.cast<MSAAQuadProcessor>(); |
| 164 | uint32_t key = 0; |
| 165 | key |= qp.viewMatrix().hasPerspective() ? 0x1 : 0x0; |
| 166 | key |= qp.viewMatrix().isIdentity() ? 0x2: 0x0; |
| 167 | b->add32(key); |
| 168 | } |
| 169 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 170 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& gp, |
| 171 | FPCoordTransformIter&& transformIter) override { |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 172 | const MSAAQuadProcessor& qp = gp.cast<MSAAQuadProcessor>(); |
| 173 | if (!qp.viewMatrix().isIdentity()) { |
| 174 | float viewMatrix[3 * 3]; |
| 175 | GrGLSLGetMatrix<3>(viewMatrix, qp.viewMatrix()); |
| 176 | pdman.setMatrix3f(fViewMatrixUniform, viewMatrix); |
| 177 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 178 | this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 179 | } |
| 180 | |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 181 | private: |
| 182 | typedef GrGLSLGeometryProcessor INHERITED; |
| 183 | |
| 184 | UniformHandle fViewMatrixUniform; |
| 185 | }; |
| 186 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 187 | virtual void getGLSLProcessorKey(const GrShaderCaps& caps, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 188 | GrProcessorKeyBuilder* b) const override { |
| 189 | GLSLProcessor::GenKey(*this, caps, b); |
| 190 | } |
| 191 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 192 | virtual GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override { |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 193 | return new GLSLProcessor(*this); |
| 194 | } |
| 195 | |
| 196 | private: |
| 197 | MSAAQuadProcessor(const SkMatrix& viewMatrix) |
| 198 | : fViewMatrix(viewMatrix) { |
| 199 | this->initClassID<MSAAQuadProcessor>(); |
bsalomon | 6cb807b | 2016-08-17 11:33:39 -0700 | [diff] [blame] | 200 | 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); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 204 | this->setSampleShading(1.0f); |
| 205 | } |
| 206 | |
| 207 | const Attribute* fInPosition; |
| 208 | const Attribute* fInUV; |
| 209 | const Attribute* fInColor; |
| 210 | SkMatrix fViewMatrix; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 211 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame^] | 212 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 213 | |
| 214 | typedef GrGeometryProcessor INHERITED; |
| 215 | }; |
| 216 | |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 217 | class MSAAPathOp final : public GrLegacyMeshDrawOp { |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 218 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 219 | DEFINE_OP_CLASS_ID |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 220 | static std::unique_ptr<GrLegacyMeshDrawOp> Make(GrColor color, const SkPath& path, |
| 221 | const SkMatrix& viewMatrix, |
| 222 | const SkRect& devBounds) { |
bsalomon | 50c56a3 | 2016-06-30 12:05:32 -0700 | [diff] [blame] | 223 | int contourCount; |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 224 | int maxLineVertices; |
| 225 | int maxQuadVertices; |
Brian Salomon | 32ebaba | 2017-03-30 10:22:28 -0400 | [diff] [blame] | 226 | ComputeWorstCasePointCount(path, viewMatrix, &contourCount, &maxLineVertices, |
| 227 | &maxQuadVertices); |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 228 | bool isIndexed = contourCount > 1; |
| 229 | if (isIndexed && |
| 230 | (maxLineVertices > kMaxIndexedVertexCnt || maxQuadVertices > kMaxIndexedVertexCnt)) { |
| 231 | return nullptr; |
| 232 | } |
| 233 | |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 234 | return std::unique_ptr<GrLegacyMeshDrawOp>(new MSAAPathOp( |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 235 | color, path, viewMatrix, devBounds, maxLineVertices, maxQuadVertices, isIndexed)); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 238 | const char* name() const override { return "MSAAPathOp"; } |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 239 | |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 240 | 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 Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 251 | private: |
| 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); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 263 | void getProcessorAnalysisInputs(GrProcessorAnalysisColor* color, |
| 264 | GrProcessorAnalysisCoverage* coverage) const override { |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 265 | color->setToConstant(fPaths[0].fColor); |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 266 | *coverage = GrProcessorAnalysisCoverage::kNone; |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 267 | } |
| 268 | |
Brian Salomon | e7d3048 | 2017-03-29 12:09:15 -0400 | [diff] [blame] | 269 | void applyPipelineOptimizations(const PipelineOptimizations& optimizations) override { |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 270 | optimizations.getOverrideColorIfSet(&fPaths[0].fColor); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Brian Salomon | 32ebaba | 2017-03-30 10:22:28 -0400 | [diff] [blame] | 273 | static void ComputeWorstCasePointCount(const SkPath& path, const SkMatrix& m, int* subpaths, |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 274 | int* outLinePointCount, int* outQuadPointCount) { |
Brian Salomon | 32ebaba | 2017-03-30 10:22:28 -0400 | [diff] [blame] | 275 | SkScalar tolerance = GrPathUtils::scaleToleranceToSrc(kTolerance, m, path.getBounds()); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 276 | int linePointCount = 0; |
| 277 | int quadPointCount = 0; |
| 278 | *subpaths = 1; |
| 279 | |
| 280 | bool first = true; |
| 281 | |
bsalomon | 8eb43e5 | 2016-09-21 07:47:34 -0700 | [diff] [blame] | 282 | SkPath::Iter iter(path, true); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 283 | 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 Salomon | 32ebaba | 2017-03-30 10:22:28 -0400 | [diff] [blame] | 294 | converter.computeQuads(pts, weight, tolerance); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 295 | 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 Salomon | 32ebaba | 2017-03-30 10:22:28 -0400 | [diff] [blame] | 305 | GrPathUtils::convertCubicToQuads(pts, tolerance, &quadPts); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 306 | 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 { |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 327 | if (fMaxLineVertices == 0) { |
| 328 | SkASSERT(fMaxQuadVertices == 0); |
| 329 | return; |
| 330 | } |
| 331 | |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 332 | GrPrimitiveType primitiveType = fIsIndexed ? GrPrimitiveType::kTriangles |
| 333 | : GrPrimitiveType::kTriangleFan; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 334 | |
| 335 | // allocate vertex / index buffers |
| 336 | const GrBuffer* lineVertexBuffer; |
| 337 | int firstLineVertex; |
| 338 | MSAALineVertices lines; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 339 | int lineVertexStride = sizeof(MSAALineVertices::Vertex); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 340 | lines.vertices = (MSAALineVertices::Vertex*) target->makeVertexSpace(lineVertexStride, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 341 | fMaxLineVertices, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 342 | &lineVertexBuffer, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 343 | &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 Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 352 | int quadVertexStride = sizeof(MSAAQuadVertices::Vertex); |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 353 | SkAutoMalloc quadVertexPtr(fMaxQuadVertices * quadVertexStride); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 354 | 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 Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 361 | lines.indices = |
| 362 | target->makeIndexSpace(3 * fMaxLineVertices, &lineIndexBuffer, &firstLineIndex); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 363 | 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 Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 375 | quads.indices = (uint16_t*)sk_malloc_throw(3 * fMaxQuadVertices * sizeof(uint16_t)); |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 376 | quadIndexPtr.reset(quads.indices); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 377 | quads.nextIndex = quads.indices; |
| 378 | } else { |
| 379 | quads.indices = nullptr; |
| 380 | quads.nextIndex = nullptr; |
| 381 | } |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 382 | // fill buffers |
bsalomon | 50c56a3 | 2016-06-30 12:05:32 -0700 | [diff] [blame] | 383 | for (int i = 0; i < fPaths.count(); i++) { |
| 384 | const PathInfo& pathInfo = fPaths[i]; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 385 | if (!this->createGeom(lines, |
| 386 | quads, |
bsalomon | 50c56a3 | 2016-06-30 12:05:32 -0700 | [diff] [blame] | 387 | pathInfo.fPath, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 388 | fViewMatrix, |
bsalomon | 50c56a3 | 2016-06-30 12:05:32 -0700 | [diff] [blame] | 389 | pathInfo.fColor, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 390 | fIsIndexed)) { |
| 391 | return; |
| 392 | } |
| 393 | } |
| 394 | int lineVertexOffset = (int) (lines.nextVertex - lines.vertices); |
| 395 | int lineIndexOffset = (int) (lines.nextIndex - lines.indices); |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 396 | SkASSERT(lineVertexOffset <= fMaxLineVertices && lineIndexOffset <= 3 * fMaxLineVertices); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 397 | int quadVertexOffset = (int) (quads.nextVertex - quads.vertices); |
| 398 | int quadIndexOffset = (int) (quads.nextIndex - quads.indices); |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 399 | SkASSERT(quadVertexOffset <= fMaxQuadVertices && quadIndexOffset <= 3 * fMaxQuadVertices); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 400 | |
| 401 | if (lineVertexOffset) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 402 | sk_sp<GrGeometryProcessor> lineGP; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 403 | { |
| 404 | using namespace GrDefaultGeoProcFactory; |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 405 | lineGP = GrDefaultGeoProcFactory::Make(Color(Color::kPremulGrColorAttribute_Type), |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 406 | Coverage::kSolid_Type, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 407 | LocalCoords(LocalCoords::kUnused_Type), |
| 408 | fViewMatrix); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 409 | } |
| 410 | SkASSERT(lineVertexStride == lineGP->getVertexStride()); |
| 411 | |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 412 | GrMesh lineMeshes(primitiveType); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 413 | if (!fIsIndexed) { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 414 | lineMeshes.setNonIndexedNonInstanced(lineVertexOffset); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 415 | } else { |
| 416 | lineMeshes.setIndexed(lineIndexBuffer, lineIndexOffset, firstLineIndex, |
| 417 | 0, lineVertexOffset - 1); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 418 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 419 | lineMeshes.setVertexData(lineVertexBuffer, firstLineVertex); |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 420 | |
Brian Salomon | 2a55c8e | 2017-05-09 09:57:19 -0400 | [diff] [blame] | 421 | // 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 | } |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | if (quadVertexOffset) { |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 430 | sk_sp<const GrGeometryProcessor> quadGP(MSAAQuadProcessor::Create(fViewMatrix)); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 431 | SkASSERT(quadVertexStride == quadGP->getVertexStride()); |
| 432 | |
| 433 | const GrBuffer* quadVertexBuffer; |
| 434 | int firstQuadVertex; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 435 | MSAAQuadVertices::Vertex* quadVertices = (MSAAQuadVertices::Vertex*) |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 436 | target->makeVertexSpace(quadVertexStride, quadVertexOffset, &quadVertexBuffer, |
| 437 | &firstQuadVertex); |
| 438 | memcpy(quadVertices, quads.vertices, quadVertexStride * quadVertexOffset); |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 439 | GrMesh quadMeshes(GrPrimitiveType::kTriangles); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 440 | if (!fIsIndexed) { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 441 | quadMeshes.setNonIndexedNonInstanced(quadVertexOffset); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 442 | } else { |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 443 | const GrBuffer* quadIndexBuffer; |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 444 | int firstQuadIndex; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 445 | uint16_t* quadIndices = (uint16_t*) target->makeIndexSpace(quadIndexOffset, |
| 446 | &quadIndexBuffer, |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 447 | &firstQuadIndex); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 448 | memcpy(quadIndices, quads.indices, sizeof(uint16_t) * quadIndexOffset); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 449 | quadMeshes.setIndexed(quadIndexBuffer, quadIndexOffset, firstQuadIndex, |
| 450 | 0, quadVertexOffset - 1); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 451 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 452 | quadMeshes.setVertexData(quadVertexBuffer, firstQuadVertex); |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 453 | target->draw(quadGP.get(), this->pipeline(), quadMeshes); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 457 | bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 458 | MSAAPathOp* that = t->cast<MSAAPathOp>(); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 459 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
Brian Salomon | 9e50f7b | 2017-03-06 12:02:34 -0500 | [diff] [blame] | 460 | that->bounds(), caps)) { |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 461 | return false; |
| 462 | } |
| 463 | |
Jim Van Verth | 9d01fbc | 2017-02-22 14:50:52 -0500 | [diff] [blame] | 464 | if (this->bounds().intersects(that->bounds())) { |
| 465 | return false; |
| 466 | } |
| 467 | |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 468 | if (!fViewMatrix.cheapEqualTo(that->fViewMatrix)) { |
| 469 | return false; |
| 470 | } |
| 471 | |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 472 | // If we grow to include 2+ paths we will be indexed. |
| 473 | if (((fMaxLineVertices + that->fMaxLineVertices) > kMaxIndexedVertexCnt) || |
| 474 | ((fMaxQuadVertices + that->fMaxQuadVertices) > kMaxIndexedVertexCnt)) { |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 475 | return false; |
| 476 | } |
| 477 | |
bsalomon | 50c56a3 | 2016-06-30 12:05:32 -0700 | [diff] [blame] | 478 | fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin()); |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 479 | this->joinBounds(*that); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 480 | fIsIndexed = true; |
| 481 | fMaxLineVertices += that->fMaxLineVertices; |
| 482 | fMaxQuadVertices += that->fMaxQuadVertices; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 483 | return true; |
| 484 | } |
| 485 | |
| 486 | bool createGeom(MSAALineVertices& lines, |
| 487 | MSAAQuadVertices& quads, |
| 488 | const SkPath& path, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 489 | const SkMatrix& m, |
| 490 | SkColor color, |
| 491 | bool isIndexed) const { |
| 492 | { |
Brian Salomon | 32ebaba | 2017-03-30 10:22:28 -0400 | [diff] [blame] | 493 | const SkScalar tolerance = GrPathUtils::scaleToleranceToSrc(kTolerance, m, |
| 494 | path.getBounds()); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 495 | uint16_t subpathIdxStart = (uint16_t) (lines.nextVertex - lines.vertices); |
| 496 | |
| 497 | SkPoint pts[4]; |
| 498 | |
| 499 | bool first = true; |
bsalomon | 8eb43e5 | 2016-09-21 07:47:34 -0700 | [diff] [blame] | 500 | SkPath::Iter iter(path, true); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 501 | |
| 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 Salomon | 32ebaba | 2017-03-30 10:22:28 -0400 | [diff] [blame] | 527 | const SkPoint* quadPts = converter.computeQuads(pts, weight, tolerance); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 528 | for (int i = 0; i < converter.countQuads(); ++i) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 529 | add_quad(lines, quads, quadPts + i * 2, color, isIndexed, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 530 | subpathIdxStart); |
| 531 | } |
| 532 | break; |
| 533 | } |
| 534 | case SkPath::kQuad_Verb: { |
| 535 | add_quad(lines, quads, pts, color, isIndexed, subpathIdxStart); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 536 | break; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 537 | } |
| 538 | case SkPath::kCubic_Verb: { |
| 539 | SkSTArray<15, SkPoint, true> quadPts; |
Brian Salomon | 32ebaba | 2017-03-30 10:22:28 -0400 | [diff] [blame] | 540 | GrPathUtils::convertCubicToQuads(pts, tolerance, &quadPts); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 541 | 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 Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 558 | // 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; |
bsalomon | 50c56a3 | 2016-06-30 12:05:32 -0700 | [diff] [blame] | 561 | struct PathInfo { |
| 562 | GrColor fColor; |
| 563 | SkPath fPath; |
| 564 | }; |
| 565 | |
| 566 | SkSTArray<1, PathInfo, true> fPaths; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 567 | |
| 568 | SkMatrix fViewMatrix; |
| 569 | int fMaxLineVertices; |
| 570 | int fMaxQuadVertices; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 571 | bool fIsIndexed; |
| 572 | |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 573 | typedef GrLegacyMeshDrawOp INHERITED; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 574 | }; |
| 575 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 576 | bool GrMSAAPathRenderer::internalDrawPath(GrRenderTargetContext* renderTargetContext, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 577 | GrPaint&& paint, |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 578 | GrAAType aaType, |
robertphillips | d2b6d64 | 2016-07-21 08:55:08 -0700 | [diff] [blame] | 579 | const GrUserStencilSettings& userStencilSettings, |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 580 | const GrClip& clip, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 581 | const SkMatrix& viewMatrix, |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 582 | const GrShape& shape, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 583 | bool stencilOnly) { |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 584 | SkASSERT(shape.style().isSimpleFill()); |
| 585 | SkPath path; |
| 586 | shape.asPath(&path); |
| 587 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 588 | const GrUserStencilSettings* passes[2] = {nullptr, nullptr}; |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 589 | bool reverse = false; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 590 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 591 | if (single_pass_shape(shape)) { |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 592 | if (stencilOnly) { |
| 593 | passes[0] = &gDirectToStencil; |
| 594 | } else { |
robertphillips | d2b6d64 | 2016-07-21 08:55:08 -0700 | [diff] [blame] | 595 | passes[0] = &userStencilSettings; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 596 | } |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 597 | } 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 Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 604 | if (!stencilOnly) { |
| 605 | passes[1] = reverse ? &gInvEOColorPass : &gEOColorPass; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 606 | } |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 607 | break; |
| 608 | |
| 609 | case SkPath::kInverseWinding_FillType: |
| 610 | reverse = true; |
| 611 | // fallthrough |
| 612 | case SkPath::kWinding_FillType: |
Brian Salomon | 15b2509 | 2017-05-08 11:10:53 -0400 | [diff] [blame] | 613 | passes[0] = &gWindStencilPass; |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 614 | if (!stencilOnly) { |
| 615 | passes[1] = reverse ? &gInvWindColorPass : &gWindColorPass; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 616 | } |
| 617 | break; |
| 618 | default: |
| 619 | SkDEBUGFAIL("Unknown path fFill!"); |
| 620 | return false; |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | SkRect devBounds; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 625 | GetPathDevBounds(path, renderTargetContext->width(), renderTargetContext->height(), viewMatrix, |
| 626 | &devBounds); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 627 | |
Brian Salomon | d4652ca | 2017-01-13 12:11:36 -0500 | [diff] [blame] | 628 | SkASSERT(passes[0]); |
| 629 | { // First pass |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 630 | std::unique_ptr<GrLegacyMeshDrawOp> op = |
Brian Salomon | d4652ca | 2017-01-13 12:11:36 -0500 | [diff] [blame] | 631 | 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 Salomon | e14bd80 | 2017-04-04 15:13:25 -0400 | [diff] [blame] | 644 | renderTargetContext->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op)); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 645 | } |
robertphillips | 8e37530 | 2016-07-11 10:43:58 -0700 | [diff] [blame] | 646 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 647 | 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); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 657 | } else { |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 658 | if (!viewMatrix.invert(&localMatrix)) { |
| 659 | return false; |
| 660 | } |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 661 | } |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 662 | } else { |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 663 | bounds = path.getBounds(); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 664 | } |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 665 | const SkMatrix& viewM = |
| 666 | (reverse && viewMatrix.hasPerspective()) ? SkMatrix::I() : viewMatrix; |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 667 | renderTargetContext->addDrawOp( |
| 668 | clip, |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 669 | GrRectOpFactory::MakeNonAAFillWithLocalMatrix(std::move(paint), viewM, localMatrix, |
| 670 | bounds, aaType, passes[1])); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 671 | } |
| 672 | return true; |
| 673 | } |
| 674 | |
| 675 | bool GrMSAAPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 676 | // 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 | } |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 680 | // 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 Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 683 | return args.fShape->style().isSimpleFill() && (GrAAType::kCoverage != args.fAAType); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | bool GrMSAAPathRenderer::onDrawPath(const DrawPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 687 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 688 | "GrMSAAPathRenderer::onDrawPath"); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 689 | SkTLazy<GrShape> tmpShape; |
| 690 | const GrShape* shape = args.fShape; |
| 691 | if (shape->style().applies()) { |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 692 | SkScalar styleScale = GrStyle::MatrixToScaleFactor(*args.fViewMatrix); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 693 | tmpShape.init(args.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale)); |
| 694 | shape = tmpShape.get(); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 695 | } |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 696 | return this->internalDrawPath(args.fRenderTargetContext, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 697 | std::move(args.fPaint), |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 698 | args.fAAType, |
robertphillips | d2b6d64 | 2016-07-21 08:55:08 -0700 | [diff] [blame] | 699 | *args.fUserStencilSettings, |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 700 | *args.fClip, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 701 | *args.fViewMatrix, |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 702 | *shape, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 703 | false); |
| 704 | } |
| 705 | |
| 706 | void GrMSAAPathRenderer::onStencilPath(const StencilPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 707 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 708 | "GrMSAAPathRenderer::onStencilPath"); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 709 | SkASSERT(args.fShape->style().isSimpleFill()); |
| 710 | SkASSERT(!args.fShape->mayBeInverseFilledAfterStyling()); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 711 | |
| 712 | GrPaint paint; |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 713 | paint.setXPFactory(GrDisableColorXPFactory::Get()); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 714 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 715 | this->internalDrawPath(args.fRenderTargetContext, std::move(paint), args.fAAType, |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 716 | GrUserStencilSettings::kUnused, *args.fClip, *args.fViewMatrix, |
| 717 | *args.fShape, true); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | /////////////////////////////////////////////////////////////////////////////////////////////////// |