bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 "GrDefaultPathRenderer.h" |
| 9 | |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 10 | #include "GrBatch.h" |
| 11 | #include "GrBatchTarget.h" |
joshualitt | 622d3ad | 2015-05-07 08:13:11 -0700 | [diff] [blame] | 12 | #include "GrBatchTest.h" |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 13 | #include "GrContext.h" |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 14 | #include "GrDefaultGeoProcFactory.h" |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 15 | #include "GrPathUtils.h" |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 16 | #include "GrPipelineBuilder.h" |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 17 | #include "GrVertices.h" |
egdaniel | af18a09 | 2015-01-05 10:22:28 -0800 | [diff] [blame] | 18 | #include "SkGeometry.h" |
tomhudson@google.com | dd5f744 | 2011-08-30 15:13:55 +0000 | [diff] [blame] | 19 | #include "SkString.h" |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 20 | #include "SkStrokeRec.h" |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 21 | #include "SkTLazy.h" |
commit-bot@chromium.org | 933e65d | 2014-03-20 20:00:24 +0000 | [diff] [blame] | 22 | #include "SkTraceEvent.h" |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 23 | |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 24 | GrDefaultPathRenderer::GrDefaultPathRenderer(bool separateStencilSupport, |
| 25 | bool stencilWrapOpsSupport) |
| 26 | : fSeparateStencil(separateStencilSupport) |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 27 | , fStencilWrapOps(stencilWrapOpsSupport) { |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 31 | //////////////////////////////////////////////////////////////////////////////// |
| 32 | // Stencil rules for paths |
| 33 | |
| 34 | ////// Even/Odd |
| 35 | |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 36 | GR_STATIC_CONST_SAME_STENCIL(gEOStencilPass, |
| 37 | kInvert_StencilOp, |
| 38 | kKeep_StencilOp, |
| 39 | kAlwaysIfInClip_StencilFunc, |
| 40 | 0xffff, |
| 41 | 0xffff, |
| 42 | 0xffff); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 43 | |
| 44 | // ok not to check clip b/c stencil pass only wrote inside clip |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 45 | GR_STATIC_CONST_SAME_STENCIL(gEOColorPass, |
| 46 | kZero_StencilOp, |
| 47 | kZero_StencilOp, |
| 48 | kNotEqual_StencilFunc, |
| 49 | 0xffff, |
| 50 | 0x0000, |
| 51 | 0xffff); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 52 | |
| 53 | // have to check clip b/c outside clip will always be zero. |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 54 | GR_STATIC_CONST_SAME_STENCIL(gInvEOColorPass, |
| 55 | kZero_StencilOp, |
| 56 | kZero_StencilOp, |
| 57 | kEqualIfInClip_StencilFunc, |
| 58 | 0xffff, |
| 59 | 0x0000, |
| 60 | 0xffff); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 61 | |
| 62 | ////// Winding |
| 63 | |
| 64 | // when we have separate stencil we increment front faces / decrement back faces |
| 65 | // when we don't have wrap incr and decr we use the stencil test to simulate |
| 66 | // them. |
| 67 | |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 68 | GR_STATIC_CONST_STENCIL(gWindStencilSeparateWithWrap, |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 69 | kIncWrap_StencilOp, kDecWrap_StencilOp, |
| 70 | kKeep_StencilOp, kKeep_StencilOp, |
| 71 | kAlwaysIfInClip_StencilFunc, kAlwaysIfInClip_StencilFunc, |
tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 72 | 0xffff, 0xffff, |
| 73 | 0xffff, 0xffff, |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 74 | 0xffff, 0xffff); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 75 | |
| 76 | // if inc'ing the max value, invert to make 0 |
| 77 | // if dec'ing zero invert to make all ones. |
| 78 | // we can't avoid touching the stencil on both passing and |
| 79 | // failing, so we can't resctrict ourselves to the clip. |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 80 | GR_STATIC_CONST_STENCIL(gWindStencilSeparateNoWrap, |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 81 | kInvert_StencilOp, kInvert_StencilOp, |
| 82 | kIncClamp_StencilOp, kDecClamp_StencilOp, |
| 83 | kEqual_StencilFunc, kEqual_StencilFunc, |
tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 84 | 0xffff, 0xffff, |
| 85 | 0xffff, 0x0000, |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 86 | 0xffff, 0xffff); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 87 | |
| 88 | // When there are no separate faces we do two passes to setup the winding rule |
| 89 | // stencil. First we draw the front faces and inc, then we draw the back faces |
| 90 | // and dec. These are same as the above two split into the incrementing and |
| 91 | // decrementing passes. |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 92 | GR_STATIC_CONST_SAME_STENCIL(gWindSingleStencilWithWrapInc, |
| 93 | kIncWrap_StencilOp, |
| 94 | kKeep_StencilOp, |
| 95 | kAlwaysIfInClip_StencilFunc, |
| 96 | 0xffff, |
| 97 | 0xffff, |
| 98 | 0xffff); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 99 | |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 100 | GR_STATIC_CONST_SAME_STENCIL(gWindSingleStencilWithWrapDec, |
| 101 | kDecWrap_StencilOp, |
| 102 | kKeep_StencilOp, |
| 103 | kAlwaysIfInClip_StencilFunc, |
| 104 | 0xffff, |
| 105 | 0xffff, |
| 106 | 0xffff); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 107 | |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 108 | GR_STATIC_CONST_SAME_STENCIL(gWindSingleStencilNoWrapInc, |
| 109 | kInvert_StencilOp, |
| 110 | kIncClamp_StencilOp, |
| 111 | kEqual_StencilFunc, |
| 112 | 0xffff, |
| 113 | 0xffff, |
| 114 | 0xffff); |
| 115 | |
| 116 | GR_STATIC_CONST_SAME_STENCIL(gWindSingleStencilNoWrapDec, |
| 117 | kInvert_StencilOp, |
| 118 | kDecClamp_StencilOp, |
| 119 | kEqual_StencilFunc, |
| 120 | 0xffff, |
| 121 | 0x0000, |
| 122 | 0xffff); |
| 123 | |
| 124 | // Color passes are the same whether we use the two-sided stencil or two passes |
| 125 | |
| 126 | GR_STATIC_CONST_SAME_STENCIL(gWindColorPass, |
| 127 | kZero_StencilOp, |
| 128 | kZero_StencilOp, |
| 129 | kNonZeroIfInClip_StencilFunc, |
| 130 | 0xffff, |
| 131 | 0x0000, |
| 132 | 0xffff); |
| 133 | |
| 134 | GR_STATIC_CONST_SAME_STENCIL(gInvWindColorPass, |
| 135 | kZero_StencilOp, |
| 136 | kZero_StencilOp, |
| 137 | kEqualIfInClip_StencilFunc, |
| 138 | 0xffff, |
| 139 | 0x0000, |
| 140 | 0xffff); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 141 | |
| 142 | ////// Normal render to stencil |
| 143 | |
| 144 | // Sometimes the default path renderer can draw a path directly to the stencil |
| 145 | // buffer without having to first resolve the interior / exterior. |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 146 | GR_STATIC_CONST_SAME_STENCIL(gDirectToStencil, |
| 147 | kZero_StencilOp, |
| 148 | kIncClamp_StencilOp, |
| 149 | kAlwaysIfInClip_StencilFunc, |
| 150 | 0xffff, |
| 151 | 0x0000, |
| 152 | 0xffff); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 153 | |
| 154 | //////////////////////////////////////////////////////////////////////////////// |
| 155 | // Helpers for drawPath |
| 156 | |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 157 | #define STENCIL_OFF 0 // Always disable stencil (even when needed) |
| 158 | |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 159 | static inline bool single_pass_path(const SkPath& path, const SkStrokeRec& stroke) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 160 | #if STENCIL_OFF |
| 161 | return true; |
| 162 | #else |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 163 | if (!stroke.isHairlineStyle() && !path.isInverseFillType()) { |
bsalomon@google.com | 7d72c45 | 2012-01-30 14:02:44 +0000 | [diff] [blame] | 164 | return path.isConvex(); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 165 | } |
| 166 | return false; |
| 167 | #endif |
| 168 | } |
| 169 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 170 | GrPathRenderer::StencilSupport |
| 171 | GrDefaultPathRenderer::onGetStencilSupport(const GrDrawTarget*, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 172 | const GrPipelineBuilder*, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 173 | const SkPath& path, |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 174 | const GrStrokeInfo& stroke) const { |
kkinnunen | d156d36 | 2015-05-18 22:23:54 -0700 | [diff] [blame] | 175 | if (single_pass_path(path, stroke)) { |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 176 | return GrPathRenderer::kNoRestriction_StencilSupport; |
| 177 | } else { |
| 178 | return GrPathRenderer::kStencilOnly_StencilSupport; |
| 179 | } |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 180 | } |
| 181 | |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 182 | static inline void append_countour_edge_indices(bool hairLine, |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 183 | uint16_t fanCenterIdx, |
| 184 | uint16_t edgeV0Idx, |
| 185 | uint16_t** indices) { |
| 186 | // when drawing lines we're appending line segments along |
| 187 | // the contour. When applying the other fill rules we're |
| 188 | // drawing triangle fans around fanCenterIdx. |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 189 | if (!hairLine) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 190 | *((*indices)++) = fanCenterIdx; |
| 191 | } |
| 192 | *((*indices)++) = edgeV0Idx; |
| 193 | *((*indices)++) = edgeV0Idx + 1; |
| 194 | } |
| 195 | |
egdaniel | af18a09 | 2015-01-05 10:22:28 -0800 | [diff] [blame] | 196 | static inline void add_quad(SkPoint** vert, const SkPoint* base, const SkPoint pts[], |
| 197 | SkScalar srcSpaceTolSqd, SkScalar srcSpaceTol, bool indexed, |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 198 | bool isHairline, uint16_t subpathIdxStart, int offset, uint16_t** idx) { |
egdaniel | af18a09 | 2015-01-05 10:22:28 -0800 | [diff] [blame] | 199 | // first pt of quad is the pt we ended on in previous step |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 200 | uint16_t firstQPtIdx = (uint16_t)(*vert - base) - 1 + offset; |
egdaniel | af18a09 | 2015-01-05 10:22:28 -0800 | [diff] [blame] | 201 | uint16_t numPts = (uint16_t) |
| 202 | GrPathUtils::generateQuadraticPoints( |
| 203 | pts[0], pts[1], pts[2], |
| 204 | srcSpaceTolSqd, vert, |
| 205 | GrPathUtils::quadraticPointCount(pts, srcSpaceTol)); |
| 206 | if (indexed) { |
| 207 | for (uint16_t i = 0; i < numPts; ++i) { |
| 208 | append_countour_edge_indices(isHairline, subpathIdxStart, |
| 209 | firstQPtIdx + i, idx); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 214 | class DefaultPathBatch : public GrBatch { |
| 215 | public: |
| 216 | struct Geometry { |
| 217 | GrColor fColor; |
| 218 | SkPath fPath; |
| 219 | SkScalar fTolerance; |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 220 | }; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 221 | |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 222 | static GrBatch* Create(const Geometry& geometry, uint8_t coverage, const SkMatrix& viewMatrix, |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 223 | bool isHairline, const SkRect& devBounds) { |
| 224 | return SkNEW_ARGS(DefaultPathBatch, (geometry, coverage, viewMatrix, isHairline, |
| 225 | devBounds)); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 226 | } |
| 227 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 228 | const char* name() const override { return "DefaultPathBatch"; } |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 229 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 230 | void getInvariantOutputColor(GrInitInvariantOutput* out) const override { |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 231 | // When this is called on a batch, there is only one geometry bundle |
| 232 | out->setKnownFourComponents(fGeoData[0].fColor); |
| 233 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 234 | void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { |
joshualitt | 233c6ce | 2015-02-24 08:17:49 -0800 | [diff] [blame] | 235 | out->setKnownSingleComponent(this->coverage()); |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 236 | } |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 237 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 238 | void initBatchTracker(const GrPipelineInfo& init) override { |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 239 | // Handle any color overrides |
| 240 | if (init.fColorIgnored) { |
| 241 | fGeoData[0].fColor = GrColor_ILLEGAL; |
| 242 | } else if (GrColor_ILLEGAL != init.fOverrideColor) { |
| 243 | fGeoData[0].fColor = init.fOverrideColor; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 244 | } |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 245 | |
| 246 | // setup batch properties |
| 247 | fBatch.fColorIgnored = init.fColorIgnored; |
| 248 | fBatch.fColor = fGeoData[0].fColor; |
| 249 | fBatch.fUsesLocalCoords = init.fUsesLocalCoords; |
| 250 | fBatch.fCoverageIgnored = init.fCoverageIgnored; |
| 251 | } |
| 252 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 253 | void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override { |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 254 | SkAutoTUnref<const GrGeometryProcessor> gp( |
| 255 | GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPosition_GPType, |
| 256 | this->color(), |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 257 | this->usesLocalCoords(), |
| 258 | this->coverageIgnored(), |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 259 | this->viewMatrix(), |
| 260 | SkMatrix::I(), |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 261 | this->coverage())); |
| 262 | |
| 263 | size_t vertexStride = gp->getVertexStride(); |
| 264 | SkASSERT(vertexStride == sizeof(SkPoint)); |
| 265 | |
| 266 | batchTarget->initDraw(gp, pipeline); |
| 267 | |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 268 | int instanceCount = fGeoData.count(); |
| 269 | |
| 270 | // compute number of vertices |
| 271 | int maxVertices = 0; |
| 272 | |
| 273 | // We will use index buffers if we have multiple paths or one path with multiple contours |
| 274 | bool isIndexed = instanceCount > 1; |
| 275 | for (int i = 0; i < instanceCount; i++) { |
| 276 | Geometry& args = fGeoData[i]; |
| 277 | |
| 278 | int contourCount; |
| 279 | maxVertices += GrPathUtils::worstCasePointCount(args.fPath, &contourCount, |
| 280 | args.fTolerance); |
| 281 | |
| 282 | isIndexed = isIndexed || contourCount > 1; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 283 | } |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 284 | |
joshualitt | 5b27b14 | 2015-02-24 12:58:46 -0800 | [diff] [blame] | 285 | if (maxVertices == 0 || maxVertices > ((int)SK_MaxU16 + 1)) { |
| 286 | SkDebugf("Cannot render path (%d)\n", maxVertices); |
| 287 | return; |
| 288 | } |
| 289 | |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 290 | // determine primitiveType |
| 291 | int maxIndices = 0; |
| 292 | GrPrimitiveType primitiveType; |
| 293 | if (this->isHairline()) { |
| 294 | if (isIndexed) { |
| 295 | maxIndices = 2 * maxVertices; |
| 296 | primitiveType = kLines_GrPrimitiveType; |
| 297 | } else { |
| 298 | primitiveType = kLineStrip_GrPrimitiveType; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 299 | } |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 300 | } else { |
| 301 | if (isIndexed) { |
| 302 | maxIndices = 3 * maxVertices; |
| 303 | primitiveType = kTriangles_GrPrimitiveType; |
| 304 | } else { |
| 305 | primitiveType = kTriangleFan_GrPrimitiveType; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | // allocate vertex / index buffers |
| 310 | const GrVertexBuffer* vertexBuffer; |
| 311 | int firstVertex; |
| 312 | |
robertphillips | e40d397 | 2015-05-07 09:51:43 -0700 | [diff] [blame] | 313 | void* verts = batchTarget->makeVertSpace(vertexStride, maxVertices, |
| 314 | &vertexBuffer, &firstVertex); |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 315 | |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 316 | if (!verts) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 317 | SkDebugf("Could not allocate vertices\n"); |
| 318 | return; |
| 319 | } |
| 320 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 321 | const GrIndexBuffer* indexBuffer = NULL; |
| 322 | int firstIndex = 0; |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 323 | |
| 324 | void* indices = NULL; |
| 325 | if (isIndexed) { |
robertphillips | e40d397 | 2015-05-07 09:51:43 -0700 | [diff] [blame] | 326 | indices = batchTarget->makeIndexSpace(maxIndices, &indexBuffer, &firstIndex); |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 327 | |
| 328 | if (!indices) { |
| 329 | SkDebugf("Could not allocate indices\n"); |
| 330 | return; |
| 331 | } |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | // fill buffers |
| 335 | int vertexOffset = 0; |
| 336 | int indexOffset = 0; |
| 337 | for (int i = 0; i < instanceCount; i++) { |
| 338 | Geometry& args = fGeoData[i]; |
| 339 | |
| 340 | int vertexCnt = 0; |
| 341 | int indexCnt = 0; |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 342 | if (!this->createGeom(verts, |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 343 | vertexOffset, |
| 344 | indices, |
| 345 | indexOffset, |
| 346 | &vertexCnt, |
| 347 | &indexCnt, |
| 348 | args.fPath, |
| 349 | args.fTolerance, |
| 350 | isIndexed)) { |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | vertexOffset += vertexCnt; |
| 355 | indexOffset += indexCnt; |
| 356 | SkASSERT(vertexOffset <= maxVertices && indexOffset <= maxIndices); |
| 357 | } |
| 358 | |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 359 | GrVertices vertices; |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 360 | if (isIndexed) { |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 361 | vertices.initIndexed(primitiveType, vertexBuffer, indexBuffer, firstVertex, firstIndex, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 362 | vertexOffset, indexOffset); |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 363 | } else { |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 364 | vertices.init(primitiveType, vertexBuffer, firstVertex, vertexOffset); |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 365 | } |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 366 | batchTarget->draw(vertices); |
joshualitt | 6065b88 | 2015-02-24 13:20:59 -0800 | [diff] [blame] | 367 | |
| 368 | // put back reserves |
| 369 | batchTarget->putBackIndices((size_t)(maxIndices - indexOffset)); |
| 370 | batchTarget->putBackVertices((size_t)(maxVertices - vertexOffset), (size_t)vertexStride); |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 374 | |
| 375 | private: |
| 376 | DefaultPathBatch(const Geometry& geometry, uint8_t coverage, const SkMatrix& viewMatrix, |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 377 | bool isHairline, const SkRect& devBounds) { |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 378 | this->initClassID<DefaultPathBatch>(); |
| 379 | fBatch.fCoverage = coverage; |
| 380 | fBatch.fIsHairline = isHairline; |
| 381 | fBatch.fViewMatrix = viewMatrix; |
| 382 | fGeoData.push_back(geometry); |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 383 | |
| 384 | this->setBounds(devBounds); |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 385 | } |
| 386 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 387 | bool onCombineIfPossible(GrBatch* t) override { |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 388 | DefaultPathBatch* that = t->cast<DefaultPathBatch>(); |
| 389 | |
| 390 | if (this->color() != that->color()) { |
| 391 | return false; |
| 392 | } |
| 393 | |
| 394 | if (this->coverage() != that->coverage()) { |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
| 399 | return false; |
| 400 | } |
| 401 | |
| 402 | if (this->isHairline() != that->isHairline()) { |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 407 | this->joinBounds(that->bounds()); |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 408 | return true; |
| 409 | } |
| 410 | |
| 411 | bool createGeom(void* vertices, |
| 412 | size_t vertexOffset, |
| 413 | void* indices, |
| 414 | size_t indexOffset, |
| 415 | int* vertexCnt, |
| 416 | int* indexCnt, |
| 417 | const SkPath& path, |
| 418 | SkScalar srcSpaceTol, |
| 419 | bool isIndexed) { |
| 420 | { |
| 421 | SkScalar srcSpaceTolSqd = SkScalarMul(srcSpaceTol, srcSpaceTol); |
| 422 | |
| 423 | uint16_t indexOffsetU16 = (uint16_t)indexOffset; |
| 424 | uint16_t vertexOffsetU16 = (uint16_t)vertexOffset; |
| 425 | |
| 426 | uint16_t* idxBase = reinterpret_cast<uint16_t*>(indices) + indexOffsetU16; |
| 427 | uint16_t* idx = idxBase; |
| 428 | uint16_t subpathIdxStart = vertexOffsetU16; |
| 429 | |
| 430 | SkPoint* base = reinterpret_cast<SkPoint*>(vertices) + vertexOffset; |
| 431 | SkPoint* vert = base; |
| 432 | |
| 433 | SkPoint pts[4]; |
| 434 | |
| 435 | bool first = true; |
| 436 | int subpath = 0; |
| 437 | |
| 438 | SkPath::Iter iter(path, false); |
| 439 | |
| 440 | bool done = false; |
| 441 | while (!done) { |
| 442 | SkPath::Verb verb = iter.next(pts); |
| 443 | switch (verb) { |
| 444 | case SkPath::kMove_Verb: |
| 445 | if (!first) { |
| 446 | uint16_t currIdx = (uint16_t) (vert - base) + vertexOffsetU16; |
| 447 | subpathIdxStart = currIdx; |
| 448 | ++subpath; |
| 449 | } |
| 450 | *vert = pts[0]; |
| 451 | vert++; |
| 452 | break; |
| 453 | case SkPath::kLine_Verb: |
| 454 | if (isIndexed) { |
| 455 | uint16_t prevIdx = (uint16_t)(vert - base) - 1 + vertexOffsetU16; |
| 456 | append_countour_edge_indices(this->isHairline(), subpathIdxStart, |
| 457 | prevIdx, &idx); |
| 458 | } |
| 459 | *(vert++) = pts[1]; |
| 460 | break; |
| 461 | case SkPath::kConic_Verb: { |
| 462 | SkScalar weight = iter.conicWeight(); |
| 463 | SkAutoConicToQuads converter; |
| 464 | // Converting in src-space, hance the finer tolerance (0.25) |
| 465 | // TODO: find a way to do this in dev-space so the tolerance means something |
| 466 | const SkPoint* quadPts = converter.computeQuads(pts, weight, 0.25f); |
| 467 | for (int i = 0; i < converter.countQuads(); ++i) { |
| 468 | add_quad(&vert, base, quadPts + i*2, srcSpaceTolSqd, srcSpaceTol, |
| 469 | isIndexed, this->isHairline(), subpathIdxStart, |
| 470 | (int)vertexOffset, &idx); |
| 471 | } |
| 472 | break; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 473 | } |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 474 | case SkPath::kQuad_Verb: |
| 475 | add_quad(&vert, base, pts, srcSpaceTolSqd, srcSpaceTol, isIndexed, |
| 476 | this->isHairline(), subpathIdxStart, (int)vertexOffset, &idx); |
| 477 | break; |
| 478 | case SkPath::kCubic_Verb: { |
| 479 | // first pt of cubic is the pt we ended on in previous step |
| 480 | uint16_t firstCPtIdx = (uint16_t)(vert - base) - 1 + vertexOffsetU16; |
| 481 | uint16_t numPts = (uint16_t) GrPathUtils::generateCubicPoints( |
| 482 | pts[0], pts[1], pts[2], pts[3], |
| 483 | srcSpaceTolSqd, &vert, |
| 484 | GrPathUtils::cubicPointCount(pts, srcSpaceTol)); |
| 485 | if (isIndexed) { |
| 486 | for (uint16_t i = 0; i < numPts; ++i) { |
| 487 | append_countour_edge_indices(this->isHairline(), subpathIdxStart, |
| 488 | firstCPtIdx + i, &idx); |
| 489 | } |
| 490 | } |
| 491 | break; |
| 492 | } |
| 493 | case SkPath::kClose_Verb: |
| 494 | break; |
| 495 | case SkPath::kDone_Verb: |
| 496 | done = true; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 497 | } |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 498 | first = false; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 499 | } |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 500 | |
| 501 | *vertexCnt = static_cast<int>(vert - base); |
| 502 | *indexCnt = static_cast<int>(idx - idxBase); |
| 503 | |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 504 | } |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 505 | return true; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 506 | } |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 507 | |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 508 | GrColor color() const { return fBatch.fColor; } |
| 509 | uint8_t coverage() const { return fBatch.fCoverage; } |
| 510 | bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 511 | const SkMatrix& viewMatrix() const { return fBatch.fViewMatrix; } |
| 512 | bool isHairline() const { return fBatch.fIsHairline; } |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 513 | bool coverageIgnored() const { return fBatch.fCoverageIgnored; } |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 514 | |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 515 | struct BatchTracker { |
| 516 | GrColor fColor; |
| 517 | uint8_t fCoverage; |
| 518 | SkMatrix fViewMatrix; |
| 519 | bool fUsesLocalCoords; |
| 520 | bool fColorIgnored; |
| 521 | bool fCoverageIgnored; |
| 522 | bool fIsHairline; |
| 523 | }; |
| 524 | |
| 525 | BatchTracker fBatch; |
| 526 | SkSTArray<1, Geometry, true> fGeoData; |
| 527 | }; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 528 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 529 | bool GrDefaultPathRenderer::internalDrawPath(GrDrawTarget* target, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 530 | GrPipelineBuilder* pipelineBuilder, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 531 | GrColor color, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 532 | const SkMatrix& viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 533 | const SkPath& path, |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 534 | const GrStrokeInfo& origStroke, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 535 | bool stencilOnly) { |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 536 | SkTCopyOnFirstWrite<GrStrokeInfo> stroke(origStroke); |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 537 | |
| 538 | SkScalar hairlineCoverage; |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 539 | uint8_t newCoverage = 0xff; |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 540 | if (IsStrokeHairlineOrEquivalent(*stroke, viewMatrix, &hairlineCoverage)) { |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 541 | newCoverage = SkScalarRoundToInt(hairlineCoverage * 0xff); |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 542 | |
kkinnunen | d156d36 | 2015-05-18 22:23:54 -0700 | [diff] [blame] | 543 | if (!stroke->isHairlineStyle()) { |
| 544 | stroke.writable()->setHairlineStyle(); |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | |
kkinnunen | d156d36 | 2015-05-18 22:23:54 -0700 | [diff] [blame] | 548 | const bool isHairline = stroke->isHairlineStyle(); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 549 | |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 550 | // Save the current xp on the draw state so we can reset it if needed |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 551 | SkAutoTUnref<const GrXPFactory> backupXPFactory(SkRef(pipelineBuilder->getXPFactory())); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 552 | // face culling doesn't make sense here |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 553 | SkASSERT(GrPipelineBuilder::kBoth_DrawFace == pipelineBuilder->getDrawFace()); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 554 | |
| 555 | int passCount = 0; |
| 556 | const GrStencilSettings* passes[3]; |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 557 | GrPipelineBuilder::DrawFace drawFace[3]; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 558 | bool reverse = false; |
| 559 | bool lastPassIsBounds; |
| 560 | |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 561 | if (isHairline) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 562 | passCount = 1; |
| 563 | if (stencilOnly) { |
| 564 | passes[0] = &gDirectToStencil; |
| 565 | } else { |
| 566 | passes[0] = NULL; |
| 567 | } |
| 568 | lastPassIsBounds = false; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 569 | drawFace[0] = GrPipelineBuilder::kBoth_DrawFace; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 570 | } else { |
kkinnunen | d156d36 | 2015-05-18 22:23:54 -0700 | [diff] [blame] | 571 | if (single_pass_path(path, *stroke)) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 572 | passCount = 1; |
| 573 | if (stencilOnly) { |
| 574 | passes[0] = &gDirectToStencil; |
| 575 | } else { |
| 576 | passes[0] = NULL; |
| 577 | } |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 578 | drawFace[0] = GrPipelineBuilder::kBoth_DrawFace; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 579 | lastPassIsBounds = false; |
| 580 | } else { |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 581 | switch (path.getFillType()) { |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 582 | case SkPath::kInverseEvenOdd_FillType: |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 583 | reverse = true; |
| 584 | // fallthrough |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 585 | case SkPath::kEvenOdd_FillType: |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 586 | passes[0] = &gEOStencilPass; |
| 587 | if (stencilOnly) { |
| 588 | passCount = 1; |
| 589 | lastPassIsBounds = false; |
| 590 | } else { |
| 591 | passCount = 2; |
| 592 | lastPassIsBounds = true; |
| 593 | if (reverse) { |
| 594 | passes[1] = &gInvEOColorPass; |
| 595 | } else { |
| 596 | passes[1] = &gEOColorPass; |
| 597 | } |
| 598 | } |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 599 | drawFace[0] = drawFace[1] = GrPipelineBuilder::kBoth_DrawFace; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 600 | break; |
| 601 | |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 602 | case SkPath::kInverseWinding_FillType: |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 603 | reverse = true; |
| 604 | // fallthrough |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 605 | case SkPath::kWinding_FillType: |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 606 | if (fSeparateStencil) { |
| 607 | if (fStencilWrapOps) { |
| 608 | passes[0] = &gWindStencilSeparateWithWrap; |
| 609 | } else { |
| 610 | passes[0] = &gWindStencilSeparateNoWrap; |
| 611 | } |
| 612 | passCount = 2; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 613 | drawFace[0] = GrPipelineBuilder::kBoth_DrawFace; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 614 | } else { |
| 615 | if (fStencilWrapOps) { |
| 616 | passes[0] = &gWindSingleStencilWithWrapInc; |
| 617 | passes[1] = &gWindSingleStencilWithWrapDec; |
| 618 | } else { |
| 619 | passes[0] = &gWindSingleStencilNoWrapInc; |
| 620 | passes[1] = &gWindSingleStencilNoWrapDec; |
| 621 | } |
| 622 | // which is cw and which is ccw is arbitrary. |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 623 | drawFace[0] = GrPipelineBuilder::kCW_DrawFace; |
| 624 | drawFace[1] = GrPipelineBuilder::kCCW_DrawFace; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 625 | passCount = 3; |
| 626 | } |
| 627 | if (stencilOnly) { |
| 628 | lastPassIsBounds = false; |
| 629 | --passCount; |
| 630 | } else { |
| 631 | lastPassIsBounds = true; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 632 | drawFace[passCount-1] = GrPipelineBuilder::kBoth_DrawFace; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 633 | if (reverse) { |
| 634 | passes[passCount-1] = &gInvWindColorPass; |
| 635 | } else { |
| 636 | passes[passCount-1] = &gWindColorPass; |
| 637 | } |
| 638 | } |
| 639 | break; |
| 640 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 641 | SkDEBUGFAIL("Unknown path fFill!"); |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 642 | return false; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 643 | } |
| 644 | } |
| 645 | } |
| 646 | |
senorblanco | 2b4bb07 | 2015-04-22 13:45:18 -0700 | [diff] [blame] | 647 | SkScalar tol = GrPathUtils::kDefaultTolerance; |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 648 | SkScalar srcSpaceTol = GrPathUtils::scaleToleranceToSrc(tol, viewMatrix, path.getBounds()); |
| 649 | |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 650 | SkRect devBounds; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 651 | GetPathDevBounds(path, pipelineBuilder->getRenderTarget(), viewMatrix, &devBounds); |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 652 | |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 653 | for (int p = 0; p < passCount; ++p) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 654 | pipelineBuilder->setDrawFace(drawFace[p]); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 655 | if (passes[p]) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 656 | *pipelineBuilder->stencil() = *passes[p]; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | if (lastPassIsBounds && (p == passCount-1)) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 660 | // Reset the XP Factory on pipelineBuilder |
| 661 | pipelineBuilder->setXPFactory(backupXPFactory); |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 662 | SkRect bounds; |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 663 | SkMatrix localMatrix = SkMatrix::I(); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 664 | if (reverse) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 665 | SkASSERT(pipelineBuilder->getRenderTarget()); |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 666 | // draw over the dev bounds (which will be the whole dst surface for inv fill). |
| 667 | bounds = devBounds; |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 668 | SkMatrix vmi; |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 669 | // mapRect through persp matrix may not be correct |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 670 | if (!viewMatrix.hasPerspective() && viewMatrix.invert(&vmi)) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 671 | vmi.mapRect(&bounds); |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 672 | } else { |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 673 | if (!viewMatrix.invert(&localMatrix)) { |
| 674 | return false; |
| 675 | } |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 676 | } |
| 677 | } else { |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 678 | bounds = path.getBounds(); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 679 | } |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 680 | const SkMatrix& viewM = (reverse && viewMatrix.hasPerspective()) ? SkMatrix::I() : |
| 681 | viewMatrix; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 682 | target->drawBWRect(pipelineBuilder, color, viewM, bounds, NULL, &localMatrix); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 683 | } else { |
| 684 | if (passCount > 1) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 685 | pipelineBuilder->setDisableColorXPFactory(); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 686 | } |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 687 | |
| 688 | DefaultPathBatch::Geometry geometry; |
| 689 | geometry.fColor = color; |
| 690 | geometry.fPath = path; |
| 691 | geometry.fTolerance = srcSpaceTol; |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 692 | |
| 693 | SkAutoTUnref<GrBatch> batch(DefaultPathBatch::Create(geometry, newCoverage, viewMatrix, |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 694 | isHairline, devBounds)); |
joshualitt | 332c729 | 2015-02-23 08:44:31 -0800 | [diff] [blame] | 695 | |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 696 | target->drawBatch(pipelineBuilder, batch); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 697 | } |
| 698 | } |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 699 | return true; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 700 | } |
| 701 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 702 | bool GrDefaultPathRenderer::canDrawPath(const GrDrawTarget* target, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 703 | const GrPipelineBuilder* pipelineBuilder, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 704 | const SkMatrix& viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 705 | const SkPath& path, |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 706 | const GrStrokeInfo& stroke, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 707 | bool antiAlias) const { |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 708 | // this class can draw any path with any fill but doesn't do any anti-aliasing. |
egdaniel | af18a09 | 2015-01-05 10:22:28 -0800 | [diff] [blame] | 709 | return !antiAlias && (stroke.isFillStyle() || IsStrokeHairlineOrEquivalent(stroke, |
| 710 | viewMatrix, |
| 711 | NULL)); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 712 | } |
| 713 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 714 | bool GrDefaultPathRenderer::onDrawPath(GrDrawTarget* target, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 715 | GrPipelineBuilder* pipelineBuilder, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 716 | GrColor color, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 717 | const SkMatrix& viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 718 | const SkPath& path, |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 719 | const GrStrokeInfo& stroke, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 720 | bool antiAlias) { |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 721 | return this->internalDrawPath(target, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 722 | pipelineBuilder, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 723 | color, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 724 | viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 725 | path, |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 726 | stroke, |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 727 | false); |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 728 | } |
| 729 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 730 | void GrDefaultPathRenderer::onStencilPath(GrDrawTarget* target, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 731 | GrPipelineBuilder* pipelineBuilder, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 732 | const SkMatrix& viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 733 | const SkPath& path, |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 734 | const GrStrokeInfo& stroke) { |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 735 | SkASSERT(SkPath::kInverseEvenOdd_FillType != path.getFillType()); |
| 736 | SkASSERT(SkPath::kInverseWinding_FillType != path.getFillType()); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 737 | this->internalDrawPath(target, pipelineBuilder, GrColor_WHITE, viewMatrix, path, stroke, true); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 738 | } |
joshualitt | 622d3ad | 2015-05-07 08:13:11 -0700 | [diff] [blame] | 739 | |
| 740 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 741 | |
| 742 | #ifdef GR_TEST_UTILS |
| 743 | |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 744 | BATCH_TEST_DEFINE(DefaultPathBatch) { |
joshualitt | 622d3ad | 2015-05-07 08:13:11 -0700 | [diff] [blame] | 745 | GrColor color = GrRandomColor(random); |
| 746 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
| 747 | |
| 748 | // For now just hairlines because the other types of draws require two batches. |
| 749 | // TODO we should figure out a way to combine the stencil and cover steps into one batch |
| 750 | GrStrokeInfo stroke(SkStrokeRec::kHairline_InitStyle); |
| 751 | SkPath path = GrTest::TestPath(random); |
| 752 | |
| 753 | // Compute srcSpaceTol |
| 754 | SkRect bounds = path.getBounds(); |
| 755 | SkScalar tol = GrPathUtils::kDefaultTolerance; |
| 756 | SkScalar srcSpaceTol = GrPathUtils::scaleToleranceToSrc(tol, viewMatrix, bounds); |
| 757 | |
| 758 | DefaultPathBatch::Geometry geometry; |
| 759 | geometry.fColor = color; |
| 760 | geometry.fPath = path; |
| 761 | geometry.fTolerance = srcSpaceTol; |
| 762 | |
| 763 | viewMatrix.mapRect(&bounds); |
| 764 | uint8_t coverage = GrRandomCoverage(random); |
| 765 | return DefaultPathBatch::Create(geometry, coverage, viewMatrix, true, bounds); |
| 766 | } |
| 767 | |
| 768 | #endif |