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" |
| 9 | |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 10 | #include "GrAuditTrail.h" |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 11 | #include "GrClip.h" |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 12 | #include "GrDefaultGeoProcFactory.h" |
csmartdalton | 02fa32c | 2016-08-19 13:29:27 -0700 | [diff] [blame] | 13 | #include "GrFixedClip.h" |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 14 | #include "GrMesh.h" |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 15 | #include "GrOpFlushState.h" |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 16 | #include "GrPathStencilSettings.h" |
| 17 | #include "GrPathUtils.h" |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 18 | #include "GrPipelineBuilder.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 "batches/GrMeshDrawOp.h" |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 22 | #include "batches/GrRectOpFactory.h" |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 23 | #include "gl/GrGLVaryingHandler.h" |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 24 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 25 | #include "glsl/GrGLSLGeometryProcessor.h" |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 26 | #include "glsl/GrGLSLProgramDataManager.h" |
| 27 | #include "glsl/GrGLSLUtil.h" |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 28 | #include "glsl/GrGLSLVertexShaderBuilder.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 | |
| 118 | virtual ~MSAAQuadProcessor() {} |
| 119 | |
| 120 | const char* name() const override { return "MSAAQuadProcessor"; } |
| 121 | |
| 122 | const Attribute* inPosition() const { return fInPosition; } |
| 123 | const Attribute* inUV() const { return fInUV; } |
| 124 | const Attribute* inColor() const { return fInColor; } |
| 125 | const SkMatrix& viewMatrix() const { return fViewMatrix; } |
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 | |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 212 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST; |
| 213 | |
| 214 | typedef GrGeometryProcessor INHERITED; |
| 215 | }; |
| 216 | |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame^] | 217 | class MSAAPathOp final : public GrMeshDrawOp { |
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 | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame^] | 220 | static sk_sp<GrDrawOp> Make(GrColor color, const SkPath& path, const SkMatrix& viewMatrix, |
| 221 | const SkRect& devBounds) { |
bsalomon | 50c56a3 | 2016-06-30 12:05:32 -0700 | [diff] [blame] | 222 | int contourCount; |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame^] | 223 | int maxLineVertices; |
| 224 | int maxQuadVertices; |
| 225 | ComputeWorstCasePointCount(path, &contourCount, &maxLineVertices, &maxQuadVertices); |
| 226 | bool isIndexed = contourCount > 1; |
| 227 | if (isIndexed && |
| 228 | (maxLineVertices > kMaxIndexedVertexCnt || maxQuadVertices > kMaxIndexedVertexCnt)) { |
| 229 | return nullptr; |
| 230 | } |
| 231 | |
| 232 | return sk_sp<GrDrawOp>(new MSAAPathOp(color, path, viewMatrix, devBounds, maxLineVertices, |
| 233 | maxQuadVertices, isIndexed)); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame^] | 236 | const char* name() const override { return "MSAAPathOp"; } |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 237 | |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 238 | SkString dumpInfo() const override { |
| 239 | SkString string; |
| 240 | string.appendf("Indexed: %d\n", fIsIndexed); |
| 241 | for (const auto& path : fPaths) { |
| 242 | string.appendf("Color: 0x%08x\n", path.fColor); |
| 243 | } |
| 244 | string.append(DumpPipelineInfo(*this->pipeline())); |
| 245 | string.append(INHERITED::dumpInfo()); |
| 246 | return string; |
| 247 | } |
| 248 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 249 | void computePipelineOptimizations(GrInitInvariantOutput* color, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 250 | GrInitInvariantOutput* coverage, |
| 251 | GrBatchToXPOverrides* overrides) const override { |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame^] | 252 | // When this is called there is only one path. |
bsalomon | 50c56a3 | 2016-06-30 12:05:32 -0700 | [diff] [blame] | 253 | color->setKnownFourComponents(fPaths[0].fColor); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 254 | coverage->setKnownSingleComponent(0xff); |
| 255 | } |
| 256 | |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame^] | 257 | private: |
| 258 | MSAAPathOp(GrColor color, const SkPath& path, const SkMatrix& viewMatrix, |
| 259 | const SkRect& devBounds, int maxLineVertices, int maxQuadVertices, bool isIndexed) |
| 260 | : INHERITED(ClassID()) |
| 261 | , fViewMatrix(viewMatrix) |
| 262 | , fMaxLineVertices(maxLineVertices) |
| 263 | , fMaxQuadVertices(maxQuadVertices) |
| 264 | , fIsIndexed(isIndexed) { |
| 265 | fPaths.emplace_back(PathInfo{color, path}); |
| 266 | this->setBounds(devBounds, HasAABloat::kNo, IsZeroArea::kNo); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 267 | } |
| 268 | |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 269 | void initBatchTracker(const GrXPOverridesForBatch& overrides) override { |
| 270 | // Handle any color overrides |
| 271 | if (!overrides.readsColor()) { |
bsalomon | 50c56a3 | 2016-06-30 12:05:32 -0700 | [diff] [blame] | 272 | fPaths[0].fColor = GrColor_ILLEGAL; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 273 | } |
bsalomon | 50c56a3 | 2016-06-30 12:05:32 -0700 | [diff] [blame] | 274 | overrides.getOverrideColorIfSet(&fPaths[0].fColor); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame^] | 277 | static void ComputeWorstCasePointCount(const SkPath& path, int* subpaths, |
| 278 | int* outLinePointCount, int* outQuadPointCount) { |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 279 | int linePointCount = 0; |
| 280 | int quadPointCount = 0; |
| 281 | *subpaths = 1; |
| 282 | |
| 283 | bool first = true; |
| 284 | |
bsalomon | 8eb43e5 | 2016-09-21 07:47:34 -0700 | [diff] [blame] | 285 | SkPath::Iter iter(path, true); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 286 | SkPath::Verb verb; |
| 287 | |
| 288 | SkPoint pts[4]; |
| 289 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 290 | switch (verb) { |
| 291 | case SkPath::kLine_Verb: |
| 292 | linePointCount += 1; |
| 293 | break; |
| 294 | case SkPath::kConic_Verb: { |
| 295 | SkScalar weight = iter.conicWeight(); |
| 296 | SkAutoConicToQuads converter; |
| 297 | converter.computeQuads(pts, weight, kTolerance); |
| 298 | int quadPts = converter.countQuads(); |
| 299 | linePointCount += quadPts; |
| 300 | quadPointCount += 3 * quadPts; |
| 301 | } |
| 302 | case SkPath::kQuad_Verb: |
| 303 | linePointCount += 1; |
| 304 | quadPointCount += 3; |
| 305 | break; |
| 306 | case SkPath::kCubic_Verb: { |
| 307 | SkSTArray<15, SkPoint, true> quadPts; |
| 308 | GrPathUtils::convertCubicToQuads(pts, kTolerance, &quadPts); |
| 309 | int count = quadPts.count(); |
| 310 | linePointCount += count / 3; |
| 311 | quadPointCount += count; |
| 312 | break; |
| 313 | } |
| 314 | case SkPath::kMove_Verb: |
| 315 | linePointCount += 1; |
| 316 | if (!first) { |
| 317 | ++(*subpaths); |
| 318 | } |
| 319 | break; |
| 320 | default: |
| 321 | break; |
| 322 | } |
| 323 | first = false; |
| 324 | } |
| 325 | *outLinePointCount = linePointCount; |
| 326 | *outQuadPointCount = quadPointCount; |
| 327 | } |
| 328 | |
| 329 | void onPrepareDraws(Target* target) const override { |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 330 | if (fMaxLineVertices == 0) { |
| 331 | SkASSERT(fMaxQuadVertices == 0); |
| 332 | return; |
| 333 | } |
| 334 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 335 | GrPrimitiveType primitiveType = fIsIndexed ? kTriangles_GrPrimitiveType |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 336 | : kTriangleFan_GrPrimitiveType; |
| 337 | |
| 338 | // allocate vertex / index buffers |
| 339 | const GrBuffer* lineVertexBuffer; |
| 340 | int firstLineVertex; |
| 341 | MSAALineVertices lines; |
| 342 | size_t lineVertexStride = sizeof(MSAALineVertices::Vertex); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 343 | lines.vertices = (MSAALineVertices::Vertex*) target->makeVertexSpace(lineVertexStride, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 344 | fMaxLineVertices, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 345 | &lineVertexBuffer, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 346 | &firstLineVertex); |
| 347 | if (!lines.vertices) { |
| 348 | SkDebugf("Could not allocate vertices\n"); |
| 349 | return; |
| 350 | } |
| 351 | lines.nextVertex = lines.vertices; |
| 352 | SkDEBUGCODE(lines.verticesEnd = lines.vertices + fMaxLineVertices;) |
| 353 | |
| 354 | MSAAQuadVertices quads; |
| 355 | size_t quadVertexStride = sizeof(MSAAQuadVertices::Vertex); |
| 356 | SkAutoFree quadVertexPtr(sk_malloc_throw(fMaxQuadVertices * quadVertexStride)); |
| 357 | quads.vertices = (MSAAQuadVertices::Vertex*) quadVertexPtr.get(); |
| 358 | quads.nextVertex = quads.vertices; |
| 359 | SkDEBUGCODE(quads.verticesEnd = quads.vertices + fMaxQuadVertices;) |
| 360 | |
| 361 | const GrBuffer* lineIndexBuffer = nullptr; |
| 362 | int firstLineIndex; |
| 363 | if (fIsIndexed) { |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame^] | 364 | lines.indices = |
| 365 | target->makeIndexSpace(3 * fMaxLineVertices, &lineIndexBuffer, &firstLineIndex); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 366 | if (!lines.indices) { |
| 367 | SkDebugf("Could not allocate indices\n"); |
| 368 | return; |
| 369 | } |
| 370 | lines.nextIndex = lines.indices; |
| 371 | } else { |
| 372 | lines.indices = nullptr; |
| 373 | lines.nextIndex = nullptr; |
| 374 | } |
| 375 | |
| 376 | SkAutoFree quadIndexPtr; |
| 377 | if (fIsIndexed) { |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame^] | 378 | quads.indices = (uint16_t*)sk_malloc_throw(3 * fMaxQuadVertices * sizeof(uint16_t)); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 379 | quadIndexPtr.set(quads.indices); |
| 380 | quads.nextIndex = quads.indices; |
| 381 | } else { |
| 382 | quads.indices = nullptr; |
| 383 | quads.nextIndex = nullptr; |
| 384 | } |
| 385 | |
| 386 | // fill buffers |
bsalomon | 50c56a3 | 2016-06-30 12:05:32 -0700 | [diff] [blame] | 387 | for (int i = 0; i < fPaths.count(); i++) { |
| 388 | const PathInfo& pathInfo = fPaths[i]; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 389 | |
| 390 | if (!this->createGeom(lines, |
| 391 | quads, |
bsalomon | 50c56a3 | 2016-06-30 12:05:32 -0700 | [diff] [blame] | 392 | pathInfo.fPath, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 393 | fViewMatrix, |
bsalomon | 50c56a3 | 2016-06-30 12:05:32 -0700 | [diff] [blame] | 394 | pathInfo.fColor, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 395 | fIsIndexed)) { |
| 396 | return; |
| 397 | } |
| 398 | } |
| 399 | int lineVertexOffset = (int) (lines.nextVertex - lines.vertices); |
| 400 | int lineIndexOffset = (int) (lines.nextIndex - lines.indices); |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame^] | 401 | SkASSERT(lineVertexOffset <= fMaxLineVertices && lineIndexOffset <= 3 * fMaxLineVertices); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 402 | int quadVertexOffset = (int) (quads.nextVertex - quads.vertices); |
| 403 | int quadIndexOffset = (int) (quads.nextIndex - quads.indices); |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame^] | 404 | SkASSERT(quadVertexOffset <= fMaxQuadVertices && quadIndexOffset <= 3 * fMaxQuadVertices); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 405 | |
| 406 | if (lineVertexOffset) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 407 | sk_sp<GrGeometryProcessor> lineGP; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 408 | { |
| 409 | using namespace GrDefaultGeoProcFactory; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 410 | lineGP = GrDefaultGeoProcFactory::Make(Color(Color::kAttribute_Type), |
| 411 | Coverage(255), |
| 412 | LocalCoords(LocalCoords::kUnused_Type), |
| 413 | fViewMatrix); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 414 | } |
| 415 | SkASSERT(lineVertexStride == lineGP->getVertexStride()); |
| 416 | |
| 417 | GrMesh lineMeshes; |
| 418 | if (fIsIndexed) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 419 | lineMeshes.initIndexed(primitiveType, lineVertexBuffer, lineIndexBuffer, |
| 420 | firstLineVertex, firstLineIndex, lineVertexOffset, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 421 | lineIndexOffset); |
| 422 | } else { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 423 | lineMeshes.init(primitiveType, lineVertexBuffer, firstLineVertex, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 424 | lineVertexOffset); |
| 425 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 426 | target->draw(lineGP.get(), lineMeshes); |
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); |
| 439 | GrMesh quadMeshes; |
| 440 | if (fIsIndexed) { |
| 441 | const GrBuffer* quadIndexBuffer; |
| 442 | int firstQuadIndex; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 443 | uint16_t* quadIndices = (uint16_t*) target->makeIndexSpace(quadIndexOffset, |
| 444 | &quadIndexBuffer, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 445 | &firstQuadIndex); |
| 446 | memcpy(quadIndices, quads.indices, sizeof(uint16_t) * quadIndexOffset); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 447 | quadMeshes.initIndexed(kTriangles_GrPrimitiveType, quadVertexBuffer, |
| 448 | quadIndexBuffer, firstQuadVertex, firstQuadIndex, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 449 | quadVertexOffset, quadIndexOffset); |
| 450 | } else { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 451 | quadMeshes.init(kTriangles_GrPrimitiveType, quadVertexBuffer, firstQuadVertex, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 452 | quadVertexOffset); |
| 453 | } |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 454 | target->draw(quadGP.get(), quadMeshes); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 458 | bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame^] | 459 | MSAAPathOp* that = t->cast<MSAAPathOp>(); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 460 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 461 | that->bounds(), caps)) { |
| 462 | return false; |
| 463 | } |
| 464 | |
| 465 | if (!fViewMatrix.cheapEqualTo(that->fViewMatrix)) { |
| 466 | return false; |
| 467 | } |
| 468 | |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame^] | 469 | // If we grow to include 2+ paths we will be indexed. |
| 470 | if (((fMaxLineVertices + that->fMaxLineVertices) > kMaxIndexedVertexCnt) || |
| 471 | ((fMaxQuadVertices + that->fMaxQuadVertices) > kMaxIndexedVertexCnt)) { |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 472 | return false; |
| 473 | } |
| 474 | |
bsalomon | 50c56a3 | 2016-06-30 12:05:32 -0700 | [diff] [blame] | 475 | fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin()); |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 476 | this->joinBounds(*that); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 477 | fIsIndexed = true; |
| 478 | fMaxLineVertices += that->fMaxLineVertices; |
| 479 | fMaxQuadVertices += that->fMaxQuadVertices; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 480 | return true; |
| 481 | } |
| 482 | |
| 483 | bool createGeom(MSAALineVertices& lines, |
| 484 | MSAAQuadVertices& quads, |
| 485 | const SkPath& path, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 486 | const SkMatrix& m, |
| 487 | SkColor color, |
| 488 | bool isIndexed) const { |
| 489 | { |
| 490 | uint16_t subpathIdxStart = (uint16_t) (lines.nextVertex - lines.vertices); |
| 491 | |
| 492 | SkPoint pts[4]; |
| 493 | |
| 494 | bool first = true; |
bsalomon | 8eb43e5 | 2016-09-21 07:47:34 -0700 | [diff] [blame] | 495 | SkPath::Iter iter(path, true); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 496 | |
| 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; |
bsalomon | 50c56a3 | 2016-06-30 12:05:32 -0700 | [diff] [blame] | 522 | const SkPoint* quadPts = converter.computeQuads(pts, weight, kTolerance); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 523 | for (int i = 0; i < converter.countQuads(); ++i) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 524 | add_quad(lines, quads, quadPts + i * 2, color, isIndexed, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 525 | subpathIdxStart); |
| 526 | } |
| 527 | break; |
| 528 | } |
| 529 | case SkPath::kQuad_Verb: { |
| 530 | add_quad(lines, quads, pts, color, isIndexed, subpathIdxStart); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 531 | break; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 532 | } |
| 533 | case SkPath::kCubic_Verb: { |
| 534 | SkSTArray<15, SkPoint, true> quadPts; |
| 535 | GrPathUtils::convertCubicToQuads(pts, kTolerance, &quadPts); |
| 536 | 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 Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame^] | 553 | // 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; |
bsalomon | 50c56a3 | 2016-06-30 12:05:32 -0700 | [diff] [blame] | 556 | struct PathInfo { |
| 557 | GrColor fColor; |
| 558 | SkPath fPath; |
| 559 | }; |
| 560 | |
| 561 | SkSTArray<1, PathInfo, true> fPaths; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 562 | |
| 563 | SkMatrix fViewMatrix; |
| 564 | int fMaxLineVertices; |
| 565 | int fMaxQuadVertices; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 566 | bool fIsIndexed; |
| 567 | |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 568 | typedef GrMeshDrawOp INHERITED; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 569 | }; |
| 570 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 571 | bool GrMSAAPathRenderer::internalDrawPath(GrRenderTargetContext* renderTargetContext, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 572 | const GrPaint& paint, |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 573 | GrAAType aaType, |
robertphillips | d2b6d64 | 2016-07-21 08:55:08 -0700 | [diff] [blame] | 574 | const GrUserStencilSettings& userStencilSettings, |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 575 | const GrClip& clip, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 576 | const SkMatrix& viewMatrix, |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 577 | const GrShape& shape, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 578 | bool stencilOnly) { |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 579 | SkASSERT(shape.style().isSimpleFill()); |
| 580 | SkPath path; |
| 581 | shape.asPath(&path); |
| 582 | |
robertphillips | 8e37530 | 2016-07-11 10:43:58 -0700 | [diff] [blame] | 583 | static const int kMaxNumPasses = 2; |
| 584 | |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 585 | int passCount = 0; |
robertphillips | 8e37530 | 2016-07-11 10:43:58 -0700 | [diff] [blame] | 586 | const GrUserStencilSettings* passes[kMaxNumPasses]; |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 587 | bool reverse = false; |
| 588 | bool lastPassIsBounds; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 589 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 590 | if (single_pass_shape(shape)) { |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 591 | passCount = 1; |
| 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 | lastPassIsBounds = false; |
| 598 | } else { |
| 599 | switch (path.getFillType()) { |
| 600 | case SkPath::kInverseEvenOdd_FillType: |
| 601 | reverse = true; |
| 602 | // fallthrough |
| 603 | case SkPath::kEvenOdd_FillType: |
| 604 | passes[0] = &gEOStencilPass; |
| 605 | if (stencilOnly) { |
| 606 | passCount = 1; |
| 607 | lastPassIsBounds = false; |
| 608 | } else { |
| 609 | passCount = 2; |
| 610 | lastPassIsBounds = true; |
| 611 | if (reverse) { |
| 612 | passes[1] = &gInvEOColorPass; |
| 613 | } else { |
| 614 | passes[1] = &gEOColorPass; |
| 615 | } |
| 616 | } |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 617 | break; |
| 618 | |
| 619 | case SkPath::kInverseWinding_FillType: |
| 620 | reverse = true; |
| 621 | // fallthrough |
| 622 | case SkPath::kWinding_FillType: |
| 623 | passes[0] = &gWindStencilSeparateWithWrap; |
| 624 | passCount = 2; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 625 | if (stencilOnly) { |
| 626 | lastPassIsBounds = false; |
robertphillips | 8e37530 | 2016-07-11 10:43:58 -0700 | [diff] [blame] | 627 | passCount = 1; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 628 | } else { |
| 629 | lastPassIsBounds = true; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 630 | if (reverse) { |
robertphillips | 8e37530 | 2016-07-11 10:43:58 -0700 | [diff] [blame] | 631 | passes[1] = &gInvWindColorPass; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 632 | } else { |
robertphillips | 8e37530 | 2016-07-11 10:43:58 -0700 | [diff] [blame] | 633 | passes[1] = &gWindColorPass; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | break; |
| 637 | default: |
| 638 | SkDEBUGFAIL("Unknown path fFill!"); |
| 639 | return false; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | SkRect devBounds; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 644 | GetPathDevBounds(path, renderTargetContext->width(), renderTargetContext->height(), viewMatrix, |
| 645 | &devBounds); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 646 | |
robertphillips | 8e37530 | 2016-07-11 10:43:58 -0700 | [diff] [blame] | 647 | SkASSERT(passCount <= kMaxNumPasses); |
| 648 | |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 649 | for (int p = 0; p < passCount; ++p) { |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 650 | if (lastPassIsBounds && (p == passCount-1)) { |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 651 | SkRect bounds; |
| 652 | SkMatrix localMatrix = SkMatrix::I(); |
| 653 | if (reverse) { |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 654 | // draw over the dev bounds (which will be the whole dst surface for inv fill). |
| 655 | bounds = devBounds; |
| 656 | SkMatrix vmi; |
| 657 | // mapRect through persp matrix may not be correct |
| 658 | if (!viewMatrix.hasPerspective() && viewMatrix.invert(&vmi)) { |
| 659 | vmi.mapRect(&bounds); |
| 660 | } else { |
| 661 | if (!viewMatrix.invert(&localMatrix)) { |
| 662 | return false; |
| 663 | } |
| 664 | } |
| 665 | } else { |
| 666 | bounds = path.getBounds(); |
| 667 | } |
| 668 | const SkMatrix& viewM = (reverse && viewMatrix.hasPerspective()) ? SkMatrix::I() : |
| 669 | viewMatrix; |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 670 | sk_sp<GrDrawOp> op(GrRectOpFactory::MakeNonAAFill(paint.getColor(), viewM, bounds, |
| 671 | nullptr, &localMatrix)); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 672 | |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 673 | GrPipelineBuilder pipelineBuilder(paint, aaType); |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 674 | pipelineBuilder.setUserStencil(passes[p]); |
| 675 | |
Brian Salomon | 24f1978 | 2016-12-13 15:10:11 -0500 | [diff] [blame] | 676 | renderTargetContext->addDrawOp(pipelineBuilder, clip, std::move(op)); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 677 | } else { |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame^] | 678 | sk_sp<GrDrawOp> op = MSAAPathOp::Make(paint.getColor(), path, viewMatrix, devBounds); |
| 679 | if (!op) { |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 680 | return false; |
| 681 | } |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 682 | GrPipelineBuilder pipelineBuilder(paint, aaType); |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 683 | pipelineBuilder.setUserStencil(passes[p]); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 684 | if (passCount > 1) { |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 685 | pipelineBuilder.setDisableColorXPFactory(); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 686 | } |
Brian Salomon | 24f1978 | 2016-12-13 15:10:11 -0500 | [diff] [blame] | 687 | renderTargetContext->addDrawOp(pipelineBuilder, clip, std::move(op)); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | return true; |
| 691 | } |
| 692 | |
| 693 | bool GrMSAAPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 694 | // This path renderer only fills and relies on MSAA for antialiasing. Stroked shapes are |
| 695 | // handled by passing on the original shape and letting the caller compute the stroked shape |
| 696 | // which will have a fill style. |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 697 | return args.fShape->style().isSimpleFill() && (GrAAType::kCoverage != args.fAAType); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | bool GrMSAAPathRenderer::onDrawPath(const DrawPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 701 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 702 | "GrMSAAPathRenderer::onDrawPath"); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 703 | SkTLazy<GrShape> tmpShape; |
| 704 | const GrShape* shape = args.fShape; |
| 705 | if (shape->style().applies()) { |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 706 | SkScalar styleScale = GrStyle::MatrixToScaleFactor(*args.fViewMatrix); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 707 | tmpShape.init(args.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale)); |
| 708 | shape = tmpShape.get(); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 709 | } |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 710 | return this->internalDrawPath(args.fRenderTargetContext, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 711 | *args.fPaint, |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 712 | args.fAAType, |
robertphillips | d2b6d64 | 2016-07-21 08:55:08 -0700 | [diff] [blame] | 713 | *args.fUserStencilSettings, |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 714 | *args.fClip, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 715 | *args.fViewMatrix, |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 716 | *shape, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 717 | false); |
| 718 | } |
| 719 | |
| 720 | void GrMSAAPathRenderer::onStencilPath(const StencilPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 721 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 722 | "GrMSAAPathRenderer::onStencilPath"); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 723 | SkASSERT(args.fShape->style().isSimpleFill()); |
| 724 | SkASSERT(!args.fShape->mayBeInverseFilledAfterStyling()); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 725 | |
| 726 | GrPaint paint; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 727 | paint.setXPFactory(GrDisableColorXPFactory::Make()); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 728 | |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 729 | this->internalDrawPath(args.fRenderTargetContext, paint, args.fAAType, |
| 730 | GrUserStencilSettings::kUnused, *args.fClip, *args.fViewMatrix, |
| 731 | *args.fShape, true); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | /////////////////////////////////////////////////////////////////////////////////////////////////// |