blob: e87c7e1bc5f29f074af032585caf04647559960b [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,
365 GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000366 const SkClipStack::Element* element,
367 GrPathRenderer* pr) {
joshualitt9853cce2014-11-17 14:22:48 -0800368 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000369
370 drawState->setRenderTarget(target->asRenderTarget());
371
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000372 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000373 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000374 case Element::kEmpty_Type:
375 SkDEBUGFAIL("Should never get here with an empty element.");
376 break;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000377 case Element::kRect_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700378 // TODO: Do rects directly to the accumulator using a aa-rect GrProcessor that covers
379 // the entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000380 if (element->isAA()) {
joshualitt329bf482014-10-29 12:31:28 -0700381 this->getContext()->getAARectRenderer()->fillAARect(fClipTarget,
joshualitt9853cce2014-11-17 14:22:48 -0800382 drawState,
joshualitta58fe352014-10-27 08:39:00 -0700383 element->getRect(),
384 SkMatrix::I(),
385 element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000386 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800387 fClipTarget->drawSimpleRect(drawState, element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000388 }
389 return true;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000390 default: {
391 SkPath path;
392 element->asPath(&path);
jvanverthb3eb6872014-10-24 07:12:51 -0700393 path.setIsVolatile(true);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000394 if (path.isInverseFillType()) {
395 path.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000396 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000397 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000398 if (NULL == pr) {
399 GrPathRendererChain::DrawType type;
400 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
401 GrPathRendererChain::kColor_DrawType;
joshualitt9853cce2014-11-17 14:22:48 -0800402 pr = this->getContext()->getPathRenderer(fClipTarget, drawState, path, stroke,
403 false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000404 }
405 if (NULL == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000406 return false;
407 }
joshualitt9853cce2014-11-17 14:22:48 -0800408
409 pr->drawPath(fClipTarget, drawState, path, stroke, element->isAA());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000410 break;
411 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000412 }
413 return true;
414}
415
joshualitt9853cce2014-11-17 14:22:48 -0800416bool GrClipMaskManager::canStencilAndDrawElement(GrDrawState* drawState,
417 GrTexture* target,
418 GrPathRenderer** pr,
419 const SkClipStack::Element* element) {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000420 drawState->setRenderTarget(target->asRenderTarget());
421
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000422 if (Element::kRect_Type == element->getType()) {
423 return true;
424 } else {
425 // We shouldn't get here with an empty clip element.
426 SkASSERT(Element::kEmpty_Type != element->getType());
427 SkPath path;
428 element->asPath(&path);
429 if (path.isInverseFillType()) {
430 path.toggleInverseFillType();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000431 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000432 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
433 GrPathRendererChain::DrawType type = element->isAA() ?
434 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
435 GrPathRendererChain::kStencilAndColor_DrawType;
joshualitt9853cce2014-11-17 14:22:48 -0800436 *pr = this->getContext()->getPathRenderer(fClipTarget, drawState, path, stroke, false,
437 type);
bsalomon49f085d2014-09-05 13:34:00 -0700438 return SkToBool(*pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000439 }
440}
441
joshualitt9853cce2014-11-17 14:22:48 -0800442void GrClipMaskManager::mergeMask(GrDrawState* drawState,
443 GrTexture* dstMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000444 GrTexture* srcMask,
445 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000446 const SkIRect& dstBound,
447 const SkIRect& srcBound) {
joshualitt9853cce2014-11-17 14:22:48 -0800448 SkAssertResult(drawState->setIdentityViewMatrix());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000449
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000450 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000451
joshualitt9853cce2014-11-17 14:22:48 -0800452 setup_boolean_blendcoeffs(op, drawState);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000453
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000454 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000455 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000456
joshualittb0a8a372014-09-23 09:50:21 -0700457 drawState->addColorProcessor(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000458 GrTextureDomainEffect::Create(srcMask,
459 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000460 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
461 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000462 GrTextureParams::kNone_FilterMode))->unref();
joshualitt9853cce2014-11-17 14:22:48 -0800463 fClipTarget->drawSimpleRect(drawState, SkRect::Make(dstBound));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000464}
465
bsalomon427cf282014-10-16 13:41:43 -0700466GrTexture* GrClipMaskManager::createTempMask(int width, int height) {
bsalomonf2703d82014-10-28 14:33:06 -0700467 GrSurfaceDesc desc;
468 desc.fFlags = kRenderTarget_GrSurfaceFlag|kNoStencil_GrSurfaceFlag;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000469 desc.fWidth = width;
470 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000471 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000472
joshualitt329bf482014-10-29 12:31:28 -0700473 return this->getContext()->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000474}
475
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000476////////////////////////////////////////////////////////////////////////////////
krajcevskiad1dc582014-06-10 15:06:47 -0700477// Return the texture currently in the cache if it exists. Otherwise, return NULL
478GrTexture* GrClipMaskManager::getCachedMaskTexture(int32_t elementsGenID,
479 const SkIRect& clipSpaceIBounds) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000480 bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000481 if (!cached) {
krajcevskiad1dc582014-06-10 15:06:47 -0700482 return NULL;
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000483 }
484
krajcevskiad1dc582014-06-10 15:06:47 -0700485 return fAACache.getLastMask();
486}
487
488////////////////////////////////////////////////////////////////////////////////
489// Allocate a texture in the texture cache. This function returns the texture
490// allocated (or NULL on error).
491GrTexture* GrClipMaskManager::allocMaskTexture(int32_t elementsGenID,
492 const SkIRect& clipSpaceIBounds,
493 bool willUpload) {
494 // Since we are setting up the cache we should free up the
495 // currently cached mask so it can be reused.
496 fAACache.reset();
497
bsalomonf2703d82014-10-28 14:33:06 -0700498 GrSurfaceDesc desc;
499 desc.fFlags = willUpload ? kNone_GrSurfaceFlags : kRenderTarget_GrSurfaceFlag;
krajcevskiad1dc582014-06-10 15:06:47 -0700500 desc.fWidth = clipSpaceIBounds.width();
501 desc.fHeight = clipSpaceIBounds.height();
502 desc.fConfig = kRGBA_8888_GrPixelConfig;
503 if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
504 // We would always like A8 but it isn't supported on all platforms
505 desc.fConfig = kAlpha_8_GrPixelConfig;
506 }
507
508 fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds);
509 return fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000510}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000511
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000512////////////////////////////////////////////////////////////////////////////////
513// Create a 8-bit clip mask in alpha
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000514GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700515 GrReducedClip::InitialState initialState,
516 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000517 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000518 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000519
krajcevskiad1dc582014-06-10 15:06:47 -0700520 // First, check for cached texture
521 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -0700522 if (result) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000523 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000524 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000525 }
526
krajcevskiad1dc582014-06-10 15:06:47 -0700527 // There's no texture in the cache. Let's try to allocate it then.
528 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, false);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000529 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000530 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000531 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000532 }
533
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000534 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000535 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000536 SkIntToScalar(-clipSpaceIBounds.fLeft),
537 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000538 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000539 // The texture may be larger than necessary, this rect represents the part of the texture
540 // we populate with a rasterization of the clip.
541 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
542
bsalomon@google.com137f1342013-05-29 21:27:53 +0000543 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
544 SkMatrix translate;
545 translate.setTranslate(clipToMaskOffset);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000546
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000547 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
548 // clear the part that we care about.
joshualitt329bf482014-10-29 12:31:28 -0700549 fClipTarget->clear(&maskSpaceIBounds,
550 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
551 true,
552 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000553
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000554 // When we use the stencil in the below loop it is important to have this clip installed.
555 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
556 // pass must not set values outside of this bounds or stencil values outside the rect won't be
557 // cleared.
joshualitt329bf482014-10-29 12:31:28 -0700558 GrDrawTarget::AutoClipRestore acr(fClipTarget, maskSpaceIBounds);
bsalomon427cf282014-10-16 13:41:43 -0700559 SkAutoTUnref<GrTexture> temp;
joshualitt9853cce2014-11-17 14:22:48 -0800560
robertphillips@google.comf294b772012-04-27 14:29:26 +0000561 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700562 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000563 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000564 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000565 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000566 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
joshualitt9853cce2014-11-17 14:22:48 -0800567 GrDrawState drawState(translate);
568 // We're drawing a coverage mask and want coverage to be run through the blend function.
569 drawState.enableState(GrDrawState::kCoverageDrawing_StateBit |
570 GrDrawState::kClip_StateBit);
571
robertphillips@google.come79f3202014-02-11 16:30:21 +0000572 GrPathRenderer* pr = NULL;
joshualitt9853cce2014-11-17 14:22:48 -0800573 bool useTemp = !this->canStencilAndDrawElement(&drawState, result, &pr, element);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000574 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000575 // 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 +0000576 // mask buffer can be substantially larger than the actually clip stack element. We
577 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000578 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000579 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000580
581 if (useTemp) {
582 if (invert) {
583 maskSpaceElementIBounds = maskSpaceIBounds;
584 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000585 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000586 elementBounds.offset(clipToMaskOffset);
587 elementBounds.roundOut(&maskSpaceElementIBounds);
588 }
589
bsalomon427cf282014-10-16 13:41:43 -0700590 if (!temp) {
591 temp.reset(this->createTempMask(maskSpaceIBounds.fRight,
592 maskSpaceIBounds.fBottom));
593 if (!temp) {
594 fAACache.reset();
595 return NULL;
596 }
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000597 }
bsalomon427cf282014-10-16 13:41:43 -0700598 dst = temp;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000599 // clear the temp target and set blend to replace
joshualitt329bf482014-10-29 12:31:28 -0700600 fClipTarget->clear(&maskSpaceElementIBounds,
joshualitt9853cce2014-11-17 14:22:48 -0800601 invert ? 0xffffffff : 0x00000000,
602 true,
603 dst->asRenderTarget());
604 setup_boolean_blendcoeffs(SkRegion::kReplace_Op, &drawState);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000605 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000606 // draw directly into the result with the stencil set to make the pixels affected
607 // by the clip shape be non-zero.
608 dst = result;
609 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
610 kReplace_StencilOp,
611 kReplace_StencilOp,
612 kAlways_StencilFunc,
613 0xffff,
614 0xffff,
615 0xffff);
joshualitt9853cce2014-11-17 14:22:48 -0800616 drawState.setStencil(kStencilInElement);
617 setup_boolean_blendcoeffs(op, &drawState);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000618 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000619
joshualitt9853cce2014-11-17 14:22:48 -0800620 drawState.setAlpha(invert ? 0x00 : 0xff);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +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
626 if (!this->drawElement(&drawState, dst, element, pr)) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000627 fAACache.reset();
628 return NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000629 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000630
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000631 if (useTemp) {
632 // Now draw into the accumulator using the real operation and the temp buffer as a
633 // texture
joshualitt9853cce2014-11-17 14:22:48 -0800634 this->mergeMask(&backupDrawState,
635 result,
bsalomon427cf282014-10-16 13:41:43 -0700636 temp,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000637 op,
638 maskSpaceIBounds,
639 maskSpaceElementIBounds);
640 } else {
641 // Draw to the exterior pixels (those with a zero stencil value).
joshualitt9853cce2014-11-17 14:22:48 -0800642 backupDrawState.setAlpha(invert ? 0xff : 0x00);
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);
651 fClipTarget->drawSimpleRect(&backupDrawState, clipSpaceIBounds);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000652 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000653 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800654 GrDrawState drawState(translate);
655 drawState.enableState(GrDrawState::kCoverageDrawing_StateBit |
656 GrDrawState::kClip_StateBit);
657
robertphillips@google.come79f3202014-02-11 16:30:21 +0000658 // all the remaining ops can just be directly draw into the accumulation buffer
joshualitt9853cce2014-11-17 14:22:48 -0800659 drawState.setAlpha(0xff);
660 setup_boolean_blendcoeffs(op, &drawState);
661 this->drawElement(&drawState, 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 };
694 SkMatrix matrix;
695 matrix.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
715 GrDrawState drawState(matrix);
716 drawState.setRenderTarget(rt);
717 drawState.enableState(GrDrawState::kClip_StateBit);
718 drawState.enableState(GrDrawState::kNoColorWrites_StateBit);
719
720 // if the target is MSAA then we want MSAA enabled when the clip is soft
721 if (rt->isMultisampled()) {
722 drawState.setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
723 }
724
tomhudson@google.com8afae612012-08-14 15:03:35 +0000725 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000726 // enabled at bottom of loop
joshualitt7a6184f2014-10-29 18:29:27 -0700727 fClipMode = kIgnoreClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000728
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000729 // This will be used to determine whether the clip shape can be rendered into the
730 // stencil with arbitrary stencil settings.
731 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000732
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000733 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000734 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000735
robertphillips@google.come79f3202014-02-11 16:30:21 +0000736 GrPathRenderer* pr = NULL;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000737 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000738 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000739 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000740 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000741 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000742 element->asPath(&clipPath);
743 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000744 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000745 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000746 }
joshualitt9853cce2014-11-17 14:22:48 -0800747 pr = this->getContext()->getPathRenderer(fClipTarget,
748 &drawState,
749 clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000750 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000751 false,
752 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000753 &stencilSupport);
754 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000755 return false;
756 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000757 }
758
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000759 int passes;
760 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
761
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000762 bool canRenderDirectToStencil =
763 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000764 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000765 // fill rule, and set operation can
766 // we render the element directly to
767 // stencil bit used for clipping.
768 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
769 canRenderDirectToStencil,
770 clipBit,
771 fillInverted,
772 &passes,
773 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000774
775 // draw the element to the client stencil bits if necessary
776 if (!canDrawDirectToClip) {
777 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000778 kIncClamp_StencilOp,
779 kIncClamp_StencilOp,
780 kAlways_StencilFunc,
781 0xffff,
782 0x0000,
783 0xffff);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000784 if (Element::kRect_Type == element->getType()) {
joshualitt9853cce2014-11-17 14:22:48 -0800785 *drawState.stencil() = gDrawToStencil;
786 fClipTarget->drawSimpleRect(&drawState, element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000787 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000788 if (!clipPath.isEmpty()) {
joshualitt9853cce2014-11-17 14:22:48 -0800789 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000790 if (canRenderDirectToStencil) {
joshualitt9853cce2014-11-17 14:22:48 -0800791 *drawState.stencil() = gDrawToStencil;
792 pr->drawPath(fClipTarget, &drawState, clipPath, stroke, false);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000793 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800794 pr->stencilPath(fClipTarget, &drawState, clipPath, stroke);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000795 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000796 }
797 }
798 }
799
800 // now we modify the clip bit by rendering either the clip
801 // element directly or a bounding rect of the entire clip.
joshualitt7a6184f2014-10-29 18:29:27 -0700802 fClipMode = kModifyClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000803 for (int p = 0; p < passes; ++p) {
joshualitt9853cce2014-11-17 14:22:48 -0800804 GrDrawState drawStateCopy(drawState);
805 *drawStateCopy.stencil() = stencilSettings[p];
806
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000807 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000808 if (Element::kRect_Type == element->getType()) {
joshualitt9853cce2014-11-17 14:22:48 -0800809 fClipTarget->drawSimpleRect(&drawStateCopy, element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000810 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800811 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
812 pr->drawPath(fClipTarget, &drawStateCopy, clipPath, stroke, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000813 }
814 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000815 // The view matrix is setup to do clip space -> stencil space translation, so
816 // draw rect in clip space.
joshualitt9853cce2014-11-17 14:22:48 -0800817 fClipTarget->drawSimpleRect(&drawStateCopy, SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000818 }
819 }
820 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000821 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000822 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000823 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000824 fCurrClipMaskType = kStencil_ClipMaskType;
joshualitt7a6184f2014-10-29 18:29:27 -0700825 fClipMode = kRespectClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000826 return true;
827}
828
bsalomon@google.com411dad02012-06-05 20:24:20 +0000829// mapping of clip-respecting stencil funcs to normal stencil funcs
830// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000831static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000832 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
833 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
834 // In the Clip Funcs
835 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
836 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
837 kLess_StencilFunc, // kLessIfInClip_StencilFunc
838 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
839 // Special in the clip func that forces user's ref to be 0.
840 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
841 // make ref 0 and do normal nequal.
842 },
843 {// Stencil-Clipping is ENABLED
844 // In the Clip Funcs
845 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
846 // eq stencil clip bit, mask
847 // out user bits.
848
849 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
850 // add stencil bit to mask and ref
851
852 kLess_StencilFunc, // kLessIfInClip_StencilFunc
853 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
854 // for both of these we can add
855 // the clip bit to the mask and
856 // ref and compare as normal
857 // Special in the clip func that forces user's ref to be 0.
858 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
859 // make ref have only the clip bit set
860 // and make comparison be less
861 // 10..0 < 1..user_bits..
862 }
863};
864
bsalomon@google.coma3201942012-06-21 19:58:20 +0000865namespace {
866// Sets the settings to clip against the stencil buffer clip while ignoring the
867// client bits.
868const GrStencilSettings& basic_apply_stencil_clip_settings() {
869 // stencil settings to use when clip is in stencil
870 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
871 kKeep_StencilOp,
872 kKeep_StencilOp,
873 kAlwaysIfInClip_StencilFunc,
874 0x0000,
875 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000876 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000877 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
878}
879}
880
joshualitt9853cce2014-11-17 14:22:48 -0800881void GrClipMaskManager::setDrawStateStencil(GrDrawState* drawState,
882 GrDrawState::AutoRestoreStencil* ars) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000883 // We make two copies of the StencilSettings here (except in the early
884 // exit scenario. One copy from draw state to the stack var. Then another
885 // from the stack var to the gpu. We could make this class hold a ptr to
886 // GrGpu's fStencilSettings and eliminate the stack copy here.
887
bsalomon@google.coma3201942012-06-21 19:58:20 +0000888 // use stencil for clipping if clipping is enabled and the clip
889 // has been written into the stencil.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000890 GrStencilSettings settings;
joshualitt9853cce2014-11-17 14:22:48 -0800891
bsalomon@google.coma3201942012-06-21 19:58:20 +0000892 // The GrGpu client may not be using the stencil buffer but we may need to
893 // enable it in order to respect a stencil clip.
joshualitt9853cce2014-11-17 14:22:48 -0800894 if (drawState->getStencil().isDisabled()) {
joshualitt7a6184f2014-10-29 18:29:27 -0700895 if (GrClipMaskManager::kRespectClip_StencilClipMode == fClipMode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000896 settings = basic_apply_stencil_clip_settings();
897 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000898 return;
899 }
900 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800901 settings = drawState->getStencil();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000902 }
903
904 // TODO: dynamically attach a stencil buffer
905 int stencilBits = 0;
joshualitt9853cce2014-11-17 14:22:48 -0800906 GrStencilBuffer* stencilBuffer = drawState->getRenderTarget()->getStencilBuffer();
bsalomon49f085d2014-09-05 13:34:00 -0700907 if (stencilBuffer) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000908 stencilBits = stencilBuffer->bits();
909 }
910
joshualitt329bf482014-10-29 12:31:28 -0700911 SkASSERT(fClipTarget->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
912 SkASSERT(fClipTarget->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
joshualitt7a6184f2014-10-29 18:29:27 -0700913 this->adjustStencilParams(&settings, fClipMode, stencilBits);
joshualitt9853cce2014-11-17 14:22:48 -0800914 ars->set(drawState);
915 drawState->setStencil(settings);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000916}
917
918void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
919 StencilClipMode mode,
920 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000921 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000922
923 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000924 // We assume that this clip manager itself is drawing to the GrGpu and
925 // has already setup the correct values.
926 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000927 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000928
bsalomon@google.com411dad02012-06-05 20:24:20 +0000929 unsigned int clipBit = (1 << (stencilBitCnt - 1));
930 unsigned int userBits = clipBit - 1;
931
bsalomon@google.coma3201942012-06-21 19:58:20 +0000932 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
joshualitt329bf482014-10-29 12:31:28 -0700933 bool twoSided = fClipTarget->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000934
bsalomon@google.coma3201942012-06-21 19:58:20 +0000935 bool finished = false;
936 while (!finished) {
937 GrStencilFunc func = settings->func(face);
938 uint16_t writeMask = settings->writeMask(face);
939 uint16_t funcMask = settings->funcMask(face);
940 uint16_t funcRef = settings->funcRef(face);
941
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000942 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000943
944 writeMask &= userBits;
945
946 if (func >= kBasicStencilFuncCount) {
947 int respectClip = kRespectClip_StencilClipMode == mode;
948 if (respectClip) {
949 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000950 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000951 switch (func) {
952 case kAlwaysIfInClip_StencilFunc:
953 funcMask = clipBit;
954 funcRef = clipBit;
955 break;
956 case kEqualIfInClip_StencilFunc:
957 case kLessIfInClip_StencilFunc:
958 case kLEqualIfInClip_StencilFunc:
959 funcMask = (funcMask & userBits) | clipBit;
960 funcRef = (funcRef & userBits) | clipBit;
961 break;
962 case kNonZeroIfInClip_StencilFunc:
963 funcMask = (funcMask & userBits) | clipBit;
964 funcRef = clipBit;
965 break;
966 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000967 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +0000968 }
969 } else {
970 funcMask &= userBits;
971 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000972 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000973 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000974 gSpecialToBasicStencilFunc[respectClip];
975 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000976 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000977 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000978 funcMask &= userBits;
979 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000980 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000981
982 settings->setFunc(face, func);
983 settings->setWriteMask(face, writeMask);
984 settings->setFuncMask(face, funcMask);
985 settings->setFuncRef(face, funcRef);
986
987 if (GrStencilSettings::kFront_Face == face) {
988 face = GrStencilSettings::kBack_Face;
989 finished = !twoSided;
990 } else {
991 finished = true;
992 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000993 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000994 if (!twoSided) {
995 settings->copyFrontSettingsToBack();
996 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000997}
998
999////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +00001000GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001001 GrReducedClip::InitialState initialState,
1002 const GrReducedClip::ElementList& elements,
1003 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001004 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001005
krajcevskiad1dc582014-06-10 15:06:47 -07001006 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -07001007 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001008 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001009 }
1010
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001011 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1012 // the top left corner of the resulting rect to the top left of the texture.
1013 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1014
robertphillips@google.com2c756812012-05-22 20:28:23 +00001015 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001016
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001017 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001018 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
1019 SkIntToScalar(-clipSpaceIBounds.fTop));
joshualitt9853cce2014-11-17 14:22:48 -08001020
krajcevski71614ac2014-08-13 10:36:18 -07001021 helper.init(maskSpaceIBounds, &matrix, false);
tfarinabf54e492014-10-23 17:47:18 -07001022 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001023 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001024
tfarinabf54e492014-10-23 17:47:18 -07001025 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001026 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001027 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001028
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001029 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1030 // Intersect and reverse difference require modifying pixels outside of the geometry
1031 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1032 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1033 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001034 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001035 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001036 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001037 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001038 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001039 SkPath clipPath;
1040 element->asPath(&clipPath);
1041 clipPath.toggleInverseFillType();
1042 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001043 continue;
1044 }
1045
1046 // The other ops (union, xor, diff) only affect pixels inside
1047 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001048 if (Element::kRect_Type == element->getType()) {
1049 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1050 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001051 SkPath path;
1052 element->asPath(&path);
1053 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001054 }
1055 }
1056
krajcevskiad1dc582014-06-10 15:06:47 -07001057 // Allocate clip mask texture
1058 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, true);
1059 if (NULL == result) {
1060 fAACache.reset();
1061 return NULL;
1062 }
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001063 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001064
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001065 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001066 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001067}
1068
robertphillips@google.comf294b772012-04-27 14:29:26 +00001069////////////////////////////////////////////////////////////////////////////////
bsalomonc8dc1f72014-08-21 13:02:13 -07001070void GrClipMaskManager::purgeResources() {
1071 fAACache.purgeResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001072}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001073
joshualitt329bf482014-10-29 12:31:28 -07001074void GrClipMaskManager::setClipTarget(GrClipTarget* clipTarget) {
1075 fClipTarget = clipTarget;
1076 fAACache.setContext(clipTarget->getContext());
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001077}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001078
joshualitt9853cce2014-11-17 14:22:48 -08001079void GrClipMaskManager::adjustPathStencilParams(const GrStencilBuffer* stencilBuffer,
1080 GrStencilSettings* settings) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001081 // TODO: dynamically attach a stencil buffer
bsalomon49f085d2014-09-05 13:34:00 -07001082 if (stencilBuffer) {
joshualitt9853cce2014-11-17 14:22:48 -08001083 int stencilBits = stencilBuffer->bits();
joshualitt7a6184f2014-10-29 18:29:27 -07001084 this->adjustStencilParams(settings, fClipMode, stencilBits);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001085 }
1086}