blob: 83b3b01159b99fd61687f94114be9a0aaa09b447 [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.come79f3202014-02-11 16:30:21 +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
robertphillips@google.come79f3202014-02-11 16:30:21 +000036void setup_drawstate_aaclip(GrGpu* gpu,
37 GrTexture* result,
38 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.come79f3202014-02-11 16:30:21 +000062bool path_needs_SW_renderer(GrContext* context,
63 GrGpu* gpu,
64 const SkPath& origPath,
65 const SkStrokeRec& stroke,
66 bool doAA) {
67 // 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 }
72 // 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
robertphillips@google.come79f3202014-02-11 16:30:21 +000077 return NULL == context->getPathRenderer(*path, stroke, gpu, false, type);
78}
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,
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000113 GrDrawState::AutoRestoreEffects* are,
114 const SkRect* devBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000115 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000116
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000117 ElementList elements(16);
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000118 int32_t genID;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000119 InitialState initialState;
120 SkIRect clipSpaceIBounds;
121 bool requiresAA;
122 bool isRect = false;
123
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000124 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000125
126 const GrRenderTarget* rt = drawState->getRenderTarget();
127 // GrDrawTarget should have filtered this for us
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000128 SkASSERT(NULL != rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000129
130 bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen();
131
132 if (!ignoreClip) {
133 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
134 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
135 ReduceClipStack(*clipDataIn->fClipStack,
136 clipSpaceRTIBounds,
137 &elements,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000138 &genID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000139 &initialState,
140 &clipSpaceIBounds,
141 &requiresAA);
142 if (elements.isEmpty()) {
143 if (kAllIn_InitialState == initialState) {
144 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
145 isRect = true;
146 } else {
147 return false;
148 }
149 }
150 }
151
152 if (ignoreClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000153 fGpu->disableScissor();
154 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000155 return true;
156 }
157
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000158 // If there is only one clip element we check whether the draw's bounds are contained
159 // fully within the clip. If not, we install an effect that handles the clip for some
160 // cases.
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000161 if (1 == elements.count() && SkRegion::kReplace_Op == elements.tail()->getOp()) {
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000162 if (NULL != devBounds) {
163 SkRect boundsInClipSpace = *devBounds;
164 boundsInClipSpace.offset(SkIntToScalar(clipDataIn->fOrigin.fX),
165 SkIntToScalar(clipDataIn->fOrigin.fY));
166 if (elements.tail()->contains(boundsInClipSpace)) {
167 fGpu->disableScissor();
168 this->setGpuStencil();
169 return true;
170 }
171 }
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000172 SkAutoTUnref<GrEffectRef> effect;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000173 if (SkClipStack::Element::kPath_Type == elements.tail()->getType()) {
174 const SkPath& path = elements.tail()->getPath();
175 bool isAA = GR_AA_CLIP && elements.tail()->isAA();
176 if (rt->isMultisampled()) {
177 // A coverage effect for AA clipping won't play nicely with MSAA.
178 if (!isAA) {
179 SkVector offset = { SkIntToScalar(-clipDataIn->fOrigin.fX),
180 SkIntToScalar(-clipDataIn->fOrigin.fY) };
181 effect.reset(GrConvexPolyEffect::Create(GrConvexPolyEffect::kFillNoAA_EdgeType,
182 path, &offset));
183 }
184 } else {
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000185 SkVector offset = { SkIntToScalar(-clipDataIn->fOrigin.fX),
186 SkIntToScalar(-clipDataIn->fOrigin.fY) };
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000187 GrConvexPolyEffect::EdgeType type = isAA ? GrConvexPolyEffect::kFillAA_EdgeType :
188 GrConvexPolyEffect::kFillNoAA_EdgeType;
189 effect.reset(GrConvexPolyEffect::Create(type, path, &offset));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000190 }
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000191 } else if (GR_AA_CLIP && elements.tail()->isAA() && !rt->isMultisampled()) {
192 // We only handle AA/non-MSAA rects here. Coverage effect AA isn't MSAA friendly and
193 // non-AA rect clips are handled by the scissor.
194 SkASSERT(SkClipStack::Element::kRect_Type == elements.tail()->getType());
195 SkRect rect = elements.tail()->getRect();
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000196 SkVector offset = { SkIntToScalar(-clipDataIn->fOrigin.fX),
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000197 SkIntToScalar(-clipDataIn->fOrigin.fY) };
198 rect.offset(offset);
199 effect.reset(GrConvexPolyEffect::CreateForAAFillRect(rect));
200 // This should never fail.
201 SkASSERT(effect);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000202 }
203 if (effect) {
204 are->set(fGpu->drawState());
205 fGpu->drawState()->addCoverageEffect(effect);
commit-bot@chromium.org6516d4b2014-02-07 14:04:48 +0000206 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
207 scissorSpaceIBounds.offset(-clipDataIn->fOrigin);
208 fGpu->enableScissor(scissorSpaceIBounds);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000209 this->setGpuStencil();
210 return true;
211 }
212 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000213
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000214#if GR_AA_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000215 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000216 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000217 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000218
219 if (this->useSWOnlyPath(elements)) {
220 // The clip geometry is complex enough that it will be more efficient to create it
221 // entirely in software
222 result = this->createSoftwareClipMask(genID,
223 initialState,
224 elements,
225 clipSpaceIBounds);
226 } else {
227 result = this->createAlphaClipMask(genID,
228 initialState,
229 elements,
230 clipSpaceIBounds);
231 }
232
233 if (NULL != result) {
234 // The mask's top left coord should be pinned to the rounded-out top left corner of
235 // clipSpace bounds. We determine the mask's position WRT to the render target here.
236 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
237 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000238 are->set(fGpu->drawState());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000239 setup_drawstate_aaclip(fGpu, result, rtSpaceMaskBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000240 fGpu->disableScissor();
241 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000242 return true;
243 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000244 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000245 }
246#endif // GR_AA_CLIP
247
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000248 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
249 // be created. In either case, free up the texture in the anti-aliased mask cache.
250 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
251 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
252 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000253 fAACache.reset();
254
bsalomon@google.coma3201942012-06-21 19:58:20 +0000255 // If the clip is a rectangle then just set the scissor. Otherwise, create
256 // a stencil mask.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000257 if (isRect) {
258 SkIRect clipRect = clipSpaceIBounds;
259 clipRect.offset(-clipDataIn->fOrigin);
260 fGpu->enableScissor(clipRect);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000261 this->setGpuStencil();
262 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000263 }
264
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000265 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000266 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000267 this->createStencilClipMask(genID,
268 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000269 elements,
270 clipSpaceIBounds,
271 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000272
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000273 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
274 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
275 // use both stencil and scissor test to the bounds for the final draw.
276 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
277 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
278 fGpu->enableScissor(scissorSpaceIBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000279 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000280 return true;
281}
282
283#define VISUALIZE_COMPLEX_CLIP 0
284
285#if VISUALIZE_COMPLEX_CLIP
tfarina@chromium.org223137f2012-11-21 22:38:36 +0000286 #include "SkRandom.h"
287 SkRandom gRandom;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000288 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
289#else
290 #define SET_RANDOM_COLOR
291#endif
292
293namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000294
295////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000296// set up the OpenGL blend function to perform the specified
297// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000298void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000299
300 switch (op) {
301 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000302 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000303 break;
304 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000305 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000306 break;
307 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000308 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000309 break;
310 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000311 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000312 break;
313 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000314 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000315 break;
316 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000317 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000318 break;
319 default:
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000320 SkASSERT(false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000321 break;
322 }
323}
324
robertphillips@google.com72176b22012-05-23 13:19:12 +0000325}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000326
327////////////////////////////////////////////////////////////////////////////////
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000328bool GrClipMaskManager::drawElement(GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000329 const SkClipStack::Element* element,
330 GrPathRenderer* pr) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000331 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000332
333 drawState->setRenderTarget(target->asRenderTarget());
334
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000335 switch (element->getType()) {
336 case Element::kRect_Type:
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000337 // TODO: Do rects directly to the accumulator using a aa-rect GrEffect that covers the
338 // entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000339 if (element->isAA()) {
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000340 getContext()->getAARectRenderer()->fillAARect(fGpu,
341 fGpu,
342 element->getRect(),
robertphillips@google.comb19cb7f2013-05-02 15:37:20 +0000343 SkMatrix::I(),
robertphillips@google.comafd1cba2013-05-14 19:47:47 +0000344 element->getRect(),
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000345 false);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000346 } else {
347 fGpu->drawSimpleRect(element->getRect(), NULL);
348 }
349 return true;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000350 case Element::kPath_Type: {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000351 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
352 if (path->isInverseFillType()) {
353 path.writable()->toggleInverseFillType();
354 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000355 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000356 if (NULL == pr) {
357 GrPathRendererChain::DrawType type;
358 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
359 GrPathRendererChain::kColor_DrawType;
360 pr = this->getContext()->getPathRenderer(*path, stroke, fGpu, false, type);
361 }
362 if (NULL == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000363 return false;
364 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000365 pr->drawPath(element->getPath(), stroke, fGpu, element->isAA());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000366 break;
367 }
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000368 default:
369 // something is wrong if we're trying to draw an empty element.
370 GrCrash("Unexpected element type");
371 return false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000372 }
373 return true;
374}
375
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000376bool GrClipMaskManager::canStencilAndDrawElement(GrTexture* target,
377 const SkClipStack::Element* element,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000378 GrPathRenderer** pr) {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000379 GrDrawState* drawState = fGpu->drawState();
380 drawState->setRenderTarget(target->asRenderTarget());
381
382 switch (element->getType()) {
383 case Element::kRect_Type:
384 return true;
385 case Element::kPath_Type: {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000386 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
387 if (path->isInverseFillType()) {
388 path.writable()->toggleInverseFillType();
389 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000390 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000391 GrPathRendererChain::DrawType type = element->isAA() ?
392 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
393 GrPathRendererChain::kStencilAndColor_DrawType;
robertphillips@google.come79f3202014-02-11 16:30:21 +0000394 *pr = this->getContext()->getPathRenderer(*path, stroke, fGpu, false, type);
395 return NULL != *pr;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000396 }
397 default:
398 // something is wrong if we're trying to draw an empty element.
399 GrCrash("Unexpected element type");
400 return false;
401 }
402}
403
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000404void GrClipMaskManager::mergeMask(GrTexture* dstMask,
405 GrTexture* srcMask,
406 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000407 const SkIRect& dstBound,
408 const SkIRect& srcBound) {
bsalomon@google.com137f1342013-05-29 21:27:53 +0000409 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000410 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000411 SkAssertResult(avmr.setIdentity(drawState));
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000412 GrDrawState::AutoRestoreEffects are(drawState);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000413
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000414 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000415
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000416 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000417
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000418 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000419 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000420
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000421 drawState->addColorEffect(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000422 GrTextureDomainEffect::Create(srcMask,
423 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000424 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
425 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000426 GrTextureParams::kNone_FilterMode))->unref();
reed@google.com44699382013-10-31 17:28:30 +0000427 fGpu->drawSimpleRect(SkRect::Make(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000428}
429
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000430// get a texture to act as a temporary buffer for AA clip boolean operations
431// TODO: given the expense of createTexture we may want to just cache this too
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000432void GrClipMaskManager::getTemp(int width, int height, GrAutoScratchTexture* temp) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000433 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000434 // we've already allocated the temp texture
435 return;
436 }
437
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000438 GrTextureDesc desc;
439 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000440 desc.fWidth = width;
441 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000442 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000443
robertphillips@google.com2c756812012-05-22 20:28:23 +0000444 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000445}
446
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000447////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000448// Handles caching & allocation (if needed) of a clip alpha-mask texture for both the sw-upload
449// or gpu-rendered cases. Returns true if there is no more work to be done (i.e., we got a cache
450// hit)
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000451bool GrClipMaskManager::getMaskTexture(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000452 const SkIRect& clipSpaceIBounds,
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000453 GrTexture** result,
454 bool willUpload) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000455 bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000456 if (!cached) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000457
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000458 // There isn't a suitable entry in the cache so we create a new texture to store the mask.
459 // Since we are setting up the cache we know the last lookup was a miss. Free up the
460 // currently cached mask so it can be reused.
461 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000462
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000463 GrTextureDesc desc;
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000464 desc.fFlags = willUpload ? kNone_GrTextureFlags : kRenderTarget_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000465 desc.fWidth = clipSpaceIBounds.width();
466 desc.fHeight = clipSpaceIBounds.height();
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000467 desc.fConfig = kRGBA_8888_GrPixelConfig;
robertphillips@google.com94bdd7e2013-10-31 15:50:43 +0000468 if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000469 // We would always like A8 but it isn't supported on all platforms
470 desc.fConfig = kAlpha_8_GrPixelConfig;
471 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000472
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000473 fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000474 }
475
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000476 *result = fAACache.getLastMask();
477 return cached;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000478}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000479
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000480////////////////////////////////////////////////////////////////////////////////
481// Create a 8-bit clip mask in alpha
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000482GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000483 InitialState initialState,
484 const ElementList& elements,
485 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000486 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000487
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000488 GrTexture* result;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000489 if (this->getMaskTexture(elementsGenID, clipSpaceIBounds, &result, false)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000490 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000491 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000492 }
493
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000494 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000495 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000496 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000497 }
498
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000499 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000500 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000501 SkIntToScalar(-clipSpaceIBounds.fLeft),
502 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000503 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000504 // The texture may be larger than necessary, this rect represents the part of the texture
505 // we populate with a rasterization of the clip.
506 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
507
bsalomon@google.com137f1342013-05-29 21:27:53 +0000508 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
509 SkMatrix translate;
510 translate.setTranslate(clipToMaskOffset);
511 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &translate);
512
513 GrDrawState* drawState = fGpu->drawState();
514
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000515 // We're drawing a coverage mask and want coverage to be run through the blend function.
516 drawState->enableState(GrDrawState::kCoverageDrawing_StateBit);
517
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000518 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
519 // clear the part that we care about.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000520 fGpu->clear(&maskSpaceIBounds,
521 kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000522 true,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000523 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000524
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000525 // When we use the stencil in the below loop it is important to have this clip installed.
526 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
527 // pass must not set values outside of this bounds or stencil values outside the rect won't be
528 // cleared.
529 GrDrawTarget::AutoClipRestore acr(fGpu, maskSpaceIBounds);
530 drawState->enableState(GrDrawState::kClip_StateBit);
531
robertphillips@google.comf105b102012-05-14 12:18:26 +0000532 GrAutoScratchTexture temp;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000533 // walk through each clip element and perform its set op
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000534 for (ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000535 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000536 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000537 bool invert = element->isInverseFilled();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000538
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000539 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000540 GrPathRenderer* pr = NULL;
541 bool useTemp = !this->canStencilAndDrawElement(result, element, &pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000542 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000543 // 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 +0000544 // mask buffer can be substantially larger than the actually clip stack element. We
545 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000546 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000547 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000548
549 if (useTemp) {
550 if (invert) {
551 maskSpaceElementIBounds = maskSpaceIBounds;
552 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000553 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000554 elementBounds.offset(clipToMaskOffset);
555 elementBounds.roundOut(&maskSpaceElementIBounds);
556 }
557
558 this->getTemp(maskSpaceIBounds.fRight, maskSpaceIBounds.fBottom, &temp);
559 if (NULL == temp.texture()) {
560 fAACache.reset();
561 return NULL;
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000562 }
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000563 dst = temp.texture();
564 // clear the temp target and set blend to replace
565 fGpu->clear(&maskSpaceElementIBounds,
566 invert ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000567 true,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000568 dst->asRenderTarget());
569 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000570
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000571 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000572 // draw directly into the result with the stencil set to make the pixels affected
573 // by the clip shape be non-zero.
574 dst = result;
575 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
576 kReplace_StencilOp,
577 kReplace_StencilOp,
578 kAlways_StencilFunc,
579 0xffff,
580 0xffff,
581 0xffff);
582 drawState->setStencil(kStencilInElement);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000583 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000584 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000585
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000586 drawState->setAlpha(invert ? 0x00 : 0xff);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000587
robertphillips@google.come79f3202014-02-11 16:30:21 +0000588 if (!this->drawElement(dst, element, pr)) {
589 fAACache.reset();
590 return NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000591 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000592
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000593 if (useTemp) {
594 // Now draw into the accumulator using the real operation and the temp buffer as a
595 // texture
596 this->mergeMask(result,
597 temp.texture(),
598 op,
599 maskSpaceIBounds,
600 maskSpaceElementIBounds);
601 } else {
602 // Draw to the exterior pixels (those with a zero stencil value).
603 drawState->setAlpha(invert ? 0xff : 0x00);
604 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
605 kZero_StencilOp,
606 kZero_StencilOp,
607 kEqual_StencilFunc,
608 0xffff,
609 0x0000,
610 0xffff);
611 drawState->setStencil(kDrawOutsideElement);
612 fGpu->drawSimpleRect(clipSpaceIBounds);
613 drawState->disableStencil();
614 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000615 } else {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000616 // all the remaining ops can just be directly draw into the accumulation buffer
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000617 drawState->setAlpha(0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000618 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000619 this->drawElement(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000620 }
621 }
622
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000623 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000624 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000625}
626
627////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000628// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000629// (as opposed to canvas) coordinates
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000630bool GrClipMaskManager::createStencilClipMask(int32_t elementsGenID,
631 InitialState initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000632 const ElementList& elements,
633 const SkIRect& clipSpaceIBounds,
634 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000635
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000636 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000637
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000638 GrDrawState* drawState = fGpu->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000639 SkASSERT(drawState->isClipState());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000640
641 GrRenderTarget* rt = drawState->getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000642 SkASSERT(NULL != rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000643
644 // TODO: dynamically attach a SB when needed.
645 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
646 if (NULL == stencilBuffer) {
647 return false;
648 }
649
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000650 if (stencilBuffer->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000651
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000652 stencilBuffer->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000653
bsalomon@google.com137f1342013-05-29 21:27:53 +0000654 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
655 SkVector translate = {
656 SkIntToScalar(clipSpaceToStencilOffset.fX),
657 SkIntToScalar(clipSpaceToStencilOffset.fY)
658 };
659 SkMatrix matrix;
660 matrix.setTranslate(translate);
661 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &matrix);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000662 drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000663
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000664 drawState->setRenderTarget(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000665
bsalomon@google.com9f131742012-12-13 20:43:56 +0000666 // We set the current clip to the bounds so that our recursive draws are scissored to them.
667 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
668 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
669 GrDrawTarget::AutoClipRestore acr(fGpu, stencilSpaceIBounds);
670 drawState->enableState(GrDrawState::kClip_StateBit);
671
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000672#if !VISUALIZE_COMPLEX_CLIP
673 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
674#endif
675
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000676 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000677 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000678 clipBit = (1 << (clipBit-1));
679
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000680 fGpu->clearStencilClip(stencilSpaceIBounds, kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000681
682 // walk through each clip element and perform its set op
683 // with the existing clip.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000684 for (ElementList::Iter iter(elements.headIter()); NULL != iter.get(); iter.next()) {
685 const Element* element = iter.get();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000686 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000687 // enabled at bottom of loop
688 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000689 // if the target is MSAA then we want MSAA enabled when the clip is soft
690 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000691 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000692 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000693
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000694 // This will be used to determine whether the clip shape can be rendered into the
695 // stencil with arbitrary stencil settings.
696 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000697
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000698 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000699
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000700 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000701
robertphillips@google.come79f3202014-02-11 16:30:21 +0000702 GrPathRenderer* pr = NULL;
703 SkTCopyOnFirstWrite<SkPath> clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000704 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000705 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000706 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000707 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000708 SkASSERT(Element::kPath_Type == element->getType());
robertphillips@google.come79f3202014-02-11 16:30:21 +0000709 clipPath.init(element->getPath());
710 fillInverted = clipPath->isInverseFillType();
711 if (fillInverted) {
712 clipPath.writable()->toggleInverseFillType();
713 }
714 pr = this->getContext()->getPathRenderer(*clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000715 stroke,
716 fGpu,
717 false,
718 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000719 &stencilSupport);
720 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000721 return false;
722 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000723 }
724
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000725 int passes;
726 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
727
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000728 bool canRenderDirectToStencil =
729 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000730 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000731 // fill rule, and set operation can
732 // we render the element directly to
733 // stencil bit used for clipping.
734 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
735 canRenderDirectToStencil,
736 clipBit,
737 fillInverted,
738 &passes,
739 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000740
741 // draw the element to the client stencil bits if necessary
742 if (!canDrawDirectToClip) {
743 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000744 kIncClamp_StencilOp,
745 kIncClamp_StencilOp,
746 kAlways_StencilFunc,
747 0xffff,
748 0x0000,
749 0xffff);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000750 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000751 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000752 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000753 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000754 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000755 SkASSERT(Element::kPath_Type == element->getType());
robertphillips@google.come79f3202014-02-11 16:30:21 +0000756 if (!clipPath->isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000757 if (canRenderDirectToStencil) {
758 *drawState->stencil() = gDrawToStencil;
robertphillips@google.come79f3202014-02-11 16:30:21 +0000759 pr->drawPath(*clipPath, stroke, fGpu, false);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000760 } else {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000761 pr->stencilPath(*clipPath, stroke, fGpu);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000762 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000763 }
764 }
765 }
766
767 // now we modify the clip bit by rendering either the clip
768 // element directly or a bounding rect of the entire clip.
769 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
770 for (int p = 0; p < passes; ++p) {
771 *drawState->stencil() = stencilSettings[p];
772 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000773 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000774 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000775 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000776 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000777 SkASSERT(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000778 SET_RANDOM_COLOR
robertphillips@google.come79f3202014-02-11 16:30:21 +0000779 pr->drawPath(*clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000780 }
781 } else {
782 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000783 // The view matrix is setup to do clip space -> stencil space translation, so
784 // draw rect in clip space.
reed@google.com44699382013-10-31 17:28:30 +0000785 fGpu->drawSimpleRect(SkRect::Make(clipSpaceIBounds), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000786 }
787 }
788 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000789 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000790 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000791 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000792 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000793 return true;
794}
795
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000796
bsalomon@google.com411dad02012-06-05 20:24:20 +0000797// mapping of clip-respecting stencil funcs to normal stencil funcs
798// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000799static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000800 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
801 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
802 // In the Clip Funcs
803 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
804 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
805 kLess_StencilFunc, // kLessIfInClip_StencilFunc
806 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
807 // Special in the clip func that forces user's ref to be 0.
808 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
809 // make ref 0 and do normal nequal.
810 },
811 {// Stencil-Clipping is ENABLED
812 // In the Clip Funcs
813 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
814 // eq stencil clip bit, mask
815 // out user bits.
816
817 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
818 // add stencil bit to mask and ref
819
820 kLess_StencilFunc, // kLessIfInClip_StencilFunc
821 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
822 // for both of these we can add
823 // the clip bit to the mask and
824 // ref and compare as normal
825 // Special in the clip func that forces user's ref to be 0.
826 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
827 // make ref have only the clip bit set
828 // and make comparison be less
829 // 10..0 < 1..user_bits..
830 }
831};
832
bsalomon@google.coma3201942012-06-21 19:58:20 +0000833namespace {
834// Sets the settings to clip against the stencil buffer clip while ignoring the
835// client bits.
836const GrStencilSettings& basic_apply_stencil_clip_settings() {
837 // stencil settings to use when clip is in stencil
838 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
839 kKeep_StencilOp,
840 kKeep_StencilOp,
841 kAlwaysIfInClip_StencilFunc,
842 0x0000,
843 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000844 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000845 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
846}
847}
848
849void GrClipMaskManager::setGpuStencil() {
850 // We make two copies of the StencilSettings here (except in the early
851 // exit scenario. One copy from draw state to the stack var. Then another
852 // from the stack var to the gpu. We could make this class hold a ptr to
853 // GrGpu's fStencilSettings and eliminate the stack copy here.
854
855 const GrDrawState& drawState = fGpu->getDrawState();
856
857 // use stencil for clipping if clipping is enabled and the clip
858 // has been written into the stencil.
859 GrClipMaskManager::StencilClipMode clipMode;
860 if (this->isClipInStencil() && drawState.isClipState()) {
861 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
862 // We can't be modifying the clip and respecting it at the same time.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000863 SkASSERT(!drawState.isStateFlagEnabled(
bsalomon@google.coma3201942012-06-21 19:58:20 +0000864 GrGpu::kModifyStencilClip_StateBit));
865 } else if (drawState.isStateFlagEnabled(
866 GrGpu::kModifyStencilClip_StateBit)) {
867 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
868 } else {
869 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
870 }
871
872 GrStencilSettings settings;
873 // The GrGpu client may not be using the stencil buffer but we may need to
874 // enable it in order to respect a stencil clip.
875 if (drawState.getStencil().isDisabled()) {
876 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
877 settings = basic_apply_stencil_clip_settings();
878 } else {
879 fGpu->disableStencil();
880 return;
881 }
882 } else {
883 settings = drawState.getStencil();
884 }
885
886 // TODO: dynamically attach a stencil buffer
887 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000888 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000889 drawState.getRenderTarget()->getStencilBuffer();
890 if (NULL != stencilBuffer) {
891 stencilBits = stencilBuffer->bits();
892 }
893
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000894 SkASSERT(fGpu->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
895 SkASSERT(fGpu->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000896 this->adjustStencilParams(&settings, clipMode, stencilBits);
897 fGpu->setStencilSettings(settings);
898}
899
900void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
901 StencilClipMode mode,
902 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000903 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000904
905 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000906 // We assume that this clip manager itself is drawing to the GrGpu and
907 // has already setup the correct values.
908 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000909 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000910
bsalomon@google.com411dad02012-06-05 20:24:20 +0000911 unsigned int clipBit = (1 << (stencilBitCnt - 1));
912 unsigned int userBits = clipBit - 1;
913
bsalomon@google.coma3201942012-06-21 19:58:20 +0000914 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000915 bool twoSided = fGpu->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000916
bsalomon@google.coma3201942012-06-21 19:58:20 +0000917 bool finished = false;
918 while (!finished) {
919 GrStencilFunc func = settings->func(face);
920 uint16_t writeMask = settings->writeMask(face);
921 uint16_t funcMask = settings->funcMask(face);
922 uint16_t funcRef = settings->funcRef(face);
923
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000924 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000925
926 writeMask &= userBits;
927
928 if (func >= kBasicStencilFuncCount) {
929 int respectClip = kRespectClip_StencilClipMode == mode;
930 if (respectClip) {
931 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000932 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000933 switch (func) {
934 case kAlwaysIfInClip_StencilFunc:
935 funcMask = clipBit;
936 funcRef = clipBit;
937 break;
938 case kEqualIfInClip_StencilFunc:
939 case kLessIfInClip_StencilFunc:
940 case kLEqualIfInClip_StencilFunc:
941 funcMask = (funcMask & userBits) | clipBit;
942 funcRef = (funcRef & userBits) | clipBit;
943 break;
944 case kNonZeroIfInClip_StencilFunc:
945 funcMask = (funcMask & userBits) | clipBit;
946 funcRef = clipBit;
947 break;
948 default:
949 GrCrash("Unknown stencil func");
950 }
951 } else {
952 funcMask &= userBits;
953 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000954 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000955 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000956 gSpecialToBasicStencilFunc[respectClip];
957 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000958 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000959 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000960 funcMask &= userBits;
961 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000962 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000963
964 settings->setFunc(face, func);
965 settings->setWriteMask(face, writeMask);
966 settings->setFuncMask(face, funcMask);
967 settings->setFuncRef(face, funcRef);
968
969 if (GrStencilSettings::kFront_Face == face) {
970 face = GrStencilSettings::kBack_Face;
971 finished = !twoSided;
972 } else {
973 finished = true;
974 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000975 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000976 if (!twoSided) {
977 settings->copyFrontSettingsToBack();
978 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000979}
980
981////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000982GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000983 GrReducedClip::InitialState initialState,
984 const GrReducedClip::ElementList& elements,
985 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000986 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000987
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000988 GrTexture* result;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000989 if (this->getMaskTexture(elementsGenID, clipSpaceIBounds, &result, true)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000990 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000991 }
992
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000993 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000994 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000995 return NULL;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000996 }
997
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000998 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
999 // the top left corner of the resulting rect to the top left of the texture.
1000 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1001
robertphillips@google.com2c756812012-05-22 20:28:23 +00001002 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001003
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001004 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001005 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
1006 SkIntToScalar(-clipSpaceIBounds.fTop));
1007 helper.init(maskSpaceIBounds, &matrix);
1008
1009 helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001010
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001011 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001012
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001013 for (ElementList::Iter iter(elements.headIter()) ; NULL != iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001014
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001015 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001016 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001017
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001018 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1019 // Intersect and reverse difference require modifying pixels outside of the geometry
1020 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1021 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1022 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001023 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001024 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001025 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001026 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001027 }
1028
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001029 if (Element::kRect_Type == element->getType()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001030 // convert the rect to a path so we can invert the fill
1031 SkPath temp;
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001032 temp.addRect(element->getRect());
sugoi@google.com12b4e272012-12-06 20:13:11 +00001033 temp.setFillType(SkPath::kInverseEvenOdd_FillType);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001034
sugoi@google.com12b4e272012-12-06 20:13:11 +00001035 helper.draw(temp, stroke, SkRegion::kReplace_Op,
1036 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001037 0x00);
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001038 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001039 SkASSERT(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +00001040 SkPath clipPath = element->getPath();
1041 clipPath.toggleInverseFillType();
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +00001042 helper.draw(clipPath, stroke,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001043 SkRegion::kReplace_Op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001044 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001045 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001046 }
1047
1048 continue;
1049 }
1050
1051 // The other ops (union, xor, diff) only affect pixels inside
1052 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001053 if (Element::kRect_Type == element->getType()) {
1054 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1055 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001056 SkASSERT(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +00001057 helper.draw(element->getPath(), stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001058 }
1059 }
1060
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001061 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001062
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001063 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001064 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001065}
1066
robertphillips@google.comf294b772012-04-27 14:29:26 +00001067////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001068void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001069 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001070}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001071
1072void GrClipMaskManager::setGpu(GrGpu* gpu) {
1073 fGpu = gpu;
1074 fAACache.setContext(gpu->getContext());
1075}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001076
1077void GrClipMaskManager::adjustPathStencilParams(GrStencilSettings* settings) {
1078 const GrDrawState& drawState = fGpu->getDrawState();
1079 GrClipMaskManager::StencilClipMode clipMode;
1080 if (this->isClipInStencil() && drawState.isClipState()) {
1081 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
1082 // We can't be modifying the clip and respecting it at the same time.
1083 SkASSERT(!drawState.isStateFlagEnabled(
1084 GrGpu::kModifyStencilClip_StateBit));
1085 } else if (drawState.isStateFlagEnabled(
1086 GrGpu::kModifyStencilClip_StateBit)) {
1087 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
1088 } else {
1089 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
1090 }
1091
1092 // TODO: dynamically attach a stencil buffer
1093 int stencilBits = 0;
1094 GrStencilBuffer* stencilBuffer =
1095 drawState.getRenderTarget()->getStencilBuffer();
1096 if (NULL != stencilBuffer) {
1097 stencilBits = stencilBuffer->bits();
1098 this->adjustStencilParams(settings, clipMode, stencilBits);
1099 }
1100}