blob: 071b9d31916cd4207b4d559d1542f5f702bd20c6 [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"
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +000022#include "effects/GrRRectEffect.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000023#include "SkRasterClip.h"
24#include "SkStrokeRec.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000025#include "SkTLazy.h"
26
robertphillips@google.comba998f22012-10-12 11:33:56 +000027#define GR_AA_CLIP 1
robertphillips@google.coma72eef32012-05-01 17:22:59 +000028
bsalomon@google.com8182fa02012-12-04 14:06:06 +000029typedef SkClipStack::Element Element;
bsalomon@google.com51a62862012-11-26 21:19:43 +000030
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000031using namespace GrReducedClip;
32
bsalomon@google.com51a62862012-11-26 21:19:43 +000033////////////////////////////////////////////////////////////////////////////////
robertphillips@google.come79f3202014-02-11 16:30:21 +000034namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000035// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000036// stage matrix this also alters the vertex layout
robertphillips@google.come79f3202014-02-11 16:30:21 +000037void setup_drawstate_aaclip(GrGpu* gpu,
38 GrTexture* result,
39 const SkIRect &devBound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000040 GrDrawState* drawState = gpu->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000041 SkASSERT(drawState);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000042
bsalomon@google.comb9086a02012-11-01 18:02:54 +000043 SkMatrix mat;
bsalomon@google.comc7818882013-03-20 19:19:53 +000044 // We want to use device coords to compute the texture coordinates. We set our matrix to be
45 // equal to the view matrix followed by an offset to the devBound, and then a scaling matrix to
46 // normalized coords. We apply this matrix to the vertex positions rather than local coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000047 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000048 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000049 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000050 mat.preConcat(drawState->getViewMatrix());
51
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000052 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000053 // This could be a long-lived effect that is cached with the alpha-mask.
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000054 drawState->addCoverageEffect(
55 GrTextureDomainEffect::Create(result,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000056 mat,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000057 GrTextureDomain::MakeTexelDomain(result, domainTexels),
58 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +000059 GrTextureParams::kNone_FilterMode,
bsalomon@google.com77af6802013-10-02 13:04:56 +000060 kPosition_GrCoordSet))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000061}
62
robertphillips@google.come79f3202014-02-11 16:30:21 +000063bool path_needs_SW_renderer(GrContext* context,
64 GrGpu* gpu,
65 const SkPath& origPath,
66 const SkStrokeRec& stroke,
67 bool doAA) {
68 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
69 SkTCopyOnFirstWrite<SkPath> path(origPath);
70 if (path->isInverseFillType()) {
71 path.writable()->toggleInverseFillType();
72 }
73 // last (false) parameter disallows use of the SW path renderer
bsalomon@google.com45a15f52012-12-10 19:10:17 +000074 GrPathRendererChain::DrawType type = doAA ?
75 GrPathRendererChain::kColorAntiAlias_DrawType :
76 GrPathRendererChain::kColor_DrawType;
77
robertphillips@google.come79f3202014-02-11 16:30:21 +000078 return NULL == context->getPathRenderer(*path, stroke, gpu, false, type);
79}
80
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000081}
82
robertphillips@google.comfa662942012-05-17 12:20:22 +000083/*
84 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
85 * will be used on any element. If so, it returns true to indicate that the
86 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
87 */
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000088bool GrClipMaskManager::useSWOnlyPath(const ElementList& elements) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +000089
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000090 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000091 // a clip gets complex enough it can just be done in SW regardless
92 // of whether it would invoke the GrSoftwarePathRenderer.
sugoi@google.com5f74cf82012-12-17 21:16:45 +000093 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000094
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000095 for (ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
96 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000097 // rects can always be drawn directly w/o using the software path
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000098 // Skip rrects once we're drawing them directly.
99 if (Element::kRect_Type != element->getType()) {
100 SkPath path;
101 element->asPath(&path);
102 if (path_needs_SW_renderer(this->getContext(), fGpu, path, stroke, element->isAA())) {
103 return true;
104 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000105 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000106 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000107 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000108}
109
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000110bool GrClipMaskManager::installClipEffects(const ElementList& elements,
111 GrDrawState::AutoRestoreEffects* are,
112 const SkVector& clipToRTOffset,
113 const SkRect* drawBounds) {
114
115 GrDrawState* drawState = fGpu->drawState();
116 SkRect boundsInClipSpace;
117 if (NULL != drawBounds) {
118 boundsInClipSpace = *drawBounds;
119 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
120 }
121
122 are->set(drawState);
123 GrRenderTarget* rt = drawState->getRenderTarget();
124 ElementList::Iter iter(elements);
125
126 bool setARE = false;
127 bool failed = false;
128
129 while (NULL != iter.get()) {
130 SkRegion::Op op = iter.get()->getOp();
131 bool invert;
132 bool skip = false;
133 switch (op) {
134 case SkRegion::kReplace_Op:
135 SkASSERT(iter.get() == elements.head());
136 // Fallthrough, handled same as intersect.
137 case SkRegion::kIntersect_Op:
138 invert = false;
139 if (NULL != drawBounds && iter.get()->contains(boundsInClipSpace)) {
140 skip = true;
141 }
142 break;
143 case SkRegion::kDifference_Op:
144 invert = true;
145 // We don't currently have a cheap test for whether a rect is fully outside an
146 // element's primitive, so don't attempt to set skip.
147 break;
148 default:
149 failed = true;
150 break;
151 }
152 if (failed) {
153 break;
154 }
155
156 if (!skip) {
157 GrEffectEdgeType edgeType;
158 if (GR_AA_CLIP && iter.get()->isAA()) {
159 if (rt->isMultisampled()) {
160 // Coverage based AA clips don't place nicely with MSAA.
161 failed = true;
162 break;
163 }
164 edgeType = invert ? kInverseFillAA_GrEffectEdgeType : kFillAA_GrEffectEdgeType;
165 } else {
166 edgeType = invert ? kInverseFillBW_GrEffectEdgeType : kFillBW_GrEffectEdgeType;
167 }
168 SkAutoTUnref<GrEffectRef> effect;
169 switch (iter.get()->getType()) {
170 case SkClipStack::Element::kPath_Type:
171 effect.reset(GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
172 &clipToRTOffset));
173 break;
174 case SkClipStack::Element::kRRect_Type: {
175 SkRRect rrect = iter.get()->getRRect();
176 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
177 effect.reset(GrRRectEffect::Create(edgeType, rrect));
178 break;
179 }
180 case SkClipStack::Element::kRect_Type: {
181 SkRect rect = iter.get()->getRect();
182 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
183 effect.reset(GrConvexPolyEffect::Create(edgeType, rect));
184 break;
185 }
186 default:
187 break;
188 }
189 if (effect) {
190 if (!setARE) {
191 are->set(fGpu->drawState());
192 setARE = true;
193 }
194 fGpu->drawState()->addCoverageEffect(effect);
195 } else {
196 failed = true;
197 break;
198 }
199 }
200 iter.next();
201 }
202
203 if (failed) {
204 are->set(NULL);
205 }
206
207 return !failed;
208}
209
robertphillips@google.comf294b772012-04-27 14:29:26 +0000210////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000211// sort out what kind of clip mask needs to be created: alpha, stencil,
212// scissor, or entirely software
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000213bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn,
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000214 GrDrawState::AutoRestoreEffects* are,
215 const SkRect* devBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000216 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000217
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000218 ElementList elements(16);
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000219 int32_t genID;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000220 InitialState initialState;
221 SkIRect clipSpaceIBounds;
222 bool requiresAA;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000223
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000224 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000225
226 const GrRenderTarget* rt = drawState->getRenderTarget();
227 // GrDrawTarget should have filtered this for us
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000228 SkASSERT(NULL != rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000229
230 bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen();
231
232 if (!ignoreClip) {
233 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
234 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
235 ReduceClipStack(*clipDataIn->fClipStack,
236 clipSpaceRTIBounds,
237 &elements,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000238 &genID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000239 &initialState,
240 &clipSpaceIBounds,
241 &requiresAA);
242 if (elements.isEmpty()) {
243 if (kAllIn_InitialState == initialState) {
244 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000245 } else {
246 return false;
247 }
248 }
249 }
250
251 if (ignoreClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000252 fGpu->disableScissor();
253 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000254 return true;
255 }
256
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000257 // An element count of 4 was chosen because of the common pattern in Blink of:
258 // isect RR
259 // diff RR
260 // isect convex_poly
261 // isect convex_poly
262 // when drawing rounded div borders. This could probably be tuned based on a
263 // configuration's relative costs of switching RTs to generate a mask vs
264 // longer shaders.
265 if (elements.count() <= 4) {
266 SkVector clipToRTOffset = { SkIntToScalar(-clipDataIn->fOrigin.fX),
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000267 SkIntToScalar(-clipDataIn->fOrigin.fY) };
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000268 if (elements.isEmpty() ||
commit-bot@chromium.org2472b322014-03-19 21:26:35 +0000269 (requiresAA && this->installClipEffects(elements, are, clipToRTOffset, devBounds))) {
commit-bot@chromium.org6516d4b2014-02-07 14:04:48 +0000270 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
271 scissorSpaceIBounds.offset(-clipDataIn->fOrigin);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000272 if (NULL == devBounds ||
273 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
274 fGpu->enableScissor(scissorSpaceIBounds);
275 } else {
276 fGpu->disableScissor();
277 }
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000278 this->setGpuStencil();
279 return true;
280 }
281 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000282
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000283#if GR_AA_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000284 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000285 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000286 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000287
288 if (this->useSWOnlyPath(elements)) {
289 // The clip geometry is complex enough that it will be more efficient to create it
290 // entirely in software
291 result = this->createSoftwareClipMask(genID,
292 initialState,
293 elements,
294 clipSpaceIBounds);
295 } else {
296 result = this->createAlphaClipMask(genID,
297 initialState,
298 elements,
299 clipSpaceIBounds);
300 }
301
302 if (NULL != result) {
303 // The mask's top left coord should be pinned to the rounded-out top left corner of
304 // clipSpace bounds. We determine the mask's position WRT to the render target here.
305 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
306 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000307 are->set(fGpu->drawState());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000308 setup_drawstate_aaclip(fGpu, result, rtSpaceMaskBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000309 fGpu->disableScissor();
310 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000311 return true;
312 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000313 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000314 }
315#endif // GR_AA_CLIP
316
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000317 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
318 // be created. In either case, free up the texture in the anti-aliased mask cache.
319 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
320 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
321 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000322 fAACache.reset();
323
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000324 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000325 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000326 this->createStencilClipMask(genID,
327 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000328 elements,
329 clipSpaceIBounds,
330 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000331
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000332 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
333 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
334 // use both stencil and scissor test to the bounds for the final draw.
335 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
336 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
337 fGpu->enableScissor(scissorSpaceIBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000338 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000339 return true;
340}
341
342#define VISUALIZE_COMPLEX_CLIP 0
343
344#if VISUALIZE_COMPLEX_CLIP
tfarina@chromium.org223137f2012-11-21 22:38:36 +0000345 #include "SkRandom.h"
346 SkRandom gRandom;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000347 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
348#else
349 #define SET_RANDOM_COLOR
350#endif
351
352namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000353
354////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000355// set up the OpenGL blend function to perform the specified
356// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000357void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000358
359 switch (op) {
360 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000361 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000362 break;
363 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000364 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000365 break;
366 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000367 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000368 break;
369 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000370 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000371 break;
372 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000373 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000374 break;
375 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000376 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000377 break;
378 default:
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000379 SkASSERT(false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000380 break;
381 }
382}
383
robertphillips@google.com72176b22012-05-23 13:19:12 +0000384}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000385
386////////////////////////////////////////////////////////////////////////////////
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000387bool GrClipMaskManager::drawElement(GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000388 const SkClipStack::Element* element,
389 GrPathRenderer* pr) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000390 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000391
392 drawState->setRenderTarget(target->asRenderTarget());
393
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000394 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000395 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000396 case Element::kEmpty_Type:
397 SkDEBUGFAIL("Should never get here with an empty element.");
398 break;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000399 case Element::kRect_Type:
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000400 // TODO: Do rects directly to the accumulator using a aa-rect GrEffect that covers the
401 // entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000402 if (element->isAA()) {
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000403 getContext()->getAARectRenderer()->fillAARect(fGpu,
404 fGpu,
405 element->getRect(),
robertphillips@google.comb19cb7f2013-05-02 15:37:20 +0000406 SkMatrix::I(),
robertphillips@google.comafd1cba2013-05-14 19:47:47 +0000407 element->getRect(),
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000408 false);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000409 } else {
410 fGpu->drawSimpleRect(element->getRect(), NULL);
411 }
412 return true;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000413 default: {
414 SkPath path;
415 element->asPath(&path);
416 if (path.isInverseFillType()) {
417 path.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000418 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000419 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000420 if (NULL == pr) {
421 GrPathRendererChain::DrawType type;
422 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
423 GrPathRendererChain::kColor_DrawType;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000424 pr = this->getContext()->getPathRenderer(path, stroke, fGpu, false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000425 }
426 if (NULL == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000427 return false;
428 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000429 pr->drawPath(path, stroke, fGpu, element->isAA());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000430 break;
431 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000432 }
433 return true;
434}
435
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000436bool GrClipMaskManager::canStencilAndDrawElement(GrTexture* target,
437 const SkClipStack::Element* element,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000438 GrPathRenderer** pr) {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000439 GrDrawState* drawState = fGpu->drawState();
440 drawState->setRenderTarget(target->asRenderTarget());
441
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000442 if (Element::kRect_Type == element->getType()) {
443 return true;
444 } else {
445 // We shouldn't get here with an empty clip element.
446 SkASSERT(Element::kEmpty_Type != element->getType());
447 SkPath path;
448 element->asPath(&path);
449 if (path.isInverseFillType()) {
450 path.toggleInverseFillType();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000451 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000452 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
453 GrPathRendererChain::DrawType type = element->isAA() ?
454 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
455 GrPathRendererChain::kStencilAndColor_DrawType;
456 *pr = this->getContext()->getPathRenderer(path, stroke, fGpu, false, type);
457 return NULL != *pr;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000458 }
459}
460
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000461void GrClipMaskManager::mergeMask(GrTexture* dstMask,
462 GrTexture* srcMask,
463 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000464 const SkIRect& dstBound,
465 const SkIRect& srcBound) {
bsalomon@google.com137f1342013-05-29 21:27:53 +0000466 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000467 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000468 SkAssertResult(avmr.setIdentity(drawState));
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000469 GrDrawState::AutoRestoreEffects are(drawState);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000470
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000471 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000472
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000473 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000474
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000475 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000476 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000477
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000478 drawState->addColorEffect(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000479 GrTextureDomainEffect::Create(srcMask,
480 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000481 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
482 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000483 GrTextureParams::kNone_FilterMode))->unref();
reed@google.com44699382013-10-31 17:28:30 +0000484 fGpu->drawSimpleRect(SkRect::Make(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000485}
486
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000487// get a texture to act as a temporary buffer for AA clip boolean operations
488// TODO: given the expense of createTexture we may want to just cache this too
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000489void GrClipMaskManager::getTemp(int width, int height, GrAutoScratchTexture* temp) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000490 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000491 // we've already allocated the temp texture
492 return;
493 }
494
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000495 GrTextureDesc desc;
496 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000497 desc.fWidth = width;
498 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000499 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000500
robertphillips@google.com2c756812012-05-22 20:28:23 +0000501 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000502}
503
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000504////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000505// Handles caching & allocation (if needed) of a clip alpha-mask texture for both the sw-upload
506// or gpu-rendered cases. Returns true if there is no more work to be done (i.e., we got a cache
507// hit)
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000508bool GrClipMaskManager::getMaskTexture(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000509 const SkIRect& clipSpaceIBounds,
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000510 GrTexture** result,
511 bool willUpload) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000512 bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000513 if (!cached) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000514
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000515 // There isn't a suitable entry in the cache so we create a new texture to store the mask.
516 // Since we are setting up the cache we know the last lookup was a miss. Free up the
517 // currently cached mask so it can be reused.
518 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000519
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000520 GrTextureDesc desc;
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000521 desc.fFlags = willUpload ? kNone_GrTextureFlags : kRenderTarget_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000522 desc.fWidth = clipSpaceIBounds.width();
523 desc.fHeight = clipSpaceIBounds.height();
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000524 desc.fConfig = kRGBA_8888_GrPixelConfig;
robertphillips@google.com94bdd7e2013-10-31 15:50:43 +0000525 if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000526 // We would always like A8 but it isn't supported on all platforms
527 desc.fConfig = kAlpha_8_GrPixelConfig;
528 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000529
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000530 fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000531 }
532
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000533 *result = fAACache.getLastMask();
534 return cached;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000535}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000536
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000537////////////////////////////////////////////////////////////////////////////////
538// Create a 8-bit clip mask in alpha
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000539GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000540 InitialState initialState,
541 const ElementList& elements,
542 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000543 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000544
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000545 GrTexture* result;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000546 if (this->getMaskTexture(elementsGenID, clipSpaceIBounds, &result, false)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000547 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000548 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000549 }
550
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000551 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000552 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000553 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000554 }
555
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000556 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000557 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000558 SkIntToScalar(-clipSpaceIBounds.fLeft),
559 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000560 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000561 // The texture may be larger than necessary, this rect represents the part of the texture
562 // we populate with a rasterization of the clip.
563 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
564
bsalomon@google.com137f1342013-05-29 21:27:53 +0000565 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
566 SkMatrix translate;
567 translate.setTranslate(clipToMaskOffset);
568 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &translate);
569
570 GrDrawState* drawState = fGpu->drawState();
571
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000572 // We're drawing a coverage mask and want coverage to be run through the blend function.
573 drawState->enableState(GrDrawState::kCoverageDrawing_StateBit);
574
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000575 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
576 // clear the part that we care about.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000577 fGpu->clear(&maskSpaceIBounds,
578 kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000579 true,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000580 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000581
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000582 // When we use the stencil in the below loop it is important to have this clip installed.
583 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
584 // pass must not set values outside of this bounds or stencil values outside the rect won't be
585 // cleared.
586 GrDrawTarget::AutoClipRestore acr(fGpu, maskSpaceIBounds);
587 drawState->enableState(GrDrawState::kClip_StateBit);
588
robertphillips@google.comf105b102012-05-14 12:18:26 +0000589 GrAutoScratchTexture temp;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000590 // walk through each clip element and perform its set op
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000591 for (ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000592 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000593 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000594 bool invert = element->isInverseFilled();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000595
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000596 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000597 GrPathRenderer* pr = NULL;
598 bool useTemp = !this->canStencilAndDrawElement(result, element, &pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000599 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000600 // 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 +0000601 // mask buffer can be substantially larger than the actually clip stack element. We
602 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000603 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000604 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000605
606 if (useTemp) {
607 if (invert) {
608 maskSpaceElementIBounds = maskSpaceIBounds;
609 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000610 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000611 elementBounds.offset(clipToMaskOffset);
612 elementBounds.roundOut(&maskSpaceElementIBounds);
613 }
614
615 this->getTemp(maskSpaceIBounds.fRight, maskSpaceIBounds.fBottom, &temp);
616 if (NULL == temp.texture()) {
617 fAACache.reset();
618 return NULL;
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000619 }
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000620 dst = temp.texture();
621 // clear the temp target and set blend to replace
622 fGpu->clear(&maskSpaceElementIBounds,
623 invert ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000624 true,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000625 dst->asRenderTarget());
626 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000627
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000628 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000629 // draw directly into the result with the stencil set to make the pixels affected
630 // by the clip shape be non-zero.
631 dst = result;
632 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
633 kReplace_StencilOp,
634 kReplace_StencilOp,
635 kAlways_StencilFunc,
636 0xffff,
637 0xffff,
638 0xffff);
639 drawState->setStencil(kStencilInElement);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000640 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000641 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000642
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000643 drawState->setAlpha(invert ? 0x00 : 0xff);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000644
robertphillips@google.come79f3202014-02-11 16:30:21 +0000645 if (!this->drawElement(dst, element, pr)) {
646 fAACache.reset();
647 return NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000648 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000649
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000650 if (useTemp) {
651 // Now draw into the accumulator using the real operation and the temp buffer as a
652 // texture
653 this->mergeMask(result,
654 temp.texture(),
655 op,
656 maskSpaceIBounds,
657 maskSpaceElementIBounds);
658 } else {
659 // Draw to the exterior pixels (those with a zero stencil value).
660 drawState->setAlpha(invert ? 0xff : 0x00);
661 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
662 kZero_StencilOp,
663 kZero_StencilOp,
664 kEqual_StencilFunc,
665 0xffff,
666 0x0000,
667 0xffff);
668 drawState->setStencil(kDrawOutsideElement);
669 fGpu->drawSimpleRect(clipSpaceIBounds);
670 drawState->disableStencil();
671 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000672 } else {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000673 // all the remaining ops can just be directly draw into the accumulation buffer
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000674 drawState->setAlpha(0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000675 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000676 this->drawElement(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000677 }
678 }
679
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000680 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000681 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000682}
683
684////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000685// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000686// (as opposed to canvas) coordinates
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000687bool GrClipMaskManager::createStencilClipMask(int32_t elementsGenID,
688 InitialState initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000689 const ElementList& elements,
690 const SkIRect& clipSpaceIBounds,
691 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000692
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000693 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000694
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000695 GrDrawState* drawState = fGpu->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000696 SkASSERT(drawState->isClipState());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000697
698 GrRenderTarget* rt = drawState->getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000699 SkASSERT(NULL != rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000700
701 // TODO: dynamically attach a SB when needed.
702 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
703 if (NULL == stencilBuffer) {
704 return false;
705 }
706
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000707 if (stencilBuffer->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000708
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000709 stencilBuffer->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000710
bsalomon@google.com137f1342013-05-29 21:27:53 +0000711 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
712 SkVector translate = {
713 SkIntToScalar(clipSpaceToStencilOffset.fX),
714 SkIntToScalar(clipSpaceToStencilOffset.fY)
715 };
716 SkMatrix matrix;
717 matrix.setTranslate(translate);
718 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &matrix);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000719 drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000720
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000721 drawState->setRenderTarget(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000722
bsalomon@google.com9f131742012-12-13 20:43:56 +0000723 // We set the current clip to the bounds so that our recursive draws are scissored to them.
724 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
725 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
726 GrDrawTarget::AutoClipRestore acr(fGpu, stencilSpaceIBounds);
727 drawState->enableState(GrDrawState::kClip_StateBit);
728
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000729#if !VISUALIZE_COMPLEX_CLIP
730 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
731#endif
732
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000733 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000734 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000735 clipBit = (1 << (clipBit-1));
736
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000737 fGpu->clearStencilClip(stencilSpaceIBounds, kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000738
739 // walk through each clip element and perform its set op
740 // with the existing clip.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000741 for (ElementList::Iter iter(elements.headIter()); NULL != iter.get(); iter.next()) {
742 const Element* element = iter.get();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000743 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000744 // enabled at bottom of loop
745 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000746 // if the target is MSAA then we want MSAA enabled when the clip is soft
747 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000748 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000749 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000750
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000751 // This will be used to determine whether the clip shape can be rendered into the
752 // stencil with arbitrary stencil settings.
753 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000754
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000755 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000756
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000757 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000758
robertphillips@google.come79f3202014-02-11 16:30:21 +0000759 GrPathRenderer* pr = NULL;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000760 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000761 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000762 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000763 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000764 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000765 element->asPath(&clipPath);
766 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000767 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000768 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000769 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000770 pr = this->getContext()->getPathRenderer(clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000771 stroke,
772 fGpu,
773 false,
774 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000775 &stencilSupport);
776 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000777 return false;
778 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000779 }
780
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000781 int passes;
782 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
783
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000784 bool canRenderDirectToStencil =
785 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000786 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000787 // fill rule, and set operation can
788 // we render the element directly to
789 // stencil bit used for clipping.
790 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
791 canRenderDirectToStencil,
792 clipBit,
793 fillInverted,
794 &passes,
795 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000796
797 // draw the element to the client stencil bits if necessary
798 if (!canDrawDirectToClip) {
799 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000800 kIncClamp_StencilOp,
801 kIncClamp_StencilOp,
802 kAlways_StencilFunc,
803 0xffff,
804 0x0000,
805 0xffff);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000806 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000807 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000808 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000809 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000810 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000811 if (!clipPath.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000812 if (canRenderDirectToStencil) {
813 *drawState->stencil() = gDrawToStencil;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000814 pr->drawPath(clipPath, stroke, fGpu, false);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000815 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000816 pr->stencilPath(clipPath, stroke, fGpu);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000817 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000818 }
819 }
820 }
821
822 // now we modify the clip bit by rendering either the clip
823 // element directly or a bounding rect of the entire clip.
824 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
825 for (int p = 0; p < passes; ++p) {
826 *drawState->stencil() = stencilSettings[p];
827 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000828 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000829 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000830 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000831 } else {
832 SET_RANDOM_COLOR
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000833 pr->drawPath(clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000834 }
835 } else {
836 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000837 // The view matrix is setup to do clip space -> stencil space translation, so
838 // draw rect in clip space.
reed@google.com44699382013-10-31 17:28:30 +0000839 fGpu->drawSimpleRect(SkRect::Make(clipSpaceIBounds), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000840 }
841 }
842 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000843 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000844 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000845 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000846 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000847 return true;
848}
849
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000850
bsalomon@google.com411dad02012-06-05 20:24:20 +0000851// mapping of clip-respecting stencil funcs to normal stencil funcs
852// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000853static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000854 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
855 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
856 // In the Clip Funcs
857 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
858 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
859 kLess_StencilFunc, // kLessIfInClip_StencilFunc
860 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
861 // Special in the clip func that forces user's ref to be 0.
862 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
863 // make ref 0 and do normal nequal.
864 },
865 {// Stencil-Clipping is ENABLED
866 // In the Clip Funcs
867 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
868 // eq stencil clip bit, mask
869 // out user bits.
870
871 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
872 // add stencil bit to mask and ref
873
874 kLess_StencilFunc, // kLessIfInClip_StencilFunc
875 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
876 // for both of these we can add
877 // the clip bit to the mask and
878 // ref and compare as normal
879 // Special in the clip func that forces user's ref to be 0.
880 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
881 // make ref have only the clip bit set
882 // and make comparison be less
883 // 10..0 < 1..user_bits..
884 }
885};
886
bsalomon@google.coma3201942012-06-21 19:58:20 +0000887namespace {
888// Sets the settings to clip against the stencil buffer clip while ignoring the
889// client bits.
890const GrStencilSettings& basic_apply_stencil_clip_settings() {
891 // stencil settings to use when clip is in stencil
892 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
893 kKeep_StencilOp,
894 kKeep_StencilOp,
895 kAlwaysIfInClip_StencilFunc,
896 0x0000,
897 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000898 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000899 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
900}
901}
902
903void GrClipMaskManager::setGpuStencil() {
904 // We make two copies of the StencilSettings here (except in the early
905 // exit scenario. One copy from draw state to the stack var. Then another
906 // from the stack var to the gpu. We could make this class hold a ptr to
907 // GrGpu's fStencilSettings and eliminate the stack copy here.
908
909 const GrDrawState& drawState = fGpu->getDrawState();
910
911 // use stencil for clipping if clipping is enabled and the clip
912 // has been written into the stencil.
913 GrClipMaskManager::StencilClipMode clipMode;
914 if (this->isClipInStencil() && drawState.isClipState()) {
915 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
916 // We can't be modifying the clip and respecting it at the same time.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000917 SkASSERT(!drawState.isStateFlagEnabled(
bsalomon@google.coma3201942012-06-21 19:58:20 +0000918 GrGpu::kModifyStencilClip_StateBit));
919 } else if (drawState.isStateFlagEnabled(
920 GrGpu::kModifyStencilClip_StateBit)) {
921 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
922 } else {
923 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
924 }
925
926 GrStencilSettings settings;
927 // The GrGpu client may not be using the stencil buffer but we may need to
928 // enable it in order to respect a stencil clip.
929 if (drawState.getStencil().isDisabled()) {
930 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
931 settings = basic_apply_stencil_clip_settings();
932 } else {
933 fGpu->disableStencil();
934 return;
935 }
936 } else {
937 settings = drawState.getStencil();
938 }
939
940 // TODO: dynamically attach a stencil buffer
941 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000942 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000943 drawState.getRenderTarget()->getStencilBuffer();
944 if (NULL != stencilBuffer) {
945 stencilBits = stencilBuffer->bits();
946 }
947
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000948 SkASSERT(fGpu->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
949 SkASSERT(fGpu->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000950 this->adjustStencilParams(&settings, clipMode, stencilBits);
951 fGpu->setStencilSettings(settings);
952}
953
954void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
955 StencilClipMode mode,
956 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000957 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000958
959 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000960 // We assume that this clip manager itself is drawing to the GrGpu and
961 // has already setup the correct values.
962 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000963 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000964
bsalomon@google.com411dad02012-06-05 20:24:20 +0000965 unsigned int clipBit = (1 << (stencilBitCnt - 1));
966 unsigned int userBits = clipBit - 1;
967
bsalomon@google.coma3201942012-06-21 19:58:20 +0000968 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000969 bool twoSided = fGpu->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000970
bsalomon@google.coma3201942012-06-21 19:58:20 +0000971 bool finished = false;
972 while (!finished) {
973 GrStencilFunc func = settings->func(face);
974 uint16_t writeMask = settings->writeMask(face);
975 uint16_t funcMask = settings->funcMask(face);
976 uint16_t funcRef = settings->funcRef(face);
977
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000978 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000979
980 writeMask &= userBits;
981
982 if (func >= kBasicStencilFuncCount) {
983 int respectClip = kRespectClip_StencilClipMode == mode;
984 if (respectClip) {
985 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000986 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000987 switch (func) {
988 case kAlwaysIfInClip_StencilFunc:
989 funcMask = clipBit;
990 funcRef = clipBit;
991 break;
992 case kEqualIfInClip_StencilFunc:
993 case kLessIfInClip_StencilFunc:
994 case kLEqualIfInClip_StencilFunc:
995 funcMask = (funcMask & userBits) | clipBit;
996 funcRef = (funcRef & userBits) | clipBit;
997 break;
998 case kNonZeroIfInClip_StencilFunc:
999 funcMask = (funcMask & userBits) | clipBit;
1000 funcRef = clipBit;
1001 break;
1002 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001003 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +00001004 }
1005 } else {
1006 funcMask &= userBits;
1007 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001008 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001009 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001010 gSpecialToBasicStencilFunc[respectClip];
1011 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001012 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001013 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001014 funcMask &= userBits;
1015 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001016 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001017
1018 settings->setFunc(face, func);
1019 settings->setWriteMask(face, writeMask);
1020 settings->setFuncMask(face, funcMask);
1021 settings->setFuncRef(face, funcRef);
1022
1023 if (GrStencilSettings::kFront_Face == face) {
1024 face = GrStencilSettings::kBack_Face;
1025 finished = !twoSided;
1026 } else {
1027 finished = true;
1028 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001029 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001030 if (!twoSided) {
1031 settings->copyFrontSettingsToBack();
1032 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001033}
1034
1035////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +00001036GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001037 GrReducedClip::InitialState initialState,
1038 const GrReducedClip::ElementList& elements,
1039 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001040 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001041
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001042 GrTexture* result;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +00001043 if (this->getMaskTexture(elementsGenID, clipSpaceIBounds, &result, true)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001044 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001045 }
1046
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001047 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001048 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001049 return NULL;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001050 }
1051
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001052 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1053 // the top left corner of the resulting rect to the top left of the texture.
1054 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1055
robertphillips@google.com2c756812012-05-22 20:28:23 +00001056 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001057
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001058 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001059 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
1060 SkIntToScalar(-clipSpaceIBounds.fTop));
1061 helper.init(maskSpaceIBounds, &matrix);
1062
1063 helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001064
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001065 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001066
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001067 for (ElementList::Iter iter(elements.headIter()) ; NULL != iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001068
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001069 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001070 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001071
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001072 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1073 // Intersect and reverse difference require modifying pixels outside of the geometry
1074 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1075 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1076 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001077 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001078 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001079 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001080 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001081 }
1082
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001083 SkPath clipPath;
1084 element->asPath(&clipPath);
1085 clipPath.toggleInverseFillType();
1086 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001087
1088 continue;
1089 }
1090
1091 // The other ops (union, xor, diff) only affect pixels inside
1092 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001093 if (Element::kRect_Type == element->getType()) {
1094 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1095 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001096 SkPath path;
1097 element->asPath(&path);
1098 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001099 }
1100 }
1101
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001102 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001103
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001104 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001105 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001106}
1107
robertphillips@google.comf294b772012-04-27 14:29:26 +00001108////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001109void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001110 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001111}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001112
1113void GrClipMaskManager::setGpu(GrGpu* gpu) {
1114 fGpu = gpu;
1115 fAACache.setContext(gpu->getContext());
1116}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001117
1118void GrClipMaskManager::adjustPathStencilParams(GrStencilSettings* settings) {
1119 const GrDrawState& drawState = fGpu->getDrawState();
1120 GrClipMaskManager::StencilClipMode clipMode;
1121 if (this->isClipInStencil() && drawState.isClipState()) {
1122 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
1123 // We can't be modifying the clip and respecting it at the same time.
1124 SkASSERT(!drawState.isStateFlagEnabled(
1125 GrGpu::kModifyStencilClip_StateBit));
1126 } else if (drawState.isStateFlagEnabled(
1127 GrGpu::kModifyStencilClip_StateBit)) {
1128 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
1129 } else {
1130 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
1131 }
1132
1133 // TODO: dynamically attach a stencil buffer
1134 int stencilBits = 0;
1135 GrStencilBuffer* stencilBuffer =
1136 drawState.getRenderTarget()->getStencilBuffer();
1137 if (NULL != stencilBuffer) {
1138 stencilBits = stencilBuffer->bits();
1139 this->adjustStencilParams(settings, clipMode, stencilBits);
1140 }
1141}