blob: 19fd2beff8310e3e50d8bb28ef78cb3d14608065 [file] [log] [blame]
bsalomon@google.com30085192011-08-19 15:42:31 +00001
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.com93813632011-10-27 20:21:16 +000012#include "GrDrawState.h"
bsalomon@google.com30085192011-08-19 15:42:31 +000013#include "GrPathUtils.h"
tomhudson@google.comdd5f7442011-08-30 15:13:55 +000014#include "SkString.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000015#include "SkStrokeRec.h"
bsalomon@google.com30085192011-08-19 15:42:31 +000016#include "SkTrace.h"
17
18
19GrDefaultPathRenderer::GrDefaultPathRenderer(bool separateStencilSupport,
20 bool stencilWrapOpsSupport)
21 : fSeparateStencil(separateStencilSupport)
bsalomon@google.comc2099d22012-03-02 21:26:50 +000022 , fStencilWrapOps(stencilWrapOpsSupport) {
bsalomon@google.com289533a2011-10-27 12:34:25 +000023}
24
25
bsalomon@google.com30085192011-08-19 15:42:31 +000026////////////////////////////////////////////////////////////////////////////////
27// Stencil rules for paths
28
29////// Even/Odd
30
bsalomon@google.com6b2445e2011-12-15 19:47:46 +000031GR_STATIC_CONST_SAME_STENCIL(gEOStencilPass,
32 kInvert_StencilOp,
33 kKeep_StencilOp,
34 kAlwaysIfInClip_StencilFunc,
35 0xffff,
36 0xffff,
37 0xffff);
bsalomon@google.com30085192011-08-19 15:42:31 +000038
39// ok not to check clip b/c stencil pass only wrote inside clip
bsalomon@google.com6b2445e2011-12-15 19:47:46 +000040GR_STATIC_CONST_SAME_STENCIL(gEOColorPass,
41 kZero_StencilOp,
42 kZero_StencilOp,
43 kNotEqual_StencilFunc,
44 0xffff,
45 0x0000,
46 0xffff);
bsalomon@google.com30085192011-08-19 15:42:31 +000047
48// have to check clip b/c outside clip will always be zero.
bsalomon@google.com6b2445e2011-12-15 19:47:46 +000049GR_STATIC_CONST_SAME_STENCIL(gInvEOColorPass,
50 kZero_StencilOp,
51 kZero_StencilOp,
52 kEqualIfInClip_StencilFunc,
53 0xffff,
54 0x0000,
55 0xffff);
bsalomon@google.com30085192011-08-19 15:42:31 +000056
57////// Winding
58
59// when we have separate stencil we increment front faces / decrement back faces
60// when we don't have wrap incr and decr we use the stencil test to simulate
61// them.
62
bsalomon@google.com6b2445e2011-12-15 19:47:46 +000063GR_STATIC_CONST_STENCIL(gWindStencilSeparateWithWrap,
bsalomon@google.com30085192011-08-19 15:42:31 +000064 kIncWrap_StencilOp, kDecWrap_StencilOp,
65 kKeep_StencilOp, kKeep_StencilOp,
66 kAlwaysIfInClip_StencilFunc, kAlwaysIfInClip_StencilFunc,
tomhudson@google.com62b09682011-11-09 16:39:17 +000067 0xffff, 0xffff,
68 0xffff, 0xffff,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +000069 0xffff, 0xffff);
bsalomon@google.com30085192011-08-19 15:42:31 +000070
71// if inc'ing the max value, invert to make 0
72// if dec'ing zero invert to make all ones.
73// we can't avoid touching the stencil on both passing and
74// failing, so we can't resctrict ourselves to the clip.
bsalomon@google.com6b2445e2011-12-15 19:47:46 +000075GR_STATIC_CONST_STENCIL(gWindStencilSeparateNoWrap,
bsalomon@google.com30085192011-08-19 15:42:31 +000076 kInvert_StencilOp, kInvert_StencilOp,
77 kIncClamp_StencilOp, kDecClamp_StencilOp,
78 kEqual_StencilFunc, kEqual_StencilFunc,
tomhudson@google.com62b09682011-11-09 16:39:17 +000079 0xffff, 0xffff,
80 0xffff, 0x0000,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +000081 0xffff, 0xffff);
bsalomon@google.com30085192011-08-19 15:42:31 +000082
83// When there are no separate faces we do two passes to setup the winding rule
84// stencil. First we draw the front faces and inc, then we draw the back faces
85// and dec. These are same as the above two split into the incrementing and
86// decrementing passes.
bsalomon@google.com6b2445e2011-12-15 19:47:46 +000087GR_STATIC_CONST_SAME_STENCIL(gWindSingleStencilWithWrapInc,
88 kIncWrap_StencilOp,
89 kKeep_StencilOp,
90 kAlwaysIfInClip_StencilFunc,
91 0xffff,
92 0xffff,
93 0xffff);
bsalomon@google.com30085192011-08-19 15:42:31 +000094
bsalomon@google.com6b2445e2011-12-15 19:47:46 +000095GR_STATIC_CONST_SAME_STENCIL(gWindSingleStencilWithWrapDec,
96 kDecWrap_StencilOp,
97 kKeep_StencilOp,
98 kAlwaysIfInClip_StencilFunc,
99 0xffff,
100 0xffff,
101 0xffff);
bsalomon@google.com30085192011-08-19 15:42:31 +0000102
bsalomon@google.com6b2445e2011-12-15 19:47:46 +0000103GR_STATIC_CONST_SAME_STENCIL(gWindSingleStencilNoWrapInc,
104 kInvert_StencilOp,
105 kIncClamp_StencilOp,
106 kEqual_StencilFunc,
107 0xffff,
108 0xffff,
109 0xffff);
110
111GR_STATIC_CONST_SAME_STENCIL(gWindSingleStencilNoWrapDec,
112 kInvert_StencilOp,
113 kDecClamp_StencilOp,
114 kEqual_StencilFunc,
115 0xffff,
116 0x0000,
117 0xffff);
118
119// Color passes are the same whether we use the two-sided stencil or two passes
120
121GR_STATIC_CONST_SAME_STENCIL(gWindColorPass,
122 kZero_StencilOp,
123 kZero_StencilOp,
124 kNonZeroIfInClip_StencilFunc,
125 0xffff,
126 0x0000,
127 0xffff);
128
129GR_STATIC_CONST_SAME_STENCIL(gInvWindColorPass,
130 kZero_StencilOp,
131 kZero_StencilOp,
132 kEqualIfInClip_StencilFunc,
133 0xffff,
134 0x0000,
135 0xffff);
bsalomon@google.com30085192011-08-19 15:42:31 +0000136
137////// Normal render to stencil
138
139// Sometimes the default path renderer can draw a path directly to the stencil
140// buffer without having to first resolve the interior / exterior.
bsalomon@google.com6b2445e2011-12-15 19:47:46 +0000141GR_STATIC_CONST_SAME_STENCIL(gDirectToStencil,
142 kZero_StencilOp,
143 kIncClamp_StencilOp,
144 kAlwaysIfInClip_StencilFunc,
145 0xffff,
146 0x0000,
147 0xffff);
bsalomon@google.com30085192011-08-19 15:42:31 +0000148
149////////////////////////////////////////////////////////////////////////////////
150// Helpers for drawPath
151
bsalomon@google.com30085192011-08-19 15:42:31 +0000152#define STENCIL_OFF 0 // Always disable stencil (even when needed)
153
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000154static inline bool single_pass_path(const SkPath& path, const SkStrokeRec& stroke) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000155#if STENCIL_OFF
156 return true;
157#else
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000158 if (!stroke.isHairlineStyle() && !path.isInverseFillType()) {
bsalomon@google.com7d72c452012-01-30 14:02:44 +0000159 return path.isConvex();
bsalomon@google.com30085192011-08-19 15:42:31 +0000160 }
161 return false;
162#endif
163}
164
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000165GrPathRenderer::StencilSupport GrDefaultPathRenderer::onGetStencilSupport(
166 const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000167 const SkStrokeRec& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000168 const GrDrawTarget*) const {
169 if (single_pass_path(path, stroke)) {
170 return GrPathRenderer::kNoRestriction_StencilSupport;
171 } else {
172 return GrPathRenderer::kStencilOnly_StencilSupport;
173 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000174}
175
sugoi@google.com12b4e272012-12-06 20:13:11 +0000176static inline void append_countour_edge_indices(bool hairLine,
bsalomon@google.com30085192011-08-19 15:42:31 +0000177 uint16_t fanCenterIdx,
178 uint16_t edgeV0Idx,
179 uint16_t** indices) {
180 // when drawing lines we're appending line segments along
181 // the contour. When applying the other fill rules we're
182 // drawing triangle fans around fanCenterIdx.
sugoi@google.com12b4e272012-12-06 20:13:11 +0000183 if (!hairLine) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000184 *((*indices)++) = fanCenterIdx;
185 }
186 *((*indices)++) = edgeV0Idx;
187 *((*indices)++) = edgeV0Idx + 1;
188}
189
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000190bool GrDefaultPathRenderer::createGeom(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000191 const SkStrokeRec& stroke,
bsalomon@google.com81712882012-11-01 17:12:34 +0000192 SkScalar srcSpaceTol,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000193 GrDrawTarget* target,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000194 GrPrimitiveType* primType,
195 int* vertexCnt,
bsalomon@google.comb3729422012-03-07 19:13:28 +0000196 int* indexCnt,
197 GrDrawTarget::AutoReleaseGeometry* arg) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000198 {
199 SK_TRACE_EVENT0("GrDefaultPathRenderer::createGeom");
200
bsalomon@google.com81712882012-11-01 17:12:34 +0000201 SkScalar srcSpaceTolSqd = SkScalarMul(srcSpaceTol, srcSpaceTol);
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000202 int contourCnt;
203 int maxPts = GrPathUtils::worstCasePointCount(path, &contourCnt,
bsalomon@google.com30085192011-08-19 15:42:31 +0000204 srcSpaceTol);
205
206 if (maxPts <= 0) {
207 return false;
208 }
209 if (maxPts > ((int)SK_MaxU16 + 1)) {
210 GrPrintf("Path not rendered, too many verts (%d)\n", maxPts);
211 return false;
212 }
213
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000214 bool indexed = contourCnt > 1;
bsalomon@google.com30085192011-08-19 15:42:31 +0000215
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000216 const bool isHairline = stroke.isHairlineStyle();
sugoi@google.com12b4e272012-12-06 20:13:11 +0000217
bsalomon@google.com30085192011-08-19 15:42:31 +0000218 int maxIdxs = 0;
sugoi@google.com12b4e272012-12-06 20:13:11 +0000219 if (isHairline) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000220 if (indexed) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000221 maxIdxs = 2 * maxPts;
bsalomon@google.com47059542012-06-06 20:51:20 +0000222 *primType = kLines_GrPrimitiveType;
bsalomon@google.com30085192011-08-19 15:42:31 +0000223 } else {
bsalomon@google.com47059542012-06-06 20:51:20 +0000224 *primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com30085192011-08-19 15:42:31 +0000225 }
226 } else {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000227 if (indexed) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000228 maxIdxs = 3 * maxPts;
bsalomon@google.com47059542012-06-06 20:51:20 +0000229 *primType = kTriangles_GrPrimitiveType;
bsalomon@google.com30085192011-08-19 15:42:31 +0000230 } else {
bsalomon@google.com47059542012-06-06 20:51:20 +0000231 *primType = kTriangleFan_GrPrimitiveType;
bsalomon@google.com30085192011-08-19 15:42:31 +0000232 }
233 }
234
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000235 target->drawState()->setDefaultVertexAttribs();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000236 if (!arg->set(target, maxPts, maxIdxs)) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000237 return false;
238 }
bsalomon@google.comb3729422012-03-07 19:13:28 +0000239
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000240 uint16_t* idxBase = reinterpret_cast<uint16_t*>(arg->indices());
bsalomon@google.comb3729422012-03-07 19:13:28 +0000241 uint16_t* idx = idxBase;
242 uint16_t subpathIdxStart = 0;
243
244 GrPoint* base = reinterpret_cast<GrPoint*>(arg->vertices());
bsalomon@google.com30085192011-08-19 15:42:31 +0000245 GrAssert(NULL != base);
246 GrPoint* vert = base;
247
bsalomon@google.com30085192011-08-19 15:42:31 +0000248 GrPoint pts[4];
249
250 bool first = true;
251 int subpath = 0;
252
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000253 SkPath::Iter iter(path, false);
bsalomon@google.com30085192011-08-19 15:42:31 +0000254
255 for (;;) {
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000256 SkPath::Verb verb = iter.next(pts);
257 switch (verb) {
reed@google.com277c3f82013-05-31 15:17:50 +0000258 case SkPath::kConic_Verb:
259 SkASSERT(0);
260 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000261 case SkPath::kMove_Verb:
bsalomon@google.com30085192011-08-19 15:42:31 +0000262 if (!first) {
263 uint16_t currIdx = (uint16_t) (vert - base);
bsalomon@google.com30085192011-08-19 15:42:31 +0000264 subpathIdxStart = currIdx;
265 ++subpath;
266 }
267 *vert = pts[0];
268 vert++;
269 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000270 case SkPath::kLine_Verb:
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000271 if (indexed) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000272 uint16_t prevIdx = (uint16_t)(vert - base) - 1;
sugoi@google.com12b4e272012-12-06 20:13:11 +0000273 append_countour_edge_indices(isHairline, subpathIdxStart,
bsalomon@google.com30085192011-08-19 15:42:31 +0000274 prevIdx, &idx);
275 }
276 *(vert++) = pts[1];
277 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000278 case SkPath::kQuad_Verb: {
bsalomon@google.com30085192011-08-19 15:42:31 +0000279 // first pt of quad is the pt we ended on in previous step
280 uint16_t firstQPtIdx = (uint16_t)(vert - base) - 1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000281 uint16_t numPts = (uint16_t)
bsalomon@google.com30085192011-08-19 15:42:31 +0000282 GrPathUtils::generateQuadraticPoints(
283 pts[0], pts[1], pts[2],
284 srcSpaceTolSqd, &vert,
285 GrPathUtils::quadraticPointCount(pts, srcSpaceTol));
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000286 if (indexed) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000287 for (uint16_t i = 0; i < numPts; ++i) {
sugoi@google.com12b4e272012-12-06 20:13:11 +0000288 append_countour_edge_indices(isHairline, subpathIdxStart,
bsalomon@google.com30085192011-08-19 15:42:31 +0000289 firstQPtIdx + i, &idx);
290 }
291 }
292 break;
293 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000294 case SkPath::kCubic_Verb: {
bsalomon@google.com30085192011-08-19 15:42:31 +0000295 // first pt of cubic is the pt we ended on in previous step
296 uint16_t firstCPtIdx = (uint16_t)(vert - base) - 1;
297 uint16_t numPts = (uint16_t) GrPathUtils::generateCubicPoints(
298 pts[0], pts[1], pts[2], pts[3],
299 srcSpaceTolSqd, &vert,
300 GrPathUtils::cubicPointCount(pts, srcSpaceTol));
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000301 if (indexed) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000302 for (uint16_t i = 0; i < numPts; ++i) {
sugoi@google.com12b4e272012-12-06 20:13:11 +0000303 append_countour_edge_indices(isHairline, subpathIdxStart,
bsalomon@google.com30085192011-08-19 15:42:31 +0000304 firstCPtIdx + i, &idx);
305 }
306 }
307 break;
308 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000309 case SkPath::kClose_Verb:
bsalomon@google.com30085192011-08-19 15:42:31 +0000310 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000311 case SkPath::kDone_Verb:
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000312 // uint16_t currIdx = (uint16_t) (vert - base);
bsalomon@google.com30085192011-08-19 15:42:31 +0000313 goto FINISHED;
314 }
315 first = false;
316 }
317FINISHED:
318 GrAssert((vert - base) <= maxPts);
319 GrAssert((idx - idxBase) <= maxIdxs);
320
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000321 *vertexCnt = vert - base;
322 *indexCnt = idx - idxBase;
bsalomon@google.com30085192011-08-19 15:42:31 +0000323
bsalomon@google.com30085192011-08-19 15:42:31 +0000324 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000325 return true;
326}
327
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000328bool GrDefaultPathRenderer::internalDrawPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000329 const SkStrokeRec& stroke,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000330 GrDrawTarget* target,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000331 bool stencilOnly) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000332
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000333 SkMatrix viewM = target->getDrawState().getViewMatrix();
bsalomon@google.com81712882012-11-01 17:12:34 +0000334 SkScalar tol = SK_Scalar1;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000335 tol = GrPathUtils::scaleToleranceToSrc(tol, viewM, path.getBounds());
bsalomon@google.com30085192011-08-19 15:42:31 +0000336
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000337 int vertexCnt;
338 int indexCnt;
339 GrPrimitiveType primType;
bsalomon@google.comb3729422012-03-07 19:13:28 +0000340 GrDrawTarget::AutoReleaseGeometry arg;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000341 if (!this->createGeom(path,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000342 stroke,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000343 tol,
344 target,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000345 &primType,
346 &vertexCnt,
bsalomon@google.comb3729422012-03-07 19:13:28 +0000347 &indexCnt,
348 &arg)) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000349 return false;
bsalomon@google.com30085192011-08-19 15:42:31 +0000350 }
351
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000352 GrAssert(NULL != target);
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000353 GrDrawTarget::AutoStateRestore asr(target, GrDrawTarget::kPreserve_ASRInit);
354 GrDrawState* drawState = target->drawState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000355 bool colorWritesWereDisabled = drawState->isColorWriteDisabled();
bsalomon@google.com30085192011-08-19 15:42:31 +0000356 // face culling doesn't make sense here
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000357 GrAssert(GrDrawState::kBoth_DrawFace == drawState->getDrawFace());
bsalomon@google.com30085192011-08-19 15:42:31 +0000358
359 int passCount = 0;
360 const GrStencilSettings* passes[3];
tomhudson@google.com93813632011-10-27 20:21:16 +0000361 GrDrawState::DrawFace drawFace[3];
bsalomon@google.com30085192011-08-19 15:42:31 +0000362 bool reverse = false;
363 bool lastPassIsBounds;
364
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000365 if (stroke.isHairlineStyle()) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000366 passCount = 1;
367 if (stencilOnly) {
368 passes[0] = &gDirectToStencil;
369 } else {
370 passes[0] = NULL;
371 }
372 lastPassIsBounds = false;
tomhudson@google.com93813632011-10-27 20:21:16 +0000373 drawFace[0] = GrDrawState::kBoth_DrawFace;
bsalomon@google.com30085192011-08-19 15:42:31 +0000374 } else {
sugoi@google.com12b4e272012-12-06 20:13:11 +0000375 if (single_pass_path(path, stroke)) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000376 passCount = 1;
377 if (stencilOnly) {
378 passes[0] = &gDirectToStencil;
379 } else {
380 passes[0] = NULL;
381 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000382 drawFace[0] = GrDrawState::kBoth_DrawFace;
bsalomon@google.com30085192011-08-19 15:42:31 +0000383 lastPassIsBounds = false;
384 } else {
sugoi@google.com12b4e272012-12-06 20:13:11 +0000385 switch (path.getFillType()) {
386 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com30085192011-08-19 15:42:31 +0000387 reverse = true;
388 // fallthrough
sugoi@google.com12b4e272012-12-06 20:13:11 +0000389 case SkPath::kEvenOdd_FillType:
bsalomon@google.com30085192011-08-19 15:42:31 +0000390 passes[0] = &gEOStencilPass;
391 if (stencilOnly) {
392 passCount = 1;
393 lastPassIsBounds = false;
394 } else {
395 passCount = 2;
396 lastPassIsBounds = true;
397 if (reverse) {
398 passes[1] = &gInvEOColorPass;
399 } else {
400 passes[1] = &gEOColorPass;
401 }
402 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000403 drawFace[0] = drawFace[1] = GrDrawState::kBoth_DrawFace;
bsalomon@google.com30085192011-08-19 15:42:31 +0000404 break;
405
sugoi@google.com12b4e272012-12-06 20:13:11 +0000406 case SkPath::kInverseWinding_FillType:
bsalomon@google.com30085192011-08-19 15:42:31 +0000407 reverse = true;
408 // fallthrough
sugoi@google.com12b4e272012-12-06 20:13:11 +0000409 case SkPath::kWinding_FillType:
bsalomon@google.com30085192011-08-19 15:42:31 +0000410 if (fSeparateStencil) {
411 if (fStencilWrapOps) {
412 passes[0] = &gWindStencilSeparateWithWrap;
413 } else {
414 passes[0] = &gWindStencilSeparateNoWrap;
415 }
416 passCount = 2;
tomhudson@google.com93813632011-10-27 20:21:16 +0000417 drawFace[0] = GrDrawState::kBoth_DrawFace;
bsalomon@google.com30085192011-08-19 15:42:31 +0000418 } else {
419 if (fStencilWrapOps) {
420 passes[0] = &gWindSingleStencilWithWrapInc;
421 passes[1] = &gWindSingleStencilWithWrapDec;
422 } else {
423 passes[0] = &gWindSingleStencilNoWrapInc;
424 passes[1] = &gWindSingleStencilNoWrapDec;
425 }
426 // which is cw and which is ccw is arbitrary.
tomhudson@google.com93813632011-10-27 20:21:16 +0000427 drawFace[0] = GrDrawState::kCW_DrawFace;
428 drawFace[1] = GrDrawState::kCCW_DrawFace;
bsalomon@google.com30085192011-08-19 15:42:31 +0000429 passCount = 3;
430 }
431 if (stencilOnly) {
432 lastPassIsBounds = false;
433 --passCount;
434 } else {
435 lastPassIsBounds = true;
tomhudson@google.com93813632011-10-27 20:21:16 +0000436 drawFace[passCount-1] = GrDrawState::kBoth_DrawFace;
bsalomon@google.com30085192011-08-19 15:42:31 +0000437 if (reverse) {
438 passes[passCount-1] = &gInvWindColorPass;
439 } else {
440 passes[passCount-1] = &gWindColorPass;
441 }
442 }
443 break;
444 default:
445 GrAssert(!"Unknown path fFill!");
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000446 return false;
bsalomon@google.com30085192011-08-19 15:42:31 +0000447 }
448 }
449 }
450
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000451 SkRect devBounds;
452 GetPathDevBounds(path, drawState->getRenderTarget(), viewM, &devBounds);
453
bsalomon@google.com30085192011-08-19 15:42:31 +0000454 for (int p = 0; p < passCount; ++p) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000455 drawState->setDrawFace(drawFace[p]);
bsalomon@google.com30085192011-08-19 15:42:31 +0000456 if (NULL != passes[p]) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000457 *drawState->stencil() = *passes[p];
bsalomon@google.com30085192011-08-19 15:42:31 +0000458 }
459
460 if (lastPassIsBounds && (p == passCount-1)) {
461 if (!colorWritesWereDisabled) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000462 drawState->disableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.com30085192011-08-19 15:42:31 +0000463 }
464 GrRect bounds;
bsalomon@google.com137f1342013-05-29 21:27:53 +0000465 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com30085192011-08-19 15:42:31 +0000466 if (reverse) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000467 GrAssert(NULL != drawState->getRenderTarget());
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000468 // draw over the dev bounds (which will be the whole dst surface for inv fill).
469 bounds = devBounds;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000470 SkMatrix vmi;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000471 // mapRect through persp matrix may not be correct
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000472 if (!drawState->getViewMatrix().hasPerspective() &&
473 drawState->getViewInverse(&vmi)) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000474 vmi.mapRect(&bounds);
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000475 } else {
bsalomon@google.com137f1342013-05-29 21:27:53 +0000476 avmr.setIdentity(drawState);
bsalomon@google.com30085192011-08-19 15:42:31 +0000477 }
478 } else {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000479 bounds = path.getBounds();
bsalomon@google.com30085192011-08-19 15:42:31 +0000480 }
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000481 GrDrawTarget::AutoGeometryAndStatePush agasp(target, GrDrawTarget::kPreserve_ASRInit);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000482 target->drawSimpleRect(bounds, NULL);
bsalomon@google.com30085192011-08-19 15:42:31 +0000483 } else {
484 if (passCount > 1) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000485 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.com30085192011-08-19 15:42:31 +0000486 }
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000487 if (indexCnt) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000488 target->drawIndexed(primType, 0, 0,
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000489 vertexCnt, indexCnt, &devBounds);
bsalomon@google.com30085192011-08-19 15:42:31 +0000490 } else {
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000491 target->drawNonIndexed(primType, 0, vertexCnt, &devBounds);
bsalomon@google.com30085192011-08-19 15:42:31 +0000492 }
493 }
494 }
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000495 return true;
bsalomon@google.com30085192011-08-19 15:42:31 +0000496}
497
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000498bool GrDefaultPathRenderer::canDrawPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000499 const SkStrokeRec& stroke,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000500 const GrDrawTarget* target,
501 bool antiAlias) const {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000502 // this class can draw any path with any fill but doesn't do any anti-aliasing.
503 return (stroke.isFillStyle() || stroke.isHairlineStyle()) && !antiAlias;
bsalomon@google.com30085192011-08-19 15:42:31 +0000504}
505
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000506bool GrDefaultPathRenderer::onDrawPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000507 const SkStrokeRec& stroke,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000508 GrDrawTarget* target,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000509 bool antiAlias) {
510 return this->internalDrawPath(path,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000511 stroke,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000512 target,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000513 false);
514}
515
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000516void GrDefaultPathRenderer::onStencilPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000517 const SkStrokeRec& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000518 GrDrawTarget* target) {
sugoi@google.com12b4e272012-12-06 20:13:11 +0000519 GrAssert(SkPath::kInverseEvenOdd_FillType != path.getFillType());
520 GrAssert(SkPath::kInverseWinding_FillType != path.getFillType());
521 this->internalDrawPath(path, stroke, target, true);
bsalomon@google.com30085192011-08-19 15:42:31 +0000522}