blob: f69c8717f8e050521e0602ce10347e070eb8e14b [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
bsalomon@google.com8182fa02012-12-04 14:06:06 +000026typedef SkClipStack::Element Element;
bsalomon@google.com51a62862012-11-26 21:19:43 +000027
28////////////////////////////////////////////////////////////////////////////////
robertphillips@google.come79f3202014-02-11 16:30:21 +000029namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000030// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000031// stage matrix this also alters the vertex layout
joshualitt9853cce2014-11-17 14:22:48 -080032void setup_drawstate_aaclip(const SkIRect &devBound,
egdaniel8dd688b2015-01-22 10:16:09 -080033 GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -080034 GrTexture* result) {
egdaniel8dd688b2015-01-22 10:16:09 -080035 SkASSERT(pipelineBuilder);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000036
bsalomon@google.comb9086a02012-11-01 18:02:54 +000037 SkMatrix mat;
bsalomon309d4d52014-12-18 10:17:44 -080038 // We use device coords to compute the texture coordinates. We set our matrix to be a
39 // translation to the devBound, and then a scaling matrix to normalized coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000040 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000041 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000042 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000043
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000044 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000045 // This could be a long-lived effect that is cached with the alpha-mask.
egdaniel8dd688b2015-01-22 10:16:09 -080046 pipelineBuilder->addCoverageProcessor(
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000047 GrTextureDomainEffect::Create(result,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000048 mat,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000049 GrTextureDomain::MakeTexelDomain(result, domainTexels),
50 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +000051 GrTextureParams::kNone_FilterMode,
bsalomon309d4d52014-12-18 10:17:44 -080052 kDevice_GrCoordSet))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000053}
54
robertphillips@google.come79f3202014-02-11 16:30:21 +000055bool path_needs_SW_renderer(GrContext* context,
joshualitt9853cce2014-11-17 14:22:48 -080056 const GrDrawTarget* gpu,
egdaniel8dd688b2015-01-22 10:16:09 -080057 const GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -080058 const SkMatrix& viewMatrix,
robertphillips@google.come79f3202014-02-11 16:30:21 +000059 const SkPath& origPath,
60 const SkStrokeRec& stroke,
61 bool doAA) {
62 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
63 SkTCopyOnFirstWrite<SkPath> path(origPath);
64 if (path->isInverseFillType()) {
65 path.writable()->toggleInverseFillType();
66 }
67 // last (false) parameter disallows use of the SW path renderer
bsalomon@google.com45a15f52012-12-10 19:10:17 +000068 GrPathRendererChain::DrawType type = doAA ?
69 GrPathRendererChain::kColorAntiAlias_DrawType :
70 GrPathRendererChain::kColor_DrawType;
71
egdaniel8dd688b2015-01-22 10:16:09 -080072 return NULL == context->getPathRenderer(gpu, pipelineBuilder, viewMatrix, *path, stroke,
73 false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +000074}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000075}
76
robertphillips@google.comfa662942012-05-17 12:20:22 +000077/*
78 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
79 * will be used on any element. If so, it returns true to indicate that the
80 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
81 */
egdaniel8dd688b2015-01-22 10:16:09 -080082bool GrClipMaskManager::useSWOnlyPath(const GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -080083 const SkVector& clipToMaskOffset,
joshualitt9853cce2014-11-17 14:22:48 -080084 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000085 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000086 // a clip gets complex enough it can just be done in SW regardless
87 // of whether it would invoke the GrSoftwarePathRenderer.
sugoi@google.com5f74cf82012-12-17 21:16:45 +000088 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000089
joshualitt8059eb92014-12-29 15:10:07 -080090 // Set the matrix so that rendered clip elements are transformed to mask space from clip
91 // space.
92 SkMatrix translate;
93 translate.setTranslate(clipToMaskOffset);
94
tfarinabf54e492014-10-23 17:47:18 -070095 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000096 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000097 // rects can always be drawn directly w/o using the software path
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000098 // Skip rrects once we're drawing them directly.
99 if (Element::kRect_Type != element->getType()) {
100 SkPath path;
101 element->asPath(&path);
egdaniel8dd688b2015-01-22 10:16:09 -0800102 if (path_needs_SW_renderer(this->getContext(), fClipTarget, pipelineBuilder, translate,
joshualitt8059eb92014-12-29 15:10:07 -0800103 path, stroke, element->isAA())) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000104 return true;
105 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000106 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000107 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000108 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000109}
110
egdaniel8dd688b2015-01-22 10:16:09 -0800111bool GrClipMaskManager::installClipEffects(GrPipelineBuilder* pipelineBuilder,
112 GrPipelineBuilder::AutoRestoreEffects* are,
joshualitt9853cce2014-11-17 14:22:48 -0800113 const GrReducedClip::ElementList& elements,
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000114 const SkVector& clipToRTOffset,
mtklein217daa72014-07-02 12:55:21 -0700115 const SkRect* drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000116 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700117 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000118 boundsInClipSpace = *drawBounds;
119 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
120 }
121
egdaniel8dd688b2015-01-22 10:16:09 -0800122 are->set(pipelineBuilder);
123 GrRenderTarget* rt = pipelineBuilder->getRenderTarget();
tfarinabf54e492014-10-23 17:47:18 -0700124 GrReducedClip::ElementList::Iter iter(elements);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000125 bool failed = false;
bsalomon49f085d2014-09-05 13:34:00 -0700126 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000127 SkRegion::Op op = iter.get()->getOp();
128 bool invert;
129 bool skip = false;
130 switch (op) {
131 case SkRegion::kReplace_Op:
132 SkASSERT(iter.get() == elements.head());
133 // Fallthrough, handled same as intersect.
134 case SkRegion::kIntersect_Op:
135 invert = false;
bsalomon49f085d2014-09-05 13:34:00 -0700136 if (drawBounds && iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000137 skip = true;
138 }
139 break;
140 case SkRegion::kDifference_Op:
141 invert = true;
142 // We don't currently have a cheap test for whether a rect is fully outside an
143 // element's primitive, so don't attempt to set skip.
144 break;
145 default:
146 failed = true;
147 break;
148 }
149 if (failed) {
150 break;
151 }
152
153 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700154 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800155 if (iter.get()->isAA()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000156 if (rt->isMultisampled()) {
mtklein217daa72014-07-02 12:55:21 -0700157 // Coverage based AA clips don't place nicely with MSAA.
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000158 failed = true;
159 break;
160 }
joshualittb0a8a372014-09-23 09:50:21 -0700161 edgeType =
162 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000163 } else {
joshualittb0a8a372014-09-23 09:50:21 -0700164 edgeType =
165 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000166 }
joshualittb0a8a372014-09-23 09:50:21 -0700167 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000168 switch (iter.get()->getType()) {
169 case SkClipStack::Element::kPath_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700170 fp.reset(GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000171 &clipToRTOffset));
172 break;
173 case SkClipStack::Element::kRRect_Type: {
174 SkRRect rrect = iter.get()->getRRect();
175 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
joshualittb0a8a372014-09-23 09:50:21 -0700176 fp.reset(GrRRectEffect::Create(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000177 break;
178 }
179 case SkClipStack::Element::kRect_Type: {
180 SkRect rect = iter.get()->getRect();
181 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
joshualittb0a8a372014-09-23 09:50:21 -0700182 fp.reset(GrConvexPolyEffect::Create(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000183 break;
184 }
185 default:
186 break;
187 }
joshualittb0a8a372014-09-23 09:50:21 -0700188 if (fp) {
egdaniel8dd688b2015-01-22 10:16:09 -0800189 pipelineBuilder->addCoverageProcessor(fp);
mtklein217daa72014-07-02 12:55:21 -0700190 } else {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000191 failed = true;
192 break;
193 }
194 }
mtklein217daa72014-07-02 12:55:21 -0700195 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000196 }
197
198 if (failed) {
199 are->set(NULL);
200 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000201 return !failed;
202}
203
robertphillips@google.comf294b772012-04-27 14:29:26 +0000204////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000205// sort out what kind of clip mask needs to be created: alpha, stencil,
206// scissor, or entirely software
egdaniel8dd688b2015-01-22 10:16:09 -0800207bool GrClipMaskManager::setupClipping(GrPipelineBuilder* pipelineBuilder,
208 GrPipelineBuilder::AutoRestoreEffects* are,
209 GrPipelineBuilder::AutoRestoreStencil* ars,
bsalomon3e791242014-12-17 13:43:13 -0800210 GrScissorState* scissorState,
joshualitt9853cce2014-11-17 14:22:48 -0800211 const GrClipData* clipDataIn,
212 const SkRect* devBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000213 fCurrClipMaskType = kNone_ClipMaskType;
joshualitt7a6184f2014-10-29 18:29:27 -0700214 if (kRespectClip_StencilClipMode == fClipMode) {
215 fClipMode = kIgnoreClip_StencilClipMode;
216 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000217
tfarinabf54e492014-10-23 17:47:18 -0700218 GrReducedClip::ElementList elements(16);
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000219 int32_t genID;
tfarinabf54e492014-10-23 17:47:18 -0700220 GrReducedClip::InitialState initialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000221 SkIRect clipSpaceIBounds;
222 bool requiresAA;
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
egdaniel8dd688b2015-01-22 10:16:09 -0800228 bool ignoreClip = !pipelineBuilder->isClipState() || clipDataIn->fClipStack->isWideOpen();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000229 if (!ignoreClip) {
230 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
231 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
tfarinabf54e492014-10-23 17:47:18 -0700232 GrReducedClip::ReduceClipStack(*clipDataIn->fClipStack,
233 clipSpaceRTIBounds,
234 &elements,
235 &genID,
236 &initialState,
237 &clipSpaceIBounds,
238 &requiresAA);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000239 if (elements.isEmpty()) {
tfarinabf54e492014-10-23 17:47:18 -0700240 if (GrReducedClip::kAllIn_InitialState == initialState) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000241 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000242 } else {
243 return false;
244 }
245 }
246 }
247
248 if (ignoreClip) {
egdaniel8dd688b2015-01-22 10:16:09 -0800249 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000250 return true;
251 }
252
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000253 // An element count of 4 was chosen because of the common pattern in Blink of:
254 // isect RR
255 // diff RR
256 // isect convex_poly
257 // isect convex_poly
258 // when drawing rounded div borders. This could probably be tuned based on a
259 // configuration's relative costs of switching RTs to generate a mask vs
260 // longer shaders.
261 if (elements.count() <= 4) {
262 SkVector clipToRTOffset = { SkIntToScalar(-clipDataIn->fOrigin.fX),
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000263 SkIntToScalar(-clipDataIn->fOrigin.fY) };
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000264 if (elements.isEmpty() ||
egdaniel8dd688b2015-01-22 10:16:09 -0800265 (requiresAA && this->installClipEffects(pipelineBuilder, are, elements, clipToRTOffset,
joshualitt9853cce2014-11-17 14:22:48 -0800266 devBounds))) {
mtklein217daa72014-07-02 12:55:21 -0700267 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
268 scissorSpaceIBounds.offset(-clipDataIn->fOrigin);
269 if (NULL == devBounds ||
270 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
joshualitt77b13072014-10-27 14:51:01 -0700271 scissorState->set(scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000272 }
egdaniel8dd688b2015-01-22 10:16:09 -0800273 this->setPipelineBuilderStencil(pipelineBuilder, ars);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000274 return true;
275 }
276 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000277
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000278 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000279 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000280 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000281
joshualitt8059eb92014-12-29 15:10:07 -0800282 // The top-left of the mask corresponds to the top-left corner of the bounds.
283 SkVector clipToMaskOffset = {
284 SkIntToScalar(-clipSpaceIBounds.fLeft),
285 SkIntToScalar(-clipSpaceIBounds.fTop)
286 };
287
egdaniel8dd688b2015-01-22 10:16:09 -0800288 if (this->useSWOnlyPath(pipelineBuilder, clipToMaskOffset, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000289 // The clip geometry is complex enough that it will be more efficient to create it
290 // entirely in software
291 result = this->createSoftwareClipMask(genID,
292 initialState,
293 elements,
joshualitt8059eb92014-12-29 15:10:07 -0800294 clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000295 clipSpaceIBounds);
296 } else {
297 result = this->createAlphaClipMask(genID,
298 initialState,
299 elements,
joshualitt8059eb92014-12-29 15:10:07 -0800300 clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000301 clipSpaceIBounds);
302 }
303
bsalomon49f085d2014-09-05 13:34:00 -0700304 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000305 // The mask's top left coord should be pinned to the rounded-out top left corner of
306 // clipSpace bounds. We determine the mask's position WRT to the render target here.
307 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
308 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
egdaniel8dd688b2015-01-22 10:16:09 -0800309 setup_drawstate_aaclip(rtSpaceMaskBounds, pipelineBuilder, result);
310 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000311 return true;
312 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000313 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000314 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000315
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000316 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
317 // be created. In either case, free up the texture in the anti-aliased mask cache.
318 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
319 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
320 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000321 fAACache.reset();
322
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000323 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000324 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
joshualitt9853cce2014-11-17 14:22:48 -0800325 this->createStencilClipMask(rt,
326 genID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000327 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000328 elements,
329 clipSpaceIBounds,
330 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000331
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000332 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
333 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
334 // use both stencil and scissor test to the bounds for the final draw.
335 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
336 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
joshualitt77b13072014-10-27 14:51:01 -0700337 scissorState->set(scissorSpaceIBounds);
egdaniel8dd688b2015-01-22 10:16:09 -0800338 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000339 return true;
340}
341
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000342namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000343////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800344// Set a coverage drawing XPF on the pipelineBuilder for the given op and invertCoverage mode
345void set_coverage_drawing_xpf(SkRegion::Op op, bool invertCoverage,
346 GrPipelineBuilder* pipelineBuilder) {
egdaniel87509242014-12-17 13:37:13 -0800347 SkASSERT(op <= SkRegion::kLastOp);
egdaniel8dd688b2015-01-22 10:16:09 -0800348 pipelineBuilder->setCoverageSetOpXPFactory(op, invertCoverage);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000349}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000350}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000351
352////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800353bool GrClipMaskManager::drawElement(GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800354 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800355 GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000356 const SkClipStack::Element* element,
357 GrPathRenderer* pr) {
joshualitt9853cce2014-11-17 14:22:48 -0800358 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000359
egdaniel8dd688b2015-01-22 10:16:09 -0800360 pipelineBuilder->setRenderTarget(target->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000361
egdaniel87509242014-12-17 13:37:13 -0800362 // The color we use to draw does not matter since we will always be using a GrCoverageSetOpXP
363 // which ignores color.
364 GrColor color = GrColor_WHITE;
365
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000366 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000367 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000368 case Element::kEmpty_Type:
369 SkDEBUGFAIL("Should never get here with an empty element.");
370 break;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000371 case Element::kRect_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700372 // TODO: Do rects directly to the accumulator using a aa-rect GrProcessor that covers
373 // the entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000374 if (element->isAA()) {
joshualitt8059eb92014-12-29 15:10:07 -0800375 SkRect devRect = element->getRect();
376 viewMatrix.mapRect(&devRect);
joshualitt329bf482014-10-29 12:31:28 -0700377 this->getContext()->getAARectRenderer()->fillAARect(fClipTarget,
egdaniel8dd688b2015-01-22 10:16:09 -0800378 pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -0800379 color,
joshualitt8059eb92014-12-29 15:10:07 -0800380 viewMatrix,
joshualitta58fe352014-10-27 08:39:00 -0700381 element->getRect(),
joshualitt8059eb92014-12-29 15:10:07 -0800382 devRect);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000383 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800384 fClipTarget->drawSimpleRect(pipelineBuilder, color, viewMatrix, element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000385 }
386 return true;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000387 default: {
388 SkPath path;
389 element->asPath(&path);
jvanverthb3eb6872014-10-24 07:12:51 -0700390 path.setIsVolatile(true);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000391 if (path.isInverseFillType()) {
392 path.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000393 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000394 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000395 if (NULL == pr) {
396 GrPathRendererChain::DrawType type;
397 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
398 GrPathRendererChain::kColor_DrawType;
egdaniel8dd688b2015-01-22 10:16:09 -0800399 pr = this->getContext()->getPathRenderer(fClipTarget, pipelineBuilder, viewMatrix,
400 path, stroke, false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000401 }
402 if (NULL == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000403 return false;
404 }
joshualitt9853cce2014-11-17 14:22:48 -0800405
egdaniel8dd688b2015-01-22 10:16:09 -0800406 pr->drawPath(fClipTarget, pipelineBuilder, color, viewMatrix, path, stroke,
407 element->isAA());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000408 break;
409 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000410 }
411 return true;
412}
413
egdaniel8dd688b2015-01-22 10:16:09 -0800414bool GrClipMaskManager::canStencilAndDrawElement(GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800415 GrTexture* target,
416 GrPathRenderer** pr,
417 const SkClipStack::Element* element) {
egdaniel8dd688b2015-01-22 10:16:09 -0800418 pipelineBuilder->setRenderTarget(target->asRenderTarget());
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000419
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000420 if (Element::kRect_Type == element->getType()) {
421 return true;
422 } else {
423 // We shouldn't get here with an empty clip element.
424 SkASSERT(Element::kEmpty_Type != element->getType());
425 SkPath path;
426 element->asPath(&path);
427 if (path.isInverseFillType()) {
428 path.toggleInverseFillType();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000429 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000430 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
431 GrPathRendererChain::DrawType type = element->isAA() ?
432 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
433 GrPathRendererChain::kStencilAndColor_DrawType;
egdaniel8dd688b2015-01-22 10:16:09 -0800434 *pr = this->getContext()->getPathRenderer(fClipTarget, pipelineBuilder, SkMatrix::I(), path,
joshualitt8059eb92014-12-29 15:10:07 -0800435 stroke, false, type);
bsalomon49f085d2014-09-05 13:34:00 -0700436 return SkToBool(*pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000437 }
438}
439
egdaniel8dd688b2015-01-22 10:16:09 -0800440void GrClipMaskManager::mergeMask(GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800441 GrTexture* dstMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000442 GrTexture* srcMask,
443 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000444 const SkIRect& dstBound,
445 const SkIRect& srcBound) {
egdaniel8dd688b2015-01-22 10:16:09 -0800446 pipelineBuilder->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000447
egdaniel87509242014-12-17 13:37:13 -0800448 // We want to invert the coverage here
egdaniel8dd688b2015-01-22 10:16:09 -0800449 set_coverage_drawing_xpf(op, false, pipelineBuilder);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000450
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000451 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000452 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000453
egdaniel8dd688b2015-01-22 10:16:09 -0800454 pipelineBuilder->addCoverageProcessor(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000455 GrTextureDomainEffect::Create(srcMask,
456 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000457 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
458 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000459 GrTextureParams::kNone_FilterMode))->unref();
egdaniel87509242014-12-17 13:37:13 -0800460 // The color passed in here does not matter since the coverageSetOpXP won't read it.
egdaniel8dd688b2015-01-22 10:16:09 -0800461 fClipTarget->drawSimpleRect(pipelineBuilder, GrColor_WHITE, SkMatrix::I(),
462 SkRect::Make(dstBound));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000463}
464
bsalomon427cf282014-10-16 13:41:43 -0700465GrTexture* GrClipMaskManager::createTempMask(int width, int height) {
bsalomonf2703d82014-10-28 14:33:06 -0700466 GrSurfaceDesc desc;
bsalomon3f490a02014-12-18 06:20:52 -0800467 desc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000468 desc.fWidth = width;
469 desc.fHeight = height;
bsalomon51d1f7e2014-12-22 08:40:49 -0800470 if (this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
471 desc.fConfig = kAlpha_8_GrPixelConfig;
472 } else {
473 desc.fConfig = kRGBA_8888_GrPixelConfig;
474 }
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000475
joshualitt329bf482014-10-29 12:31:28 -0700476 return this->getContext()->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000477}
478
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000479////////////////////////////////////////////////////////////////////////////////
krajcevskiad1dc582014-06-10 15:06:47 -0700480// Return the texture currently in the cache if it exists. Otherwise, return NULL
481GrTexture* GrClipMaskManager::getCachedMaskTexture(int32_t elementsGenID,
482 const SkIRect& clipSpaceIBounds) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000483 bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000484 if (!cached) {
krajcevskiad1dc582014-06-10 15:06:47 -0700485 return NULL;
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000486 }
487
krajcevskiad1dc582014-06-10 15:06:47 -0700488 return fAACache.getLastMask();
489}
490
491////////////////////////////////////////////////////////////////////////////////
492// Allocate a texture in the texture cache. This function returns the texture
493// allocated (or NULL on error).
494GrTexture* GrClipMaskManager::allocMaskTexture(int32_t elementsGenID,
495 const SkIRect& clipSpaceIBounds,
496 bool willUpload) {
497 // Since we are setting up the cache we should free up the
498 // currently cached mask so it can be reused.
499 fAACache.reset();
500
bsalomonf2703d82014-10-28 14:33:06 -0700501 GrSurfaceDesc desc;
502 desc.fFlags = willUpload ? kNone_GrSurfaceFlags : kRenderTarget_GrSurfaceFlag;
krajcevskiad1dc582014-06-10 15:06:47 -0700503 desc.fWidth = clipSpaceIBounds.width();
504 desc.fHeight = clipSpaceIBounds.height();
505 desc.fConfig = kRGBA_8888_GrPixelConfig;
506 if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
507 // We would always like A8 but it isn't supported on all platforms
508 desc.fConfig = kAlpha_8_GrPixelConfig;
509 }
510
511 fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds);
512 return fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000513}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000514
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000515////////////////////////////////////////////////////////////////////////////////
516// Create a 8-bit clip mask in alpha
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000517GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700518 GrReducedClip::InitialState initialState,
519 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800520 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000521 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000522 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000523
krajcevskiad1dc582014-06-10 15:06:47 -0700524 // First, check for cached texture
525 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -0700526 if (result) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000527 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000528 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000529 }
530
krajcevskiad1dc582014-06-10 15:06:47 -0700531 // There's no texture in the cache. Let's try to allocate it then.
532 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, false);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000533 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000534 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000535 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000536 }
537
joshualitt8059eb92014-12-29 15:10:07 -0800538 // Set the matrix so that rendered clip elements are transformed to mask space from clip
539 // space.
540 SkMatrix translate;
541 translate.setTranslate(clipToMaskOffset);
542
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000543 // The texture may be larger than necessary, this rect represents the part of the texture
544 // we populate with a rasterization of the clip.
545 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
546
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) {
egdaniel8dd688b2015-01-22 10:16:09 -0800567 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800568
robertphillips@google.come79f3202014-02-11 16:30:21 +0000569 GrPathRenderer* pr = NULL;
egdaniel8dd688b2015-01-22 10:16:09 -0800570 bool useTemp = !this->canStencilAndDrawElement(&pipelineBuilder, result, &pr, element);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000571 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000572 // This is the bounds of the clip element in the space of the alpha-mask. The temporary
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000573 // mask buffer can be substantially larger than the actually clip stack element. We
574 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000575 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000576 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000577
578 if (useTemp) {
579 if (invert) {
580 maskSpaceElementIBounds = maskSpaceIBounds;
581 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000582 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000583 elementBounds.offset(clipToMaskOffset);
584 elementBounds.roundOut(&maskSpaceElementIBounds);
585 }
586
bsalomon427cf282014-10-16 13:41:43 -0700587 if (!temp) {
588 temp.reset(this->createTempMask(maskSpaceIBounds.fRight,
589 maskSpaceIBounds.fBottom));
590 if (!temp) {
591 fAACache.reset();
592 return NULL;
593 }
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000594 }
bsalomon427cf282014-10-16 13:41:43 -0700595 dst = temp;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000596 // clear the temp target and set blend to replace
joshualitt329bf482014-10-29 12:31:28 -0700597 fClipTarget->clear(&maskSpaceElementIBounds,
joshualitt9853cce2014-11-17 14:22:48 -0800598 invert ? 0xffffffff : 0x00000000,
599 true,
600 dst->asRenderTarget());
egdaniel8dd688b2015-01-22 10:16:09 -0800601 set_coverage_drawing_xpf(SkRegion::kReplace_Op, invert, &pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000602 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000603 // draw directly into the result with the stencil set to make the pixels affected
604 // by the clip shape be non-zero.
605 dst = result;
606 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
607 kReplace_StencilOp,
608 kReplace_StencilOp,
609 kAlways_StencilFunc,
610 0xffff,
611 0xffff,
612 0xffff);
egdaniel8dd688b2015-01-22 10:16:09 -0800613 pipelineBuilder.setStencil(kStencilInElement);
614 set_coverage_drawing_xpf(op, invert, &pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000615 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000616
egdaniel8dd688b2015-01-22 10:16:09 -0800617 if (!this->drawElement(&pipelineBuilder, translate, dst, element, pr)) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000618 fAACache.reset();
619 return NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000620 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000621
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000622 if (useTemp) {
egdaniel8dd688b2015-01-22 10:16:09 -0800623 GrPipelineBuilder backgroundPipelineBuilder;
egdaniel8dd688b2015-01-22 10:16:09 -0800624 backgroundPipelineBuilder.setRenderTarget(result->asRenderTarget());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800625
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000626 // Now draw into the accumulator using the real operation and the temp buffer as a
627 // texture
egdaniel8dd688b2015-01-22 10:16:09 -0800628 this->mergeMask(&backgroundPipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800629 result,
bsalomon427cf282014-10-16 13:41:43 -0700630 temp,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000631 op,
632 maskSpaceIBounds,
633 maskSpaceElementIBounds);
634 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800635 GrPipelineBuilder backgroundPipelineBuilder;
egdaniel8dd688b2015-01-22 10:16:09 -0800636 backgroundPipelineBuilder.setRenderTarget(result->asRenderTarget());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800637
egdaniel8dd688b2015-01-22 10:16:09 -0800638 set_coverage_drawing_xpf(op, !invert, &backgroundPipelineBuilder);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000639 // Draw to the exterior pixels (those with a zero stencil value).
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000640 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
641 kZero_StencilOp,
642 kZero_StencilOp,
643 kEqual_StencilFunc,
644 0xffff,
645 0x0000,
646 0xffff);
egdaniel8dd688b2015-01-22 10:16:09 -0800647 backgroundPipelineBuilder.setStencil(kDrawOutsideElement);
egdaniel87509242014-12-17 13:37:13 -0800648 // The color passed in here does not matter since the coverageSetOpXP won't read it.
egdaniel8dd688b2015-01-22 10:16:09 -0800649 fClipTarget->drawSimpleRect(&backgroundPipelineBuilder, GrColor_WHITE, translate,
joshualitt8059eb92014-12-29 15:10:07 -0800650 clipSpaceIBounds);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000651 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000652 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800653 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800654
robertphillips@google.come79f3202014-02-11 16:30:21 +0000655 // all the remaining ops can just be directly draw into the accumulation buffer
egdaniel8dd688b2015-01-22 10:16:09 -0800656 set_coverage_drawing_xpf(op, false, &pipelineBuilder);
egdaniel87509242014-12-17 13:37:13 -0800657 // The color passed in here does not matter since the coverageSetOpXP won't read it.
egdaniel8dd688b2015-01-22 10:16:09 -0800658 this->drawElement(&pipelineBuilder, translate, result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000659 }
660 }
661
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000662 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000663 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000664}
665
666////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000667// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000668// (as opposed to canvas) coordinates
joshualitt9853cce2014-11-17 14:22:48 -0800669bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt,
670 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700671 GrReducedClip::InitialState initialState,
672 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000673 const SkIRect& clipSpaceIBounds,
674 const SkIPoint& clipSpaceToStencilOffset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000675 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon49f085d2014-09-05 13:34:00 -0700676 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000677
678 // TODO: dynamically attach a SB when needed.
679 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
680 if (NULL == stencilBuffer) {
681 return false;
682 }
683
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000684 if (stencilBuffer->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000685 stencilBuffer->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000686 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
687 SkVector translate = {
688 SkIntToScalar(clipSpaceToStencilOffset.fX),
689 SkIntToScalar(clipSpaceToStencilOffset.fY)
690 };
joshualitt8059eb92014-12-29 15:10:07 -0800691 SkMatrix viewMatrix;
692 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000693
bsalomon@google.com9f131742012-12-13 20:43:56 +0000694 // We set the current clip to the bounds so that our recursive draws are scissored to them.
695 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
696 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
joshualitt329bf482014-10-29 12:31:28 -0700697 GrDrawTarget::AutoClipRestore acr(fClipTarget, stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000698
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000699 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000700 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000701 clipBit = (1 << (clipBit-1));
702
joshualitt329bf482014-10-29 12:31:28 -0700703 fClipTarget->clearStencilClip(stencilSpaceIBounds,
704 GrReducedClip::kAllIn_InitialState == initialState,
705 rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000706
707 // walk through each clip element and perform its set op
708 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700709 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000710 const Element* element = iter.get();
joshualitt9853cce2014-11-17 14:22:48 -0800711
egdaniel8dd688b2015-01-22 10:16:09 -0800712 GrPipelineBuilder pipelineBuilder;
713 pipelineBuilder.setRenderTarget(rt);
egdaniel080e6732014-12-22 07:35:52 -0800714
egdaniel8dd688b2015-01-22 10:16:09 -0800715 pipelineBuilder.setDisableColorXPFactory();
joshualitt9853cce2014-11-17 14:22:48 -0800716
717 // if the target is MSAA then we want MSAA enabled when the clip is soft
718 if (rt->isMultisampled()) {
egdaniel8dd688b2015-01-22 10:16:09 -0800719 pipelineBuilder.setState(GrPipelineBuilder::kHWAntialias_StateBit, element->isAA());
joshualitt9853cce2014-11-17 14:22:48 -0800720 }
721
tomhudson@google.com8afae612012-08-14 15:03:35 +0000722 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000723 // enabled at bottom of loop
joshualitt7a6184f2014-10-29 18:29:27 -0700724 fClipMode = kIgnoreClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000725
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000726 // This will be used to determine whether the clip shape can be rendered into the
727 // stencil with arbitrary stencil settings.
728 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000729
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000730 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000731 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000732
robertphillips@google.come79f3202014-02-11 16:30:21 +0000733 GrPathRenderer* pr = NULL;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000734 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000735 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000736 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000737 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000738 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000739 element->asPath(&clipPath);
740 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000741 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000742 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000743 }
joshualitt9853cce2014-11-17 14:22:48 -0800744 pr = this->getContext()->getPathRenderer(fClipTarget,
egdaniel8dd688b2015-01-22 10:16:09 -0800745 &pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800746 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800747 clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000748 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000749 false,
750 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000751 &stencilSupport);
752 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000753 return false;
754 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000755 }
756
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000757 int passes;
758 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
759
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000760 bool canRenderDirectToStencil =
761 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000762 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000763 // fill rule, and set operation can
764 // we render the element directly to
765 // stencil bit used for clipping.
766 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
767 canRenderDirectToStencil,
768 clipBit,
769 fillInverted,
770 &passes,
771 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000772
773 // draw the element to the client stencil bits if necessary
774 if (!canDrawDirectToClip) {
775 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000776 kIncClamp_StencilOp,
777 kIncClamp_StencilOp,
778 kAlways_StencilFunc,
779 0xffff,
780 0x0000,
781 0xffff);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000782 if (Element::kRect_Type == element->getType()) {
egdaniel8dd688b2015-01-22 10:16:09 -0800783 *pipelineBuilder.stencil() = gDrawToStencil;
784 fClipTarget->drawSimpleRect(&pipelineBuilder, GrColor_WHITE, viewMatrix,
joshualitt8059eb92014-12-29 15:10:07 -0800785 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000786 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000787 if (!clipPath.isEmpty()) {
joshualitt9853cce2014-11-17 14:22:48 -0800788 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000789 if (canRenderDirectToStencil) {
egdaniel8dd688b2015-01-22 10:16:09 -0800790 *pipelineBuilder.stencil() = gDrawToStencil;
791 pr->drawPath(fClipTarget, &pipelineBuilder, GrColor_WHITE, viewMatrix,
joshualitt8059eb92014-12-29 15:10:07 -0800792 clipPath, stroke, false);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000793 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800794 pr->stencilPath(fClipTarget, &pipelineBuilder, viewMatrix, clipPath,
795 stroke);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000796 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000797 }
798 }
799 }
800
801 // now we modify the clip bit by rendering either the clip
802 // element directly or a bounding rect of the entire clip.
joshualitt7a6184f2014-10-29 18:29:27 -0700803 fClipMode = kModifyClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000804 for (int p = 0; p < passes; ++p) {
egdaniel8dd688b2015-01-22 10:16:09 -0800805 GrPipelineBuilder pipelineBuilderCopy(pipelineBuilder);
806 *pipelineBuilderCopy.stencil() = stencilSettings[p];
joshualitt9853cce2014-11-17 14:22:48 -0800807
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000808 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000809 if (Element::kRect_Type == element->getType()) {
egdaniel8dd688b2015-01-22 10:16:09 -0800810 fClipTarget->drawSimpleRect(&pipelineBuilderCopy, GrColor_WHITE, viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800811 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000812 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800813 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
egdaniel8dd688b2015-01-22 10:16:09 -0800814 pr->drawPath(fClipTarget, &pipelineBuilderCopy, GrColor_WHITE, viewMatrix,
joshualitt8059eb92014-12-29 15:10:07 -0800815 clipPath, stroke, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000816 }
817 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000818 // The view matrix is setup to do clip space -> stencil space translation, so
819 // draw rect in clip space.
egdaniel8dd688b2015-01-22 10:16:09 -0800820 fClipTarget->drawSimpleRect(&pipelineBuilderCopy, GrColor_WHITE, viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800821 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000822 }
823 }
824 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000825 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000826 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000827 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000828 fCurrClipMaskType = kStencil_ClipMaskType;
joshualitt7a6184f2014-10-29 18:29:27 -0700829 fClipMode = kRespectClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000830 return true;
831}
832
bsalomon@google.com411dad02012-06-05 20:24:20 +0000833// mapping of clip-respecting stencil funcs to normal stencil funcs
834// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000835static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000836 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
837 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
838 // In the Clip Funcs
839 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
840 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
841 kLess_StencilFunc, // kLessIfInClip_StencilFunc
842 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
843 // Special in the clip func that forces user's ref to be 0.
844 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
845 // make ref 0 and do normal nequal.
846 },
847 {// Stencil-Clipping is ENABLED
848 // In the Clip Funcs
849 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
850 // eq stencil clip bit, mask
851 // out user bits.
852
853 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
854 // add stencil bit to mask and ref
855
856 kLess_StencilFunc, // kLessIfInClip_StencilFunc
857 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
858 // for both of these we can add
859 // the clip bit to the mask and
860 // ref and compare as normal
861 // Special in the clip func that forces user's ref to be 0.
862 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
863 // make ref have only the clip bit set
864 // and make comparison be less
865 // 10..0 < 1..user_bits..
866 }
867};
868
bsalomon@google.coma3201942012-06-21 19:58:20 +0000869namespace {
870// Sets the settings to clip against the stencil buffer clip while ignoring the
871// client bits.
872const GrStencilSettings& basic_apply_stencil_clip_settings() {
873 // stencil settings to use when clip is in stencil
874 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
875 kKeep_StencilOp,
876 kKeep_StencilOp,
877 kAlwaysIfInClip_StencilFunc,
878 0x0000,
879 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000880 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000881 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
882}
883}
884
egdaniel8dd688b2015-01-22 10:16:09 -0800885void GrClipMaskManager::setPipelineBuilderStencil(GrPipelineBuilder* pipelineBuilder,
886 GrPipelineBuilder::AutoRestoreStencil* ars) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000887 // We make two copies of the StencilSettings here (except in the early
888 // exit scenario. One copy from draw state to the stack var. Then another
889 // from the stack var to the gpu. We could make this class hold a ptr to
890 // GrGpu's fStencilSettings and eliminate the stack copy here.
891
bsalomon@google.coma3201942012-06-21 19:58:20 +0000892 // use stencil for clipping if clipping is enabled and the clip
893 // has been written into the stencil.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000894 GrStencilSettings settings;
joshualitt9853cce2014-11-17 14:22:48 -0800895
bsalomon@google.coma3201942012-06-21 19:58:20 +0000896 // The GrGpu client may not be using the stencil buffer but we may need to
897 // enable it in order to respect a stencil clip.
egdaniel8dd688b2015-01-22 10:16:09 -0800898 if (pipelineBuilder->getStencil().isDisabled()) {
joshualitt7a6184f2014-10-29 18:29:27 -0700899 if (GrClipMaskManager::kRespectClip_StencilClipMode == fClipMode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000900 settings = basic_apply_stencil_clip_settings();
901 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000902 return;
903 }
904 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800905 settings = pipelineBuilder->getStencil();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000906 }
907
908 // TODO: dynamically attach a stencil buffer
909 int stencilBits = 0;
egdaniel8dd688b2015-01-22 10:16:09 -0800910 GrStencilBuffer* stencilBuffer = pipelineBuilder->getRenderTarget()->getStencilBuffer();
bsalomon49f085d2014-09-05 13:34:00 -0700911 if (stencilBuffer) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000912 stencilBits = stencilBuffer->bits();
913 }
914
joshualitt329bf482014-10-29 12:31:28 -0700915 SkASSERT(fClipTarget->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
916 SkASSERT(fClipTarget->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
joshualitt7a6184f2014-10-29 18:29:27 -0700917 this->adjustStencilParams(&settings, fClipMode, stencilBits);
egdaniel8dd688b2015-01-22 10:16:09 -0800918 ars->set(pipelineBuilder);
919 pipelineBuilder->setStencil(settings);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000920}
921
922void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
923 StencilClipMode mode,
924 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000925 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000926
927 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000928 // We assume that this clip manager itself is drawing to the GrGpu and
929 // has already setup the correct values.
930 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000931 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000932
bsalomon@google.com411dad02012-06-05 20:24:20 +0000933 unsigned int clipBit = (1 << (stencilBitCnt - 1));
934 unsigned int userBits = clipBit - 1;
935
bsalomon@google.coma3201942012-06-21 19:58:20 +0000936 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
joshualitt329bf482014-10-29 12:31:28 -0700937 bool twoSided = fClipTarget->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000938
bsalomon@google.coma3201942012-06-21 19:58:20 +0000939 bool finished = false;
940 while (!finished) {
941 GrStencilFunc func = settings->func(face);
942 uint16_t writeMask = settings->writeMask(face);
943 uint16_t funcMask = settings->funcMask(face);
944 uint16_t funcRef = settings->funcRef(face);
945
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000946 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000947
948 writeMask &= userBits;
949
950 if (func >= kBasicStencilFuncCount) {
951 int respectClip = kRespectClip_StencilClipMode == mode;
952 if (respectClip) {
953 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000954 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000955 switch (func) {
956 case kAlwaysIfInClip_StencilFunc:
957 funcMask = clipBit;
958 funcRef = clipBit;
959 break;
960 case kEqualIfInClip_StencilFunc:
961 case kLessIfInClip_StencilFunc:
962 case kLEqualIfInClip_StencilFunc:
963 funcMask = (funcMask & userBits) | clipBit;
964 funcRef = (funcRef & userBits) | clipBit;
965 break;
966 case kNonZeroIfInClip_StencilFunc:
967 funcMask = (funcMask & userBits) | clipBit;
968 funcRef = clipBit;
969 break;
970 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000971 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +0000972 }
973 } else {
974 funcMask &= userBits;
975 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000976 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000977 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000978 gSpecialToBasicStencilFunc[respectClip];
979 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000980 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000981 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000982 funcMask &= userBits;
983 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000984 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000985
986 settings->setFunc(face, func);
987 settings->setWriteMask(face, writeMask);
988 settings->setFuncMask(face, funcMask);
989 settings->setFuncRef(face, funcRef);
990
991 if (GrStencilSettings::kFront_Face == face) {
992 face = GrStencilSettings::kBack_Face;
993 finished = !twoSided;
994 } else {
995 finished = true;
996 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000997 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000998 if (!twoSided) {
999 settings->copyFrontSettingsToBack();
1000 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001001}
1002
1003////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +00001004GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001005 GrReducedClip::InitialState initialState,
1006 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -08001007 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001008 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001009 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001010
krajcevskiad1dc582014-06-10 15:06:47 -07001011 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -07001012 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001013 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001014 }
1015
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001016 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1017 // the top left corner of the resulting rect to the top left of the texture.
1018 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1019
robertphillips@google.com2c756812012-05-22 20:28:23 +00001020 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001021
joshualitt8059eb92014-12-29 15:10:07 -08001022 // Set the matrix so that rendered clip elements are transformed to mask space from clip
1023 // space.
1024 SkMatrix translate;
1025 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -08001026
joshualitt8059eb92014-12-29 15:10:07 -08001027 helper.init(maskSpaceIBounds, &translate, false);
tfarinabf54e492014-10-23 17:47:18 -07001028 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001029 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001030
tfarinabf54e492014-10-23 17:47:18 -07001031 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001032 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001033 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001034
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001035 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1036 // Intersect and reverse difference require modifying pixels outside of the geometry
1037 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1038 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1039 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001040 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001041 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001042 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001043 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001044 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001045 SkPath clipPath;
1046 element->asPath(&clipPath);
1047 clipPath.toggleInverseFillType();
1048 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001049 continue;
1050 }
1051
1052 // The other ops (union, xor, diff) only affect pixels inside
1053 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001054 if (Element::kRect_Type == element->getType()) {
1055 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1056 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001057 SkPath path;
1058 element->asPath(&path);
1059 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001060 }
1061 }
1062
krajcevskiad1dc582014-06-10 15:06:47 -07001063 // Allocate clip mask texture
1064 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, true);
1065 if (NULL == result) {
1066 fAACache.reset();
1067 return NULL;
1068 }
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001069 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001070
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001071 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001072 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001073}
1074
robertphillips@google.comf294b772012-04-27 14:29:26 +00001075////////////////////////////////////////////////////////////////////////////////
bsalomonc8dc1f72014-08-21 13:02:13 -07001076void GrClipMaskManager::purgeResources() {
1077 fAACache.purgeResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001078}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001079
joshualitt329bf482014-10-29 12:31:28 -07001080void GrClipMaskManager::setClipTarget(GrClipTarget* clipTarget) {
1081 fClipTarget = clipTarget;
1082 fAACache.setContext(clipTarget->getContext());
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001083}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001084
joshualitt9853cce2014-11-17 14:22:48 -08001085void GrClipMaskManager::adjustPathStencilParams(const GrStencilBuffer* stencilBuffer,
1086 GrStencilSettings* settings) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001087 // TODO: dynamically attach a stencil buffer
bsalomon49f085d2014-09-05 13:34:00 -07001088 if (stencilBuffer) {
joshualitt9853cce2014-11-17 14:22:48 -08001089 int stencilBits = stencilBuffer->bits();
joshualitt7a6184f2014-10-29 18:29:27 -07001090 this->adjustStencilParams(settings, fClipMode, stencilBits);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001091 }
1092}