blob: f86aa9de29f7e1eb649972d4b88179505ab1c614 [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"
egdaniel7c663422014-12-08 11:20:39 -080021#include "effects/GrTextureDomain.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080022#include "effects/GrConvexPolyEffect.h"
23#include "effects/GrRRectEffect.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000024
robertphillips@google.comba998f22012-10-12 11:33:56 +000025#define GR_AA_CLIP 1
bsalomon@google.com8182fa02012-12-04 14:06:06 +000026typedef SkClipStack::Element Element;
bsalomon@google.com51a62862012-11-26 21:19:43 +000027
28////////////////////////////////////////////////////////////////////////////////
robertphillips@google.come79f3202014-02-11 16:30:21 +000029namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000030// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000031// stage matrix this also alters the vertex layout
joshualitt9853cce2014-11-17 14:22:48 -080032void setup_drawstate_aaclip(const SkIRect &devBound,
33 GrDrawState* drawState,
34 GrTexture* result) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000035 SkASSERT(drawState);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000036
bsalomon@google.comb9086a02012-11-01 18:02:54 +000037 SkMatrix mat;
bsalomon@google.comc7818882013-03-20 19:19:53 +000038 // We want to use device coords to compute the texture coordinates. We set our matrix to be
39 // equal to the view matrix followed by an offset to the devBound, and then a scaling matrix to
40 // normalized coords. We apply this matrix to the vertex positions rather than local 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 mat.preConcat(drawState->getViewMatrix());
45
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000046 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000047 // This could be a long-lived effect that is cached with the alpha-mask.
joshualittb0a8a372014-09-23 09:50:21 -070048 drawState->addCoverageProcessor(
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000049 GrTextureDomainEffect::Create(result,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000050 mat,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000051 GrTextureDomain::MakeTexelDomain(result, domainTexels),
52 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +000053 GrTextureParams::kNone_FilterMode,
bsalomon@google.com77af6802013-10-02 13:04:56 +000054 kPosition_GrCoordSet))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000055}
56
robertphillips@google.come79f3202014-02-11 16:30:21 +000057bool path_needs_SW_renderer(GrContext* context,
joshualitt9853cce2014-11-17 14:22:48 -080058 const GrDrawTarget* gpu,
59 const GrDrawState* drawState,
robertphillips@google.come79f3202014-02-11 16:30:21 +000060 const SkPath& origPath,
61 const SkStrokeRec& stroke,
62 bool doAA) {
63 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
64 SkTCopyOnFirstWrite<SkPath> path(origPath);
65 if (path->isInverseFillType()) {
66 path.writable()->toggleInverseFillType();
67 }
68 // last (false) parameter disallows use of the SW path renderer
bsalomon@google.com45a15f52012-12-10 19:10:17 +000069 GrPathRendererChain::DrawType type = doAA ?
70 GrPathRendererChain::kColorAntiAlias_DrawType :
71 GrPathRendererChain::kColor_DrawType;
72
joshualitt9853cce2014-11-17 14:22:48 -080073 return NULL == context->getPathRenderer(gpu, drawState, *path, stroke, false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +000074}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000075}
76
robertphillips@google.comfa662942012-05-17 12:20:22 +000077/*
78 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
79 * will be used on any element. If so, it returns true to indicate that the
80 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
81 */
joshualitt9853cce2014-11-17 14:22:48 -080082bool GrClipMaskManager::useSWOnlyPath(const GrDrawState* drawState,
83 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000084 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000085 // a clip gets complex enough it can just be done in SW regardless
86 // of whether it would invoke the GrSoftwarePathRenderer.
sugoi@google.com5f74cf82012-12-17 21:16:45 +000087 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000088
tfarinabf54e492014-10-23 17:47:18 -070089 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000090 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000091 // rects can always be drawn directly w/o using the software path
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000092 // Skip rrects once we're drawing them directly.
93 if (Element::kRect_Type != element->getType()) {
94 SkPath path;
95 element->asPath(&path);
joshualitt9853cce2014-11-17 14:22:48 -080096 if (path_needs_SW_renderer(this->getContext(), fClipTarget, drawState, path, stroke,
joshualitt329bf482014-10-29 12:31:28 -070097 element->isAA())) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000098 return true;
99 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000100 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000101 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000102 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000103}
104
joshualitt9853cce2014-11-17 14:22:48 -0800105bool GrClipMaskManager::installClipEffects(GrDrawState* drawState,
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000106 GrDrawState::AutoRestoreEffects* are,
joshualitt9853cce2014-11-17 14:22:48 -0800107 const GrReducedClip::ElementList& elements,
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000108 const SkVector& clipToRTOffset,
mtklein217daa72014-07-02 12:55:21 -0700109 const SkRect* drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000110 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700111 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000112 boundsInClipSpace = *drawBounds;
113 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
114 }
115
116 are->set(drawState);
117 GrRenderTarget* rt = drawState->getRenderTarget();
tfarinabf54e492014-10-23 17:47:18 -0700118 GrReducedClip::ElementList::Iter iter(elements);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000119 bool failed = false;
bsalomon49f085d2014-09-05 13:34:00 -0700120 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000121 SkRegion::Op op = iter.get()->getOp();
122 bool invert;
123 bool skip = false;
124 switch (op) {
125 case SkRegion::kReplace_Op:
126 SkASSERT(iter.get() == elements.head());
127 // Fallthrough, handled same as intersect.
128 case SkRegion::kIntersect_Op:
129 invert = false;
bsalomon49f085d2014-09-05 13:34:00 -0700130 if (drawBounds && iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000131 skip = true;
132 }
133 break;
134 case SkRegion::kDifference_Op:
135 invert = true;
136 // We don't currently have a cheap test for whether a rect is fully outside an
137 // element's primitive, so don't attempt to set skip.
138 break;
139 default:
140 failed = true;
141 break;
142 }
143 if (failed) {
144 break;
145 }
146
147 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700148 GrPrimitiveEdgeType edgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000149 if (GR_AA_CLIP && iter.get()->isAA()) {
150 if (rt->isMultisampled()) {
mtklein217daa72014-07-02 12:55:21 -0700151 // Coverage based AA clips don't place nicely with MSAA.
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000152 failed = true;
153 break;
154 }
joshualittb0a8a372014-09-23 09:50:21 -0700155 edgeType =
156 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000157 } else {
joshualittb0a8a372014-09-23 09:50:21 -0700158 edgeType =
159 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000160 }
joshualittb0a8a372014-09-23 09:50:21 -0700161 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000162 switch (iter.get()->getType()) {
163 case SkClipStack::Element::kPath_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700164 fp.reset(GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000165 &clipToRTOffset));
166 break;
167 case SkClipStack::Element::kRRect_Type: {
168 SkRRect rrect = iter.get()->getRRect();
169 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
joshualittb0a8a372014-09-23 09:50:21 -0700170 fp.reset(GrRRectEffect::Create(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000171 break;
172 }
173 case SkClipStack::Element::kRect_Type: {
174 SkRect rect = iter.get()->getRect();
175 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
joshualittb0a8a372014-09-23 09:50:21 -0700176 fp.reset(GrConvexPolyEffect::Create(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000177 break;
178 }
179 default:
180 break;
181 }
joshualittb0a8a372014-09-23 09:50:21 -0700182 if (fp) {
joshualitt9853cce2014-11-17 14:22:48 -0800183 drawState->addCoverageProcessor(fp);
mtklein217daa72014-07-02 12:55:21 -0700184 } else {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000185 failed = true;
186 break;
187 }
188 }
mtklein217daa72014-07-02 12:55:21 -0700189 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000190 }
191
192 if (failed) {
193 are->set(NULL);
194 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000195 return !failed;
196}
197
robertphillips@google.comf294b772012-04-27 14:29:26 +0000198////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000199// sort out what kind of clip mask needs to be created: alpha, stencil,
200// scissor, or entirely software
joshualitt9853cce2014-11-17 14:22:48 -0800201bool GrClipMaskManager::setupClipping(GrDrawState* drawState,
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000202 GrDrawState::AutoRestoreEffects* are,
joshualitt77b13072014-10-27 14:51:01 -0700203 GrDrawState::AutoRestoreStencil* ars,
joshualitt9853cce2014-11-17 14:22:48 -0800204 ScissorState* scissorState,
205 const GrClipData* clipDataIn,
206 const SkRect* devBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000207 fCurrClipMaskType = kNone_ClipMaskType;
joshualitt7a6184f2014-10-29 18:29:27 -0700208 if (kRespectClip_StencilClipMode == fClipMode) {
209 fClipMode = kIgnoreClip_StencilClipMode;
210 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000211
tfarinabf54e492014-10-23 17:47:18 -0700212 GrReducedClip::ElementList elements(16);
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000213 int32_t genID;
tfarinabf54e492014-10-23 17:47:18 -0700214 GrReducedClip::InitialState initialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000215 SkIRect clipSpaceIBounds;
216 bool requiresAA;
joshualitt9853cce2014-11-17 14:22:48 -0800217 GrRenderTarget* rt = drawState->getRenderTarget();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000218
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000219 // GrDrawTarget should have filtered this for us
bsalomon49f085d2014-09-05 13:34:00 -0700220 SkASSERT(rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000221
222 bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000223 if (!ignoreClip) {
224 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
225 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
tfarinabf54e492014-10-23 17:47:18 -0700226 GrReducedClip::ReduceClipStack(*clipDataIn->fClipStack,
227 clipSpaceRTIBounds,
228 &elements,
229 &genID,
230 &initialState,
231 &clipSpaceIBounds,
232 &requiresAA);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000233 if (elements.isEmpty()) {
tfarinabf54e492014-10-23 17:47:18 -0700234 if (GrReducedClip::kAllIn_InitialState == initialState) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000235 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000236 } else {
237 return false;
238 }
239 }
240 }
241
242 if (ignoreClip) {
joshualitt9853cce2014-11-17 14:22:48 -0800243 this->setDrawStateStencil(drawState, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000244 return true;
245 }
246
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000247 // An element count of 4 was chosen because of the common pattern in Blink of:
248 // isect RR
249 // diff RR
250 // isect convex_poly
251 // isect convex_poly
252 // when drawing rounded div borders. This could probably be tuned based on a
253 // configuration's relative costs of switching RTs to generate a mask vs
254 // longer shaders.
255 if (elements.count() <= 4) {
256 SkVector clipToRTOffset = { SkIntToScalar(-clipDataIn->fOrigin.fX),
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000257 SkIntToScalar(-clipDataIn->fOrigin.fY) };
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000258 if (elements.isEmpty() ||
joshualitt9853cce2014-11-17 14:22:48 -0800259 (requiresAA && this->installClipEffects(drawState, are, elements, clipToRTOffset,
260 devBounds))) {
mtklein217daa72014-07-02 12:55:21 -0700261 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
262 scissorSpaceIBounds.offset(-clipDataIn->fOrigin);
263 if (NULL == devBounds ||
264 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
joshualitt77b13072014-10-27 14:51:01 -0700265 scissorState->set(scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000266 }
joshualitt9853cce2014-11-17 14:22:48 -0800267 this->setDrawStateStencil(drawState, ars);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000268 return true;
269 }
270 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000271
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000272#if GR_AA_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000273 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000274 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000275 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000276
joshualitt9853cce2014-11-17 14:22:48 -0800277 if (this->useSWOnlyPath(drawState, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000278 // The clip geometry is complex enough that it will be more efficient to create it
279 // entirely in software
280 result = this->createSoftwareClipMask(genID,
281 initialState,
282 elements,
283 clipSpaceIBounds);
284 } else {
285 result = this->createAlphaClipMask(genID,
286 initialState,
287 elements,
288 clipSpaceIBounds);
289 }
290
bsalomon49f085d2014-09-05 13:34:00 -0700291 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000292 // The mask's top left coord should be pinned to the rounded-out top left corner of
293 // clipSpace bounds. We determine the mask's position WRT to the render target here.
294 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
295 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
joshualitt9853cce2014-11-17 14:22:48 -0800296 setup_drawstate_aaclip(rtSpaceMaskBounds, drawState, result);
297 this->setDrawStateStencil(drawState, ars);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000298 return true;
299 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000300 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000301 }
302#endif // GR_AA_CLIP
303
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000304 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
305 // be created. In either case, free up the texture in the anti-aliased mask cache.
306 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
307 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
308 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000309 fAACache.reset();
310
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000311 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000312 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
joshualitt9853cce2014-11-17 14:22:48 -0800313 this->createStencilClipMask(rt,
314 genID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000315 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000316 elements,
317 clipSpaceIBounds,
318 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000319
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000320 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
321 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
322 // use both stencil and scissor test to the bounds for the final draw.
323 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
324 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
joshualitt77b13072014-10-27 14:51:01 -0700325 scissorState->set(scissorSpaceIBounds);
joshualitt9853cce2014-11-17 14:22:48 -0800326 this->setDrawStateStencil(drawState, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000327 return true;
328}
329
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000330namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000331////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000332// set up the OpenGL blend function to perform the specified
333// boolean operation for alpha clip mask creation
joshualitt9853cce2014-11-17 14:22:48 -0800334void setup_boolean_blendcoeffs(SkRegion::Op op, GrDrawState* drawState) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000335 switch (op) {
336 case SkRegion::kReplace_Op:
egdaniel8d95ffa2014-12-08 13:26:43 -0800337 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000338 break;
339 case SkRegion::kIntersect_Op:
egdaniel8d95ffa2014-12-08 13:26:43 -0800340 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000341 break;
342 case SkRegion::kUnion_Op:
egdaniel8d95ffa2014-12-08 13:26:43 -0800343 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000344 break;
345 case SkRegion::kXOR_Op:
egdaniel8d95ffa2014-12-08 13:26:43 -0800346 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000347 break;
348 case SkRegion::kDifference_Op:
egdaniel8d95ffa2014-12-08 13:26:43 -0800349 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000350 break;
351 case SkRegion::kReverseDifference_Op:
egdaniel8d95ffa2014-12-08 13:26:43 -0800352 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000353 break;
354 default:
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000355 SkASSERT(false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000356 break;
357 }
358}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000359}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000360
361////////////////////////////////////////////////////////////////////////////////
joshualitt9853cce2014-11-17 14:22:48 -0800362bool GrClipMaskManager::drawElement(GrDrawState* drawState,
363 GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000364 const SkClipStack::Element* element,
365 GrPathRenderer* pr) {
joshualitt9853cce2014-11-17 14:22:48 -0800366 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000367
368 drawState->setRenderTarget(target->asRenderTarget());
369
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000370 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000371 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000372 case Element::kEmpty_Type:
373 SkDEBUGFAIL("Should never get here with an empty element.");
374 break;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000375 case Element::kRect_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700376 // TODO: Do rects directly to the accumulator using a aa-rect GrProcessor that covers
377 // the entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000378 if (element->isAA()) {
joshualitt329bf482014-10-29 12:31:28 -0700379 this->getContext()->getAARectRenderer()->fillAARect(fClipTarget,
joshualitt9853cce2014-11-17 14:22:48 -0800380 drawState,
joshualitta58fe352014-10-27 08:39:00 -0700381 element->getRect(),
382 SkMatrix::I(),
383 element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000384 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800385 fClipTarget->drawSimpleRect(drawState, element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000386 }
387 return true;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000388 default: {
389 SkPath path;
390 element->asPath(&path);
jvanverthb3eb6872014-10-24 07:12:51 -0700391 path.setIsVolatile(true);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000392 if (path.isInverseFillType()) {
393 path.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000394 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000395 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000396 if (NULL == pr) {
397 GrPathRendererChain::DrawType type;
398 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
399 GrPathRendererChain::kColor_DrawType;
joshualitt9853cce2014-11-17 14:22:48 -0800400 pr = this->getContext()->getPathRenderer(fClipTarget, drawState, path, stroke,
401 false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000402 }
403 if (NULL == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000404 return false;
405 }
joshualitt9853cce2014-11-17 14:22:48 -0800406
407 pr->drawPath(fClipTarget, drawState, path, stroke, element->isAA());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000408 break;
409 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000410 }
411 return true;
412}
413
joshualitt9853cce2014-11-17 14:22:48 -0800414bool GrClipMaskManager::canStencilAndDrawElement(GrDrawState* drawState,
415 GrTexture* target,
416 GrPathRenderer** pr,
417 const SkClipStack::Element* element) {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000418 drawState->setRenderTarget(target->asRenderTarget());
419
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000420 if (Element::kRect_Type == element->getType()) {
421 return true;
422 } else {
423 // We shouldn't get here with an empty clip element.
424 SkASSERT(Element::kEmpty_Type != element->getType());
425 SkPath path;
426 element->asPath(&path);
427 if (path.isInverseFillType()) {
428 path.toggleInverseFillType();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000429 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000430 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
431 GrPathRendererChain::DrawType type = element->isAA() ?
432 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
433 GrPathRendererChain::kStencilAndColor_DrawType;
joshualitt9853cce2014-11-17 14:22:48 -0800434 *pr = this->getContext()->getPathRenderer(fClipTarget, drawState, path, stroke, false,
435 type);
bsalomon49f085d2014-09-05 13:34:00 -0700436 return SkToBool(*pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000437 }
438}
439
joshualitt9853cce2014-11-17 14:22:48 -0800440void GrClipMaskManager::mergeMask(GrDrawState* drawState,
441 GrTexture* dstMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000442 GrTexture* srcMask,
443 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000444 const SkIRect& dstBound,
445 const SkIRect& srcBound) {
joshualitt9853cce2014-11-17 14:22:48 -0800446 SkAssertResult(drawState->setIdentityViewMatrix());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000447
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000448 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000449
joshualitt9853cce2014-11-17 14:22:48 -0800450 setup_boolean_blendcoeffs(op, drawState);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000451
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000452 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000453 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000454
joshualittb0a8a372014-09-23 09:50:21 -0700455 drawState->addColorProcessor(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000456 GrTextureDomainEffect::Create(srcMask,
457 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000458 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
459 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000460 GrTextureParams::kNone_FilterMode))->unref();
joshualitt9853cce2014-11-17 14:22:48 -0800461 fClipTarget->drawSimpleRect(drawState, SkRect::Make(dstBound));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000462}
463
bsalomon427cf282014-10-16 13:41:43 -0700464GrTexture* GrClipMaskManager::createTempMask(int width, int height) {
bsalomonf2703d82014-10-28 14:33:06 -0700465 GrSurfaceDesc desc;
466 desc.fFlags = kRenderTarget_GrSurfaceFlag|kNoStencil_GrSurfaceFlag;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000467 desc.fWidth = width;
468 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000469 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000470
joshualitt329bf482014-10-29 12:31:28 -0700471 return this->getContext()->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000472}
473
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000474////////////////////////////////////////////////////////////////////////////////
krajcevskiad1dc582014-06-10 15:06:47 -0700475// Return the texture currently in the cache if it exists. Otherwise, return NULL
476GrTexture* GrClipMaskManager::getCachedMaskTexture(int32_t elementsGenID,
477 const SkIRect& clipSpaceIBounds) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000478 bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000479 if (!cached) {
krajcevskiad1dc582014-06-10 15:06:47 -0700480 return NULL;
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000481 }
482
krajcevskiad1dc582014-06-10 15:06:47 -0700483 return fAACache.getLastMask();
484}
485
486////////////////////////////////////////////////////////////////////////////////
487// Allocate a texture in the texture cache. This function returns the texture
488// allocated (or NULL on error).
489GrTexture* GrClipMaskManager::allocMaskTexture(int32_t elementsGenID,
490 const SkIRect& clipSpaceIBounds,
491 bool willUpload) {
492 // Since we are setting up the cache we should free up the
493 // currently cached mask so it can be reused.
494 fAACache.reset();
495
bsalomonf2703d82014-10-28 14:33:06 -0700496 GrSurfaceDesc desc;
497 desc.fFlags = willUpload ? kNone_GrSurfaceFlags : kRenderTarget_GrSurfaceFlag;
krajcevskiad1dc582014-06-10 15:06:47 -0700498 desc.fWidth = clipSpaceIBounds.width();
499 desc.fHeight = clipSpaceIBounds.height();
500 desc.fConfig = kRGBA_8888_GrPixelConfig;
501 if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
502 // We would always like A8 but it isn't supported on all platforms
503 desc.fConfig = kAlpha_8_GrPixelConfig;
504 }
505
506 fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds);
507 return fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000508}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000509
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000510////////////////////////////////////////////////////////////////////////////////
511// Create a 8-bit clip mask in alpha
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000512GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700513 GrReducedClip::InitialState initialState,
514 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000515 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000516 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000517
krajcevskiad1dc582014-06-10 15:06:47 -0700518 // First, check for cached texture
519 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -0700520 if (result) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000521 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000522 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000523 }
524
krajcevskiad1dc582014-06-10 15:06:47 -0700525 // There's no texture in the cache. Let's try to allocate it then.
526 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, false);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000527 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000528 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000529 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000530 }
531
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000532 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000533 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000534 SkIntToScalar(-clipSpaceIBounds.fLeft),
535 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000536 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000537 // The texture may be larger than necessary, this rect represents the part of the texture
538 // we populate with a rasterization of the clip.
539 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
540
bsalomon@google.com137f1342013-05-29 21:27:53 +0000541 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
542 SkMatrix translate;
543 translate.setTranslate(clipToMaskOffset);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000544
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000545 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
546 // clear the part that we care about.
joshualitt329bf482014-10-29 12:31:28 -0700547 fClipTarget->clear(&maskSpaceIBounds,
548 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
549 true,
550 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000551
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000552 // When we use the stencil in the below loop it is important to have this clip installed.
553 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
554 // pass must not set values outside of this bounds or stencil values outside the rect won't be
555 // cleared.
joshualitt329bf482014-10-29 12:31:28 -0700556 GrDrawTarget::AutoClipRestore acr(fClipTarget, maskSpaceIBounds);
bsalomon427cf282014-10-16 13:41:43 -0700557 SkAutoTUnref<GrTexture> temp;
joshualitt9853cce2014-11-17 14:22:48 -0800558
robertphillips@google.comf294b772012-04-27 14:29:26 +0000559 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700560 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000561 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000562 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000563 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000564 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
joshualitt9853cce2014-11-17 14:22:48 -0800565 GrDrawState drawState(translate);
566 // We're drawing a coverage mask and want coverage to be run through the blend function.
567 drawState.enableState(GrDrawState::kCoverageDrawing_StateBit |
568 GrDrawState::kClip_StateBit);
569
robertphillips@google.come79f3202014-02-11 16:30:21 +0000570 GrPathRenderer* pr = NULL;
joshualitt9853cce2014-11-17 14:22:48 -0800571 bool useTemp = !this->canStencilAndDrawElement(&drawState, result, &pr, element);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000572 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000573 // 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 +0000574 // mask buffer can be substantially larger than the actually clip stack element. We
575 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000576 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000577 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000578
579 if (useTemp) {
580 if (invert) {
581 maskSpaceElementIBounds = maskSpaceIBounds;
582 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000583 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000584 elementBounds.offset(clipToMaskOffset);
585 elementBounds.roundOut(&maskSpaceElementIBounds);
586 }
587
bsalomon427cf282014-10-16 13:41:43 -0700588 if (!temp) {
589 temp.reset(this->createTempMask(maskSpaceIBounds.fRight,
590 maskSpaceIBounds.fBottom));
591 if (!temp) {
592 fAACache.reset();
593 return NULL;
594 }
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000595 }
bsalomon427cf282014-10-16 13:41:43 -0700596 dst = temp;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000597 // clear the temp target and set blend to replace
joshualitt329bf482014-10-29 12:31:28 -0700598 fClipTarget->clear(&maskSpaceElementIBounds,
joshualitt9853cce2014-11-17 14:22:48 -0800599 invert ? 0xffffffff : 0x00000000,
600 true,
601 dst->asRenderTarget());
602 setup_boolean_blendcoeffs(SkRegion::kReplace_Op, &drawState);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000603 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000604 // draw directly into the result with the stencil set to make the pixels affected
605 // by the clip shape be non-zero.
606 dst = result;
607 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
608 kReplace_StencilOp,
609 kReplace_StencilOp,
610 kAlways_StencilFunc,
611 0xffff,
612 0xffff,
613 0xffff);
joshualitt9853cce2014-11-17 14:22:48 -0800614 drawState.setStencil(kStencilInElement);
615 setup_boolean_blendcoeffs(op, &drawState);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000616 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000617
joshualitt9853cce2014-11-17 14:22:48 -0800618 drawState.setAlpha(invert ? 0x00 : 0xff);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000619
joshualitt9853cce2014-11-17 14:22:48 -0800620 // We have to backup the drawstate because the drawElement call may call into
621 // renderers which consume it.
622 GrDrawState backupDrawState(drawState);
623
624 if (!this->drawElement(&drawState, dst, element, pr)) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000625 fAACache.reset();
626 return NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000627 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000628
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000629 if (useTemp) {
630 // Now draw into the accumulator using the real operation and the temp buffer as a
631 // texture
joshualitt9853cce2014-11-17 14:22:48 -0800632 this->mergeMask(&backupDrawState,
633 result,
bsalomon427cf282014-10-16 13:41:43 -0700634 temp,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000635 op,
636 maskSpaceIBounds,
637 maskSpaceElementIBounds);
638 } else {
639 // Draw to the exterior pixels (those with a zero stencil value).
joshualitt9853cce2014-11-17 14:22:48 -0800640 backupDrawState.setAlpha(invert ? 0xff : 0x00);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000641 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
642 kZero_StencilOp,
643 kZero_StencilOp,
644 kEqual_StencilFunc,
645 0xffff,
646 0x0000,
647 0xffff);
joshualitt9853cce2014-11-17 14:22:48 -0800648 backupDrawState.setStencil(kDrawOutsideElement);
649 fClipTarget->drawSimpleRect(&backupDrawState, clipSpaceIBounds);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000650 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000651 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800652 GrDrawState drawState(translate);
653 drawState.enableState(GrDrawState::kCoverageDrawing_StateBit |
654 GrDrawState::kClip_StateBit);
655
robertphillips@google.come79f3202014-02-11 16:30:21 +0000656 // all the remaining ops can just be directly draw into the accumulation buffer
joshualitt9853cce2014-11-17 14:22:48 -0800657 drawState.setAlpha(0xff);
658 setup_boolean_blendcoeffs(op, &drawState);
659 this->drawElement(&drawState, result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000660 }
661 }
662
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000663 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000664 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000665}
666
667////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000668// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000669// (as opposed to canvas) coordinates
joshualitt9853cce2014-11-17 14:22:48 -0800670bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt,
671 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700672 GrReducedClip::InitialState initialState,
673 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000674 const SkIRect& clipSpaceIBounds,
675 const SkIPoint& clipSpaceToStencilOffset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000676 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon49f085d2014-09-05 13:34:00 -0700677 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000678
679 // TODO: dynamically attach a SB when needed.
680 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
681 if (NULL == stencilBuffer) {
682 return false;
683 }
684
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000685 if (stencilBuffer->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000686 stencilBuffer->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000687 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
688 SkVector translate = {
689 SkIntToScalar(clipSpaceToStencilOffset.fX),
690 SkIntToScalar(clipSpaceToStencilOffset.fY)
691 };
692 SkMatrix matrix;
693 matrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000694
bsalomon@google.com9f131742012-12-13 20:43:56 +0000695 // We set the current clip to the bounds so that our recursive draws are scissored to them.
696 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
697 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
joshualitt329bf482014-10-29 12:31:28 -0700698 GrDrawTarget::AutoClipRestore acr(fClipTarget, stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000699
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000700 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000701 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000702 clipBit = (1 << (clipBit-1));
703
joshualitt329bf482014-10-29 12:31:28 -0700704 fClipTarget->clearStencilClip(stencilSpaceIBounds,
705 GrReducedClip::kAllIn_InitialState == initialState,
706 rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000707
708 // walk through each clip element and perform its set op
709 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700710 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000711 const Element* element = iter.get();
joshualitt9853cce2014-11-17 14:22:48 -0800712
713 GrDrawState drawState(matrix);
714 drawState.setRenderTarget(rt);
715 drawState.enableState(GrDrawState::kClip_StateBit);
716 drawState.enableState(GrDrawState::kNoColorWrites_StateBit);
717
718 // if the target is MSAA then we want MSAA enabled when the clip is soft
719 if (rt->isMultisampled()) {
720 drawState.setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
721 }
722
tomhudson@google.com8afae612012-08-14 15:03:35 +0000723 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000724 // enabled at bottom of loop
joshualitt7a6184f2014-10-29 18:29:27 -0700725 fClipMode = kIgnoreClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000726
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000727 // This will be used to determine whether the clip shape can be rendered into the
728 // stencil with arbitrary stencil settings.
729 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000730
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000731 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000732 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000733
robertphillips@google.come79f3202014-02-11 16:30:21 +0000734 GrPathRenderer* pr = NULL;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000735 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000736 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000737 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000738 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000739 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000740 element->asPath(&clipPath);
741 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000742 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000743 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000744 }
joshualitt9853cce2014-11-17 14:22:48 -0800745 pr = this->getContext()->getPathRenderer(fClipTarget,
746 &drawState,
747 clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000748 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000749 false,
750 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000751 &stencilSupport);
752 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000753 return false;
754 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000755 }
756
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000757 int passes;
758 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
759
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000760 bool canRenderDirectToStencil =
761 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000762 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000763 // fill rule, and set operation can
764 // we render the element directly to
765 // stencil bit used for clipping.
766 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
767 canRenderDirectToStencil,
768 clipBit,
769 fillInverted,
770 &passes,
771 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000772
773 // draw the element to the client stencil bits if necessary
774 if (!canDrawDirectToClip) {
775 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000776 kIncClamp_StencilOp,
777 kIncClamp_StencilOp,
778 kAlways_StencilFunc,
779 0xffff,
780 0x0000,
781 0xffff);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000782 if (Element::kRect_Type == element->getType()) {
joshualitt9853cce2014-11-17 14:22:48 -0800783 *drawState.stencil() = gDrawToStencil;
784 fClipTarget->drawSimpleRect(&drawState, element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000785 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000786 if (!clipPath.isEmpty()) {
joshualitt9853cce2014-11-17 14:22:48 -0800787 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000788 if (canRenderDirectToStencil) {
joshualitt9853cce2014-11-17 14:22:48 -0800789 *drawState.stencil() = gDrawToStencil;
790 pr->drawPath(fClipTarget, &drawState, clipPath, stroke, false);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000791 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800792 pr->stencilPath(fClipTarget, &drawState, clipPath, stroke);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000793 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000794 }
795 }
796 }
797
798 // now we modify the clip bit by rendering either the clip
799 // element directly or a bounding rect of the entire clip.
joshualitt7a6184f2014-10-29 18:29:27 -0700800 fClipMode = kModifyClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000801 for (int p = 0; p < passes; ++p) {
joshualitt9853cce2014-11-17 14:22:48 -0800802 GrDrawState drawStateCopy(drawState);
803 *drawStateCopy.stencil() = stencilSettings[p];
804
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000805 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000806 if (Element::kRect_Type == element->getType()) {
joshualitt9853cce2014-11-17 14:22:48 -0800807 fClipTarget->drawSimpleRect(&drawStateCopy, element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000808 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800809 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
810 pr->drawPath(fClipTarget, &drawStateCopy, clipPath, stroke, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000811 }
812 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000813 // The view matrix is setup to do clip space -> stencil space translation, so
814 // draw rect in clip space.
joshualitt9853cce2014-11-17 14:22:48 -0800815 fClipTarget->drawSimpleRect(&drawStateCopy, SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000816 }
817 }
818 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000819 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000820 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000821 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000822 fCurrClipMaskType = kStencil_ClipMaskType;
joshualitt7a6184f2014-10-29 18:29:27 -0700823 fClipMode = kRespectClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000824 return true;
825}
826
bsalomon@google.com411dad02012-06-05 20:24:20 +0000827// mapping of clip-respecting stencil funcs to normal stencil funcs
828// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000829static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000830 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
831 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
832 // In the Clip Funcs
833 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
834 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
835 kLess_StencilFunc, // kLessIfInClip_StencilFunc
836 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
837 // Special in the clip func that forces user's ref to be 0.
838 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
839 // make ref 0 and do normal nequal.
840 },
841 {// Stencil-Clipping is ENABLED
842 // In the Clip Funcs
843 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
844 // eq stencil clip bit, mask
845 // out user bits.
846
847 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
848 // add stencil bit to mask and ref
849
850 kLess_StencilFunc, // kLessIfInClip_StencilFunc
851 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
852 // for both of these we can add
853 // the clip bit to the mask and
854 // ref and compare as normal
855 // Special in the clip func that forces user's ref to be 0.
856 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
857 // make ref have only the clip bit set
858 // and make comparison be less
859 // 10..0 < 1..user_bits..
860 }
861};
862
bsalomon@google.coma3201942012-06-21 19:58:20 +0000863namespace {
864// Sets the settings to clip against the stencil buffer clip while ignoring the
865// client bits.
866const GrStencilSettings& basic_apply_stencil_clip_settings() {
867 // stencil settings to use when clip is in stencil
868 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
869 kKeep_StencilOp,
870 kKeep_StencilOp,
871 kAlwaysIfInClip_StencilFunc,
872 0x0000,
873 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000874 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000875 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
876}
877}
878
joshualitt9853cce2014-11-17 14:22:48 -0800879void GrClipMaskManager::setDrawStateStencil(GrDrawState* drawState,
880 GrDrawState::AutoRestoreStencil* ars) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000881 // We make two copies of the StencilSettings here (except in the early
882 // exit scenario. One copy from draw state to the stack var. Then another
883 // from the stack var to the gpu. We could make this class hold a ptr to
884 // GrGpu's fStencilSettings and eliminate the stack copy here.
885
bsalomon@google.coma3201942012-06-21 19:58:20 +0000886 // use stencil for clipping if clipping is enabled and the clip
887 // has been written into the stencil.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000888 GrStencilSettings settings;
joshualitt9853cce2014-11-17 14:22:48 -0800889
bsalomon@google.coma3201942012-06-21 19:58:20 +0000890 // The GrGpu client may not be using the stencil buffer but we may need to
891 // enable it in order to respect a stencil clip.
joshualitt9853cce2014-11-17 14:22:48 -0800892 if (drawState->getStencil().isDisabled()) {
joshualitt7a6184f2014-10-29 18:29:27 -0700893 if (GrClipMaskManager::kRespectClip_StencilClipMode == fClipMode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000894 settings = basic_apply_stencil_clip_settings();
895 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000896 return;
897 }
898 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800899 settings = drawState->getStencil();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000900 }
901
902 // TODO: dynamically attach a stencil buffer
903 int stencilBits = 0;
joshualitt9853cce2014-11-17 14:22:48 -0800904 GrStencilBuffer* stencilBuffer = drawState->getRenderTarget()->getStencilBuffer();
bsalomon49f085d2014-09-05 13:34:00 -0700905 if (stencilBuffer) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000906 stencilBits = stencilBuffer->bits();
907 }
908
joshualitt329bf482014-10-29 12:31:28 -0700909 SkASSERT(fClipTarget->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
910 SkASSERT(fClipTarget->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
joshualitt7a6184f2014-10-29 18:29:27 -0700911 this->adjustStencilParams(&settings, fClipMode, stencilBits);
joshualitt9853cce2014-11-17 14:22:48 -0800912 ars->set(drawState);
913 drawState->setStencil(settings);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000914}
915
916void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
917 StencilClipMode mode,
918 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000919 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000920
921 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000922 // We assume that this clip manager itself is drawing to the GrGpu and
923 // has already setup the correct values.
924 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000925 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000926
bsalomon@google.com411dad02012-06-05 20:24:20 +0000927 unsigned int clipBit = (1 << (stencilBitCnt - 1));
928 unsigned int userBits = clipBit - 1;
929
bsalomon@google.coma3201942012-06-21 19:58:20 +0000930 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
joshualitt329bf482014-10-29 12:31:28 -0700931 bool twoSided = fClipTarget->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000932
bsalomon@google.coma3201942012-06-21 19:58:20 +0000933 bool finished = false;
934 while (!finished) {
935 GrStencilFunc func = settings->func(face);
936 uint16_t writeMask = settings->writeMask(face);
937 uint16_t funcMask = settings->funcMask(face);
938 uint16_t funcRef = settings->funcRef(face);
939
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000940 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000941
942 writeMask &= userBits;
943
944 if (func >= kBasicStencilFuncCount) {
945 int respectClip = kRespectClip_StencilClipMode == mode;
946 if (respectClip) {
947 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000948 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000949 switch (func) {
950 case kAlwaysIfInClip_StencilFunc:
951 funcMask = clipBit;
952 funcRef = clipBit;
953 break;
954 case kEqualIfInClip_StencilFunc:
955 case kLessIfInClip_StencilFunc:
956 case kLEqualIfInClip_StencilFunc:
957 funcMask = (funcMask & userBits) | clipBit;
958 funcRef = (funcRef & userBits) | clipBit;
959 break;
960 case kNonZeroIfInClip_StencilFunc:
961 funcMask = (funcMask & userBits) | clipBit;
962 funcRef = clipBit;
963 break;
964 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000965 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +0000966 }
967 } else {
968 funcMask &= userBits;
969 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000970 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000971 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000972 gSpecialToBasicStencilFunc[respectClip];
973 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000974 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000975 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000976 funcMask &= userBits;
977 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000978 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000979
980 settings->setFunc(face, func);
981 settings->setWriteMask(face, writeMask);
982 settings->setFuncMask(face, funcMask);
983 settings->setFuncRef(face, funcRef);
984
985 if (GrStencilSettings::kFront_Face == face) {
986 face = GrStencilSettings::kBack_Face;
987 finished = !twoSided;
988 } else {
989 finished = true;
990 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000991 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000992 if (!twoSided) {
993 settings->copyFrontSettingsToBack();
994 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000995}
996
997////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000998GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000999 GrReducedClip::InitialState initialState,
1000 const GrReducedClip::ElementList& elements,
1001 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001002 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001003
krajcevskiad1dc582014-06-10 15:06:47 -07001004 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -07001005 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001006 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001007 }
1008
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001009 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1010 // the top left corner of the resulting rect to the top left of the texture.
1011 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1012
robertphillips@google.com2c756812012-05-22 20:28:23 +00001013 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001014
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001015 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001016 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
1017 SkIntToScalar(-clipSpaceIBounds.fTop));
joshualitt9853cce2014-11-17 14:22:48 -08001018
krajcevski71614ac2014-08-13 10:36:18 -07001019 helper.init(maskSpaceIBounds, &matrix, false);
tfarinabf54e492014-10-23 17:47:18 -07001020 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001021 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001022
tfarinabf54e492014-10-23 17:47:18 -07001023 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001024 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001025 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001026
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001027 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1028 // Intersect and reverse difference require modifying pixels outside of the geometry
1029 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1030 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1031 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001032 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001033 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001034 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001035 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001036 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001037 SkPath clipPath;
1038 element->asPath(&clipPath);
1039 clipPath.toggleInverseFillType();
1040 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001041 continue;
1042 }
1043
1044 // The other ops (union, xor, diff) only affect pixels inside
1045 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001046 if (Element::kRect_Type == element->getType()) {
1047 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1048 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001049 SkPath path;
1050 element->asPath(&path);
1051 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001052 }
1053 }
1054
krajcevskiad1dc582014-06-10 15:06:47 -07001055 // Allocate clip mask texture
1056 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, true);
1057 if (NULL == result) {
1058 fAACache.reset();
1059 return NULL;
1060 }
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001061 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001062
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001063 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001064 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001065}
1066
robertphillips@google.comf294b772012-04-27 14:29:26 +00001067////////////////////////////////////////////////////////////////////////////////
bsalomonc8dc1f72014-08-21 13:02:13 -07001068void GrClipMaskManager::purgeResources() {
1069 fAACache.purgeResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001070}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001071
joshualitt329bf482014-10-29 12:31:28 -07001072void GrClipMaskManager::setClipTarget(GrClipTarget* clipTarget) {
1073 fClipTarget = clipTarget;
1074 fAACache.setContext(clipTarget->getContext());
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001075}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001076
joshualitt9853cce2014-11-17 14:22:48 -08001077void GrClipMaskManager::adjustPathStencilParams(const GrStencilBuffer* stencilBuffer,
1078 GrStencilSettings* settings) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001079 // TODO: dynamically attach a stencil buffer
bsalomon49f085d2014-09-05 13:34:00 -07001080 if (stencilBuffer) {
joshualitt9853cce2014-11-17 14:22:48 -08001081 int stencilBits = stencilBuffer->bits();
joshualitt7a6184f2014-10-29 18:29:27 -07001082 this->adjustStencilParams(settings, fClipMode, stencilBits);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001083 }
1084}