epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 8 | #include "GrPathRenderer.h" |
| 9 | |
| 10 | #include "GrPoint.h" |
| 11 | #include "GrDrawTarget.h" |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 12 | #include "GrPathUtils.h" |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 13 | #include "GrTexture.h" |
| 14 | |
tomhudson@google.com | 278cbb4 | 2011-06-30 19:37:01 +0000 | [diff] [blame] | 15 | #include "SkString.h" |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 16 | #include "SkTemplates.h" |
tomhudson@google.com | 0c8d93a | 2011-07-01 17:08:26 +0000 | [diff] [blame] | 17 | #include "SkTrace.h" |
tomhudson@google.com | 278cbb4 | 2011-06-30 19:37:01 +0000 | [diff] [blame] | 18 | |
tomhudson@google.com | d22b6e4 | 2011-06-24 15:53:40 +0000 | [diff] [blame] | 19 | GrPathRenderer::GrPathRenderer() |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 20 | : fCurveTolerance (GR_Scalar1) |
| 21 | , fPath(NULL) |
| 22 | , fTarget(NULL) { |
tomhudson@google.com | d22b6e4 | 2011-06-24 15:53:40 +0000 | [diff] [blame] | 23 | } |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 24 | |
| 25 | |
| 26 | void GrPathRenderer::setPath(GrDrawTarget* target, |
| 27 | const SkPath* path, |
| 28 | GrPathFill fill, |
| 29 | const GrPoint* translate) { |
| 30 | GrAssert(NULL == fPath); |
| 31 | GrAssert(NULL == fTarget); |
| 32 | GrAssert(NULL != target); |
| 33 | |
| 34 | fTarget = target; |
| 35 | fPath = path; |
| 36 | fFill = fill; |
| 37 | if (NULL != translate) { |
| 38 | fTranslate = *translate; |
| 39 | } else { |
| 40 | fTranslate.fX = fTranslate.fY = 0; |
| 41 | } |
| 42 | this->pathWasSet(); |
| 43 | } |
| 44 | |
| 45 | void GrPathRenderer::clearPath() { |
| 46 | this->pathWillClear(); |
| 47 | fTarget->resetVertexSource(); |
bsalomon@google.com | 25fd36c | 2011-07-06 17:41:08 +0000 | [diff] [blame] | 48 | fTarget->resetIndexSource(); |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 49 | fTarget = NULL; |
| 50 | fPath = NULL; |
| 51 | } |
| 52 | |
| 53 | //////////////////////////////////////////////////////////////////////////////// |
| 54 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 55 | GrDefaultPathRenderer::GrDefaultPathRenderer(bool separateStencilSupport, |
| 56 | bool stencilWrapOpsSupport) |
tomhudson@google.com | d22b6e4 | 2011-06-24 15:53:40 +0000 | [diff] [blame] | 57 | : fSeparateStencil(separateStencilSupport) |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 58 | , fStencilWrapOps(stencilWrapOpsSupport) |
| 59 | , fSubpathCount(0) |
| 60 | , fSubpathVertCount(0) |
| 61 | , fPreviousSrcTol(-GR_Scalar1) |
| 62 | , fPreviousStages(-1) { |
| 63 | fTarget = NULL; |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 67 | // Stencil rules for paths |
| 68 | |
| 69 | ////// Even/Odd |
| 70 | |
| 71 | static const GrStencilSettings gEOStencilPass = { |
| 72 | kInvert_StencilOp, kInvert_StencilOp, |
| 73 | kKeep_StencilOp, kKeep_StencilOp, |
| 74 | kAlwaysIfInClip_StencilFunc, kAlwaysIfInClip_StencilFunc, |
| 75 | 0xffffffff, 0xffffffff, |
| 76 | 0xffffffff, 0xffffffff, |
| 77 | 0xffffffff, 0xffffffff |
| 78 | }; |
| 79 | |
| 80 | // ok not to check clip b/c stencil pass only wrote inside clip |
| 81 | static const GrStencilSettings gEOColorPass = { |
| 82 | kZero_StencilOp, kZero_StencilOp, |
| 83 | kZero_StencilOp, kZero_StencilOp, |
| 84 | kNotEqual_StencilFunc, kNotEqual_StencilFunc, |
| 85 | 0xffffffff, 0xffffffff, |
| 86 | 0x0, 0x0, |
| 87 | 0xffffffff, 0xffffffff |
| 88 | }; |
| 89 | |
| 90 | // have to check clip b/c outside clip will always be zero. |
| 91 | static const GrStencilSettings gInvEOColorPass = { |
| 92 | kZero_StencilOp, kZero_StencilOp, |
| 93 | kZero_StencilOp, kZero_StencilOp, |
| 94 | kEqualIfInClip_StencilFunc, kEqualIfInClip_StencilFunc, |
| 95 | 0xffffffff, 0xffffffff, |
| 96 | 0x0, 0x0, |
| 97 | 0xffffffff, 0xffffffff |
| 98 | }; |
| 99 | |
| 100 | ////// Winding |
| 101 | |
| 102 | // when we have separate stencil we increment front faces / decrement back faces |
| 103 | // when we don't have wrap incr and decr we use the stencil test to simulate |
| 104 | // them. |
| 105 | |
| 106 | static const GrStencilSettings gWindStencilSeparateWithWrap = { |
| 107 | kIncWrap_StencilOp, kDecWrap_StencilOp, |
| 108 | kKeep_StencilOp, kKeep_StencilOp, |
| 109 | kAlwaysIfInClip_StencilFunc, kAlwaysIfInClip_StencilFunc, |
| 110 | 0xffffffff, 0xffffffff, |
| 111 | 0xffffffff, 0xffffffff, |
| 112 | 0xffffffff, 0xffffffff |
| 113 | }; |
| 114 | |
| 115 | // if inc'ing the max value, invert to make 0 |
| 116 | // if dec'ing zero invert to make all ones. |
| 117 | // we can't avoid touching the stencil on both passing and |
| 118 | // failing, so we can't resctrict ourselves to the clip. |
| 119 | static const GrStencilSettings gWindStencilSeparateNoWrap = { |
| 120 | kInvert_StencilOp, kInvert_StencilOp, |
| 121 | kIncClamp_StencilOp, kDecClamp_StencilOp, |
| 122 | kEqual_StencilFunc, kEqual_StencilFunc, |
| 123 | 0xffffffff, 0xffffffff, |
| 124 | 0xffffffff, 0x0, |
| 125 | 0xffffffff, 0xffffffff |
| 126 | }; |
| 127 | |
| 128 | // When there are no separate faces we do two passes to setup the winding rule |
| 129 | // stencil. First we draw the front faces and inc, then we draw the back faces |
| 130 | // and dec. These are same as the above two split into the incrementing and |
| 131 | // decrementing passes. |
| 132 | static const GrStencilSettings gWindSingleStencilWithWrapInc = { |
| 133 | kIncWrap_StencilOp, kIncWrap_StencilOp, |
| 134 | kKeep_StencilOp, kKeep_StencilOp, |
| 135 | kAlwaysIfInClip_StencilFunc, kAlwaysIfInClip_StencilFunc, |
| 136 | 0xffffffff, 0xffffffff, |
| 137 | 0xffffffff, 0xffffffff, |
| 138 | 0xffffffff, 0xffffffff |
| 139 | }; |
| 140 | static const GrStencilSettings gWindSingleStencilWithWrapDec = { |
| 141 | kDecWrap_StencilOp, kDecWrap_StencilOp, |
| 142 | kKeep_StencilOp, kKeep_StencilOp, |
| 143 | kAlwaysIfInClip_StencilFunc, kAlwaysIfInClip_StencilFunc, |
| 144 | 0xffffffff, 0xffffffff, |
| 145 | 0xffffffff, 0xffffffff, |
| 146 | 0xffffffff, 0xffffffff |
| 147 | }; |
| 148 | static const GrStencilSettings gWindSingleStencilNoWrapInc = { |
| 149 | kInvert_StencilOp, kInvert_StencilOp, |
| 150 | kIncClamp_StencilOp, kIncClamp_StencilOp, |
| 151 | kEqual_StencilFunc, kEqual_StencilFunc, |
| 152 | 0xffffffff, 0xffffffff, |
| 153 | 0xffffffff, 0xffffffff, |
| 154 | 0xffffffff, 0xffffffff |
| 155 | }; |
| 156 | static const GrStencilSettings gWindSingleStencilNoWrapDec = { |
| 157 | kInvert_StencilOp, kInvert_StencilOp, |
| 158 | kDecClamp_StencilOp, kDecClamp_StencilOp, |
| 159 | kEqual_StencilFunc, kEqual_StencilFunc, |
| 160 | 0xffffffff, 0xffffffff, |
| 161 | 0x0, 0x0, |
| 162 | 0xffffffff, 0xffffffff |
| 163 | }; |
| 164 | |
| 165 | static const GrStencilSettings gWindColorPass = { |
| 166 | kZero_StencilOp, kZero_StencilOp, |
| 167 | kZero_StencilOp, kZero_StencilOp, |
| 168 | kNonZeroIfInClip_StencilFunc, kNonZeroIfInClip_StencilFunc, |
| 169 | 0xffffffff, 0xffffffff, |
| 170 | 0x0, 0x0, |
| 171 | 0xffffffff, 0xffffffff |
| 172 | }; |
| 173 | |
| 174 | static const GrStencilSettings gInvWindColorPass = { |
| 175 | kZero_StencilOp, kZero_StencilOp, |
| 176 | kZero_StencilOp, kZero_StencilOp, |
| 177 | kEqualIfInClip_StencilFunc, kEqualIfInClip_StencilFunc, |
| 178 | 0xffffffff, 0xffffffff, |
| 179 | 0x0, 0x0, |
| 180 | 0xffffffff, 0xffffffff |
| 181 | }; |
| 182 | |
| 183 | ////// Normal render to stencil |
| 184 | |
| 185 | // Sometimes the default path renderer can draw a path directly to the stencil |
| 186 | // buffer without having to first resolve the interior / exterior. |
| 187 | static const GrStencilSettings gDirectToStencil = { |
| 188 | kZero_StencilOp, kZero_StencilOp, |
| 189 | kIncClamp_StencilOp, kIncClamp_StencilOp, |
| 190 | kAlwaysIfInClip_StencilFunc, kAlwaysIfInClip_StencilFunc, |
| 191 | 0xffffffff, 0xffffffff, |
| 192 | 0x0, 0x0, |
| 193 | 0xffffffff, 0xffffffff |
| 194 | }; |
| 195 | |
| 196 | //////////////////////////////////////////////////////////////////////////////// |
| 197 | // Helpers for drawPath |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 198 | |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 199 | static GrConvexHint getConvexHint(const SkPath& path) { |
| 200 | return path.isConvex() ? kConvex_ConvexHint : kConcave_ConvexHint; |
| 201 | } |
| 202 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 203 | #define STENCIL_OFF 0 // Always disable stencil (even when needed) |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 204 | |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 205 | static inline bool single_pass_path(const GrDrawTarget& target, |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 206 | const GrPath& path, |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 207 | GrPathFill fill) { |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 208 | #if STENCIL_OFF |
| 209 | return true; |
| 210 | #else |
| 211 | if (kEvenOdd_PathFill == fill) { |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 212 | GrConvexHint hint = getConvexHint(path); |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 213 | return hint == kConvex_ConvexHint || |
| 214 | hint == kNonOverlappingConvexPieces_ConvexHint; |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 215 | } else if (kWinding_PathFill == fill) { |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 216 | GrConvexHint hint = getConvexHint(path); |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 217 | return hint == kConvex_ConvexHint || |
| 218 | hint == kNonOverlappingConvexPieces_ConvexHint || |
| 219 | (hint == kSameWindingConvexPieces_ConvexHint && |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 220 | target.canDisableBlend() && !target.isDitherState()); |
| 221 | |
| 222 | } |
| 223 | return false; |
| 224 | #endif |
| 225 | } |
| 226 | |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 227 | bool GrDefaultPathRenderer::requiresStencilPass(const GrDrawTarget* target, |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 228 | const GrPath& path, |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 229 | GrPathFill fill) const { |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 230 | return !single_pass_path(*target, path, fill); |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 231 | } |
| 232 | |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 233 | void GrDefaultPathRenderer::pathWillClear() { |
| 234 | fSubpathVertCount.realloc(0); |
| 235 | fTarget->resetVertexSource(); |
bsalomon@google.com | 82866fd | 2011-08-05 16:27:32 +0000 | [diff] [blame] | 236 | if (fUseIndexedDraw) { |
| 237 | fTarget->resetIndexSource(); |
| 238 | } |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 239 | fPreviousSrcTol = -GR_Scalar1; |
| 240 | fPreviousStages = -1; |
| 241 | } |
| 242 | |
bsalomon@google.com | 25fd36c | 2011-07-06 17:41:08 +0000 | [diff] [blame] | 243 | static inline void append_countour_edge_indices(GrPathFill fillType, |
| 244 | uint16_t fanCenterIdx, |
| 245 | uint16_t edgeV0Idx, |
| 246 | uint16_t** indices) { |
| 247 | // when drawing lines we're appending line segments along |
| 248 | // the contour. When applying the other fill rules we're |
| 249 | // drawing triangle fans around fanCenterIdx. |
| 250 | if (kHairLine_PathFill != fillType) { |
| 251 | *((*indices)++) = fanCenterIdx; |
| 252 | } |
| 253 | *((*indices)++) = edgeV0Idx; |
| 254 | *((*indices)++) = edgeV0Idx + 1; |
| 255 | } |
| 256 | |
| 257 | bool GrDefaultPathRenderer::createGeom(GrScalar srcSpaceTol, |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 258 | GrDrawTarget::StageBitfield stages) { |
| 259 | { |
| 260 | SK_TRACE_EVENT0("GrDefaultPathRenderer::createGeom"); |
| 261 | |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 262 | GrScalar srcSpaceTolSqd = GrMul(srcSpaceTol, srcSpaceTol); |
| 263 | int maxPts = GrPathUtils::worstCasePointCount(*fPath, &fSubpathCount, |
| 264 | srcSpaceTol); |
| 265 | |
bsalomon@google.com | 25fd36c | 2011-07-06 17:41:08 +0000 | [diff] [blame] | 266 | if (maxPts <= 0) { |
| 267 | return false; |
| 268 | } |
| 269 | if (maxPts > ((int)SK_MaxU16 + 1)) { |
| 270 | GrPrintf("Path not rendered, too many verts (%d)\n", maxPts); |
| 271 | return false; |
| 272 | } |
| 273 | |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 274 | GrVertexLayout layout = 0; |
| 275 | for (int s = 0; s < GrDrawTarget::kNumStages; ++s) { |
| 276 | if ((1 << s) & stages) { |
| 277 | layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s); |
| 278 | } |
| 279 | } |
| 280 | |
bsalomon@google.com | 25fd36c | 2011-07-06 17:41:08 +0000 | [diff] [blame] | 281 | fUseIndexedDraw = fSubpathCount > 1; |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 282 | |
bsalomon@google.com | 25fd36c | 2011-07-06 17:41:08 +0000 | [diff] [blame] | 283 | int maxIdxs = 0; |
| 284 | if (kHairLine_PathFill == fFill) { |
| 285 | if (fUseIndexedDraw) { |
| 286 | maxIdxs = 2 * maxPts; |
| 287 | fPrimitiveType = kLines_PrimitiveType; |
| 288 | } else { |
| 289 | fPrimitiveType = kLineStrip_PrimitiveType; |
| 290 | } |
| 291 | } else { |
| 292 | if (fUseIndexedDraw) { |
| 293 | maxIdxs = 3 * maxPts; |
| 294 | fPrimitiveType = kTriangles_PrimitiveType; |
| 295 | } else { |
| 296 | fPrimitiveType = kTriangleFan_PrimitiveType; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | GrPoint* base; |
bsalomon@google.com | 82866fd | 2011-08-05 16:27:32 +0000 | [diff] [blame] | 301 | if (!fTarget->reserveVertexSpace(layout, maxPts, (void**)&base)) { |
| 302 | return false; |
| 303 | } |
| 304 | GrAssert(NULL != base); |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 305 | GrPoint* vert = base; |
bsalomon@google.com | 25fd36c | 2011-07-06 17:41:08 +0000 | [diff] [blame] | 306 | |
| 307 | uint16_t* idxBase = NULL; |
| 308 | uint16_t* idx = NULL; |
| 309 | uint16_t subpathIdxStart = 0; |
| 310 | if (fUseIndexedDraw) { |
bsalomon@google.com | 82866fd | 2011-08-05 16:27:32 +0000 | [diff] [blame] | 311 | if (!fTarget->reserveIndexSpace(maxIdxs, (void**)&idxBase)) { |
| 312 | fTarget->resetVertexSource(); |
| 313 | return false; |
| 314 | } |
| 315 | GrAssert(NULL != idxBase); |
bsalomon@google.com | 25fd36c | 2011-07-06 17:41:08 +0000 | [diff] [blame] | 316 | idx = idxBase; |
| 317 | } |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 318 | |
| 319 | fSubpathVertCount.realloc(fSubpathCount); |
| 320 | |
| 321 | GrPoint pts[4]; |
| 322 | |
| 323 | bool first = true; |
| 324 | int subpath = 0; |
| 325 | |
| 326 | SkPath::Iter iter(*fPath, false); |
| 327 | |
| 328 | for (;;) { |
| 329 | GrPathCmd cmd = (GrPathCmd)iter.next(pts); |
| 330 | switch (cmd) { |
| 331 | case kMove_PathCmd: |
| 332 | if (!first) { |
bsalomon@google.com | 25fd36c | 2011-07-06 17:41:08 +0000 | [diff] [blame] | 333 | uint16_t currIdx = (uint16_t) (vert - base); |
| 334 | fSubpathVertCount[subpath] = currIdx - subpathIdxStart; |
| 335 | subpathIdxStart = currIdx; |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 336 | ++subpath; |
| 337 | } |
| 338 | *vert = pts[0]; |
| 339 | vert++; |
| 340 | break; |
| 341 | case kLine_PathCmd: |
bsalomon@google.com | 25fd36c | 2011-07-06 17:41:08 +0000 | [diff] [blame] | 342 | if (fUseIndexedDraw) { |
| 343 | uint16_t prevIdx = (uint16_t)(vert - base) - 1; |
| 344 | append_countour_edge_indices(fFill, subpathIdxStart, |
| 345 | prevIdx, &idx); |
| 346 | } |
| 347 | *(vert++) = pts[1]; |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 348 | break; |
| 349 | case kQuadratic_PathCmd: { |
bsalomon@google.com | 25fd36c | 2011-07-06 17:41:08 +0000 | [diff] [blame] | 350 | // first pt of quad is the pt we ended on in previous step |
| 351 | uint16_t firstQPtIdx = (uint16_t)(vert - base) - 1; |
| 352 | uint16_t numPts = (uint16_t) |
| 353 | GrPathUtils::generateQuadraticPoints( |
| 354 | pts[0], pts[1], pts[2], |
| 355 | srcSpaceTolSqd, &vert, |
| 356 | GrPathUtils::quadraticPointCount(pts, srcSpaceTol)); |
| 357 | if (fUseIndexedDraw) { |
| 358 | for (uint16_t i = 0; i < numPts; ++i) { |
| 359 | append_countour_edge_indices(fFill, subpathIdxStart, |
| 360 | firstQPtIdx + i, &idx); |
| 361 | } |
| 362 | } |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 363 | break; |
| 364 | } |
| 365 | case kCubic_PathCmd: { |
bsalomon@google.com | 25fd36c | 2011-07-06 17:41:08 +0000 | [diff] [blame] | 366 | // first pt of cubic is the pt we ended on in previous step |
| 367 | uint16_t firstCPtIdx = (uint16_t)(vert - base) - 1; |
| 368 | uint16_t numPts = (uint16_t) GrPathUtils::generateCubicPoints( |
| 369 | pts[0], pts[1], pts[2], pts[3], |
| 370 | srcSpaceTolSqd, &vert, |
| 371 | GrPathUtils::cubicPointCount(pts, srcSpaceTol)); |
| 372 | if (fUseIndexedDraw) { |
| 373 | for (uint16_t i = 0; i < numPts; ++i) { |
| 374 | append_countour_edge_indices(fFill, subpathIdxStart, |
| 375 | firstCPtIdx + i, &idx); |
| 376 | } |
| 377 | } |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 378 | break; |
| 379 | } |
| 380 | case kClose_PathCmd: |
| 381 | break; |
| 382 | case kEnd_PathCmd: |
bsalomon@google.com | 25fd36c | 2011-07-06 17:41:08 +0000 | [diff] [blame] | 383 | uint16_t currIdx = (uint16_t) (vert - base); |
| 384 | fSubpathVertCount[subpath] = currIdx - subpathIdxStart; |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 385 | goto FINISHED; |
| 386 | } |
| 387 | first = false; |
| 388 | } |
| 389 | FINISHED: |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 390 | GrAssert((vert - base) <= maxPts); |
bsalomon@google.com | 25fd36c | 2011-07-06 17:41:08 +0000 | [diff] [blame] | 391 | GrAssert((idx - idxBase) <= maxIdxs); |
| 392 | |
| 393 | fVertexCnt = vert - base; |
| 394 | fIndexCnt = idx - idxBase; |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 395 | |
| 396 | if (fTranslate.fX || fTranslate.fY) { |
| 397 | int count = vert - base; |
| 398 | for (int i = 0; i < count; i++) { |
| 399 | base[i].offset(fTranslate.fX, fTranslate.fY); |
| 400 | } |
| 401 | } |
| 402 | } |
bsalomon@google.com | 82866fd | 2011-08-05 16:27:32 +0000 | [diff] [blame] | 403 | // set these at the end so if we failed on first drawPath inside a |
| 404 | // setPath/clearPath block we won't assume geom was created on a subsequent |
| 405 | // drawPath in the same block. |
| 406 | fPreviousSrcTol = srcSpaceTol; |
| 407 | fPreviousStages = stages; |
bsalomon@google.com | 25fd36c | 2011-07-06 17:41:08 +0000 | [diff] [blame] | 408 | return true; |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | void GrDefaultPathRenderer::onDrawPath(GrDrawTarget::StageBitfield stages, |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 412 | bool stencilOnly) { |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 413 | |
tomhudson@google.com | 278cbb4 | 2011-06-30 19:37:01 +0000 | [diff] [blame] | 414 | SK_TRACE_EVENT1("GrDefaultPathRenderer::onDrawPath", |
| 415 | "points", SkStringPrintf("%i", path.countPoints()).c_str()); |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 416 | |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 417 | GrMatrix viewM = fTarget->getViewMatrix(); |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 418 | // In order to tesselate the path we get a bound on how much the matrix can |
| 419 | // stretch when mapping to screen coordinates. |
| 420 | GrScalar stretch = viewM.getMaxStretch(); |
| 421 | bool useStretch = stretch > 0; |
tomhudson@google.com | d22b6e4 | 2011-06-24 15:53:40 +0000 | [diff] [blame] | 422 | GrScalar tol = fCurveTolerance; |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 423 | |
| 424 | if (!useStretch) { |
| 425 | // TODO: deal with perspective in some better way. |
| 426 | tol /= 10; |
| 427 | } else { |
bungeman@google.com | 8c5753e | 2011-05-20 19:11:50 +0000 | [diff] [blame] | 428 | tol = GrScalarDiv(tol, stretch); |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 429 | } |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 430 | // FIXME: It's really dumb that we recreate the verts for a new vertex |
| 431 | // layout. We only do that because the GrDrawTarget API doesn't allow |
| 432 | // us to change the vertex layout after reserveVertexSpace(). We won't |
| 433 | // actually change the vertex data when the layout changes since all the |
| 434 | // stages reference the positions (rather than having separate tex coords) |
| 435 | // and we don't ever have per-vert colors. In practice our call sites |
| 436 | // won't change the stages in use inside a setPath / removePath pair. But |
| 437 | // it is a silly limitation of the GrDrawTarget design that should be fixed. |
| 438 | if (tol != fPreviousSrcTol || |
| 439 | stages != fPreviousStages) { |
bsalomon@google.com | 25fd36c | 2011-07-06 17:41:08 +0000 | [diff] [blame] | 440 | if (!this->createGeom(tol, stages)) { |
| 441 | return; |
| 442 | } |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 443 | } |
| 444 | |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 445 | GrAssert(NULL != fTarget); |
| 446 | GrDrawTarget::AutoStateRestore asr(fTarget); |
| 447 | bool colorWritesWereDisabled = fTarget->isColorWriteDisabled(); |
| 448 | // face culling doesn't make sense here |
| 449 | GrAssert(GrDrawTarget::kBoth_DrawFace == fTarget->getDrawFace()); |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 450 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 451 | int passCount = 0; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 452 | const GrStencilSettings* passes[3]; |
| 453 | GrDrawTarget::DrawFace drawFace[3]; |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 454 | bool reverse = false; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 455 | bool lastPassIsBounds; |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 456 | |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 457 | if (kHairLine_PathFill == fFill) { |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 458 | passCount = 1; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 459 | if (stencilOnly) { |
| 460 | passes[0] = &gDirectToStencil; |
| 461 | } else { |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 462 | passes[0] = NULL; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 463 | } |
| 464 | lastPassIsBounds = false; |
| 465 | drawFace[0] = GrDrawTarget::kBoth_DrawFace; |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 466 | } else { |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 467 | if (single_pass_path(*fTarget, *fPath, fFill)) { |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 468 | passCount = 1; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 469 | if (stencilOnly) { |
| 470 | passes[0] = &gDirectToStencil; |
| 471 | } else { |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 472 | passes[0] = NULL; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 473 | } |
| 474 | drawFace[0] = GrDrawTarget::kBoth_DrawFace; |
| 475 | lastPassIsBounds = false; |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 476 | } else { |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 477 | switch (fFill) { |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 478 | case kInverseEvenOdd_PathFill: |
| 479 | reverse = true; |
| 480 | // fallthrough |
| 481 | case kEvenOdd_PathFill: |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 482 | passes[0] = &gEOStencilPass; |
| 483 | if (stencilOnly) { |
| 484 | passCount = 1; |
| 485 | lastPassIsBounds = false; |
| 486 | } else { |
| 487 | passCount = 2; |
| 488 | lastPassIsBounds = true; |
| 489 | if (reverse) { |
| 490 | passes[1] = &gInvEOColorPass; |
| 491 | } else { |
| 492 | passes[1] = &gEOColorPass; |
| 493 | } |
| 494 | } |
| 495 | drawFace[0] = drawFace[1] = GrDrawTarget::kBoth_DrawFace; |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 496 | break; |
| 497 | |
| 498 | case kInverseWinding_PathFill: |
| 499 | reverse = true; |
| 500 | // fallthrough |
| 501 | case kWinding_PathFill: |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 502 | if (fSeparateStencil) { |
| 503 | if (fStencilWrapOps) { |
| 504 | passes[0] = &gWindStencilSeparateWithWrap; |
| 505 | } else { |
| 506 | passes[0] = &gWindStencilSeparateNoWrap; |
| 507 | } |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 508 | passCount = 2; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 509 | drawFace[0] = GrDrawTarget::kBoth_DrawFace; |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 510 | } else { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 511 | if (fStencilWrapOps) { |
| 512 | passes[0] = &gWindSingleStencilWithWrapInc; |
| 513 | passes[1] = &gWindSingleStencilWithWrapDec; |
| 514 | } else { |
| 515 | passes[0] = &gWindSingleStencilNoWrapInc; |
| 516 | passes[1] = &gWindSingleStencilNoWrapDec; |
| 517 | } |
| 518 | // which is cw and which is ccw is arbitrary. |
| 519 | drawFace[0] = GrDrawTarget::kCW_DrawFace; |
| 520 | drawFace[1] = GrDrawTarget::kCCW_DrawFace; |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 521 | passCount = 3; |
| 522 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 523 | if (stencilOnly) { |
| 524 | lastPassIsBounds = false; |
| 525 | --passCount; |
| 526 | } else { |
| 527 | lastPassIsBounds = true; |
| 528 | drawFace[passCount-1] = GrDrawTarget::kBoth_DrawFace; |
| 529 | if (reverse) { |
| 530 | passes[passCount-1] = &gInvWindColorPass; |
| 531 | } else { |
| 532 | passes[passCount-1] = &gWindColorPass; |
| 533 | } |
| 534 | } |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 535 | break; |
| 536 | default: |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 537 | GrAssert(!"Unknown path fFill!"); |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 538 | return; |
| 539 | } |
| 540 | } |
| 541 | } |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 542 | |
tomhudson@google.com | 278cbb4 | 2011-06-30 19:37:01 +0000 | [diff] [blame] | 543 | { |
| 544 | SK_TRACE_EVENT1("GrDefaultPathRenderer::onDrawPath::renderPasses", |
| 545 | "verts", SkStringPrintf("%i", vert - base).c_str()); |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 546 | for (int p = 0; p < passCount; ++p) { |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 547 | fTarget->setDrawFace(drawFace[p]); |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 548 | if (NULL != passes[p]) { |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 549 | fTarget->setStencil(*passes[p]); |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 550 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 551 | |
| 552 | if (lastPassIsBounds && (p == passCount-1)) { |
| 553 | if (!colorWritesWereDisabled) { |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 554 | fTarget->disableState(GrDrawTarget::kNoColorWrites_StateBit); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 555 | } |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 556 | GrRect bounds; |
| 557 | if (reverse) { |
| 558 | GrAssert(NULL != fTarget->getRenderTarget()); |
| 559 | // draw over the whole world. |
| 560 | bounds.setLTRB(0, 0, |
| 561 | GrIntToScalar(fTarget->getRenderTarget()->width()), |
| 562 | GrIntToScalar(fTarget->getRenderTarget()->height())); |
| 563 | GrMatrix vmi; |
| 564 | if (fTarget->getViewInverse(&vmi)) { |
| 565 | vmi.mapRect(&bounds); |
| 566 | } |
| 567 | } else { |
| 568 | bounds = fPath->getBounds(); |
bsalomon@google.com | fc89927 | 2011-07-01 22:10:30 +0000 | [diff] [blame] | 569 | bounds.offset(fTranslate); |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 570 | } |
| 571 | GrDrawTarget::AutoGeometryPush agp(fTarget); |
| 572 | fTarget->drawSimpleRect(bounds, NULL, stages); |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 573 | } else { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 574 | if (passCount > 1) { |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 575 | fTarget->enableState(GrDrawTarget::kNoColorWrites_StateBit); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 576 | } |
bsalomon@google.com | 25fd36c | 2011-07-06 17:41:08 +0000 | [diff] [blame] | 577 | if (fUseIndexedDraw) { |
| 578 | fTarget->drawIndexed(fPrimitiveType, 0, 0, |
| 579 | fVertexCnt, fIndexCnt); |
| 580 | } else { |
| 581 | int baseVertex = 0; |
| 582 | for (int sp = 0; sp < fSubpathCount; ++sp) { |
| 583 | fTarget->drawNonIndexed(fPrimitiveType, baseVertex, |
| 584 | fSubpathVertCount[sp]); |
| 585 | baseVertex += fSubpathVertCount[sp]; |
| 586 | } |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | } |
tomhudson@google.com | 278cbb4 | 2011-06-30 19:37:01 +0000 | [diff] [blame] | 590 | } |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 591 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 592 | |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 593 | void GrDefaultPathRenderer::drawPath(GrDrawTarget::StageBitfield stages) { |
| 594 | this->onDrawPath(stages, false); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 595 | } |
| 596 | |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 597 | void GrDefaultPathRenderer::drawPathToStencil() { |
| 598 | GrAssert(kInverseEvenOdd_PathFill != fFill); |
| 599 | GrAssert(kInverseWinding_PathFill != fFill); |
| 600 | this->onDrawPath(0, true); |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 601 | } |