blob: 340ede0d314484247d892cf66324c90ba6715e71 [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001
2/*
3 * Copyright 2012 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 "GrClipMaskManager.h"
robertphillips@google.comfa662942012-05-17 12:20:22 +000010#include "GrAAConvexPathRenderer.h"
11#include "GrAAHairLinePathRenderer.h"
jvanverth@google.combfe2b9d2013-09-06 16:57:29 +000012#include "GrAARectRenderer.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000013#include "GrDrawTargetCaps.h"
14#include "GrGpu.h"
15#include "GrPaint.h"
16#include "GrPathRenderer.h"
17#include "GrRenderTarget.h"
18#include "GrStencilBuffer.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000019#include "GrSWMaskHelper.h"
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000020#include "effects/GrTextureDomain.h"
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +000021#include "effects/GrConvexPolyEffect.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000022#include "SkRasterClip.h"
23#include "SkStrokeRec.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000024#include "SkTLazy.h"
25
robertphillips@google.comba998f22012-10-12 11:33:56 +000026#define GR_AA_CLIP 1
robertphillips@google.coma72eef32012-05-01 17:22:59 +000027
bsalomon@google.com8182fa02012-12-04 14:06:06 +000028typedef SkClipStack::Element Element;
bsalomon@google.com51a62862012-11-26 21:19:43 +000029
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000030using namespace GrReducedClip;
31
bsalomon@google.com51a62862012-11-26 21:19:43 +000032////////////////////////////////////////////////////////////////////////////////
robertphillips@google.coma72eef32012-05-01 17:22:59 +000033namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000034// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000035// stage matrix this also alters the vertex layout
rmistry@google.comfbfcd562012-08-23 18:09:54 +000036void setup_drawstate_aaclip(GrGpu* gpu,
37 GrTexture* result,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000038 const SkIRect &devBound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000039 GrDrawState* drawState = gpu->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000040 SkASSERT(drawState);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000041
bsalomon@google.comb9086a02012-11-01 18:02:54 +000042 SkMatrix mat;
bsalomon@google.comc7818882013-03-20 19:19:53 +000043 // We want to use device coords to compute the texture coordinates. We set our matrix to be
44 // equal to the view matrix followed by an offset to the devBound, and then a scaling matrix to
45 // normalized coords. We apply this matrix to the vertex positions rather than local coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000046 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000047 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000048 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000049 mat.preConcat(drawState->getViewMatrix());
50
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000051 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000052 // This could be a long-lived effect that is cached with the alpha-mask.
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000053 drawState->addCoverageEffect(
54 GrTextureDomainEffect::Create(result,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000055 mat,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000056 GrTextureDomain::MakeTexelDomain(result, domainTexels),
57 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +000058 GrTextureParams::kNone_FilterMode,
bsalomon@google.com77af6802013-10-02 13:04:56 +000059 kPosition_GrCoordSet))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000060}
61
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000062bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000063 GrGpu* gpu,
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000064 const SkPath& origPath,
sugoi@google.com5f74cf82012-12-17 21:16:45 +000065 const SkStrokeRec& stroke,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000066 bool doAA) {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000067 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
68 SkTCopyOnFirstWrite<SkPath> path(origPath);
69 if (path->isInverseFillType()) {
70 path.writable()->toggleInverseFillType();
71 }
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000072 // last (false) parameter disallows use of the SW path renderer
bsalomon@google.com45a15f52012-12-10 19:10:17 +000073 GrPathRendererChain::DrawType type = doAA ?
74 GrPathRendererChain::kColorAntiAlias_DrawType :
75 GrPathRendererChain::kColor_DrawType;
76
77 return NULL == context->getPathRenderer(*path, stroke, gpu, false, type);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +000078}
79
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000080}
81
robertphillips@google.comfa662942012-05-17 12:20:22 +000082/*
83 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
84 * will be used on any element. If so, it returns true to indicate that the
85 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
86 */
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000087bool GrClipMaskManager::useSWOnlyPath(const ElementList& elements) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +000088
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000089 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000090 // a clip gets complex enough it can just be done in SW regardless
91 // of whether it would invoke the GrSoftwarePathRenderer.
sugoi@google.com5f74cf82012-12-17 21:16:45 +000092 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000093
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000094 for (ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
95 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000096 // rects can always be drawn directly w/o using the software path
97 // so only paths need to be checked
bsalomon@google.com8182fa02012-12-04 14:06:06 +000098 if (Element::kPath_Type == element->getType() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +000099 path_needs_SW_renderer(this->getContext(), fGpu,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000100 element->getPath(),
sugoi@google.com12b4e272012-12-06 20:13:11 +0000101 stroke,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000102 element->isAA())) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000103 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000104 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000105 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000106 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000107}
108
robertphillips@google.comf294b772012-04-27 14:29:26 +0000109////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000110// sort out what kind of clip mask needs to be created: alpha, stencil,
111// scissor, or entirely software
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000112bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn,
113 GrDrawState::AutoRestoreEffects* are) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000114 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000115
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000116 ElementList elements(16);
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000117 int32_t genID;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000118 InitialState initialState;
119 SkIRect clipSpaceIBounds;
120 bool requiresAA;
121 bool isRect = false;
122
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000123 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000124
125 const GrRenderTarget* rt = drawState->getRenderTarget();
126 // GrDrawTarget should have filtered this for us
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000127 SkASSERT(NULL != rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000128
129 bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen();
130
131 if (!ignoreClip) {
132 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
133 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
134 ReduceClipStack(*clipDataIn->fClipStack,
135 clipSpaceRTIBounds,
136 &elements,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000137 &genID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000138 &initialState,
139 &clipSpaceIBounds,
140 &requiresAA);
141 if (elements.isEmpty()) {
142 if (kAllIn_InitialState == initialState) {
143 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
144 isRect = true;
145 } else {
146 return false;
147 }
148 }
149 }
150
151 if (ignoreClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000152 fGpu->disableScissor();
153 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000154 return true;
155 }
156
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000157 // If there is only one clip element and it is a convex polygon we just install an effect that
158 // clips against the edges.
159 if (1 == elements.count() && SkClipStack::Element::kPath_Type == elements.tail()->getType() &&
160 SkRegion::kReplace_Op == elements.tail()->getOp()) {
161 const SkPath& p = elements.tail()->getPath();
162 bool isAA = GR_AA_CLIP && elements.tail()->isAA();
163 SkAutoTUnref<GrEffectRef> effect;
164 if (rt->isMultisampled()) {
165 // A coverage effect for AA clipping won't play nicely with MSAA.
166 if (!isAA) {
167 effect.reset(GrConvexPolyEffect::Create(GrConvexPolyEffect::kFillNoAA_EdgeType, p));
168 }
169 } else {
170 GrConvexPolyEffect::EdgeType type = isAA ? GrConvexPolyEffect::kFillAA_EdgeType :
171 GrConvexPolyEffect::kFillNoAA_EdgeType;
172 effect.reset(GrConvexPolyEffect::Create(type, p));
173 }
174 if (effect) {
175 are->set(fGpu->drawState());
176 fGpu->drawState()->addCoverageEffect(effect);
177 fGpu->disableScissor();
178 this->setGpuStencil();
179 return true;
180 }
181 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000182
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000183#if GR_AA_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000184 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000185 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000186 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000187
188 if (this->useSWOnlyPath(elements)) {
189 // The clip geometry is complex enough that it will be more efficient to create it
190 // entirely in software
191 result = this->createSoftwareClipMask(genID,
192 initialState,
193 elements,
194 clipSpaceIBounds);
195 } else {
196 result = this->createAlphaClipMask(genID,
197 initialState,
198 elements,
199 clipSpaceIBounds);
200 }
201
202 if (NULL != result) {
203 // The mask's top left coord should be pinned to the rounded-out top left corner of
204 // clipSpace bounds. We determine the mask's position WRT to the render target here.
205 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
206 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000207 are->set(fGpu->drawState());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000208 setup_drawstate_aaclip(fGpu, result, rtSpaceMaskBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000209 fGpu->disableScissor();
210 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000211 return true;
212 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000213 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000214 }
215#endif // GR_AA_CLIP
216
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000217 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
218 // be created. In either case, free up the texture in the anti-aliased mask cache.
219 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
220 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
221 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000222 fAACache.reset();
223
bsalomon@google.coma3201942012-06-21 19:58:20 +0000224 // If the clip is a rectangle then just set the scissor. Otherwise, create
225 // a stencil mask.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000226 if (isRect) {
227 SkIRect clipRect = clipSpaceIBounds;
228 clipRect.offset(-clipDataIn->fOrigin);
229 fGpu->enableScissor(clipRect);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000230 this->setGpuStencil();
231 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000232 }
233
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000234 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000235 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000236 this->createStencilClipMask(genID,
237 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000238 elements,
239 clipSpaceIBounds,
240 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000241
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000242 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
243 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
244 // use both stencil and scissor test to the bounds for the final draw.
245 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
246 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
247 fGpu->enableScissor(scissorSpaceIBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000248 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000249 return true;
250}
251
252#define VISUALIZE_COMPLEX_CLIP 0
253
254#if VISUALIZE_COMPLEX_CLIP
tfarina@chromium.org223137f2012-11-21 22:38:36 +0000255 #include "SkRandom.h"
256 SkRandom gRandom;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000257 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
258#else
259 #define SET_RANDOM_COLOR
260#endif
261
262namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000263
264////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000265// set up the OpenGL blend function to perform the specified
266// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000267void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000268
269 switch (op) {
270 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000271 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000272 break;
273 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000274 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000275 break;
276 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000277 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000278 break;
279 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000280 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000281 break;
282 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000283 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000284 break;
285 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000286 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000287 break;
288 default:
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000289 SkASSERT(false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000290 break;
291 }
292}
293
robertphillips@google.com72176b22012-05-23 13:19:12 +0000294}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000295
296////////////////////////////////////////////////////////////////////////////////
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000297bool GrClipMaskManager::drawElement(GrTexture* target,
298 const SkClipStack::Element* element,
299 GrPathRenderer* pr) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000300 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000301
302 drawState->setRenderTarget(target->asRenderTarget());
303
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000304 switch (element->getType()) {
305 case Element::kRect_Type:
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000306 // TODO: Do rects directly to the accumulator using a aa-rect GrEffect that covers the
307 // entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000308 if (element->isAA()) {
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000309 getContext()->getAARectRenderer()->fillAARect(fGpu,
310 fGpu,
311 element->getRect(),
robertphillips@google.comb19cb7f2013-05-02 15:37:20 +0000312 SkMatrix::I(),
robertphillips@google.comafd1cba2013-05-14 19:47:47 +0000313 element->getRect(),
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000314 false);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000315 } else {
316 fGpu->drawSimpleRect(element->getRect(), NULL);
317 }
318 return true;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000319 case Element::kPath_Type: {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000320 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
321 if (path->isInverseFillType()) {
322 path.writable()->toggleInverseFillType();
323 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000324 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000325 if (NULL == pr) {
326 GrPathRendererChain::DrawType type;
327 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
328 GrPathRendererChain::kColor_DrawType;
329 pr = this->getContext()->getPathRenderer(*path, stroke, fGpu, false, type);
330 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000331 if (NULL == pr) {
332 return false;
333 }
334 pr->drawPath(element->getPath(), stroke, fGpu, element->isAA());
335 break;
336 }
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000337 default:
338 // something is wrong if we're trying to draw an empty element.
339 GrCrash("Unexpected element type");
340 return false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000341 }
342 return true;
343}
344
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000345bool GrClipMaskManager::canStencilAndDrawElement(GrTexture* target,
346 const SkClipStack::Element* element,
347 GrPathRenderer** pr) {
348 GrDrawState* drawState = fGpu->drawState();
349 drawState->setRenderTarget(target->asRenderTarget());
350
351 switch (element->getType()) {
352 case Element::kRect_Type:
353 return true;
354 case Element::kPath_Type: {
355 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
356 if (path->isInverseFillType()) {
357 path.writable()->toggleInverseFillType();
358 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000359 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000360 GrPathRendererChain::DrawType type = element->isAA() ?
361 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
362 GrPathRendererChain::kStencilAndColor_DrawType;
363 *pr = this->getContext()->getPathRenderer(*path, stroke, fGpu, false, type);
364 return NULL != *pr;
365 }
366 default:
367 // something is wrong if we're trying to draw an empty element.
368 GrCrash("Unexpected element type");
369 return false;
370 }
371}
372
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000373void GrClipMaskManager::mergeMask(GrTexture* dstMask,
374 GrTexture* srcMask,
375 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000376 const SkIRect& dstBound,
377 const SkIRect& srcBound) {
bsalomon@google.com137f1342013-05-29 21:27:53 +0000378 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000379 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000380 SkAssertResult(avmr.setIdentity(drawState));
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000381 GrDrawState::AutoRestoreEffects are(drawState);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000382
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000383 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000384
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000385 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000386
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000387 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000388 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000389
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000390 drawState->addColorEffect(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000391 GrTextureDomainEffect::Create(srcMask,
392 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000393 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
394 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000395 GrTextureParams::kNone_FilterMode))->unref();
reed@google.com44699382013-10-31 17:28:30 +0000396 fGpu->drawSimpleRect(SkRect::Make(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000397}
398
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000399// get a texture to act as a temporary buffer for AA clip boolean operations
400// TODO: given the expense of createTexture we may want to just cache this too
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000401void GrClipMaskManager::getTemp(int width, int height, GrAutoScratchTexture* temp) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000402 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000403 // we've already allocated the temp texture
404 return;
405 }
406
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000407 GrTextureDesc desc;
408 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000409 desc.fWidth = width;
410 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000411 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000412
robertphillips@google.com2c756812012-05-22 20:28:23 +0000413 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000414}
415
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000416////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000417// Handles caching & allocation (if needed) of a clip alpha-mask texture for both the sw-upload
418// or gpu-rendered cases. Returns true if there is no more work to be done (i.e., we got a cache
419// hit)
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000420bool GrClipMaskManager::getMaskTexture(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000421 const SkIRect& clipSpaceIBounds,
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000422 GrTexture** result,
423 bool willUpload) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000424 bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000425 if (!cached) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000426
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000427 // There isn't a suitable entry in the cache so we create a new texture to store the mask.
428 // Since we are setting up the cache we know the last lookup was a miss. Free up the
429 // currently cached mask so it can be reused.
430 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000431
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000432 GrTextureDesc desc;
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000433 desc.fFlags = willUpload ? kNone_GrTextureFlags : kRenderTarget_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000434 desc.fWidth = clipSpaceIBounds.width();
435 desc.fHeight = clipSpaceIBounds.height();
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000436 desc.fConfig = kRGBA_8888_GrPixelConfig;
robertphillips@google.com94bdd7e2013-10-31 15:50:43 +0000437 if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000438 // We would always like A8 but it isn't supported on all platforms
439 desc.fConfig = kAlpha_8_GrPixelConfig;
440 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000441
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000442 fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000443 }
444
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000445 *result = fAACache.getLastMask();
446 return cached;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000447}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000448
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000449////////////////////////////////////////////////////////////////////////////////
450// Create a 8-bit clip mask in alpha
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000451GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000452 InitialState initialState,
453 const ElementList& elements,
454 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000455 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000456
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000457 GrTexture* result;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000458 if (this->getMaskTexture(elementsGenID, clipSpaceIBounds, &result, false)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000459 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000460 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000461 }
462
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000463 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000464 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000465 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000466 }
467
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000468 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000469 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000470 SkIntToScalar(-clipSpaceIBounds.fLeft),
471 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000472 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000473 // The texture may be larger than necessary, this rect represents the part of the texture
474 // we populate with a rasterization of the clip.
475 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
476
bsalomon@google.com137f1342013-05-29 21:27:53 +0000477 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
478 SkMatrix translate;
479 translate.setTranslate(clipToMaskOffset);
480 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &translate);
481
482 GrDrawState* drawState = fGpu->drawState();
483
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000484 // We're drawing a coverage mask and want coverage to be run through the blend function.
485 drawState->enableState(GrDrawState::kCoverageDrawing_StateBit);
486
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000487 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
488 // clear the part that we care about.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000489 fGpu->clear(&maskSpaceIBounds,
490 kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000491 true,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000492 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000493
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000494 // When we use the stencil in the below loop it is important to have this clip installed.
495 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
496 // pass must not set values outside of this bounds or stencil values outside the rect won't be
497 // cleared.
498 GrDrawTarget::AutoClipRestore acr(fGpu, maskSpaceIBounds);
499 drawState->enableState(GrDrawState::kClip_StateBit);
500
robertphillips@google.comf105b102012-05-14 12:18:26 +0000501 GrAutoScratchTexture temp;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000502 // walk through each clip element and perform its set op
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000503 for (ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000504 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000505 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000506 bool invert = element->isInverseFilled();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000507
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000508 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
509 GrPathRenderer* pr = NULL;
510 bool useTemp = !this->canStencilAndDrawElement(result, element, &pr);
511 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000512 // This is the bounds of the clip element in the space of the alpha-mask. The temporary
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000513 // mask buffer can be substantially larger than the actually clip stack element. We
514 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000515 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000516 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000517
518 if (useTemp) {
519 if (invert) {
520 maskSpaceElementIBounds = maskSpaceIBounds;
521 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000522 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000523 elementBounds.offset(clipToMaskOffset);
524 elementBounds.roundOut(&maskSpaceElementIBounds);
525 }
526
527 this->getTemp(maskSpaceIBounds.fRight, maskSpaceIBounds.fBottom, &temp);
528 if (NULL == temp.texture()) {
529 fAACache.reset();
530 return NULL;
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000531 }
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000532 dst = temp.texture();
533 // clear the temp target and set blend to replace
534 fGpu->clear(&maskSpaceElementIBounds,
535 invert ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000536 true,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000537 dst->asRenderTarget());
538 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000539
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000540 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000541 // draw directly into the result with the stencil set to make the pixels affected
542 // by the clip shape be non-zero.
543 dst = result;
544 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
545 kReplace_StencilOp,
546 kReplace_StencilOp,
547 kAlways_StencilFunc,
548 0xffff,
549 0xffff,
550 0xffff);
551 drawState->setStencil(kStencilInElement);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000552 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000553 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000554
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000555 drawState->setAlpha(invert ? 0x00 : 0xff);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000556
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000557 if (!this->drawElement(dst, element, pr)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000558 fAACache.reset();
559 return NULL;
560 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000561
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000562 if (useTemp) {
563 // Now draw into the accumulator using the real operation and the temp buffer as a
564 // texture
565 this->mergeMask(result,
566 temp.texture(),
567 op,
568 maskSpaceIBounds,
569 maskSpaceElementIBounds);
570 } else {
571 // Draw to the exterior pixels (those with a zero stencil value).
572 drawState->setAlpha(invert ? 0xff : 0x00);
573 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
574 kZero_StencilOp,
575 kZero_StencilOp,
576 kEqual_StencilFunc,
577 0xffff,
578 0x0000,
579 0xffff);
580 drawState->setStencil(kDrawOutsideElement);
581 fGpu->drawSimpleRect(clipSpaceIBounds);
582 drawState->disableStencil();
583 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000584 } else {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000585 // all the remaining ops can just be directly draw into the accumulation buffer
586 drawState->setAlpha(0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000587 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000588 this->drawElement(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000589 }
590 }
591
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000592 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000593 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000594}
595
596////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000597// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000598// (as opposed to canvas) coordinates
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000599bool GrClipMaskManager::createStencilClipMask(int32_t elementsGenID,
600 InitialState initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000601 const ElementList& elements,
602 const SkIRect& clipSpaceIBounds,
603 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000604
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000605 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000606
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000607 GrDrawState* drawState = fGpu->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000608 SkASSERT(drawState->isClipState());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000609
610 GrRenderTarget* rt = drawState->getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000611 SkASSERT(NULL != rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000612
613 // TODO: dynamically attach a SB when needed.
614 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
615 if (NULL == stencilBuffer) {
616 return false;
617 }
618
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000619 if (stencilBuffer->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000620
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000621 stencilBuffer->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000622
bsalomon@google.com137f1342013-05-29 21:27:53 +0000623 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
624 SkVector translate = {
625 SkIntToScalar(clipSpaceToStencilOffset.fX),
626 SkIntToScalar(clipSpaceToStencilOffset.fY)
627 };
628 SkMatrix matrix;
629 matrix.setTranslate(translate);
630 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &matrix);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000631 drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000632
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000633 drawState->setRenderTarget(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000634
bsalomon@google.com9f131742012-12-13 20:43:56 +0000635 // We set the current clip to the bounds so that our recursive draws are scissored to them.
636 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
637 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
638 GrDrawTarget::AutoClipRestore acr(fGpu, stencilSpaceIBounds);
639 drawState->enableState(GrDrawState::kClip_StateBit);
640
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000641#if !VISUALIZE_COMPLEX_CLIP
642 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
643#endif
644
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000645 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000646 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000647 clipBit = (1 << (clipBit-1));
648
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000649 fGpu->clearStencilClip(stencilSpaceIBounds, kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000650
651 // walk through each clip element and perform its set op
652 // with the existing clip.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000653 for (ElementList::Iter iter(elements.headIter()); NULL != iter.get(); iter.next()) {
654 const Element* element = iter.get();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000655 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000656 // enabled at bottom of loop
657 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000658 // if the target is MSAA then we want MSAA enabled when the clip is soft
659 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000660 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000661 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000662
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000663 // This will be used to determine whether the clip shape can be rendered into the
664 // stencil with arbitrary stencil settings.
665 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000666
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000667 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000668
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000669 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000670
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000671 GrPathRenderer* pr = NULL;
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000672 SkTCopyOnFirstWrite<SkPath> clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000673 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000674 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000675 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000676 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000677 SkASSERT(Element::kPath_Type == element->getType());
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000678 clipPath.init(element->getPath());
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000679 fillInverted = clipPath->isInverseFillType();
680 if (fillInverted) {
681 clipPath.writable()->toggleInverseFillType();
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000682 }
683 pr = this->getContext()->getPathRenderer(*clipPath,
684 stroke,
685 fGpu,
686 false,
687 GrPathRendererChain::kStencilOnly_DrawType,
688 &stencilSupport);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000689 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000690 return false;
691 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000692 }
693
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000694 int passes;
695 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
696
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000697 bool canRenderDirectToStencil =
698 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000699 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000700 // fill rule, and set operation can
701 // we render the element directly to
702 // stencil bit used for clipping.
703 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
704 canRenderDirectToStencil,
705 clipBit,
706 fillInverted,
707 &passes,
708 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000709
710 // draw the element to the client stencil bits if necessary
711 if (!canDrawDirectToClip) {
712 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000713 kIncClamp_StencilOp,
714 kIncClamp_StencilOp,
715 kAlways_StencilFunc,
716 0xffff,
717 0x0000,
718 0xffff);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000719 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000720 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000721 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000722 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000723 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000724 SkASSERT(Element::kPath_Type == element->getType());
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000725 if (!clipPath->isEmpty()) {
726 if (canRenderDirectToStencil) {
727 *drawState->stencil() = gDrawToStencil;
728 pr->drawPath(*clipPath, stroke, fGpu, false);
729 } else {
730 pr->stencilPath(*clipPath, stroke, fGpu);
731 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000732 }
733 }
734 }
735
736 // now we modify the clip bit by rendering either the clip
737 // element directly or a bounding rect of the entire clip.
738 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
739 for (int p = 0; p < passes; ++p) {
740 *drawState->stencil() = stencilSettings[p];
741 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000742 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000743 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000744 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000745 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000746 SkASSERT(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000747 SET_RANDOM_COLOR
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000748 pr->drawPath(*clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000749 }
750 } else {
751 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000752 // The view matrix is setup to do clip space -> stencil space translation, so
753 // draw rect in clip space.
reed@google.com44699382013-10-31 17:28:30 +0000754 fGpu->drawSimpleRect(SkRect::Make(clipSpaceIBounds), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000755 }
756 }
757 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000758 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000759 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000760 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000761 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000762 return true;
763}
764
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000765
bsalomon@google.com411dad02012-06-05 20:24:20 +0000766// mapping of clip-respecting stencil funcs to normal stencil funcs
767// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000768static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000769 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
770 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
771 // In the Clip Funcs
772 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
773 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
774 kLess_StencilFunc, // kLessIfInClip_StencilFunc
775 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
776 // Special in the clip func that forces user's ref to be 0.
777 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
778 // make ref 0 and do normal nequal.
779 },
780 {// Stencil-Clipping is ENABLED
781 // In the Clip Funcs
782 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
783 // eq stencil clip bit, mask
784 // out user bits.
785
786 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
787 // add stencil bit to mask and ref
788
789 kLess_StencilFunc, // kLessIfInClip_StencilFunc
790 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
791 // for both of these we can add
792 // the clip bit to the mask and
793 // ref and compare as normal
794 // Special in the clip func that forces user's ref to be 0.
795 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
796 // make ref have only the clip bit set
797 // and make comparison be less
798 // 10..0 < 1..user_bits..
799 }
800};
801
bsalomon@google.coma3201942012-06-21 19:58:20 +0000802namespace {
803// Sets the settings to clip against the stencil buffer clip while ignoring the
804// client bits.
805const GrStencilSettings& basic_apply_stencil_clip_settings() {
806 // stencil settings to use when clip is in stencil
807 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
808 kKeep_StencilOp,
809 kKeep_StencilOp,
810 kAlwaysIfInClip_StencilFunc,
811 0x0000,
812 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000813 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000814 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
815}
816}
817
818void GrClipMaskManager::setGpuStencil() {
819 // We make two copies of the StencilSettings here (except in the early
820 // exit scenario. One copy from draw state to the stack var. Then another
821 // from the stack var to the gpu. We could make this class hold a ptr to
822 // GrGpu's fStencilSettings and eliminate the stack copy here.
823
824 const GrDrawState& drawState = fGpu->getDrawState();
825
826 // use stencil for clipping if clipping is enabled and the clip
827 // has been written into the stencil.
828 GrClipMaskManager::StencilClipMode clipMode;
829 if (this->isClipInStencil() && drawState.isClipState()) {
830 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
831 // We can't be modifying the clip and respecting it at the same time.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000832 SkASSERT(!drawState.isStateFlagEnabled(
bsalomon@google.coma3201942012-06-21 19:58:20 +0000833 GrGpu::kModifyStencilClip_StateBit));
834 } else if (drawState.isStateFlagEnabled(
835 GrGpu::kModifyStencilClip_StateBit)) {
836 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
837 } else {
838 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
839 }
840
841 GrStencilSettings settings;
842 // The GrGpu client may not be using the stencil buffer but we may need to
843 // enable it in order to respect a stencil clip.
844 if (drawState.getStencil().isDisabled()) {
845 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
846 settings = basic_apply_stencil_clip_settings();
847 } else {
848 fGpu->disableStencil();
849 return;
850 }
851 } else {
852 settings = drawState.getStencil();
853 }
854
855 // TODO: dynamically attach a stencil buffer
856 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000857 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000858 drawState.getRenderTarget()->getStencilBuffer();
859 if (NULL != stencilBuffer) {
860 stencilBits = stencilBuffer->bits();
861 }
862
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000863 SkASSERT(fGpu->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
864 SkASSERT(fGpu->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000865 this->adjustStencilParams(&settings, clipMode, stencilBits);
866 fGpu->setStencilSettings(settings);
867}
868
869void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
870 StencilClipMode mode,
871 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000872 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000873
874 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000875 // We assume that this clip manager itself is drawing to the GrGpu and
876 // has already setup the correct values.
877 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000878 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000879
bsalomon@google.com411dad02012-06-05 20:24:20 +0000880 unsigned int clipBit = (1 << (stencilBitCnt - 1));
881 unsigned int userBits = clipBit - 1;
882
bsalomon@google.coma3201942012-06-21 19:58:20 +0000883 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000884 bool twoSided = fGpu->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000885
bsalomon@google.coma3201942012-06-21 19:58:20 +0000886 bool finished = false;
887 while (!finished) {
888 GrStencilFunc func = settings->func(face);
889 uint16_t writeMask = settings->writeMask(face);
890 uint16_t funcMask = settings->funcMask(face);
891 uint16_t funcRef = settings->funcRef(face);
892
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000893 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000894
895 writeMask &= userBits;
896
897 if (func >= kBasicStencilFuncCount) {
898 int respectClip = kRespectClip_StencilClipMode == mode;
899 if (respectClip) {
900 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000901 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000902 switch (func) {
903 case kAlwaysIfInClip_StencilFunc:
904 funcMask = clipBit;
905 funcRef = clipBit;
906 break;
907 case kEqualIfInClip_StencilFunc:
908 case kLessIfInClip_StencilFunc:
909 case kLEqualIfInClip_StencilFunc:
910 funcMask = (funcMask & userBits) | clipBit;
911 funcRef = (funcRef & userBits) | clipBit;
912 break;
913 case kNonZeroIfInClip_StencilFunc:
914 funcMask = (funcMask & userBits) | clipBit;
915 funcRef = clipBit;
916 break;
917 default:
918 GrCrash("Unknown stencil func");
919 }
920 } else {
921 funcMask &= userBits;
922 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000923 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000924 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000925 gSpecialToBasicStencilFunc[respectClip];
926 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000927 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000928 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000929 funcMask &= userBits;
930 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000931 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000932
933 settings->setFunc(face, func);
934 settings->setWriteMask(face, writeMask);
935 settings->setFuncMask(face, funcMask);
936 settings->setFuncRef(face, funcRef);
937
938 if (GrStencilSettings::kFront_Face == face) {
939 face = GrStencilSettings::kBack_Face;
940 finished = !twoSided;
941 } else {
942 finished = true;
943 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000944 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000945 if (!twoSided) {
946 settings->copyFrontSettingsToBack();
947 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000948}
949
950////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000951GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000952 GrReducedClip::InitialState initialState,
953 const GrReducedClip::ElementList& elements,
954 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000955 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000956
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000957 GrTexture* result;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000958 if (this->getMaskTexture(elementsGenID, clipSpaceIBounds, &result, true)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000959 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000960 }
961
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000962 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000963 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000964 return NULL;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000965 }
966
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000967 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
968 // the top left corner of the resulting rect to the top left of the texture.
969 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
970
robertphillips@google.com2c756812012-05-22 20:28:23 +0000971 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000972
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000973 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000974 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
975 SkIntToScalar(-clipSpaceIBounds.fTop));
976 helper.init(maskSpaceIBounds, &matrix);
977
978 helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000979
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000980 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000981
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000982 for (ElementList::Iter iter(elements.headIter()) ; NULL != iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000983
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000984 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000985 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000986
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000987 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
988 // Intersect and reverse difference require modifying pixels outside of the geometry
989 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
990 // but leave the pixels inside the geometry alone. For reverse difference we invert all
991 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000992 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +0000993 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000994 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000995 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000996 }
997
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000998 if (Element::kRect_Type == element->getType()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000999 // convert the rect to a path so we can invert the fill
1000 SkPath temp;
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001001 temp.addRect(element->getRect());
sugoi@google.com12b4e272012-12-06 20:13:11 +00001002 temp.setFillType(SkPath::kInverseEvenOdd_FillType);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001003
sugoi@google.com12b4e272012-12-06 20:13:11 +00001004 helper.draw(temp, stroke, SkRegion::kReplace_Op,
1005 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001006 0x00);
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001007 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001008 SkASSERT(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +00001009 SkPath clipPath = element->getPath();
1010 clipPath.toggleInverseFillType();
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +00001011 helper.draw(clipPath, stroke,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001012 SkRegion::kReplace_Op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001013 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001014 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001015 }
1016
1017 continue;
1018 }
1019
1020 // The other ops (union, xor, diff) only affect pixels inside
1021 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001022 if (Element::kRect_Type == element->getType()) {
1023 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1024 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001025 SkASSERT(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +00001026 helper.draw(element->getPath(), stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001027 }
1028 }
1029
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001030 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001031
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001032 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001033 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001034}
1035
robertphillips@google.comf294b772012-04-27 14:29:26 +00001036////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001037void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001038 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001039}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001040
1041void GrClipMaskManager::setGpu(GrGpu* gpu) {
1042 fGpu = gpu;
1043 fAACache.setContext(gpu->getContext());
1044}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001045
1046void GrClipMaskManager::adjustPathStencilParams(GrStencilSettings* settings) {
1047 const GrDrawState& drawState = fGpu->getDrawState();
1048 GrClipMaskManager::StencilClipMode clipMode;
1049 if (this->isClipInStencil() && drawState.isClipState()) {
1050 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
1051 // We can't be modifying the clip and respecting it at the same time.
1052 SkASSERT(!drawState.isStateFlagEnabled(
1053 GrGpu::kModifyStencilClip_StateBit));
1054 } else if (drawState.isStateFlagEnabled(
1055 GrGpu::kModifyStencilClip_StateBit)) {
1056 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
1057 } else {
1058 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
1059 }
1060
1061 // TODO: dynamically attach a stencil buffer
1062 int stencilBits = 0;
1063 GrStencilBuffer* stencilBuffer =
1064 drawState.getRenderTarget()->getStencilBuffer();
1065 if (NULL != stencilBuffer) {
1066 stencilBits = stencilBuffer->bits();
1067 this->adjustStencilParams(settings, clipMode, stencilBits);
1068 }
1069}