blob: c2dc4fb2238f01400ec82e03d9cac14e375652b3 [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrClipMaskManager.h"
robertphillips@google.comfa662942012-05-17 12:20:22 +00009#include "GrAAConvexPathRenderer.h"
10#include "GrAAHairLinePathRenderer.h"
jvanverth@google.combfe2b9d2013-09-06 16:57:29 +000011#include "GrAARectRenderer.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000012#include "GrDrawTargetCaps.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000013#include "GrPaint.h"
14#include "GrPathRenderer.h"
15#include "GrRenderTarget.h"
16#include "GrStencilBuffer.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000017#include "GrSWMaskHelper.h"
joshualitt3a0cfeb2014-10-27 07:38:01 -070018#include "SkRasterClip.h"
19#include "SkStrokeRec.h"
20#include "SkTLazy.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080021#include "effects/GrConvexPolyEffect.h"
egdaniel95131432014-12-09 11:15:43 -080022#include "effects/GrPorterDuffXferProcessor.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080023#include "effects/GrRRectEffect.h"
egdaniel95131432014-12-09 11:15:43 -080024#include "effects/GrTextureDomain.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000025
robertphillips@google.comba998f22012-10-12 11:33:56 +000026#define GR_AA_CLIP 1
bsalomon@google.com8182fa02012-12-04 14:06:06 +000027typedef SkClipStack::Element Element;
bsalomon@google.com51a62862012-11-26 21:19:43 +000028
29////////////////////////////////////////////////////////////////////////////////
robertphillips@google.come79f3202014-02-11 16:30:21 +000030namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000031// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000032// stage matrix this also alters the vertex layout
joshualitt9853cce2014-11-17 14:22:48 -080033void setup_drawstate_aaclip(const SkIRect &devBound,
34 GrDrawState* drawState,
35 GrTexture* result) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000036 SkASSERT(drawState);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000037
bsalomon@google.comb9086a02012-11-01 18:02:54 +000038 SkMatrix mat;
bsalomon309d4d52014-12-18 10:17:44 -080039 // We use device coords to compute the texture coordinates. We set our matrix to be a
40 // translation to the devBound, and then a scaling matrix to normalized coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000041 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000042 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000043 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000044
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000045 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000046 // This could be a long-lived effect that is cached with the alpha-mask.
joshualittb0a8a372014-09-23 09:50:21 -070047 drawState->addCoverageProcessor(
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000048 GrTextureDomainEffect::Create(result,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000049 mat,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000050 GrTextureDomain::MakeTexelDomain(result, domainTexels),
51 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +000052 GrTextureParams::kNone_FilterMode,
bsalomon309d4d52014-12-18 10:17:44 -080053 kDevice_GrCoordSet))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000054}
55
robertphillips@google.come79f3202014-02-11 16:30:21 +000056bool path_needs_SW_renderer(GrContext* context,
joshualitt9853cce2014-11-17 14:22:48 -080057 const GrDrawTarget* gpu,
58 const GrDrawState* drawState,
joshualitt8059eb92014-12-29 15:10:07 -080059 const SkMatrix& viewMatrix,
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
joshualitt8059eb92014-12-29 15:10:07 -080073 return NULL == context->getPathRenderer(gpu, drawState, viewMatrix, *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,
joshualitt8059eb92014-12-29 15:10:07 -080083 const SkVector& clipToMaskOffset,
joshualitt9853cce2014-11-17 14:22:48 -080084 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
joshualitt8059eb92014-12-29 15:10:07 -080090 // Set the matrix so that rendered clip elements are transformed to mask space from clip
91 // space.
92 SkMatrix translate;
93 translate.setTranslate(clipToMaskOffset);
94
tfarinabf54e492014-10-23 17:47:18 -070095 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000096 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000097 // rects can always be drawn directly w/o using the software path
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000098 // Skip rrects once we're drawing them directly.
99 if (Element::kRect_Type != element->getType()) {
100 SkPath path;
101 element->asPath(&path);
joshualitt8059eb92014-12-29 15:10:07 -0800102 if (path_needs_SW_renderer(this->getContext(), fClipTarget, drawState, translate,
103 path, stroke, element->isAA())) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000104 return true;
105 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000106 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000107 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000108 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000109}
110
joshualitt9853cce2014-11-17 14:22:48 -0800111bool GrClipMaskManager::installClipEffects(GrDrawState* drawState,
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000112 GrDrawState::AutoRestoreEffects* are,
joshualitt9853cce2014-11-17 14:22:48 -0800113 const GrReducedClip::ElementList& elements,
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000114 const SkVector& clipToRTOffset,
mtklein217daa72014-07-02 12:55:21 -0700115 const SkRect* drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000116 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700117 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000118 boundsInClipSpace = *drawBounds;
119 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
120 }
121
122 are->set(drawState);
123 GrRenderTarget* rt = drawState->getRenderTarget();
tfarinabf54e492014-10-23 17:47:18 -0700124 GrReducedClip::ElementList::Iter iter(elements);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000125 bool failed = false;
bsalomon49f085d2014-09-05 13:34:00 -0700126 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000127 SkRegion::Op op = iter.get()->getOp();
128 bool invert;
129 bool skip = false;
130 switch (op) {
131 case SkRegion::kReplace_Op:
132 SkASSERT(iter.get() == elements.head());
133 // Fallthrough, handled same as intersect.
134 case SkRegion::kIntersect_Op:
135 invert = false;
bsalomon49f085d2014-09-05 13:34:00 -0700136 if (drawBounds && iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000137 skip = true;
138 }
139 break;
140 case SkRegion::kDifference_Op:
141 invert = true;
142 // We don't currently have a cheap test for whether a rect is fully outside an
143 // element's primitive, so don't attempt to set skip.
144 break;
145 default:
146 failed = true;
147 break;
148 }
149 if (failed) {
150 break;
151 }
152
153 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700154 GrPrimitiveEdgeType edgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000155 if (GR_AA_CLIP && iter.get()->isAA()) {
156 if (rt->isMultisampled()) {
mtklein217daa72014-07-02 12:55:21 -0700157 // Coverage based AA clips don't place nicely with MSAA.
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000158 failed = true;
159 break;
160 }
joshualittb0a8a372014-09-23 09:50:21 -0700161 edgeType =
162 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000163 } else {
joshualittb0a8a372014-09-23 09:50:21 -0700164 edgeType =
165 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000166 }
joshualittb0a8a372014-09-23 09:50:21 -0700167 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000168 switch (iter.get()->getType()) {
169 case SkClipStack::Element::kPath_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700170 fp.reset(GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000171 &clipToRTOffset));
172 break;
173 case SkClipStack::Element::kRRect_Type: {
174 SkRRect rrect = iter.get()->getRRect();
175 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
joshualittb0a8a372014-09-23 09:50:21 -0700176 fp.reset(GrRRectEffect::Create(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000177 break;
178 }
179 case SkClipStack::Element::kRect_Type: {
180 SkRect rect = iter.get()->getRect();
181 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
joshualittb0a8a372014-09-23 09:50:21 -0700182 fp.reset(GrConvexPolyEffect::Create(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000183 break;
184 }
185 default:
186 break;
187 }
joshualittb0a8a372014-09-23 09:50:21 -0700188 if (fp) {
joshualitt9853cce2014-11-17 14:22:48 -0800189 drawState->addCoverageProcessor(fp);
mtklein217daa72014-07-02 12:55:21 -0700190 } else {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000191 failed = true;
192 break;
193 }
194 }
mtklein217daa72014-07-02 12:55:21 -0700195 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000196 }
197
198 if (failed) {
199 are->set(NULL);
200 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000201 return !failed;
202}
203
robertphillips@google.comf294b772012-04-27 14:29:26 +0000204////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000205// sort out what kind of clip mask needs to be created: alpha, stencil,
206// scissor, or entirely software
joshualitt9853cce2014-11-17 14:22:48 -0800207bool GrClipMaskManager::setupClipping(GrDrawState* drawState,
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000208 GrDrawState::AutoRestoreEffects* are,
joshualitt77b13072014-10-27 14:51:01 -0700209 GrDrawState::AutoRestoreStencil* ars,
bsalomon3e791242014-12-17 13:43:13 -0800210 GrScissorState* scissorState,
joshualitt9853cce2014-11-17 14:22:48 -0800211 const GrClipData* clipDataIn,
212 const SkRect* devBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000213 fCurrClipMaskType = kNone_ClipMaskType;
joshualitt7a6184f2014-10-29 18:29:27 -0700214 if (kRespectClip_StencilClipMode == fClipMode) {
215 fClipMode = kIgnoreClip_StencilClipMode;
216 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000217
tfarinabf54e492014-10-23 17:47:18 -0700218 GrReducedClip::ElementList elements(16);
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000219 int32_t genID;
tfarinabf54e492014-10-23 17:47:18 -0700220 GrReducedClip::InitialState initialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000221 SkIRect clipSpaceIBounds;
222 bool requiresAA;
joshualitt9853cce2014-11-17 14:22:48 -0800223 GrRenderTarget* rt = drawState->getRenderTarget();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000224
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000225 // GrDrawTarget should have filtered this for us
bsalomon49f085d2014-09-05 13:34:00 -0700226 SkASSERT(rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000227
228 bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000229 if (!ignoreClip) {
230 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
231 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
tfarinabf54e492014-10-23 17:47:18 -0700232 GrReducedClip::ReduceClipStack(*clipDataIn->fClipStack,
233 clipSpaceRTIBounds,
234 &elements,
235 &genID,
236 &initialState,
237 &clipSpaceIBounds,
238 &requiresAA);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000239 if (elements.isEmpty()) {
tfarinabf54e492014-10-23 17:47:18 -0700240 if (GrReducedClip::kAllIn_InitialState == initialState) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000241 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000242 } else {
243 return false;
244 }
245 }
246 }
247
248 if (ignoreClip) {
joshualitt9853cce2014-11-17 14:22:48 -0800249 this->setDrawStateStencil(drawState, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000250 return true;
251 }
252
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000253 // An element count of 4 was chosen because of the common pattern in Blink of:
254 // isect RR
255 // diff RR
256 // isect convex_poly
257 // isect convex_poly
258 // when drawing rounded div borders. This could probably be tuned based on a
259 // configuration's relative costs of switching RTs to generate a mask vs
260 // longer shaders.
261 if (elements.count() <= 4) {
262 SkVector clipToRTOffset = { SkIntToScalar(-clipDataIn->fOrigin.fX),
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000263 SkIntToScalar(-clipDataIn->fOrigin.fY) };
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000264 if (elements.isEmpty() ||
joshualitt9853cce2014-11-17 14:22:48 -0800265 (requiresAA && this->installClipEffects(drawState, are, elements, clipToRTOffset,
266 devBounds))) {
mtklein217daa72014-07-02 12:55:21 -0700267 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
268 scissorSpaceIBounds.offset(-clipDataIn->fOrigin);
269 if (NULL == devBounds ||
270 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
joshualitt77b13072014-10-27 14:51:01 -0700271 scissorState->set(scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000272 }
joshualitt9853cce2014-11-17 14:22:48 -0800273 this->setDrawStateStencil(drawState, ars);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000274 return true;
275 }
276 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000277
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000278#if GR_AA_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000279 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000280 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000281 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000282
joshualitt8059eb92014-12-29 15:10:07 -0800283 // The top-left of the mask corresponds to the top-left corner of the bounds.
284 SkVector clipToMaskOffset = {
285 SkIntToScalar(-clipSpaceIBounds.fLeft),
286 SkIntToScalar(-clipSpaceIBounds.fTop)
287 };
288
289 if (this->useSWOnlyPath(drawState, clipToMaskOffset, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000290 // The clip geometry is complex enough that it will be more efficient to create it
291 // entirely in software
292 result = this->createSoftwareClipMask(genID,
293 initialState,
294 elements,
joshualitt8059eb92014-12-29 15:10:07 -0800295 clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000296 clipSpaceIBounds);
297 } else {
298 result = this->createAlphaClipMask(genID,
299 initialState,
300 elements,
joshualitt8059eb92014-12-29 15:10:07 -0800301 clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000302 clipSpaceIBounds);
303 }
304
bsalomon49f085d2014-09-05 13:34:00 -0700305 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000306 // The mask's top left coord should be pinned to the rounded-out top left corner of
307 // clipSpace bounds. We determine the mask's position WRT to the render target here.
308 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
309 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
joshualitt9853cce2014-11-17 14:22:48 -0800310 setup_drawstate_aaclip(rtSpaceMaskBounds, drawState, result);
311 this->setDrawStateStencil(drawState, ars);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000312 return true;
313 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000314 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000315 }
316#endif // GR_AA_CLIP
317
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000318 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
319 // be created. In either case, free up the texture in the anti-aliased mask cache.
320 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
321 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
322 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000323 fAACache.reset();
324
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000325 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000326 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
joshualitt9853cce2014-11-17 14:22:48 -0800327 this->createStencilClipMask(rt,
328 genID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000329 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000330 elements,
331 clipSpaceIBounds,
332 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000333
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000334 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
335 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
336 // use both stencil and scissor test to the bounds for the final draw.
337 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
338 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
joshualitt77b13072014-10-27 14:51:01 -0700339 scissorState->set(scissorSpaceIBounds);
joshualitt9853cce2014-11-17 14:22:48 -0800340 this->setDrawStateStencil(drawState, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000341 return true;
342}
343
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000344namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000345////////////////////////////////////////////////////////////////////////////////
egdaniel87509242014-12-17 13:37:13 -0800346// Set a coverage drawing XPF on the drawState for the given op and invertCoverage mode
347void set_coverage_drawing_xpf(SkRegion::Op op, bool invertCoverage, GrDrawState* drawState) {
348 SkASSERT(op <= SkRegion::kLastOp);
349 drawState->setCoverageSetOpXPFactory(op, invertCoverage);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000350}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000351}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000352
353////////////////////////////////////////////////////////////////////////////////
joshualitt9853cce2014-11-17 14:22:48 -0800354bool GrClipMaskManager::drawElement(GrDrawState* drawState,
joshualitt8059eb92014-12-29 15:10:07 -0800355 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800356 GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000357 const SkClipStack::Element* element,
358 GrPathRenderer* pr) {
joshualitt9853cce2014-11-17 14:22:48 -0800359 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000360
361 drawState->setRenderTarget(target->asRenderTarget());
362
egdaniel87509242014-12-17 13:37:13 -0800363 // The color we use to draw does not matter since we will always be using a GrCoverageSetOpXP
364 // which ignores color.
365 GrColor color = GrColor_WHITE;
366
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000367 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000368 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000369 case Element::kEmpty_Type:
370 SkDEBUGFAIL("Should never get here with an empty element.");
371 break;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000372 case Element::kRect_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700373 // TODO: Do rects directly to the accumulator using a aa-rect GrProcessor that covers
374 // the entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000375 if (element->isAA()) {
joshualitt8059eb92014-12-29 15:10:07 -0800376 SkRect devRect = element->getRect();
377 viewMatrix.mapRect(&devRect);
joshualitt329bf482014-10-29 12:31:28 -0700378 this->getContext()->getAARectRenderer()->fillAARect(fClipTarget,
joshualitt9853cce2014-11-17 14:22:48 -0800379 drawState,
joshualitt2e3b3e32014-12-09 13:31:14 -0800380 color,
joshualitt8059eb92014-12-29 15:10:07 -0800381 viewMatrix,
joshualitta58fe352014-10-27 08:39:00 -0700382 element->getRect(),
joshualitt8059eb92014-12-29 15:10:07 -0800383 devRect);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000384 } else {
joshualitt8059eb92014-12-29 15:10:07 -0800385 fClipTarget->drawSimpleRect(drawState, color, viewMatrix, 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;
joshualitt8059eb92014-12-29 15:10:07 -0800400 pr = this->getContext()->getPathRenderer(fClipTarget, drawState, viewMatrix, path,
401 stroke, 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
joshualitt8059eb92014-12-29 15:10:07 -0800407 pr->drawPath(fClipTarget, drawState, color, viewMatrix, 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;
joshualitt8059eb92014-12-29 15:10:07 -0800434 *pr = this->getContext()->getPathRenderer(fClipTarget, drawState, SkMatrix::I(), path,
435 stroke, false, 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) {
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000446 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000447
egdaniel87509242014-12-17 13:37:13 -0800448 // We want to invert the coverage here
449 set_coverage_drawing_xpf(op, false, drawState);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000450
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000451 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000452 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000453
egdaniel87509242014-12-17 13:37:13 -0800454 drawState->addCoverageProcessor(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000455 GrTextureDomainEffect::Create(srcMask,
456 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000457 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
458 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000459 GrTextureParams::kNone_FilterMode))->unref();
egdaniel87509242014-12-17 13:37:13 -0800460 // The color passed in here does not matter since the coverageSetOpXP won't read it.
joshualitt8059eb92014-12-29 15:10:07 -0800461 fClipTarget->drawSimpleRect(drawState, GrColor_WHITE, SkMatrix::I(), 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;
bsalomon3f490a02014-12-18 06:20:52 -0800466 desc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000467 desc.fWidth = width;
468 desc.fHeight = height;
bsalomon51d1f7e2014-12-22 08:40:49 -0800469 if (this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
470 desc.fConfig = kAlpha_8_GrPixelConfig;
471 } else {
472 desc.fConfig = kRGBA_8888_GrPixelConfig;
473 }
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000474
joshualitt329bf482014-10-29 12:31:28 -0700475 return this->getContext()->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000476}
477
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000478////////////////////////////////////////////////////////////////////////////////
krajcevskiad1dc582014-06-10 15:06:47 -0700479// Return the texture currently in the cache if it exists. Otherwise, return NULL
480GrTexture* GrClipMaskManager::getCachedMaskTexture(int32_t elementsGenID,
481 const SkIRect& clipSpaceIBounds) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000482 bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000483 if (!cached) {
krajcevskiad1dc582014-06-10 15:06:47 -0700484 return NULL;
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000485 }
486
krajcevskiad1dc582014-06-10 15:06:47 -0700487 return fAACache.getLastMask();
488}
489
490////////////////////////////////////////////////////////////////////////////////
491// Allocate a texture in the texture cache. This function returns the texture
492// allocated (or NULL on error).
493GrTexture* GrClipMaskManager::allocMaskTexture(int32_t elementsGenID,
494 const SkIRect& clipSpaceIBounds,
495 bool willUpload) {
496 // Since we are setting up the cache we should free up the
497 // currently cached mask so it can be reused.
498 fAACache.reset();
499
bsalomonf2703d82014-10-28 14:33:06 -0700500 GrSurfaceDesc desc;
501 desc.fFlags = willUpload ? kNone_GrSurfaceFlags : kRenderTarget_GrSurfaceFlag;
krajcevskiad1dc582014-06-10 15:06:47 -0700502 desc.fWidth = clipSpaceIBounds.width();
503 desc.fHeight = clipSpaceIBounds.height();
504 desc.fConfig = kRGBA_8888_GrPixelConfig;
505 if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
506 // We would always like A8 but it isn't supported on all platforms
507 desc.fConfig = kAlpha_8_GrPixelConfig;
508 }
509
510 fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds);
511 return fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000512}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000513
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000514////////////////////////////////////////////////////////////////////////////////
515// Create a 8-bit clip mask in alpha
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000516GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700517 GrReducedClip::InitialState initialState,
518 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800519 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000520 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000521 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000522
krajcevskiad1dc582014-06-10 15:06:47 -0700523 // First, check for cached texture
524 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -0700525 if (result) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000526 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000527 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000528 }
529
krajcevskiad1dc582014-06-10 15:06:47 -0700530 // There's no texture in the cache. Let's try to allocate it then.
531 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, false);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000532 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000533 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000534 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000535 }
536
joshualitt8059eb92014-12-29 15:10:07 -0800537 // Set the matrix so that rendered clip elements are transformed to mask space from clip
538 // space.
539 SkMatrix translate;
540 translate.setTranslate(clipToMaskOffset);
541
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000542 // The texture may be larger than necessary, this rect represents the part of the texture
543 // we populate with a rasterization of the clip.
544 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
545
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000546 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
547 // clear the part that we care about.
joshualitt329bf482014-10-29 12:31:28 -0700548 fClipTarget->clear(&maskSpaceIBounds,
549 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
550 true,
551 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000552
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000553 // When we use the stencil in the below loop it is important to have this clip installed.
554 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
555 // pass must not set values outside of this bounds or stencil values outside the rect won't be
556 // cleared.
joshualitt329bf482014-10-29 12:31:28 -0700557 GrDrawTarget::AutoClipRestore acr(fClipTarget, maskSpaceIBounds);
bsalomon427cf282014-10-16 13:41:43 -0700558 SkAutoTUnref<GrTexture> temp;
joshualitt9853cce2014-11-17 14:22:48 -0800559
robertphillips@google.comf294b772012-04-27 14:29:26 +0000560 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700561 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000562 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000563 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000564 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000565 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
joshualitt8059eb92014-12-29 15:10:07 -0800566 GrDrawState drawState;
egdaniel87509242014-12-17 13:37:13 -0800567 drawState.enableState(GrDrawState::kClip_StateBit);
joshualitt9853cce2014-11-17 14:22:48 -0800568
robertphillips@google.come79f3202014-02-11 16:30:21 +0000569 GrPathRenderer* pr = NULL;
joshualitt9853cce2014-11-17 14:22:48 -0800570 bool useTemp = !this->canStencilAndDrawElement(&drawState, result, &pr, element);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000571 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000572 // 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 +0000573 // mask buffer can be substantially larger than the actually clip stack element. We
574 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000575 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000576 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000577
578 if (useTemp) {
579 if (invert) {
580 maskSpaceElementIBounds = maskSpaceIBounds;
581 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000582 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000583 elementBounds.offset(clipToMaskOffset);
584 elementBounds.roundOut(&maskSpaceElementIBounds);
585 }
586
bsalomon427cf282014-10-16 13:41:43 -0700587 if (!temp) {
588 temp.reset(this->createTempMask(maskSpaceIBounds.fRight,
589 maskSpaceIBounds.fBottom));
590 if (!temp) {
591 fAACache.reset();
592 return NULL;
593 }
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000594 }
bsalomon427cf282014-10-16 13:41:43 -0700595 dst = temp;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000596 // clear the temp target and set blend to replace
joshualitt329bf482014-10-29 12:31:28 -0700597 fClipTarget->clear(&maskSpaceElementIBounds,
joshualitt9853cce2014-11-17 14:22:48 -0800598 invert ? 0xffffffff : 0x00000000,
599 true,
600 dst->asRenderTarget());
egdaniel87509242014-12-17 13:37:13 -0800601 set_coverage_drawing_xpf(SkRegion::kReplace_Op, invert, &drawState);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000602 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000603 // draw directly into the result with the stencil set to make the pixels affected
604 // by the clip shape be non-zero.
605 dst = result;
606 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
607 kReplace_StencilOp,
608 kReplace_StencilOp,
609 kAlways_StencilFunc,
610 0xffff,
611 0xffff,
612 0xffff);
joshualitt9853cce2014-11-17 14:22:48 -0800613 drawState.setStencil(kStencilInElement);
egdaniel87509242014-12-17 13:37:13 -0800614 set_coverage_drawing_xpf(op, invert, &drawState);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000615 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000616
joshualitt8059eb92014-12-29 15:10:07 -0800617 if (!this->drawElement(&drawState, translate, dst, element, pr)) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000618 fAACache.reset();
619 return NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000620 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000621
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000622 if (useTemp) {
joshualitt8fc6c2d2014-12-22 15:27:05 -0800623 GrDrawState backgroundDrawState;
624 backgroundDrawState.enableState(GrDrawState::kClip_StateBit);
625 backgroundDrawState.setRenderTarget(result->asRenderTarget());
626
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000627 // Now draw into the accumulator using the real operation and the temp buffer as a
628 // texture
egdaniel87509242014-12-17 13:37:13 -0800629 this->mergeMask(&backgroundDrawState,
joshualitt9853cce2014-11-17 14:22:48 -0800630 result,
bsalomon427cf282014-10-16 13:41:43 -0700631 temp,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000632 op,
633 maskSpaceIBounds,
634 maskSpaceElementIBounds);
635 } else {
joshualitt8059eb92014-12-29 15:10:07 -0800636 GrDrawState backgroundDrawState;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800637 backgroundDrawState.enableState(GrDrawState::kClip_StateBit);
638 backgroundDrawState.setRenderTarget(result->asRenderTarget());
639
egdaniel87509242014-12-17 13:37:13 -0800640 set_coverage_drawing_xpf(op, !invert, &backgroundDrawState);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000641 // Draw to the exterior pixels (those with a zero stencil value).
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000642 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
643 kZero_StencilOp,
644 kZero_StencilOp,
645 kEqual_StencilFunc,
646 0xffff,
647 0x0000,
648 0xffff);
egdaniel87509242014-12-17 13:37:13 -0800649 backgroundDrawState.setStencil(kDrawOutsideElement);
650 // The color passed in here does not matter since the coverageSetOpXP won't read it.
joshualitt8059eb92014-12-29 15:10:07 -0800651 fClipTarget->drawSimpleRect(&backgroundDrawState, GrColor_WHITE, translate,
652 clipSpaceIBounds);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000653 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000654 } else {
joshualitt8059eb92014-12-29 15:10:07 -0800655 GrDrawState drawState;
egdaniel87509242014-12-17 13:37:13 -0800656 drawState.enableState(GrDrawState::kClip_StateBit);
joshualitt9853cce2014-11-17 14:22:48 -0800657
robertphillips@google.come79f3202014-02-11 16:30:21 +0000658 // all the remaining ops can just be directly draw into the accumulation buffer
egdaniel87509242014-12-17 13:37:13 -0800659 set_coverage_drawing_xpf(op, false, &drawState);
660 // The color passed in here does not matter since the coverageSetOpXP won't read it.
joshualitt8059eb92014-12-29 15:10:07 -0800661 this->drawElement(&drawState, translate, result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000662 }
663 }
664
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000665 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000666 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000667}
668
669////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000670// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000671// (as opposed to canvas) coordinates
joshualitt9853cce2014-11-17 14:22:48 -0800672bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt,
673 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700674 GrReducedClip::InitialState initialState,
675 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000676 const SkIRect& clipSpaceIBounds,
677 const SkIPoint& clipSpaceToStencilOffset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000678 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon49f085d2014-09-05 13:34:00 -0700679 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000680
681 // TODO: dynamically attach a SB when needed.
682 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
683 if (NULL == stencilBuffer) {
684 return false;
685 }
686
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000687 if (stencilBuffer->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000688 stencilBuffer->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000689 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
690 SkVector translate = {
691 SkIntToScalar(clipSpaceToStencilOffset.fX),
692 SkIntToScalar(clipSpaceToStencilOffset.fY)
693 };
joshualitt8059eb92014-12-29 15:10:07 -0800694 SkMatrix viewMatrix;
695 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000696
bsalomon@google.com9f131742012-12-13 20:43:56 +0000697 // We set the current clip to the bounds so that our recursive draws are scissored to them.
698 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
699 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
joshualitt329bf482014-10-29 12:31:28 -0700700 GrDrawTarget::AutoClipRestore acr(fClipTarget, stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000701
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000702 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000703 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000704 clipBit = (1 << (clipBit-1));
705
joshualitt329bf482014-10-29 12:31:28 -0700706 fClipTarget->clearStencilClip(stencilSpaceIBounds,
707 GrReducedClip::kAllIn_InitialState == initialState,
708 rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000709
710 // walk through each clip element and perform its set op
711 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700712 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000713 const Element* element = iter.get();
joshualitt9853cce2014-11-17 14:22:48 -0800714
joshualitt8059eb92014-12-29 15:10:07 -0800715 GrDrawState drawState;
joshualitt9853cce2014-11-17 14:22:48 -0800716 drawState.setRenderTarget(rt);
717 drawState.enableState(GrDrawState::kClip_StateBit);
egdaniel080e6732014-12-22 07:35:52 -0800718
719 drawState.setDisableColorXPFactory();
joshualitt9853cce2014-11-17 14:22:48 -0800720
721 // if the target is MSAA then we want MSAA enabled when the clip is soft
722 if (rt->isMultisampled()) {
723 drawState.setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
724 }
725
tomhudson@google.com8afae612012-08-14 15:03:35 +0000726 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000727 // enabled at bottom of loop
joshualitt7a6184f2014-10-29 18:29:27 -0700728 fClipMode = kIgnoreClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000729
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000730 // This will be used to determine whether the clip shape can be rendered into the
731 // stencil with arbitrary stencil settings.
732 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000733
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000734 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000735 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000736
robertphillips@google.come79f3202014-02-11 16:30:21 +0000737 GrPathRenderer* pr = NULL;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000738 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000739 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000740 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000741 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000742 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000743 element->asPath(&clipPath);
744 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000745 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000746 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000747 }
joshualitt9853cce2014-11-17 14:22:48 -0800748 pr = this->getContext()->getPathRenderer(fClipTarget,
749 &drawState,
joshualitt8059eb92014-12-29 15:10:07 -0800750 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800751 clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000752 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000753 false,
754 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000755 &stencilSupport);
756 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000757 return false;
758 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000759 }
760
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000761 int passes;
762 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
763
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000764 bool canRenderDirectToStencil =
765 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000766 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000767 // fill rule, and set operation can
768 // we render the element directly to
769 // stencil bit used for clipping.
770 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
771 canRenderDirectToStencil,
772 clipBit,
773 fillInverted,
774 &passes,
775 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000776
777 // draw the element to the client stencil bits if necessary
778 if (!canDrawDirectToClip) {
779 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000780 kIncClamp_StencilOp,
781 kIncClamp_StencilOp,
782 kAlways_StencilFunc,
783 0xffff,
784 0x0000,
785 0xffff);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000786 if (Element::kRect_Type == element->getType()) {
joshualitt9853cce2014-11-17 14:22:48 -0800787 *drawState.stencil() = gDrawToStencil;
joshualitt8059eb92014-12-29 15:10:07 -0800788 fClipTarget->drawSimpleRect(&drawState, GrColor_WHITE, viewMatrix,
789 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000790 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000791 if (!clipPath.isEmpty()) {
joshualitt9853cce2014-11-17 14:22:48 -0800792 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000793 if (canRenderDirectToStencil) {
joshualitt9853cce2014-11-17 14:22:48 -0800794 *drawState.stencil() = gDrawToStencil;
joshualitt8059eb92014-12-29 15:10:07 -0800795 pr->drawPath(fClipTarget, &drawState, GrColor_WHITE, viewMatrix,
796 clipPath, stroke, false);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000797 } else {
joshualitt8059eb92014-12-29 15:10:07 -0800798 pr->stencilPath(fClipTarget, &drawState, viewMatrix, clipPath, stroke);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000799 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000800 }
801 }
802 }
803
804 // now we modify the clip bit by rendering either the clip
805 // element directly or a bounding rect of the entire clip.
joshualitt7a6184f2014-10-29 18:29:27 -0700806 fClipMode = kModifyClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000807 for (int p = 0; p < passes; ++p) {
joshualitt9853cce2014-11-17 14:22:48 -0800808 GrDrawState drawStateCopy(drawState);
809 *drawStateCopy.stencil() = stencilSettings[p];
810
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000811 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000812 if (Element::kRect_Type == element->getType()) {
joshualitt8059eb92014-12-29 15:10:07 -0800813 fClipTarget->drawSimpleRect(&drawStateCopy, GrColor_WHITE, viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800814 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000815 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800816 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
joshualitt8059eb92014-12-29 15:10:07 -0800817 pr->drawPath(fClipTarget, &drawStateCopy, GrColor_WHITE, viewMatrix,
818 clipPath, stroke, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000819 }
820 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000821 // The view matrix is setup to do clip space -> stencil space translation, so
822 // draw rect in clip space.
joshualitt8059eb92014-12-29 15:10:07 -0800823 fClipTarget->drawSimpleRect(&drawStateCopy, GrColor_WHITE, viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800824 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000825 }
826 }
827 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000828 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000829 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000830 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000831 fCurrClipMaskType = kStencil_ClipMaskType;
joshualitt7a6184f2014-10-29 18:29:27 -0700832 fClipMode = kRespectClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000833 return true;
834}
835
bsalomon@google.com411dad02012-06-05 20:24:20 +0000836// mapping of clip-respecting stencil funcs to normal stencil funcs
837// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000838static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000839 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
840 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
841 // In the Clip Funcs
842 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
843 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
844 kLess_StencilFunc, // kLessIfInClip_StencilFunc
845 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
846 // Special in the clip func that forces user's ref to be 0.
847 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
848 // make ref 0 and do normal nequal.
849 },
850 {// Stencil-Clipping is ENABLED
851 // In the Clip Funcs
852 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
853 // eq stencil clip bit, mask
854 // out user bits.
855
856 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
857 // add stencil bit to mask and ref
858
859 kLess_StencilFunc, // kLessIfInClip_StencilFunc
860 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
861 // for both of these we can add
862 // the clip bit to the mask and
863 // ref and compare as normal
864 // Special in the clip func that forces user's ref to be 0.
865 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
866 // make ref have only the clip bit set
867 // and make comparison be less
868 // 10..0 < 1..user_bits..
869 }
870};
871
bsalomon@google.coma3201942012-06-21 19:58:20 +0000872namespace {
873// Sets the settings to clip against the stencil buffer clip while ignoring the
874// client bits.
875const GrStencilSettings& basic_apply_stencil_clip_settings() {
876 // stencil settings to use when clip is in stencil
877 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
878 kKeep_StencilOp,
879 kKeep_StencilOp,
880 kAlwaysIfInClip_StencilFunc,
881 0x0000,
882 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000883 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000884 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
885}
886}
887
joshualitt9853cce2014-11-17 14:22:48 -0800888void GrClipMaskManager::setDrawStateStencil(GrDrawState* drawState,
889 GrDrawState::AutoRestoreStencil* ars) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000890 // We make two copies of the StencilSettings here (except in the early
891 // exit scenario. One copy from draw state to the stack var. Then another
892 // from the stack var to the gpu. We could make this class hold a ptr to
893 // GrGpu's fStencilSettings and eliminate the stack copy here.
894
bsalomon@google.coma3201942012-06-21 19:58:20 +0000895 // use stencil for clipping if clipping is enabled and the clip
896 // has been written into the stencil.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000897 GrStencilSettings settings;
joshualitt9853cce2014-11-17 14:22:48 -0800898
bsalomon@google.coma3201942012-06-21 19:58:20 +0000899 // The GrGpu client may not be using the stencil buffer but we may need to
900 // enable it in order to respect a stencil clip.
joshualitt9853cce2014-11-17 14:22:48 -0800901 if (drawState->getStencil().isDisabled()) {
joshualitt7a6184f2014-10-29 18:29:27 -0700902 if (GrClipMaskManager::kRespectClip_StencilClipMode == fClipMode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000903 settings = basic_apply_stencil_clip_settings();
904 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000905 return;
906 }
907 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800908 settings = drawState->getStencil();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000909 }
910
911 // TODO: dynamically attach a stencil buffer
912 int stencilBits = 0;
joshualitt9853cce2014-11-17 14:22:48 -0800913 GrStencilBuffer* stencilBuffer = drawState->getRenderTarget()->getStencilBuffer();
bsalomon49f085d2014-09-05 13:34:00 -0700914 if (stencilBuffer) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000915 stencilBits = stencilBuffer->bits();
916 }
917
joshualitt329bf482014-10-29 12:31:28 -0700918 SkASSERT(fClipTarget->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
919 SkASSERT(fClipTarget->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
joshualitt7a6184f2014-10-29 18:29:27 -0700920 this->adjustStencilParams(&settings, fClipMode, stencilBits);
joshualitt9853cce2014-11-17 14:22:48 -0800921 ars->set(drawState);
922 drawState->setStencil(settings);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000923}
924
925void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
926 StencilClipMode mode,
927 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000928 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000929
930 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000931 // We assume that this clip manager itself is drawing to the GrGpu and
932 // has already setup the correct values.
933 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000934 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000935
bsalomon@google.com411dad02012-06-05 20:24:20 +0000936 unsigned int clipBit = (1 << (stencilBitCnt - 1));
937 unsigned int userBits = clipBit - 1;
938
bsalomon@google.coma3201942012-06-21 19:58:20 +0000939 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
joshualitt329bf482014-10-29 12:31:28 -0700940 bool twoSided = fClipTarget->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000941
bsalomon@google.coma3201942012-06-21 19:58:20 +0000942 bool finished = false;
943 while (!finished) {
944 GrStencilFunc func = settings->func(face);
945 uint16_t writeMask = settings->writeMask(face);
946 uint16_t funcMask = settings->funcMask(face);
947 uint16_t funcRef = settings->funcRef(face);
948
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000949 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000950
951 writeMask &= userBits;
952
953 if (func >= kBasicStencilFuncCount) {
954 int respectClip = kRespectClip_StencilClipMode == mode;
955 if (respectClip) {
956 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000957 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000958 switch (func) {
959 case kAlwaysIfInClip_StencilFunc:
960 funcMask = clipBit;
961 funcRef = clipBit;
962 break;
963 case kEqualIfInClip_StencilFunc:
964 case kLessIfInClip_StencilFunc:
965 case kLEqualIfInClip_StencilFunc:
966 funcMask = (funcMask & userBits) | clipBit;
967 funcRef = (funcRef & userBits) | clipBit;
968 break;
969 case kNonZeroIfInClip_StencilFunc:
970 funcMask = (funcMask & userBits) | clipBit;
971 funcRef = clipBit;
972 break;
973 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000974 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +0000975 }
976 } else {
977 funcMask &= userBits;
978 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000979 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000980 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000981 gSpecialToBasicStencilFunc[respectClip];
982 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000983 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000984 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000985 funcMask &= userBits;
986 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000987 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000988
989 settings->setFunc(face, func);
990 settings->setWriteMask(face, writeMask);
991 settings->setFuncMask(face, funcMask);
992 settings->setFuncRef(face, funcRef);
993
994 if (GrStencilSettings::kFront_Face == face) {
995 face = GrStencilSettings::kBack_Face;
996 finished = !twoSided;
997 } else {
998 finished = true;
999 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001000 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001001 if (!twoSided) {
1002 settings->copyFrontSettingsToBack();
1003 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001004}
1005
1006////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +00001007GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001008 GrReducedClip::InitialState initialState,
1009 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -08001010 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001011 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001012 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001013
krajcevskiad1dc582014-06-10 15:06:47 -07001014 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -07001015 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001016 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001017 }
1018
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001019 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1020 // the top left corner of the resulting rect to the top left of the texture.
1021 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1022
robertphillips@google.com2c756812012-05-22 20:28:23 +00001023 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001024
joshualitt8059eb92014-12-29 15:10:07 -08001025 // Set the matrix so that rendered clip elements are transformed to mask space from clip
1026 // space.
1027 SkMatrix translate;
1028 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -08001029
joshualitt8059eb92014-12-29 15:10:07 -08001030 helper.init(maskSpaceIBounds, &translate, false);
tfarinabf54e492014-10-23 17:47:18 -07001031 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001032 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001033
tfarinabf54e492014-10-23 17:47:18 -07001034 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001035 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001036 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001037
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001038 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1039 // Intersect and reverse difference require modifying pixels outside of the geometry
1040 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1041 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1042 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001043 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001044 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001045 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001046 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001047 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001048 SkPath clipPath;
1049 element->asPath(&clipPath);
1050 clipPath.toggleInverseFillType();
1051 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001052 continue;
1053 }
1054
1055 // The other ops (union, xor, diff) only affect pixels inside
1056 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001057 if (Element::kRect_Type == element->getType()) {
1058 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1059 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001060 SkPath path;
1061 element->asPath(&path);
1062 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001063 }
1064 }
1065
krajcevskiad1dc582014-06-10 15:06:47 -07001066 // Allocate clip mask texture
1067 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, true);
1068 if (NULL == result) {
1069 fAACache.reset();
1070 return NULL;
1071 }
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001072 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001073
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001074 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001075 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001076}
1077
robertphillips@google.comf294b772012-04-27 14:29:26 +00001078////////////////////////////////////////////////////////////////////////////////
bsalomonc8dc1f72014-08-21 13:02:13 -07001079void GrClipMaskManager::purgeResources() {
1080 fAACache.purgeResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001081}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001082
joshualitt329bf482014-10-29 12:31:28 -07001083void GrClipMaskManager::setClipTarget(GrClipTarget* clipTarget) {
1084 fClipTarget = clipTarget;
1085 fAACache.setContext(clipTarget->getContext());
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001086}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001087
joshualitt9853cce2014-11-17 14:22:48 -08001088void GrClipMaskManager::adjustPathStencilParams(const GrStencilBuffer* stencilBuffer,
1089 GrStencilSettings* settings) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001090 // TODO: dynamically attach a stencil buffer
bsalomon49f085d2014-09-05 13:34:00 -07001091 if (stencilBuffer) {
joshualitt9853cce2014-11-17 14:22:48 -08001092 int stencilBits = stencilBuffer->bits();
joshualitt7a6184f2014-10-29 18:29:27 -07001093 this->adjustStencilParams(settings, fClipMode, stencilBits);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001094 }
1095}