blob: 7184fe2d3d98857852f4c5e169016adff4767c21 [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
joshualitt5e6ba212015-07-13 07:35:05 -070033void setup_drawstate_aaclip(const GrPipelineBuilder& pipelineBuilder,
joshualitt642429e2015-02-26 08:47:52 -080034 GrTexture* result,
joshualitt4421a4c2015-07-13 09:36:41 -070035 GrPipelineBuilder::AutoRestoreFragmentProcessorState* arfps,
joshualitt642429e2015-02-26 08:47:52 -080036 const SkIRect &devBound) {
joshualitt4421a4c2015-07-13 09:36:41 -070037 SkASSERT(arfps);
38 arfps->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.
bsalomonac856c92015-08-27 06:30:17 -070049 arfps->addCoverageFragmentProcessor(
joshualitt4421a4c2015-07-13 09:36:41 -070050 GrTextureDomainEffect::Create(arfps->getProcessorDataManager(),
joshualitt5f10b5c2015-07-09 10:24:35 -070051 result,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000052 mat,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000053 GrTextureDomain::MakeTexelDomain(result, domainTexels),
54 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +000055 GrTextureParams::kNone_FilterMode,
bsalomon309d4d52014-12-18 10:17:44 -080056 kDevice_GrCoordSet))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000057}
58
robertphillips@google.come79f3202014-02-11 16:30:21 +000059bool path_needs_SW_renderer(GrContext* context,
joshualitt9853cce2014-11-17 14:22:48 -080060 const GrDrawTarget* gpu,
joshualitt5e6ba212015-07-13 07:35:05 -070061 const GrPipelineBuilder& pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -080062 const SkMatrix& viewMatrix,
robertphillips@google.come79f3202014-02-11 16:30:21 +000063 const SkPath& origPath,
kkinnunen18996512015-04-26 23:18:49 -070064 const GrStrokeInfo& stroke,
robertphillips@google.come79f3202014-02-11 16:30:21 +000065 bool doAA) {
66 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
67 SkTCopyOnFirstWrite<SkPath> path(origPath);
68 if (path->isInverseFillType()) {
69 path.writable()->toggleInverseFillType();
70 }
71 // last (false) parameter disallows use of the SW path renderer
bsalomon@google.com45a15f52012-12-10 19:10:17 +000072 GrPathRendererChain::DrawType type = doAA ?
73 GrPathRendererChain::kColorAntiAlias_DrawType :
74 GrPathRendererChain::kColor_DrawType;
75
halcanary96fcdcc2015-08-27 07:41:13 -070076 return nullptr == context->getPathRenderer(gpu, &pipelineBuilder, viewMatrix, *path, stroke,
egdaniel8dd688b2015-01-22 10:16:09 -080077 false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +000078}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000079}
80
bsalomonedd77a12015-05-29 09:45:57 -070081GrClipMaskManager::GrClipMaskManager(GrClipTarget* clipTarget)
82 : fCurrClipMaskType(kNone_ClipMaskType)
83 , fAACache(clipTarget->getContext()->resourceProvider())
84 , fClipTarget(clipTarget)
85 , fClipMode(kIgnoreClip_StencilClipMode) {
86}
87
88GrContext* GrClipMaskManager::getContext() { return fClipTarget->getContext(); }
89
robertphillips@google.comfa662942012-05-17 12:20:22 +000090/*
91 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
92 * will be used on any element. If so, it returns true to indicate that the
93 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
94 */
joshualitt5e6ba212015-07-13 07:35:05 -070095bool GrClipMaskManager::useSWOnlyPath(const GrPipelineBuilder& pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -080096 const SkVector& clipToMaskOffset,
joshualitt9853cce2014-11-17 14:22:48 -080097 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000098 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000099 // a clip gets complex enough it can just be done in SW regardless
100 // of whether it would invoke the GrSoftwarePathRenderer.
kkinnunen18996512015-04-26 23:18:49 -0700101 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000102
joshualitt8059eb92014-12-29 15:10:07 -0800103 // Set the matrix so that rendered clip elements are transformed to mask space from clip
104 // space.
105 SkMatrix translate;
106 translate.setTranslate(clipToMaskOffset);
107
tfarinabf54e492014-10-23 17:47:18 -0700108 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000109 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000110 // rects can always be drawn directly w/o using the software path
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000111 // Skip rrects once we're drawing them directly.
112 if (Element::kRect_Type != element->getType()) {
113 SkPath path;
114 element->asPath(&path);
egdaniel8dd688b2015-01-22 10:16:09 -0800115 if (path_needs_SW_renderer(this->getContext(), fClipTarget, pipelineBuilder, translate,
joshualitt8059eb92014-12-29 15:10:07 -0800116 path, stroke, element->isAA())) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000117 return true;
118 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000119 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000120 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000121 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000122}
123
joshualitt4421a4c2015-07-13 09:36:41 -0700124bool GrClipMaskManager::installClipEffects(
125 const GrPipelineBuilder& pipelineBuilder,
126 GrPipelineBuilder::AutoRestoreFragmentProcessorState* arfps,
127 const GrReducedClip::ElementList& elements,
128 const SkVector& clipToRTOffset,
129 const SkRect* drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000130 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700131 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000132 boundsInClipSpace = *drawBounds;
133 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
134 }
135
joshualitt4421a4c2015-07-13 09:36:41 -0700136 arfps->set(&pipelineBuilder);
joshualitt5e6ba212015-07-13 07:35:05 -0700137 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
tfarinabf54e492014-10-23 17:47:18 -0700138 GrReducedClip::ElementList::Iter iter(elements);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000139 bool failed = false;
bsalomon49f085d2014-09-05 13:34:00 -0700140 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000141 SkRegion::Op op = iter.get()->getOp();
142 bool invert;
143 bool skip = false;
144 switch (op) {
145 case SkRegion::kReplace_Op:
146 SkASSERT(iter.get() == elements.head());
147 // Fallthrough, handled same as intersect.
148 case SkRegion::kIntersect_Op:
149 invert = false;
bsalomon49f085d2014-09-05 13:34:00 -0700150 if (drawBounds && iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000151 skip = true;
152 }
153 break;
154 case SkRegion::kDifference_Op:
155 invert = true;
156 // We don't currently have a cheap test for whether a rect is fully outside an
157 // element's primitive, so don't attempt to set skip.
158 break;
159 default:
160 failed = true;
161 break;
162 }
163 if (failed) {
164 break;
165 }
166
167 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700168 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800169 if (iter.get()->isAA()) {
vbuzinovdded6962015-06-12 08:59:45 -0700170 if (rt->isUnifiedMultisampled()) {
mtklein217daa72014-07-02 12:55:21 -0700171 // Coverage based AA clips don't place nicely with MSAA.
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000172 failed = true;
173 break;
174 }
joshualittb0a8a372014-09-23 09:50:21 -0700175 edgeType =
176 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000177 } else {
joshualitt5e6ba212015-07-13 07:35:05 -0700178 edgeType = invert ? kInverseFillBW_GrProcessorEdgeType :
179 kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000180 }
joshualittb0a8a372014-09-23 09:50:21 -0700181 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000182 switch (iter.get()->getType()) {
183 case SkClipStack::Element::kPath_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700184 fp.reset(GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
joshualitt642429e2015-02-26 08:47:52 -0800185 &clipToRTOffset));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000186 break;
187 case SkClipStack::Element::kRRect_Type: {
188 SkRRect rrect = iter.get()->getRRect();
189 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
joshualittb0a8a372014-09-23 09:50:21 -0700190 fp.reset(GrRRectEffect::Create(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000191 break;
192 }
193 case SkClipStack::Element::kRect_Type: {
194 SkRect rect = iter.get()->getRect();
195 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
joshualittb0a8a372014-09-23 09:50:21 -0700196 fp.reset(GrConvexPolyEffect::Create(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000197 break;
198 }
199 default:
200 break;
201 }
joshualittb0a8a372014-09-23 09:50:21 -0700202 if (fp) {
bsalomonac856c92015-08-27 06:30:17 -0700203 arfps->addCoverageFragmentProcessor(fp);
mtklein217daa72014-07-02 12:55:21 -0700204 } else {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000205 failed = true;
206 break;
207 }
208 }
mtklein217daa72014-07-02 12:55:21 -0700209 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000210 }
211
212 if (failed) {
halcanary96fcdcc2015-08-27 07:41:13 -0700213 arfps->set(nullptr);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000214 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000215 return !failed;
216}
217
robertphillips@google.comf294b772012-04-27 14:29:26 +0000218////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000219// sort out what kind of clip mask needs to be created: alpha, stencil,
220// scissor, or entirely software
joshualitt5e6ba212015-07-13 07:35:05 -0700221bool GrClipMaskManager::setupClipping(const GrPipelineBuilder& pipelineBuilder,
joshualitt4421a4c2015-07-13 09:36:41 -0700222 GrPipelineBuilder::AutoRestoreFragmentProcessorState* arfps,
egdaniel8dd688b2015-01-22 10:16:09 -0800223 GrPipelineBuilder::AutoRestoreStencil* ars,
bsalomon3e791242014-12-17 13:43:13 -0800224 GrScissorState* scissorState,
joshualitt9853cce2014-11-17 14:22:48 -0800225 const SkRect* devBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000226 fCurrClipMaskType = kNone_ClipMaskType;
joshualitt7a6184f2014-10-29 18:29:27 -0700227 if (kRespectClip_StencilClipMode == fClipMode) {
228 fClipMode = kIgnoreClip_StencilClipMode;
229 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000230
tfarinabf54e492014-10-23 17:47:18 -0700231 GrReducedClip::ElementList elements(16);
brucedawson71d7f7f2015-02-26 13:28:53 -0800232 int32_t genID = 0;
233 GrReducedClip::InitialState initialState = GrReducedClip::kAllIn_InitialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000234 SkIRect clipSpaceIBounds;
brucedawson71d7f7f2015-02-26 13:28:53 -0800235 bool requiresAA = false;
joshualitt5e6ba212015-07-13 07:35:05 -0700236 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000237
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000238 // GrDrawTarget should have filtered this for us
bsalomon49f085d2014-09-05 13:34:00 -0700239 SkASSERT(rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000240
joshualitt44701df2015-02-23 14:44:57 -0800241 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
joshualitt5e6ba212015-07-13 07:35:05 -0700242 const GrClip& clip = pipelineBuilder.clip();
bsalomon96e02a82015-03-06 07:13:01 -0800243 if (clip.isWideOpen(clipSpaceRTIBounds)) {
egdaniel8dd688b2015-01-22 10:16:09 -0800244 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000245 return true;
246 }
247
bsalomon96e02a82015-03-06 07:13:01 -0800248 // The clip mask manager always draws with a single IRect so we special case that logic here
249 // Image filters just use a rect, so we also special case that logic
250 switch (clip.clipType()) {
251 case GrClip::kWideOpen_ClipType:
252 SkFAIL("Should have caught this with clip.isWideOpen()");
253 return true;
bsalomon9ce30e12015-03-06 08:42:34 -0800254 case GrClip::kIRect_ClipType: {
255 SkIRect scissor = clip.irect();
256 if (scissor.intersect(clipSpaceRTIBounds)) {
257 scissorState->set(scissor);
258 this->setPipelineBuilderStencil(pipelineBuilder, ars);
259 return true;
260 }
261 return false;
262 }
bsalomon96e02a82015-03-06 07:13:01 -0800263 case GrClip::kClipStack_ClipType: {
264 clipSpaceRTIBounds.offset(clip.origin());
265 GrReducedClip::ReduceClipStack(*clip.clipStack(),
266 clipSpaceRTIBounds,
267 &elements,
268 &genID,
269 &initialState,
270 &clipSpaceIBounds,
271 &requiresAA);
272 if (elements.isEmpty()) {
273 if (GrReducedClip::kAllIn_InitialState == initialState) {
274 if (clipSpaceIBounds == clipSpaceRTIBounds) {
275 this->setPipelineBuilderStencil(pipelineBuilder, ars);
276 return true;
277 }
278 } else {
279 return false;
280 }
281 }
282 } break;
283 }
284
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000285 // An element count of 4 was chosen because of the common pattern in Blink of:
286 // isect RR
287 // diff RR
288 // isect convex_poly
289 // isect convex_poly
290 // when drawing rounded div borders. This could probably be tuned based on a
291 // configuration's relative costs of switching RTs to generate a mask vs
292 // longer shaders.
293 if (elements.count() <= 4) {
joshualitt44701df2015-02-23 14:44:57 -0800294 SkVector clipToRTOffset = { SkIntToScalar(-clip.origin().fX),
295 SkIntToScalar(-clip.origin().fY) };
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000296 if (elements.isEmpty() ||
joshualitt4421a4c2015-07-13 09:36:41 -0700297 (requiresAA && this->installClipEffects(pipelineBuilder, arfps, elements, clipToRTOffset,
bsalomon55f0b182015-03-05 17:43:09 -0800298 devBounds))) {
mtklein217daa72014-07-02 12:55:21 -0700299 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
joshualitt44701df2015-02-23 14:44:57 -0800300 scissorSpaceIBounds.offset(-clip.origin());
halcanary96fcdcc2015-08-27 07:41:13 -0700301 if (nullptr == devBounds ||
mtklein217daa72014-07-02 12:55:21 -0700302 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
joshualitt77b13072014-10-27 14:51:01 -0700303 scissorState->set(scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000304 }
egdaniel8dd688b2015-01-22 10:16:09 -0800305 this->setPipelineBuilderStencil(pipelineBuilder, ars);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000306 return true;
307 }
308 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000309
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000310 // If MSAA is enabled we can do everything in the stencil buffer.
vbuzinovdded6962015-06-12 08:59:45 -0700311 if (0 == rt->numColorSamples() && requiresAA) {
halcanary96fcdcc2015-08-27 07:41:13 -0700312 GrTexture* result = nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000313
joshualitt8059eb92014-12-29 15:10:07 -0800314 // The top-left of the mask corresponds to the top-left corner of the bounds.
315 SkVector clipToMaskOffset = {
316 SkIntToScalar(-clipSpaceIBounds.fLeft),
317 SkIntToScalar(-clipSpaceIBounds.fTop)
318 };
319
egdaniel8dd688b2015-01-22 10:16:09 -0800320 if (this->useSWOnlyPath(pipelineBuilder, clipToMaskOffset, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000321 // The clip geometry is complex enough that it will be more efficient to create it
322 // entirely in software
323 result = this->createSoftwareClipMask(genID,
324 initialState,
325 elements,
joshualitt8059eb92014-12-29 15:10:07 -0800326 clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000327 clipSpaceIBounds);
328 } else {
329 result = this->createAlphaClipMask(genID,
330 initialState,
331 elements,
joshualitt8059eb92014-12-29 15:10:07 -0800332 clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000333 clipSpaceIBounds);
334 }
335
bsalomon49f085d2014-09-05 13:34:00 -0700336 if (result) {
joshualitt4421a4c2015-07-13 09:36:41 -0700337 arfps->set(&pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000338 // The mask's top left coord should be pinned to the rounded-out top left corner of
339 // clipSpace bounds. We determine the mask's position WRT to the render target here.
340 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
joshualitt44701df2015-02-23 14:44:57 -0800341 rtSpaceMaskBounds.offset(-clip.origin());
joshualitt4421a4c2015-07-13 09:36:41 -0700342 setup_drawstate_aaclip(pipelineBuilder, result, arfps, rtSpaceMaskBounds);
egdaniel8dd688b2015-01-22 10:16:09 -0800343 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000344 return true;
345 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000346 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000347 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000348
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000349 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
350 // be created. In either case, free up the texture in the anti-aliased mask cache.
351 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
joshualittb542bae2015-07-28 09:58:39 -0700352 // clears, GrBufferedDrawTarget playbacks) that hit the stencil buffer path. These may be
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000353 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000354 fAACache.reset();
355
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000356 // use the stencil clip if we can't represent the clip as a rectangle.
joshualitt44701df2015-02-23 14:44:57 -0800357 SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin();
joshualitt9853cce2014-11-17 14:22:48 -0800358 this->createStencilClipMask(rt,
359 genID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000360 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000361 elements,
362 clipSpaceIBounds,
363 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000364
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000365 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
366 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
367 // use both stencil and scissor test to the bounds for the final draw.
368 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
369 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
joshualitt77b13072014-10-27 14:51:01 -0700370 scissorState->set(scissorSpaceIBounds);
egdaniel8dd688b2015-01-22 10:16:09 -0800371 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000372 return true;
373}
374
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000375namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000376////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800377// Set a coverage drawing XPF on the pipelineBuilder for the given op and invertCoverage mode
378void set_coverage_drawing_xpf(SkRegion::Op op, bool invertCoverage,
379 GrPipelineBuilder* pipelineBuilder) {
egdaniel87509242014-12-17 13:37:13 -0800380 SkASSERT(op <= SkRegion::kLastOp);
egdaniel8dd688b2015-01-22 10:16:09 -0800381 pipelineBuilder->setCoverageSetOpXPFactory(op, invertCoverage);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000382}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000383}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000384
385////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800386bool GrClipMaskManager::drawElement(GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800387 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800388 GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000389 const SkClipStack::Element* element,
390 GrPathRenderer* pr) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000391
egdaniel8dd688b2015-01-22 10:16:09 -0800392 pipelineBuilder->setRenderTarget(target->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000393
egdaniel87509242014-12-17 13:37:13 -0800394 // The color we use to draw does not matter since we will always be using a GrCoverageSetOpXP
395 // which ignores color.
396 GrColor color = GrColor_WHITE;
397
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000398 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000399 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000400 case Element::kEmpty_Type:
401 SkDEBUGFAIL("Should never get here with an empty element.");
402 break;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000403 case Element::kRect_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700404 // TODO: Do rects directly to the accumulator using a aa-rect GrProcessor that covers
405 // the entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000406 if (element->isAA()) {
joshualitt8059eb92014-12-29 15:10:07 -0800407 SkRect devRect = element->getRect();
408 viewMatrix.mapRect(&devRect);
robertphillipsea461502015-05-26 11:38:03 -0700409
joshualitt1c735482015-07-13 08:08:25 -0700410 fClipTarget->drawAARect(*pipelineBuilder, color, viewMatrix,
robertphillipsea461502015-05-26 11:38:03 -0700411 element->getRect(), devRect);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000412 } else {
joshualittd2b23e02015-08-21 10:53:34 -0700413 fClipTarget->drawNonAARect(*pipelineBuilder, color, viewMatrix,
414 element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000415 }
416 return true;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000417 default: {
418 SkPath path;
419 element->asPath(&path);
jvanverthb3eb6872014-10-24 07:12:51 -0700420 path.setIsVolatile(true);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000421 if (path.isInverseFillType()) {
422 path.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000423 }
kkinnunen18996512015-04-26 23:18:49 -0700424 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
halcanary96fcdcc2015-08-27 07:41:13 -0700425 if (nullptr == pr) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000426 GrPathRendererChain::DrawType type;
427 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
428 GrPathRendererChain::kColor_DrawType;
egdaniel8dd688b2015-01-22 10:16:09 -0800429 pr = this->getContext()->getPathRenderer(fClipTarget, pipelineBuilder, viewMatrix,
430 path, stroke, false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000431 }
halcanary96fcdcc2015-08-27 07:41:13 -0700432 if (nullptr == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000433 return false;
434 }
bsalomon0aff2fa2015-07-31 06:48:27 -0700435 GrPathRenderer::DrawPathArgs args;
436 args.fTarget = fClipTarget;
437 args.fResourceProvider = this->getContext()->resourceProvider();
438 args.fPipelineBuilder = pipelineBuilder;
439 args.fColor = color;
440 args.fViewMatrix = &viewMatrix;
441 args.fPath = &path;
442 args.fStroke = &stroke;
443 args.fAntiAlias = element->isAA();
444 pr->drawPath(args);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000445 break;
446 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000447 }
448 return true;
449}
450
egdaniel8dd688b2015-01-22 10:16:09 -0800451bool GrClipMaskManager::canStencilAndDrawElement(GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800452 GrTexture* target,
453 GrPathRenderer** pr,
454 const SkClipStack::Element* element) {
egdaniel8dd688b2015-01-22 10:16:09 -0800455 pipelineBuilder->setRenderTarget(target->asRenderTarget());
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000456
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000457 if (Element::kRect_Type == element->getType()) {
458 return true;
459 } else {
460 // We shouldn't get here with an empty clip element.
461 SkASSERT(Element::kEmpty_Type != element->getType());
462 SkPath path;
463 element->asPath(&path);
464 if (path.isInverseFillType()) {
465 path.toggleInverseFillType();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000466 }
kkinnunen18996512015-04-26 23:18:49 -0700467 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000468 GrPathRendererChain::DrawType type = element->isAA() ?
469 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
470 GrPathRendererChain::kStencilAndColor_DrawType;
egdaniel8dd688b2015-01-22 10:16:09 -0800471 *pr = this->getContext()->getPathRenderer(fClipTarget, pipelineBuilder, SkMatrix::I(), path,
joshualitt8059eb92014-12-29 15:10:07 -0800472 stroke, false, type);
bsalomon49f085d2014-09-05 13:34:00 -0700473 return SkToBool(*pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000474 }
475}
476
egdaniel8dd688b2015-01-22 10:16:09 -0800477void GrClipMaskManager::mergeMask(GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800478 GrTexture* dstMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000479 GrTexture* srcMask,
480 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000481 const SkIRect& dstBound,
482 const SkIRect& srcBound) {
egdaniel8dd688b2015-01-22 10:16:09 -0800483 pipelineBuilder->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000484
egdaniel87509242014-12-17 13:37:13 -0800485 // We want to invert the coverage here
egdaniel8dd688b2015-01-22 10:16:09 -0800486 set_coverage_drawing_xpf(op, false, pipelineBuilder);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000487
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000488 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000489 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000490
bsalomonac856c92015-08-27 06:30:17 -0700491 pipelineBuilder->addCoverageFragmentProcessor(
joshualitt5f10b5c2015-07-09 10:24:35 -0700492 GrTextureDomainEffect::Create(pipelineBuilder->getProcessorDataManager(),
493 srcMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000494 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000495 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
496 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000497 GrTextureParams::kNone_FilterMode))->unref();
joshualitt73bb4562015-03-25 07:16:21 -0700498
egdaniel87509242014-12-17 13:37:13 -0800499 // The color passed in here does not matter since the coverageSetOpXP won't read it.
joshualittd2b23e02015-08-21 10:53:34 -0700500 fClipTarget->drawNonAARect(*pipelineBuilder,
501 GrColor_WHITE,
502 SkMatrix::I(),
503 SkRect::Make(dstBound));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000504}
505
bsalomon427cf282014-10-16 13:41:43 -0700506GrTexture* GrClipMaskManager::createTempMask(int width, int height) {
bsalomonf2703d82014-10-28 14:33:06 -0700507 GrSurfaceDesc desc;
bsalomon3f490a02014-12-18 06:20:52 -0800508 desc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000509 desc.fWidth = width;
510 desc.fHeight = height;
bsalomon76228632015-05-29 08:02:10 -0700511 if (this->getContext()->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
bsalomon51d1f7e2014-12-22 08:40:49 -0800512 desc.fConfig = kAlpha_8_GrPixelConfig;
513 } else {
514 desc.fConfig = kRGBA_8888_GrPixelConfig;
515 }
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000516
bsalomoneae62002015-07-31 13:59:30 -0700517 return this->getContext()->textureProvider()->createApproxTexture(desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000518}
519
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000520////////////////////////////////////////////////////////////////////////////////
halcanary96fcdcc2015-08-27 07:41:13 -0700521// Return the texture currently in the cache if it exists. Otherwise, return nullptr
krajcevskiad1dc582014-06-10 15:06:47 -0700522GrTexture* GrClipMaskManager::getCachedMaskTexture(int32_t elementsGenID,
523 const SkIRect& clipSpaceIBounds) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000524 bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000525 if (!cached) {
halcanary96fcdcc2015-08-27 07:41:13 -0700526 return nullptr;
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000527 }
528
krajcevskiad1dc582014-06-10 15:06:47 -0700529 return fAACache.getLastMask();
530}
531
532////////////////////////////////////////////////////////////////////////////////
533// Allocate a texture in the texture cache. This function returns the texture
halcanary96fcdcc2015-08-27 07:41:13 -0700534// allocated (or nullptr on error).
krajcevskiad1dc582014-06-10 15:06:47 -0700535GrTexture* GrClipMaskManager::allocMaskTexture(int32_t elementsGenID,
536 const SkIRect& clipSpaceIBounds,
537 bool willUpload) {
538 // Since we are setting up the cache we should free up the
539 // currently cached mask so it can be reused.
540 fAACache.reset();
541
bsalomonf2703d82014-10-28 14:33:06 -0700542 GrSurfaceDesc desc;
543 desc.fFlags = willUpload ? kNone_GrSurfaceFlags : kRenderTarget_GrSurfaceFlag;
krajcevskiad1dc582014-06-10 15:06:47 -0700544 desc.fWidth = clipSpaceIBounds.width();
545 desc.fHeight = clipSpaceIBounds.height();
546 desc.fConfig = kRGBA_8888_GrPixelConfig;
bsalomon76228632015-05-29 08:02:10 -0700547 if (willUpload ||
548 this->getContext()->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
krajcevskiad1dc582014-06-10 15:06:47 -0700549 // We would always like A8 but it isn't supported on all platforms
550 desc.fConfig = kAlpha_8_GrPixelConfig;
551 }
552
553 fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds);
554 return fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000555}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000556
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000557////////////////////////////////////////////////////////////////////////////////
558// Create a 8-bit clip mask in alpha
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000559GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700560 GrReducedClip::InitialState initialState,
561 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800562 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000563 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000564 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000565
krajcevskiad1dc582014-06-10 15:06:47 -0700566 // First, check for cached texture
567 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -0700568 if (result) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000569 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000570 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000571 }
572
krajcevskiad1dc582014-06-10 15:06:47 -0700573 // There's no texture in the cache. Let's try to allocate it then.
574 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, false);
halcanary96fcdcc2015-08-27 07:41:13 -0700575 if (nullptr == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000576 fAACache.reset();
halcanary96fcdcc2015-08-27 07:41:13 -0700577 return nullptr;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000578 }
579
joshualitt8059eb92014-12-29 15:10:07 -0800580 // Set the matrix so that rendered clip elements are transformed to mask space from clip
581 // space.
582 SkMatrix translate;
583 translate.setTranslate(clipToMaskOffset);
584
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000585 // The texture may be larger than necessary, this rect represents the part of the texture
586 // we populate with a rasterization of the clip.
587 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
588
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000589 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
590 // clear the part that we care about.
joshualitt329bf482014-10-29 12:31:28 -0700591 fClipTarget->clear(&maskSpaceIBounds,
592 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
593 true,
594 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000595
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000596 // When we use the stencil in the below loop it is important to have this clip installed.
597 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
598 // pass must not set values outside of this bounds or stencil values outside the rect won't be
599 // cleared.
joshualitt44701df2015-02-23 14:44:57 -0800600 GrClip clip(maskSpaceIBounds);
bsalomon427cf282014-10-16 13:41:43 -0700601 SkAutoTUnref<GrTexture> temp;
joshualitt9853cce2014-11-17 14:22:48 -0800602
robertphillips@google.comf294b772012-04-27 14:29:26 +0000603 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700604 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000605 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000606 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000607 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000608 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
egdaniel8dd688b2015-01-22 10:16:09 -0800609 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800610
joshualitt44701df2015-02-23 14:44:57 -0800611 pipelineBuilder.setClip(clip);
halcanary96fcdcc2015-08-27 07:41:13 -0700612 GrPathRenderer* pr = nullptr;
egdaniel8dd688b2015-01-22 10:16:09 -0800613 bool useTemp = !this->canStencilAndDrawElement(&pipelineBuilder, result, &pr, element);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000614 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000615 // 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 +0000616 // mask buffer can be substantially larger than the actually clip stack element. We
617 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000618 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000619 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000620
621 if (useTemp) {
622 if (invert) {
623 maskSpaceElementIBounds = maskSpaceIBounds;
624 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000625 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000626 elementBounds.offset(clipToMaskOffset);
627 elementBounds.roundOut(&maskSpaceElementIBounds);
628 }
629
bsalomon427cf282014-10-16 13:41:43 -0700630 if (!temp) {
631 temp.reset(this->createTempMask(maskSpaceIBounds.fRight,
632 maskSpaceIBounds.fBottom));
633 if (!temp) {
634 fAACache.reset();
halcanary96fcdcc2015-08-27 07:41:13 -0700635 return nullptr;
bsalomon427cf282014-10-16 13:41:43 -0700636 }
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000637 }
bsalomon427cf282014-10-16 13:41:43 -0700638 dst = temp;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000639 // clear the temp target and set blend to replace
joshualitt329bf482014-10-29 12:31:28 -0700640 fClipTarget->clear(&maskSpaceElementIBounds,
joshualitt9853cce2014-11-17 14:22:48 -0800641 invert ? 0xffffffff : 0x00000000,
642 true,
643 dst->asRenderTarget());
egdaniel8dd688b2015-01-22 10:16:09 -0800644 set_coverage_drawing_xpf(SkRegion::kReplace_Op, invert, &pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000645 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000646 // draw directly into the result with the stencil set to make the pixels affected
647 // by the clip shape be non-zero.
648 dst = result;
649 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
650 kReplace_StencilOp,
651 kReplace_StencilOp,
652 kAlways_StencilFunc,
653 0xffff,
654 0xffff,
655 0xffff);
egdaniel8dd688b2015-01-22 10:16:09 -0800656 pipelineBuilder.setStencil(kStencilInElement);
657 set_coverage_drawing_xpf(op, invert, &pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000658 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000659
egdaniel8dd688b2015-01-22 10:16:09 -0800660 if (!this->drawElement(&pipelineBuilder, translate, dst, element, pr)) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000661 fAACache.reset();
halcanary96fcdcc2015-08-27 07:41:13 -0700662 return nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000663 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000664
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000665 if (useTemp) {
egdaniel8dd688b2015-01-22 10:16:09 -0800666 GrPipelineBuilder backgroundPipelineBuilder;
egdaniel8dd688b2015-01-22 10:16:09 -0800667 backgroundPipelineBuilder.setRenderTarget(result->asRenderTarget());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800668
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000669 // Now draw into the accumulator using the real operation and the temp buffer as a
670 // texture
egdaniel8dd688b2015-01-22 10:16:09 -0800671 this->mergeMask(&backgroundPipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800672 result,
bsalomon427cf282014-10-16 13:41:43 -0700673 temp,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000674 op,
675 maskSpaceIBounds,
676 maskSpaceElementIBounds);
677 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800678 GrPipelineBuilder backgroundPipelineBuilder;
egdaniel8dd688b2015-01-22 10:16:09 -0800679 backgroundPipelineBuilder.setRenderTarget(result->asRenderTarget());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800680
egdaniel8dd688b2015-01-22 10:16:09 -0800681 set_coverage_drawing_xpf(op, !invert, &backgroundPipelineBuilder);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000682 // Draw to the exterior pixels (those with a zero stencil value).
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000683 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
684 kZero_StencilOp,
685 kZero_StencilOp,
686 kEqual_StencilFunc,
687 0xffff,
688 0x0000,
689 0xffff);
egdaniel8dd688b2015-01-22 10:16:09 -0800690 backgroundPipelineBuilder.setStencil(kDrawOutsideElement);
joshualitt73bb4562015-03-25 07:16:21 -0700691
egdaniel87509242014-12-17 13:37:13 -0800692 // The color passed in here does not matter since the coverageSetOpXP won't read it.
joshualittd2b23e02015-08-21 10:53:34 -0700693 fClipTarget->drawNonAARect(backgroundPipelineBuilder, GrColor_WHITE, translate,
694 clipSpaceIBounds);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000695 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000696 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800697 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800698
robertphillips@google.come79f3202014-02-11 16:30:21 +0000699 // all the remaining ops can just be directly draw into the accumulation buffer
egdaniel8dd688b2015-01-22 10:16:09 -0800700 set_coverage_drawing_xpf(op, false, &pipelineBuilder);
egdaniel87509242014-12-17 13:37:13 -0800701 // The color passed in here does not matter since the coverageSetOpXP won't read it.
egdaniel8dd688b2015-01-22 10:16:09 -0800702 this->drawElement(&pipelineBuilder, translate, result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000703 }
704 }
705
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000706 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000707 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000708}
709
710////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000711// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000712// (as opposed to canvas) coordinates
joshualitt9853cce2014-11-17 14:22:48 -0800713bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt,
714 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700715 GrReducedClip::InitialState initialState,
716 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000717 const SkIRect& clipSpaceIBounds,
718 const SkIPoint& clipSpaceToStencilOffset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000719 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon49f085d2014-09-05 13:34:00 -0700720 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000721
egdaniel8dc7c3a2015-04-16 11:22:42 -0700722 GrStencilAttachment* stencilAttachment = rt->renderTargetPriv().attachStencilAttachment();
halcanary96fcdcc2015-08-27 07:41:13 -0700723 if (nullptr == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000724 return false;
725 }
726
egdaniel8dc7c3a2015-04-16 11:22:42 -0700727 if (stencilAttachment->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
728 stencilAttachment->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000729 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
730 SkVector translate = {
731 SkIntToScalar(clipSpaceToStencilOffset.fX),
732 SkIntToScalar(clipSpaceToStencilOffset.fY)
733 };
joshualitt8059eb92014-12-29 15:10:07 -0800734 SkMatrix viewMatrix;
735 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000736
bsalomon@google.com9f131742012-12-13 20:43:56 +0000737 // We set the current clip to the bounds so that our recursive draws are scissored to them.
738 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
739 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
joshualitt44701df2015-02-23 14:44:57 -0800740 GrClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000741
egdaniel8dc7c3a2015-04-16 11:22:42 -0700742 int clipBit = stencilAttachment->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000743 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000744 clipBit = (1 << (clipBit-1));
745
joshualitt329bf482014-10-29 12:31:28 -0700746 fClipTarget->clearStencilClip(stencilSpaceIBounds,
747 GrReducedClip::kAllIn_InitialState == initialState,
748 rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000749
750 // walk through each clip element and perform its set op
751 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700752 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000753 const Element* element = iter.get();
joshualitt9853cce2014-11-17 14:22:48 -0800754
egdaniel8dd688b2015-01-22 10:16:09 -0800755 GrPipelineBuilder pipelineBuilder;
joshualitt44701df2015-02-23 14:44:57 -0800756 pipelineBuilder.setClip(clip);
egdaniel8dd688b2015-01-22 10:16:09 -0800757 pipelineBuilder.setRenderTarget(rt);
egdaniel080e6732014-12-22 07:35:52 -0800758
egdaniel8dd688b2015-01-22 10:16:09 -0800759 pipelineBuilder.setDisableColorXPFactory();
joshualitt9853cce2014-11-17 14:22:48 -0800760
761 // if the target is MSAA then we want MSAA enabled when the clip is soft
vbuzinovdded6962015-06-12 08:59:45 -0700762 if (rt->isUnifiedMultisampled()) {
bsalomond79c5492015-04-27 10:07:04 -0700763 pipelineBuilder.setState(GrPipelineBuilder::kHWAntialias_Flag, element->isAA());
joshualitt9853cce2014-11-17 14:22:48 -0800764 }
765
tomhudson@google.com8afae612012-08-14 15:03:35 +0000766 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000767 // enabled at bottom of loop
joshualitt7a6184f2014-10-29 18:29:27 -0700768 fClipMode = kIgnoreClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000769
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000770 // This will be used to determine whether the clip shape can be rendered into the
771 // stencil with arbitrary stencil settings.
772 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000773
kkinnunen18996512015-04-26 23:18:49 -0700774 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000775 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000776
halcanary96fcdcc2015-08-27 07:41:13 -0700777 GrPathRenderer* pr = nullptr;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000778 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000779 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000780 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000781 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000782 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000783 element->asPath(&clipPath);
784 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000785 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000786 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000787 }
joshualitt9853cce2014-11-17 14:22:48 -0800788 pr = this->getContext()->getPathRenderer(fClipTarget,
egdaniel8dd688b2015-01-22 10:16:09 -0800789 &pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800790 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800791 clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000792 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000793 false,
794 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000795 &stencilSupport);
halcanary96fcdcc2015-08-27 07:41:13 -0700796 if (nullptr == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000797 return false;
798 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000799 }
800
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000801 int passes;
802 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
803
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000804 bool canRenderDirectToStencil =
805 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000806 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000807 // fill rule, and set operation can
808 // we render the element directly to
809 // stencil bit used for clipping.
810 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
811 canRenderDirectToStencil,
812 clipBit,
813 fillInverted,
814 &passes,
815 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000816
817 // draw the element to the client stencil bits if necessary
818 if (!canDrawDirectToClip) {
819 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000820 kIncClamp_StencilOp,
821 kIncClamp_StencilOp,
822 kAlways_StencilFunc,
823 0xffff,
824 0x0000,
825 0xffff);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000826 if (Element::kRect_Type == element->getType()) {
egdaniel8dd688b2015-01-22 10:16:09 -0800827 *pipelineBuilder.stencil() = gDrawToStencil;
joshualitt73bb4562015-03-25 07:16:21 -0700828
829 // We need this AGP until everything is in GrBatch
joshualittd2b23e02015-08-21 10:53:34 -0700830 fClipTarget->drawNonAARect(pipelineBuilder,
831 GrColor_WHITE,
832 viewMatrix,
833 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000834 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000835 if (!clipPath.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000836 if (canRenderDirectToStencil) {
egdaniel8dd688b2015-01-22 10:16:09 -0800837 *pipelineBuilder.stencil() = gDrawToStencil;
bsalomon0aff2fa2015-07-31 06:48:27 -0700838
839 GrPathRenderer::DrawPathArgs args;
840 args.fTarget = fClipTarget;
841 args.fResourceProvider = this->getContext()->resourceProvider();
842 args.fPipelineBuilder = &pipelineBuilder;
843 args.fColor = GrColor_WHITE;
844 args.fViewMatrix = &viewMatrix;
845 args.fPath = &clipPath;
846 args.fStroke = &stroke;
847 args.fAntiAlias = false;
848 pr->drawPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000849 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700850 GrPathRenderer::StencilPathArgs args;
851 args.fTarget = fClipTarget;
852 args.fResourceProvider = this->getContext()->resourceProvider();
853 args.fPipelineBuilder = &pipelineBuilder;
854 args.fViewMatrix = &viewMatrix;
855 args.fPath = &clipPath;
856 args.fStroke = &stroke;
857 pr->stencilPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000858 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000859 }
860 }
861 }
862
863 // now we modify the clip bit by rendering either the clip
864 // element directly or a bounding rect of the entire clip.
joshualitt7a6184f2014-10-29 18:29:27 -0700865 fClipMode = kModifyClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000866 for (int p = 0; p < passes; ++p) {
joshualitt4f6dc522015-07-09 12:17:44 -0700867 *pipelineBuilder.stencil() = stencilSettings[p];
joshualitt9853cce2014-11-17 14:22:48 -0800868
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000869 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000870 if (Element::kRect_Type == element->getType()) {
joshualitt73bb4562015-03-25 07:16:21 -0700871 // We need this AGP until everything is in GrBatch
joshualittd2b23e02015-08-21 10:53:34 -0700872 fClipTarget->drawNonAARect(pipelineBuilder,
873 GrColor_WHITE,
874 viewMatrix,
875 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000876 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700877 GrPathRenderer::DrawPathArgs args;
878 args.fTarget = fClipTarget;
879 args.fResourceProvider = this->getContext()->resourceProvider();
880 args.fPipelineBuilder = &pipelineBuilder;
881 args.fColor = GrColor_WHITE;
882 args.fViewMatrix = &viewMatrix;
883 args.fPath = &clipPath;
884 args.fStroke = &stroke;
885 args.fAntiAlias = false;
886 pr->drawPath(args);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000887 }
888 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000889 // The view matrix is setup to do clip space -> stencil space translation, so
890 // draw rect in clip space.
joshualittd2b23e02015-08-21 10:53:34 -0700891 fClipTarget->drawNonAARect(pipelineBuilder,
892 GrColor_WHITE,
893 viewMatrix,
894 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000895 }
896 }
897 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000898 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000899 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000900 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000901 fCurrClipMaskType = kStencil_ClipMaskType;
joshualitt7a6184f2014-10-29 18:29:27 -0700902 fClipMode = kRespectClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000903 return true;
904}
905
bsalomon@google.com411dad02012-06-05 20:24:20 +0000906// mapping of clip-respecting stencil funcs to normal stencil funcs
907// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000908static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000909 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
910 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
911 // In the Clip Funcs
912 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
913 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
914 kLess_StencilFunc, // kLessIfInClip_StencilFunc
915 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
916 // Special in the clip func that forces user's ref to be 0.
917 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
918 // make ref 0 and do normal nequal.
919 },
920 {// Stencil-Clipping is ENABLED
921 // In the Clip Funcs
922 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
923 // eq stencil clip bit, mask
924 // out user bits.
925
926 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
927 // add stencil bit to mask and ref
928
929 kLess_StencilFunc, // kLessIfInClip_StencilFunc
930 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
931 // for both of these we can add
932 // the clip bit to the mask and
933 // ref and compare as normal
934 // Special in the clip func that forces user's ref to be 0.
935 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
936 // make ref have only the clip bit set
937 // and make comparison be less
938 // 10..0 < 1..user_bits..
939 }
940};
941
bsalomon@google.coma3201942012-06-21 19:58:20 +0000942namespace {
943// Sets the settings to clip against the stencil buffer clip while ignoring the
944// client bits.
945const GrStencilSettings& basic_apply_stencil_clip_settings() {
946 // stencil settings to use when clip is in stencil
947 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
948 kKeep_StencilOp,
949 kKeep_StencilOp,
950 kAlwaysIfInClip_StencilFunc,
951 0x0000,
952 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000953 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000954 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
955}
956}
957
joshualitt5e6ba212015-07-13 07:35:05 -0700958void GrClipMaskManager::setPipelineBuilderStencil(const GrPipelineBuilder& pipelineBuilder,
egdaniel8dd688b2015-01-22 10:16:09 -0800959 GrPipelineBuilder::AutoRestoreStencil* ars) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000960 // We make two copies of the StencilSettings here (except in the early
961 // exit scenario. One copy from draw state to the stack var. Then another
962 // from the stack var to the gpu. We could make this class hold a ptr to
963 // GrGpu's fStencilSettings and eliminate the stack copy here.
964
bsalomon@google.coma3201942012-06-21 19:58:20 +0000965 // use stencil for clipping if clipping is enabled and the clip
966 // has been written into the stencil.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000967 GrStencilSettings settings;
joshualitt9853cce2014-11-17 14:22:48 -0800968
bsalomon@google.coma3201942012-06-21 19:58:20 +0000969 // The GrGpu client may not be using the stencil buffer but we may need to
970 // enable it in order to respect a stencil clip.
joshualitt5e6ba212015-07-13 07:35:05 -0700971 if (pipelineBuilder.getStencil().isDisabled()) {
joshualitt7a6184f2014-10-29 18:29:27 -0700972 if (GrClipMaskManager::kRespectClip_StencilClipMode == fClipMode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000973 settings = basic_apply_stencil_clip_settings();
974 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000975 return;
976 }
977 } else {
joshualitt5e6ba212015-07-13 07:35:05 -0700978 settings = pipelineBuilder.getStencil();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000979 }
980
bsalomon@google.coma3201942012-06-21 19:58:20 +0000981 int stencilBits = 0;
joshualitt5e6ba212015-07-13 07:35:05 -0700982 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700983 GrStencilAttachment* stencilAttachment = rt->renderTargetPriv().attachStencilAttachment();
984 if (stencilAttachment) {
985 stencilBits = stencilAttachment->bits();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000986 }
987
joshualitt329bf482014-10-29 12:31:28 -0700988 SkASSERT(fClipTarget->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
989 SkASSERT(fClipTarget->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
joshualitt7a6184f2014-10-29 18:29:27 -0700990 this->adjustStencilParams(&settings, fClipMode, stencilBits);
joshualitt5e6ba212015-07-13 07:35:05 -0700991 ars->set(&pipelineBuilder);
992 ars->setStencil(settings);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000993}
994
995void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
996 StencilClipMode mode,
997 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000998 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000999
1000 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001001 // We assume that this clip manager itself is drawing to the GrGpu and
1002 // has already setup the correct values.
1003 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001004 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001005
bsalomon@google.com411dad02012-06-05 20:24:20 +00001006 unsigned int clipBit = (1 << (stencilBitCnt - 1));
1007 unsigned int userBits = clipBit - 1;
1008
bsalomon@google.coma3201942012-06-21 19:58:20 +00001009 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
joshualitt329bf482014-10-29 12:31:28 -07001010 bool twoSided = fClipTarget->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +00001011
bsalomon@google.coma3201942012-06-21 19:58:20 +00001012 bool finished = false;
1013 while (!finished) {
1014 GrStencilFunc func = settings->func(face);
1015 uint16_t writeMask = settings->writeMask(face);
1016 uint16_t funcMask = settings->funcMask(face);
1017 uint16_t funcRef = settings->funcRef(face);
1018
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001019 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001020
1021 writeMask &= userBits;
1022
1023 if (func >= kBasicStencilFuncCount) {
1024 int respectClip = kRespectClip_StencilClipMode == mode;
1025 if (respectClip) {
1026 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001027 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001028 switch (func) {
1029 case kAlwaysIfInClip_StencilFunc:
1030 funcMask = clipBit;
1031 funcRef = clipBit;
1032 break;
1033 case kEqualIfInClip_StencilFunc:
1034 case kLessIfInClip_StencilFunc:
1035 case kLEqualIfInClip_StencilFunc:
1036 funcMask = (funcMask & userBits) | clipBit;
1037 funcRef = (funcRef & userBits) | clipBit;
1038 break;
1039 case kNonZeroIfInClip_StencilFunc:
1040 funcMask = (funcMask & userBits) | clipBit;
1041 funcRef = clipBit;
1042 break;
1043 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001044 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +00001045 }
1046 } else {
1047 funcMask &= userBits;
1048 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001049 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001050 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001051 gSpecialToBasicStencilFunc[respectClip];
1052 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001053 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001054 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001055 funcMask &= userBits;
1056 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001057 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001058
1059 settings->setFunc(face, func);
1060 settings->setWriteMask(face, writeMask);
1061 settings->setFuncMask(face, funcMask);
1062 settings->setFuncRef(face, funcRef);
1063
1064 if (GrStencilSettings::kFront_Face == face) {
1065 face = GrStencilSettings::kBack_Face;
1066 finished = !twoSided;
1067 } else {
1068 finished = true;
1069 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001070 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001071 if (!twoSided) {
1072 settings->copyFrontSettingsToBack();
1073 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001074}
1075
1076////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +00001077GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001078 GrReducedClip::InitialState initialState,
1079 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -08001080 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001081 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001082 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001083
krajcevskiad1dc582014-06-10 15:06:47 -07001084 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -07001085 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001086 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001087 }
1088
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001089 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1090 // the top left corner of the resulting rect to the top left of the texture.
1091 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1092
robertphillips@google.com2c756812012-05-22 20:28:23 +00001093 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001094
joshualitt8059eb92014-12-29 15:10:07 -08001095 // Set the matrix so that rendered clip elements are transformed to mask space from clip
1096 // space.
1097 SkMatrix translate;
1098 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -08001099
joshualitt8059eb92014-12-29 15:10:07 -08001100 helper.init(maskSpaceIBounds, &translate, false);
tfarinabf54e492014-10-23 17:47:18 -07001101 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001102 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001103
tfarinabf54e492014-10-23 17:47:18 -07001104 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001105 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001106 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001107
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001108 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1109 // Intersect and reverse difference require modifying pixels outside of the geometry
1110 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1111 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1112 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001113 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001114 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001115 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001116 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001117 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001118 SkPath clipPath;
1119 element->asPath(&clipPath);
1120 clipPath.toggleInverseFillType();
1121 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001122 continue;
1123 }
1124
1125 // The other ops (union, xor, diff) only affect pixels inside
1126 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001127 if (Element::kRect_Type == element->getType()) {
1128 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1129 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001130 SkPath path;
1131 element->asPath(&path);
1132 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001133 }
1134 }
1135
krajcevskiad1dc582014-06-10 15:06:47 -07001136 // Allocate clip mask texture
1137 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, true);
halcanary96fcdcc2015-08-27 07:41:13 -07001138 if (nullptr == result) {
krajcevskiad1dc582014-06-10 15:06:47 -07001139 fAACache.reset();
halcanary96fcdcc2015-08-27 07:41:13 -07001140 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -07001141 }
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001142 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001143
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001144 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001145 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001146}
1147
robertphillips@google.comf294b772012-04-27 14:29:26 +00001148////////////////////////////////////////////////////////////////////////////////
bsalomonc8dc1f72014-08-21 13:02:13 -07001149void GrClipMaskManager::purgeResources() {
1150 fAACache.purgeResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001151}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001152
egdaniel8dc7c3a2015-04-16 11:22:42 -07001153void GrClipMaskManager::adjustPathStencilParams(const GrStencilAttachment* stencilAttachment,
joshualitt9853cce2014-11-17 14:22:48 -08001154 GrStencilSettings* settings) {
egdaniel8dc7c3a2015-04-16 11:22:42 -07001155 if (stencilAttachment) {
1156 int stencilBits = stencilAttachment->bits();
joshualitt7a6184f2014-10-29 18:29:27 -07001157 this->adjustStencilParams(settings, fClipMode, stencilBits);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001158 }
1159}