blob: d663c7939a1049decabce29306aa5267350afff1 [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;
bsalomon@google.comc7818882013-03-20 19:19:53 +000039 // We want to use device coords to compute the texture coordinates. We set our matrix to be
40 // equal to the view matrix followed by an offset to the devBound, and then a scaling matrix to
41 // normalized coords. We apply this matrix to the vertex positions rather than local coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000042 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000043 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000044 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000045 mat.preConcat(drawState->getViewMatrix());
46
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000047 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000048 // This could be a long-lived effect that is cached with the alpha-mask.
joshualittb0a8a372014-09-23 09:50:21 -070049 drawState->addCoverageProcessor(
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000050 GrTextureDomainEffect::Create(result,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000051 mat,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000052 GrTextureDomain::MakeTexelDomain(result, domainTexels),
53 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +000054 GrTextureParams::kNone_FilterMode,
bsalomon@google.com77af6802013-10-02 13:04:56 +000055 kPosition_GrCoordSet))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000056}
57
robertphillips@google.come79f3202014-02-11 16:30:21 +000058bool path_needs_SW_renderer(GrContext* context,
joshualitt9853cce2014-11-17 14:22:48 -080059 const GrDrawTarget* gpu,
60 const GrDrawState* drawState,
robertphillips@google.come79f3202014-02-11 16:30:21 +000061 const SkPath& origPath,
62 const SkStrokeRec& stroke,
63 bool doAA) {
64 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
65 SkTCopyOnFirstWrite<SkPath> path(origPath);
66 if (path->isInverseFillType()) {
67 path.writable()->toggleInverseFillType();
68 }
69 // last (false) parameter disallows use of the SW path renderer
bsalomon@google.com45a15f52012-12-10 19:10:17 +000070 GrPathRendererChain::DrawType type = doAA ?
71 GrPathRendererChain::kColorAntiAlias_DrawType :
72 GrPathRendererChain::kColor_DrawType;
73
joshualitt9853cce2014-11-17 14:22:48 -080074 return NULL == context->getPathRenderer(gpu, drawState, *path, stroke, false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +000075}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000076}
77
robertphillips@google.comfa662942012-05-17 12:20:22 +000078/*
79 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
80 * will be used on any element. If so, it returns true to indicate that the
81 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
82 */
joshualitt9853cce2014-11-17 14:22:48 -080083bool GrClipMaskManager::useSWOnlyPath(const GrDrawState* drawState,
84 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000085 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000086 // a clip gets complex enough it can just be done in SW regardless
87 // of whether it would invoke the GrSoftwarePathRenderer.
sugoi@google.com5f74cf82012-12-17 21:16:45 +000088 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000089
tfarinabf54e492014-10-23 17:47:18 -070090 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000091 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000092 // rects can always be drawn directly w/o using the software path
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000093 // Skip rrects once we're drawing them directly.
94 if (Element::kRect_Type != element->getType()) {
95 SkPath path;
96 element->asPath(&path);
joshualitt9853cce2014-11-17 14:22:48 -080097 if (path_needs_SW_renderer(this->getContext(), fClipTarget, drawState, path, stroke,
joshualitt329bf482014-10-29 12:31:28 -070098 element->isAA())) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000099 return true;
100 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000101 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000102 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000103 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000104}
105
joshualitt9853cce2014-11-17 14:22:48 -0800106bool GrClipMaskManager::installClipEffects(GrDrawState* drawState,
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000107 GrDrawState::AutoRestoreEffects* are,
joshualitt9853cce2014-11-17 14:22:48 -0800108 const GrReducedClip::ElementList& elements,
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000109 const SkVector& clipToRTOffset,
mtklein217daa72014-07-02 12:55:21 -0700110 const SkRect* drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000111 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700112 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000113 boundsInClipSpace = *drawBounds;
114 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
115 }
116
117 are->set(drawState);
118 GrRenderTarget* rt = drawState->getRenderTarget();
tfarinabf54e492014-10-23 17:47:18 -0700119 GrReducedClip::ElementList::Iter iter(elements);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000120 bool failed = false;
bsalomon49f085d2014-09-05 13:34:00 -0700121 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000122 SkRegion::Op op = iter.get()->getOp();
123 bool invert;
124 bool skip = false;
125 switch (op) {
126 case SkRegion::kReplace_Op:
127 SkASSERT(iter.get() == elements.head());
128 // Fallthrough, handled same as intersect.
129 case SkRegion::kIntersect_Op:
130 invert = false;
bsalomon49f085d2014-09-05 13:34:00 -0700131 if (drawBounds && iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000132 skip = true;
133 }
134 break;
135 case SkRegion::kDifference_Op:
136 invert = true;
137 // We don't currently have a cheap test for whether a rect is fully outside an
138 // element's primitive, so don't attempt to set skip.
139 break;
140 default:
141 failed = true;
142 break;
143 }
144 if (failed) {
145 break;
146 }
147
148 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700149 GrPrimitiveEdgeType edgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000150 if (GR_AA_CLIP && iter.get()->isAA()) {
151 if (rt->isMultisampled()) {
mtklein217daa72014-07-02 12:55:21 -0700152 // Coverage based AA clips don't place nicely with MSAA.
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000153 failed = true;
154 break;
155 }
joshualittb0a8a372014-09-23 09:50:21 -0700156 edgeType =
157 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000158 } else {
joshualittb0a8a372014-09-23 09:50:21 -0700159 edgeType =
160 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000161 }
joshualittb0a8a372014-09-23 09:50:21 -0700162 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000163 switch (iter.get()->getType()) {
164 case SkClipStack::Element::kPath_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700165 fp.reset(GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000166 &clipToRTOffset));
167 break;
168 case SkClipStack::Element::kRRect_Type: {
169 SkRRect rrect = iter.get()->getRRect();
170 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
joshualittb0a8a372014-09-23 09:50:21 -0700171 fp.reset(GrRRectEffect::Create(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000172 break;
173 }
174 case SkClipStack::Element::kRect_Type: {
175 SkRect rect = iter.get()->getRect();
176 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
joshualittb0a8a372014-09-23 09:50:21 -0700177 fp.reset(GrConvexPolyEffect::Create(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000178 break;
179 }
180 default:
181 break;
182 }
joshualittb0a8a372014-09-23 09:50:21 -0700183 if (fp) {
joshualitt9853cce2014-11-17 14:22:48 -0800184 drawState->addCoverageProcessor(fp);
mtklein217daa72014-07-02 12:55:21 -0700185 } else {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000186 failed = true;
187 break;
188 }
189 }
mtklein217daa72014-07-02 12:55:21 -0700190 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000191 }
192
193 if (failed) {
194 are->set(NULL);
195 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000196 return !failed;
197}
198
robertphillips@google.comf294b772012-04-27 14:29:26 +0000199////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000200// sort out what kind of clip mask needs to be created: alpha, stencil,
201// scissor, or entirely software
joshualitt9853cce2014-11-17 14:22:48 -0800202bool GrClipMaskManager::setupClipping(GrDrawState* drawState,
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000203 GrDrawState::AutoRestoreEffects* are,
joshualitt77b13072014-10-27 14:51:01 -0700204 GrDrawState::AutoRestoreStencil* ars,
joshualitt9853cce2014-11-17 14:22:48 -0800205 ScissorState* scissorState,
206 const GrClipData* clipDataIn,
207 const SkRect* devBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000208 fCurrClipMaskType = kNone_ClipMaskType;
joshualitt7a6184f2014-10-29 18:29:27 -0700209 if (kRespectClip_StencilClipMode == fClipMode) {
210 fClipMode = kIgnoreClip_StencilClipMode;
211 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000212
tfarinabf54e492014-10-23 17:47:18 -0700213 GrReducedClip::ElementList elements(16);
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000214 int32_t genID;
tfarinabf54e492014-10-23 17:47:18 -0700215 GrReducedClip::InitialState initialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000216 SkIRect clipSpaceIBounds;
217 bool requiresAA;
joshualitt9853cce2014-11-17 14:22:48 -0800218 GrRenderTarget* rt = drawState->getRenderTarget();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000219
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000220 // GrDrawTarget should have filtered this for us
bsalomon49f085d2014-09-05 13:34:00 -0700221 SkASSERT(rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000222
223 bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000224 if (!ignoreClip) {
225 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
226 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
tfarinabf54e492014-10-23 17:47:18 -0700227 GrReducedClip::ReduceClipStack(*clipDataIn->fClipStack,
228 clipSpaceRTIBounds,
229 &elements,
230 &genID,
231 &initialState,
232 &clipSpaceIBounds,
233 &requiresAA);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000234 if (elements.isEmpty()) {
tfarinabf54e492014-10-23 17:47:18 -0700235 if (GrReducedClip::kAllIn_InitialState == initialState) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000236 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000237 } else {
238 return false;
239 }
240 }
241 }
242
243 if (ignoreClip) {
joshualitt9853cce2014-11-17 14:22:48 -0800244 this->setDrawStateStencil(drawState, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000245 return true;
246 }
247
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000248 // An element count of 4 was chosen because of the common pattern in Blink of:
249 // isect RR
250 // diff RR
251 // isect convex_poly
252 // isect convex_poly
253 // when drawing rounded div borders. This could probably be tuned based on a
254 // configuration's relative costs of switching RTs to generate a mask vs
255 // longer shaders.
256 if (elements.count() <= 4) {
257 SkVector clipToRTOffset = { SkIntToScalar(-clipDataIn->fOrigin.fX),
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000258 SkIntToScalar(-clipDataIn->fOrigin.fY) };
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000259 if (elements.isEmpty() ||
joshualitt9853cce2014-11-17 14:22:48 -0800260 (requiresAA && this->installClipEffects(drawState, are, elements, clipToRTOffset,
261 devBounds))) {
mtklein217daa72014-07-02 12:55:21 -0700262 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
263 scissorSpaceIBounds.offset(-clipDataIn->fOrigin);
264 if (NULL == devBounds ||
265 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
joshualitt77b13072014-10-27 14:51:01 -0700266 scissorState->set(scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000267 }
joshualitt9853cce2014-11-17 14:22:48 -0800268 this->setDrawStateStencil(drawState, ars);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000269 return true;
270 }
271 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000272
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000273#if GR_AA_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000274 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000275 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000276 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000277
joshualitt9853cce2014-11-17 14:22:48 -0800278 if (this->useSWOnlyPath(drawState, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000279 // The clip geometry is complex enough that it will be more efficient to create it
280 // entirely in software
281 result = this->createSoftwareClipMask(genID,
282 initialState,
283 elements,
284 clipSpaceIBounds);
285 } else {
286 result = this->createAlphaClipMask(genID,
287 initialState,
288 elements,
289 clipSpaceIBounds);
290 }
291
bsalomon49f085d2014-09-05 13:34:00 -0700292 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000293 // The mask's top left coord should be pinned to the rounded-out top left corner of
294 // clipSpace bounds. We determine the mask's position WRT to the render target here.
295 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
296 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
joshualitt9853cce2014-11-17 14:22:48 -0800297 setup_drawstate_aaclip(rtSpaceMaskBounds, drawState, result);
298 this->setDrawStateStencil(drawState, ars);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000299 return true;
300 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000301 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000302 }
303#endif // GR_AA_CLIP
304
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000305 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
306 // be created. In either case, free up the texture in the anti-aliased mask cache.
307 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
308 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
309 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000310 fAACache.reset();
311
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000312 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000313 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
joshualitt9853cce2014-11-17 14:22:48 -0800314 this->createStencilClipMask(rt,
315 genID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000316 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000317 elements,
318 clipSpaceIBounds,
319 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000320
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000321 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
322 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
323 // use both stencil and scissor test to the bounds for the final draw.
324 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
325 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
joshualitt77b13072014-10-27 14:51:01 -0700326 scissorState->set(scissorSpaceIBounds);
joshualitt9853cce2014-11-17 14:22:48 -0800327 this->setDrawStateStencil(drawState, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000328 return true;
329}
330
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000331namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000332////////////////////////////////////////////////////////////////////////////////
egdaniel87509242014-12-17 13:37:13 -0800333// Set a coverage drawing XPF on the drawState for the given op and invertCoverage mode
334void set_coverage_drawing_xpf(SkRegion::Op op, bool invertCoverage, GrDrawState* drawState) {
335 SkASSERT(op <= SkRegion::kLastOp);
336 drawState->setCoverageSetOpXPFactory(op, invertCoverage);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000337}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000338}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000339
340////////////////////////////////////////////////////////////////////////////////
joshualitt9853cce2014-11-17 14:22:48 -0800341bool GrClipMaskManager::drawElement(GrDrawState* drawState,
342 GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000343 const SkClipStack::Element* element,
344 GrPathRenderer* pr) {
joshualitt9853cce2014-11-17 14:22:48 -0800345 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000346
347 drawState->setRenderTarget(target->asRenderTarget());
348
egdaniel87509242014-12-17 13:37:13 -0800349 // The color we use to draw does not matter since we will always be using a GrCoverageSetOpXP
350 // which ignores color.
351 GrColor color = GrColor_WHITE;
352
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000353 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000354 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000355 case Element::kEmpty_Type:
356 SkDEBUGFAIL("Should never get here with an empty element.");
357 break;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000358 case Element::kRect_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700359 // TODO: Do rects directly to the accumulator using a aa-rect GrProcessor that covers
360 // the entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000361 if (element->isAA()) {
joshualitt329bf482014-10-29 12:31:28 -0700362 this->getContext()->getAARectRenderer()->fillAARect(fClipTarget,
joshualitt9853cce2014-11-17 14:22:48 -0800363 drawState,
joshualitt2e3b3e32014-12-09 13:31:14 -0800364 color,
joshualitta58fe352014-10-27 08:39:00 -0700365 element->getRect(),
366 SkMatrix::I(),
367 element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000368 } else {
joshualitt2e3b3e32014-12-09 13:31:14 -0800369 fClipTarget->drawSimpleRect(drawState, color, element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000370 }
371 return true;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000372 default: {
373 SkPath path;
374 element->asPath(&path);
jvanverthb3eb6872014-10-24 07:12:51 -0700375 path.setIsVolatile(true);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000376 if (path.isInverseFillType()) {
377 path.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000378 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000379 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000380 if (NULL == pr) {
381 GrPathRendererChain::DrawType type;
382 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
383 GrPathRendererChain::kColor_DrawType;
joshualitt9853cce2014-11-17 14:22:48 -0800384 pr = this->getContext()->getPathRenderer(fClipTarget, drawState, path, stroke,
385 false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000386 }
387 if (NULL == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000388 return false;
389 }
joshualitt9853cce2014-11-17 14:22:48 -0800390
joshualitt2e3b3e32014-12-09 13:31:14 -0800391 pr->drawPath(fClipTarget, drawState, color, path, stroke, element->isAA());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000392 break;
393 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000394 }
395 return true;
396}
397
joshualitt9853cce2014-11-17 14:22:48 -0800398bool GrClipMaskManager::canStencilAndDrawElement(GrDrawState* drawState,
399 GrTexture* target,
400 GrPathRenderer** pr,
401 const SkClipStack::Element* element) {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000402 drawState->setRenderTarget(target->asRenderTarget());
403
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000404 if (Element::kRect_Type == element->getType()) {
405 return true;
406 } else {
407 // We shouldn't get here with an empty clip element.
408 SkASSERT(Element::kEmpty_Type != element->getType());
409 SkPath path;
410 element->asPath(&path);
411 if (path.isInverseFillType()) {
412 path.toggleInverseFillType();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000413 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000414 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
415 GrPathRendererChain::DrawType type = element->isAA() ?
416 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
417 GrPathRendererChain::kStencilAndColor_DrawType;
joshualitt9853cce2014-11-17 14:22:48 -0800418 *pr = this->getContext()->getPathRenderer(fClipTarget, drawState, path, stroke, false,
419 type);
bsalomon49f085d2014-09-05 13:34:00 -0700420 return SkToBool(*pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000421 }
422}
423
joshualitt9853cce2014-11-17 14:22:48 -0800424void GrClipMaskManager::mergeMask(GrDrawState* drawState,
425 GrTexture* dstMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000426 GrTexture* srcMask,
427 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000428 const SkIRect& dstBound,
429 const SkIRect& srcBound) {
joshualitt9853cce2014-11-17 14:22:48 -0800430 SkAssertResult(drawState->setIdentityViewMatrix());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000431
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000432 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000433
egdaniel87509242014-12-17 13:37:13 -0800434 // We want to invert the coverage here
435 set_coverage_drawing_xpf(op, false, drawState);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000436
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000437 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000438 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000439
egdaniel87509242014-12-17 13:37:13 -0800440 drawState->addCoverageProcessor(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000441 GrTextureDomainEffect::Create(srcMask,
442 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000443 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
444 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000445 GrTextureParams::kNone_FilterMode))->unref();
egdaniel87509242014-12-17 13:37:13 -0800446 // The color passed in here does not matter since the coverageSetOpXP won't read it.
joshualitt2e3b3e32014-12-09 13:31:14 -0800447 fClipTarget->drawSimpleRect(drawState, GrColor_WHITE, SkRect::Make(dstBound));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000448}
449
bsalomon427cf282014-10-16 13:41:43 -0700450GrTexture* GrClipMaskManager::createTempMask(int width, int height) {
bsalomonf2703d82014-10-28 14:33:06 -0700451 GrSurfaceDesc desc;
452 desc.fFlags = kRenderTarget_GrSurfaceFlag|kNoStencil_GrSurfaceFlag;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000453 desc.fWidth = width;
454 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000455 desc.fConfig = kAlpha_8_GrPixelConfig;
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
egdaniel87509242014-12-17 13:37:13 -0800607 GrDrawState backgroundDrawState(translate);
608 backgroundDrawState.enableState(GrDrawState::kClip_StateBit);
609 backgroundDrawState.setRenderTarget(result->asRenderTarget());
610
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000611 if (useTemp) {
612 // 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 {
egdaniel87509242014-12-17 13:37:13 -0800621 set_coverage_drawing_xpf(op, !invert, &backgroundDrawState);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000622 // Draw to the exterior pixels (those with a zero stencil value).
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000623 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
624 kZero_StencilOp,
625 kZero_StencilOp,
626 kEqual_StencilFunc,
627 0xffff,
628 0x0000,
629 0xffff);
egdaniel87509242014-12-17 13:37:13 -0800630 backgroundDrawState.setStencil(kDrawOutsideElement);
631 // The color passed in here does not matter since the coverageSetOpXP won't read it.
632 fClipTarget->drawSimpleRect(&backgroundDrawState, GrColor_WHITE, clipSpaceIBounds);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000633 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000634 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800635 GrDrawState drawState(translate);
egdaniel87509242014-12-17 13:37:13 -0800636 drawState.enableState(GrDrawState::kClip_StateBit);
joshualitt9853cce2014-11-17 14:22:48 -0800637
robertphillips@google.come79f3202014-02-11 16:30:21 +0000638 // all the remaining ops can just be directly draw into the accumulation buffer
egdaniel87509242014-12-17 13:37:13 -0800639 set_coverage_drawing_xpf(op, false, &drawState);
640 // The color passed in here does not matter since the coverageSetOpXP won't read it.
641 this->drawElement(&drawState, result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000642 }
643 }
644
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000645 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000646 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000647}
648
649////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000650// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000651// (as opposed to canvas) coordinates
joshualitt9853cce2014-11-17 14:22:48 -0800652bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt,
653 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700654 GrReducedClip::InitialState initialState,
655 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000656 const SkIRect& clipSpaceIBounds,
657 const SkIPoint& clipSpaceToStencilOffset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000658 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon49f085d2014-09-05 13:34:00 -0700659 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000660
661 // TODO: dynamically attach a SB when needed.
662 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
663 if (NULL == stencilBuffer) {
664 return false;
665 }
666
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000667 if (stencilBuffer->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000668 stencilBuffer->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000669 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
670 SkVector translate = {
671 SkIntToScalar(clipSpaceToStencilOffset.fX),
672 SkIntToScalar(clipSpaceToStencilOffset.fY)
673 };
674 SkMatrix matrix;
675 matrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000676
bsalomon@google.com9f131742012-12-13 20:43:56 +0000677 // We set the current clip to the bounds so that our recursive draws are scissored to them.
678 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
679 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
joshualitt329bf482014-10-29 12:31:28 -0700680 GrDrawTarget::AutoClipRestore acr(fClipTarget, stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000681
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000682 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000683 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000684 clipBit = (1 << (clipBit-1));
685
joshualitt329bf482014-10-29 12:31:28 -0700686 fClipTarget->clearStencilClip(stencilSpaceIBounds,
687 GrReducedClip::kAllIn_InitialState == initialState,
688 rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000689
690 // walk through each clip element and perform its set op
691 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700692 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000693 const Element* element = iter.get();
joshualitt9853cce2014-11-17 14:22:48 -0800694
695 GrDrawState drawState(matrix);
696 drawState.setRenderTarget(rt);
697 drawState.enableState(GrDrawState::kClip_StateBit);
698 drawState.enableState(GrDrawState::kNoColorWrites_StateBit);
699
700 // if the target is MSAA then we want MSAA enabled when the clip is soft
701 if (rt->isMultisampled()) {
702 drawState.setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
703 }
704
tomhudson@google.com8afae612012-08-14 15:03:35 +0000705 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000706 // enabled at bottom of loop
joshualitt7a6184f2014-10-29 18:29:27 -0700707 fClipMode = kIgnoreClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000708
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000709 // This will be used to determine whether the clip shape can be rendered into the
710 // stencil with arbitrary stencil settings.
711 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000712
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000713 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000714 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000715
robertphillips@google.come79f3202014-02-11 16:30:21 +0000716 GrPathRenderer* pr = NULL;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000717 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000718 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000719 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000720 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000721 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000722 element->asPath(&clipPath);
723 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000724 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000725 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000726 }
joshualitt9853cce2014-11-17 14:22:48 -0800727 pr = this->getContext()->getPathRenderer(fClipTarget,
728 &drawState,
729 clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000730 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000731 false,
732 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000733 &stencilSupport);
734 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000735 return false;
736 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000737 }
738
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000739 int passes;
740 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
741
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000742 bool canRenderDirectToStencil =
743 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000744 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000745 // fill rule, and set operation can
746 // we render the element directly to
747 // stencil bit used for clipping.
748 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
749 canRenderDirectToStencil,
750 clipBit,
751 fillInverted,
752 &passes,
753 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000754
755 // draw the element to the client stencil bits if necessary
756 if (!canDrawDirectToClip) {
757 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000758 kIncClamp_StencilOp,
759 kIncClamp_StencilOp,
760 kAlways_StencilFunc,
761 0xffff,
762 0x0000,
763 0xffff);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000764 if (Element::kRect_Type == element->getType()) {
joshualitt9853cce2014-11-17 14:22:48 -0800765 *drawState.stencil() = gDrawToStencil;
joshualitt2e3b3e32014-12-09 13:31:14 -0800766 fClipTarget->drawSimpleRect(&drawState, GrColor_WHITE, element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000767 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000768 if (!clipPath.isEmpty()) {
joshualitt9853cce2014-11-17 14:22:48 -0800769 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000770 if (canRenderDirectToStencil) {
joshualitt9853cce2014-11-17 14:22:48 -0800771 *drawState.stencil() = gDrawToStencil;
joshualitt2e3b3e32014-12-09 13:31:14 -0800772 pr->drawPath(fClipTarget, &drawState, GrColor_WHITE, clipPath, stroke,
773 false);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000774 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800775 pr->stencilPath(fClipTarget, &drawState, clipPath, stroke);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000776 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000777 }
778 }
779 }
780
781 // now we modify the clip bit by rendering either the clip
782 // element directly or a bounding rect of the entire clip.
joshualitt7a6184f2014-10-29 18:29:27 -0700783 fClipMode = kModifyClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000784 for (int p = 0; p < passes; ++p) {
joshualitt9853cce2014-11-17 14:22:48 -0800785 GrDrawState drawStateCopy(drawState);
786 *drawStateCopy.stencil() = stencilSettings[p];
787
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000788 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000789 if (Element::kRect_Type == element->getType()) {
joshualitt2e3b3e32014-12-09 13:31:14 -0800790 fClipTarget->drawSimpleRect(&drawStateCopy, GrColor_WHITE,
791 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000792 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800793 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
joshualitt2e3b3e32014-12-09 13:31:14 -0800794 pr->drawPath(fClipTarget, &drawStateCopy, GrColor_WHITE, clipPath, stroke, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000795 }
796 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000797 // The view matrix is setup to do clip space -> stencil space translation, so
798 // draw rect in clip space.
joshualitt2e3b3e32014-12-09 13:31:14 -0800799 fClipTarget->drawSimpleRect(&drawStateCopy, GrColor_WHITE,
800 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000801 }
802 }
803 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000804 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000805 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000806 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000807 fCurrClipMaskType = kStencil_ClipMaskType;
joshualitt7a6184f2014-10-29 18:29:27 -0700808 fClipMode = kRespectClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000809 return true;
810}
811
bsalomon@google.com411dad02012-06-05 20:24:20 +0000812// mapping of clip-respecting stencil funcs to normal stencil funcs
813// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000814static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000815 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
816 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
817 // In the Clip Funcs
818 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
819 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
820 kLess_StencilFunc, // kLessIfInClip_StencilFunc
821 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
822 // Special in the clip func that forces user's ref to be 0.
823 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
824 // make ref 0 and do normal nequal.
825 },
826 {// Stencil-Clipping is ENABLED
827 // In the Clip Funcs
828 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
829 // eq stencil clip bit, mask
830 // out user bits.
831
832 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
833 // add stencil bit to mask and ref
834
835 kLess_StencilFunc, // kLessIfInClip_StencilFunc
836 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
837 // for both of these we can add
838 // the clip bit to the mask and
839 // ref and compare as normal
840 // Special in the clip func that forces user's ref to be 0.
841 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
842 // make ref have only the clip bit set
843 // and make comparison be less
844 // 10..0 < 1..user_bits..
845 }
846};
847
bsalomon@google.coma3201942012-06-21 19:58:20 +0000848namespace {
849// Sets the settings to clip against the stencil buffer clip while ignoring the
850// client bits.
851const GrStencilSettings& basic_apply_stencil_clip_settings() {
852 // stencil settings to use when clip is in stencil
853 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
854 kKeep_StencilOp,
855 kKeep_StencilOp,
856 kAlwaysIfInClip_StencilFunc,
857 0x0000,
858 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000859 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000860 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
861}
862}
863
joshualitt9853cce2014-11-17 14:22:48 -0800864void GrClipMaskManager::setDrawStateStencil(GrDrawState* drawState,
865 GrDrawState::AutoRestoreStencil* ars) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000866 // We make two copies of the StencilSettings here (except in the early
867 // exit scenario. One copy from draw state to the stack var. Then another
868 // from the stack var to the gpu. We could make this class hold a ptr to
869 // GrGpu's fStencilSettings and eliminate the stack copy here.
870
bsalomon@google.coma3201942012-06-21 19:58:20 +0000871 // use stencil for clipping if clipping is enabled and the clip
872 // has been written into the stencil.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000873 GrStencilSettings settings;
joshualitt9853cce2014-11-17 14:22:48 -0800874
bsalomon@google.coma3201942012-06-21 19:58:20 +0000875 // The GrGpu client may not be using the stencil buffer but we may need to
876 // enable it in order to respect a stencil clip.
joshualitt9853cce2014-11-17 14:22:48 -0800877 if (drawState->getStencil().isDisabled()) {
joshualitt7a6184f2014-10-29 18:29:27 -0700878 if (GrClipMaskManager::kRespectClip_StencilClipMode == fClipMode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000879 settings = basic_apply_stencil_clip_settings();
880 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000881 return;
882 }
883 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800884 settings = drawState->getStencil();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000885 }
886
887 // TODO: dynamically attach a stencil buffer
888 int stencilBits = 0;
joshualitt9853cce2014-11-17 14:22:48 -0800889 GrStencilBuffer* stencilBuffer = drawState->getRenderTarget()->getStencilBuffer();
bsalomon49f085d2014-09-05 13:34:00 -0700890 if (stencilBuffer) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000891 stencilBits = stencilBuffer->bits();
892 }
893
joshualitt329bf482014-10-29 12:31:28 -0700894 SkASSERT(fClipTarget->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
895 SkASSERT(fClipTarget->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
joshualitt7a6184f2014-10-29 18:29:27 -0700896 this->adjustStencilParams(&settings, fClipMode, stencilBits);
joshualitt9853cce2014-11-17 14:22:48 -0800897 ars->set(drawState);
898 drawState->setStencil(settings);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000899}
900
901void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
902 StencilClipMode mode,
903 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000904 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000905
906 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000907 // We assume that this clip manager itself is drawing to the GrGpu and
908 // has already setup the correct values.
909 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000910 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000911
bsalomon@google.com411dad02012-06-05 20:24:20 +0000912 unsigned int clipBit = (1 << (stencilBitCnt - 1));
913 unsigned int userBits = clipBit - 1;
914
bsalomon@google.coma3201942012-06-21 19:58:20 +0000915 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
joshualitt329bf482014-10-29 12:31:28 -0700916 bool twoSided = fClipTarget->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000917
bsalomon@google.coma3201942012-06-21 19:58:20 +0000918 bool finished = false;
919 while (!finished) {
920 GrStencilFunc func = settings->func(face);
921 uint16_t writeMask = settings->writeMask(face);
922 uint16_t funcMask = settings->funcMask(face);
923 uint16_t funcRef = settings->funcRef(face);
924
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000925 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000926
927 writeMask &= userBits;
928
929 if (func >= kBasicStencilFuncCount) {
930 int respectClip = kRespectClip_StencilClipMode == mode;
931 if (respectClip) {
932 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000933 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000934 switch (func) {
935 case kAlwaysIfInClip_StencilFunc:
936 funcMask = clipBit;
937 funcRef = clipBit;
938 break;
939 case kEqualIfInClip_StencilFunc:
940 case kLessIfInClip_StencilFunc:
941 case kLEqualIfInClip_StencilFunc:
942 funcMask = (funcMask & userBits) | clipBit;
943 funcRef = (funcRef & userBits) | clipBit;
944 break;
945 case kNonZeroIfInClip_StencilFunc:
946 funcMask = (funcMask & userBits) | clipBit;
947 funcRef = clipBit;
948 break;
949 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000950 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +0000951 }
952 } else {
953 funcMask &= userBits;
954 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000955 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000956 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000957 gSpecialToBasicStencilFunc[respectClip];
958 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000959 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000960 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000961 funcMask &= userBits;
962 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000963 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000964
965 settings->setFunc(face, func);
966 settings->setWriteMask(face, writeMask);
967 settings->setFuncMask(face, funcMask);
968 settings->setFuncRef(face, funcRef);
969
970 if (GrStencilSettings::kFront_Face == face) {
971 face = GrStencilSettings::kBack_Face;
972 finished = !twoSided;
973 } else {
974 finished = true;
975 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000976 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000977 if (!twoSided) {
978 settings->copyFrontSettingsToBack();
979 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000980}
981
982////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000983GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000984 GrReducedClip::InitialState initialState,
985 const GrReducedClip::ElementList& elements,
986 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000987 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000988
krajcevskiad1dc582014-06-10 15:06:47 -0700989 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -0700990 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000991 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000992 }
993
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000994 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
995 // the top left corner of the resulting rect to the top left of the texture.
996 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
997
robertphillips@google.com2c756812012-05-22 20:28:23 +0000998 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000999
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001000 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001001 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
1002 SkIntToScalar(-clipSpaceIBounds.fTop));
joshualitt9853cce2014-11-17 14:22:48 -08001003
krajcevski71614ac2014-08-13 10:36:18 -07001004 helper.init(maskSpaceIBounds, &matrix, false);
tfarinabf54e492014-10-23 17:47:18 -07001005 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001006 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001007
tfarinabf54e492014-10-23 17:47:18 -07001008 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001009 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001010 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001011
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001012 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1013 // Intersect and reverse difference require modifying pixels outside of the geometry
1014 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1015 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1016 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001017 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001018 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001019 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001020 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001021 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001022 SkPath clipPath;
1023 element->asPath(&clipPath);
1024 clipPath.toggleInverseFillType();
1025 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001026 continue;
1027 }
1028
1029 // The other ops (union, xor, diff) only affect pixels inside
1030 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001031 if (Element::kRect_Type == element->getType()) {
1032 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1033 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001034 SkPath path;
1035 element->asPath(&path);
1036 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001037 }
1038 }
1039
krajcevskiad1dc582014-06-10 15:06:47 -07001040 // Allocate clip mask texture
1041 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, true);
1042 if (NULL == result) {
1043 fAACache.reset();
1044 return NULL;
1045 }
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001046 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001047
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001048 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001049 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001050}
1051
robertphillips@google.comf294b772012-04-27 14:29:26 +00001052////////////////////////////////////////////////////////////////////////////////
bsalomonc8dc1f72014-08-21 13:02:13 -07001053void GrClipMaskManager::purgeResources() {
1054 fAACache.purgeResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001055}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001056
joshualitt329bf482014-10-29 12:31:28 -07001057void GrClipMaskManager::setClipTarget(GrClipTarget* clipTarget) {
1058 fClipTarget = clipTarget;
1059 fAACache.setContext(clipTarget->getContext());
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001060}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001061
joshualitt9853cce2014-11-17 14:22:48 -08001062void GrClipMaskManager::adjustPathStencilParams(const GrStencilBuffer* stencilBuffer,
1063 GrStencilSettings* settings) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001064 // TODO: dynamically attach a stencil buffer
bsalomon49f085d2014-09-05 13:34:00 -07001065 if (stencilBuffer) {
joshualitt9853cce2014-11-17 14:22:48 -08001066 int stencilBits = stencilBuffer->bits();
joshualitt7a6184f2014-10-29 18:29:27 -07001067 this->adjustStencilParams(settings, fClipMode, stencilBits);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001068 }
1069}