blob: 96f4b209e385944584f1339d87bc5bdd57e6b82b [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;
223 bool isRect = false;
224
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000225 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000226
227 const GrRenderTarget* rt = drawState->getRenderTarget();
228 // GrDrawTarget should have filtered this for us
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000229 SkASSERT(NULL != rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000230
231 bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen();
232
233 if (!ignoreClip) {
234 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
235 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
236 ReduceClipStack(*clipDataIn->fClipStack,
237 clipSpaceRTIBounds,
238 &elements,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000239 &genID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000240 &initialState,
241 &clipSpaceIBounds,
242 &requiresAA);
243 if (elements.isEmpty()) {
244 if (kAllIn_InitialState == initialState) {
245 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
246 isRect = true;
247 } else {
248 return false;
249 }
250 }
251 }
252
253 if (ignoreClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000254 fGpu->disableScissor();
255 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000256 return true;
257 }
258
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000259 // An element count of 4 was chosen because of the common pattern in Blink of:
260 // isect RR
261 // diff RR
262 // isect convex_poly
263 // isect convex_poly
264 // when drawing rounded div borders. This could probably be tuned based on a
265 // configuration's relative costs of switching RTs to generate a mask vs
266 // longer shaders.
267 if (elements.count() <= 4) {
268 SkVector clipToRTOffset = { SkIntToScalar(-clipDataIn->fOrigin.fX),
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000269 SkIntToScalar(-clipDataIn->fOrigin.fY) };
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000270 if (elements.isEmpty() ||
271 this->installClipEffects(elements, are, clipToRTOffset, devBounds)) {
commit-bot@chromium.org6516d4b2014-02-07 14:04:48 +0000272 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
273 scissorSpaceIBounds.offset(-clipDataIn->fOrigin);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000274 if (NULL == devBounds ||
275 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
276 fGpu->enableScissor(scissorSpaceIBounds);
277 } else {
278 fGpu->disableScissor();
279 }
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000280 this->setGpuStencil();
281 return true;
282 }
283 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000284
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000285#if GR_AA_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000286 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000287 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000288 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000289
290 if (this->useSWOnlyPath(elements)) {
291 // The clip geometry is complex enough that it will be more efficient to create it
292 // entirely in software
293 result = this->createSoftwareClipMask(genID,
294 initialState,
295 elements,
296 clipSpaceIBounds);
297 } else {
298 result = this->createAlphaClipMask(genID,
299 initialState,
300 elements,
301 clipSpaceIBounds);
302 }
303
304 if (NULL != result) {
305 // The mask's top left coord should be pinned to the rounded-out top left corner of
306 // clipSpace bounds. We determine the mask's position WRT to the render target here.
307 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
308 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000309 are->set(fGpu->drawState());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000310 setup_drawstate_aaclip(fGpu, result, rtSpaceMaskBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000311 fGpu->disableScissor();
312 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000313 return true;
314 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000315 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000316 }
317#endif // GR_AA_CLIP
318
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000319 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
320 // be created. In either case, free up the texture in the anti-aliased mask cache.
321 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
322 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
323 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000324 fAACache.reset();
325
bsalomon@google.coma3201942012-06-21 19:58:20 +0000326 // If the clip is a rectangle then just set the scissor. Otherwise, create
327 // a stencil mask.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000328 if (isRect) {
329 SkIRect clipRect = clipSpaceIBounds;
330 clipRect.offset(-clipDataIn->fOrigin);
331 fGpu->enableScissor(clipRect);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000332 this->setGpuStencil();
333 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000334 }
335
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000336 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000337 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000338 this->createStencilClipMask(genID,
339 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000340 elements,
341 clipSpaceIBounds,
342 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000343
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000344 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
345 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
346 // use both stencil and scissor test to the bounds for the final draw.
347 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
348 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
349 fGpu->enableScissor(scissorSpaceIBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000350 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000351 return true;
352}
353
354#define VISUALIZE_COMPLEX_CLIP 0
355
356#if VISUALIZE_COMPLEX_CLIP
tfarina@chromium.org223137f2012-11-21 22:38:36 +0000357 #include "SkRandom.h"
358 SkRandom gRandom;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000359 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
360#else
361 #define SET_RANDOM_COLOR
362#endif
363
364namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000365
366////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000367// set up the OpenGL blend function to perform the specified
368// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000369void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000370
371 switch (op) {
372 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000373 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000374 break;
375 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000376 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000377 break;
378 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000379 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000380 break;
381 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000382 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000383 break;
384 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000385 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000386 break;
387 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000388 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000389 break;
390 default:
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000391 SkASSERT(false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000392 break;
393 }
394}
395
robertphillips@google.com72176b22012-05-23 13:19:12 +0000396}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000397
398////////////////////////////////////////////////////////////////////////////////
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000399bool GrClipMaskManager::drawElement(GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000400 const SkClipStack::Element* element,
401 GrPathRenderer* pr) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000402 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000403
404 drawState->setRenderTarget(target->asRenderTarget());
405
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000406 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000407 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000408 case Element::kEmpty_Type:
409 SkDEBUGFAIL("Should never get here with an empty element.");
410 break;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000411 case Element::kRect_Type:
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000412 // TODO: Do rects directly to the accumulator using a aa-rect GrEffect that covers the
413 // entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000414 if (element->isAA()) {
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000415 getContext()->getAARectRenderer()->fillAARect(fGpu,
416 fGpu,
417 element->getRect(),
robertphillips@google.comb19cb7f2013-05-02 15:37:20 +0000418 SkMatrix::I(),
robertphillips@google.comafd1cba2013-05-14 19:47:47 +0000419 element->getRect(),
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000420 false);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000421 } else {
422 fGpu->drawSimpleRect(element->getRect(), NULL);
423 }
424 return true;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000425 default: {
426 SkPath path;
427 element->asPath(&path);
428 if (path.isInverseFillType()) {
429 path.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000430 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000431 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000432 if (NULL == pr) {
433 GrPathRendererChain::DrawType type;
434 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
435 GrPathRendererChain::kColor_DrawType;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000436 pr = this->getContext()->getPathRenderer(path, stroke, fGpu, false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000437 }
438 if (NULL == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000439 return false;
440 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000441 pr->drawPath(path, stroke, fGpu, element->isAA());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000442 break;
443 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000444 }
445 return true;
446}
447
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000448bool GrClipMaskManager::canStencilAndDrawElement(GrTexture* target,
449 const SkClipStack::Element* element,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000450 GrPathRenderer** pr) {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000451 GrDrawState* drawState = fGpu->drawState();
452 drawState->setRenderTarget(target->asRenderTarget());
453
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000454 if (Element::kRect_Type == element->getType()) {
455 return true;
456 } else {
457 // We shouldn't get here with an empty clip element.
458 SkASSERT(Element::kEmpty_Type != element->getType());
459 SkPath path;
460 element->asPath(&path);
461 if (path.isInverseFillType()) {
462 path.toggleInverseFillType();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000463 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000464 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
465 GrPathRendererChain::DrawType type = element->isAA() ?
466 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
467 GrPathRendererChain::kStencilAndColor_DrawType;
468 *pr = this->getContext()->getPathRenderer(path, stroke, fGpu, false, type);
469 return NULL != *pr;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000470 }
471}
472
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000473void GrClipMaskManager::mergeMask(GrTexture* dstMask,
474 GrTexture* srcMask,
475 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000476 const SkIRect& dstBound,
477 const SkIRect& srcBound) {
bsalomon@google.com137f1342013-05-29 21:27:53 +0000478 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000479 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000480 SkAssertResult(avmr.setIdentity(drawState));
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000481 GrDrawState::AutoRestoreEffects are(drawState);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000482
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000483 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000484
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000485 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000486
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000487 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000488 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000489
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000490 drawState->addColorEffect(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000491 GrTextureDomainEffect::Create(srcMask,
492 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000493 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
494 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000495 GrTextureParams::kNone_FilterMode))->unref();
reed@google.com44699382013-10-31 17:28:30 +0000496 fGpu->drawSimpleRect(SkRect::Make(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000497}
498
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000499// get a texture to act as a temporary buffer for AA clip boolean operations
500// TODO: given the expense of createTexture we may want to just cache this too
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000501void GrClipMaskManager::getTemp(int width, int height, GrAutoScratchTexture* temp) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000502 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000503 // we've already allocated the temp texture
504 return;
505 }
506
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000507 GrTextureDesc desc;
508 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000509 desc.fWidth = width;
510 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000511 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000512
robertphillips@google.com2c756812012-05-22 20:28:23 +0000513 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000514}
515
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000516////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000517// Handles caching & allocation (if needed) of a clip alpha-mask texture for both the sw-upload
518// or gpu-rendered cases. Returns true if there is no more work to be done (i.e., we got a cache
519// hit)
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000520bool GrClipMaskManager::getMaskTexture(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000521 const SkIRect& clipSpaceIBounds,
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000522 GrTexture** result,
523 bool willUpload) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000524 bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000525 if (!cached) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000526
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000527 // There isn't a suitable entry in the cache so we create a new texture to store the mask.
528 // Since we are setting up the cache we know the last lookup was a miss. Free up the
529 // currently cached mask so it can be reused.
530 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000531
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000532 GrTextureDesc desc;
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000533 desc.fFlags = willUpload ? kNone_GrTextureFlags : kRenderTarget_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000534 desc.fWidth = clipSpaceIBounds.width();
535 desc.fHeight = clipSpaceIBounds.height();
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000536 desc.fConfig = kRGBA_8888_GrPixelConfig;
robertphillips@google.com94bdd7e2013-10-31 15:50:43 +0000537 if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000538 // We would always like A8 but it isn't supported on all platforms
539 desc.fConfig = kAlpha_8_GrPixelConfig;
540 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000541
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000542 fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000543 }
544
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000545 *result = fAACache.getLastMask();
546 return cached;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000547}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000548
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000549////////////////////////////////////////////////////////////////////////////////
550// Create a 8-bit clip mask in alpha
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000551GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000552 InitialState initialState,
553 const ElementList& elements,
554 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000555 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000556
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000557 GrTexture* result;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000558 if (this->getMaskTexture(elementsGenID, clipSpaceIBounds, &result, false)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000559 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000560 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000561 }
562
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000563 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000564 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000565 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000566 }
567
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000568 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000569 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000570 SkIntToScalar(-clipSpaceIBounds.fLeft),
571 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000572 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000573 // The texture may be larger than necessary, this rect represents the part of the texture
574 // we populate with a rasterization of the clip.
575 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
576
bsalomon@google.com137f1342013-05-29 21:27:53 +0000577 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
578 SkMatrix translate;
579 translate.setTranslate(clipToMaskOffset);
580 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &translate);
581
582 GrDrawState* drawState = fGpu->drawState();
583
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000584 // We're drawing a coverage mask and want coverage to be run through the blend function.
585 drawState->enableState(GrDrawState::kCoverageDrawing_StateBit);
586
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000587 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
588 // clear the part that we care about.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000589 fGpu->clear(&maskSpaceIBounds,
590 kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000591 true,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000592 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000593
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000594 // When we use the stencil in the below loop it is important to have this clip installed.
595 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
596 // pass must not set values outside of this bounds or stencil values outside the rect won't be
597 // cleared.
598 GrDrawTarget::AutoClipRestore acr(fGpu, maskSpaceIBounds);
599 drawState->enableState(GrDrawState::kClip_StateBit);
600
robertphillips@google.comf105b102012-05-14 12:18:26 +0000601 GrAutoScratchTexture temp;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000602 // walk through each clip element and perform its set op
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000603 for (ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000604 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000605 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000606 bool invert = element->isInverseFilled();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000607
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000608 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000609 GrPathRenderer* pr = NULL;
610 bool useTemp = !this->canStencilAndDrawElement(result, element, &pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000611 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000612 // 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 +0000613 // mask buffer can be substantially larger than the actually clip stack element. We
614 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000615 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000616 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000617
618 if (useTemp) {
619 if (invert) {
620 maskSpaceElementIBounds = maskSpaceIBounds;
621 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000622 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000623 elementBounds.offset(clipToMaskOffset);
624 elementBounds.roundOut(&maskSpaceElementIBounds);
625 }
626
627 this->getTemp(maskSpaceIBounds.fRight, maskSpaceIBounds.fBottom, &temp);
628 if (NULL == temp.texture()) {
629 fAACache.reset();
630 return NULL;
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000631 }
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000632 dst = temp.texture();
633 // clear the temp target and set blend to replace
634 fGpu->clear(&maskSpaceElementIBounds,
635 invert ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000636 true,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000637 dst->asRenderTarget());
638 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000639
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000640 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000641 // draw directly into the result with the stencil set to make the pixels affected
642 // by the clip shape be non-zero.
643 dst = result;
644 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
645 kReplace_StencilOp,
646 kReplace_StencilOp,
647 kAlways_StencilFunc,
648 0xffff,
649 0xffff,
650 0xffff);
651 drawState->setStencil(kStencilInElement);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000652 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000653 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000654
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000655 drawState->setAlpha(invert ? 0x00 : 0xff);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000656
robertphillips@google.come79f3202014-02-11 16:30:21 +0000657 if (!this->drawElement(dst, element, pr)) {
658 fAACache.reset();
659 return NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000660 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000661
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000662 if (useTemp) {
663 // Now draw into the accumulator using the real operation and the temp buffer as a
664 // texture
665 this->mergeMask(result,
666 temp.texture(),
667 op,
668 maskSpaceIBounds,
669 maskSpaceElementIBounds);
670 } else {
671 // Draw to the exterior pixels (those with a zero stencil value).
672 drawState->setAlpha(invert ? 0xff : 0x00);
673 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
674 kZero_StencilOp,
675 kZero_StencilOp,
676 kEqual_StencilFunc,
677 0xffff,
678 0x0000,
679 0xffff);
680 drawState->setStencil(kDrawOutsideElement);
681 fGpu->drawSimpleRect(clipSpaceIBounds);
682 drawState->disableStencil();
683 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000684 } else {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000685 // all the remaining ops can just be directly draw into the accumulation buffer
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000686 drawState->setAlpha(0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000687 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000688 this->drawElement(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000689 }
690 }
691
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000692 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000693 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000694}
695
696////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000697// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000698// (as opposed to canvas) coordinates
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000699bool GrClipMaskManager::createStencilClipMask(int32_t elementsGenID,
700 InitialState initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000701 const ElementList& elements,
702 const SkIRect& clipSpaceIBounds,
703 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000704
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000705 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000706
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000707 GrDrawState* drawState = fGpu->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000708 SkASSERT(drawState->isClipState());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000709
710 GrRenderTarget* rt = drawState->getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000711 SkASSERT(NULL != rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000712
713 // TODO: dynamically attach a SB when needed.
714 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
715 if (NULL == stencilBuffer) {
716 return false;
717 }
718
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000719 if (stencilBuffer->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000720
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000721 stencilBuffer->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000722
bsalomon@google.com137f1342013-05-29 21:27:53 +0000723 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
724 SkVector translate = {
725 SkIntToScalar(clipSpaceToStencilOffset.fX),
726 SkIntToScalar(clipSpaceToStencilOffset.fY)
727 };
728 SkMatrix matrix;
729 matrix.setTranslate(translate);
730 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &matrix);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000731 drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000732
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000733 drawState->setRenderTarget(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000734
bsalomon@google.com9f131742012-12-13 20:43:56 +0000735 // We set the current clip to the bounds so that our recursive draws are scissored to them.
736 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
737 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
738 GrDrawTarget::AutoClipRestore acr(fGpu, stencilSpaceIBounds);
739 drawState->enableState(GrDrawState::kClip_StateBit);
740
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000741#if !VISUALIZE_COMPLEX_CLIP
742 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
743#endif
744
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000745 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000746 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000747 clipBit = (1 << (clipBit-1));
748
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000749 fGpu->clearStencilClip(stencilSpaceIBounds, kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000750
751 // walk through each clip element and perform its set op
752 // with the existing clip.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000753 for (ElementList::Iter iter(elements.headIter()); NULL != iter.get(); iter.next()) {
754 const Element* element = iter.get();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000755 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000756 // enabled at bottom of loop
757 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000758 // if the target is MSAA then we want MSAA enabled when the clip is soft
759 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000760 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000761 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000762
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000763 // This will be used to determine whether the clip shape can be rendered into the
764 // stencil with arbitrary stencil settings.
765 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000766
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000767 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000768
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000769 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000770
robertphillips@google.come79f3202014-02-11 16:30:21 +0000771 GrPathRenderer* pr = NULL;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000772 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000773 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000774 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000775 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000776 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000777 element->asPath(&clipPath);
778 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000779 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000780 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000781 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000782 pr = this->getContext()->getPathRenderer(clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000783 stroke,
784 fGpu,
785 false,
786 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000787 &stencilSupport);
788 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000789 return false;
790 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000791 }
792
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000793 int passes;
794 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
795
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000796 bool canRenderDirectToStencil =
797 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000798 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000799 // fill rule, and set operation can
800 // we render the element directly to
801 // stencil bit used for clipping.
802 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
803 canRenderDirectToStencil,
804 clipBit,
805 fillInverted,
806 &passes,
807 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000808
809 // draw the element to the client stencil bits if necessary
810 if (!canDrawDirectToClip) {
811 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000812 kIncClamp_StencilOp,
813 kIncClamp_StencilOp,
814 kAlways_StencilFunc,
815 0xffff,
816 0x0000,
817 0xffff);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000818 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000819 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000820 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000821 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000822 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000823 if (!clipPath.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000824 if (canRenderDirectToStencil) {
825 *drawState->stencil() = gDrawToStencil;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000826 pr->drawPath(clipPath, stroke, fGpu, false);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000827 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000828 pr->stencilPath(clipPath, stroke, fGpu);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000829 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000830 }
831 }
832 }
833
834 // now we modify the clip bit by rendering either the clip
835 // element directly or a bounding rect of the entire clip.
836 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
837 for (int p = 0; p < passes; ++p) {
838 *drawState->stencil() = stencilSettings[p];
839 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000840 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000841 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000842 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000843 } else {
844 SET_RANDOM_COLOR
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000845 pr->drawPath(clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000846 }
847 } else {
848 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000849 // The view matrix is setup to do clip space -> stencil space translation, so
850 // draw rect in clip space.
reed@google.com44699382013-10-31 17:28:30 +0000851 fGpu->drawSimpleRect(SkRect::Make(clipSpaceIBounds), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000852 }
853 }
854 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000855 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000856 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000857 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000858 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000859 return true;
860}
861
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000862
bsalomon@google.com411dad02012-06-05 20:24:20 +0000863// mapping of clip-respecting stencil funcs to normal stencil funcs
864// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000865static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000866 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
867 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
868 // In the Clip Funcs
869 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
870 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
871 kLess_StencilFunc, // kLessIfInClip_StencilFunc
872 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
873 // Special in the clip func that forces user's ref to be 0.
874 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
875 // make ref 0 and do normal nequal.
876 },
877 {// Stencil-Clipping is ENABLED
878 // In the Clip Funcs
879 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
880 // eq stencil clip bit, mask
881 // out user bits.
882
883 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
884 // add stencil bit to mask and ref
885
886 kLess_StencilFunc, // kLessIfInClip_StencilFunc
887 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
888 // for both of these we can add
889 // the clip bit to the mask and
890 // ref and compare as normal
891 // Special in the clip func that forces user's ref to be 0.
892 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
893 // make ref have only the clip bit set
894 // and make comparison be less
895 // 10..0 < 1..user_bits..
896 }
897};
898
bsalomon@google.coma3201942012-06-21 19:58:20 +0000899namespace {
900// Sets the settings to clip against the stencil buffer clip while ignoring the
901// client bits.
902const GrStencilSettings& basic_apply_stencil_clip_settings() {
903 // stencil settings to use when clip is in stencil
904 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
905 kKeep_StencilOp,
906 kKeep_StencilOp,
907 kAlwaysIfInClip_StencilFunc,
908 0x0000,
909 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000910 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000911 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
912}
913}
914
915void GrClipMaskManager::setGpuStencil() {
916 // We make two copies of the StencilSettings here (except in the early
917 // exit scenario. One copy from draw state to the stack var. Then another
918 // from the stack var to the gpu. We could make this class hold a ptr to
919 // GrGpu's fStencilSettings and eliminate the stack copy here.
920
921 const GrDrawState& drawState = fGpu->getDrawState();
922
923 // use stencil for clipping if clipping is enabled and the clip
924 // has been written into the stencil.
925 GrClipMaskManager::StencilClipMode clipMode;
926 if (this->isClipInStencil() && drawState.isClipState()) {
927 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
928 // We can't be modifying the clip and respecting it at the same time.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000929 SkASSERT(!drawState.isStateFlagEnabled(
bsalomon@google.coma3201942012-06-21 19:58:20 +0000930 GrGpu::kModifyStencilClip_StateBit));
931 } else if (drawState.isStateFlagEnabled(
932 GrGpu::kModifyStencilClip_StateBit)) {
933 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
934 } else {
935 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
936 }
937
938 GrStencilSettings settings;
939 // The GrGpu client may not be using the stencil buffer but we may need to
940 // enable it in order to respect a stencil clip.
941 if (drawState.getStencil().isDisabled()) {
942 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
943 settings = basic_apply_stencil_clip_settings();
944 } else {
945 fGpu->disableStencil();
946 return;
947 }
948 } else {
949 settings = drawState.getStencil();
950 }
951
952 // TODO: dynamically attach a stencil buffer
953 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000954 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000955 drawState.getRenderTarget()->getStencilBuffer();
956 if (NULL != stencilBuffer) {
957 stencilBits = stencilBuffer->bits();
958 }
959
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000960 SkASSERT(fGpu->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
961 SkASSERT(fGpu->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000962 this->adjustStencilParams(&settings, clipMode, stencilBits);
963 fGpu->setStencilSettings(settings);
964}
965
966void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
967 StencilClipMode mode,
968 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000969 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000970
971 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000972 // We assume that this clip manager itself is drawing to the GrGpu and
973 // has already setup the correct values.
974 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000975 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000976
bsalomon@google.com411dad02012-06-05 20:24:20 +0000977 unsigned int clipBit = (1 << (stencilBitCnt - 1));
978 unsigned int userBits = clipBit - 1;
979
bsalomon@google.coma3201942012-06-21 19:58:20 +0000980 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000981 bool twoSided = fGpu->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000982
bsalomon@google.coma3201942012-06-21 19:58:20 +0000983 bool finished = false;
984 while (!finished) {
985 GrStencilFunc func = settings->func(face);
986 uint16_t writeMask = settings->writeMask(face);
987 uint16_t funcMask = settings->funcMask(face);
988 uint16_t funcRef = settings->funcRef(face);
989
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000990 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000991
992 writeMask &= userBits;
993
994 if (func >= kBasicStencilFuncCount) {
995 int respectClip = kRespectClip_StencilClipMode == mode;
996 if (respectClip) {
997 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000998 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000999 switch (func) {
1000 case kAlwaysIfInClip_StencilFunc:
1001 funcMask = clipBit;
1002 funcRef = clipBit;
1003 break;
1004 case kEqualIfInClip_StencilFunc:
1005 case kLessIfInClip_StencilFunc:
1006 case kLEqualIfInClip_StencilFunc:
1007 funcMask = (funcMask & userBits) | clipBit;
1008 funcRef = (funcRef & userBits) | clipBit;
1009 break;
1010 case kNonZeroIfInClip_StencilFunc:
1011 funcMask = (funcMask & userBits) | clipBit;
1012 funcRef = clipBit;
1013 break;
1014 default:
1015 GrCrash("Unknown stencil func");
1016 }
1017 } else {
1018 funcMask &= userBits;
1019 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001020 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001021 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001022 gSpecialToBasicStencilFunc[respectClip];
1023 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001024 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001025 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001026 funcMask &= userBits;
1027 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001028 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001029
1030 settings->setFunc(face, func);
1031 settings->setWriteMask(face, writeMask);
1032 settings->setFuncMask(face, funcMask);
1033 settings->setFuncRef(face, funcRef);
1034
1035 if (GrStencilSettings::kFront_Face == face) {
1036 face = GrStencilSettings::kBack_Face;
1037 finished = !twoSided;
1038 } else {
1039 finished = true;
1040 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001041 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001042 if (!twoSided) {
1043 settings->copyFrontSettingsToBack();
1044 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001045}
1046
1047////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +00001048GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001049 GrReducedClip::InitialState initialState,
1050 const GrReducedClip::ElementList& elements,
1051 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001052 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001053
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001054 GrTexture* result;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +00001055 if (this->getMaskTexture(elementsGenID, clipSpaceIBounds, &result, true)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001056 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001057 }
1058
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001059 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001060 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001061 return NULL;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001062 }
1063
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001064 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1065 // the top left corner of the resulting rect to the top left of the texture.
1066 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1067
robertphillips@google.com2c756812012-05-22 20:28:23 +00001068 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001069
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001070 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001071 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
1072 SkIntToScalar(-clipSpaceIBounds.fTop));
1073 helper.init(maskSpaceIBounds, &matrix);
1074
1075 helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001076
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001077 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001078
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001079 for (ElementList::Iter iter(elements.headIter()) ; NULL != iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001080
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001081 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001082 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001083
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001084 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1085 // Intersect and reverse difference require modifying pixels outside of the geometry
1086 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1087 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1088 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001089 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001090 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001091 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001092 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001093 }
1094
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001095 SkPath clipPath;
1096 element->asPath(&clipPath);
1097 clipPath.toggleInverseFillType();
1098 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001099
1100 continue;
1101 }
1102
1103 // The other ops (union, xor, diff) only affect pixels inside
1104 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001105 if (Element::kRect_Type == element->getType()) {
1106 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1107 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001108 SkPath path;
1109 element->asPath(&path);
1110 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001111 }
1112 }
1113
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001114 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001115
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001116 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001117 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001118}
1119
robertphillips@google.comf294b772012-04-27 14:29:26 +00001120////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001121void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001122 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001123}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001124
1125void GrClipMaskManager::setGpu(GrGpu* gpu) {
1126 fGpu = gpu;
1127 fAACache.setContext(gpu->getContext());
1128}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001129
1130void GrClipMaskManager::adjustPathStencilParams(GrStencilSettings* settings) {
1131 const GrDrawState& drawState = fGpu->getDrawState();
1132 GrClipMaskManager::StencilClipMode clipMode;
1133 if (this->isClipInStencil() && drawState.isClipState()) {
1134 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
1135 // We can't be modifying the clip and respecting it at the same time.
1136 SkASSERT(!drawState.isStateFlagEnabled(
1137 GrGpu::kModifyStencilClip_StateBit));
1138 } else if (drawState.isStateFlagEnabled(
1139 GrGpu::kModifyStencilClip_StateBit)) {
1140 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
1141 } else {
1142 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
1143 }
1144
1145 // TODO: dynamically attach a stencil buffer
1146 int stencilBits = 0;
1147 GrStencilBuffer* stencilBuffer =
1148 drawState.getRenderTarget()->getStencilBuffer();
1149 if (NULL != stencilBuffer) {
1150 stencilBits = stencilBuffer->bits();
1151 this->adjustStencilParams(settings, clipMode, stencilBits);
1152 }
1153}