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 | |
| 10 | #include "GrContext.h" |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 11 | #include "GrDrawState.h" |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 12 | #include "GrPathUtils.h" |
tomhudson@google.com | dd5f744 | 2011-08-30 15:13:55 +0000 | [diff] [blame] | 13 | #include "SkString.h" |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 14 | #include "SkStrokeRec.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 | |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 153 | 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] | 154 | #if STENCIL_OFF |
| 155 | return true; |
| 156 | #else |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 157 | if (!stroke.isHairlineStyle() && !path.isInverseFillType()) { |
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 | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 164 | GrPathRenderer::StencilSupport GrDefaultPathRenderer::onGetStencilSupport( |
| 165 | const SkPath& path, |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 166 | const SkStrokeRec& stroke, |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 167 | const GrDrawTarget*) const { |
| 168 | if (single_pass_path(path, stroke)) { |
| 169 | return GrPathRenderer::kNoRestriction_StencilSupport; |
| 170 | } else { |
| 171 | return GrPathRenderer::kStencilOnly_StencilSupport; |
| 172 | } |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 173 | } |
| 174 | |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 175 | static inline void append_countour_edge_indices(bool hairLine, |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 176 | uint16_t fanCenterIdx, |
| 177 | uint16_t edgeV0Idx, |
| 178 | uint16_t** indices) { |
| 179 | // when drawing lines we're appending line segments along |
| 180 | // the contour. When applying the other fill rules we're |
| 181 | // drawing triangle fans around fanCenterIdx. |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 182 | if (!hairLine) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 183 | *((*indices)++) = fanCenterIdx; |
| 184 | } |
| 185 | *((*indices)++) = edgeV0Idx; |
| 186 | *((*indices)++) = edgeV0Idx + 1; |
| 187 | } |
| 188 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 189 | bool GrDefaultPathRenderer::createGeom(const SkPath& path, |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 190 | const SkStrokeRec& stroke, |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 191 | SkScalar srcSpaceTol, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 192 | GrDrawTarget* target, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 193 | GrPrimitiveType* primType, |
| 194 | int* vertexCnt, |
bsalomon@google.com | b372942 | 2012-03-07 19:13:28 +0000 | [diff] [blame] | 195 | int* indexCnt, |
| 196 | GrDrawTarget::AutoReleaseGeometry* arg) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 197 | { |
| 198 | SK_TRACE_EVENT0("GrDefaultPathRenderer::createGeom"); |
| 199 | |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 200 | SkScalar srcSpaceTolSqd = SkScalarMul(srcSpaceTol, srcSpaceTol); |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 201 | int contourCnt; |
| 202 | int maxPts = GrPathUtils::worstCasePointCount(path, &contourCnt, |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 203 | srcSpaceTol); |
| 204 | |
| 205 | if (maxPts <= 0) { |
| 206 | return false; |
| 207 | } |
| 208 | if (maxPts > ((int)SK_MaxU16 + 1)) { |
| 209 | GrPrintf("Path not rendered, too many verts (%d)\n", maxPts); |
| 210 | return false; |
| 211 | } |
| 212 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 213 | bool indexed = contourCnt > 1; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 214 | |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 215 | const bool isHairline = stroke.isHairlineStyle(); |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 216 | |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 217 | int maxIdxs = 0; |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 218 | if (isHairline) { |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 219 | if (indexed) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 220 | maxIdxs = 2 * maxPts; |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 221 | *primType = kLines_GrPrimitiveType; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 222 | } else { |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 223 | *primType = kLineStrip_GrPrimitiveType; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 224 | } |
| 225 | } else { |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 226 | if (indexed) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 227 | maxIdxs = 3 * maxPts; |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 228 | *primType = kTriangles_GrPrimitiveType; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 229 | } else { |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 230 | *primType = kTriangleFan_GrPrimitiveType; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 234 | target->drawState()->setDefaultVertexAttribs(); |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 235 | if (!arg->set(target, maxPts, maxIdxs)) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 236 | return false; |
| 237 | } |
bsalomon@google.com | b372942 | 2012-03-07 19:13:28 +0000 | [diff] [blame] | 238 | |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 239 | uint16_t* idxBase = reinterpret_cast<uint16_t*>(arg->indices()); |
bsalomon@google.com | b372942 | 2012-03-07 19:13:28 +0000 | [diff] [blame] | 240 | uint16_t* idx = idxBase; |
| 241 | uint16_t subpathIdxStart = 0; |
| 242 | |
| 243 | GrPoint* base = reinterpret_cast<GrPoint*>(arg->vertices()); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 244 | SkASSERT(NULL != base); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 245 | GrPoint* vert = base; |
| 246 | |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 247 | GrPoint pts[4]; |
| 248 | |
| 249 | bool first = true; |
| 250 | int subpath = 0; |
| 251 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 252 | SkPath::Iter iter(path, false); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 253 | |
| 254 | for (;;) { |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 255 | SkPath::Verb verb = iter.next(pts); |
| 256 | switch (verb) { |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 257 | case SkPath::kConic_Verb: |
| 258 | SkASSERT(0); |
| 259 | break; |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 260 | case SkPath::kMove_Verb: |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 261 | if (!first) { |
| 262 | uint16_t currIdx = (uint16_t) (vert - base); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 263 | subpathIdxStart = currIdx; |
| 264 | ++subpath; |
| 265 | } |
| 266 | *vert = pts[0]; |
| 267 | vert++; |
| 268 | break; |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 269 | case SkPath::kLine_Verb: |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 270 | if (indexed) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 271 | uint16_t prevIdx = (uint16_t)(vert - base) - 1; |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 272 | append_countour_edge_indices(isHairline, subpathIdxStart, |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 273 | prevIdx, &idx); |
| 274 | } |
| 275 | *(vert++) = pts[1]; |
| 276 | break; |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 277 | case SkPath::kQuad_Verb: { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 278 | // first pt of quad is the pt we ended on in previous step |
| 279 | uint16_t firstQPtIdx = (uint16_t)(vert - base) - 1; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 280 | uint16_t numPts = (uint16_t) |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 281 | GrPathUtils::generateQuadraticPoints( |
| 282 | pts[0], pts[1], pts[2], |
| 283 | srcSpaceTolSqd, &vert, |
| 284 | GrPathUtils::quadraticPointCount(pts, srcSpaceTol)); |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 285 | if (indexed) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 286 | for (uint16_t i = 0; i < numPts; ++i) { |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 287 | append_countour_edge_indices(isHairline, subpathIdxStart, |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 288 | firstQPtIdx + i, &idx); |
| 289 | } |
| 290 | } |
| 291 | break; |
| 292 | } |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 293 | case SkPath::kCubic_Verb: { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 294 | // first pt of cubic is the pt we ended on in previous step |
| 295 | uint16_t firstCPtIdx = (uint16_t)(vert - base) - 1; |
| 296 | uint16_t numPts = (uint16_t) GrPathUtils::generateCubicPoints( |
| 297 | pts[0], pts[1], pts[2], pts[3], |
| 298 | srcSpaceTolSqd, &vert, |
| 299 | GrPathUtils::cubicPointCount(pts, srcSpaceTol)); |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 300 | if (indexed) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 301 | for (uint16_t i = 0; i < numPts; ++i) { |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 302 | append_countour_edge_indices(isHairline, subpathIdxStart, |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 303 | firstCPtIdx + i, &idx); |
| 304 | } |
| 305 | } |
| 306 | break; |
| 307 | } |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 308 | case SkPath::kClose_Verb: |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 309 | break; |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 310 | case SkPath::kDone_Verb: |
caryclark@google.com | cf6285b | 2012-06-06 12:09:01 +0000 | [diff] [blame] | 311 | // uint16_t currIdx = (uint16_t) (vert - base); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 312 | goto FINISHED; |
| 313 | } |
| 314 | first = false; |
| 315 | } |
| 316 | FINISHED: |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 317 | SkASSERT((vert - base) <= maxPts); |
| 318 | SkASSERT((idx - idxBase) <= maxIdxs); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 319 | |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 320 | *vertexCnt = static_cast<int>(vert - base); |
| 321 | *indexCnt = static_cast<int>(idx - idxBase); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 322 | |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 323 | } |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 324 | return true; |
| 325 | } |
| 326 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 327 | bool GrDefaultPathRenderer::internalDrawPath(const SkPath& path, |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 328 | const SkStrokeRec& stroke, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 329 | GrDrawTarget* target, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 330 | bool stencilOnly) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 331 | |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 332 | SkMatrix viewM = target->getDrawState().getViewMatrix(); |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 333 | SkScalar tol = SK_Scalar1; |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 334 | tol = GrPathUtils::scaleToleranceToSrc(tol, viewM, path.getBounds()); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 335 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 336 | int vertexCnt; |
| 337 | int indexCnt; |
| 338 | GrPrimitiveType primType; |
bsalomon@google.com | b372942 | 2012-03-07 19:13:28 +0000 | [diff] [blame] | 339 | GrDrawTarget::AutoReleaseGeometry arg; |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 340 | if (!this->createGeom(path, |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 341 | stroke, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 342 | tol, |
| 343 | target, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 344 | &primType, |
| 345 | &vertexCnt, |
bsalomon@google.com | b372942 | 2012-03-07 19:13:28 +0000 | [diff] [blame] | 346 | &indexCnt, |
| 347 | &arg)) { |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 348 | return false; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 349 | } |
| 350 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 351 | SkASSERT(NULL != target); |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 352 | GrDrawTarget::AutoStateRestore asr(target, GrDrawTarget::kPreserve_ASRInit); |
| 353 | GrDrawState* drawState = target->drawState(); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 354 | bool colorWritesWereDisabled = drawState->isColorWriteDisabled(); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 355 | // face culling doesn't make sense here |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 356 | SkASSERT(GrDrawState::kBoth_DrawFace == drawState->getDrawFace()); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 357 | |
| 358 | int passCount = 0; |
| 359 | const GrStencilSettings* passes[3]; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 360 | GrDrawState::DrawFace drawFace[3]; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 361 | bool reverse = false; |
| 362 | bool lastPassIsBounds; |
| 363 | |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 364 | if (stroke.isHairlineStyle()) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 365 | passCount = 1; |
| 366 | if (stencilOnly) { |
| 367 | passes[0] = &gDirectToStencil; |
| 368 | } else { |
| 369 | passes[0] = NULL; |
| 370 | } |
| 371 | lastPassIsBounds = false; |
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 | } else { |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 374 | if (single_pass_path(path, stroke)) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 375 | passCount = 1; |
| 376 | if (stencilOnly) { |
| 377 | passes[0] = &gDirectToStencil; |
| 378 | } else { |
| 379 | passes[0] = NULL; |
| 380 | } |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 381 | drawFace[0] = GrDrawState::kBoth_DrawFace; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 382 | lastPassIsBounds = false; |
| 383 | } else { |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 384 | switch (path.getFillType()) { |
| 385 | case SkPath::kInverseEvenOdd_FillType: |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 386 | reverse = true; |
| 387 | // fallthrough |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 388 | case SkPath::kEvenOdd_FillType: |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 389 | passes[0] = &gEOStencilPass; |
| 390 | if (stencilOnly) { |
| 391 | passCount = 1; |
| 392 | lastPassIsBounds = false; |
| 393 | } else { |
| 394 | passCount = 2; |
| 395 | lastPassIsBounds = true; |
| 396 | if (reverse) { |
| 397 | passes[1] = &gInvEOColorPass; |
| 398 | } else { |
| 399 | passes[1] = &gEOColorPass; |
| 400 | } |
| 401 | } |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 402 | drawFace[0] = drawFace[1] = GrDrawState::kBoth_DrawFace; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 403 | break; |
| 404 | |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 405 | case SkPath::kInverseWinding_FillType: |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 406 | reverse = true; |
| 407 | // fallthrough |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 408 | case SkPath::kWinding_FillType: |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 409 | if (fSeparateStencil) { |
| 410 | if (fStencilWrapOps) { |
| 411 | passes[0] = &gWindStencilSeparateWithWrap; |
| 412 | } else { |
| 413 | passes[0] = &gWindStencilSeparateNoWrap; |
| 414 | } |
| 415 | passCount = 2; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 416 | drawFace[0] = GrDrawState::kBoth_DrawFace; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 417 | } else { |
| 418 | if (fStencilWrapOps) { |
| 419 | passes[0] = &gWindSingleStencilWithWrapInc; |
| 420 | passes[1] = &gWindSingleStencilWithWrapDec; |
| 421 | } else { |
| 422 | passes[0] = &gWindSingleStencilNoWrapInc; |
| 423 | passes[1] = &gWindSingleStencilNoWrapDec; |
| 424 | } |
| 425 | // which is cw and which is ccw is arbitrary. |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 426 | drawFace[0] = GrDrawState::kCW_DrawFace; |
| 427 | drawFace[1] = GrDrawState::kCCW_DrawFace; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 428 | passCount = 3; |
| 429 | } |
| 430 | if (stencilOnly) { |
| 431 | lastPassIsBounds = false; |
| 432 | --passCount; |
| 433 | } else { |
| 434 | lastPassIsBounds = true; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 435 | drawFace[passCount-1] = GrDrawState::kBoth_DrawFace; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 436 | if (reverse) { |
| 437 | passes[passCount-1] = &gInvWindColorPass; |
| 438 | } else { |
| 439 | passes[passCount-1] = &gWindColorPass; |
| 440 | } |
| 441 | } |
| 442 | break; |
| 443 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 444 | SkDEBUGFAIL("Unknown path fFill!"); |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 445 | return false; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | } |
| 449 | |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 450 | SkRect devBounds; |
| 451 | GetPathDevBounds(path, drawState->getRenderTarget(), viewM, &devBounds); |
| 452 | |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 453 | for (int p = 0; p < passCount; ++p) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 454 | drawState->setDrawFace(drawFace[p]); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 455 | if (NULL != passes[p]) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 456 | *drawState->stencil() = *passes[p]; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | if (lastPassIsBounds && (p == passCount-1)) { |
| 460 | if (!colorWritesWereDisabled) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 461 | drawState->disableState(GrDrawState::kNoColorWrites_StateBit); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 462 | } |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 463 | SkRect bounds; |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 464 | GrDrawState::AutoViewMatrixRestore avmr; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 465 | if (reverse) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 466 | SkASSERT(NULL != drawState->getRenderTarget()); |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 467 | // draw over the dev bounds (which will be the whole dst surface for inv fill). |
| 468 | bounds = devBounds; |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 469 | SkMatrix vmi; |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 470 | // mapRect through persp matrix may not be correct |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 471 | if (!drawState->getViewMatrix().hasPerspective() && |
| 472 | drawState->getViewInverse(&vmi)) { |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 473 | vmi.mapRect(&bounds); |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 474 | } else { |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 475 | avmr.setIdentity(drawState); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 476 | } |
| 477 | } else { |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 478 | bounds = path.getBounds(); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 479 | } |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 480 | GrDrawTarget::AutoGeometryAndStatePush agasp(target, GrDrawTarget::kPreserve_ASRInit); |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 481 | target->drawSimpleRect(bounds, NULL); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 482 | } else { |
| 483 | if (passCount > 1) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 484 | drawState->enableState(GrDrawState::kNoColorWrites_StateBit); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 485 | } |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 486 | if (indexCnt) { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 487 | target->drawIndexed(primType, 0, 0, |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 488 | vertexCnt, indexCnt, &devBounds); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 489 | } else { |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 490 | target->drawNonIndexed(primType, 0, vertexCnt, &devBounds); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | } |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 494 | return true; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 495 | } |
| 496 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 497 | bool GrDefaultPathRenderer::canDrawPath(const SkPath& path, |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 498 | const SkStrokeRec& stroke, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 499 | const GrDrawTarget* target, |
| 500 | bool antiAlias) const { |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 501 | // this class can draw any path with any fill but doesn't do any anti-aliasing. |
| 502 | return (stroke.isFillStyle() || stroke.isHairlineStyle()) && !antiAlias; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 503 | } |
| 504 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 505 | bool GrDefaultPathRenderer::onDrawPath(const SkPath& path, |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 506 | const SkStrokeRec& stroke, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 507 | GrDrawTarget* target, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 508 | bool antiAlias) { |
| 509 | return this->internalDrawPath(path, |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 510 | stroke, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 511 | target, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 512 | false); |
| 513 | } |
| 514 | |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 515 | void GrDefaultPathRenderer::onStencilPath(const SkPath& path, |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 516 | const SkStrokeRec& stroke, |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 517 | GrDrawTarget* target) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 518 | SkASSERT(SkPath::kInverseEvenOdd_FillType != path.getFillType()); |
| 519 | SkASSERT(SkPath::kInverseWinding_FillType != path.getFillType()); |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 520 | this->internalDrawPath(path, stroke, target, true); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 521 | } |