blob: 5af6fb69ff508d86f600f37b10b6c66ac0dfb4fe [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrClipMaskManager.h"
robertphillips@google.comfa662942012-05-17 12:20:22 +00009#include "GrAAConvexPathRenderer.h"
10#include "GrAAHairLinePathRenderer.h"
jvanverth@google.combfe2b9d2013-09-06 16:57:29 +000011#include "GrAARectRenderer.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000012#include "GrDrawTargetCaps.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000013#include "GrPaint.h"
14#include "GrPathRenderer.h"
15#include "GrRenderTarget.h"
16#include "GrStencilBuffer.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000017#include "GrSWMaskHelper.h"
joshualitt3a0cfeb2014-10-27 07:38:01 -070018#include "SkRasterClip.h"
19#include "SkStrokeRec.h"
20#include "SkTLazy.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080021#include "effects/GrConvexPolyEffect.h"
egdaniel95131432014-12-09 11:15:43 -080022#include "effects/GrPorterDuffXferProcessor.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080023#include "effects/GrRRectEffect.h"
egdaniel95131432014-12-09 11:15:43 -080024#include "effects/GrTextureDomain.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000025
robertphillips@google.comba998f22012-10-12 11:33:56 +000026#define GR_AA_CLIP 1
bsalomon@google.com8182fa02012-12-04 14:06:06 +000027typedef SkClipStack::Element Element;
bsalomon@google.com51a62862012-11-26 21:19:43 +000028
29////////////////////////////////////////////////////////////////////////////////
robertphillips@google.come79f3202014-02-11 16:30:21 +000030namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000031// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000032// stage matrix this also alters the vertex layout
joshualitt9853cce2014-11-17 14:22:48 -080033void setup_drawstate_aaclip(const SkIRect &devBound,
34 GrDrawState* drawState,
35 GrTexture* result) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000036 SkASSERT(drawState);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000037
bsalomon@google.comb9086a02012-11-01 18:02:54 +000038 SkMatrix mat;
bsalomon@google.comc7818882013-03-20 19:19:53 +000039 // We want to use device coords to compute the texture coordinates. We set our matrix to be
40 // equal to the view matrix followed by an offset to the devBound, and then a scaling matrix to
41 // normalized coords. We apply this matrix to the vertex positions rather than local coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000042 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000043 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000044 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000045 mat.preConcat(drawState->getViewMatrix());
46
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000047 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000048 // This could be a long-lived effect that is cached with the alpha-mask.
joshualittb0a8a372014-09-23 09:50:21 -070049 drawState->addCoverageProcessor(
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000050 GrTextureDomainEffect::Create(result,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000051 mat,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000052 GrTextureDomain::MakeTexelDomain(result, domainTexels),
53 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +000054 GrTextureParams::kNone_FilterMode,
bsalomon@google.com77af6802013-10-02 13:04:56 +000055 kPosition_GrCoordSet))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000056}
57
robertphillips@google.come79f3202014-02-11 16:30:21 +000058bool path_needs_SW_renderer(GrContext* context,
joshualitt9853cce2014-11-17 14:22:48 -080059 const GrDrawTarget* gpu,
60 const GrDrawState* drawState,
robertphillips@google.come79f3202014-02-11 16:30:21 +000061 const SkPath& origPath,
62 const SkStrokeRec& stroke,
63 bool doAA) {
64 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
65 SkTCopyOnFirstWrite<SkPath> path(origPath);
66 if (path->isInverseFillType()) {
67 path.writable()->toggleInverseFillType();
68 }
69 // last (false) parameter disallows use of the SW path renderer
bsalomon@google.com45a15f52012-12-10 19:10:17 +000070 GrPathRendererChain::DrawType type = doAA ?
71 GrPathRendererChain::kColorAntiAlias_DrawType :
72 GrPathRendererChain::kColor_DrawType;
73
joshualitt9853cce2014-11-17 14:22:48 -080074 return NULL == context->getPathRenderer(gpu, drawState, *path, stroke, false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +000075}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000076}
77
robertphillips@google.comfa662942012-05-17 12:20:22 +000078/*
79 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
80 * will be used on any element. If so, it returns true to indicate that the
81 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
82 */
joshualitt9853cce2014-11-17 14:22:48 -080083bool GrClipMaskManager::useSWOnlyPath(const GrDrawState* drawState,
84 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000085 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000086 // a clip gets complex enough it can just be done in SW regardless
87 // of whether it would invoke the GrSoftwarePathRenderer.
sugoi@google.com5f74cf82012-12-17 21:16:45 +000088 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000089
tfarinabf54e492014-10-23 17:47:18 -070090 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000091 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000092 // rects can always be drawn directly w/o using the software path
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000093 // Skip rrects once we're drawing them directly.
94 if (Element::kRect_Type != element->getType()) {
95 SkPath path;
96 element->asPath(&path);
joshualitt9853cce2014-11-17 14:22:48 -080097 if (path_needs_SW_renderer(this->getContext(), fClipTarget, drawState, path, stroke,
joshualitt329bf482014-10-29 12:31:28 -070098 element->isAA())) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000099 return true;
100 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000101 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000102 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000103 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000104}
105
joshualitt9853cce2014-11-17 14:22:48 -0800106bool GrClipMaskManager::installClipEffects(GrDrawState* drawState,
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000107 GrDrawState::AutoRestoreEffects* are,
joshualitt9853cce2014-11-17 14:22:48 -0800108 const GrReducedClip::ElementList& elements,
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000109 const SkVector& clipToRTOffset,
mtklein217daa72014-07-02 12:55:21 -0700110 const SkRect* drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000111 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700112 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000113 boundsInClipSpace = *drawBounds;
114 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
115 }
116
117 are->set(drawState);
118 GrRenderTarget* rt = drawState->getRenderTarget();
tfarinabf54e492014-10-23 17:47:18 -0700119 GrReducedClip::ElementList::Iter iter(elements);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000120 bool failed = false;
bsalomon49f085d2014-09-05 13:34:00 -0700121 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000122 SkRegion::Op op = iter.get()->getOp();
123 bool invert;
124 bool skip = false;
125 switch (op) {
126 case SkRegion::kReplace_Op:
127 SkASSERT(iter.get() == elements.head());
128 // Fallthrough, handled same as intersect.
129 case SkRegion::kIntersect_Op:
130 invert = false;
bsalomon49f085d2014-09-05 13:34:00 -0700131 if (drawBounds && iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000132 skip = true;
133 }
134 break;
135 case SkRegion::kDifference_Op:
136 invert = true;
137 // We don't currently have a cheap test for whether a rect is fully outside an
138 // element's primitive, so don't attempt to set skip.
139 break;
140 default:
141 failed = true;
142 break;
143 }
144 if (failed) {
145 break;
146 }
147
148 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700149 GrPrimitiveEdgeType edgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000150 if (GR_AA_CLIP && iter.get()->isAA()) {
151 if (rt->isMultisampled()) {
mtklein217daa72014-07-02 12:55:21 -0700152 // Coverage based AA clips don't place nicely with MSAA.
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000153 failed = true;
154 break;
155 }
joshualittb0a8a372014-09-23 09:50:21 -0700156 edgeType =
157 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000158 } else {
joshualittb0a8a372014-09-23 09:50:21 -0700159 edgeType =
160 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000161 }
joshualittb0a8a372014-09-23 09:50:21 -0700162 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000163 switch (iter.get()->getType()) {
164 case SkClipStack::Element::kPath_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700165 fp.reset(GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000166 &clipToRTOffset));
167 break;
168 case SkClipStack::Element::kRRect_Type: {
169 SkRRect rrect = iter.get()->getRRect();
170 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
joshualittb0a8a372014-09-23 09:50:21 -0700171 fp.reset(GrRRectEffect::Create(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000172 break;
173 }
174 case SkClipStack::Element::kRect_Type: {
175 SkRect rect = iter.get()->getRect();
176 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
joshualittb0a8a372014-09-23 09:50:21 -0700177 fp.reset(GrConvexPolyEffect::Create(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000178 break;
179 }
180 default:
181 break;
182 }
joshualittb0a8a372014-09-23 09:50:21 -0700183 if (fp) {
joshualitt9853cce2014-11-17 14:22:48 -0800184 drawState->addCoverageProcessor(fp);
mtklein217daa72014-07-02 12:55:21 -0700185 } else {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000186 failed = true;
187 break;
188 }
189 }
mtklein217daa72014-07-02 12:55:21 -0700190 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000191 }
192
193 if (failed) {
194 are->set(NULL);
195 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000196 return !failed;
197}
198
robertphillips@google.comf294b772012-04-27 14:29:26 +0000199////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000200// sort out what kind of clip mask needs to be created: alpha, stencil,
201// scissor, or entirely software
joshualitt9853cce2014-11-17 14:22:48 -0800202bool GrClipMaskManager::setupClipping(GrDrawState* drawState,
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000203 GrDrawState::AutoRestoreEffects* are,
joshualitt77b13072014-10-27 14:51:01 -0700204 GrDrawState::AutoRestoreStencil* ars,
joshualitt9853cce2014-11-17 14:22:48 -0800205 ScissorState* scissorState,
206 const GrClipData* clipDataIn,
207 const SkRect* devBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000208 fCurrClipMaskType = kNone_ClipMaskType;
joshualitt7a6184f2014-10-29 18:29:27 -0700209 if (kRespectClip_StencilClipMode == fClipMode) {
210 fClipMode = kIgnoreClip_StencilClipMode;
211 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000212
tfarinabf54e492014-10-23 17:47:18 -0700213 GrReducedClip::ElementList elements(16);
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000214 int32_t genID;
tfarinabf54e492014-10-23 17:47:18 -0700215 GrReducedClip::InitialState initialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000216 SkIRect clipSpaceIBounds;
217 bool requiresAA;
joshualitt9853cce2014-11-17 14:22:48 -0800218 GrRenderTarget* rt = drawState->getRenderTarget();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000219
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000220 // GrDrawTarget should have filtered this for us
bsalomon49f085d2014-09-05 13:34:00 -0700221 SkASSERT(rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000222
223 bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000224 if (!ignoreClip) {
225 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
226 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
tfarinabf54e492014-10-23 17:47:18 -0700227 GrReducedClip::ReduceClipStack(*clipDataIn->fClipStack,
228 clipSpaceRTIBounds,
229 &elements,
230 &genID,
231 &initialState,
232 &clipSpaceIBounds,
233 &requiresAA);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000234 if (elements.isEmpty()) {
tfarinabf54e492014-10-23 17:47:18 -0700235 if (GrReducedClip::kAllIn_InitialState == initialState) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000236 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000237 } else {
238 return false;
239 }
240 }
241 }
242
243 if (ignoreClip) {
joshualitt9853cce2014-11-17 14:22:48 -0800244 this->setDrawStateStencil(drawState, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000245 return true;
246 }
247
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000248 // An element count of 4 was chosen because of the common pattern in Blink of:
249 // isect RR
250 // diff RR
251 // isect convex_poly
252 // isect convex_poly
253 // when drawing rounded div borders. This could probably be tuned based on a
254 // configuration's relative costs of switching RTs to generate a mask vs
255 // longer shaders.
256 if (elements.count() <= 4) {
257 SkVector clipToRTOffset = { SkIntToScalar(-clipDataIn->fOrigin.fX),
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000258 SkIntToScalar(-clipDataIn->fOrigin.fY) };
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000259 if (elements.isEmpty() ||
joshualitt9853cce2014-11-17 14:22:48 -0800260 (requiresAA && this->installClipEffects(drawState, are, elements, clipToRTOffset,
261 devBounds))) {
mtklein217daa72014-07-02 12:55:21 -0700262 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
263 scissorSpaceIBounds.offset(-clipDataIn->fOrigin);
264 if (NULL == devBounds ||
265 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
joshualitt77b13072014-10-27 14:51:01 -0700266 scissorState->set(scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000267 }
joshualitt9853cce2014-11-17 14:22:48 -0800268 this->setDrawStateStencil(drawState, ars);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000269 return true;
270 }
271 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000272
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000273#if GR_AA_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000274 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000275 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000276 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000277
joshualitt9853cce2014-11-17 14:22:48 -0800278 if (this->useSWOnlyPath(drawState, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000279 // The clip geometry is complex enough that it will be more efficient to create it
280 // entirely in software
281 result = this->createSoftwareClipMask(genID,
282 initialState,
283 elements,
284 clipSpaceIBounds);
285 } else {
286 result = this->createAlphaClipMask(genID,
287 initialState,
288 elements,
289 clipSpaceIBounds);
290 }
291
bsalomon49f085d2014-09-05 13:34:00 -0700292 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000293 // The mask's top left coord should be pinned to the rounded-out top left corner of
294 // clipSpace bounds. We determine the mask's position WRT to the render target here.
295 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
296 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
joshualitt9853cce2014-11-17 14:22:48 -0800297 setup_drawstate_aaclip(rtSpaceMaskBounds, drawState, result);
298 this->setDrawStateStencil(drawState, ars);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000299 return true;
300 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000301 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000302 }
303#endif // GR_AA_CLIP
304
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000305 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
306 // be created. In either case, free up the texture in the anti-aliased mask cache.
307 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
308 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
309 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000310 fAACache.reset();
311
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000312 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000313 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
joshualitt9853cce2014-11-17 14:22:48 -0800314 this->createStencilClipMask(rt,
315 genID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000316 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000317 elements,
318 clipSpaceIBounds,
319 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000320
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000321 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
322 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
323 // use both stencil and scissor test to the bounds for the final draw.
324 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
325 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
joshualitt77b13072014-10-27 14:51:01 -0700326 scissorState->set(scissorSpaceIBounds);
joshualitt9853cce2014-11-17 14:22:48 -0800327 this->setDrawStateStencil(drawState, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000328 return true;
329}
330
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000331namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000332////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000333// set up the OpenGL blend function to perform the specified
334// boolean operation for alpha clip mask creation
joshualitt9853cce2014-11-17 14:22:48 -0800335void setup_boolean_blendcoeffs(SkRegion::Op op, GrDrawState* drawState) {
egdaniel95131432014-12-09 11:15:43 -0800336 // TODO: once we have a coverageDrawing XP this will all use that instead of PD
robertphillips@google.comf294b772012-04-27 14:29:26 +0000337 switch (op) {
338 case SkRegion::kReplace_Op:
egdaniel95131432014-12-09 11:15:43 -0800339 drawState->setPorterDuffXPFactory(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000340 break;
341 case SkRegion::kIntersect_Op:
egdaniel95131432014-12-09 11:15:43 -0800342 drawState->setPorterDuffXPFactory(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000343 break;
344 case SkRegion::kUnion_Op:
egdaniel95131432014-12-09 11:15:43 -0800345 drawState->setPorterDuffXPFactory(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000346 break;
347 case SkRegion::kXOR_Op:
egdaniel95131432014-12-09 11:15:43 -0800348 drawState->setPorterDuffXPFactory(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000349 break;
350 case SkRegion::kDifference_Op:
egdaniel95131432014-12-09 11:15:43 -0800351 drawState->setPorterDuffXPFactory(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000352 break;
353 case SkRegion::kReverseDifference_Op:
egdaniel95131432014-12-09 11:15:43 -0800354 drawState->setPorterDuffXPFactory(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000355 break;
356 default:
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000357 SkASSERT(false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000358 break;
359 }
360}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000361}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000362
363////////////////////////////////////////////////////////////////////////////////
joshualitt9853cce2014-11-17 14:22:48 -0800364bool GrClipMaskManager::drawElement(GrDrawState* drawState,
joshualitt2e3b3e32014-12-09 13:31:14 -0800365 GrColor color,
joshualitt9853cce2014-11-17 14:22:48 -0800366 GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000367 const SkClipStack::Element* element,
368 GrPathRenderer* pr) {
joshualitt9853cce2014-11-17 14:22:48 -0800369 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000370
371 drawState->setRenderTarget(target->asRenderTarget());
372
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000373 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000374 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000375 case Element::kEmpty_Type:
376 SkDEBUGFAIL("Should never get here with an empty element.");
377 break;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000378 case Element::kRect_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700379 // TODO: Do rects directly to the accumulator using a aa-rect GrProcessor that covers
380 // the entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000381 if (element->isAA()) {
joshualitt329bf482014-10-29 12:31:28 -0700382 this->getContext()->getAARectRenderer()->fillAARect(fClipTarget,
joshualitt9853cce2014-11-17 14:22:48 -0800383 drawState,
joshualitt2e3b3e32014-12-09 13:31:14 -0800384 color,
joshualitta58fe352014-10-27 08:39:00 -0700385 element->getRect(),
386 SkMatrix::I(),
387 element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000388 } else {
joshualitt2e3b3e32014-12-09 13:31:14 -0800389 fClipTarget->drawSimpleRect(drawState, color, element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000390 }
391 return true;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000392 default: {
393 SkPath path;
394 element->asPath(&path);
jvanverthb3eb6872014-10-24 07:12:51 -0700395 path.setIsVolatile(true);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000396 if (path.isInverseFillType()) {
397 path.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000398 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000399 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000400 if (NULL == pr) {
401 GrPathRendererChain::DrawType type;
402 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
403 GrPathRendererChain::kColor_DrawType;
joshualitt9853cce2014-11-17 14:22:48 -0800404 pr = this->getContext()->getPathRenderer(fClipTarget, drawState, path, stroke,
405 false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000406 }
407 if (NULL == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000408 return false;
409 }
joshualitt9853cce2014-11-17 14:22:48 -0800410
joshualitt2e3b3e32014-12-09 13:31:14 -0800411 pr->drawPath(fClipTarget, drawState, color, path, stroke, element->isAA());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000412 break;
413 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000414 }
415 return true;
416}
417
joshualitt9853cce2014-11-17 14:22:48 -0800418bool GrClipMaskManager::canStencilAndDrawElement(GrDrawState* drawState,
419 GrTexture* target,
420 GrPathRenderer** pr,
421 const SkClipStack::Element* element) {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000422 drawState->setRenderTarget(target->asRenderTarget());
423
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000424 if (Element::kRect_Type == element->getType()) {
425 return true;
426 } else {
427 // We shouldn't get here with an empty clip element.
428 SkASSERT(Element::kEmpty_Type != element->getType());
429 SkPath path;
430 element->asPath(&path);
431 if (path.isInverseFillType()) {
432 path.toggleInverseFillType();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000433 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000434 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
435 GrPathRendererChain::DrawType type = element->isAA() ?
436 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
437 GrPathRendererChain::kStencilAndColor_DrawType;
joshualitt9853cce2014-11-17 14:22:48 -0800438 *pr = this->getContext()->getPathRenderer(fClipTarget, drawState, path, stroke, false,
439 type);
bsalomon49f085d2014-09-05 13:34:00 -0700440 return SkToBool(*pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000441 }
442}
443
joshualitt9853cce2014-11-17 14:22:48 -0800444void GrClipMaskManager::mergeMask(GrDrawState* drawState,
445 GrTexture* dstMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000446 GrTexture* srcMask,
447 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000448 const SkIRect& dstBound,
449 const SkIRect& srcBound) {
joshualitt9853cce2014-11-17 14:22:48 -0800450 SkAssertResult(drawState->setIdentityViewMatrix());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000451
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000452 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000453
joshualitt9853cce2014-11-17 14:22:48 -0800454 setup_boolean_blendcoeffs(op, drawState);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000455
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000456 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000457 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000458
joshualittb0a8a372014-09-23 09:50:21 -0700459 drawState->addColorProcessor(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000460 GrTextureDomainEffect::Create(srcMask,
461 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000462 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
463 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000464 GrTextureParams::kNone_FilterMode))->unref();
joshualitt2e3b3e32014-12-09 13:31:14 -0800465 fClipTarget->drawSimpleRect(drawState, GrColor_WHITE, SkRect::Make(dstBound));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000466}
467
bsalomon427cf282014-10-16 13:41:43 -0700468GrTexture* GrClipMaskManager::createTempMask(int width, int height) {
bsalomonf2703d82014-10-28 14:33:06 -0700469 GrSurfaceDesc desc;
470 desc.fFlags = kRenderTarget_GrSurfaceFlag|kNoStencil_GrSurfaceFlag;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000471 desc.fWidth = width;
472 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000473 desc.fConfig = kAlpha_8_GrPixelConfig;
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,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000519 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000520 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000521
krajcevskiad1dc582014-06-10 15:06:47 -0700522 // First, check for cached texture
523 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -0700524 if (result) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000525 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000526 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000527 }
528
krajcevskiad1dc582014-06-10 15:06:47 -0700529 // There's no texture in the cache. Let's try to allocate it then.
530 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, false);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000531 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000532 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000533 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000534 }
535
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000536 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000537 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000538 SkIntToScalar(-clipSpaceIBounds.fLeft),
539 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000540 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000541 // The texture may be larger than necessary, this rect represents the part of the texture
542 // we populate with a rasterization of the clip.
543 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
544
bsalomon@google.com137f1342013-05-29 21:27:53 +0000545 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
546 SkMatrix translate;
547 translate.setTranslate(clipToMaskOffset);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000548
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000549 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
550 // clear the part that we care about.
joshualitt329bf482014-10-29 12:31:28 -0700551 fClipTarget->clear(&maskSpaceIBounds,
552 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
553 true,
554 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000555
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000556 // When we use the stencil in the below loop it is important to have this clip installed.
557 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
558 // pass must not set values outside of this bounds or stencil values outside the rect won't be
559 // cleared.
joshualitt329bf482014-10-29 12:31:28 -0700560 GrDrawTarget::AutoClipRestore acr(fClipTarget, maskSpaceIBounds);
bsalomon427cf282014-10-16 13:41:43 -0700561 SkAutoTUnref<GrTexture> temp;
joshualitt9853cce2014-11-17 14:22:48 -0800562
robertphillips@google.comf294b772012-04-27 14:29:26 +0000563 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700564 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000565 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000566 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000567 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000568 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
joshualitt9853cce2014-11-17 14:22:48 -0800569 GrDrawState drawState(translate);
570 // We're drawing a coverage mask and want coverage to be run through the blend function.
571 drawState.enableState(GrDrawState::kCoverageDrawing_StateBit |
572 GrDrawState::kClip_StateBit);
573
robertphillips@google.come79f3202014-02-11 16:30:21 +0000574 GrPathRenderer* pr = NULL;
joshualitt9853cce2014-11-17 14:22:48 -0800575 bool useTemp = !this->canStencilAndDrawElement(&drawState, result, &pr, element);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000576 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000577 // 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 +0000578 // mask buffer can be substantially larger than the actually clip stack element. We
579 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000580 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000581 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000582
583 if (useTemp) {
584 if (invert) {
585 maskSpaceElementIBounds = maskSpaceIBounds;
586 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000587 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000588 elementBounds.offset(clipToMaskOffset);
589 elementBounds.roundOut(&maskSpaceElementIBounds);
590 }
591
bsalomon427cf282014-10-16 13:41:43 -0700592 if (!temp) {
593 temp.reset(this->createTempMask(maskSpaceIBounds.fRight,
594 maskSpaceIBounds.fBottom));
595 if (!temp) {
596 fAACache.reset();
597 return NULL;
598 }
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000599 }
bsalomon427cf282014-10-16 13:41:43 -0700600 dst = temp;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000601 // clear the temp target and set blend to replace
joshualitt329bf482014-10-29 12:31:28 -0700602 fClipTarget->clear(&maskSpaceElementIBounds,
joshualitt9853cce2014-11-17 14:22:48 -0800603 invert ? 0xffffffff : 0x00000000,
604 true,
605 dst->asRenderTarget());
606 setup_boolean_blendcoeffs(SkRegion::kReplace_Op, &drawState);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000607 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000608 // draw directly into the result with the stencil set to make the pixels affected
609 // by the clip shape be non-zero.
610 dst = result;
611 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
612 kReplace_StencilOp,
613 kReplace_StencilOp,
614 kAlways_StencilFunc,
615 0xffff,
616 0xffff,
617 0xffff);
joshualitt9853cce2014-11-17 14:22:48 -0800618 drawState.setStencil(kStencilInElement);
619 setup_boolean_blendcoeffs(op, &drawState);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000620 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000621
joshualitt9853cce2014-11-17 14:22:48 -0800622 // We have to backup the drawstate because the drawElement call may call into
623 // renderers which consume it.
624 GrDrawState backupDrawState(drawState);
625
joshualitt2e3b3e32014-12-09 13:31:14 -0800626 if (!this->drawElement(&drawState, invert ? GrColor_TRANS_BLACK :
627 GrColor_WHITE, dst, element, pr)) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000628 fAACache.reset();
629 return NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000630 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000631
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000632 if (useTemp) {
633 // Now draw into the accumulator using the real operation and the temp buffer as a
634 // texture
joshualitt9853cce2014-11-17 14:22:48 -0800635 this->mergeMask(&backupDrawState,
636 result,
bsalomon427cf282014-10-16 13:41:43 -0700637 temp,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000638 op,
639 maskSpaceIBounds,
640 maskSpaceElementIBounds);
641 } else {
642 // Draw to the exterior pixels (those with a zero stencil value).
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000643 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
644 kZero_StencilOp,
645 kZero_StencilOp,
646 kEqual_StencilFunc,
647 0xffff,
648 0x0000,
649 0xffff);
joshualitt9853cce2014-11-17 14:22:48 -0800650 backupDrawState.setStencil(kDrawOutsideElement);
joshualitt2e3b3e32014-12-09 13:31:14 -0800651 fClipTarget->drawSimpleRect(&backupDrawState,
652 invert ? GrColor_WHITE : GrColor_TRANS_BLACK,
653 clipSpaceIBounds);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000654 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000655 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800656 GrDrawState drawState(translate);
657 drawState.enableState(GrDrawState::kCoverageDrawing_StateBit |
658 GrDrawState::kClip_StateBit);
659
robertphillips@google.come79f3202014-02-11 16:30:21 +0000660 // all the remaining ops can just be directly draw into the accumulation buffer
joshualitt9853cce2014-11-17 14:22:48 -0800661 setup_boolean_blendcoeffs(op, &drawState);
joshualitt2e3b3e32014-12-09 13:31:14 -0800662 this->drawElement(&drawState, GrColor_WHITE, result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000663 }
664 }
665
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000666 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000667 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000668}
669
670////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000671// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000672// (as opposed to canvas) coordinates
joshualitt9853cce2014-11-17 14:22:48 -0800673bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt,
674 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700675 GrReducedClip::InitialState initialState,
676 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000677 const SkIRect& clipSpaceIBounds,
678 const SkIPoint& clipSpaceToStencilOffset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000679 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon49f085d2014-09-05 13:34:00 -0700680 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000681
682 // TODO: dynamically attach a SB when needed.
683 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
684 if (NULL == stencilBuffer) {
685 return false;
686 }
687
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000688 if (stencilBuffer->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000689 stencilBuffer->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000690 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
691 SkVector translate = {
692 SkIntToScalar(clipSpaceToStencilOffset.fX),
693 SkIntToScalar(clipSpaceToStencilOffset.fY)
694 };
695 SkMatrix matrix;
696 matrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000697
bsalomon@google.com9f131742012-12-13 20:43:56 +0000698 // We set the current clip to the bounds so that our recursive draws are scissored to them.
699 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
700 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
joshualitt329bf482014-10-29 12:31:28 -0700701 GrDrawTarget::AutoClipRestore acr(fClipTarget, stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000702
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000703 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000704 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000705 clipBit = (1 << (clipBit-1));
706
joshualitt329bf482014-10-29 12:31:28 -0700707 fClipTarget->clearStencilClip(stencilSpaceIBounds,
708 GrReducedClip::kAllIn_InitialState == initialState,
709 rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000710
711 // walk through each clip element and perform its set op
712 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700713 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000714 const Element* element = iter.get();
joshualitt9853cce2014-11-17 14:22:48 -0800715
716 GrDrawState drawState(matrix);
717 drawState.setRenderTarget(rt);
718 drawState.enableState(GrDrawState::kClip_StateBit);
719 drawState.enableState(GrDrawState::kNoColorWrites_StateBit);
720
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,
750 clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000751 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000752 false,
753 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000754 &stencilSupport);
755 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000756 return false;
757 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000758 }
759
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000760 int passes;
761 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
762
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000763 bool canRenderDirectToStencil =
764 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000765 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000766 // fill rule, and set operation can
767 // we render the element directly to
768 // stencil bit used for clipping.
769 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
770 canRenderDirectToStencil,
771 clipBit,
772 fillInverted,
773 &passes,
774 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000775
776 // draw the element to the client stencil bits if necessary
777 if (!canDrawDirectToClip) {
778 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000779 kIncClamp_StencilOp,
780 kIncClamp_StencilOp,
781 kAlways_StencilFunc,
782 0xffff,
783 0x0000,
784 0xffff);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000785 if (Element::kRect_Type == element->getType()) {
joshualitt9853cce2014-11-17 14:22:48 -0800786 *drawState.stencil() = gDrawToStencil;
joshualitt2e3b3e32014-12-09 13:31:14 -0800787 fClipTarget->drawSimpleRect(&drawState, GrColor_WHITE, element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000788 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000789 if (!clipPath.isEmpty()) {
joshualitt9853cce2014-11-17 14:22:48 -0800790 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000791 if (canRenderDirectToStencil) {
joshualitt9853cce2014-11-17 14:22:48 -0800792 *drawState.stencil() = gDrawToStencil;
joshualitt2e3b3e32014-12-09 13:31:14 -0800793 pr->drawPath(fClipTarget, &drawState, GrColor_WHITE, clipPath, stroke,
794 false);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000795 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800796 pr->stencilPath(fClipTarget, &drawState, clipPath, stroke);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000797 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000798 }
799 }
800 }
801
802 // now we modify the clip bit by rendering either the clip
803 // element directly or a bounding rect of the entire clip.
joshualitt7a6184f2014-10-29 18:29:27 -0700804 fClipMode = kModifyClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000805 for (int p = 0; p < passes; ++p) {
joshualitt9853cce2014-11-17 14:22:48 -0800806 GrDrawState drawStateCopy(drawState);
807 *drawStateCopy.stencil() = stencilSettings[p];
808
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000809 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000810 if (Element::kRect_Type == element->getType()) {
joshualitt2e3b3e32014-12-09 13:31:14 -0800811 fClipTarget->drawSimpleRect(&drawStateCopy, GrColor_WHITE,
812 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000813 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800814 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
joshualitt2e3b3e32014-12-09 13:31:14 -0800815 pr->drawPath(fClipTarget, &drawStateCopy, GrColor_WHITE, clipPath, stroke, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000816 }
817 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000818 // The view matrix is setup to do clip space -> stencil space translation, so
819 // draw rect in clip space.
joshualitt2e3b3e32014-12-09 13:31:14 -0800820 fClipTarget->drawSimpleRect(&drawStateCopy, GrColor_WHITE,
821 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000822 }
823 }
824 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000825 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000826 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000827 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000828 fCurrClipMaskType = kStencil_ClipMaskType;
joshualitt7a6184f2014-10-29 18:29:27 -0700829 fClipMode = kRespectClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000830 return true;
831}
832
bsalomon@google.com411dad02012-06-05 20:24:20 +0000833// mapping of clip-respecting stencil funcs to normal stencil funcs
834// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000835static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000836 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
837 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
838 // In the Clip Funcs
839 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
840 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
841 kLess_StencilFunc, // kLessIfInClip_StencilFunc
842 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
843 // Special in the clip func that forces user's ref to be 0.
844 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
845 // make ref 0 and do normal nequal.
846 },
847 {// Stencil-Clipping is ENABLED
848 // In the Clip Funcs
849 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
850 // eq stencil clip bit, mask
851 // out user bits.
852
853 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
854 // add stencil bit to mask and ref
855
856 kLess_StencilFunc, // kLessIfInClip_StencilFunc
857 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
858 // for both of these we can add
859 // the clip bit to the mask and
860 // ref and compare as normal
861 // Special in the clip func that forces user's ref to be 0.
862 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
863 // make ref have only the clip bit set
864 // and make comparison be less
865 // 10..0 < 1..user_bits..
866 }
867};
868
bsalomon@google.coma3201942012-06-21 19:58:20 +0000869namespace {
870// Sets the settings to clip against the stencil buffer clip while ignoring the
871// client bits.
872const GrStencilSettings& basic_apply_stencil_clip_settings() {
873 // stencil settings to use when clip is in stencil
874 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
875 kKeep_StencilOp,
876 kKeep_StencilOp,
877 kAlwaysIfInClip_StencilFunc,
878 0x0000,
879 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000880 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000881 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
882}
883}
884
joshualitt9853cce2014-11-17 14:22:48 -0800885void GrClipMaskManager::setDrawStateStencil(GrDrawState* drawState,
886 GrDrawState::AutoRestoreStencil* ars) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000887 // We make two copies of the StencilSettings here (except in the early
888 // exit scenario. One copy from draw state to the stack var. Then another
889 // from the stack var to the gpu. We could make this class hold a ptr to
890 // GrGpu's fStencilSettings and eliminate the stack copy here.
891
bsalomon@google.coma3201942012-06-21 19:58:20 +0000892 // use stencil for clipping if clipping is enabled and the clip
893 // has been written into the stencil.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000894 GrStencilSettings settings;
joshualitt9853cce2014-11-17 14:22:48 -0800895
bsalomon@google.coma3201942012-06-21 19:58:20 +0000896 // The GrGpu client may not be using the stencil buffer but we may need to
897 // enable it in order to respect a stencil clip.
joshualitt9853cce2014-11-17 14:22:48 -0800898 if (drawState->getStencil().isDisabled()) {
joshualitt7a6184f2014-10-29 18:29:27 -0700899 if (GrClipMaskManager::kRespectClip_StencilClipMode == fClipMode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000900 settings = basic_apply_stencil_clip_settings();
901 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000902 return;
903 }
904 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800905 settings = drawState->getStencil();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000906 }
907
908 // TODO: dynamically attach a stencil buffer
909 int stencilBits = 0;
joshualitt9853cce2014-11-17 14:22:48 -0800910 GrStencilBuffer* stencilBuffer = drawState->getRenderTarget()->getStencilBuffer();
bsalomon49f085d2014-09-05 13:34:00 -0700911 if (stencilBuffer) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000912 stencilBits = stencilBuffer->bits();
913 }
914
joshualitt329bf482014-10-29 12:31:28 -0700915 SkASSERT(fClipTarget->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
916 SkASSERT(fClipTarget->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
joshualitt7a6184f2014-10-29 18:29:27 -0700917 this->adjustStencilParams(&settings, fClipMode, stencilBits);
joshualitt9853cce2014-11-17 14:22:48 -0800918 ars->set(drawState);
919 drawState->setStencil(settings);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000920}
921
922void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
923 StencilClipMode mode,
924 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000925 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000926
927 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000928 // We assume that this clip manager itself is drawing to the GrGpu and
929 // has already setup the correct values.
930 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000931 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000932
bsalomon@google.com411dad02012-06-05 20:24:20 +0000933 unsigned int clipBit = (1 << (stencilBitCnt - 1));
934 unsigned int userBits = clipBit - 1;
935
bsalomon@google.coma3201942012-06-21 19:58:20 +0000936 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
joshualitt329bf482014-10-29 12:31:28 -0700937 bool twoSided = fClipTarget->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000938
bsalomon@google.coma3201942012-06-21 19:58:20 +0000939 bool finished = false;
940 while (!finished) {
941 GrStencilFunc func = settings->func(face);
942 uint16_t writeMask = settings->writeMask(face);
943 uint16_t funcMask = settings->funcMask(face);
944 uint16_t funcRef = settings->funcRef(face);
945
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000946 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000947
948 writeMask &= userBits;
949
950 if (func >= kBasicStencilFuncCount) {
951 int respectClip = kRespectClip_StencilClipMode == mode;
952 if (respectClip) {
953 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000954 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000955 switch (func) {
956 case kAlwaysIfInClip_StencilFunc:
957 funcMask = clipBit;
958 funcRef = clipBit;
959 break;
960 case kEqualIfInClip_StencilFunc:
961 case kLessIfInClip_StencilFunc:
962 case kLEqualIfInClip_StencilFunc:
963 funcMask = (funcMask & userBits) | clipBit;
964 funcRef = (funcRef & userBits) | clipBit;
965 break;
966 case kNonZeroIfInClip_StencilFunc:
967 funcMask = (funcMask & userBits) | clipBit;
968 funcRef = clipBit;
969 break;
970 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000971 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +0000972 }
973 } else {
974 funcMask &= userBits;
975 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000976 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000977 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000978 gSpecialToBasicStencilFunc[respectClip];
979 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000980 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000981 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000982 funcMask &= userBits;
983 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000984 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000985
986 settings->setFunc(face, func);
987 settings->setWriteMask(face, writeMask);
988 settings->setFuncMask(face, funcMask);
989 settings->setFuncRef(face, funcRef);
990
991 if (GrStencilSettings::kFront_Face == face) {
992 face = GrStencilSettings::kBack_Face;
993 finished = !twoSided;
994 } else {
995 finished = true;
996 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000997 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000998 if (!twoSided) {
999 settings->copyFrontSettingsToBack();
1000 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001001}
1002
1003////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +00001004GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001005 GrReducedClip::InitialState initialState,
1006 const GrReducedClip::ElementList& elements,
1007 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001008 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001009
krajcevskiad1dc582014-06-10 15:06:47 -07001010 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -07001011 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001012 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001013 }
1014
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001015 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1016 // the top left corner of the resulting rect to the top left of the texture.
1017 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1018
robertphillips@google.com2c756812012-05-22 20:28:23 +00001019 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001020
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001021 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001022 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
1023 SkIntToScalar(-clipSpaceIBounds.fTop));
joshualitt9853cce2014-11-17 14:22:48 -08001024
krajcevski71614ac2014-08-13 10:36:18 -07001025 helper.init(maskSpaceIBounds, &matrix, false);
tfarinabf54e492014-10-23 17:47:18 -07001026 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001027 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001028
tfarinabf54e492014-10-23 17:47:18 -07001029 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001030 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001031 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001032
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001033 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1034 // Intersect and reverse difference require modifying pixels outside of the geometry
1035 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1036 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1037 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001038 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001039 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001040 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001041 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001042 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001043 SkPath clipPath;
1044 element->asPath(&clipPath);
1045 clipPath.toggleInverseFillType();
1046 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001047 continue;
1048 }
1049
1050 // The other ops (union, xor, diff) only affect pixels inside
1051 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001052 if (Element::kRect_Type == element->getType()) {
1053 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1054 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001055 SkPath path;
1056 element->asPath(&path);
1057 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001058 }
1059 }
1060
krajcevskiad1dc582014-06-10 15:06:47 -07001061 // Allocate clip mask texture
1062 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, true);
1063 if (NULL == result) {
1064 fAACache.reset();
1065 return NULL;
1066 }
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001067 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001068
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001069 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001070 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001071}
1072
robertphillips@google.comf294b772012-04-27 14:29:26 +00001073////////////////////////////////////////////////////////////////////////////////
bsalomonc8dc1f72014-08-21 13:02:13 -07001074void GrClipMaskManager::purgeResources() {
1075 fAACache.purgeResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001076}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001077
joshualitt329bf482014-10-29 12:31:28 -07001078void GrClipMaskManager::setClipTarget(GrClipTarget* clipTarget) {
1079 fClipTarget = clipTarget;
1080 fAACache.setContext(clipTarget->getContext());
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001081}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001082
joshualitt9853cce2014-11-17 14:22:48 -08001083void GrClipMaskManager::adjustPathStencilParams(const GrStencilBuffer* stencilBuffer,
1084 GrStencilSettings* settings) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001085 // TODO: dynamically attach a stencil buffer
bsalomon49f085d2014-09-05 13:34:00 -07001086 if (stencilBuffer) {
joshualitt9853cce2014-11-17 14:22:48 -08001087 int stencilBits = stencilBuffer->bits();
joshualitt7a6184f2014-10-29 18:29:27 -07001088 this->adjustStencilParams(settings, fClipMode, stencilBits);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001089 }
1090}