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" |
Brian Salomon | 1cc3b15 | 2017-07-11 15:53:18 -0400 | [diff] [blame] | 17 | #include "GrSimpleMeshDrawOpHelper.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 | |
Brian Salomon | 1cc3b15 | 2017-07-11 15:53:18 -0400 | [diff] [blame] | 112 | namespace { |
| 113 | |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 114 | class MSAAQuadProcessor : public GrGeometryProcessor { |
| 115 | public: |
| 116 | static GrGeometryProcessor* Create(const SkMatrix& viewMatrix) { |
| 117 | return new MSAAQuadProcessor(viewMatrix); |
| 118 | } |
| 119 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 120 | ~MSAAQuadProcessor() override {} |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 121 | |
| 122 | const char* name() const override { return "MSAAQuadProcessor"; } |
| 123 | |
| 124 | const Attribute* inPosition() const { return fInPosition; } |
| 125 | const Attribute* inUV() const { return fInUV; } |
| 126 | const Attribute* inColor() const { return fInColor; } |
| 127 | const SkMatrix& viewMatrix() const { return fViewMatrix; } |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 128 | |
| 129 | class GLSLProcessor : public GrGLSLGeometryProcessor { |
| 130 | public: |
| 131 | GLSLProcessor(const GrGeometryProcessor& qpr) {} |
| 132 | |
| 133 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
| 134 | const MSAAQuadProcessor& qp = args.fGP.cast<MSAAQuadProcessor>(); |
| 135 | GrGLSLVertexBuilder* vsBuilder = args.fVertBuilder; |
| 136 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
| 137 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 138 | |
| 139 | // emit attributes |
| 140 | varyingHandler->emitAttributes(qp); |
| 141 | varyingHandler->addPassThroughAttribute(qp.inColor(), args.fOutputColor); |
| 142 | |
| 143 | GrGLSLVertToFrag uv(kVec2f_GrSLType); |
| 144 | varyingHandler->addVarying("uv", &uv, kHigh_GrSLPrecision); |
| 145 | vsBuilder->codeAppendf("%s = %s;", uv.vsOut(), qp.inUV()->fName); |
| 146 | |
| 147 | // Setup position |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 148 | this->setupPosition(vsBuilder, uniformHandler, gpArgs, qp.inPosition()->fName, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 149 | qp.viewMatrix(), &fViewMatrixUniform); |
| 150 | |
| 151 | // emit transforms |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 152 | this->emitTransforms(vsBuilder, varyingHandler, uniformHandler, gpArgs->fPositionVar, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 153 | qp.inPosition()->fName, SkMatrix::I(), |
| 154 | args.fFPCoordTransformHandler); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 155 | |
| 156 | GrGLSLPPFragmentBuilder* fsBuilder = args.fFragBuilder; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 157 | 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] | 158 | uv.fsIn()); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 159 | fsBuilder->codeAppendf("%s = float4(1.0);", args.fOutputCoverage); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static inline void GenKey(const GrGeometryProcessor& gp, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 163 | const GrShaderCaps&, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 164 | GrProcessorKeyBuilder* b) { |
| 165 | const MSAAQuadProcessor& qp = gp.cast<MSAAQuadProcessor>(); |
| 166 | uint32_t key = 0; |
| 167 | key |= qp.viewMatrix().hasPerspective() ? 0x1 : 0x0; |
| 168 | key |= qp.viewMatrix().isIdentity() ? 0x2: 0x0; |
| 169 | b->add32(key); |
| 170 | } |
| 171 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 172 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& gp, |
| 173 | FPCoordTransformIter&& transformIter) override { |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 174 | const MSAAQuadProcessor& qp = gp.cast<MSAAQuadProcessor>(); |
| 175 | if (!qp.viewMatrix().isIdentity()) { |
| 176 | float viewMatrix[3 * 3]; |
| 177 | GrGLSLGetMatrix<3>(viewMatrix, qp.viewMatrix()); |
| 178 | pdman.setMatrix3f(fViewMatrixUniform, viewMatrix); |
| 179 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 180 | this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 181 | } |
| 182 | |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 183 | private: |
| 184 | typedef GrGLSLGeometryProcessor INHERITED; |
| 185 | |
| 186 | UniformHandle fViewMatrixUniform; |
| 187 | }; |
| 188 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 189 | virtual void getGLSLProcessorKey(const GrShaderCaps& caps, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 190 | GrProcessorKeyBuilder* b) const override { |
| 191 | GLSLProcessor::GenKey(*this, caps, b); |
| 192 | } |
| 193 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 194 | virtual GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override { |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 195 | return new GLSLProcessor(*this); |
| 196 | } |
| 197 | |
| 198 | private: |
| 199 | MSAAQuadProcessor(const SkMatrix& viewMatrix) |
| 200 | : fViewMatrix(viewMatrix) { |
| 201 | this->initClassID<MSAAQuadProcessor>(); |
bsalomon | 6cb807b | 2016-08-17 11:33:39 -0700 | [diff] [blame] | 202 | fInPosition = &this->addVertexAttrib("inPosition", kVec2f_GrVertexAttribType, |
| 203 | kHigh_GrSLPrecision); |
| 204 | fInUV = &this->addVertexAttrib("inUV", kVec2f_GrVertexAttribType, kHigh_GrSLPrecision); |
| 205 | fInColor = &this->addVertexAttrib("inColor", kVec4ub_GrVertexAttribType); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 206 | this->setSampleShading(1.0f); |
| 207 | } |
| 208 | |
| 209 | const Attribute* fInPosition; |
| 210 | const Attribute* fInUV; |
| 211 | const Attribute* fInColor; |
| 212 | SkMatrix fViewMatrix; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 213 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 214 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 215 | |
| 216 | typedef GrGeometryProcessor INHERITED; |
| 217 | }; |
| 218 | |
Brian Salomon | 1cc3b15 | 2017-07-11 15:53:18 -0400 | [diff] [blame] | 219 | class MSAAPathOp final : public GrMeshDrawOp { |
| 220 | private: |
| 221 | using Helper = GrSimpleMeshDrawOpHelperWithStencil; |
| 222 | |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 223 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 224 | DEFINE_OP_CLASS_ID |
Brian Salomon | 1cc3b15 | 2017-07-11 15:53:18 -0400 | [diff] [blame] | 225 | static std::unique_ptr<GrDrawOp> Make(GrPaint&& paint, const SkPath& path, GrAAType aaType, |
| 226 | const SkMatrix& viewMatrix, const SkRect& devBounds, |
| 227 | const GrUserStencilSettings* stencilSettings) { |
bsalomon | 50c56a3 | 2016-06-30 12:05:32 -0700 | [diff] [blame] | 228 | int contourCount; |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 229 | int maxLineVertices; |
| 230 | int maxQuadVertices; |
Brian Salomon | 32ebaba | 2017-03-30 10:22:28 -0400 | [diff] [blame] | 231 | ComputeWorstCasePointCount(path, viewMatrix, &contourCount, &maxLineVertices, |
| 232 | &maxQuadVertices); |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 233 | bool isIndexed = contourCount > 1; |
| 234 | if (isIndexed && |
| 235 | (maxLineVertices > kMaxIndexedVertexCnt || maxQuadVertices > kMaxIndexedVertexCnt)) { |
| 236 | return nullptr; |
| 237 | } |
| 238 | |
Brian Salomon | 1cc3b15 | 2017-07-11 15:53:18 -0400 | [diff] [blame] | 239 | return Helper::FactoryHelper<MSAAPathOp>(std::move(paint), path, aaType, viewMatrix, |
| 240 | devBounds, maxLineVertices, maxQuadVertices, |
| 241 | isIndexed, stencilSettings); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 244 | const char* name() const override { return "MSAAPathOp"; } |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 245 | |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 246 | SkString dumpInfo() const override { |
| 247 | SkString string; |
| 248 | string.appendf("Indexed: %d\n", fIsIndexed); |
| 249 | for (const auto& path : fPaths) { |
| 250 | string.appendf("Color: 0x%08x\n", path.fColor); |
| 251 | } |
Brian Salomon | 1cc3b15 | 2017-07-11 15:53:18 -0400 | [diff] [blame] | 252 | string += fHelper.dumpInfo(); |
| 253 | string += INHERITED::dumpInfo(); |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 254 | return string; |
| 255 | } |
| 256 | |
Brian Salomon | 1cc3b15 | 2017-07-11 15:53:18 -0400 | [diff] [blame] | 257 | MSAAPathOp(const Helper::MakeArgs& helperArgs, GrColor color, const SkPath& path, |
| 258 | GrAAType aaType, const SkMatrix& viewMatrix, const SkRect& devBounds, |
| 259 | int maxLineVertices, int maxQuadVertices, bool isIndexed, |
| 260 | const GrUserStencilSettings* stencilSettings) |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 261 | : INHERITED(ClassID()) |
Brian Salomon | 1cc3b15 | 2017-07-11 15:53:18 -0400 | [diff] [blame] | 262 | , fHelper(helperArgs, aaType, stencilSettings) |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 263 | , fViewMatrix(viewMatrix) |
| 264 | , fMaxLineVertices(maxLineVertices) |
| 265 | , fMaxQuadVertices(maxQuadVertices) |
| 266 | , fIsIndexed(isIndexed) { |
| 267 | fPaths.emplace_back(PathInfo{color, path}); |
| 268 | this->setBounds(devBounds, HasAABloat::kNo, IsZeroArea::kNo); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Brian Salomon | 1cc3b15 | 2017-07-11 15:53:18 -0400 | [diff] [blame] | 271 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 272 | |
| 273 | RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override { |
| 274 | return fHelper.xpRequiresDstTexture(caps, clip, GrProcessorAnalysisCoverage::kNone, |
| 275 | &fPaths.front().fColor); |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 276 | } |
| 277 | |
Brian Salomon | 1cc3b15 | 2017-07-11 15:53:18 -0400 | [diff] [blame] | 278 | private: |
Brian Salomon | 32ebaba | 2017-03-30 10:22:28 -0400 | [diff] [blame] | 279 | static void ComputeWorstCasePointCount(const SkPath& path, const SkMatrix& m, int* subpaths, |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 280 | int* outLinePointCount, int* outQuadPointCount) { |
Brian Salomon | 32ebaba | 2017-03-30 10:22:28 -0400 | [diff] [blame] | 281 | SkScalar tolerance = GrPathUtils::scaleToleranceToSrc(kTolerance, m, path.getBounds()); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 282 | int linePointCount = 0; |
| 283 | int quadPointCount = 0; |
| 284 | *subpaths = 1; |
| 285 | |
| 286 | bool first = true; |
| 287 | |
bsalomon | 8eb43e5 | 2016-09-21 07:47:34 -0700 | [diff] [blame] | 288 | SkPath::Iter iter(path, true); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 289 | SkPath::Verb verb; |
| 290 | |
| 291 | SkPoint pts[4]; |
| 292 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 293 | switch (verb) { |
| 294 | case SkPath::kLine_Verb: |
| 295 | linePointCount += 1; |
| 296 | break; |
| 297 | case SkPath::kConic_Verb: { |
| 298 | SkScalar weight = iter.conicWeight(); |
| 299 | SkAutoConicToQuads converter; |
Brian Salomon | 32ebaba | 2017-03-30 10:22:28 -0400 | [diff] [blame] | 300 | converter.computeQuads(pts, weight, tolerance); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 301 | int quadPts = converter.countQuads(); |
| 302 | linePointCount += quadPts; |
| 303 | quadPointCount += 3 * quadPts; |
| 304 | } |
| 305 | case SkPath::kQuad_Verb: |
| 306 | linePointCount += 1; |
| 307 | quadPointCount += 3; |
| 308 | break; |
| 309 | case SkPath::kCubic_Verb: { |
| 310 | SkSTArray<15, SkPoint, true> quadPts; |
Brian Salomon | 32ebaba | 2017-03-30 10:22:28 -0400 | [diff] [blame] | 311 | GrPathUtils::convertCubicToQuads(pts, tolerance, &quadPts); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 312 | int count = quadPts.count(); |
| 313 | linePointCount += count / 3; |
| 314 | quadPointCount += count; |
| 315 | break; |
| 316 | } |
| 317 | case SkPath::kMove_Verb: |
| 318 | linePointCount += 1; |
| 319 | if (!first) { |
| 320 | ++(*subpaths); |
| 321 | } |
| 322 | break; |
| 323 | default: |
| 324 | break; |
| 325 | } |
| 326 | first = false; |
| 327 | } |
| 328 | *outLinePointCount = linePointCount; |
| 329 | *outQuadPointCount = quadPointCount; |
| 330 | } |
| 331 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame^] | 332 | void onPrepareDraws(Target* target) override { |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 333 | if (fMaxLineVertices == 0) { |
| 334 | SkASSERT(fMaxQuadVertices == 0); |
| 335 | return; |
| 336 | } |
| 337 | |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 338 | GrPrimitiveType primitiveType = fIsIndexed ? GrPrimitiveType::kTriangles |
| 339 | : GrPrimitiveType::kTriangleFan; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 340 | |
| 341 | // allocate vertex / index buffers |
| 342 | const GrBuffer* lineVertexBuffer; |
| 343 | int firstLineVertex; |
| 344 | MSAALineVertices lines; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 345 | int lineVertexStride = sizeof(MSAALineVertices::Vertex); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 346 | lines.vertices = (MSAALineVertices::Vertex*) target->makeVertexSpace(lineVertexStride, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 347 | fMaxLineVertices, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 348 | &lineVertexBuffer, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 349 | &firstLineVertex); |
| 350 | if (!lines.vertices) { |
| 351 | SkDebugf("Could not allocate vertices\n"); |
| 352 | return; |
| 353 | } |
| 354 | lines.nextVertex = lines.vertices; |
| 355 | SkDEBUGCODE(lines.verticesEnd = lines.vertices + fMaxLineVertices;) |
| 356 | |
| 357 | MSAAQuadVertices quads; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 358 | int quadVertexStride = sizeof(MSAAQuadVertices::Vertex); |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 359 | SkAutoMalloc quadVertexPtr(fMaxQuadVertices * quadVertexStride); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 360 | quads.vertices = (MSAAQuadVertices::Vertex*) quadVertexPtr.get(); |
| 361 | quads.nextVertex = quads.vertices; |
| 362 | SkDEBUGCODE(quads.verticesEnd = quads.vertices + fMaxQuadVertices;) |
| 363 | |
| 364 | const GrBuffer* lineIndexBuffer = nullptr; |
| 365 | int firstLineIndex; |
| 366 | if (fIsIndexed) { |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 367 | lines.indices = |
| 368 | target->makeIndexSpace(3 * fMaxLineVertices, &lineIndexBuffer, &firstLineIndex); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 369 | if (!lines.indices) { |
| 370 | SkDebugf("Could not allocate indices\n"); |
| 371 | return; |
| 372 | } |
| 373 | lines.nextIndex = lines.indices; |
| 374 | } else { |
| 375 | lines.indices = nullptr; |
| 376 | lines.nextIndex = nullptr; |
| 377 | } |
| 378 | |
| 379 | SkAutoFree quadIndexPtr; |
| 380 | if (fIsIndexed) { |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 381 | quads.indices = (uint16_t*)sk_malloc_throw(3 * fMaxQuadVertices * sizeof(uint16_t)); |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 382 | quadIndexPtr.reset(quads.indices); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 383 | quads.nextIndex = quads.indices; |
| 384 | } else { |
| 385 | quads.indices = nullptr; |
| 386 | quads.nextIndex = nullptr; |
| 387 | } |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 388 | // fill buffers |
bsalomon | 50c56a3 | 2016-06-30 12:05:32 -0700 | [diff] [blame] | 389 | for (int i = 0; i < fPaths.count(); i++) { |
| 390 | const PathInfo& pathInfo = fPaths[i]; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 391 | if (!this->createGeom(lines, |
| 392 | quads, |
bsalomon | 50c56a3 | 2016-06-30 12:05:32 -0700 | [diff] [blame] | 393 | pathInfo.fPath, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 394 | fViewMatrix, |
bsalomon | 50c56a3 | 2016-06-30 12:05:32 -0700 | [diff] [blame] | 395 | pathInfo.fColor, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 396 | fIsIndexed)) { |
| 397 | return; |
| 398 | } |
| 399 | } |
| 400 | int lineVertexOffset = (int) (lines.nextVertex - lines.vertices); |
| 401 | int lineIndexOffset = (int) (lines.nextIndex - lines.indices); |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 402 | SkASSERT(lineVertexOffset <= fMaxLineVertices && lineIndexOffset <= 3 * fMaxLineVertices); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 403 | int quadVertexOffset = (int) (quads.nextVertex - quads.vertices); |
| 404 | int quadIndexOffset = (int) (quads.nextIndex - quads.indices); |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 405 | SkASSERT(quadVertexOffset <= fMaxQuadVertices && quadIndexOffset <= 3 * fMaxQuadVertices); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 406 | |
Brian Salomon | 1cc3b15 | 2017-07-11 15:53:18 -0400 | [diff] [blame] | 407 | const GrPipeline* pipeline = fHelper.makePipeline(target); |
| 408 | |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 409 | if (lineVertexOffset) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 410 | sk_sp<GrGeometryProcessor> lineGP; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 411 | { |
| 412 | using namespace GrDefaultGeoProcFactory; |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 413 | lineGP = GrDefaultGeoProcFactory::Make(Color(Color::kPremulGrColorAttribute_Type), |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 414 | Coverage::kSolid_Type, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 415 | LocalCoords(LocalCoords::kUnused_Type), |
| 416 | fViewMatrix); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 417 | } |
| 418 | SkASSERT(lineVertexStride == lineGP->getVertexStride()); |
| 419 | |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 420 | GrMesh lineMeshes(primitiveType); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 421 | if (!fIsIndexed) { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 422 | lineMeshes.setNonIndexedNonInstanced(lineVertexOffset); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 423 | } else { |
| 424 | lineMeshes.setIndexed(lineIndexBuffer, lineIndexOffset, firstLineIndex, |
| 425 | 0, lineVertexOffset - 1); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 426 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 427 | lineMeshes.setVertexData(lineVertexBuffer, firstLineVertex); |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 428 | |
Brian Salomon | 2a55c8e | 2017-05-09 09:57:19 -0400 | [diff] [blame] | 429 | // We can get line vertices from path moveTos with no actual segments and thus no index |
| 430 | // count. We assert that indexed draws contain a positive index count, so bail here in |
| 431 | // that case. |
| 432 | if (!fIsIndexed || lineIndexOffset) { |
Brian Salomon | 1cc3b15 | 2017-07-11 15:53:18 -0400 | [diff] [blame] | 433 | target->draw(lineGP.get(), pipeline, lineMeshes); |
Brian Salomon | 2a55c8e | 2017-05-09 09:57:19 -0400 | [diff] [blame] | 434 | } |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | if (quadVertexOffset) { |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 438 | sk_sp<const GrGeometryProcessor> quadGP(MSAAQuadProcessor::Create(fViewMatrix)); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 439 | SkASSERT(quadVertexStride == quadGP->getVertexStride()); |
| 440 | |
| 441 | const GrBuffer* quadVertexBuffer; |
| 442 | int firstQuadVertex; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 443 | MSAAQuadVertices::Vertex* quadVertices = (MSAAQuadVertices::Vertex*) |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 444 | target->makeVertexSpace(quadVertexStride, quadVertexOffset, &quadVertexBuffer, |
| 445 | &firstQuadVertex); |
| 446 | memcpy(quadVertices, quads.vertices, quadVertexStride * quadVertexOffset); |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 447 | GrMesh quadMeshes(GrPrimitiveType::kTriangles); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 448 | if (!fIsIndexed) { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 449 | quadMeshes.setNonIndexedNonInstanced(quadVertexOffset); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 450 | } else { |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 451 | const GrBuffer* quadIndexBuffer; |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 452 | int firstQuadIndex; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 453 | uint16_t* quadIndices = (uint16_t*) target->makeIndexSpace(quadIndexOffset, |
| 454 | &quadIndexBuffer, |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 455 | &firstQuadIndex); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 456 | memcpy(quadIndices, quads.indices, sizeof(uint16_t) * quadIndexOffset); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 457 | quadMeshes.setIndexed(quadIndexBuffer, quadIndexOffset, firstQuadIndex, |
| 458 | 0, quadVertexOffset - 1); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 459 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 460 | quadMeshes.setVertexData(quadVertexBuffer, firstQuadVertex); |
Brian Salomon | 1cc3b15 | 2017-07-11 15:53:18 -0400 | [diff] [blame] | 461 | target->draw(quadGP.get(), pipeline, quadMeshes); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 465 | bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 466 | MSAAPathOp* that = t->cast<MSAAPathOp>(); |
Brian Salomon | 1cc3b15 | 2017-07-11 15:53:18 -0400 | [diff] [blame] | 467 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 468 | return false; |
| 469 | } |
| 470 | |
Jim Van Verth | 9d01fbc | 2017-02-22 14:50:52 -0500 | [diff] [blame] | 471 | if (this->bounds().intersects(that->bounds())) { |
| 472 | return false; |
| 473 | } |
| 474 | |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 475 | if (!fViewMatrix.cheapEqualTo(that->fViewMatrix)) { |
| 476 | return false; |
| 477 | } |
| 478 | |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 479 | // If we grow to include 2+ paths we will be indexed. |
| 480 | if (((fMaxLineVertices + that->fMaxLineVertices) > kMaxIndexedVertexCnt) || |
| 481 | ((fMaxQuadVertices + that->fMaxQuadVertices) > kMaxIndexedVertexCnt)) { |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 482 | return false; |
| 483 | } |
| 484 | |
bsalomon | 50c56a3 | 2016-06-30 12:05:32 -0700 | [diff] [blame] | 485 | fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin()); |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 486 | this->joinBounds(*that); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 487 | fIsIndexed = true; |
| 488 | fMaxLineVertices += that->fMaxLineVertices; |
| 489 | fMaxQuadVertices += that->fMaxQuadVertices; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 490 | return true; |
| 491 | } |
| 492 | |
| 493 | bool createGeom(MSAALineVertices& lines, |
| 494 | MSAAQuadVertices& quads, |
| 495 | const SkPath& path, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 496 | const SkMatrix& m, |
| 497 | SkColor color, |
| 498 | bool isIndexed) const { |
| 499 | { |
Brian Salomon | 32ebaba | 2017-03-30 10:22:28 -0400 | [diff] [blame] | 500 | const SkScalar tolerance = GrPathUtils::scaleToleranceToSrc(kTolerance, m, |
| 501 | path.getBounds()); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 502 | uint16_t subpathIdxStart = (uint16_t) (lines.nextVertex - lines.vertices); |
| 503 | |
| 504 | SkPoint pts[4]; |
| 505 | |
| 506 | bool first = true; |
bsalomon | 8eb43e5 | 2016-09-21 07:47:34 -0700 | [diff] [blame] | 507 | SkPath::Iter iter(path, true); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 508 | |
| 509 | bool done = false; |
| 510 | while (!done) { |
| 511 | SkPath::Verb verb = iter.next(pts); |
| 512 | switch (verb) { |
| 513 | case SkPath::kMove_Verb: |
| 514 | if (!first) { |
| 515 | uint16_t currIdx = (uint16_t) (lines.nextVertex - lines.vertices); |
| 516 | subpathIdxStart = currIdx; |
| 517 | } |
| 518 | SkASSERT(lines.nextVertex < lines.verticesEnd); |
| 519 | *(lines.nextVertex++) = { pts[0], color }; |
| 520 | break; |
| 521 | case SkPath::kLine_Verb: |
| 522 | if (isIndexed) { |
| 523 | uint16_t prevIdx = (uint16_t) (lines.nextVertex - lines.vertices - 1); |
| 524 | if (prevIdx > subpathIdxStart) { |
| 525 | append_contour_edge_indices(subpathIdxStart, prevIdx, lines); |
| 526 | } |
| 527 | } |
| 528 | SkASSERT(lines.nextVertex < lines.verticesEnd); |
| 529 | *(lines.nextVertex++) = { pts[1], color }; |
| 530 | break; |
| 531 | case SkPath::kConic_Verb: { |
| 532 | SkScalar weight = iter.conicWeight(); |
| 533 | SkAutoConicToQuads converter; |
Brian Salomon | 32ebaba | 2017-03-30 10:22:28 -0400 | [diff] [blame] | 534 | const SkPoint* quadPts = converter.computeQuads(pts, weight, tolerance); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 535 | for (int i = 0; i < converter.countQuads(); ++i) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 536 | add_quad(lines, quads, quadPts + i * 2, color, isIndexed, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 537 | subpathIdxStart); |
| 538 | } |
| 539 | break; |
| 540 | } |
| 541 | case SkPath::kQuad_Verb: { |
| 542 | add_quad(lines, quads, pts, color, isIndexed, subpathIdxStart); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 543 | break; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 544 | } |
| 545 | case SkPath::kCubic_Verb: { |
| 546 | SkSTArray<15, SkPoint, true> quadPts; |
Brian Salomon | 32ebaba | 2017-03-30 10:22:28 -0400 | [diff] [blame] | 547 | GrPathUtils::convertCubicToQuads(pts, tolerance, &quadPts); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 548 | int count = quadPts.count(); |
| 549 | for (int i = 0; i < count; i += 3) { |
| 550 | add_quad(lines, quads, &quadPts[i], color, isIndexed, subpathIdxStart); |
| 551 | } |
| 552 | break; |
| 553 | } |
| 554 | case SkPath::kClose_Verb: |
| 555 | break; |
| 556 | case SkPath::kDone_Verb: |
| 557 | done = true; |
| 558 | } |
| 559 | first = false; |
| 560 | } |
| 561 | } |
| 562 | return true; |
| 563 | } |
| 564 | |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 565 | // Lines and quads may render with an index buffer. However, we don't have any support for |
| 566 | // overflowing the max index. |
| 567 | static constexpr int kMaxIndexedVertexCnt = SK_MaxU16 / 3; |
bsalomon | 50c56a3 | 2016-06-30 12:05:32 -0700 | [diff] [blame] | 568 | struct PathInfo { |
| 569 | GrColor fColor; |
| 570 | SkPath fPath; |
| 571 | }; |
| 572 | |
Brian Salomon | 1cc3b15 | 2017-07-11 15:53:18 -0400 | [diff] [blame] | 573 | Helper fHelper; |
bsalomon | 50c56a3 | 2016-06-30 12:05:32 -0700 | [diff] [blame] | 574 | SkSTArray<1, PathInfo, true> fPaths; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 575 | SkMatrix fViewMatrix; |
| 576 | int fMaxLineVertices; |
| 577 | int fMaxQuadVertices; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 578 | bool fIsIndexed; |
| 579 | |
Brian Salomon | 1cc3b15 | 2017-07-11 15:53:18 -0400 | [diff] [blame] | 580 | typedef GrMeshDrawOp INHERITED; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 581 | }; |
| 582 | |
Brian Salomon | 1cc3b15 | 2017-07-11 15:53:18 -0400 | [diff] [blame] | 583 | } // anonymous namespace |
| 584 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 585 | bool GrMSAAPathRenderer::internalDrawPath(GrRenderTargetContext* renderTargetContext, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 586 | GrPaint&& paint, |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 587 | GrAAType aaType, |
robertphillips | d2b6d64 | 2016-07-21 08:55:08 -0700 | [diff] [blame] | 588 | const GrUserStencilSettings& userStencilSettings, |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 589 | const GrClip& clip, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 590 | const SkMatrix& viewMatrix, |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 591 | const GrShape& shape, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 592 | bool stencilOnly) { |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 593 | SkASSERT(shape.style().isSimpleFill()); |
| 594 | SkPath path; |
| 595 | shape.asPath(&path); |
| 596 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 597 | const GrUserStencilSettings* passes[2] = {nullptr, nullptr}; |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 598 | bool reverse = false; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 599 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 600 | if (single_pass_shape(shape)) { |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 601 | if (stencilOnly) { |
| 602 | passes[0] = &gDirectToStencil; |
| 603 | } else { |
robertphillips | d2b6d64 | 2016-07-21 08:55:08 -0700 | [diff] [blame] | 604 | passes[0] = &userStencilSettings; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 605 | } |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 606 | } else { |
| 607 | switch (path.getFillType()) { |
| 608 | case SkPath::kInverseEvenOdd_FillType: |
| 609 | reverse = true; |
| 610 | // fallthrough |
| 611 | case SkPath::kEvenOdd_FillType: |
| 612 | passes[0] = &gEOStencilPass; |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 613 | if (!stencilOnly) { |
| 614 | passes[1] = reverse ? &gInvEOColorPass : &gEOColorPass; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 615 | } |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 616 | break; |
| 617 | |
| 618 | case SkPath::kInverseWinding_FillType: |
| 619 | reverse = true; |
| 620 | // fallthrough |
| 621 | case SkPath::kWinding_FillType: |
Brian Salomon | 15b2509 | 2017-05-08 11:10:53 -0400 | [diff] [blame] | 622 | passes[0] = &gWindStencilPass; |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 623 | if (!stencilOnly) { |
| 624 | passes[1] = reverse ? &gInvWindColorPass : &gWindColorPass; |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 625 | } |
| 626 | break; |
| 627 | default: |
| 628 | SkDEBUGFAIL("Unknown path fFill!"); |
| 629 | return false; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | SkRect devBounds; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 634 | GetPathDevBounds(path, renderTargetContext->width(), renderTargetContext->height(), viewMatrix, |
| 635 | &devBounds); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 636 | |
Brian Salomon | d4652ca | 2017-01-13 12:11:36 -0500 | [diff] [blame] | 637 | SkASSERT(passes[0]); |
| 638 | { // First pass |
Brian Salomon | d4652ca | 2017-01-13 12:11:36 -0500 | [diff] [blame] | 639 | bool firstPassIsStencil = stencilOnly || passes[1]; |
| 640 | // If we have a cover pass then we ignore the paint in the first pass and apply it in the |
| 641 | // second. |
| 642 | GrPaint::MoveOrNew firstPassPaint(paint, firstPassIsStencil); |
| 643 | if (firstPassIsStencil) { |
| 644 | firstPassPaint.paint().setXPFactory(GrDisableColorXPFactory::Get()); |
| 645 | } |
Brian Salomon | 1cc3b15 | 2017-07-11 15:53:18 -0400 | [diff] [blame] | 646 | std::unique_ptr<GrDrawOp> op = MSAAPathOp::Make(std::move(firstPassPaint), path, aaType, |
| 647 | viewMatrix, devBounds, passes[0]); |
| 648 | if (!op) { |
| 649 | return false; |
| 650 | } |
| 651 | renderTargetContext->addDrawOp(clip, std::move(op)); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 652 | } |
robertphillips | 8e37530 | 2016-07-11 10:43:58 -0700 | [diff] [blame] | 653 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 654 | if (passes[1]) { |
| 655 | SkRect bounds; |
| 656 | SkMatrix localMatrix = SkMatrix::I(); |
| 657 | if (reverse) { |
| 658 | // draw over the dev bounds (which will be the whole dst surface for inv fill). |
| 659 | bounds = devBounds; |
| 660 | SkMatrix vmi; |
| 661 | // mapRect through persp matrix may not be correct |
| 662 | if (!viewMatrix.hasPerspective() && viewMatrix.invert(&vmi)) { |
| 663 | vmi.mapRect(&bounds); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 664 | } else { |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 665 | if (!viewMatrix.invert(&localMatrix)) { |
| 666 | return false; |
| 667 | } |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 668 | } |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 669 | } else { |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 670 | bounds = path.getBounds(); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 671 | } |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 672 | const SkMatrix& viewM = |
| 673 | (reverse && viewMatrix.hasPerspective()) ? SkMatrix::I() : viewMatrix; |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 674 | renderTargetContext->addDrawOp( |
| 675 | clip, |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 676 | GrRectOpFactory::MakeNonAAFillWithLocalMatrix(std::move(paint), viewM, localMatrix, |
| 677 | bounds, aaType, passes[1])); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 678 | } |
| 679 | return true; |
| 680 | } |
| 681 | |
| 682 | bool GrMSAAPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 683 | // If we aren't a single_pass_shape, we require stencil buffers. |
| 684 | if (!single_pass_shape(*args.fShape) && args.fCaps->avoidStencilBuffers()) { |
| 685 | return false; |
| 686 | } |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 687 | // This path renderer only fills and relies on MSAA for antialiasing. Stroked shapes are |
| 688 | // handled by passing on the original shape and letting the caller compute the stroked shape |
| 689 | // which will have a fill style. |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 690 | return args.fShape->style().isSimpleFill() && (GrAAType::kCoverage != args.fAAType); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | bool GrMSAAPathRenderer::onDrawPath(const DrawPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 694 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 695 | "GrMSAAPathRenderer::onDrawPath"); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 696 | SkTLazy<GrShape> tmpShape; |
| 697 | const GrShape* shape = args.fShape; |
| 698 | if (shape->style().applies()) { |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 699 | SkScalar styleScale = GrStyle::MatrixToScaleFactor(*args.fViewMatrix); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 700 | tmpShape.init(args.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale)); |
| 701 | shape = tmpShape.get(); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 702 | } |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 703 | return this->internalDrawPath(args.fRenderTargetContext, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 704 | std::move(args.fPaint), |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 705 | args.fAAType, |
robertphillips | d2b6d64 | 2016-07-21 08:55:08 -0700 | [diff] [blame] | 706 | *args.fUserStencilSettings, |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 707 | *args.fClip, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 708 | *args.fViewMatrix, |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 709 | *shape, |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 710 | false); |
| 711 | } |
| 712 | |
| 713 | void GrMSAAPathRenderer::onStencilPath(const StencilPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 714 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 715 | "GrMSAAPathRenderer::onStencilPath"); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 716 | SkASSERT(args.fShape->style().isSimpleFill()); |
| 717 | SkASSERT(!args.fShape->mayBeInverseFilledAfterStyling()); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 718 | |
| 719 | GrPaint paint; |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 720 | paint.setXPFactory(GrDisableColorXPFactory::Get()); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 721 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 722 | this->internalDrawPath(args.fRenderTargetContext, std::move(paint), args.fAAType, |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 723 | GrUserStencilSettings::kUnused, *args.fClip, *args.fViewMatrix, |
| 724 | *args.fShape, true); |
ethannicholas | 6536ae5 | 2016-05-02 12:16:49 -0700 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | /////////////////////////////////////////////////////////////////////////////////////////////////// |