blob: e56f4dd77d523ad9f64df980c0435b82e7b552cd [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrClipMaskManager.h"
robertphillips@google.comfa662942012-05-17 12:20:22 +00009#include "GrAAConvexPathRenderer.h"
10#include "GrAAHairLinePathRenderer.h"
jvanverth@google.combfe2b9d2013-09-06 16:57:29 +000011#include "GrAARectRenderer.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000012#include "GrDrawTargetCaps.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000013#include "GrPaint.h"
14#include "GrPathRenderer.h"
15#include "GrRenderTarget.h"
16#include "GrStencilBuffer.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000017#include "GrSWMaskHelper.h"
joshualitt3a0cfeb2014-10-27 07:38:01 -070018#include "SkRasterClip.h"
19#include "SkStrokeRec.h"
20#include "SkTLazy.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080021#include "effects/GrConvexPolyEffect.h"
egdaniel95131432014-12-09 11:15:43 -080022#include "effects/GrPorterDuffXferProcessor.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080023#include "effects/GrRRectEffect.h"
egdaniel95131432014-12-09 11:15:43 -080024#include "effects/GrTextureDomain.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000025
robertphillips@google.comba998f22012-10-12 11:33:56 +000026#define GR_AA_CLIP 1
bsalomon@google.com8182fa02012-12-04 14:06:06 +000027typedef SkClipStack::Element Element;
bsalomon@google.com51a62862012-11-26 21:19:43 +000028
29////////////////////////////////////////////////////////////////////////////////
robertphillips@google.come79f3202014-02-11 16:30:21 +000030namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000031// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000032// stage matrix this also alters the vertex layout
joshualitt9853cce2014-11-17 14:22:48 -080033void setup_drawstate_aaclip(const SkIRect &devBound,
34 GrDrawState* drawState,
35 GrTexture* result) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000036 SkASSERT(drawState);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000037
bsalomon@google.comb9086a02012-11-01 18:02:54 +000038 SkMatrix mat;
bsalomon309d4d52014-12-18 10:17:44 -080039 // We use device coords to compute the texture coordinates. We set our matrix to be a
40 // translation to the devBound, and then a scaling matrix to normalized coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000041 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000042 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000043 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000044
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000045 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000046 // This could be a long-lived effect that is cached with the alpha-mask.
joshualittb0a8a372014-09-23 09:50:21 -070047 drawState->addCoverageProcessor(
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000048 GrTextureDomainEffect::Create(result,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000049 mat,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000050 GrTextureDomain::MakeTexelDomain(result, domainTexels),
51 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +000052 GrTextureParams::kNone_FilterMode,
bsalomon309d4d52014-12-18 10:17:44 -080053 kDevice_GrCoordSet))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000054}
55
robertphillips@google.come79f3202014-02-11 16:30:21 +000056bool path_needs_SW_renderer(GrContext* context,
joshualitt9853cce2014-11-17 14:22:48 -080057 const GrDrawTarget* gpu,
58 const GrDrawState* drawState,
robertphillips@google.come79f3202014-02-11 16:30:21 +000059 const SkPath& origPath,
60 const SkStrokeRec& stroke,
61 bool doAA) {
62 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
63 SkTCopyOnFirstWrite<SkPath> path(origPath);
64 if (path->isInverseFillType()) {
65 path.writable()->toggleInverseFillType();
66 }
67 // last (false) parameter disallows use of the SW path renderer
bsalomon@google.com45a15f52012-12-10 19:10:17 +000068 GrPathRendererChain::DrawType type = doAA ?
69 GrPathRendererChain::kColorAntiAlias_DrawType :
70 GrPathRendererChain::kColor_DrawType;
71
joshualitt9853cce2014-11-17 14:22:48 -080072 return NULL == context->getPathRenderer(gpu, drawState, *path, stroke, false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +000073}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000074}
75
robertphillips@google.comfa662942012-05-17 12:20:22 +000076/*
77 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
78 * will be used on any element. If so, it returns true to indicate that the
79 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
80 */
joshualitt9853cce2014-11-17 14:22:48 -080081bool GrClipMaskManager::useSWOnlyPath(const GrDrawState* drawState,
82 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000083 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000084 // a clip gets complex enough it can just be done in SW regardless
85 // of whether it would invoke the GrSoftwarePathRenderer.
sugoi@google.com5f74cf82012-12-17 21:16:45 +000086 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000087
tfarinabf54e492014-10-23 17:47:18 -070088 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000089 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000090 // rects can always be drawn directly w/o using the software path
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000091 // Skip rrects once we're drawing them directly.
92 if (Element::kRect_Type != element->getType()) {
93 SkPath path;
94 element->asPath(&path);
joshualitt9853cce2014-11-17 14:22:48 -080095 if (path_needs_SW_renderer(this->getContext(), fClipTarget, drawState, path, stroke,
joshualitt329bf482014-10-29 12:31:28 -070096 element->isAA())) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000097 return true;
98 }
robertphillips@google.comfa662942012-05-17 12:20:22 +000099 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000100 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000101 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000102}
103
joshualitt9853cce2014-11-17 14:22:48 -0800104bool GrClipMaskManager::installClipEffects(GrDrawState* drawState,
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000105 GrDrawState::AutoRestoreEffects* are,
joshualitt9853cce2014-11-17 14:22:48 -0800106 const GrReducedClip::ElementList& elements,
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000107 const SkVector& clipToRTOffset,
mtklein217daa72014-07-02 12:55:21 -0700108 const SkRect* drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000109 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700110 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000111 boundsInClipSpace = *drawBounds;
112 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
113 }
114
115 are->set(drawState);
116 GrRenderTarget* rt = drawState->getRenderTarget();
tfarinabf54e492014-10-23 17:47:18 -0700117 GrReducedClip::ElementList::Iter iter(elements);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000118 bool failed = false;
bsalomon49f085d2014-09-05 13:34:00 -0700119 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000120 SkRegion::Op op = iter.get()->getOp();
121 bool invert;
122 bool skip = false;
123 switch (op) {
124 case SkRegion::kReplace_Op:
125 SkASSERT(iter.get() == elements.head());
126 // Fallthrough, handled same as intersect.
127 case SkRegion::kIntersect_Op:
128 invert = false;
bsalomon49f085d2014-09-05 13:34:00 -0700129 if (drawBounds && iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000130 skip = true;
131 }
132 break;
133 case SkRegion::kDifference_Op:
134 invert = true;
135 // We don't currently have a cheap test for whether a rect is fully outside an
136 // element's primitive, so don't attempt to set skip.
137 break;
138 default:
139 failed = true;
140 break;
141 }
142 if (failed) {
143 break;
144 }
145
146 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700147 GrPrimitiveEdgeType edgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000148 if (GR_AA_CLIP && iter.get()->isAA()) {
149 if (rt->isMultisampled()) {
mtklein217daa72014-07-02 12:55:21 -0700150 // Coverage based AA clips don't place nicely with MSAA.
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000151 failed = true;
152 break;
153 }
joshualittb0a8a372014-09-23 09:50:21 -0700154 edgeType =
155 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000156 } else {
joshualittb0a8a372014-09-23 09:50:21 -0700157 edgeType =
158 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000159 }
joshualittb0a8a372014-09-23 09:50:21 -0700160 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000161 switch (iter.get()->getType()) {
162 case SkClipStack::Element::kPath_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700163 fp.reset(GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000164 &clipToRTOffset));
165 break;
166 case SkClipStack::Element::kRRect_Type: {
167 SkRRect rrect = iter.get()->getRRect();
168 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
joshualittb0a8a372014-09-23 09:50:21 -0700169 fp.reset(GrRRectEffect::Create(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000170 break;
171 }
172 case SkClipStack::Element::kRect_Type: {
173 SkRect rect = iter.get()->getRect();
174 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
joshualittb0a8a372014-09-23 09:50:21 -0700175 fp.reset(GrConvexPolyEffect::Create(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000176 break;
177 }
178 default:
179 break;
180 }
joshualittb0a8a372014-09-23 09:50:21 -0700181 if (fp) {
joshualitt9853cce2014-11-17 14:22:48 -0800182 drawState->addCoverageProcessor(fp);
mtklein217daa72014-07-02 12:55:21 -0700183 } else {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000184 failed = true;
185 break;
186 }
187 }
mtklein217daa72014-07-02 12:55:21 -0700188 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000189 }
190
191 if (failed) {
192 are->set(NULL);
193 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000194 return !failed;
195}
196
robertphillips@google.comf294b772012-04-27 14:29:26 +0000197////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000198// sort out what kind of clip mask needs to be created: alpha, stencil,
199// scissor, or entirely software
joshualitt9853cce2014-11-17 14:22:48 -0800200bool GrClipMaskManager::setupClipping(GrDrawState* drawState,
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000201 GrDrawState::AutoRestoreEffects* are,
joshualitt77b13072014-10-27 14:51:01 -0700202 GrDrawState::AutoRestoreStencil* ars,
bsalomon3e791242014-12-17 13:43:13 -0800203 GrScissorState* scissorState,
joshualitt9853cce2014-11-17 14:22:48 -0800204 const GrClipData* clipDataIn,
205 const SkRect* devBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000206 fCurrClipMaskType = kNone_ClipMaskType;
joshualitt7a6184f2014-10-29 18:29:27 -0700207 if (kRespectClip_StencilClipMode == fClipMode) {
208 fClipMode = kIgnoreClip_StencilClipMode;
209 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000210
tfarinabf54e492014-10-23 17:47:18 -0700211 GrReducedClip::ElementList elements(16);
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000212 int32_t genID;
tfarinabf54e492014-10-23 17:47:18 -0700213 GrReducedClip::InitialState initialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000214 SkIRect clipSpaceIBounds;
215 bool requiresAA;
joshualitt9853cce2014-11-17 14:22:48 -0800216 GrRenderTarget* rt = drawState->getRenderTarget();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000217
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000218 // GrDrawTarget should have filtered this for us
bsalomon49f085d2014-09-05 13:34:00 -0700219 SkASSERT(rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000220
221 bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000222 if (!ignoreClip) {
223 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
224 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
tfarinabf54e492014-10-23 17:47:18 -0700225 GrReducedClip::ReduceClipStack(*clipDataIn->fClipStack,
226 clipSpaceRTIBounds,
227 &elements,
228 &genID,
229 &initialState,
230 &clipSpaceIBounds,
231 &requiresAA);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000232 if (elements.isEmpty()) {
tfarinabf54e492014-10-23 17:47:18 -0700233 if (GrReducedClip::kAllIn_InitialState == initialState) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000234 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000235 } else {
236 return false;
237 }
238 }
239 }
240
241 if (ignoreClip) {
joshualitt9853cce2014-11-17 14:22:48 -0800242 this->setDrawStateStencil(drawState, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000243 return true;
244 }
245
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000246 // An element count of 4 was chosen because of the common pattern in Blink of:
247 // isect RR
248 // diff RR
249 // isect convex_poly
250 // isect convex_poly
251 // when drawing rounded div borders. This could probably be tuned based on a
252 // configuration's relative costs of switching RTs to generate a mask vs
253 // longer shaders.
254 if (elements.count() <= 4) {
255 SkVector clipToRTOffset = { SkIntToScalar(-clipDataIn->fOrigin.fX),
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000256 SkIntToScalar(-clipDataIn->fOrigin.fY) };
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000257 if (elements.isEmpty() ||
joshualitt9853cce2014-11-17 14:22:48 -0800258 (requiresAA && this->installClipEffects(drawState, are, elements, clipToRTOffset,
259 devBounds))) {
mtklein217daa72014-07-02 12:55:21 -0700260 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
261 scissorSpaceIBounds.offset(-clipDataIn->fOrigin);
262 if (NULL == devBounds ||
263 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
joshualitt77b13072014-10-27 14:51:01 -0700264 scissorState->set(scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000265 }
joshualitt9853cce2014-11-17 14:22:48 -0800266 this->setDrawStateStencil(drawState, ars);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000267 return true;
268 }
269 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000270
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000271#if GR_AA_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000272 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000273 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000274 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000275
joshualitt9853cce2014-11-17 14:22:48 -0800276 if (this->useSWOnlyPath(drawState, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000277 // The clip geometry is complex enough that it will be more efficient to create it
278 // entirely in software
279 result = this->createSoftwareClipMask(genID,
280 initialState,
281 elements,
282 clipSpaceIBounds);
283 } else {
284 result = this->createAlphaClipMask(genID,
285 initialState,
286 elements,
287 clipSpaceIBounds);
288 }
289
bsalomon49f085d2014-09-05 13:34:00 -0700290 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000291 // The mask's top left coord should be pinned to the rounded-out top left corner of
292 // clipSpace bounds. We determine the mask's position WRT to the render target here.
293 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
294 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
joshualitt9853cce2014-11-17 14:22:48 -0800295 setup_drawstate_aaclip(rtSpaceMaskBounds, drawState, result);
296 this->setDrawStateStencil(drawState, ars);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000297 return true;
298 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000299 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000300 }
301#endif // GR_AA_CLIP
302
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000303 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
304 // be created. In either case, free up the texture in the anti-aliased mask cache.
305 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
306 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
307 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000308 fAACache.reset();
309
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000310 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000311 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
joshualitt9853cce2014-11-17 14:22:48 -0800312 this->createStencilClipMask(rt,
313 genID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000314 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000315 elements,
316 clipSpaceIBounds,
317 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000318
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000319 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
320 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
321 // use both stencil and scissor test to the bounds for the final draw.
322 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
323 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
joshualitt77b13072014-10-27 14:51:01 -0700324 scissorState->set(scissorSpaceIBounds);
joshualitt9853cce2014-11-17 14:22:48 -0800325 this->setDrawStateStencil(drawState, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000326 return true;
327}
328
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000329namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000330////////////////////////////////////////////////////////////////////////////////
egdaniel87509242014-12-17 13:37:13 -0800331// Set a coverage drawing XPF on the drawState for the given op and invertCoverage mode
332void set_coverage_drawing_xpf(SkRegion::Op op, bool invertCoverage, GrDrawState* drawState) {
333 SkASSERT(op <= SkRegion::kLastOp);
334 drawState->setCoverageSetOpXPFactory(op, invertCoverage);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000335}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000336}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000337
338////////////////////////////////////////////////////////////////////////////////
joshualitt9853cce2014-11-17 14:22:48 -0800339bool GrClipMaskManager::drawElement(GrDrawState* drawState,
340 GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000341 const SkClipStack::Element* element,
342 GrPathRenderer* pr) {
joshualitt9853cce2014-11-17 14:22:48 -0800343 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000344
345 drawState->setRenderTarget(target->asRenderTarget());
346
egdaniel87509242014-12-17 13:37:13 -0800347 // The color we use to draw does not matter since we will always be using a GrCoverageSetOpXP
348 // which ignores color.
349 GrColor color = GrColor_WHITE;
350
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000351 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000352 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000353 case Element::kEmpty_Type:
354 SkDEBUGFAIL("Should never get here with an empty element.");
355 break;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000356 case Element::kRect_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700357 // TODO: Do rects directly to the accumulator using a aa-rect GrProcessor that covers
358 // the entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000359 if (element->isAA()) {
joshualitt329bf482014-10-29 12:31:28 -0700360 this->getContext()->getAARectRenderer()->fillAARect(fClipTarget,
joshualitt9853cce2014-11-17 14:22:48 -0800361 drawState,
joshualitt2e3b3e32014-12-09 13:31:14 -0800362 color,
joshualitta58fe352014-10-27 08:39:00 -0700363 element->getRect(),
364 SkMatrix::I(),
365 element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000366 } else {
joshualitt2e3b3e32014-12-09 13:31:14 -0800367 fClipTarget->drawSimpleRect(drawState, color, element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000368 }
369 return true;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000370 default: {
371 SkPath path;
372 element->asPath(&path);
jvanverthb3eb6872014-10-24 07:12:51 -0700373 path.setIsVolatile(true);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000374 if (path.isInverseFillType()) {
375 path.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000376 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000377 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000378 if (NULL == pr) {
379 GrPathRendererChain::DrawType type;
380 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
381 GrPathRendererChain::kColor_DrawType;
joshualitt9853cce2014-11-17 14:22:48 -0800382 pr = this->getContext()->getPathRenderer(fClipTarget, drawState, path, stroke,
383 false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000384 }
385 if (NULL == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000386 return false;
387 }
joshualitt9853cce2014-11-17 14:22:48 -0800388
joshualitt2e3b3e32014-12-09 13:31:14 -0800389 pr->drawPath(fClipTarget, drawState, color, path, stroke, element->isAA());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000390 break;
391 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000392 }
393 return true;
394}
395
joshualitt9853cce2014-11-17 14:22:48 -0800396bool GrClipMaskManager::canStencilAndDrawElement(GrDrawState* drawState,
397 GrTexture* target,
398 GrPathRenderer** pr,
399 const SkClipStack::Element* element) {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000400 drawState->setRenderTarget(target->asRenderTarget());
401
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000402 if (Element::kRect_Type == element->getType()) {
403 return true;
404 } else {
405 // We shouldn't get here with an empty clip element.
406 SkASSERT(Element::kEmpty_Type != element->getType());
407 SkPath path;
408 element->asPath(&path);
409 if (path.isInverseFillType()) {
410 path.toggleInverseFillType();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000411 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000412 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
413 GrPathRendererChain::DrawType type = element->isAA() ?
414 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
415 GrPathRendererChain::kStencilAndColor_DrawType;
joshualitt9853cce2014-11-17 14:22:48 -0800416 *pr = this->getContext()->getPathRenderer(fClipTarget, drawState, path, stroke, false,
417 type);
bsalomon49f085d2014-09-05 13:34:00 -0700418 return SkToBool(*pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000419 }
420}
421
joshualitt9853cce2014-11-17 14:22:48 -0800422void GrClipMaskManager::mergeMask(GrDrawState* drawState,
423 GrTexture* dstMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000424 GrTexture* srcMask,
425 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000426 const SkIRect& dstBound,
427 const SkIRect& srcBound) {
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000428 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000429
egdaniel87509242014-12-17 13:37:13 -0800430 // We want to invert the coverage here
431 set_coverage_drawing_xpf(op, false, drawState);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000432
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000433 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000434 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000435
egdaniel87509242014-12-17 13:37:13 -0800436 drawState->addCoverageProcessor(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000437 GrTextureDomainEffect::Create(srcMask,
438 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000439 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
440 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000441 GrTextureParams::kNone_FilterMode))->unref();
egdaniel87509242014-12-17 13:37:13 -0800442 // The color passed in here does not matter since the coverageSetOpXP won't read it.
joshualitt2e3b3e32014-12-09 13:31:14 -0800443 fClipTarget->drawSimpleRect(drawState, GrColor_WHITE, SkRect::Make(dstBound));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000444}
445
bsalomon427cf282014-10-16 13:41:43 -0700446GrTexture* GrClipMaskManager::createTempMask(int width, int height) {
bsalomonf2703d82014-10-28 14:33:06 -0700447 GrSurfaceDesc desc;
bsalomon3f490a02014-12-18 06:20:52 -0800448 desc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000449 desc.fWidth = width;
450 desc.fHeight = height;
bsalomon51d1f7e2014-12-22 08:40:49 -0800451 if (this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
452 desc.fConfig = kAlpha_8_GrPixelConfig;
453 } else {
454 desc.fConfig = kRGBA_8888_GrPixelConfig;
455 }
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000456
joshualitt329bf482014-10-29 12:31:28 -0700457 return this->getContext()->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000458}
459
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000460////////////////////////////////////////////////////////////////////////////////
krajcevskiad1dc582014-06-10 15:06:47 -0700461// Return the texture currently in the cache if it exists. Otherwise, return NULL
462GrTexture* GrClipMaskManager::getCachedMaskTexture(int32_t elementsGenID,
463 const SkIRect& clipSpaceIBounds) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000464 bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000465 if (!cached) {
krajcevskiad1dc582014-06-10 15:06:47 -0700466 return NULL;
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000467 }
468
krajcevskiad1dc582014-06-10 15:06:47 -0700469 return fAACache.getLastMask();
470}
471
472////////////////////////////////////////////////////////////////////////////////
473// Allocate a texture in the texture cache. This function returns the texture
474// allocated (or NULL on error).
475GrTexture* GrClipMaskManager::allocMaskTexture(int32_t elementsGenID,
476 const SkIRect& clipSpaceIBounds,
477 bool willUpload) {
478 // Since we are setting up the cache we should free up the
479 // currently cached mask so it can be reused.
480 fAACache.reset();
481
bsalomonf2703d82014-10-28 14:33:06 -0700482 GrSurfaceDesc desc;
483 desc.fFlags = willUpload ? kNone_GrSurfaceFlags : kRenderTarget_GrSurfaceFlag;
krajcevskiad1dc582014-06-10 15:06:47 -0700484 desc.fWidth = clipSpaceIBounds.width();
485 desc.fHeight = clipSpaceIBounds.height();
486 desc.fConfig = kRGBA_8888_GrPixelConfig;
487 if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
488 // We would always like A8 but it isn't supported on all platforms
489 desc.fConfig = kAlpha_8_GrPixelConfig;
490 }
491
492 fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds);
493 return fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000494}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000495
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000496////////////////////////////////////////////////////////////////////////////////
497// Create a 8-bit clip mask in alpha
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000498GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700499 GrReducedClip::InitialState initialState,
500 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000501 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000502 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000503
krajcevskiad1dc582014-06-10 15:06:47 -0700504 // First, check for cached texture
505 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -0700506 if (result) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000507 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000508 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000509 }
510
krajcevskiad1dc582014-06-10 15:06:47 -0700511 // There's no texture in the cache. Let's try to allocate it then.
512 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, false);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000513 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000514 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000515 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000516 }
517
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000518 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000519 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000520 SkIntToScalar(-clipSpaceIBounds.fLeft),
521 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000522 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000523 // The texture may be larger than necessary, this rect represents the part of the texture
524 // we populate with a rasterization of the clip.
525 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
526
bsalomon@google.com137f1342013-05-29 21:27:53 +0000527 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
528 SkMatrix translate;
529 translate.setTranslate(clipToMaskOffset);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000530
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000531 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
532 // clear the part that we care about.
joshualitt329bf482014-10-29 12:31:28 -0700533 fClipTarget->clear(&maskSpaceIBounds,
534 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
535 true,
536 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000537
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000538 // When we use the stencil in the below loop it is important to have this clip installed.
539 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
540 // pass must not set values outside of this bounds or stencil values outside the rect won't be
541 // cleared.
joshualitt329bf482014-10-29 12:31:28 -0700542 GrDrawTarget::AutoClipRestore acr(fClipTarget, maskSpaceIBounds);
bsalomon427cf282014-10-16 13:41:43 -0700543 SkAutoTUnref<GrTexture> temp;
joshualitt9853cce2014-11-17 14:22:48 -0800544
robertphillips@google.comf294b772012-04-27 14:29:26 +0000545 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700546 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000547 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000548 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000549 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000550 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
joshualitt9853cce2014-11-17 14:22:48 -0800551 GrDrawState drawState(translate);
egdaniel87509242014-12-17 13:37:13 -0800552 drawState.enableState(GrDrawState::kClip_StateBit);
joshualitt9853cce2014-11-17 14:22:48 -0800553
robertphillips@google.come79f3202014-02-11 16:30:21 +0000554 GrPathRenderer* pr = NULL;
joshualitt9853cce2014-11-17 14:22:48 -0800555 bool useTemp = !this->canStencilAndDrawElement(&drawState, result, &pr, element);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000556 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000557 // 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 +0000558 // mask buffer can be substantially larger than the actually clip stack element. We
559 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000560 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000561 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000562
563 if (useTemp) {
564 if (invert) {
565 maskSpaceElementIBounds = maskSpaceIBounds;
566 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000567 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000568 elementBounds.offset(clipToMaskOffset);
569 elementBounds.roundOut(&maskSpaceElementIBounds);
570 }
571
bsalomon427cf282014-10-16 13:41:43 -0700572 if (!temp) {
573 temp.reset(this->createTempMask(maskSpaceIBounds.fRight,
574 maskSpaceIBounds.fBottom));
575 if (!temp) {
576 fAACache.reset();
577 return NULL;
578 }
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000579 }
bsalomon427cf282014-10-16 13:41:43 -0700580 dst = temp;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000581 // clear the temp target and set blend to replace
joshualitt329bf482014-10-29 12:31:28 -0700582 fClipTarget->clear(&maskSpaceElementIBounds,
joshualitt9853cce2014-11-17 14:22:48 -0800583 invert ? 0xffffffff : 0x00000000,
584 true,
585 dst->asRenderTarget());
egdaniel87509242014-12-17 13:37:13 -0800586 set_coverage_drawing_xpf(SkRegion::kReplace_Op, invert, &drawState);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000587 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000588 // draw directly into the result with the stencil set to make the pixels affected
589 // by the clip shape be non-zero.
590 dst = result;
591 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
592 kReplace_StencilOp,
593 kReplace_StencilOp,
594 kAlways_StencilFunc,
595 0xffff,
596 0xffff,
597 0xffff);
joshualitt9853cce2014-11-17 14:22:48 -0800598 drawState.setStencil(kStencilInElement);
egdaniel87509242014-12-17 13:37:13 -0800599 set_coverage_drawing_xpf(op, invert, &drawState);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000600 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000601
egdaniel87509242014-12-17 13:37:13 -0800602 if (!this->drawElement(&drawState, dst, element, pr)) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000603 fAACache.reset();
604 return NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000605 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000606
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000607 if (useTemp) {
joshualitt8fc6c2d2014-12-22 15:27:05 -0800608 GrDrawState backgroundDrawState;
609 backgroundDrawState.enableState(GrDrawState::kClip_StateBit);
610 backgroundDrawState.setRenderTarget(result->asRenderTarget());
611
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000612 // Now draw into the accumulator using the real operation and the temp buffer as a
613 // texture
egdaniel87509242014-12-17 13:37:13 -0800614 this->mergeMask(&backgroundDrawState,
joshualitt9853cce2014-11-17 14:22:48 -0800615 result,
bsalomon427cf282014-10-16 13:41:43 -0700616 temp,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000617 op,
618 maskSpaceIBounds,
619 maskSpaceElementIBounds);
620 } else {
joshualitt8fc6c2d2014-12-22 15:27:05 -0800621 GrDrawState backgroundDrawState(translate);
622 backgroundDrawState.enableState(GrDrawState::kClip_StateBit);
623 backgroundDrawState.setRenderTarget(result->asRenderTarget());
624
egdaniel87509242014-12-17 13:37:13 -0800625 set_coverage_drawing_xpf(op, !invert, &backgroundDrawState);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000626 // Draw to the exterior pixels (those with a zero stencil value).
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000627 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
628 kZero_StencilOp,
629 kZero_StencilOp,
630 kEqual_StencilFunc,
631 0xffff,
632 0x0000,
633 0xffff);
egdaniel87509242014-12-17 13:37:13 -0800634 backgroundDrawState.setStencil(kDrawOutsideElement);
635 // The color passed in here does not matter since the coverageSetOpXP won't read it.
636 fClipTarget->drawSimpleRect(&backgroundDrawState, GrColor_WHITE, clipSpaceIBounds);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000637 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000638 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800639 GrDrawState drawState(translate);
egdaniel87509242014-12-17 13:37:13 -0800640 drawState.enableState(GrDrawState::kClip_StateBit);
joshualitt9853cce2014-11-17 14:22:48 -0800641
robertphillips@google.come79f3202014-02-11 16:30:21 +0000642 // all the remaining ops can just be directly draw into the accumulation buffer
egdaniel87509242014-12-17 13:37:13 -0800643 set_coverage_drawing_xpf(op, false, &drawState);
644 // The color passed in here does not matter since the coverageSetOpXP won't read it.
645 this->drawElement(&drawState, result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000646 }
647 }
648
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000649 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000650 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000651}
652
653////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000654// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000655// (as opposed to canvas) coordinates
joshualitt9853cce2014-11-17 14:22:48 -0800656bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt,
657 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700658 GrReducedClip::InitialState initialState,
659 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000660 const SkIRect& clipSpaceIBounds,
661 const SkIPoint& clipSpaceToStencilOffset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000662 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon49f085d2014-09-05 13:34:00 -0700663 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000664
665 // TODO: dynamically attach a SB when needed.
666 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
667 if (NULL == stencilBuffer) {
668 return false;
669 }
670
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000671 if (stencilBuffer->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000672 stencilBuffer->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000673 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
674 SkVector translate = {
675 SkIntToScalar(clipSpaceToStencilOffset.fX),
676 SkIntToScalar(clipSpaceToStencilOffset.fY)
677 };
678 SkMatrix matrix;
679 matrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000680
bsalomon@google.com9f131742012-12-13 20:43:56 +0000681 // We set the current clip to the bounds so that our recursive draws are scissored to them.
682 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
683 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
joshualitt329bf482014-10-29 12:31:28 -0700684 GrDrawTarget::AutoClipRestore acr(fClipTarget, stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000685
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000686 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000687 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000688 clipBit = (1 << (clipBit-1));
689
joshualitt329bf482014-10-29 12:31:28 -0700690 fClipTarget->clearStencilClip(stencilSpaceIBounds,
691 GrReducedClip::kAllIn_InitialState == initialState,
692 rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000693
694 // walk through each clip element and perform its set op
695 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700696 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000697 const Element* element = iter.get();
joshualitt9853cce2014-11-17 14:22:48 -0800698
699 GrDrawState drawState(matrix);
700 drawState.setRenderTarget(rt);
701 drawState.enableState(GrDrawState::kClip_StateBit);
egdaniel080e6732014-12-22 07:35:52 -0800702
703 drawState.setDisableColorXPFactory();
joshualitt9853cce2014-11-17 14:22:48 -0800704
705 // if the target is MSAA then we want MSAA enabled when the clip is soft
706 if (rt->isMultisampled()) {
707 drawState.setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
708 }
709
tomhudson@google.com8afae612012-08-14 15:03:35 +0000710 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000711 // enabled at bottom of loop
joshualitt7a6184f2014-10-29 18:29:27 -0700712 fClipMode = kIgnoreClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000713
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000714 // This will be used to determine whether the clip shape can be rendered into the
715 // stencil with arbitrary stencil settings.
716 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000717
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000718 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000719 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000720
robertphillips@google.come79f3202014-02-11 16:30:21 +0000721 GrPathRenderer* pr = NULL;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000722 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000723 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000724 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000725 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000726 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000727 element->asPath(&clipPath);
728 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000729 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000730 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000731 }
joshualitt9853cce2014-11-17 14:22:48 -0800732 pr = this->getContext()->getPathRenderer(fClipTarget,
733 &drawState,
734 clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000735 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000736 false,
737 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000738 &stencilSupport);
739 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000740 return false;
741 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000742 }
743
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000744 int passes;
745 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
746
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000747 bool canRenderDirectToStencil =
748 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000749 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000750 // fill rule, and set operation can
751 // we render the element directly to
752 // stencil bit used for clipping.
753 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
754 canRenderDirectToStencil,
755 clipBit,
756 fillInverted,
757 &passes,
758 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000759
760 // draw the element to the client stencil bits if necessary
761 if (!canDrawDirectToClip) {
762 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000763 kIncClamp_StencilOp,
764 kIncClamp_StencilOp,
765 kAlways_StencilFunc,
766 0xffff,
767 0x0000,
768 0xffff);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000769 if (Element::kRect_Type == element->getType()) {
joshualitt9853cce2014-11-17 14:22:48 -0800770 *drawState.stencil() = gDrawToStencil;
joshualitt2e3b3e32014-12-09 13:31:14 -0800771 fClipTarget->drawSimpleRect(&drawState, GrColor_WHITE, element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000772 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000773 if (!clipPath.isEmpty()) {
joshualitt9853cce2014-11-17 14:22:48 -0800774 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000775 if (canRenderDirectToStencil) {
joshualitt9853cce2014-11-17 14:22:48 -0800776 *drawState.stencil() = gDrawToStencil;
joshualitt2e3b3e32014-12-09 13:31:14 -0800777 pr->drawPath(fClipTarget, &drawState, GrColor_WHITE, clipPath, stroke,
778 false);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000779 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800780 pr->stencilPath(fClipTarget, &drawState, clipPath, stroke);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000781 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000782 }
783 }
784 }
785
786 // now we modify the clip bit by rendering either the clip
787 // element directly or a bounding rect of the entire clip.
joshualitt7a6184f2014-10-29 18:29:27 -0700788 fClipMode = kModifyClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000789 for (int p = 0; p < passes; ++p) {
joshualitt9853cce2014-11-17 14:22:48 -0800790 GrDrawState drawStateCopy(drawState);
791 *drawStateCopy.stencil() = stencilSettings[p];
792
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000793 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000794 if (Element::kRect_Type == element->getType()) {
joshualitt2e3b3e32014-12-09 13:31:14 -0800795 fClipTarget->drawSimpleRect(&drawStateCopy, GrColor_WHITE,
796 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000797 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800798 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
joshualitt2e3b3e32014-12-09 13:31:14 -0800799 pr->drawPath(fClipTarget, &drawStateCopy, GrColor_WHITE, clipPath, stroke, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000800 }
801 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000802 // The view matrix is setup to do clip space -> stencil space translation, so
803 // draw rect in clip space.
joshualitt2e3b3e32014-12-09 13:31:14 -0800804 fClipTarget->drawSimpleRect(&drawStateCopy, GrColor_WHITE,
805 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000806 }
807 }
808 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000809 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000810 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000811 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000812 fCurrClipMaskType = kStencil_ClipMaskType;
joshualitt7a6184f2014-10-29 18:29:27 -0700813 fClipMode = kRespectClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000814 return true;
815}
816
bsalomon@google.com411dad02012-06-05 20:24:20 +0000817// mapping of clip-respecting stencil funcs to normal stencil funcs
818// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000819static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000820 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
821 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
822 // In the Clip Funcs
823 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
824 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
825 kLess_StencilFunc, // kLessIfInClip_StencilFunc
826 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
827 // Special in the clip func that forces user's ref to be 0.
828 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
829 // make ref 0 and do normal nequal.
830 },
831 {// Stencil-Clipping is ENABLED
832 // In the Clip Funcs
833 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
834 // eq stencil clip bit, mask
835 // out user bits.
836
837 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
838 // add stencil bit to mask and ref
839
840 kLess_StencilFunc, // kLessIfInClip_StencilFunc
841 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
842 // for both of these we can add
843 // the clip bit to the mask and
844 // ref and compare as normal
845 // Special in the clip func that forces user's ref to be 0.
846 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
847 // make ref have only the clip bit set
848 // and make comparison be less
849 // 10..0 < 1..user_bits..
850 }
851};
852
bsalomon@google.coma3201942012-06-21 19:58:20 +0000853namespace {
854// Sets the settings to clip against the stencil buffer clip while ignoring the
855// client bits.
856const GrStencilSettings& basic_apply_stencil_clip_settings() {
857 // stencil settings to use when clip is in stencil
858 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
859 kKeep_StencilOp,
860 kKeep_StencilOp,
861 kAlwaysIfInClip_StencilFunc,
862 0x0000,
863 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000864 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000865 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
866}
867}
868
joshualitt9853cce2014-11-17 14:22:48 -0800869void GrClipMaskManager::setDrawStateStencil(GrDrawState* drawState,
870 GrDrawState::AutoRestoreStencil* ars) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000871 // We make two copies of the StencilSettings here (except in the early
872 // exit scenario. One copy from draw state to the stack var. Then another
873 // from the stack var to the gpu. We could make this class hold a ptr to
874 // GrGpu's fStencilSettings and eliminate the stack copy here.
875
bsalomon@google.coma3201942012-06-21 19:58:20 +0000876 // use stencil for clipping if clipping is enabled and the clip
877 // has been written into the stencil.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000878 GrStencilSettings settings;
joshualitt9853cce2014-11-17 14:22:48 -0800879
bsalomon@google.coma3201942012-06-21 19:58:20 +0000880 // The GrGpu client may not be using the stencil buffer but we may need to
881 // enable it in order to respect a stencil clip.
joshualitt9853cce2014-11-17 14:22:48 -0800882 if (drawState->getStencil().isDisabled()) {
joshualitt7a6184f2014-10-29 18:29:27 -0700883 if (GrClipMaskManager::kRespectClip_StencilClipMode == fClipMode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000884 settings = basic_apply_stencil_clip_settings();
885 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000886 return;
887 }
888 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800889 settings = drawState->getStencil();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000890 }
891
892 // TODO: dynamically attach a stencil buffer
893 int stencilBits = 0;
joshualitt9853cce2014-11-17 14:22:48 -0800894 GrStencilBuffer* stencilBuffer = drawState->getRenderTarget()->getStencilBuffer();
bsalomon49f085d2014-09-05 13:34:00 -0700895 if (stencilBuffer) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000896 stencilBits = stencilBuffer->bits();
897 }
898
joshualitt329bf482014-10-29 12:31:28 -0700899 SkASSERT(fClipTarget->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
900 SkASSERT(fClipTarget->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
joshualitt7a6184f2014-10-29 18:29:27 -0700901 this->adjustStencilParams(&settings, fClipMode, stencilBits);
joshualitt9853cce2014-11-17 14:22:48 -0800902 ars->set(drawState);
903 drawState->setStencil(settings);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000904}
905
906void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
907 StencilClipMode mode,
908 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000909 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000910
911 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000912 // We assume that this clip manager itself is drawing to the GrGpu and
913 // has already setup the correct values.
914 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000915 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000916
bsalomon@google.com411dad02012-06-05 20:24:20 +0000917 unsigned int clipBit = (1 << (stencilBitCnt - 1));
918 unsigned int userBits = clipBit - 1;
919
bsalomon@google.coma3201942012-06-21 19:58:20 +0000920 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
joshualitt329bf482014-10-29 12:31:28 -0700921 bool twoSided = fClipTarget->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000922
bsalomon@google.coma3201942012-06-21 19:58:20 +0000923 bool finished = false;
924 while (!finished) {
925 GrStencilFunc func = settings->func(face);
926 uint16_t writeMask = settings->writeMask(face);
927 uint16_t funcMask = settings->funcMask(face);
928 uint16_t funcRef = settings->funcRef(face);
929
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000930 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000931
932 writeMask &= userBits;
933
934 if (func >= kBasicStencilFuncCount) {
935 int respectClip = kRespectClip_StencilClipMode == mode;
936 if (respectClip) {
937 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000938 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000939 switch (func) {
940 case kAlwaysIfInClip_StencilFunc:
941 funcMask = clipBit;
942 funcRef = clipBit;
943 break;
944 case kEqualIfInClip_StencilFunc:
945 case kLessIfInClip_StencilFunc:
946 case kLEqualIfInClip_StencilFunc:
947 funcMask = (funcMask & userBits) | clipBit;
948 funcRef = (funcRef & userBits) | clipBit;
949 break;
950 case kNonZeroIfInClip_StencilFunc:
951 funcMask = (funcMask & userBits) | clipBit;
952 funcRef = clipBit;
953 break;
954 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000955 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +0000956 }
957 } else {
958 funcMask &= userBits;
959 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000960 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000961 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000962 gSpecialToBasicStencilFunc[respectClip];
963 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000964 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000965 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000966 funcMask &= userBits;
967 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000968 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000969
970 settings->setFunc(face, func);
971 settings->setWriteMask(face, writeMask);
972 settings->setFuncMask(face, funcMask);
973 settings->setFuncRef(face, funcRef);
974
975 if (GrStencilSettings::kFront_Face == face) {
976 face = GrStencilSettings::kBack_Face;
977 finished = !twoSided;
978 } else {
979 finished = true;
980 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000981 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000982 if (!twoSided) {
983 settings->copyFrontSettingsToBack();
984 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000985}
986
987////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000988GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000989 GrReducedClip::InitialState initialState,
990 const GrReducedClip::ElementList& elements,
991 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000992 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000993
krajcevskiad1dc582014-06-10 15:06:47 -0700994 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -0700995 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000996 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000997 }
998
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000999 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1000 // the top left corner of the resulting rect to the top left of the texture.
1001 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1002
robertphillips@google.com2c756812012-05-22 20:28:23 +00001003 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001004
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001005 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001006 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
1007 SkIntToScalar(-clipSpaceIBounds.fTop));
joshualitt9853cce2014-11-17 14:22:48 -08001008
krajcevski71614ac2014-08-13 10:36:18 -07001009 helper.init(maskSpaceIBounds, &matrix, false);
tfarinabf54e492014-10-23 17:47:18 -07001010 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001011 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001012
tfarinabf54e492014-10-23 17:47:18 -07001013 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001014 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001015 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001016
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001017 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1018 // Intersect and reverse difference require modifying pixels outside of the geometry
1019 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1020 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1021 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001022 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001023 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001024 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001025 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001026 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001027 SkPath clipPath;
1028 element->asPath(&clipPath);
1029 clipPath.toggleInverseFillType();
1030 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001031 continue;
1032 }
1033
1034 // The other ops (union, xor, diff) only affect pixels inside
1035 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001036 if (Element::kRect_Type == element->getType()) {
1037 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1038 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001039 SkPath path;
1040 element->asPath(&path);
1041 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001042 }
1043 }
1044
krajcevskiad1dc582014-06-10 15:06:47 -07001045 // Allocate clip mask texture
1046 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, true);
1047 if (NULL == result) {
1048 fAACache.reset();
1049 return NULL;
1050 }
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001051 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001052
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001053 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001054 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001055}
1056
robertphillips@google.comf294b772012-04-27 14:29:26 +00001057////////////////////////////////////////////////////////////////////////////////
bsalomonc8dc1f72014-08-21 13:02:13 -07001058void GrClipMaskManager::purgeResources() {
1059 fAACache.purgeResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001060}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001061
joshualitt329bf482014-10-29 12:31:28 -07001062void GrClipMaskManager::setClipTarget(GrClipTarget* clipTarget) {
1063 fClipTarget = clipTarget;
1064 fAACache.setContext(clipTarget->getContext());
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001065}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001066
joshualitt9853cce2014-11-17 14:22:48 -08001067void GrClipMaskManager::adjustPathStencilParams(const GrStencilBuffer* stencilBuffer,
1068 GrStencilSettings* settings) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001069 // TODO: dynamically attach a stencil buffer
bsalomon49f085d2014-09-05 13:34:00 -07001070 if (stencilBuffer) {
joshualitt9853cce2014-11-17 14:22:48 -08001071 int stencilBits = stencilBuffer->bits();
joshualitt7a6184f2014-10-29 18:29:27 -07001072 this->adjustStencilParams(settings, fClipMode, stencilBits);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001073 }
1074}