blob: c1c59734ab50b6f133891dd388583bd68dc15fce [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);
commit-bot@chromium.org6516d4b2014-02-07 14:04:48 +0000177 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
178 scissorSpaceIBounds.offset(-clipDataIn->fOrigin);
179 fGpu->enableScissor(scissorSpaceIBounds);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000180 this->setGpuStencil();
181 return true;
182 }
183 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000184
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000185#if GR_AA_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000186 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000187 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000188 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000189
190 if (this->useSWOnlyPath(elements)) {
191 // The clip geometry is complex enough that it will be more efficient to create it
192 // entirely in software
193 result = this->createSoftwareClipMask(genID,
194 initialState,
195 elements,
196 clipSpaceIBounds);
197 } else {
198 result = this->createAlphaClipMask(genID,
199 initialState,
200 elements,
201 clipSpaceIBounds);
202 }
203
204 if (NULL != result) {
205 // The mask's top left coord should be pinned to the rounded-out top left corner of
206 // clipSpace bounds. We determine the mask's position WRT to the render target here.
207 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
208 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000209 are->set(fGpu->drawState());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000210 setup_drawstate_aaclip(fGpu, result, rtSpaceMaskBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000211 fGpu->disableScissor();
212 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000213 return true;
214 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000215 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000216 }
217#endif // GR_AA_CLIP
218
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000219 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
220 // be created. In either case, free up the texture in the anti-aliased mask cache.
221 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
222 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
223 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000224 fAACache.reset();
225
bsalomon@google.coma3201942012-06-21 19:58:20 +0000226 // If the clip is a rectangle then just set the scissor. Otherwise, create
227 // a stencil mask.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000228 if (isRect) {
229 SkIRect clipRect = clipSpaceIBounds;
230 clipRect.offset(-clipDataIn->fOrigin);
231 fGpu->enableScissor(clipRect);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000232 this->setGpuStencil();
233 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000234 }
235
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000236 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000237 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000238 this->createStencilClipMask(genID,
239 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000240 elements,
241 clipSpaceIBounds,
242 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000243
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000244 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
245 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
246 // use both stencil and scissor test to the bounds for the final draw.
247 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
248 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
249 fGpu->enableScissor(scissorSpaceIBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000250 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000251 return true;
252}
253
254#define VISUALIZE_COMPLEX_CLIP 0
255
256#if VISUALIZE_COMPLEX_CLIP
tfarina@chromium.org223137f2012-11-21 22:38:36 +0000257 #include "SkRandom.h"
258 SkRandom gRandom;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000259 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
260#else
261 #define SET_RANDOM_COLOR
262#endif
263
264namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000265
266////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000267// set up the OpenGL blend function to perform the specified
268// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000269void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000270
271 switch (op) {
272 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000273 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000274 break;
275 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000276 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000277 break;
278 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000279 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000280 break;
281 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000282 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000283 break;
284 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000285 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000286 break;
287 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000288 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000289 break;
290 default:
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000291 SkASSERT(false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000292 break;
293 }
294}
295
robertphillips@google.com72176b22012-05-23 13:19:12 +0000296}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000297
298////////////////////////////////////////////////////////////////////////////////
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000299bool GrClipMaskManager::drawElement(GrTexture* target,
300 const SkClipStack::Element* element,
301 GrPathRenderer* pr) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000302 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000303
304 drawState->setRenderTarget(target->asRenderTarget());
305
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000306 switch (element->getType()) {
307 case Element::kRect_Type:
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000308 // TODO: Do rects directly to the accumulator using a aa-rect GrEffect that covers the
309 // entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000310 if (element->isAA()) {
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000311 getContext()->getAARectRenderer()->fillAARect(fGpu,
312 fGpu,
313 element->getRect(),
robertphillips@google.comb19cb7f2013-05-02 15:37:20 +0000314 SkMatrix::I(),
robertphillips@google.comafd1cba2013-05-14 19:47:47 +0000315 element->getRect(),
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000316 false);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000317 } else {
318 fGpu->drawSimpleRect(element->getRect(), NULL);
319 }
320 return true;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000321 case Element::kPath_Type: {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000322 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
323 if (path->isInverseFillType()) {
324 path.writable()->toggleInverseFillType();
325 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000326 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000327 if (NULL == pr) {
328 GrPathRendererChain::DrawType type;
329 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
330 GrPathRendererChain::kColor_DrawType;
331 pr = this->getContext()->getPathRenderer(*path, stroke, fGpu, false, type);
332 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000333 if (NULL == pr) {
334 return false;
335 }
336 pr->drawPath(element->getPath(), stroke, fGpu, element->isAA());
337 break;
338 }
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000339 default:
340 // something is wrong if we're trying to draw an empty element.
341 GrCrash("Unexpected element type");
342 return false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000343 }
344 return true;
345}
346
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000347bool GrClipMaskManager::canStencilAndDrawElement(GrTexture* target,
348 const SkClipStack::Element* element,
349 GrPathRenderer** pr) {
350 GrDrawState* drawState = fGpu->drawState();
351 drawState->setRenderTarget(target->asRenderTarget());
352
353 switch (element->getType()) {
354 case Element::kRect_Type:
355 return true;
356 case Element::kPath_Type: {
357 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
358 if (path->isInverseFillType()) {
359 path.writable()->toggleInverseFillType();
360 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000361 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000362 GrPathRendererChain::DrawType type = element->isAA() ?
363 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
364 GrPathRendererChain::kStencilAndColor_DrawType;
365 *pr = this->getContext()->getPathRenderer(*path, stroke, fGpu, false, type);
366 return NULL != *pr;
367 }
368 default:
369 // something is wrong if we're trying to draw an empty element.
370 GrCrash("Unexpected element type");
371 return false;
372 }
373}
374
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000375void GrClipMaskManager::mergeMask(GrTexture* dstMask,
376 GrTexture* srcMask,
377 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000378 const SkIRect& dstBound,
379 const SkIRect& srcBound) {
bsalomon@google.com137f1342013-05-29 21:27:53 +0000380 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000381 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000382 SkAssertResult(avmr.setIdentity(drawState));
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000383 GrDrawState::AutoRestoreEffects are(drawState);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000384
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000385 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000386
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000387 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000388
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000389 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000390 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000391
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000392 drawState->addColorEffect(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000393 GrTextureDomainEffect::Create(srcMask,
394 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000395 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
396 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000397 GrTextureParams::kNone_FilterMode))->unref();
reed@google.com44699382013-10-31 17:28:30 +0000398 fGpu->drawSimpleRect(SkRect::Make(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000399}
400
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000401// get a texture to act as a temporary buffer for AA clip boolean operations
402// TODO: given the expense of createTexture we may want to just cache this too
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000403void GrClipMaskManager::getTemp(int width, int height, GrAutoScratchTexture* temp) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000404 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000405 // we've already allocated the temp texture
406 return;
407 }
408
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000409 GrTextureDesc desc;
410 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000411 desc.fWidth = width;
412 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000413 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000414
robertphillips@google.com2c756812012-05-22 20:28:23 +0000415 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000416}
417
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000418////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000419// Handles caching & allocation (if needed) of a clip alpha-mask texture for both the sw-upload
420// or gpu-rendered cases. Returns true if there is no more work to be done (i.e., we got a cache
421// hit)
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000422bool GrClipMaskManager::getMaskTexture(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000423 const SkIRect& clipSpaceIBounds,
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000424 GrTexture** result,
425 bool willUpload) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000426 bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000427 if (!cached) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000428
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000429 // There isn't a suitable entry in the cache so we create a new texture to store the mask.
430 // Since we are setting up the cache we know the last lookup was a miss. Free up the
431 // currently cached mask so it can be reused.
432 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000433
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000434 GrTextureDesc desc;
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000435 desc.fFlags = willUpload ? kNone_GrTextureFlags : kRenderTarget_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000436 desc.fWidth = clipSpaceIBounds.width();
437 desc.fHeight = clipSpaceIBounds.height();
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000438 desc.fConfig = kRGBA_8888_GrPixelConfig;
robertphillips@google.com94bdd7e2013-10-31 15:50:43 +0000439 if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000440 // We would always like A8 but it isn't supported on all platforms
441 desc.fConfig = kAlpha_8_GrPixelConfig;
442 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000443
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000444 fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000445 }
446
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000447 *result = fAACache.getLastMask();
448 return cached;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000449}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000450
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000451////////////////////////////////////////////////////////////////////////////////
452// Create a 8-bit clip mask in alpha
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000453GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000454 InitialState initialState,
455 const ElementList& elements,
456 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000457 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000458
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000459 GrTexture* result;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000460 if (this->getMaskTexture(elementsGenID, clipSpaceIBounds, &result, false)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000461 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000462 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000463 }
464
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000465 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000466 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000467 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000468 }
469
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000470 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000471 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000472 SkIntToScalar(-clipSpaceIBounds.fLeft),
473 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000474 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000475 // The texture may be larger than necessary, this rect represents the part of the texture
476 // we populate with a rasterization of the clip.
477 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
478
bsalomon@google.com137f1342013-05-29 21:27:53 +0000479 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
480 SkMatrix translate;
481 translate.setTranslate(clipToMaskOffset);
482 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &translate);
483
484 GrDrawState* drawState = fGpu->drawState();
485
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000486 // We're drawing a coverage mask and want coverage to be run through the blend function.
487 drawState->enableState(GrDrawState::kCoverageDrawing_StateBit);
488
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000489 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
490 // clear the part that we care about.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000491 fGpu->clear(&maskSpaceIBounds,
492 kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000493 true,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000494 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000495
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000496 // When we use the stencil in the below loop it is important to have this clip installed.
497 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
498 // pass must not set values outside of this bounds or stencil values outside the rect won't be
499 // cleared.
500 GrDrawTarget::AutoClipRestore acr(fGpu, maskSpaceIBounds);
501 drawState->enableState(GrDrawState::kClip_StateBit);
502
robertphillips@google.comf105b102012-05-14 12:18:26 +0000503 GrAutoScratchTexture temp;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000504 // walk through each clip element and perform its set op
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000505 for (ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000506 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000507 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000508 bool invert = element->isInverseFilled();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000509
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000510 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
511 GrPathRenderer* pr = NULL;
512 bool useTemp = !this->canStencilAndDrawElement(result, element, &pr);
513 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000514 // 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 +0000515 // mask buffer can be substantially larger than the actually clip stack element. We
516 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000517 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000518 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000519
520 if (useTemp) {
521 if (invert) {
522 maskSpaceElementIBounds = maskSpaceIBounds;
523 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000524 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000525 elementBounds.offset(clipToMaskOffset);
526 elementBounds.roundOut(&maskSpaceElementIBounds);
527 }
528
529 this->getTemp(maskSpaceIBounds.fRight, maskSpaceIBounds.fBottom, &temp);
530 if (NULL == temp.texture()) {
531 fAACache.reset();
532 return NULL;
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000533 }
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000534 dst = temp.texture();
535 // clear the temp target and set blend to replace
536 fGpu->clear(&maskSpaceElementIBounds,
537 invert ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000538 true,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000539 dst->asRenderTarget());
540 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000541
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000542 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000543 // draw directly into the result with the stencil set to make the pixels affected
544 // by the clip shape be non-zero.
545 dst = result;
546 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
547 kReplace_StencilOp,
548 kReplace_StencilOp,
549 kAlways_StencilFunc,
550 0xffff,
551 0xffff,
552 0xffff);
553 drawState->setStencil(kStencilInElement);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000554 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000555 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000556
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000557 drawState->setAlpha(invert ? 0x00 : 0xff);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000558
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000559 if (!this->drawElement(dst, element, pr)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000560 fAACache.reset();
561 return NULL;
562 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000563
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000564 if (useTemp) {
565 // Now draw into the accumulator using the real operation and the temp buffer as a
566 // texture
567 this->mergeMask(result,
568 temp.texture(),
569 op,
570 maskSpaceIBounds,
571 maskSpaceElementIBounds);
572 } else {
573 // Draw to the exterior pixels (those with a zero stencil value).
574 drawState->setAlpha(invert ? 0xff : 0x00);
575 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
576 kZero_StencilOp,
577 kZero_StencilOp,
578 kEqual_StencilFunc,
579 0xffff,
580 0x0000,
581 0xffff);
582 drawState->setStencil(kDrawOutsideElement);
583 fGpu->drawSimpleRect(clipSpaceIBounds);
584 drawState->disableStencil();
585 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000586 } else {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000587 // all the remaining ops can just be directly draw into the accumulation buffer
588 drawState->setAlpha(0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000589 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000590 this->drawElement(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000591 }
592 }
593
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000594 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000595 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000596}
597
598////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000599// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000600// (as opposed to canvas) coordinates
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000601bool GrClipMaskManager::createStencilClipMask(int32_t elementsGenID,
602 InitialState initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000603 const ElementList& elements,
604 const SkIRect& clipSpaceIBounds,
605 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000606
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000607 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000608
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000609 GrDrawState* drawState = fGpu->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000610 SkASSERT(drawState->isClipState());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000611
612 GrRenderTarget* rt = drawState->getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000613 SkASSERT(NULL != rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000614
615 // TODO: dynamically attach a SB when needed.
616 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
617 if (NULL == stencilBuffer) {
618 return false;
619 }
620
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000621 if (stencilBuffer->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000622
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000623 stencilBuffer->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000624
bsalomon@google.com137f1342013-05-29 21:27:53 +0000625 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
626 SkVector translate = {
627 SkIntToScalar(clipSpaceToStencilOffset.fX),
628 SkIntToScalar(clipSpaceToStencilOffset.fY)
629 };
630 SkMatrix matrix;
631 matrix.setTranslate(translate);
632 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &matrix);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000633 drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000634
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000635 drawState->setRenderTarget(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000636
bsalomon@google.com9f131742012-12-13 20:43:56 +0000637 // We set the current clip to the bounds so that our recursive draws are scissored to them.
638 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
639 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
640 GrDrawTarget::AutoClipRestore acr(fGpu, stencilSpaceIBounds);
641 drawState->enableState(GrDrawState::kClip_StateBit);
642
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000643#if !VISUALIZE_COMPLEX_CLIP
644 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
645#endif
646
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000647 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000648 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000649 clipBit = (1 << (clipBit-1));
650
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000651 fGpu->clearStencilClip(stencilSpaceIBounds, kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000652
653 // walk through each clip element and perform its set op
654 // with the existing clip.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000655 for (ElementList::Iter iter(elements.headIter()); NULL != iter.get(); iter.next()) {
656 const Element* element = iter.get();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000657 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000658 // enabled at bottom of loop
659 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000660 // if the target is MSAA then we want MSAA enabled when the clip is soft
661 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000662 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000663 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000664
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000665 // This will be used to determine whether the clip shape can be rendered into the
666 // stencil with arbitrary stencil settings.
667 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000668
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000669 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000670
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000671 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000672
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000673 GrPathRenderer* pr = NULL;
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000674 SkTCopyOnFirstWrite<SkPath> clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000675 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000676 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000677 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000678 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000679 SkASSERT(Element::kPath_Type == element->getType());
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000680 clipPath.init(element->getPath());
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000681 fillInverted = clipPath->isInverseFillType();
682 if (fillInverted) {
683 clipPath.writable()->toggleInverseFillType();
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000684 }
685 pr = this->getContext()->getPathRenderer(*clipPath,
686 stroke,
687 fGpu,
688 false,
689 GrPathRendererChain::kStencilOnly_DrawType,
690 &stencilSupport);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000691 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000692 return false;
693 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000694 }
695
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000696 int passes;
697 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
698
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000699 bool canRenderDirectToStencil =
700 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000701 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000702 // fill rule, and set operation can
703 // we render the element directly to
704 // stencil bit used for clipping.
705 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
706 canRenderDirectToStencil,
707 clipBit,
708 fillInverted,
709 &passes,
710 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000711
712 // draw the element to the client stencil bits if necessary
713 if (!canDrawDirectToClip) {
714 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000715 kIncClamp_StencilOp,
716 kIncClamp_StencilOp,
717 kAlways_StencilFunc,
718 0xffff,
719 0x0000,
720 0xffff);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000721 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000722 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000723 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000724 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000725 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000726 SkASSERT(Element::kPath_Type == element->getType());
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000727 if (!clipPath->isEmpty()) {
728 if (canRenderDirectToStencil) {
729 *drawState->stencil() = gDrawToStencil;
730 pr->drawPath(*clipPath, stroke, fGpu, false);
731 } else {
732 pr->stencilPath(*clipPath, stroke, fGpu);
733 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000734 }
735 }
736 }
737
738 // now we modify the clip bit by rendering either the clip
739 // element directly or a bounding rect of the entire clip.
740 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
741 for (int p = 0; p < passes; ++p) {
742 *drawState->stencil() = stencilSettings[p];
743 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000744 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000745 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000746 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000747 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000748 SkASSERT(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000749 SET_RANDOM_COLOR
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000750 pr->drawPath(*clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000751 }
752 } else {
753 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000754 // The view matrix is setup to do clip space -> stencil space translation, so
755 // draw rect in clip space.
reed@google.com44699382013-10-31 17:28:30 +0000756 fGpu->drawSimpleRect(SkRect::Make(clipSpaceIBounds), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000757 }
758 }
759 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000760 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000761 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000762 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000763 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000764 return true;
765}
766
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000767
bsalomon@google.com411dad02012-06-05 20:24:20 +0000768// mapping of clip-respecting stencil funcs to normal stencil funcs
769// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000770static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000771 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
772 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
773 // In the Clip Funcs
774 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
775 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
776 kLess_StencilFunc, // kLessIfInClip_StencilFunc
777 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
778 // Special in the clip func that forces user's ref to be 0.
779 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
780 // make ref 0 and do normal nequal.
781 },
782 {// Stencil-Clipping is ENABLED
783 // In the Clip Funcs
784 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
785 // eq stencil clip bit, mask
786 // out user bits.
787
788 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
789 // add stencil bit to mask and ref
790
791 kLess_StencilFunc, // kLessIfInClip_StencilFunc
792 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
793 // for both of these we can add
794 // the clip bit to the mask and
795 // ref and compare as normal
796 // Special in the clip func that forces user's ref to be 0.
797 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
798 // make ref have only the clip bit set
799 // and make comparison be less
800 // 10..0 < 1..user_bits..
801 }
802};
803
bsalomon@google.coma3201942012-06-21 19:58:20 +0000804namespace {
805// Sets the settings to clip against the stencil buffer clip while ignoring the
806// client bits.
807const GrStencilSettings& basic_apply_stencil_clip_settings() {
808 // stencil settings to use when clip is in stencil
809 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
810 kKeep_StencilOp,
811 kKeep_StencilOp,
812 kAlwaysIfInClip_StencilFunc,
813 0x0000,
814 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000815 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000816 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
817}
818}
819
820void GrClipMaskManager::setGpuStencil() {
821 // We make two copies of the StencilSettings here (except in the early
822 // exit scenario. One copy from draw state to the stack var. Then another
823 // from the stack var to the gpu. We could make this class hold a ptr to
824 // GrGpu's fStencilSettings and eliminate the stack copy here.
825
826 const GrDrawState& drawState = fGpu->getDrawState();
827
828 // use stencil for clipping if clipping is enabled and the clip
829 // has been written into the stencil.
830 GrClipMaskManager::StencilClipMode clipMode;
831 if (this->isClipInStencil() && drawState.isClipState()) {
832 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
833 // We can't be modifying the clip and respecting it at the same time.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000834 SkASSERT(!drawState.isStateFlagEnabled(
bsalomon@google.coma3201942012-06-21 19:58:20 +0000835 GrGpu::kModifyStencilClip_StateBit));
836 } else if (drawState.isStateFlagEnabled(
837 GrGpu::kModifyStencilClip_StateBit)) {
838 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
839 } else {
840 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
841 }
842
843 GrStencilSettings settings;
844 // The GrGpu client may not be using the stencil buffer but we may need to
845 // enable it in order to respect a stencil clip.
846 if (drawState.getStencil().isDisabled()) {
847 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
848 settings = basic_apply_stencil_clip_settings();
849 } else {
850 fGpu->disableStencil();
851 return;
852 }
853 } else {
854 settings = drawState.getStencil();
855 }
856
857 // TODO: dynamically attach a stencil buffer
858 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000859 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000860 drawState.getRenderTarget()->getStencilBuffer();
861 if (NULL != stencilBuffer) {
862 stencilBits = stencilBuffer->bits();
863 }
864
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000865 SkASSERT(fGpu->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
866 SkASSERT(fGpu->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000867 this->adjustStencilParams(&settings, clipMode, stencilBits);
868 fGpu->setStencilSettings(settings);
869}
870
871void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
872 StencilClipMode mode,
873 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000874 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000875
876 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000877 // We assume that this clip manager itself is drawing to the GrGpu and
878 // has already setup the correct values.
879 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000880 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000881
bsalomon@google.com411dad02012-06-05 20:24:20 +0000882 unsigned int clipBit = (1 << (stencilBitCnt - 1));
883 unsigned int userBits = clipBit - 1;
884
bsalomon@google.coma3201942012-06-21 19:58:20 +0000885 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000886 bool twoSided = fGpu->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000887
bsalomon@google.coma3201942012-06-21 19:58:20 +0000888 bool finished = false;
889 while (!finished) {
890 GrStencilFunc func = settings->func(face);
891 uint16_t writeMask = settings->writeMask(face);
892 uint16_t funcMask = settings->funcMask(face);
893 uint16_t funcRef = settings->funcRef(face);
894
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000895 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000896
897 writeMask &= userBits;
898
899 if (func >= kBasicStencilFuncCount) {
900 int respectClip = kRespectClip_StencilClipMode == mode;
901 if (respectClip) {
902 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000903 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000904 switch (func) {
905 case kAlwaysIfInClip_StencilFunc:
906 funcMask = clipBit;
907 funcRef = clipBit;
908 break;
909 case kEqualIfInClip_StencilFunc:
910 case kLessIfInClip_StencilFunc:
911 case kLEqualIfInClip_StencilFunc:
912 funcMask = (funcMask & userBits) | clipBit;
913 funcRef = (funcRef & userBits) | clipBit;
914 break;
915 case kNonZeroIfInClip_StencilFunc:
916 funcMask = (funcMask & userBits) | clipBit;
917 funcRef = clipBit;
918 break;
919 default:
920 GrCrash("Unknown stencil func");
921 }
922 } else {
923 funcMask &= userBits;
924 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000925 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000926 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000927 gSpecialToBasicStencilFunc[respectClip];
928 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000929 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000930 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000931 funcMask &= userBits;
932 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000933 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000934
935 settings->setFunc(face, func);
936 settings->setWriteMask(face, writeMask);
937 settings->setFuncMask(face, funcMask);
938 settings->setFuncRef(face, funcRef);
939
940 if (GrStencilSettings::kFront_Face == face) {
941 face = GrStencilSettings::kBack_Face;
942 finished = !twoSided;
943 } else {
944 finished = true;
945 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000946 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000947 if (!twoSided) {
948 settings->copyFrontSettingsToBack();
949 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000950}
951
952////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000953GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000954 GrReducedClip::InitialState initialState,
955 const GrReducedClip::ElementList& elements,
956 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000957 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000958
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000959 GrTexture* result;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000960 if (this->getMaskTexture(elementsGenID, clipSpaceIBounds, &result, true)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000961 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000962 }
963
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000964 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000965 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000966 return NULL;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000967 }
968
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000969 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
970 // the top left corner of the resulting rect to the top left of the texture.
971 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
972
robertphillips@google.com2c756812012-05-22 20:28:23 +0000973 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000974
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000975 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000976 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
977 SkIntToScalar(-clipSpaceIBounds.fTop));
978 helper.init(maskSpaceIBounds, &matrix);
979
980 helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000981
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000982 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000983
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000984 for (ElementList::Iter iter(elements.headIter()) ; NULL != iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000985
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000986 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000987 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000988
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000989 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
990 // Intersect and reverse difference require modifying pixels outside of the geometry
991 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
992 // but leave the pixels inside the geometry alone. For reverse difference we invert all
993 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000994 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +0000995 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000996 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000997 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000998 }
999
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001000 if (Element::kRect_Type == element->getType()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001001 // convert the rect to a path so we can invert the fill
1002 SkPath temp;
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001003 temp.addRect(element->getRect());
sugoi@google.com12b4e272012-12-06 20:13:11 +00001004 temp.setFillType(SkPath::kInverseEvenOdd_FillType);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001005
sugoi@google.com12b4e272012-12-06 20:13:11 +00001006 helper.draw(temp, stroke, SkRegion::kReplace_Op,
1007 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001008 0x00);
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001009 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001010 SkASSERT(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +00001011 SkPath clipPath = element->getPath();
1012 clipPath.toggleInverseFillType();
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +00001013 helper.draw(clipPath, stroke,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001014 SkRegion::kReplace_Op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001015 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001016 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001017 }
1018
1019 continue;
1020 }
1021
1022 // The other ops (union, xor, diff) only affect pixels inside
1023 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001024 if (Element::kRect_Type == element->getType()) {
1025 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1026 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001027 SkASSERT(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +00001028 helper.draw(element->getPath(), stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001029 }
1030 }
1031
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001032 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001033
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001034 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001035 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001036}
1037
robertphillips@google.comf294b772012-04-27 14:29:26 +00001038////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001039void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001040 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001041}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001042
1043void GrClipMaskManager::setGpu(GrGpu* gpu) {
1044 fGpu = gpu;
1045 fAACache.setContext(gpu->getContext());
1046}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001047
1048void GrClipMaskManager::adjustPathStencilParams(GrStencilSettings* settings) {
1049 const GrDrawState& drawState = fGpu->getDrawState();
1050 GrClipMaskManager::StencilClipMode clipMode;
1051 if (this->isClipInStencil() && drawState.isClipState()) {
1052 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
1053 // We can't be modifying the clip and respecting it at the same time.
1054 SkASSERT(!drawState.isStateFlagEnabled(
1055 GrGpu::kModifyStencilClip_StateBit));
1056 } else if (drawState.isStateFlagEnabled(
1057 GrGpu::kModifyStencilClip_StateBit)) {
1058 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
1059 } else {
1060 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
1061 }
1062
1063 // TODO: dynamically attach a stencil buffer
1064 int stencilBits = 0;
1065 GrStencilBuffer* stencilBuffer =
1066 drawState.getRenderTarget()->getStencilBuffer();
1067 if (NULL != stencilBuffer) {
1068 stencilBits = stencilBuffer->bits();
1069 this->adjustStencilParams(settings, clipMode, stencilBits);
1070 }
1071}