bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +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 | */ |
| 8 | |
| 9 | #include "GrDefaultPathRenderer.h" |
| 10 | |
| 11 | #include "GrContext.h" |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 12 | #include "GrDrawState.h" |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 13 | #include "GrPathUtils.h" |
tomhudson@google.com | dd5f744 | 2011-08-30 15:13:55 +0000 | [diff] [blame] | 14 | #include "SkString.h" |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 15 | #include "SkTrace.h" |
| 16 | |
| 17 | |
| 18 | GrDefaultPathRenderer::GrDefaultPathRenderer(bool separateStencilSupport, |
| 19 | bool stencilWrapOpsSupport) |
| 20 | : fSeparateStencil(separateStencilSupport) |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 21 | , fStencilWrapOps(stencilWrapOpsSupport) { |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 25 | //////////////////////////////////////////////////////////////////////////////// |
| 26 | // Stencil rules for paths |
| 27 | |
| 28 | ////// Even/Odd |
| 29 | |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 30 | GR_STATIC_CONST_SAME_STENCIL(gEOStencilPass, |
| 31 | kInvert_StencilOp, |
| 32 | kKeep_StencilOp, |
| 33 | kAlwaysIfInClip_StencilFunc, |
| 34 | 0xffff, |
| 35 | 0xffff, |
| 36 | 0xffff); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 37 | |
| 38 | // 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] | 39 | GR_STATIC_CONST_SAME_STENCIL(gEOColorPass, |
| 40 | kZero_StencilOp, |
| 41 | kZero_StencilOp, |
| 42 | kNotEqual_StencilFunc, |
| 43 | 0xffff, |
| 44 | 0x0000, |
| 45 | 0xffff); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 46 | |
| 47 | // 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] | 48 | GR_STATIC_CONST_SAME_STENCIL(gInvEOColorPass, |
| 49 | kZero_StencilOp, |
| 50 | kZero_StencilOp, |
| 51 | kEqualIfInClip_StencilFunc, |
| 52 | 0xffff, |
| 53 | 0x0000, |
| 54 | 0xffff); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 55 | |
| 56 | ////// Winding |
| 57 | |
| 58 | // when we have separate stencil we increment front faces / decrement back faces |
| 59 | // when we don't have wrap incr and decr we use the stencil test to simulate |
| 60 | // them. |
| 61 | |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 62 | GR_STATIC_CONST_STENCIL(gWindStencilSeparateWithWrap, |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 63 | kIncWrap_StencilOp, kDecWrap_StencilOp, |
| 64 | kKeep_StencilOp, kKeep_StencilOp, |
| 65 | kAlwaysIfInClip_StencilFunc, kAlwaysIfInClip_StencilFunc, |
tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 66 | 0xffff, 0xffff, |
| 67 | 0xffff, 0xffff, |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 68 | 0xffff, 0xffff); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 69 | |
| 70 | // if inc'ing the max value, invert to make 0 |
| 71 | // if dec'ing zero invert to make all ones. |
| 72 | // we can't avoid touching the stencil on both passing and |
| 73 | // failing, so we can't resctrict ourselves to the clip. |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 74 | GR_STATIC_CONST_STENCIL(gWindStencilSeparateNoWrap, |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 75 | kInvert_StencilOp, kInvert_StencilOp, |
| 76 | kIncClamp_StencilOp, kDecClamp_StencilOp, |
| 77 | kEqual_StencilFunc, kEqual_StencilFunc, |
tomhudson@google.com | 62b0968 | 2011-11-09 16:39:17 +0000 | [diff] [blame] | 78 | 0xffff, 0xffff, |
| 79 | 0xffff, 0x0000, |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 80 | 0xffff, 0xffff); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 81 | |
| 82 | // When there are no separate faces we do two passes to setup the winding rule |
| 83 | // stencil. First we draw the front faces and inc, then we draw the back faces |
| 84 | // and dec. These are same as the above two split into the incrementing and |
| 85 | // decrementing passes. |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 86 | GR_STATIC_CONST_SAME_STENCIL(gWindSingleStencilWithWrapInc, |
| 87 | kIncWrap_StencilOp, |
| 88 | kKeep_StencilOp, |
| 89 | kAlwaysIfInClip_StencilFunc, |
| 90 | 0xffff, |
| 91 | 0xffff, |
| 92 | 0xffff); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 93 | |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 94 | GR_STATIC_CONST_SAME_STENCIL(gWindSingleStencilWithWrapDec, |
| 95 | kDecWrap_StencilOp, |
| 96 | kKeep_StencilOp, |
| 97 | kAlwaysIfInClip_StencilFunc, |
| 98 | 0xffff, |
| 99 | 0xffff, |
| 100 | 0xffff); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 101 | |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 102 | GR_STATIC_CONST_SAME_STENCIL(gWindSingleStencilNoWrapInc, |
| 103 | kInvert_StencilOp, |
| 104 | kIncClamp_StencilOp, |
| 105 | kEqual_StencilFunc, |
| 106 | 0xffff, |
| 107 | 0xffff, |
| 108 | 0xffff); |
| 109 | |
| 110 | GR_STATIC_CONST_SAME_STENCIL(gWindSingleStencilNoWrapDec, |
| 111 | kInvert_StencilOp, |
| 112 | kDecClamp_StencilOp, |
| 113 | kEqual_StencilFunc, |
| 114 | 0xffff, |
| 115 | 0x0000, |
| 116 | 0xffff); |
| 117 | |
| 118 | // Color passes are the same whether we use the two-sided stencil or two passes |
| 119 | |
| 120 | GR_STATIC_CONST_SAME_STENCIL(gWindColorPass, |
| 121 | kZero_StencilOp, |
| 122 | kZero_StencilOp, |
| 123 | kNonZeroIfInClip_StencilFunc, |
| 124 | 0xffff, |
| 125 | 0x0000, |
| 126 | 0xffff); |
| 127 | |
| 128 | GR_STATIC_CONST_SAME_STENCIL(gInvWindColorPass, |
| 129 | kZero_StencilOp, |
| 130 | kZero_StencilOp, |
| 131 | kEqualIfInClip_StencilFunc, |
| 132 | 0xffff, |
| 133 | 0x0000, |
| 134 | 0xffff); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 135 | |
| 136 | ////// Normal render to stencil |
| 137 | |
| 138 | // Sometimes the default path renderer can draw a path directly to the stencil |
| 139 | // buffer without having to first resolve the interior / exterior. |
bsalomon@google.com | 6b2445e | 2011-12-15 19:47:46 +0000 | [diff] [blame] | 140 | GR_STATIC_CONST_SAME_STENCIL(gDirectToStencil, |
| 141 | kZero_StencilOp, |
| 142 | kIncClamp_StencilOp, |
| 143 | kAlwaysIfInClip_StencilFunc, |
| 144 | 0xffff, |
| 145 | 0x0000, |
| 146 | 0xffff); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 147 | |
| 148 | //////////////////////////////////////////////////////////////////////////////// |
| 149 | // Helpers for drawPath |
| 150 | |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 151 | #define STENCIL_OFF 0 // Always disable stencil (even when needed) |
| 152 | |
bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 153 | static inline bool single_pass_path(const SkPath& path, GrPathFill fill) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 154 | #if STENCIL_OFF |
| 155 | return true; |
| 156 | #else |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 157 | if (kEvenOdd_GrPathFill == fill || kWinding_GrPathFill == fill) { |
bsalomon@google.com | 7d72c45 | 2012-01-30 14:02:44 +0000 | [diff] [blame] | 158 | return path.isConvex(); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 159 | } |
| 160 | return false; |
| 161 | #endif |
| 162 | } |
| 163 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 164 | bool GrDefaultPathRenderer::requiresStencilPass(const SkPath& path, |
| 165 | GrPathFill fill, |
| 166 | const GrDrawTarget* target) const { |
bsalomon@google.com | 7d72c45 | 2012-01-30 14:02:44 +0000 | [diff] [blame] | 167 | return !single_pass_path(path, fill); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 168 | } |
| 169 | |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 170 | static inline void append_countour_edge_indices(GrPathFill fillType, |
| 171 | uint16_t fanCenterIdx, |
| 172 | uint16_t edgeV0Idx, |
| 173 | uint16_t** indices) { |
| 174 | // when drawing lines we're appending line segments along |
| 175 | // the contour. When applying the other fill rules we're |
| 176 | // drawing triangle fans around fanCenterIdx. |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 177 | if (kHairLine_GrPathFill != fillType) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 178 | *((*indices)++) = fanCenterIdx; |
| 179 | } |
| 180 | *((*indices)++) = edgeV0Idx; |
| 181 | *((*indices)++) = edgeV0Idx + 1; |
| 182 | } |
| 183 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 184 | bool GrDefaultPathRenderer::createGeom(const SkPath& path, |
| 185 | GrPathFill fill, |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame^] | 186 | SkScalar srcSpaceTol, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 187 | GrDrawTarget* target, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 188 | GrPrimitiveType* primType, |
| 189 | int* vertexCnt, |
bsalomon@google.com | b372942 | 2012-03-07 19:13:28 +0000 | [diff] [blame] | 190 | int* indexCnt, |
| 191 | GrDrawTarget::AutoReleaseGeometry* arg) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 192 | { |
| 193 | SK_TRACE_EVENT0("GrDefaultPathRenderer::createGeom"); |
| 194 | |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame^] | 195 | SkScalar srcSpaceTolSqd = SkScalarMul(srcSpaceTol, srcSpaceTol); |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 196 | int contourCnt; |
| 197 | int maxPts = GrPathUtils::worstCasePointCount(path, &contourCnt, |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 198 | srcSpaceTol); |
| 199 | |
| 200 | if (maxPts <= 0) { |
| 201 | return false; |
| 202 | } |
| 203 | if (maxPts > ((int)SK_MaxU16 + 1)) { |
| 204 | GrPrintf("Path not rendered, too many verts (%d)\n", maxPts); |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | GrVertexLayout layout = 0; |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 209 | bool indexed = contourCnt > 1; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 210 | |
| 211 | int maxIdxs = 0; |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 212 | if (kHairLine_GrPathFill == fill) { |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 213 | if (indexed) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 214 | maxIdxs = 2 * maxPts; |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 215 | *primType = kLines_GrPrimitiveType; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 216 | } else { |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 217 | *primType = kLineStrip_GrPrimitiveType; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 218 | } |
| 219 | } else { |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 220 | if (indexed) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 221 | maxIdxs = 3 * maxPts; |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 222 | *primType = kTriangles_GrPrimitiveType; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 223 | } else { |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 224 | *primType = kTriangleFan_GrPrimitiveType; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
bsalomon@google.com | b372942 | 2012-03-07 19:13:28 +0000 | [diff] [blame] | 228 | |
| 229 | if (!arg->set(target, layout, maxPts, maxIdxs)) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 230 | return false; |
| 231 | } |
bsalomon@google.com | b372942 | 2012-03-07 19:13:28 +0000 | [diff] [blame] | 232 | |
| 233 | uint16_t* idxBase = reinterpret_cast<uint16_t*>(arg->indices());; |
| 234 | uint16_t* idx = idxBase; |
| 235 | uint16_t subpathIdxStart = 0; |
| 236 | |
| 237 | GrPoint* base = reinterpret_cast<GrPoint*>(arg->vertices()); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 238 | GrAssert(NULL != base); |
| 239 | GrPoint* vert = base; |
| 240 | |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 241 | GrPoint pts[4]; |
| 242 | |
| 243 | bool first = true; |
| 244 | int subpath = 0; |
| 245 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 246 | SkPath::Iter iter(path, false); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 247 | |
| 248 | for (;;) { |
| 249 | GrPathCmd cmd = (GrPathCmd)iter.next(pts); |
| 250 | switch (cmd) { |
| 251 | case kMove_PathCmd: |
| 252 | if (!first) { |
| 253 | uint16_t currIdx = (uint16_t) (vert - base); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 254 | subpathIdxStart = currIdx; |
| 255 | ++subpath; |
| 256 | } |
| 257 | *vert = pts[0]; |
| 258 | vert++; |
| 259 | break; |
| 260 | case kLine_PathCmd: |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 261 | if (indexed) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 262 | uint16_t prevIdx = (uint16_t)(vert - base) - 1; |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 263 | append_countour_edge_indices(fill, subpathIdxStart, |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 264 | prevIdx, &idx); |
| 265 | } |
| 266 | *(vert++) = pts[1]; |
| 267 | break; |
| 268 | case kQuadratic_PathCmd: { |
| 269 | // first pt of quad is the pt we ended on in previous step |
| 270 | uint16_t firstQPtIdx = (uint16_t)(vert - base) - 1; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 271 | uint16_t numPts = (uint16_t) |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 272 | GrPathUtils::generateQuadraticPoints( |
| 273 | pts[0], pts[1], pts[2], |
| 274 | srcSpaceTolSqd, &vert, |
| 275 | GrPathUtils::quadraticPointCount(pts, srcSpaceTol)); |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 276 | if (indexed) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 277 | for (uint16_t i = 0; i < numPts; ++i) { |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 278 | append_countour_edge_indices(fill, subpathIdxStart, |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 279 | firstQPtIdx + i, &idx); |
| 280 | } |
| 281 | } |
| 282 | break; |
| 283 | } |
| 284 | case kCubic_PathCmd: { |
| 285 | // first pt of cubic is the pt we ended on in previous step |
| 286 | uint16_t firstCPtIdx = (uint16_t)(vert - base) - 1; |
| 287 | uint16_t numPts = (uint16_t) GrPathUtils::generateCubicPoints( |
| 288 | pts[0], pts[1], pts[2], pts[3], |
| 289 | srcSpaceTolSqd, &vert, |
| 290 | GrPathUtils::cubicPointCount(pts, srcSpaceTol)); |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 291 | if (indexed) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 292 | for (uint16_t i = 0; i < numPts; ++i) { |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 293 | append_countour_edge_indices(fill, subpathIdxStart, |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 294 | firstCPtIdx + i, &idx); |
| 295 | } |
| 296 | } |
| 297 | break; |
| 298 | } |
| 299 | case kClose_PathCmd: |
| 300 | break; |
| 301 | case kEnd_PathCmd: |
caryclark@google.com | cf6285b | 2012-06-06 12:09:01 +0000 | [diff] [blame] | 302 | // uint16_t currIdx = (uint16_t) (vert - base); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 303 | goto FINISHED; |
| 304 | } |
| 305 | first = false; |
| 306 | } |
| 307 | FINISHED: |
| 308 | GrAssert((vert - base) <= maxPts); |
| 309 | GrAssert((idx - idxBase) <= maxIdxs); |
| 310 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 311 | *vertexCnt = vert - base; |
| 312 | *indexCnt = idx - idxBase; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 313 | |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 314 | } |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 315 | return true; |
| 316 | } |
| 317 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 318 | bool GrDefaultPathRenderer::internalDrawPath(const SkPath& path, |
| 319 | GrPathFill fill, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 320 | GrDrawTarget* target, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 321 | bool stencilOnly) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 322 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 323 | GrMatrix viewM = target->getDrawState().getViewMatrix(); |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame^] | 324 | SkScalar tol = SK_Scalar1; |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 325 | tol = GrPathUtils::scaleToleranceToSrc(tol, viewM, path.getBounds()); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 326 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 327 | int vertexCnt; |
| 328 | int indexCnt; |
| 329 | GrPrimitiveType primType; |
bsalomon@google.com | b372942 | 2012-03-07 19:13:28 +0000 | [diff] [blame] | 330 | GrDrawTarget::AutoReleaseGeometry arg; |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 331 | if (!this->createGeom(path, |
| 332 | fill, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 333 | tol, |
| 334 | target, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 335 | &primType, |
| 336 | &vertexCnt, |
bsalomon@google.com | b372942 | 2012-03-07 19:13:28 +0000 | [diff] [blame] | 337 | &indexCnt, |
| 338 | &arg)) { |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 339 | return false; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 340 | } |
| 341 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 342 | GrAssert(NULL != target); |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 343 | GrDrawTarget::AutoStateRestore asr(target, GrDrawTarget::kPreserve_ASRInit); |
| 344 | GrDrawState* drawState = target->drawState(); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 345 | bool colorWritesWereDisabled = drawState->isColorWriteDisabled(); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 346 | // face culling doesn't make sense here |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 347 | GrAssert(GrDrawState::kBoth_DrawFace == drawState->getDrawFace()); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 348 | |
| 349 | int passCount = 0; |
| 350 | const GrStencilSettings* passes[3]; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 351 | GrDrawState::DrawFace drawFace[3]; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 352 | bool reverse = false; |
| 353 | bool lastPassIsBounds; |
| 354 | |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 355 | if (kHairLine_GrPathFill == fill) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 356 | passCount = 1; |
| 357 | if (stencilOnly) { |
| 358 | passes[0] = &gDirectToStencil; |
| 359 | } else { |
| 360 | passes[0] = NULL; |
| 361 | } |
| 362 | lastPassIsBounds = false; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 363 | drawFace[0] = GrDrawState::kBoth_DrawFace; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 364 | } else { |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 365 | if (single_pass_path(path, fill)) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 366 | passCount = 1; |
| 367 | if (stencilOnly) { |
| 368 | passes[0] = &gDirectToStencil; |
| 369 | } else { |
| 370 | passes[0] = NULL; |
| 371 | } |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 372 | drawFace[0] = GrDrawState::kBoth_DrawFace; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 373 | lastPassIsBounds = false; |
| 374 | } else { |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 375 | switch (fill) { |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 376 | case kInverseEvenOdd_GrPathFill: |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 377 | reverse = true; |
| 378 | // fallthrough |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 379 | case kEvenOdd_GrPathFill: |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 380 | passes[0] = &gEOStencilPass; |
| 381 | if (stencilOnly) { |
| 382 | passCount = 1; |
| 383 | lastPassIsBounds = false; |
| 384 | } else { |
| 385 | passCount = 2; |
| 386 | lastPassIsBounds = true; |
| 387 | if (reverse) { |
| 388 | passes[1] = &gInvEOColorPass; |
| 389 | } else { |
| 390 | passes[1] = &gEOColorPass; |
| 391 | } |
| 392 | } |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 393 | drawFace[0] = drawFace[1] = GrDrawState::kBoth_DrawFace; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 394 | break; |
| 395 | |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 396 | case kInverseWinding_GrPathFill: |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 397 | reverse = true; |
| 398 | // fallthrough |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 399 | case kWinding_GrPathFill: |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 400 | if (fSeparateStencil) { |
| 401 | if (fStencilWrapOps) { |
| 402 | passes[0] = &gWindStencilSeparateWithWrap; |
| 403 | } else { |
| 404 | passes[0] = &gWindStencilSeparateNoWrap; |
| 405 | } |
| 406 | passCount = 2; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 407 | drawFace[0] = GrDrawState::kBoth_DrawFace; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 408 | } else { |
| 409 | if (fStencilWrapOps) { |
| 410 | passes[0] = &gWindSingleStencilWithWrapInc; |
| 411 | passes[1] = &gWindSingleStencilWithWrapDec; |
| 412 | } else { |
| 413 | passes[0] = &gWindSingleStencilNoWrapInc; |
| 414 | passes[1] = &gWindSingleStencilNoWrapDec; |
| 415 | } |
| 416 | // which is cw and which is ccw is arbitrary. |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 417 | drawFace[0] = GrDrawState::kCW_DrawFace; |
| 418 | drawFace[1] = GrDrawState::kCCW_DrawFace; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 419 | passCount = 3; |
| 420 | } |
| 421 | if (stencilOnly) { |
| 422 | lastPassIsBounds = false; |
| 423 | --passCount; |
| 424 | } else { |
| 425 | lastPassIsBounds = true; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 426 | drawFace[passCount-1] = GrDrawState::kBoth_DrawFace; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 427 | if (reverse) { |
| 428 | passes[passCount-1] = &gInvWindColorPass; |
| 429 | } else { |
| 430 | passes[passCount-1] = &gWindColorPass; |
| 431 | } |
| 432 | } |
| 433 | break; |
| 434 | default: |
| 435 | GrAssert(!"Unknown path fFill!"); |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 436 | return false; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 442 | for (int p = 0; p < passCount; ++p) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 443 | drawState->setDrawFace(drawFace[p]); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 444 | if (NULL != passes[p]) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 445 | *drawState->stencil() = *passes[p]; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | if (lastPassIsBounds && (p == passCount-1)) { |
| 449 | if (!colorWritesWereDisabled) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 450 | drawState->disableState(GrDrawState::kNoColorWrites_StateBit); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 451 | } |
| 452 | GrRect bounds; |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 453 | GrDrawState::AutoDeviceCoordDraw adcd; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 454 | if (reverse) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 455 | GrAssert(NULL != drawState->getRenderTarget()); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 456 | // draw over the whole world. |
| 457 | bounds.setLTRB(0, 0, |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame^] | 458 | SkIntToScalar(drawState->getRenderTarget()->width()), |
| 459 | SkIntToScalar(drawState->getRenderTarget()->height())); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 460 | GrMatrix vmi; |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 461 | // mapRect through persp matrix may not be correct |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 462 | if (!drawState->getViewMatrix().hasPerspective() && |
| 463 | drawState->getViewInverse(&vmi)) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 464 | vmi.mapRect(&bounds); |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 465 | } else { |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 466 | adcd.set(drawState); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 467 | } |
| 468 | } else { |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 469 | bounds = path.getBounds(); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 470 | } |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 471 | GrDrawTarget::AutoGeometryPush agp(target); |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 472 | target->drawSimpleRect(bounds, NULL); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 473 | } else { |
| 474 | if (passCount > 1) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 475 | drawState->enableState(GrDrawState::kNoColorWrites_StateBit); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 476 | } |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 477 | if (indexCnt) { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 478 | target->drawIndexed(primType, 0, 0, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 479 | vertexCnt, indexCnt); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 480 | } else { |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 481 | target->drawNonIndexed(primType, 0, vertexCnt); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | } |
| 485 | } |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 486 | return true; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 487 | } |
| 488 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 489 | bool GrDefaultPathRenderer::canDrawPath(const SkPath& path, |
| 490 | GrPathFill fill, |
| 491 | const GrDrawTarget* target, |
| 492 | bool antiAlias) const { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 493 | // this class can draw any path with any fill but doesn't do any |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 494 | // anti-aliasing. |
| 495 | return !antiAlias; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 496 | } |
| 497 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 498 | bool GrDefaultPathRenderer::onDrawPath(const SkPath& path, |
| 499 | GrPathFill fill, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 500 | GrDrawTarget* target, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 501 | bool antiAlias) { |
| 502 | return this->internalDrawPath(path, |
| 503 | fill, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 504 | target, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 505 | false); |
| 506 | } |
| 507 | |
| 508 | void GrDefaultPathRenderer::drawPathToStencil(const SkPath& path, |
| 509 | GrPathFill fill, |
| 510 | GrDrawTarget* target) { |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 511 | GrAssert(kInverseEvenOdd_GrPathFill != fill); |
| 512 | GrAssert(kInverseWinding_GrPathFill != fill); |
bsalomon@google.com | 0f11e1a | 2012-10-08 14:48:36 +0000 | [diff] [blame] | 513 | this->internalDrawPath(path, fill, target, true); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 514 | } |