blob: 7f85801d998e882d00a13bac98bbbd0b1ee9395f [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) {
joshualitt9853cce2014-11-17 14:22:48 -0800428 SkAssertResult(drawState->setIdentityViewMatrix());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000429
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000430 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000431
egdaniel87509242014-12-17 13:37:13 -0800432 // We want to invert the coverage here
433 set_coverage_drawing_xpf(op, false, drawState);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000434
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000435 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000436 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000437
egdaniel87509242014-12-17 13:37:13 -0800438 drawState->addCoverageProcessor(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000439 GrTextureDomainEffect::Create(srcMask,
440 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000441 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
442 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000443 GrTextureParams::kNone_FilterMode))->unref();
egdaniel87509242014-12-17 13:37:13 -0800444 // The color passed in here does not matter since the coverageSetOpXP won't read it.
joshualitt2e3b3e32014-12-09 13:31:14 -0800445 fClipTarget->drawSimpleRect(drawState, GrColor_WHITE, SkRect::Make(dstBound));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000446}
447
bsalomon427cf282014-10-16 13:41:43 -0700448GrTexture* GrClipMaskManager::createTempMask(int width, int height) {
bsalomonf2703d82014-10-28 14:33:06 -0700449 GrSurfaceDesc desc;
bsalomon3f490a02014-12-18 06:20:52 -0800450 desc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000451 desc.fWidth = width;
452 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000453 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000454
joshualitt329bf482014-10-29 12:31:28 -0700455 return this->getContext()->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000456}
457
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000458////////////////////////////////////////////////////////////////////////////////
krajcevskiad1dc582014-06-10 15:06:47 -0700459// Return the texture currently in the cache if it exists. Otherwise, return NULL
460GrTexture* GrClipMaskManager::getCachedMaskTexture(int32_t elementsGenID,
461 const SkIRect& clipSpaceIBounds) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000462 bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000463 if (!cached) {
krajcevskiad1dc582014-06-10 15:06:47 -0700464 return NULL;
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000465 }
466
krajcevskiad1dc582014-06-10 15:06:47 -0700467 return fAACache.getLastMask();
468}
469
470////////////////////////////////////////////////////////////////////////////////
471// Allocate a texture in the texture cache. This function returns the texture
472// allocated (or NULL on error).
473GrTexture* GrClipMaskManager::allocMaskTexture(int32_t elementsGenID,
474 const SkIRect& clipSpaceIBounds,
475 bool willUpload) {
476 // Since we are setting up the cache we should free up the
477 // currently cached mask so it can be reused.
478 fAACache.reset();
479
bsalomonf2703d82014-10-28 14:33:06 -0700480 GrSurfaceDesc desc;
481 desc.fFlags = willUpload ? kNone_GrSurfaceFlags : kRenderTarget_GrSurfaceFlag;
krajcevskiad1dc582014-06-10 15:06:47 -0700482 desc.fWidth = clipSpaceIBounds.width();
483 desc.fHeight = clipSpaceIBounds.height();
484 desc.fConfig = kRGBA_8888_GrPixelConfig;
485 if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
486 // We would always like A8 but it isn't supported on all platforms
487 desc.fConfig = kAlpha_8_GrPixelConfig;
488 }
489
490 fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds);
491 return fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000492}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000493
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000494////////////////////////////////////////////////////////////////////////////////
495// Create a 8-bit clip mask in alpha
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000496GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700497 GrReducedClip::InitialState initialState,
498 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000499 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000500 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000501
krajcevskiad1dc582014-06-10 15:06:47 -0700502 // First, check for cached texture
503 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -0700504 if (result) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000505 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000506 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000507 }
508
krajcevskiad1dc582014-06-10 15:06:47 -0700509 // There's no texture in the cache. Let's try to allocate it then.
510 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, false);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000511 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000512 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000513 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000514 }
515
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000516 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000517 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000518 SkIntToScalar(-clipSpaceIBounds.fLeft),
519 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000520 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000521 // The texture may be larger than necessary, this rect represents the part of the texture
522 // we populate with a rasterization of the clip.
523 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
524
bsalomon@google.com137f1342013-05-29 21:27:53 +0000525 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
526 SkMatrix translate;
527 translate.setTranslate(clipToMaskOffset);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000528
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000529 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
530 // clear the part that we care about.
joshualitt329bf482014-10-29 12:31:28 -0700531 fClipTarget->clear(&maskSpaceIBounds,
532 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
533 true,
534 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000535
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000536 // When we use the stencil in the below loop it is important to have this clip installed.
537 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
538 // pass must not set values outside of this bounds or stencil values outside the rect won't be
539 // cleared.
joshualitt329bf482014-10-29 12:31:28 -0700540 GrDrawTarget::AutoClipRestore acr(fClipTarget, maskSpaceIBounds);
bsalomon427cf282014-10-16 13:41:43 -0700541 SkAutoTUnref<GrTexture> temp;
joshualitt9853cce2014-11-17 14:22:48 -0800542
robertphillips@google.comf294b772012-04-27 14:29:26 +0000543 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700544 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000545 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000546 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000547 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000548 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
joshualitt9853cce2014-11-17 14:22:48 -0800549 GrDrawState drawState(translate);
egdaniel87509242014-12-17 13:37:13 -0800550 drawState.enableState(GrDrawState::kClip_StateBit);
joshualitt9853cce2014-11-17 14:22:48 -0800551
robertphillips@google.come79f3202014-02-11 16:30:21 +0000552 GrPathRenderer* pr = NULL;
joshualitt9853cce2014-11-17 14:22:48 -0800553 bool useTemp = !this->canStencilAndDrawElement(&drawState, result, &pr, element);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000554 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000555 // 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 +0000556 // mask buffer can be substantially larger than the actually clip stack element. We
557 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000558 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000559 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000560
561 if (useTemp) {
562 if (invert) {
563 maskSpaceElementIBounds = maskSpaceIBounds;
564 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000565 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000566 elementBounds.offset(clipToMaskOffset);
567 elementBounds.roundOut(&maskSpaceElementIBounds);
568 }
569
bsalomon427cf282014-10-16 13:41:43 -0700570 if (!temp) {
571 temp.reset(this->createTempMask(maskSpaceIBounds.fRight,
572 maskSpaceIBounds.fBottom));
573 if (!temp) {
574 fAACache.reset();
575 return NULL;
576 }
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000577 }
bsalomon427cf282014-10-16 13:41:43 -0700578 dst = temp;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000579 // clear the temp target and set blend to replace
joshualitt329bf482014-10-29 12:31:28 -0700580 fClipTarget->clear(&maskSpaceElementIBounds,
joshualitt9853cce2014-11-17 14:22:48 -0800581 invert ? 0xffffffff : 0x00000000,
582 true,
583 dst->asRenderTarget());
egdaniel87509242014-12-17 13:37:13 -0800584 set_coverage_drawing_xpf(SkRegion::kReplace_Op, invert, &drawState);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000585 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000586 // draw directly into the result with the stencil set to make the pixels affected
587 // by the clip shape be non-zero.
588 dst = result;
589 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
590 kReplace_StencilOp,
591 kReplace_StencilOp,
592 kAlways_StencilFunc,
593 0xffff,
594 0xffff,
595 0xffff);
joshualitt9853cce2014-11-17 14:22:48 -0800596 drawState.setStencil(kStencilInElement);
egdaniel87509242014-12-17 13:37:13 -0800597 set_coverage_drawing_xpf(op, invert, &drawState);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000598 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000599
egdaniel87509242014-12-17 13:37:13 -0800600 if (!this->drawElement(&drawState, dst, element, pr)) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000601 fAACache.reset();
602 return NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000603 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000604
egdaniel87509242014-12-17 13:37:13 -0800605 GrDrawState backgroundDrawState(translate);
606 backgroundDrawState.enableState(GrDrawState::kClip_StateBit);
607 backgroundDrawState.setRenderTarget(result->asRenderTarget());
608
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000609 if (useTemp) {
610 // Now draw into the accumulator using the real operation and the temp buffer as a
611 // texture
egdaniel87509242014-12-17 13:37:13 -0800612 this->mergeMask(&backgroundDrawState,
joshualitt9853cce2014-11-17 14:22:48 -0800613 result,
bsalomon427cf282014-10-16 13:41:43 -0700614 temp,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000615 op,
616 maskSpaceIBounds,
617 maskSpaceElementIBounds);
618 } else {
egdaniel87509242014-12-17 13:37:13 -0800619 set_coverage_drawing_xpf(op, !invert, &backgroundDrawState);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000620 // Draw to the exterior pixels (those with a zero stencil value).
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000621 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
622 kZero_StencilOp,
623 kZero_StencilOp,
624 kEqual_StencilFunc,
625 0xffff,
626 0x0000,
627 0xffff);
egdaniel87509242014-12-17 13:37:13 -0800628 backgroundDrawState.setStencil(kDrawOutsideElement);
629 // The color passed in here does not matter since the coverageSetOpXP won't read it.
630 fClipTarget->drawSimpleRect(&backgroundDrawState, GrColor_WHITE, clipSpaceIBounds);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000631 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000632 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800633 GrDrawState drawState(translate);
egdaniel87509242014-12-17 13:37:13 -0800634 drawState.enableState(GrDrawState::kClip_StateBit);
joshualitt9853cce2014-11-17 14:22:48 -0800635
robertphillips@google.come79f3202014-02-11 16:30:21 +0000636 // all the remaining ops can just be directly draw into the accumulation buffer
egdaniel87509242014-12-17 13:37:13 -0800637 set_coverage_drawing_xpf(op, false, &drawState);
638 // The color passed in here does not matter since the coverageSetOpXP won't read it.
639 this->drawElement(&drawState, result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000640 }
641 }
642
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000643 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000644 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000645}
646
647////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000648// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000649// (as opposed to canvas) coordinates
joshualitt9853cce2014-11-17 14:22:48 -0800650bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt,
651 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700652 GrReducedClip::InitialState initialState,
653 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000654 const SkIRect& clipSpaceIBounds,
655 const SkIPoint& clipSpaceToStencilOffset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000656 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon49f085d2014-09-05 13:34:00 -0700657 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000658
659 // TODO: dynamically attach a SB when needed.
660 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
661 if (NULL == stencilBuffer) {
662 return false;
663 }
664
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000665 if (stencilBuffer->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000666 stencilBuffer->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000667 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
668 SkVector translate = {
669 SkIntToScalar(clipSpaceToStencilOffset.fX),
670 SkIntToScalar(clipSpaceToStencilOffset.fY)
671 };
672 SkMatrix matrix;
673 matrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000674
bsalomon@google.com9f131742012-12-13 20:43:56 +0000675 // We set the current clip to the bounds so that our recursive draws are scissored to them.
676 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
677 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
joshualitt329bf482014-10-29 12:31:28 -0700678 GrDrawTarget::AutoClipRestore acr(fClipTarget, stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000679
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000680 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000681 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000682 clipBit = (1 << (clipBit-1));
683
joshualitt329bf482014-10-29 12:31:28 -0700684 fClipTarget->clearStencilClip(stencilSpaceIBounds,
685 GrReducedClip::kAllIn_InitialState == initialState,
686 rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000687
688 // walk through each clip element and perform its set op
689 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700690 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000691 const Element* element = iter.get();
joshualitt9853cce2014-11-17 14:22:48 -0800692
693 GrDrawState drawState(matrix);
694 drawState.setRenderTarget(rt);
695 drawState.enableState(GrDrawState::kClip_StateBit);
696 drawState.enableState(GrDrawState::kNoColorWrites_StateBit);
697
698 // if the target is MSAA then we want MSAA enabled when the clip is soft
699 if (rt->isMultisampled()) {
700 drawState.setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
701 }
702
tomhudson@google.com8afae612012-08-14 15:03:35 +0000703 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000704 // enabled at bottom of loop
joshualitt7a6184f2014-10-29 18:29:27 -0700705 fClipMode = kIgnoreClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000706
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000707 // This will be used to determine whether the clip shape can be rendered into the
708 // stencil with arbitrary stencil settings.
709 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000710
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000711 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000712 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000713
robertphillips@google.come79f3202014-02-11 16:30:21 +0000714 GrPathRenderer* pr = NULL;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000715 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000716 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000717 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000718 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000719 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000720 element->asPath(&clipPath);
721 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000722 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000723 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000724 }
joshualitt9853cce2014-11-17 14:22:48 -0800725 pr = this->getContext()->getPathRenderer(fClipTarget,
726 &drawState,
727 clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000728 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000729 false,
730 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000731 &stencilSupport);
732 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000733 return false;
734 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000735 }
736
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000737 int passes;
738 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
739
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000740 bool canRenderDirectToStencil =
741 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000742 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000743 // fill rule, and set operation can
744 // we render the element directly to
745 // stencil bit used for clipping.
746 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
747 canRenderDirectToStencil,
748 clipBit,
749 fillInverted,
750 &passes,
751 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000752
753 // draw the element to the client stencil bits if necessary
754 if (!canDrawDirectToClip) {
755 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000756 kIncClamp_StencilOp,
757 kIncClamp_StencilOp,
758 kAlways_StencilFunc,
759 0xffff,
760 0x0000,
761 0xffff);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000762 if (Element::kRect_Type == element->getType()) {
joshualitt9853cce2014-11-17 14:22:48 -0800763 *drawState.stencil() = gDrawToStencil;
joshualitt2e3b3e32014-12-09 13:31:14 -0800764 fClipTarget->drawSimpleRect(&drawState, GrColor_WHITE, element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000765 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000766 if (!clipPath.isEmpty()) {
joshualitt9853cce2014-11-17 14:22:48 -0800767 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000768 if (canRenderDirectToStencil) {
joshualitt9853cce2014-11-17 14:22:48 -0800769 *drawState.stencil() = gDrawToStencil;
joshualitt2e3b3e32014-12-09 13:31:14 -0800770 pr->drawPath(fClipTarget, &drawState, GrColor_WHITE, clipPath, stroke,
771 false);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000772 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800773 pr->stencilPath(fClipTarget, &drawState, clipPath, stroke);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000774 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000775 }
776 }
777 }
778
779 // now we modify the clip bit by rendering either the clip
780 // element directly or a bounding rect of the entire clip.
joshualitt7a6184f2014-10-29 18:29:27 -0700781 fClipMode = kModifyClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000782 for (int p = 0; p < passes; ++p) {
joshualitt9853cce2014-11-17 14:22:48 -0800783 GrDrawState drawStateCopy(drawState);
784 *drawStateCopy.stencil() = stencilSettings[p];
785
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000786 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000787 if (Element::kRect_Type == element->getType()) {
joshualitt2e3b3e32014-12-09 13:31:14 -0800788 fClipTarget->drawSimpleRect(&drawStateCopy, GrColor_WHITE,
789 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000790 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800791 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
joshualitt2e3b3e32014-12-09 13:31:14 -0800792 pr->drawPath(fClipTarget, &drawStateCopy, GrColor_WHITE, clipPath, stroke, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000793 }
794 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000795 // The view matrix is setup to do clip space -> stencil space translation, so
796 // draw rect in clip space.
joshualitt2e3b3e32014-12-09 13:31:14 -0800797 fClipTarget->drawSimpleRect(&drawStateCopy, GrColor_WHITE,
798 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000799 }
800 }
801 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000802 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000803 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000804 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000805 fCurrClipMaskType = kStencil_ClipMaskType;
joshualitt7a6184f2014-10-29 18:29:27 -0700806 fClipMode = kRespectClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000807 return true;
808}
809
bsalomon@google.com411dad02012-06-05 20:24:20 +0000810// mapping of clip-respecting stencil funcs to normal stencil funcs
811// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000812static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000813 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
814 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
815 // In the Clip Funcs
816 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
817 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
818 kLess_StencilFunc, // kLessIfInClip_StencilFunc
819 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
820 // Special in the clip func that forces user's ref to be 0.
821 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
822 // make ref 0 and do normal nequal.
823 },
824 {// Stencil-Clipping is ENABLED
825 // In the Clip Funcs
826 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
827 // eq stencil clip bit, mask
828 // out user bits.
829
830 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
831 // add stencil bit to mask and ref
832
833 kLess_StencilFunc, // kLessIfInClip_StencilFunc
834 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
835 // for both of these we can add
836 // the clip bit to the mask and
837 // ref and compare as normal
838 // Special in the clip func that forces user's ref to be 0.
839 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
840 // make ref have only the clip bit set
841 // and make comparison be less
842 // 10..0 < 1..user_bits..
843 }
844};
845
bsalomon@google.coma3201942012-06-21 19:58:20 +0000846namespace {
847// Sets the settings to clip against the stencil buffer clip while ignoring the
848// client bits.
849const GrStencilSettings& basic_apply_stencil_clip_settings() {
850 // stencil settings to use when clip is in stencil
851 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
852 kKeep_StencilOp,
853 kKeep_StencilOp,
854 kAlwaysIfInClip_StencilFunc,
855 0x0000,
856 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000857 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000858 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
859}
860}
861
joshualitt9853cce2014-11-17 14:22:48 -0800862void GrClipMaskManager::setDrawStateStencil(GrDrawState* drawState,
863 GrDrawState::AutoRestoreStencil* ars) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000864 // We make two copies of the StencilSettings here (except in the early
865 // exit scenario. One copy from draw state to the stack var. Then another
866 // from the stack var to the gpu. We could make this class hold a ptr to
867 // GrGpu's fStencilSettings and eliminate the stack copy here.
868
bsalomon@google.coma3201942012-06-21 19:58:20 +0000869 // use stencil for clipping if clipping is enabled and the clip
870 // has been written into the stencil.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000871 GrStencilSettings settings;
joshualitt9853cce2014-11-17 14:22:48 -0800872
bsalomon@google.coma3201942012-06-21 19:58:20 +0000873 // The GrGpu client may not be using the stencil buffer but we may need to
874 // enable it in order to respect a stencil clip.
joshualitt9853cce2014-11-17 14:22:48 -0800875 if (drawState->getStencil().isDisabled()) {
joshualitt7a6184f2014-10-29 18:29:27 -0700876 if (GrClipMaskManager::kRespectClip_StencilClipMode == fClipMode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000877 settings = basic_apply_stencil_clip_settings();
878 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000879 return;
880 }
881 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800882 settings = drawState->getStencil();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000883 }
884
885 // TODO: dynamically attach a stencil buffer
886 int stencilBits = 0;
joshualitt9853cce2014-11-17 14:22:48 -0800887 GrStencilBuffer* stencilBuffer = drawState->getRenderTarget()->getStencilBuffer();
bsalomon49f085d2014-09-05 13:34:00 -0700888 if (stencilBuffer) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000889 stencilBits = stencilBuffer->bits();
890 }
891
joshualitt329bf482014-10-29 12:31:28 -0700892 SkASSERT(fClipTarget->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
893 SkASSERT(fClipTarget->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
joshualitt7a6184f2014-10-29 18:29:27 -0700894 this->adjustStencilParams(&settings, fClipMode, stencilBits);
joshualitt9853cce2014-11-17 14:22:48 -0800895 ars->set(drawState);
896 drawState->setStencil(settings);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000897}
898
899void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
900 StencilClipMode mode,
901 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000902 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000903
904 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000905 // We assume that this clip manager itself is drawing to the GrGpu and
906 // has already setup the correct values.
907 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000908 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000909
bsalomon@google.com411dad02012-06-05 20:24:20 +0000910 unsigned int clipBit = (1 << (stencilBitCnt - 1));
911 unsigned int userBits = clipBit - 1;
912
bsalomon@google.coma3201942012-06-21 19:58:20 +0000913 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
joshualitt329bf482014-10-29 12:31:28 -0700914 bool twoSided = fClipTarget->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000915
bsalomon@google.coma3201942012-06-21 19:58:20 +0000916 bool finished = false;
917 while (!finished) {
918 GrStencilFunc func = settings->func(face);
919 uint16_t writeMask = settings->writeMask(face);
920 uint16_t funcMask = settings->funcMask(face);
921 uint16_t funcRef = settings->funcRef(face);
922
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000923 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000924
925 writeMask &= userBits;
926
927 if (func >= kBasicStencilFuncCount) {
928 int respectClip = kRespectClip_StencilClipMode == mode;
929 if (respectClip) {
930 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000931 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000932 switch (func) {
933 case kAlwaysIfInClip_StencilFunc:
934 funcMask = clipBit;
935 funcRef = clipBit;
936 break;
937 case kEqualIfInClip_StencilFunc:
938 case kLessIfInClip_StencilFunc:
939 case kLEqualIfInClip_StencilFunc:
940 funcMask = (funcMask & userBits) | clipBit;
941 funcRef = (funcRef & userBits) | clipBit;
942 break;
943 case kNonZeroIfInClip_StencilFunc:
944 funcMask = (funcMask & userBits) | clipBit;
945 funcRef = clipBit;
946 break;
947 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000948 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +0000949 }
950 } else {
951 funcMask &= userBits;
952 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000953 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000954 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000955 gSpecialToBasicStencilFunc[respectClip];
956 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000957 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000958 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000959 funcMask &= userBits;
960 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000961 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000962
963 settings->setFunc(face, func);
964 settings->setWriteMask(face, writeMask);
965 settings->setFuncMask(face, funcMask);
966 settings->setFuncRef(face, funcRef);
967
968 if (GrStencilSettings::kFront_Face == face) {
969 face = GrStencilSettings::kBack_Face;
970 finished = !twoSided;
971 } else {
972 finished = true;
973 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000974 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000975 if (!twoSided) {
976 settings->copyFrontSettingsToBack();
977 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000978}
979
980////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000981GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000982 GrReducedClip::InitialState initialState,
983 const GrReducedClip::ElementList& elements,
984 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000985 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000986
krajcevskiad1dc582014-06-10 15:06:47 -0700987 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -0700988 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000989 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000990 }
991
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000992 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
993 // the top left corner of the resulting rect to the top left of the texture.
994 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
995
robertphillips@google.com2c756812012-05-22 20:28:23 +0000996 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000997
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000998 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000999 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
1000 SkIntToScalar(-clipSpaceIBounds.fTop));
joshualitt9853cce2014-11-17 14:22:48 -08001001
krajcevski71614ac2014-08-13 10:36:18 -07001002 helper.init(maskSpaceIBounds, &matrix, false);
tfarinabf54e492014-10-23 17:47:18 -07001003 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001004 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001005
tfarinabf54e492014-10-23 17:47:18 -07001006 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001007 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001008 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001009
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001010 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1011 // Intersect and reverse difference require modifying pixels outside of the geometry
1012 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1013 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1014 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001015 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001016 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001017 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001018 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001019 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001020 SkPath clipPath;
1021 element->asPath(&clipPath);
1022 clipPath.toggleInverseFillType();
1023 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001024 continue;
1025 }
1026
1027 // The other ops (union, xor, diff) only affect pixels inside
1028 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001029 if (Element::kRect_Type == element->getType()) {
1030 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1031 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001032 SkPath path;
1033 element->asPath(&path);
1034 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001035 }
1036 }
1037
krajcevskiad1dc582014-06-10 15:06:47 -07001038 // Allocate clip mask texture
1039 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, true);
1040 if (NULL == result) {
1041 fAACache.reset();
1042 return NULL;
1043 }
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001044 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001045
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001046 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001047 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001048}
1049
robertphillips@google.comf294b772012-04-27 14:29:26 +00001050////////////////////////////////////////////////////////////////////////////////
bsalomonc8dc1f72014-08-21 13:02:13 -07001051void GrClipMaskManager::purgeResources() {
1052 fAACache.purgeResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001053}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001054
joshualitt329bf482014-10-29 12:31:28 -07001055void GrClipMaskManager::setClipTarget(GrClipTarget* clipTarget) {
1056 fClipTarget = clipTarget;
1057 fAACache.setContext(clipTarget->getContext());
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001058}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001059
joshualitt9853cce2014-11-17 14:22:48 -08001060void GrClipMaskManager::adjustPathStencilParams(const GrStencilBuffer* stencilBuffer,
1061 GrStencilSettings* settings) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001062 // TODO: dynamically attach a stencil buffer
bsalomon49f085d2014-09-05 13:34:00 -07001063 if (stencilBuffer) {
joshualitt9853cce2014-11-17 14:22:48 -08001064 int stencilBits = stencilBuffer->bits();
joshualitt7a6184f2014-10-29 18:29:27 -07001065 this->adjustStencilParams(settings, fClipMode, stencilBits);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001066 }
1067}