blob: 17a15a32c45af3333b4f1088510114e978b9254c [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"
bsalomoneb1cb5c2015-05-22 08:01:09 -070011#include "GrCaps.h"
robertphillipsea461502015-05-26 11:38:03 -070012#include "GrDrawContext.h"
13#include "GrDrawTarget.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000014#include "GrPaint.h"
15#include "GrPathRenderer.h"
16#include "GrRenderTarget.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080017#include "GrRenderTargetPriv.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070018#include "GrStencilAttachment.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000019#include "GrSWMaskHelper.h"
joshualitt3a0cfeb2014-10-27 07:38:01 -070020#include "SkRasterClip.h"
joshualitt3a0cfeb2014-10-27 07:38:01 -070021#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
joshualitt642429e2015-02-26 08:47:52 -080033void setup_drawstate_aaclip(GrPipelineBuilder* pipelineBuilder,
34 GrTexture* result,
bsalomon6be6f7c2015-02-26 13:05:21 -080035 GrPipelineBuilder::AutoRestoreFragmentProcessors* arfp,
joshualitt642429e2015-02-26 08:47:52 -080036 const SkIRect &devBound) {
bsalomon6be6f7c2015-02-26 13:05:21 -080037 SkASSERT(pipelineBuilder && arfp);
38 arfp->set(pipelineBuilder);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000039
bsalomon@google.comb9086a02012-11-01 18:02:54 +000040 SkMatrix mat;
bsalomon309d4d52014-12-18 10:17:44 -080041 // We use device coords to compute the texture coordinates. We set our matrix to be a
42 // translation to the devBound, and then a scaling matrix to normalized coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000043 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000044 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000045 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000046
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000047 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000048 // This could be a long-lived effect that is cached with the alpha-mask.
egdaniel8dd688b2015-01-22 10:16:09 -080049 pipelineBuilder->addCoverageProcessor(
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000050 GrTextureDomainEffect::Create(result,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000051 mat,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000052 GrTextureDomain::MakeTexelDomain(result, domainTexels),
53 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +000054 GrTextureParams::kNone_FilterMode,
bsalomon309d4d52014-12-18 10:17:44 -080055 kDevice_GrCoordSet))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000056}
57
robertphillips@google.come79f3202014-02-11 16:30:21 +000058bool path_needs_SW_renderer(GrContext* context,
joshualitt9853cce2014-11-17 14:22:48 -080059 const GrDrawTarget* gpu,
egdaniel8dd688b2015-01-22 10:16:09 -080060 const GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -080061 const SkMatrix& viewMatrix,
robertphillips@google.come79f3202014-02-11 16:30:21 +000062 const SkPath& origPath,
kkinnunen18996512015-04-26 23:18:49 -070063 const GrStrokeInfo& stroke,
robertphillips@google.come79f3202014-02-11 16:30:21 +000064 bool doAA) {
65 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
66 SkTCopyOnFirstWrite<SkPath> path(origPath);
67 if (path->isInverseFillType()) {
68 path.writable()->toggleInverseFillType();
69 }
70 // last (false) parameter disallows use of the SW path renderer
bsalomon@google.com45a15f52012-12-10 19:10:17 +000071 GrPathRendererChain::DrawType type = doAA ?
72 GrPathRendererChain::kColorAntiAlias_DrawType :
73 GrPathRendererChain::kColor_DrawType;
74
egdaniel8dd688b2015-01-22 10:16:09 -080075 return NULL == context->getPathRenderer(gpu, pipelineBuilder, viewMatrix, *path, stroke,
76 false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +000077}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000078}
79
bsalomonedd77a12015-05-29 09:45:57 -070080GrClipMaskManager::GrClipMaskManager(GrClipTarget* clipTarget)
81 : fCurrClipMaskType(kNone_ClipMaskType)
82 , fAACache(clipTarget->getContext()->resourceProvider())
83 , fClipTarget(clipTarget)
84 , fClipMode(kIgnoreClip_StencilClipMode) {
85}
86
87GrContext* GrClipMaskManager::getContext() { return fClipTarget->getContext(); }
88
robertphillips@google.comfa662942012-05-17 12:20:22 +000089/*
90 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
91 * will be used on any element. If so, it returns true to indicate that the
92 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
93 */
egdaniel8dd688b2015-01-22 10:16:09 -080094bool GrClipMaskManager::useSWOnlyPath(const GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -080095 const SkVector& clipToMaskOffset,
joshualitt9853cce2014-11-17 14:22:48 -080096 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000097 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000098 // a clip gets complex enough it can just be done in SW regardless
99 // of whether it would invoke the GrSoftwarePathRenderer.
kkinnunen18996512015-04-26 23:18:49 -0700100 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000101
joshualitt8059eb92014-12-29 15:10:07 -0800102 // Set the matrix so that rendered clip elements are transformed to mask space from clip
103 // space.
104 SkMatrix translate;
105 translate.setTranslate(clipToMaskOffset);
106
tfarinabf54e492014-10-23 17:47:18 -0700107 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000108 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000109 // rects can always be drawn directly w/o using the software path
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000110 // Skip rrects once we're drawing them directly.
111 if (Element::kRect_Type != element->getType()) {
112 SkPath path;
113 element->asPath(&path);
egdaniel8dd688b2015-01-22 10:16:09 -0800114 if (path_needs_SW_renderer(this->getContext(), fClipTarget, pipelineBuilder, translate,
joshualitt8059eb92014-12-29 15:10:07 -0800115 path, stroke, element->isAA())) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000116 return true;
117 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000118 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000119 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000120 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000121}
122
egdaniel8dd688b2015-01-22 10:16:09 -0800123bool GrClipMaskManager::installClipEffects(GrPipelineBuilder* pipelineBuilder,
bsalomon6be6f7c2015-02-26 13:05:21 -0800124 GrPipelineBuilder::AutoRestoreFragmentProcessors* arfp,
joshualitt9853cce2014-11-17 14:22:48 -0800125 const GrReducedClip::ElementList& elements,
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000126 const SkVector& clipToRTOffset,
mtklein217daa72014-07-02 12:55:21 -0700127 const SkRect* drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000128 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700129 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000130 boundsInClipSpace = *drawBounds;
131 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
132 }
133
bsalomon6be6f7c2015-02-26 13:05:21 -0800134 arfp->set(pipelineBuilder);
egdaniel8dd688b2015-01-22 10:16:09 -0800135 GrRenderTarget* rt = pipelineBuilder->getRenderTarget();
tfarinabf54e492014-10-23 17:47:18 -0700136 GrReducedClip::ElementList::Iter iter(elements);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000137 bool failed = false;
bsalomon49f085d2014-09-05 13:34:00 -0700138 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000139 SkRegion::Op op = iter.get()->getOp();
140 bool invert;
141 bool skip = false;
142 switch (op) {
143 case SkRegion::kReplace_Op:
144 SkASSERT(iter.get() == elements.head());
145 // Fallthrough, handled same as intersect.
146 case SkRegion::kIntersect_Op:
147 invert = false;
bsalomon49f085d2014-09-05 13:34:00 -0700148 if (drawBounds && iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000149 skip = true;
150 }
151 break;
152 case SkRegion::kDifference_Op:
153 invert = true;
154 // We don't currently have a cheap test for whether a rect is fully outside an
155 // element's primitive, so don't attempt to set skip.
156 break;
157 default:
158 failed = true;
159 break;
160 }
161 if (failed) {
162 break;
163 }
164
165 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700166 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800167 if (iter.get()->isAA()) {
vbuzinovdded6962015-06-12 08:59:45 -0700168 if (rt->isUnifiedMultisampled()) {
mtklein217daa72014-07-02 12:55:21 -0700169 // Coverage based AA clips don't place nicely with MSAA.
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000170 failed = true;
171 break;
172 }
joshualittb0a8a372014-09-23 09:50:21 -0700173 edgeType =
174 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000175 } else {
joshualittb0a8a372014-09-23 09:50:21 -0700176 edgeType =
bsalomon96e02a82015-03-06 07:13:01 -0800177invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000178 }
joshualittb0a8a372014-09-23 09:50:21 -0700179 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000180 switch (iter.get()->getType()) {
181 case SkClipStack::Element::kPath_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700182 fp.reset(GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
joshualitt642429e2015-02-26 08:47:52 -0800183 &clipToRTOffset));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000184 break;
185 case SkClipStack::Element::kRRect_Type: {
186 SkRRect rrect = iter.get()->getRRect();
187 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
joshualittb0a8a372014-09-23 09:50:21 -0700188 fp.reset(GrRRectEffect::Create(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000189 break;
190 }
191 case SkClipStack::Element::kRect_Type: {
192 SkRect rect = iter.get()->getRect();
193 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
joshualittb0a8a372014-09-23 09:50:21 -0700194 fp.reset(GrConvexPolyEffect::Create(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000195 break;
196 }
197 default:
198 break;
199 }
joshualittb0a8a372014-09-23 09:50:21 -0700200 if (fp) {
egdaniel8dd688b2015-01-22 10:16:09 -0800201 pipelineBuilder->addCoverageProcessor(fp);
mtklein217daa72014-07-02 12:55:21 -0700202 } else {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000203 failed = true;
204 break;
205 }
206 }
mtklein217daa72014-07-02 12:55:21 -0700207 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000208 }
209
210 if (failed) {
bsalomon6be6f7c2015-02-26 13:05:21 -0800211 arfp->set(NULL);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000212 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000213 return !failed;
214}
215
robertphillips@google.comf294b772012-04-27 14:29:26 +0000216////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000217// sort out what kind of clip mask needs to be created: alpha, stencil,
218// scissor, or entirely software
egdaniel8dd688b2015-01-22 10:16:09 -0800219bool GrClipMaskManager::setupClipping(GrPipelineBuilder* pipelineBuilder,
bsalomon6be6f7c2015-02-26 13:05:21 -0800220 GrPipelineBuilder::AutoRestoreFragmentProcessors* arfp,
egdaniel8dd688b2015-01-22 10:16:09 -0800221 GrPipelineBuilder::AutoRestoreStencil* ars,
bsalomon3e791242014-12-17 13:43:13 -0800222 GrScissorState* scissorState,
joshualitt9853cce2014-11-17 14:22:48 -0800223 const SkRect* devBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000224 fCurrClipMaskType = kNone_ClipMaskType;
joshualitt7a6184f2014-10-29 18:29:27 -0700225 if (kRespectClip_StencilClipMode == fClipMode) {
226 fClipMode = kIgnoreClip_StencilClipMode;
227 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000228
tfarinabf54e492014-10-23 17:47:18 -0700229 GrReducedClip::ElementList elements(16);
brucedawson71d7f7f2015-02-26 13:28:53 -0800230 int32_t genID = 0;
231 GrReducedClip::InitialState initialState = GrReducedClip::kAllIn_InitialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000232 SkIRect clipSpaceIBounds;
brucedawson71d7f7f2015-02-26 13:28:53 -0800233 bool requiresAA = false;
egdaniel8dd688b2015-01-22 10:16:09 -0800234 GrRenderTarget* rt = pipelineBuilder->getRenderTarget();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000235
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000236 // GrDrawTarget should have filtered this for us
bsalomon49f085d2014-09-05 13:34:00 -0700237 SkASSERT(rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000238
joshualitt44701df2015-02-23 14:44:57 -0800239 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
240 const GrClip& clip = pipelineBuilder->clip();
bsalomon96e02a82015-03-06 07:13:01 -0800241 if (clip.isWideOpen(clipSpaceRTIBounds)) {
egdaniel8dd688b2015-01-22 10:16:09 -0800242 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000243 return true;
244 }
245
bsalomon96e02a82015-03-06 07:13:01 -0800246 // The clip mask manager always draws with a single IRect so we special case that logic here
247 // Image filters just use a rect, so we also special case that logic
248 switch (clip.clipType()) {
249 case GrClip::kWideOpen_ClipType:
250 SkFAIL("Should have caught this with clip.isWideOpen()");
251 return true;
bsalomon9ce30e12015-03-06 08:42:34 -0800252 case GrClip::kIRect_ClipType: {
253 SkIRect scissor = clip.irect();
254 if (scissor.intersect(clipSpaceRTIBounds)) {
255 scissorState->set(scissor);
256 this->setPipelineBuilderStencil(pipelineBuilder, ars);
257 return true;
258 }
259 return false;
260 }
bsalomon96e02a82015-03-06 07:13:01 -0800261 case GrClip::kClipStack_ClipType: {
262 clipSpaceRTIBounds.offset(clip.origin());
263 GrReducedClip::ReduceClipStack(*clip.clipStack(),
264 clipSpaceRTIBounds,
265 &elements,
266 &genID,
267 &initialState,
268 &clipSpaceIBounds,
269 &requiresAA);
270 if (elements.isEmpty()) {
271 if (GrReducedClip::kAllIn_InitialState == initialState) {
272 if (clipSpaceIBounds == clipSpaceRTIBounds) {
273 this->setPipelineBuilderStencil(pipelineBuilder, ars);
274 return true;
275 }
276 } else {
277 return false;
278 }
279 }
280 } break;
281 }
282
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000283 // An element count of 4 was chosen because of the common pattern in Blink of:
284 // isect RR
285 // diff RR
286 // isect convex_poly
287 // isect convex_poly
288 // when drawing rounded div borders. This could probably be tuned based on a
289 // configuration's relative costs of switching RTs to generate a mask vs
290 // longer shaders.
291 if (elements.count() <= 4) {
joshualitt44701df2015-02-23 14:44:57 -0800292 SkVector clipToRTOffset = { SkIntToScalar(-clip.origin().fX),
293 SkIntToScalar(-clip.origin().fY) };
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000294 if (elements.isEmpty() ||
bsalomon55f0b182015-03-05 17:43:09 -0800295 (requiresAA && this->installClipEffects(pipelineBuilder, arfp, elements, clipToRTOffset,
296 devBounds))) {
mtklein217daa72014-07-02 12:55:21 -0700297 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
joshualitt44701df2015-02-23 14:44:57 -0800298 scissorSpaceIBounds.offset(-clip.origin());
mtklein217daa72014-07-02 12:55:21 -0700299 if (NULL == devBounds ||
300 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
joshualitt77b13072014-10-27 14:51:01 -0700301 scissorState->set(scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000302 }
egdaniel8dd688b2015-01-22 10:16:09 -0800303 this->setPipelineBuilderStencil(pipelineBuilder, ars);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000304 return true;
305 }
306 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000307
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000308 // If MSAA is enabled we can do everything in the stencil buffer.
vbuzinovdded6962015-06-12 08:59:45 -0700309 if (0 == rt->numColorSamples() && requiresAA) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000310 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000311
joshualitt8059eb92014-12-29 15:10:07 -0800312 // The top-left of the mask corresponds to the top-left corner of the bounds.
313 SkVector clipToMaskOffset = {
314 SkIntToScalar(-clipSpaceIBounds.fLeft),
315 SkIntToScalar(-clipSpaceIBounds.fTop)
316 };
317
egdaniel8dd688b2015-01-22 10:16:09 -0800318 if (this->useSWOnlyPath(pipelineBuilder, clipToMaskOffset, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000319 // The clip geometry is complex enough that it will be more efficient to create it
320 // entirely in software
321 result = this->createSoftwareClipMask(genID,
322 initialState,
323 elements,
joshualitt8059eb92014-12-29 15:10:07 -0800324 clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000325 clipSpaceIBounds);
326 } else {
327 result = this->createAlphaClipMask(genID,
328 initialState,
329 elements,
joshualitt8059eb92014-12-29 15:10:07 -0800330 clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000331 clipSpaceIBounds);
332 }
333
bsalomon49f085d2014-09-05 13:34:00 -0700334 if (result) {
bsalomon6be6f7c2015-02-26 13:05:21 -0800335 arfp->set(pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000336 // The mask's top left coord should be pinned to the rounded-out top left corner of
337 // clipSpace bounds. We determine the mask's position WRT to the render target here.
338 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
joshualitt44701df2015-02-23 14:44:57 -0800339 rtSpaceMaskBounds.offset(-clip.origin());
bsalomon6be6f7c2015-02-26 13:05:21 -0800340 setup_drawstate_aaclip(pipelineBuilder, result, arfp, rtSpaceMaskBounds);
egdaniel8dd688b2015-01-22 10:16:09 -0800341 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000342 return true;
343 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000344 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000345 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000346
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000347 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
348 // be created. In either case, free up the texture in the anti-aliased mask cache.
349 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
350 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
351 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000352 fAACache.reset();
353
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000354 // use the stencil clip if we can't represent the clip as a rectangle.
joshualitt44701df2015-02-23 14:44:57 -0800355 SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin();
joshualitt9853cce2014-11-17 14:22:48 -0800356 this->createStencilClipMask(rt,
357 genID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000358 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000359 elements,
360 clipSpaceIBounds,
361 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000362
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000363 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
364 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
365 // use both stencil and scissor test to the bounds for the final draw.
366 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
367 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
joshualitt77b13072014-10-27 14:51:01 -0700368 scissorState->set(scissorSpaceIBounds);
egdaniel8dd688b2015-01-22 10:16:09 -0800369 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000370 return true;
371}
372
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000373namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000374////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800375// Set a coverage drawing XPF on the pipelineBuilder for the given op and invertCoverage mode
376void set_coverage_drawing_xpf(SkRegion::Op op, bool invertCoverage,
377 GrPipelineBuilder* pipelineBuilder) {
egdaniel87509242014-12-17 13:37:13 -0800378 SkASSERT(op <= SkRegion::kLastOp);
egdaniel8dd688b2015-01-22 10:16:09 -0800379 pipelineBuilder->setCoverageSetOpXPFactory(op, invertCoverage);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000380}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000381}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000382
383////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800384bool GrClipMaskManager::drawElement(GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800385 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800386 GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000387 const SkClipStack::Element* element,
388 GrPathRenderer* pr) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000389
egdaniel8dd688b2015-01-22 10:16:09 -0800390 pipelineBuilder->setRenderTarget(target->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000391
egdaniel87509242014-12-17 13:37:13 -0800392 // The color we use to draw does not matter since we will always be using a GrCoverageSetOpXP
393 // which ignores color.
394 GrColor color = GrColor_WHITE;
395
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000396 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000397 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000398 case Element::kEmpty_Type:
399 SkDEBUGFAIL("Should never get here with an empty element.");
400 break;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000401 case Element::kRect_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700402 // TODO: Do rects directly to the accumulator using a aa-rect GrProcessor that covers
403 // the entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000404 if (element->isAA()) {
joshualitt8059eb92014-12-29 15:10:07 -0800405 SkRect devRect = element->getRect();
406 viewMatrix.mapRect(&devRect);
robertphillipsea461502015-05-26 11:38:03 -0700407
408 fClipTarget->drawAARect(pipelineBuilder, color, viewMatrix,
409 element->getRect(), devRect);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000410 } else {
robertphillipsea461502015-05-26 11:38:03 -0700411 fClipTarget->drawSimpleRect(pipelineBuilder, color, viewMatrix,
412 element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000413 }
414 return true;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000415 default: {
416 SkPath path;
417 element->asPath(&path);
jvanverthb3eb6872014-10-24 07:12:51 -0700418 path.setIsVolatile(true);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000419 if (path.isInverseFillType()) {
420 path.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000421 }
kkinnunen18996512015-04-26 23:18:49 -0700422 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000423 if (NULL == pr) {
424 GrPathRendererChain::DrawType type;
425 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
426 GrPathRendererChain::kColor_DrawType;
egdaniel8dd688b2015-01-22 10:16:09 -0800427 pr = this->getContext()->getPathRenderer(fClipTarget, pipelineBuilder, viewMatrix,
428 path, stroke, false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000429 }
430 if (NULL == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000431 return false;
432 }
joshualitt9853cce2014-11-17 14:22:48 -0800433
egdaniel8dd688b2015-01-22 10:16:09 -0800434 pr->drawPath(fClipTarget, pipelineBuilder, color, viewMatrix, path, stroke,
435 element->isAA());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000436 break;
437 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000438 }
439 return true;
440}
441
egdaniel8dd688b2015-01-22 10:16:09 -0800442bool GrClipMaskManager::canStencilAndDrawElement(GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800443 GrTexture* target,
444 GrPathRenderer** pr,
445 const SkClipStack::Element* element) {
egdaniel8dd688b2015-01-22 10:16:09 -0800446 pipelineBuilder->setRenderTarget(target->asRenderTarget());
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000447
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000448 if (Element::kRect_Type == element->getType()) {
449 return true;
450 } else {
451 // We shouldn't get here with an empty clip element.
452 SkASSERT(Element::kEmpty_Type != element->getType());
453 SkPath path;
454 element->asPath(&path);
455 if (path.isInverseFillType()) {
456 path.toggleInverseFillType();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000457 }
kkinnunen18996512015-04-26 23:18:49 -0700458 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000459 GrPathRendererChain::DrawType type = element->isAA() ?
460 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
461 GrPathRendererChain::kStencilAndColor_DrawType;
egdaniel8dd688b2015-01-22 10:16:09 -0800462 *pr = this->getContext()->getPathRenderer(fClipTarget, pipelineBuilder, SkMatrix::I(), path,
joshualitt8059eb92014-12-29 15:10:07 -0800463 stroke, false, type);
bsalomon49f085d2014-09-05 13:34:00 -0700464 return SkToBool(*pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000465 }
466}
467
egdaniel8dd688b2015-01-22 10:16:09 -0800468void GrClipMaskManager::mergeMask(GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800469 GrTexture* dstMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000470 GrTexture* srcMask,
471 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000472 const SkIRect& dstBound,
473 const SkIRect& srcBound) {
egdaniel8dd688b2015-01-22 10:16:09 -0800474 pipelineBuilder->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000475
egdaniel87509242014-12-17 13:37:13 -0800476 // We want to invert the coverage here
egdaniel8dd688b2015-01-22 10:16:09 -0800477 set_coverage_drawing_xpf(op, false, pipelineBuilder);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000478
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000479 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000480 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000481
egdaniel8dd688b2015-01-22 10:16:09 -0800482 pipelineBuilder->addCoverageProcessor(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000483 GrTextureDomainEffect::Create(srcMask,
484 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000485 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
486 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000487 GrTextureParams::kNone_FilterMode))->unref();
joshualitt73bb4562015-03-25 07:16:21 -0700488
egdaniel87509242014-12-17 13:37:13 -0800489 // The color passed in here does not matter since the coverageSetOpXP won't read it.
joshualitt44701df2015-02-23 14:44:57 -0800490 fClipTarget->drawSimpleRect(pipelineBuilder,
491 GrColor_WHITE,
492 SkMatrix::I(),
egdaniel8dd688b2015-01-22 10:16:09 -0800493 SkRect::Make(dstBound));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000494}
495
bsalomon427cf282014-10-16 13:41:43 -0700496GrTexture* GrClipMaskManager::createTempMask(int width, int height) {
bsalomonf2703d82014-10-28 14:33:06 -0700497 GrSurfaceDesc desc;
bsalomon3f490a02014-12-18 06:20:52 -0800498 desc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000499 desc.fWidth = width;
500 desc.fHeight = height;
bsalomon76228632015-05-29 08:02:10 -0700501 if (this->getContext()->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
bsalomon51d1f7e2014-12-22 08:40:49 -0800502 desc.fConfig = kAlpha_8_GrPixelConfig;
503 } else {
504 desc.fConfig = kRGBA_8888_GrPixelConfig;
505 }
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000506
bsalomond309e7a2015-04-30 14:18:54 -0700507 return this->getContext()->textureProvider()->refScratchTexture(
508 desc, GrTextureProvider::kApprox_ScratchTexMatch);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000509}
510
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000511////////////////////////////////////////////////////////////////////////////////
krajcevskiad1dc582014-06-10 15:06:47 -0700512// Return the texture currently in the cache if it exists. Otherwise, return NULL
513GrTexture* GrClipMaskManager::getCachedMaskTexture(int32_t elementsGenID,
514 const SkIRect& clipSpaceIBounds) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000515 bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000516 if (!cached) {
krajcevskiad1dc582014-06-10 15:06:47 -0700517 return NULL;
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000518 }
519
krajcevskiad1dc582014-06-10 15:06:47 -0700520 return fAACache.getLastMask();
521}
522
523////////////////////////////////////////////////////////////////////////////////
524// Allocate a texture in the texture cache. This function returns the texture
525// allocated (or NULL on error).
526GrTexture* GrClipMaskManager::allocMaskTexture(int32_t elementsGenID,
527 const SkIRect& clipSpaceIBounds,
528 bool willUpload) {
529 // Since we are setting up the cache we should free up the
530 // currently cached mask so it can be reused.
531 fAACache.reset();
532
bsalomonf2703d82014-10-28 14:33:06 -0700533 GrSurfaceDesc desc;
534 desc.fFlags = willUpload ? kNone_GrSurfaceFlags : kRenderTarget_GrSurfaceFlag;
krajcevskiad1dc582014-06-10 15:06:47 -0700535 desc.fWidth = clipSpaceIBounds.width();
536 desc.fHeight = clipSpaceIBounds.height();
537 desc.fConfig = kRGBA_8888_GrPixelConfig;
bsalomon76228632015-05-29 08:02:10 -0700538 if (willUpload ||
539 this->getContext()->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
krajcevskiad1dc582014-06-10 15:06:47 -0700540 // We would always like A8 but it isn't supported on all platforms
541 desc.fConfig = kAlpha_8_GrPixelConfig;
542 }
543
544 fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds);
545 return fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000546}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000547
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000548////////////////////////////////////////////////////////////////////////////////
549// Create a 8-bit clip mask in alpha
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000550GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700551 GrReducedClip::InitialState initialState,
552 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800553 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000554 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000555 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000556
krajcevskiad1dc582014-06-10 15:06:47 -0700557 // First, check for cached texture
558 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -0700559 if (result) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000560 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000561 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000562 }
563
krajcevskiad1dc582014-06-10 15:06:47 -0700564 // There's no texture in the cache. Let's try to allocate it then.
565 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, false);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000566 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000567 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000568 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000569 }
570
joshualitt8059eb92014-12-29 15:10:07 -0800571 // Set the matrix so that rendered clip elements are transformed to mask space from clip
572 // space.
573 SkMatrix translate;
574 translate.setTranslate(clipToMaskOffset);
575
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000576 // The texture may be larger than necessary, this rect represents the part of the texture
577 // we populate with a rasterization of the clip.
578 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
579
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000580 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
581 // clear the part that we care about.
joshualitt329bf482014-10-29 12:31:28 -0700582 fClipTarget->clear(&maskSpaceIBounds,
583 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
584 true,
585 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000586
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000587 // When we use the stencil in the below loop it is important to have this clip installed.
588 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
589 // pass must not set values outside of this bounds or stencil values outside the rect won't be
590 // cleared.
joshualitt44701df2015-02-23 14:44:57 -0800591 GrClip clip(maskSpaceIBounds);
bsalomon427cf282014-10-16 13:41:43 -0700592 SkAutoTUnref<GrTexture> temp;
joshualitt9853cce2014-11-17 14:22:48 -0800593
robertphillips@google.comf294b772012-04-27 14:29:26 +0000594 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700595 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000596 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000597 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000598 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000599 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
egdaniel8dd688b2015-01-22 10:16:09 -0800600 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800601
joshualitt44701df2015-02-23 14:44:57 -0800602 pipelineBuilder.setClip(clip);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000603 GrPathRenderer* pr = NULL;
egdaniel8dd688b2015-01-22 10:16:09 -0800604 bool useTemp = !this->canStencilAndDrawElement(&pipelineBuilder, result, &pr, element);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000605 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000606 // 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 +0000607 // mask buffer can be substantially larger than the actually clip stack element. We
608 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000609 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000610 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000611
612 if (useTemp) {
613 if (invert) {
614 maskSpaceElementIBounds = maskSpaceIBounds;
615 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000616 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000617 elementBounds.offset(clipToMaskOffset);
618 elementBounds.roundOut(&maskSpaceElementIBounds);
619 }
620
bsalomon427cf282014-10-16 13:41:43 -0700621 if (!temp) {
622 temp.reset(this->createTempMask(maskSpaceIBounds.fRight,
623 maskSpaceIBounds.fBottom));
624 if (!temp) {
625 fAACache.reset();
626 return NULL;
627 }
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000628 }
bsalomon427cf282014-10-16 13:41:43 -0700629 dst = temp;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000630 // clear the temp target and set blend to replace
joshualitt329bf482014-10-29 12:31:28 -0700631 fClipTarget->clear(&maskSpaceElementIBounds,
joshualitt9853cce2014-11-17 14:22:48 -0800632 invert ? 0xffffffff : 0x00000000,
633 true,
634 dst->asRenderTarget());
egdaniel8dd688b2015-01-22 10:16:09 -0800635 set_coverage_drawing_xpf(SkRegion::kReplace_Op, invert, &pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000636 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000637 // draw directly into the result with the stencil set to make the pixels affected
638 // by the clip shape be non-zero.
639 dst = result;
640 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
641 kReplace_StencilOp,
642 kReplace_StencilOp,
643 kAlways_StencilFunc,
644 0xffff,
645 0xffff,
646 0xffff);
egdaniel8dd688b2015-01-22 10:16:09 -0800647 pipelineBuilder.setStencil(kStencilInElement);
648 set_coverage_drawing_xpf(op, invert, &pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000649 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000650
egdaniel8dd688b2015-01-22 10:16:09 -0800651 if (!this->drawElement(&pipelineBuilder, translate, dst, element, pr)) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000652 fAACache.reset();
653 return NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000654 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000655
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000656 if (useTemp) {
egdaniel8dd688b2015-01-22 10:16:09 -0800657 GrPipelineBuilder backgroundPipelineBuilder;
egdaniel8dd688b2015-01-22 10:16:09 -0800658 backgroundPipelineBuilder.setRenderTarget(result->asRenderTarget());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800659
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000660 // Now draw into the accumulator using the real operation and the temp buffer as a
661 // texture
egdaniel8dd688b2015-01-22 10:16:09 -0800662 this->mergeMask(&backgroundPipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800663 result,
bsalomon427cf282014-10-16 13:41:43 -0700664 temp,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000665 op,
666 maskSpaceIBounds,
667 maskSpaceElementIBounds);
668 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800669 GrPipelineBuilder backgroundPipelineBuilder;
egdaniel8dd688b2015-01-22 10:16:09 -0800670 backgroundPipelineBuilder.setRenderTarget(result->asRenderTarget());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800671
egdaniel8dd688b2015-01-22 10:16:09 -0800672 set_coverage_drawing_xpf(op, !invert, &backgroundPipelineBuilder);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000673 // Draw to the exterior pixels (those with a zero stencil value).
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000674 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
675 kZero_StencilOp,
676 kZero_StencilOp,
677 kEqual_StencilFunc,
678 0xffff,
679 0x0000,
680 0xffff);
egdaniel8dd688b2015-01-22 10:16:09 -0800681 backgroundPipelineBuilder.setStencil(kDrawOutsideElement);
joshualitt73bb4562015-03-25 07:16:21 -0700682
egdaniel87509242014-12-17 13:37:13 -0800683 // The color passed in here does not matter since the coverageSetOpXP won't read it.
egdaniel8dd688b2015-01-22 10:16:09 -0800684 fClipTarget->drawSimpleRect(&backgroundPipelineBuilder, GrColor_WHITE, translate,
joshualitt8059eb92014-12-29 15:10:07 -0800685 clipSpaceIBounds);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000686 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000687 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800688 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800689
robertphillips@google.come79f3202014-02-11 16:30:21 +0000690 // all the remaining ops can just be directly draw into the accumulation buffer
egdaniel8dd688b2015-01-22 10:16:09 -0800691 set_coverage_drawing_xpf(op, false, &pipelineBuilder);
egdaniel87509242014-12-17 13:37:13 -0800692 // The color passed in here does not matter since the coverageSetOpXP won't read it.
egdaniel8dd688b2015-01-22 10:16:09 -0800693 this->drawElement(&pipelineBuilder, translate, result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000694 }
695 }
696
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000697 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000698 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000699}
700
701////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000702// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000703// (as opposed to canvas) coordinates
joshualitt9853cce2014-11-17 14:22:48 -0800704bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt,
705 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700706 GrReducedClip::InitialState initialState,
707 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000708 const SkIRect& clipSpaceIBounds,
709 const SkIPoint& clipSpaceToStencilOffset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000710 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon49f085d2014-09-05 13:34:00 -0700711 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000712
egdaniel8dc7c3a2015-04-16 11:22:42 -0700713 GrStencilAttachment* stencilAttachment = rt->renderTargetPriv().attachStencilAttachment();
714 if (NULL == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000715 return false;
716 }
717
egdaniel8dc7c3a2015-04-16 11:22:42 -0700718 if (stencilAttachment->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
719 stencilAttachment->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000720 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
721 SkVector translate = {
722 SkIntToScalar(clipSpaceToStencilOffset.fX),
723 SkIntToScalar(clipSpaceToStencilOffset.fY)
724 };
joshualitt8059eb92014-12-29 15:10:07 -0800725 SkMatrix viewMatrix;
726 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000727
bsalomon@google.com9f131742012-12-13 20:43:56 +0000728 // We set the current clip to the bounds so that our recursive draws are scissored to them.
729 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
730 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
joshualitt44701df2015-02-23 14:44:57 -0800731 GrClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000732
egdaniel8dc7c3a2015-04-16 11:22:42 -0700733 int clipBit = stencilAttachment->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000734 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000735 clipBit = (1 << (clipBit-1));
736
joshualitt329bf482014-10-29 12:31:28 -0700737 fClipTarget->clearStencilClip(stencilSpaceIBounds,
738 GrReducedClip::kAllIn_InitialState == initialState,
739 rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000740
741 // walk through each clip element and perform its set op
742 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700743 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000744 const Element* element = iter.get();
joshualitt9853cce2014-11-17 14:22:48 -0800745
egdaniel8dd688b2015-01-22 10:16:09 -0800746 GrPipelineBuilder pipelineBuilder;
joshualitt44701df2015-02-23 14:44:57 -0800747 pipelineBuilder.setClip(clip);
egdaniel8dd688b2015-01-22 10:16:09 -0800748 pipelineBuilder.setRenderTarget(rt);
egdaniel080e6732014-12-22 07:35:52 -0800749
egdaniel8dd688b2015-01-22 10:16:09 -0800750 pipelineBuilder.setDisableColorXPFactory();
joshualitt9853cce2014-11-17 14:22:48 -0800751
752 // if the target is MSAA then we want MSAA enabled when the clip is soft
vbuzinovdded6962015-06-12 08:59:45 -0700753 if (rt->isUnifiedMultisampled()) {
bsalomond79c5492015-04-27 10:07:04 -0700754 pipelineBuilder.setState(GrPipelineBuilder::kHWAntialias_Flag, element->isAA());
joshualitt9853cce2014-11-17 14:22:48 -0800755 }
756
tomhudson@google.com8afae612012-08-14 15:03:35 +0000757 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000758 // enabled at bottom of loop
joshualitt7a6184f2014-10-29 18:29:27 -0700759 fClipMode = kIgnoreClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000760
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000761 // This will be used to determine whether the clip shape can be rendered into the
762 // stencil with arbitrary stencil settings.
763 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000764
kkinnunen18996512015-04-26 23:18:49 -0700765 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000766 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000767
robertphillips@google.come79f3202014-02-11 16:30:21 +0000768 GrPathRenderer* pr = NULL;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000769 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000770 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000771 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000772 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000773 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000774 element->asPath(&clipPath);
775 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000776 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000777 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000778 }
joshualitt9853cce2014-11-17 14:22:48 -0800779 pr = this->getContext()->getPathRenderer(fClipTarget,
egdaniel8dd688b2015-01-22 10:16:09 -0800780 &pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800781 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800782 clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000783 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000784 false,
785 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000786 &stencilSupport);
787 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000788 return false;
789 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000790 }
791
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000792 int passes;
793 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
794
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000795 bool canRenderDirectToStencil =
796 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000797 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000798 // fill rule, and set operation can
799 // we render the element directly to
800 // stencil bit used for clipping.
801 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
802 canRenderDirectToStencil,
803 clipBit,
804 fillInverted,
805 &passes,
806 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000807
808 // draw the element to the client stencil bits if necessary
809 if (!canDrawDirectToClip) {
810 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000811 kIncClamp_StencilOp,
812 kIncClamp_StencilOp,
813 kAlways_StencilFunc,
814 0xffff,
815 0x0000,
816 0xffff);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000817 if (Element::kRect_Type == element->getType()) {
egdaniel8dd688b2015-01-22 10:16:09 -0800818 *pipelineBuilder.stencil() = gDrawToStencil;
joshualitt73bb4562015-03-25 07:16:21 -0700819
820 // We need this AGP until everything is in GrBatch
joshualitt44701df2015-02-23 14:44:57 -0800821 fClipTarget->drawSimpleRect(&pipelineBuilder,
822 GrColor_WHITE,
823 viewMatrix,
joshualitt8059eb92014-12-29 15:10:07 -0800824 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000825 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000826 if (!clipPath.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000827 if (canRenderDirectToStencil) {
egdaniel8dd688b2015-01-22 10:16:09 -0800828 *pipelineBuilder.stencil() = gDrawToStencil;
joshualitt44701df2015-02-23 14:44:57 -0800829 pr->drawPath(fClipTarget, &pipelineBuilder, GrColor_WHITE,
830 viewMatrix, clipPath, stroke, false);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000831 } else {
joshualitt44701df2015-02-23 14:44:57 -0800832 pr->stencilPath(fClipTarget, &pipelineBuilder, viewMatrix,
833 clipPath, stroke);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000834 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000835 }
836 }
837 }
838
839 // now we modify the clip bit by rendering either the clip
840 // element directly or a bounding rect of the entire clip.
joshualitt7a6184f2014-10-29 18:29:27 -0700841 fClipMode = kModifyClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000842 for (int p = 0; p < passes; ++p) {
egdaniel8dd688b2015-01-22 10:16:09 -0800843 GrPipelineBuilder pipelineBuilderCopy(pipelineBuilder);
844 *pipelineBuilderCopy.stencil() = stencilSettings[p];
joshualitt9853cce2014-11-17 14:22:48 -0800845
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000846 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000847 if (Element::kRect_Type == element->getType()) {
joshualitt73bb4562015-03-25 07:16:21 -0700848 // We need this AGP until everything is in GrBatch
joshualitt44701df2015-02-23 14:44:57 -0800849 fClipTarget->drawSimpleRect(&pipelineBuilderCopy,
850 GrColor_WHITE,
851 viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800852 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000853 } else {
joshualitt44701df2015-02-23 14:44:57 -0800854 pr->drawPath(fClipTarget, &pipelineBuilderCopy, GrColor_WHITE,
855 viewMatrix, clipPath, stroke, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000856 }
857 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000858 // The view matrix is setup to do clip space -> stencil space translation, so
859 // draw rect in clip space.
joshualitt44701df2015-02-23 14:44:57 -0800860 fClipTarget->drawSimpleRect(&pipelineBuilderCopy,
861 GrColor_WHITE,
862 viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800863 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000864 }
865 }
866 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000867 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000868 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000869 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000870 fCurrClipMaskType = kStencil_ClipMaskType;
joshualitt7a6184f2014-10-29 18:29:27 -0700871 fClipMode = kRespectClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000872 return true;
873}
874
bsalomon@google.com411dad02012-06-05 20:24:20 +0000875// mapping of clip-respecting stencil funcs to normal stencil funcs
876// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000877static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000878 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
879 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
880 // In the Clip Funcs
881 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
882 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
883 kLess_StencilFunc, // kLessIfInClip_StencilFunc
884 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
885 // Special in the clip func that forces user's ref to be 0.
886 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
887 // make ref 0 and do normal nequal.
888 },
889 {// Stencil-Clipping is ENABLED
890 // In the Clip Funcs
891 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
892 // eq stencil clip bit, mask
893 // out user bits.
894
895 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
896 // add stencil bit to mask and ref
897
898 kLess_StencilFunc, // kLessIfInClip_StencilFunc
899 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
900 // for both of these we can add
901 // the clip bit to the mask and
902 // ref and compare as normal
903 // Special in the clip func that forces user's ref to be 0.
904 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
905 // make ref have only the clip bit set
906 // and make comparison be less
907 // 10..0 < 1..user_bits..
908 }
909};
910
bsalomon@google.coma3201942012-06-21 19:58:20 +0000911namespace {
912// Sets the settings to clip against the stencil buffer clip while ignoring the
913// client bits.
914const GrStencilSettings& basic_apply_stencil_clip_settings() {
915 // stencil settings to use when clip is in stencil
916 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
917 kKeep_StencilOp,
918 kKeep_StencilOp,
919 kAlwaysIfInClip_StencilFunc,
920 0x0000,
921 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000922 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000923 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
924}
925}
926
egdaniel8dd688b2015-01-22 10:16:09 -0800927void GrClipMaskManager::setPipelineBuilderStencil(GrPipelineBuilder* pipelineBuilder,
928 GrPipelineBuilder::AutoRestoreStencil* ars) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000929 // We make two copies of the StencilSettings here (except in the early
930 // exit scenario. One copy from draw state to the stack var. Then another
931 // from the stack var to the gpu. We could make this class hold a ptr to
932 // GrGpu's fStencilSettings and eliminate the stack copy here.
933
bsalomon@google.coma3201942012-06-21 19:58:20 +0000934 // use stencil for clipping if clipping is enabled and the clip
935 // has been written into the stencil.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000936 GrStencilSettings settings;
joshualitt9853cce2014-11-17 14:22:48 -0800937
bsalomon@google.coma3201942012-06-21 19:58:20 +0000938 // The GrGpu client may not be using the stencil buffer but we may need to
939 // enable it in order to respect a stencil clip.
egdaniel8dd688b2015-01-22 10:16:09 -0800940 if (pipelineBuilder->getStencil().isDisabled()) {
joshualitt7a6184f2014-10-29 18:29:27 -0700941 if (GrClipMaskManager::kRespectClip_StencilClipMode == fClipMode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000942 settings = basic_apply_stencil_clip_settings();
943 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000944 return;
945 }
946 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800947 settings = pipelineBuilder->getStencil();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000948 }
949
bsalomon@google.coma3201942012-06-21 19:58:20 +0000950 int stencilBits = 0;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800951 GrRenderTarget* rt = pipelineBuilder->getRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700952 GrStencilAttachment* stencilAttachment = rt->renderTargetPriv().attachStencilAttachment();
953 if (stencilAttachment) {
954 stencilBits = stencilAttachment->bits();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000955 }
956
joshualitt329bf482014-10-29 12:31:28 -0700957 SkASSERT(fClipTarget->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
958 SkASSERT(fClipTarget->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
joshualitt7a6184f2014-10-29 18:29:27 -0700959 this->adjustStencilParams(&settings, fClipMode, stencilBits);
egdaniel8dd688b2015-01-22 10:16:09 -0800960 ars->set(pipelineBuilder);
961 pipelineBuilder->setStencil(settings);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000962}
963
964void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
965 StencilClipMode mode,
966 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000967 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000968
969 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000970 // We assume that this clip manager itself is drawing to the GrGpu and
971 // has already setup the correct values.
972 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000973 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000974
bsalomon@google.com411dad02012-06-05 20:24:20 +0000975 unsigned int clipBit = (1 << (stencilBitCnt - 1));
976 unsigned int userBits = clipBit - 1;
977
bsalomon@google.coma3201942012-06-21 19:58:20 +0000978 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
joshualitt329bf482014-10-29 12:31:28 -0700979 bool twoSided = fClipTarget->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000980
bsalomon@google.coma3201942012-06-21 19:58:20 +0000981 bool finished = false;
982 while (!finished) {
983 GrStencilFunc func = settings->func(face);
984 uint16_t writeMask = settings->writeMask(face);
985 uint16_t funcMask = settings->funcMask(face);
986 uint16_t funcRef = settings->funcRef(face);
987
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000988 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000989
990 writeMask &= userBits;
991
992 if (func >= kBasicStencilFuncCount) {
993 int respectClip = kRespectClip_StencilClipMode == mode;
994 if (respectClip) {
995 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000996 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000997 switch (func) {
998 case kAlwaysIfInClip_StencilFunc:
999 funcMask = clipBit;
1000 funcRef = clipBit;
1001 break;
1002 case kEqualIfInClip_StencilFunc:
1003 case kLessIfInClip_StencilFunc:
1004 case kLEqualIfInClip_StencilFunc:
1005 funcMask = (funcMask & userBits) | clipBit;
1006 funcRef = (funcRef & userBits) | clipBit;
1007 break;
1008 case kNonZeroIfInClip_StencilFunc:
1009 funcMask = (funcMask & userBits) | clipBit;
1010 funcRef = clipBit;
1011 break;
1012 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001013 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +00001014 }
1015 } else {
1016 funcMask &= userBits;
1017 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001018 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001019 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001020 gSpecialToBasicStencilFunc[respectClip];
1021 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001022 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001023 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001024 funcMask &= userBits;
1025 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001026 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001027
1028 settings->setFunc(face, func);
1029 settings->setWriteMask(face, writeMask);
1030 settings->setFuncMask(face, funcMask);
1031 settings->setFuncRef(face, funcRef);
1032
1033 if (GrStencilSettings::kFront_Face == face) {
1034 face = GrStencilSettings::kBack_Face;
1035 finished = !twoSided;
1036 } else {
1037 finished = true;
1038 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001039 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001040 if (!twoSided) {
1041 settings->copyFrontSettingsToBack();
1042 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001043}
1044
1045////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +00001046GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001047 GrReducedClip::InitialState initialState,
1048 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -08001049 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001050 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001051 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001052
krajcevskiad1dc582014-06-10 15:06:47 -07001053 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -07001054 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001055 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001056 }
1057
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001058 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1059 // the top left corner of the resulting rect to the top left of the texture.
1060 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1061
robertphillips@google.com2c756812012-05-22 20:28:23 +00001062 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001063
joshualitt8059eb92014-12-29 15:10:07 -08001064 // Set the matrix so that rendered clip elements are transformed to mask space from clip
1065 // space.
1066 SkMatrix translate;
1067 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -08001068
joshualitt8059eb92014-12-29 15:10:07 -08001069 helper.init(maskSpaceIBounds, &translate, false);
tfarinabf54e492014-10-23 17:47:18 -07001070 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001071 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001072
tfarinabf54e492014-10-23 17:47:18 -07001073 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001074 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001075 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001076
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001077 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1078 // Intersect and reverse difference require modifying pixels outside of the geometry
1079 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1080 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1081 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001082 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001083 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001084 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001085 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001086 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001087 SkPath clipPath;
1088 element->asPath(&clipPath);
1089 clipPath.toggleInverseFillType();
1090 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001091 continue;
1092 }
1093
1094 // The other ops (union, xor, diff) only affect pixels inside
1095 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001096 if (Element::kRect_Type == element->getType()) {
1097 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1098 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001099 SkPath path;
1100 element->asPath(&path);
1101 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001102 }
1103 }
1104
krajcevskiad1dc582014-06-10 15:06:47 -07001105 // Allocate clip mask texture
1106 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, true);
1107 if (NULL == result) {
1108 fAACache.reset();
1109 return NULL;
1110 }
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001111 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001112
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001113 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001114 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001115}
1116
robertphillips@google.comf294b772012-04-27 14:29:26 +00001117////////////////////////////////////////////////////////////////////////////////
bsalomonc8dc1f72014-08-21 13:02:13 -07001118void GrClipMaskManager::purgeResources() {
1119 fAACache.purgeResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001120}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001121
egdaniel8dc7c3a2015-04-16 11:22:42 -07001122void GrClipMaskManager::adjustPathStencilParams(const GrStencilAttachment* stencilAttachment,
joshualitt9853cce2014-11-17 14:22:48 -08001123 GrStencilSettings* settings) {
egdaniel8dc7c3a2015-04-16 11:22:42 -07001124 if (stencilAttachment) {
1125 int stencilBits = stencilAttachment->bits();
joshualitt7a6184f2014-10-29 18:29:27 -07001126 this->adjustStencilParams(settings, fClipMode, stencilBits);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001127 }
1128}