blob: 4d9612534d6973a9d274725c8c69672c92b0e1a1 [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"
bsalomon6bc1b5f2015-02-23 09:06:38 -080016#include "GrRenderTargetPriv.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000017#include "GrStencilBuffer.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000018#include "GrSWMaskHelper.h"
joshualitt3a0cfeb2014-10-27 07:38:01 -070019#include "SkRasterClip.h"
20#include "SkStrokeRec.h"
21#include "SkTLazy.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080022#include "effects/GrConvexPolyEffect.h"
egdaniel95131432014-12-09 11:15:43 -080023#include "effects/GrPorterDuffXferProcessor.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080024#include "effects/GrRRectEffect.h"
egdaniel95131432014-12-09 11:15:43 -080025#include "effects/GrTextureDomain.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000026
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,
egdaniel8dd688b2015-01-22 10:16:09 -080034 GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -080035 GrTexture* result) {
egdaniel8dd688b2015-01-22 10:16:09 -080036 SkASSERT(pipelineBuilder);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000037
bsalomon@google.comb9086a02012-11-01 18:02:54 +000038 SkMatrix mat;
bsalomon309d4d52014-12-18 10:17:44 -080039 // We use device coords to compute the texture coordinates. We set our matrix to be a
40 // translation to the devBound, and then a scaling matrix to normalized coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000041 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000042 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000043 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000044
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000045 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000046 // This could be a long-lived effect that is cached with the alpha-mask.
egdaniel8dd688b2015-01-22 10:16:09 -080047 pipelineBuilder->addCoverageProcessor(
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000048 GrTextureDomainEffect::Create(result,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000049 mat,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000050 GrTextureDomain::MakeTexelDomain(result, domainTexels),
51 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +000052 GrTextureParams::kNone_FilterMode,
bsalomon309d4d52014-12-18 10:17:44 -080053 kDevice_GrCoordSet))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000054}
55
robertphillips@google.come79f3202014-02-11 16:30:21 +000056bool path_needs_SW_renderer(GrContext* context,
joshualitt9853cce2014-11-17 14:22:48 -080057 const GrDrawTarget* gpu,
egdaniel8dd688b2015-01-22 10:16:09 -080058 const GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -080059 const SkMatrix& viewMatrix,
robertphillips@google.come79f3202014-02-11 16:30:21 +000060 const SkPath& origPath,
61 const SkStrokeRec& stroke,
62 bool doAA) {
63 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
64 SkTCopyOnFirstWrite<SkPath> path(origPath);
65 if (path->isInverseFillType()) {
66 path.writable()->toggleInverseFillType();
67 }
68 // last (false) parameter disallows use of the SW path renderer
bsalomon@google.com45a15f52012-12-10 19:10:17 +000069 GrPathRendererChain::DrawType type = doAA ?
70 GrPathRendererChain::kColorAntiAlias_DrawType :
71 GrPathRendererChain::kColor_DrawType;
72
egdaniel8dd688b2015-01-22 10:16:09 -080073 return NULL == context->getPathRenderer(gpu, pipelineBuilder, viewMatrix, *path, stroke,
74 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 */
egdaniel8dd688b2015-01-22 10:16:09 -080083bool GrClipMaskManager::useSWOnlyPath(const GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -080084 const SkVector& clipToMaskOffset,
joshualitt9853cce2014-11-17 14:22:48 -080085 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000086 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000087 // a clip gets complex enough it can just be done in SW regardless
88 // of whether it would invoke the GrSoftwarePathRenderer.
sugoi@google.com5f74cf82012-12-17 21:16:45 +000089 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000090
joshualitt8059eb92014-12-29 15:10:07 -080091 // Set the matrix so that rendered clip elements are transformed to mask space from clip
92 // space.
93 SkMatrix translate;
94 translate.setTranslate(clipToMaskOffset);
95
tfarinabf54e492014-10-23 17:47:18 -070096 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000097 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000098 // rects can always be drawn directly w/o using the software path
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000099 // Skip rrects once we're drawing them directly.
100 if (Element::kRect_Type != element->getType()) {
101 SkPath path;
102 element->asPath(&path);
egdaniel8dd688b2015-01-22 10:16:09 -0800103 if (path_needs_SW_renderer(this->getContext(), fClipTarget, pipelineBuilder, translate,
joshualitt8059eb92014-12-29 15:10:07 -0800104 path, stroke, element->isAA())) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000105 return true;
106 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000107 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000108 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000109 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000110}
111
egdaniel8dd688b2015-01-22 10:16:09 -0800112bool GrClipMaskManager::installClipEffects(GrPipelineBuilder* pipelineBuilder,
113 GrPipelineBuilder::AutoRestoreEffects* are,
joshualitt9853cce2014-11-17 14:22:48 -0800114 const GrReducedClip::ElementList& elements,
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000115 const SkVector& clipToRTOffset,
mtklein217daa72014-07-02 12:55:21 -0700116 const SkRect* drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000117 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700118 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000119 boundsInClipSpace = *drawBounds;
120 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
121 }
122
egdaniel8dd688b2015-01-22 10:16:09 -0800123 are->set(pipelineBuilder);
124 GrRenderTarget* rt = pipelineBuilder->getRenderTarget();
tfarinabf54e492014-10-23 17:47:18 -0700125 GrReducedClip::ElementList::Iter iter(elements);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000126 bool failed = false;
bsalomon49f085d2014-09-05 13:34:00 -0700127 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000128 SkRegion::Op op = iter.get()->getOp();
129 bool invert;
130 bool skip = false;
131 switch (op) {
132 case SkRegion::kReplace_Op:
133 SkASSERT(iter.get() == elements.head());
134 // Fallthrough, handled same as intersect.
135 case SkRegion::kIntersect_Op:
136 invert = false;
bsalomon49f085d2014-09-05 13:34:00 -0700137 if (drawBounds && iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000138 skip = true;
139 }
140 break;
141 case SkRegion::kDifference_Op:
142 invert = true;
143 // We don't currently have a cheap test for whether a rect is fully outside an
144 // element's primitive, so don't attempt to set skip.
145 break;
146 default:
147 failed = true;
148 break;
149 }
150 if (failed) {
151 break;
152 }
153
154 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700155 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800156 if (iter.get()->isAA()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000157 if (rt->isMultisampled()) {
mtklein217daa72014-07-02 12:55:21 -0700158 // Coverage based AA clips don't place nicely with MSAA.
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000159 failed = true;
160 break;
161 }
joshualittb0a8a372014-09-23 09:50:21 -0700162 edgeType =
163 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000164 } else {
joshualittb0a8a372014-09-23 09:50:21 -0700165 edgeType =
166 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000167 }
joshualittb0a8a372014-09-23 09:50:21 -0700168 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000169 switch (iter.get()->getType()) {
170 case SkClipStack::Element::kPath_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700171 fp.reset(GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000172 &clipToRTOffset));
173 break;
174 case SkClipStack::Element::kRRect_Type: {
175 SkRRect rrect = iter.get()->getRRect();
176 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
joshualittb0a8a372014-09-23 09:50:21 -0700177 fp.reset(GrRRectEffect::Create(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000178 break;
179 }
180 case SkClipStack::Element::kRect_Type: {
181 SkRect rect = iter.get()->getRect();
182 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
joshualittb0a8a372014-09-23 09:50:21 -0700183 fp.reset(GrConvexPolyEffect::Create(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000184 break;
185 }
186 default:
187 break;
188 }
joshualittb0a8a372014-09-23 09:50:21 -0700189 if (fp) {
egdaniel8dd688b2015-01-22 10:16:09 -0800190 pipelineBuilder->addCoverageProcessor(fp);
mtklein217daa72014-07-02 12:55:21 -0700191 } else {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000192 failed = true;
193 break;
194 }
195 }
mtklein217daa72014-07-02 12:55:21 -0700196 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000197 }
198
199 if (failed) {
200 are->set(NULL);
201 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000202 return !failed;
203}
204
robertphillips@google.comf294b772012-04-27 14:29:26 +0000205////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000206// sort out what kind of clip mask needs to be created: alpha, stencil,
207// scissor, or entirely software
egdaniel8dd688b2015-01-22 10:16:09 -0800208bool GrClipMaskManager::setupClipping(GrPipelineBuilder* pipelineBuilder,
209 GrPipelineBuilder::AutoRestoreEffects* are,
210 GrPipelineBuilder::AutoRestoreStencil* ars,
bsalomon3e791242014-12-17 13:43:13 -0800211 GrScissorState* scissorState,
joshualitt9853cce2014-11-17 14:22:48 -0800212 const SkRect* devBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000213 fCurrClipMaskType = kNone_ClipMaskType;
joshualitt7a6184f2014-10-29 18:29:27 -0700214 if (kRespectClip_StencilClipMode == fClipMode) {
215 fClipMode = kIgnoreClip_StencilClipMode;
216 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000217
tfarinabf54e492014-10-23 17:47:18 -0700218 GrReducedClip::ElementList elements(16);
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000219 int32_t genID;
tfarinabf54e492014-10-23 17:47:18 -0700220 GrReducedClip::InitialState initialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000221 SkIRect clipSpaceIBounds;
222 bool requiresAA;
egdaniel8dd688b2015-01-22 10:16:09 -0800223 GrRenderTarget* rt = pipelineBuilder->getRenderTarget();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000224
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000225 // GrDrawTarget should have filtered this for us
bsalomon49f085d2014-09-05 13:34:00 -0700226 SkASSERT(rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000227
joshualitt44701df2015-02-23 14:44:57 -0800228 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
229 const GrClip& clip = pipelineBuilder->clip();
230 // TODO we shouldn't be ignoring the clip mask manager's clip. This is temporary.
231 bool ignoreClip = clip.isWideOpen(clipSpaceRTIBounds) ||
232 GrClip::kIRect_ClipType == clip.clipType();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000233 if (!ignoreClip) {
joshualitt44701df2015-02-23 14:44:57 -0800234 // The clip mask manager always draws with a single IRect so we special case that logic here
235 if (GrClip::kIRect_ClipType == clip.clipType()) {
236 initialState = GrReducedClip::kAllIn_InitialState;
237 clipSpaceIBounds = clip.irect();
238 SkNEW_INSERT_AT_LLIST_HEAD(&elements,
239 Element,
240 (SkRect::Make(clipSpaceIBounds),
241 SkRegion::kIntersect_Op, false));
242 } else {
243 clipSpaceRTIBounds.offset(clip.origin());
244 GrReducedClip::ReduceClipStack(*clip.clipStack(),
245 clipSpaceRTIBounds,
246 &elements,
247 &genID,
248 &initialState,
249 &clipSpaceIBounds,
250 &requiresAA);
251 if (elements.isEmpty()) {
252 if (GrReducedClip::kAllIn_InitialState == initialState) {
253 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
254 } else {
255 return false;
256 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000257 }
258 }
259 }
260
261 if (ignoreClip) {
egdaniel8dd688b2015-01-22 10:16:09 -0800262 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000263 return true;
264 }
265
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000266 // An element count of 4 was chosen because of the common pattern in Blink of:
267 // isect RR
268 // diff RR
269 // isect convex_poly
270 // isect convex_poly
271 // when drawing rounded div borders. This could probably be tuned based on a
272 // configuration's relative costs of switching RTs to generate a mask vs
273 // longer shaders.
274 if (elements.count() <= 4) {
joshualitt44701df2015-02-23 14:44:57 -0800275 SkVector clipToRTOffset = { SkIntToScalar(-clip.origin().fX),
276 SkIntToScalar(-clip.origin().fY) };
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000277 if (elements.isEmpty() ||
bsalomon25965672015-02-23 09:28:30 -0800278 this->installClipEffects(pipelineBuilder, are, elements, clipToRTOffset, devBounds)) {
mtklein217daa72014-07-02 12:55:21 -0700279 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
joshualitt44701df2015-02-23 14:44:57 -0800280 scissorSpaceIBounds.offset(-clip.origin());
mtklein217daa72014-07-02 12:55:21 -0700281 if (NULL == devBounds ||
282 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
joshualitt77b13072014-10-27 14:51:01 -0700283 scissorState->set(scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000284 }
egdaniel8dd688b2015-01-22 10:16:09 -0800285 this->setPipelineBuilderStencil(pipelineBuilder, ars);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000286 return true;
287 }
288 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000289
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000290 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000291 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000292 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000293
joshualitt8059eb92014-12-29 15:10:07 -0800294 // The top-left of the mask corresponds to the top-left corner of the bounds.
295 SkVector clipToMaskOffset = {
296 SkIntToScalar(-clipSpaceIBounds.fLeft),
297 SkIntToScalar(-clipSpaceIBounds.fTop)
298 };
299
egdaniel8dd688b2015-01-22 10:16:09 -0800300 if (this->useSWOnlyPath(pipelineBuilder, clipToMaskOffset, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000301 // The clip geometry is complex enough that it will be more efficient to create it
302 // entirely in software
303 result = this->createSoftwareClipMask(genID,
304 initialState,
305 elements,
joshualitt8059eb92014-12-29 15:10:07 -0800306 clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000307 clipSpaceIBounds);
308 } else {
309 result = this->createAlphaClipMask(genID,
310 initialState,
311 elements,
joshualitt8059eb92014-12-29 15:10:07 -0800312 clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000313 clipSpaceIBounds);
314 }
315
bsalomon49f085d2014-09-05 13:34:00 -0700316 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000317 // The mask's top left coord should be pinned to the rounded-out top left corner of
318 // clipSpace bounds. We determine the mask's position WRT to the render target here.
319 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
joshualitt44701df2015-02-23 14:44:57 -0800320 rtSpaceMaskBounds.offset(-clip.origin());
egdaniel8dd688b2015-01-22 10:16:09 -0800321 setup_drawstate_aaclip(rtSpaceMaskBounds, pipelineBuilder, result);
322 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000323 return true;
324 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000325 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000326 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000327
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000328 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
329 // be created. In either case, free up the texture in the anti-aliased mask cache.
330 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
331 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
332 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000333 fAACache.reset();
334
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000335 // use the stencil clip if we can't represent the clip as a rectangle.
joshualitt44701df2015-02-23 14:44:57 -0800336 SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin();
joshualitt9853cce2014-11-17 14:22:48 -0800337 this->createStencilClipMask(rt,
338 genID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000339 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000340 elements,
341 clipSpaceIBounds,
342 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000343
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000344 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
345 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
346 // use both stencil and scissor test to the bounds for the final draw.
347 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
348 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
joshualitt77b13072014-10-27 14:51:01 -0700349 scissorState->set(scissorSpaceIBounds);
egdaniel8dd688b2015-01-22 10:16:09 -0800350 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000351 return true;
352}
353
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000354namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000355////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800356// Set a coverage drawing XPF on the pipelineBuilder for the given op and invertCoverage mode
357void set_coverage_drawing_xpf(SkRegion::Op op, bool invertCoverage,
358 GrPipelineBuilder* pipelineBuilder) {
egdaniel87509242014-12-17 13:37:13 -0800359 SkASSERT(op <= SkRegion::kLastOp);
egdaniel8dd688b2015-01-22 10:16:09 -0800360 pipelineBuilder->setCoverageSetOpXPFactory(op, invertCoverage);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000361}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000362}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000363
364////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800365bool GrClipMaskManager::drawElement(GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800366 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800367 GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000368 const SkClipStack::Element* element,
369 GrPathRenderer* pr) {
joshualitt9853cce2014-11-17 14:22:48 -0800370 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000371
egdaniel8dd688b2015-01-22 10:16:09 -0800372 pipelineBuilder->setRenderTarget(target->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000373
egdaniel87509242014-12-17 13:37:13 -0800374 // The color we use to draw does not matter since we will always be using a GrCoverageSetOpXP
375 // which ignores color.
376 GrColor color = GrColor_WHITE;
377
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000378 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000379 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000380 case Element::kEmpty_Type:
381 SkDEBUGFAIL("Should never get here with an empty element.");
382 break;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000383 case Element::kRect_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700384 // TODO: Do rects directly to the accumulator using a aa-rect GrProcessor that covers
385 // the entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000386 if (element->isAA()) {
joshualitt8059eb92014-12-29 15:10:07 -0800387 SkRect devRect = element->getRect();
388 viewMatrix.mapRect(&devRect);
joshualitt329bf482014-10-29 12:31:28 -0700389 this->getContext()->getAARectRenderer()->fillAARect(fClipTarget,
egdaniel8dd688b2015-01-22 10:16:09 -0800390 pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -0800391 color,
joshualitt8059eb92014-12-29 15:10:07 -0800392 viewMatrix,
joshualitta58fe352014-10-27 08:39:00 -0700393 element->getRect(),
joshualitt8059eb92014-12-29 15:10:07 -0800394 devRect);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000395 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800396 fClipTarget->drawSimpleRect(pipelineBuilder, color, viewMatrix, element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000397 }
398 return true;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000399 default: {
400 SkPath path;
401 element->asPath(&path);
jvanverthb3eb6872014-10-24 07:12:51 -0700402 path.setIsVolatile(true);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000403 if (path.isInverseFillType()) {
404 path.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000405 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000406 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000407 if (NULL == pr) {
408 GrPathRendererChain::DrawType type;
409 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
410 GrPathRendererChain::kColor_DrawType;
egdaniel8dd688b2015-01-22 10:16:09 -0800411 pr = this->getContext()->getPathRenderer(fClipTarget, pipelineBuilder, viewMatrix,
412 path, stroke, false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000413 }
414 if (NULL == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000415 return false;
416 }
joshualitt9853cce2014-11-17 14:22:48 -0800417
egdaniel8dd688b2015-01-22 10:16:09 -0800418 pr->drawPath(fClipTarget, pipelineBuilder, color, viewMatrix, path, stroke,
419 element->isAA());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000420 break;
421 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000422 }
423 return true;
424}
425
egdaniel8dd688b2015-01-22 10:16:09 -0800426bool GrClipMaskManager::canStencilAndDrawElement(GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800427 GrTexture* target,
428 GrPathRenderer** pr,
429 const SkClipStack::Element* element) {
egdaniel8dd688b2015-01-22 10:16:09 -0800430 pipelineBuilder->setRenderTarget(target->asRenderTarget());
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000431
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000432 if (Element::kRect_Type == element->getType()) {
433 return true;
434 } else {
435 // We shouldn't get here with an empty clip element.
436 SkASSERT(Element::kEmpty_Type != element->getType());
437 SkPath path;
438 element->asPath(&path);
439 if (path.isInverseFillType()) {
440 path.toggleInverseFillType();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000441 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000442 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
443 GrPathRendererChain::DrawType type = element->isAA() ?
444 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
445 GrPathRendererChain::kStencilAndColor_DrawType;
egdaniel8dd688b2015-01-22 10:16:09 -0800446 *pr = this->getContext()->getPathRenderer(fClipTarget, pipelineBuilder, SkMatrix::I(), path,
joshualitt8059eb92014-12-29 15:10:07 -0800447 stroke, false, type);
bsalomon49f085d2014-09-05 13:34:00 -0700448 return SkToBool(*pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000449 }
450}
451
egdaniel8dd688b2015-01-22 10:16:09 -0800452void GrClipMaskManager::mergeMask(GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800453 GrTexture* dstMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000454 GrTexture* srcMask,
455 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000456 const SkIRect& dstBound,
457 const SkIRect& srcBound) {
egdaniel8dd688b2015-01-22 10:16:09 -0800458 pipelineBuilder->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000459
egdaniel87509242014-12-17 13:37:13 -0800460 // We want to invert the coverage here
egdaniel8dd688b2015-01-22 10:16:09 -0800461 set_coverage_drawing_xpf(op, false, pipelineBuilder);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000462
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000463 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000464 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000465
egdaniel8dd688b2015-01-22 10:16:09 -0800466 pipelineBuilder->addCoverageProcessor(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000467 GrTextureDomainEffect::Create(srcMask,
468 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000469 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
470 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000471 GrTextureParams::kNone_FilterMode))->unref();
egdaniel87509242014-12-17 13:37:13 -0800472 // The color passed in here does not matter since the coverageSetOpXP won't read it.
joshualitt44701df2015-02-23 14:44:57 -0800473 fClipTarget->drawSimpleRect(pipelineBuilder,
474 GrColor_WHITE,
475 SkMatrix::I(),
egdaniel8dd688b2015-01-22 10:16:09 -0800476 SkRect::Make(dstBound));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000477}
478
bsalomon427cf282014-10-16 13:41:43 -0700479GrTexture* GrClipMaskManager::createTempMask(int width, int height) {
bsalomonf2703d82014-10-28 14:33:06 -0700480 GrSurfaceDesc desc;
bsalomon3f490a02014-12-18 06:20:52 -0800481 desc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000482 desc.fWidth = width;
483 desc.fHeight = height;
bsalomon51d1f7e2014-12-22 08:40:49 -0800484 if (this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
485 desc.fConfig = kAlpha_8_GrPixelConfig;
486 } else {
487 desc.fConfig = kRGBA_8888_GrPixelConfig;
488 }
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000489
joshualitt329bf482014-10-29 12:31:28 -0700490 return this->getContext()->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000491}
492
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000493////////////////////////////////////////////////////////////////////////////////
krajcevskiad1dc582014-06-10 15:06:47 -0700494// Return the texture currently in the cache if it exists. Otherwise, return NULL
495GrTexture* GrClipMaskManager::getCachedMaskTexture(int32_t elementsGenID,
496 const SkIRect& clipSpaceIBounds) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000497 bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000498 if (!cached) {
krajcevskiad1dc582014-06-10 15:06:47 -0700499 return NULL;
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000500 }
501
krajcevskiad1dc582014-06-10 15:06:47 -0700502 return fAACache.getLastMask();
503}
504
505////////////////////////////////////////////////////////////////////////////////
506// Allocate a texture in the texture cache. This function returns the texture
507// allocated (or NULL on error).
508GrTexture* GrClipMaskManager::allocMaskTexture(int32_t elementsGenID,
509 const SkIRect& clipSpaceIBounds,
510 bool willUpload) {
511 // Since we are setting up the cache we should free up the
512 // currently cached mask so it can be reused.
513 fAACache.reset();
514
bsalomonf2703d82014-10-28 14:33:06 -0700515 GrSurfaceDesc desc;
516 desc.fFlags = willUpload ? kNone_GrSurfaceFlags : kRenderTarget_GrSurfaceFlag;
krajcevskiad1dc582014-06-10 15:06:47 -0700517 desc.fWidth = clipSpaceIBounds.width();
518 desc.fHeight = clipSpaceIBounds.height();
519 desc.fConfig = kRGBA_8888_GrPixelConfig;
520 if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
521 // We would always like A8 but it isn't supported on all platforms
522 desc.fConfig = kAlpha_8_GrPixelConfig;
523 }
524
525 fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds);
526 return fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000527}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000528
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000529////////////////////////////////////////////////////////////////////////////////
530// Create a 8-bit clip mask in alpha
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000531GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700532 GrReducedClip::InitialState initialState,
533 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800534 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000535 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000536 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000537
krajcevskiad1dc582014-06-10 15:06:47 -0700538 // First, check for cached texture
539 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -0700540 if (result) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000541 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000542 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000543 }
544
krajcevskiad1dc582014-06-10 15:06:47 -0700545 // There's no texture in the cache. Let's try to allocate it then.
546 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, false);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000547 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000548 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000549 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000550 }
551
joshualitt8059eb92014-12-29 15:10:07 -0800552 // Set the matrix so that rendered clip elements are transformed to mask space from clip
553 // space.
554 SkMatrix translate;
555 translate.setTranslate(clipToMaskOffset);
556
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000557 // The texture may be larger than necessary, this rect represents the part of the texture
558 // we populate with a rasterization of the clip.
559 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
560
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000561 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
562 // clear the part that we care about.
joshualitt329bf482014-10-29 12:31:28 -0700563 fClipTarget->clear(&maskSpaceIBounds,
564 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
565 true,
566 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000567
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000568 // When we use the stencil in the below loop it is important to have this clip installed.
569 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
570 // pass must not set values outside of this bounds or stencil values outside the rect won't be
571 // cleared.
joshualitt44701df2015-02-23 14:44:57 -0800572 GrClip clip(maskSpaceIBounds);
bsalomon427cf282014-10-16 13:41:43 -0700573 SkAutoTUnref<GrTexture> temp;
joshualitt9853cce2014-11-17 14:22:48 -0800574
robertphillips@google.comf294b772012-04-27 14:29:26 +0000575 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700576 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000577 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000578 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000579 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000580 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
egdaniel8dd688b2015-01-22 10:16:09 -0800581 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800582
joshualitt44701df2015-02-23 14:44:57 -0800583 pipelineBuilder.setClip(clip);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000584 GrPathRenderer* pr = NULL;
egdaniel8dd688b2015-01-22 10:16:09 -0800585 bool useTemp = !this->canStencilAndDrawElement(&pipelineBuilder, result, &pr, element);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000586 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000587 // 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 +0000588 // mask buffer can be substantially larger than the actually clip stack element. We
589 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000590 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000591 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000592
593 if (useTemp) {
594 if (invert) {
595 maskSpaceElementIBounds = maskSpaceIBounds;
596 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000597 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000598 elementBounds.offset(clipToMaskOffset);
599 elementBounds.roundOut(&maskSpaceElementIBounds);
600 }
601
bsalomon427cf282014-10-16 13:41:43 -0700602 if (!temp) {
603 temp.reset(this->createTempMask(maskSpaceIBounds.fRight,
604 maskSpaceIBounds.fBottom));
605 if (!temp) {
606 fAACache.reset();
607 return NULL;
608 }
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000609 }
bsalomon427cf282014-10-16 13:41:43 -0700610 dst = temp;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000611 // clear the temp target and set blend to replace
joshualitt329bf482014-10-29 12:31:28 -0700612 fClipTarget->clear(&maskSpaceElementIBounds,
joshualitt9853cce2014-11-17 14:22:48 -0800613 invert ? 0xffffffff : 0x00000000,
614 true,
615 dst->asRenderTarget());
egdaniel8dd688b2015-01-22 10:16:09 -0800616 set_coverage_drawing_xpf(SkRegion::kReplace_Op, invert, &pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000617 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000618 // draw directly into the result with the stencil set to make the pixels affected
619 // by the clip shape be non-zero.
620 dst = result;
621 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
622 kReplace_StencilOp,
623 kReplace_StencilOp,
624 kAlways_StencilFunc,
625 0xffff,
626 0xffff,
627 0xffff);
egdaniel8dd688b2015-01-22 10:16:09 -0800628 pipelineBuilder.setStencil(kStencilInElement);
629 set_coverage_drawing_xpf(op, invert, &pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000630 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000631
egdaniel8dd688b2015-01-22 10:16:09 -0800632 if (!this->drawElement(&pipelineBuilder, translate, dst, element, pr)) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000633 fAACache.reset();
634 return NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000635 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000636
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000637 if (useTemp) {
egdaniel8dd688b2015-01-22 10:16:09 -0800638 GrPipelineBuilder backgroundPipelineBuilder;
egdaniel8dd688b2015-01-22 10:16:09 -0800639 backgroundPipelineBuilder.setRenderTarget(result->asRenderTarget());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800640
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000641 // Now draw into the accumulator using the real operation and the temp buffer as a
642 // texture
egdaniel8dd688b2015-01-22 10:16:09 -0800643 this->mergeMask(&backgroundPipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800644 result,
bsalomon427cf282014-10-16 13:41:43 -0700645 temp,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000646 op,
647 maskSpaceIBounds,
648 maskSpaceElementIBounds);
649 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800650 GrPipelineBuilder backgroundPipelineBuilder;
egdaniel8dd688b2015-01-22 10:16:09 -0800651 backgroundPipelineBuilder.setRenderTarget(result->asRenderTarget());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800652
egdaniel8dd688b2015-01-22 10:16:09 -0800653 set_coverage_drawing_xpf(op, !invert, &backgroundPipelineBuilder);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000654 // Draw to the exterior pixels (those with a zero stencil value).
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000655 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
656 kZero_StencilOp,
657 kZero_StencilOp,
658 kEqual_StencilFunc,
659 0xffff,
660 0x0000,
661 0xffff);
egdaniel8dd688b2015-01-22 10:16:09 -0800662 backgroundPipelineBuilder.setStencil(kDrawOutsideElement);
egdaniel87509242014-12-17 13:37:13 -0800663 // The color passed in here does not matter since the coverageSetOpXP won't read it.
egdaniel8dd688b2015-01-22 10:16:09 -0800664 fClipTarget->drawSimpleRect(&backgroundPipelineBuilder, GrColor_WHITE, translate,
joshualitt8059eb92014-12-29 15:10:07 -0800665 clipSpaceIBounds);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000666 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000667 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800668 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800669
robertphillips@google.come79f3202014-02-11 16:30:21 +0000670 // all the remaining ops can just be directly draw into the accumulation buffer
egdaniel8dd688b2015-01-22 10:16:09 -0800671 set_coverage_drawing_xpf(op, false, &pipelineBuilder);
egdaniel87509242014-12-17 13:37:13 -0800672 // The color passed in here does not matter since the coverageSetOpXP won't read it.
egdaniel8dd688b2015-01-22 10:16:09 -0800673 this->drawElement(&pipelineBuilder, translate, result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000674 }
675 }
676
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000677 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000678 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000679}
680
681////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000682// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000683// (as opposed to canvas) coordinates
joshualitt9853cce2014-11-17 14:22:48 -0800684bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt,
685 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700686 GrReducedClip::InitialState initialState,
687 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000688 const SkIRect& clipSpaceIBounds,
689 const SkIPoint& clipSpaceToStencilOffset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000690 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon49f085d2014-09-05 13:34:00 -0700691 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000692
bsalomon6bc1b5f2015-02-23 09:06:38 -0800693 GrStencilBuffer* stencilBuffer = rt->renderTargetPriv().attachStencilBuffer();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000694 if (NULL == stencilBuffer) {
695 return false;
696 }
697
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000698 if (stencilBuffer->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000699 stencilBuffer->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000700 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
701 SkVector translate = {
702 SkIntToScalar(clipSpaceToStencilOffset.fX),
703 SkIntToScalar(clipSpaceToStencilOffset.fY)
704 };
joshualitt8059eb92014-12-29 15:10:07 -0800705 SkMatrix viewMatrix;
706 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000707
bsalomon@google.com9f131742012-12-13 20:43:56 +0000708 // We set the current clip to the bounds so that our recursive draws are scissored to them.
709 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
710 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
joshualitt44701df2015-02-23 14:44:57 -0800711 GrClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000712
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000713 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000714 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000715 clipBit = (1 << (clipBit-1));
716
joshualitt329bf482014-10-29 12:31:28 -0700717 fClipTarget->clearStencilClip(stencilSpaceIBounds,
718 GrReducedClip::kAllIn_InitialState == initialState,
719 rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000720
721 // walk through each clip element and perform its set op
722 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700723 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000724 const Element* element = iter.get();
joshualitt9853cce2014-11-17 14:22:48 -0800725
egdaniel8dd688b2015-01-22 10:16:09 -0800726 GrPipelineBuilder pipelineBuilder;
joshualitt44701df2015-02-23 14:44:57 -0800727 pipelineBuilder.setClip(clip);
egdaniel8dd688b2015-01-22 10:16:09 -0800728 pipelineBuilder.setRenderTarget(rt);
egdaniel080e6732014-12-22 07:35:52 -0800729
egdaniel8dd688b2015-01-22 10:16:09 -0800730 pipelineBuilder.setDisableColorXPFactory();
joshualitt9853cce2014-11-17 14:22:48 -0800731
732 // if the target is MSAA then we want MSAA enabled when the clip is soft
733 if (rt->isMultisampled()) {
egdaniel8dd688b2015-01-22 10:16:09 -0800734 pipelineBuilder.setState(GrPipelineBuilder::kHWAntialias_StateBit, element->isAA());
joshualitt9853cce2014-11-17 14:22:48 -0800735 }
736
tomhudson@google.com8afae612012-08-14 15:03:35 +0000737 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000738 // enabled at bottom of loop
joshualitt7a6184f2014-10-29 18:29:27 -0700739 fClipMode = kIgnoreClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000740
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000741 // This will be used to determine whether the clip shape can be rendered into the
742 // stencil with arbitrary stencil settings.
743 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000744
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000745 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000746 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000747
robertphillips@google.come79f3202014-02-11 16:30:21 +0000748 GrPathRenderer* pr = NULL;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000749 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000750 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000751 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000752 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000753 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000754 element->asPath(&clipPath);
755 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000756 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000757 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000758 }
joshualitt9853cce2014-11-17 14:22:48 -0800759 pr = this->getContext()->getPathRenderer(fClipTarget,
egdaniel8dd688b2015-01-22 10:16:09 -0800760 &pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800761 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800762 clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000763 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000764 false,
765 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000766 &stencilSupport);
767 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000768 return false;
769 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000770 }
771
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000772 int passes;
773 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
774
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000775 bool canRenderDirectToStencil =
776 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000777 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000778 // fill rule, and set operation can
779 // we render the element directly to
780 // stencil bit used for clipping.
781 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
782 canRenderDirectToStencil,
783 clipBit,
784 fillInverted,
785 &passes,
786 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000787
788 // draw the element to the client stencil bits if necessary
789 if (!canDrawDirectToClip) {
790 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000791 kIncClamp_StencilOp,
792 kIncClamp_StencilOp,
793 kAlways_StencilFunc,
794 0xffff,
795 0x0000,
796 0xffff);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000797 if (Element::kRect_Type == element->getType()) {
egdaniel8dd688b2015-01-22 10:16:09 -0800798 *pipelineBuilder.stencil() = gDrawToStencil;
joshualitt44701df2015-02-23 14:44:57 -0800799 fClipTarget->drawSimpleRect(&pipelineBuilder,
800 GrColor_WHITE,
801 viewMatrix,
joshualitt8059eb92014-12-29 15:10:07 -0800802 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000803 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000804 if (!clipPath.isEmpty()) {
joshualitt9853cce2014-11-17 14:22:48 -0800805 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000806 if (canRenderDirectToStencil) {
egdaniel8dd688b2015-01-22 10:16:09 -0800807 *pipelineBuilder.stencil() = gDrawToStencil;
joshualitt44701df2015-02-23 14:44:57 -0800808 pr->drawPath(fClipTarget, &pipelineBuilder, GrColor_WHITE,
809 viewMatrix, clipPath, stroke, false);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000810 } else {
joshualitt44701df2015-02-23 14:44:57 -0800811 pr->stencilPath(fClipTarget, &pipelineBuilder, viewMatrix,
812 clipPath, stroke);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000813 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000814 }
815 }
816 }
817
818 // now we modify the clip bit by rendering either the clip
819 // element directly or a bounding rect of the entire clip.
joshualitt7a6184f2014-10-29 18:29:27 -0700820 fClipMode = kModifyClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000821 for (int p = 0; p < passes; ++p) {
egdaniel8dd688b2015-01-22 10:16:09 -0800822 GrPipelineBuilder pipelineBuilderCopy(pipelineBuilder);
823 *pipelineBuilderCopy.stencil() = stencilSettings[p];
joshualitt9853cce2014-11-17 14:22:48 -0800824
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000825 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000826 if (Element::kRect_Type == element->getType()) {
joshualitt44701df2015-02-23 14:44:57 -0800827 fClipTarget->drawSimpleRect(&pipelineBuilderCopy,
828 GrColor_WHITE,
829 viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800830 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000831 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800832 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
joshualitt44701df2015-02-23 14:44:57 -0800833 pr->drawPath(fClipTarget, &pipelineBuilderCopy, GrColor_WHITE,
834 viewMatrix, clipPath, stroke, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000835 }
836 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000837 // The view matrix is setup to do clip space -> stencil space translation, so
838 // draw rect in clip space.
joshualitt44701df2015-02-23 14:44:57 -0800839 fClipTarget->drawSimpleRect(&pipelineBuilderCopy,
840 GrColor_WHITE,
841 viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800842 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000843 }
844 }
845 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000846 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000847 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000848 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000849 fCurrClipMaskType = kStencil_ClipMaskType;
joshualitt7a6184f2014-10-29 18:29:27 -0700850 fClipMode = kRespectClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000851 return true;
852}
853
bsalomon@google.com411dad02012-06-05 20:24:20 +0000854// mapping of clip-respecting stencil funcs to normal stencil funcs
855// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000856static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000857 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
858 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
859 // In the Clip Funcs
860 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
861 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
862 kLess_StencilFunc, // kLessIfInClip_StencilFunc
863 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
864 // Special in the clip func that forces user's ref to be 0.
865 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
866 // make ref 0 and do normal nequal.
867 },
868 {// Stencil-Clipping is ENABLED
869 // In the Clip Funcs
870 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
871 // eq stencil clip bit, mask
872 // out user bits.
873
874 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
875 // add stencil bit to mask and ref
876
877 kLess_StencilFunc, // kLessIfInClip_StencilFunc
878 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
879 // for both of these we can add
880 // the clip bit to the mask and
881 // ref and compare as normal
882 // Special in the clip func that forces user's ref to be 0.
883 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
884 // make ref have only the clip bit set
885 // and make comparison be less
886 // 10..0 < 1..user_bits..
887 }
888};
889
bsalomon@google.coma3201942012-06-21 19:58:20 +0000890namespace {
891// Sets the settings to clip against the stencil buffer clip while ignoring the
892// client bits.
893const GrStencilSettings& basic_apply_stencil_clip_settings() {
894 // stencil settings to use when clip is in stencil
895 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
896 kKeep_StencilOp,
897 kKeep_StencilOp,
898 kAlwaysIfInClip_StencilFunc,
899 0x0000,
900 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000901 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000902 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
903}
904}
905
egdaniel8dd688b2015-01-22 10:16:09 -0800906void GrClipMaskManager::setPipelineBuilderStencil(GrPipelineBuilder* pipelineBuilder,
907 GrPipelineBuilder::AutoRestoreStencil* ars) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000908 // We make two copies of the StencilSettings here (except in the early
909 // exit scenario. One copy from draw state to the stack var. Then another
910 // from the stack var to the gpu. We could make this class hold a ptr to
911 // GrGpu's fStencilSettings and eliminate the stack copy here.
912
bsalomon@google.coma3201942012-06-21 19:58:20 +0000913 // use stencil for clipping if clipping is enabled and the clip
914 // has been written into the stencil.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000915 GrStencilSettings settings;
joshualitt9853cce2014-11-17 14:22:48 -0800916
bsalomon@google.coma3201942012-06-21 19:58:20 +0000917 // The GrGpu client may not be using the stencil buffer but we may need to
918 // enable it in order to respect a stencil clip.
egdaniel8dd688b2015-01-22 10:16:09 -0800919 if (pipelineBuilder->getStencil().isDisabled()) {
joshualitt7a6184f2014-10-29 18:29:27 -0700920 if (GrClipMaskManager::kRespectClip_StencilClipMode == fClipMode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000921 settings = basic_apply_stencil_clip_settings();
922 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000923 return;
924 }
925 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800926 settings = pipelineBuilder->getStencil();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000927 }
928
bsalomon@google.coma3201942012-06-21 19:58:20 +0000929 int stencilBits = 0;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800930 GrRenderTarget* rt = pipelineBuilder->getRenderTarget();
931 GrStencilBuffer* stencilBuffer = rt->renderTargetPriv().attachStencilBuffer();
bsalomon49f085d2014-09-05 13:34:00 -0700932 if (stencilBuffer) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000933 stencilBits = stencilBuffer->bits();
934 }
935
joshualitt329bf482014-10-29 12:31:28 -0700936 SkASSERT(fClipTarget->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
937 SkASSERT(fClipTarget->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
joshualitt7a6184f2014-10-29 18:29:27 -0700938 this->adjustStencilParams(&settings, fClipMode, stencilBits);
egdaniel8dd688b2015-01-22 10:16:09 -0800939 ars->set(pipelineBuilder);
940 pipelineBuilder->setStencil(settings);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000941}
942
943void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
944 StencilClipMode mode,
945 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000946 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000947
948 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000949 // We assume that this clip manager itself is drawing to the GrGpu and
950 // has already setup the correct values.
951 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000952 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000953
bsalomon@google.com411dad02012-06-05 20:24:20 +0000954 unsigned int clipBit = (1 << (stencilBitCnt - 1));
955 unsigned int userBits = clipBit - 1;
956
bsalomon@google.coma3201942012-06-21 19:58:20 +0000957 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
joshualitt329bf482014-10-29 12:31:28 -0700958 bool twoSided = fClipTarget->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000959
bsalomon@google.coma3201942012-06-21 19:58:20 +0000960 bool finished = false;
961 while (!finished) {
962 GrStencilFunc func = settings->func(face);
963 uint16_t writeMask = settings->writeMask(face);
964 uint16_t funcMask = settings->funcMask(face);
965 uint16_t funcRef = settings->funcRef(face);
966
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000967 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000968
969 writeMask &= userBits;
970
971 if (func >= kBasicStencilFuncCount) {
972 int respectClip = kRespectClip_StencilClipMode == mode;
973 if (respectClip) {
974 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000975 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000976 switch (func) {
977 case kAlwaysIfInClip_StencilFunc:
978 funcMask = clipBit;
979 funcRef = clipBit;
980 break;
981 case kEqualIfInClip_StencilFunc:
982 case kLessIfInClip_StencilFunc:
983 case kLEqualIfInClip_StencilFunc:
984 funcMask = (funcMask & userBits) | clipBit;
985 funcRef = (funcRef & userBits) | clipBit;
986 break;
987 case kNonZeroIfInClip_StencilFunc:
988 funcMask = (funcMask & userBits) | clipBit;
989 funcRef = clipBit;
990 break;
991 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000992 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +0000993 }
994 } else {
995 funcMask &= userBits;
996 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000997 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000998 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000999 gSpecialToBasicStencilFunc[respectClip];
1000 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001001 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001002 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001003 funcMask &= userBits;
1004 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001005 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001006
1007 settings->setFunc(face, func);
1008 settings->setWriteMask(face, writeMask);
1009 settings->setFuncMask(face, funcMask);
1010 settings->setFuncRef(face, funcRef);
1011
1012 if (GrStencilSettings::kFront_Face == face) {
1013 face = GrStencilSettings::kBack_Face;
1014 finished = !twoSided;
1015 } else {
1016 finished = true;
1017 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001018 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001019 if (!twoSided) {
1020 settings->copyFrontSettingsToBack();
1021 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001022}
1023
1024////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +00001025GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001026 GrReducedClip::InitialState initialState,
1027 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -08001028 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001029 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001030 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001031
krajcevskiad1dc582014-06-10 15:06:47 -07001032 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -07001033 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001034 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001035 }
1036
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001037 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1038 // the top left corner of the resulting rect to the top left of the texture.
1039 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1040
robertphillips@google.com2c756812012-05-22 20:28:23 +00001041 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001042
joshualitt8059eb92014-12-29 15:10:07 -08001043 // Set the matrix so that rendered clip elements are transformed to mask space from clip
1044 // space.
1045 SkMatrix translate;
1046 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -08001047
joshualitt8059eb92014-12-29 15:10:07 -08001048 helper.init(maskSpaceIBounds, &translate, false);
tfarinabf54e492014-10-23 17:47:18 -07001049 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001050 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001051
tfarinabf54e492014-10-23 17:47:18 -07001052 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001053 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001054 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001055
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001056 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1057 // Intersect and reverse difference require modifying pixels outside of the geometry
1058 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1059 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1060 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001061 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001062 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001063 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001064 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001065 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001066 SkPath clipPath;
1067 element->asPath(&clipPath);
1068 clipPath.toggleInverseFillType();
1069 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001070 continue;
1071 }
1072
1073 // The other ops (union, xor, diff) only affect pixels inside
1074 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001075 if (Element::kRect_Type == element->getType()) {
1076 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1077 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001078 SkPath path;
1079 element->asPath(&path);
1080 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001081 }
1082 }
1083
krajcevskiad1dc582014-06-10 15:06:47 -07001084 // Allocate clip mask texture
1085 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, true);
1086 if (NULL == result) {
1087 fAACache.reset();
1088 return NULL;
1089 }
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001090 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001091
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001092 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001093 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001094}
1095
robertphillips@google.comf294b772012-04-27 14:29:26 +00001096////////////////////////////////////////////////////////////////////////////////
bsalomonc8dc1f72014-08-21 13:02:13 -07001097void GrClipMaskManager::purgeResources() {
1098 fAACache.purgeResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001099}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001100
joshualitt329bf482014-10-29 12:31:28 -07001101void GrClipMaskManager::setClipTarget(GrClipTarget* clipTarget) {
1102 fClipTarget = clipTarget;
1103 fAACache.setContext(clipTarget->getContext());
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001104}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001105
joshualitt9853cce2014-11-17 14:22:48 -08001106void GrClipMaskManager::adjustPathStencilParams(const GrStencilBuffer* stencilBuffer,
1107 GrStencilSettings* settings) {
bsalomon49f085d2014-09-05 13:34:00 -07001108 if (stencilBuffer) {
joshualitt9853cce2014-11-17 14:22:48 -08001109 int stencilBits = stencilBuffer->bits();
joshualitt7a6184f2014-10-29 18:29:27 -07001110 this->adjustStencilParams(settings, fClipMode, stencilBits);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001111 }
1112}