blob: e0a85d184d9eff88225100a18e209c12fa291cf3 [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"
bsalomoneb1cb5c2015-05-22 08:01:09 -07009#include "GrCaps.h"
robertphillipsea461502015-05-26 11:38:03 -070010#include "GrDrawContext.h"
11#include "GrDrawTarget.h"
bsalomon473addf2015-10-02 07:49:05 -070012#include "GrGpuResourcePriv.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000013#include "GrPaint.h"
14#include "GrPathRenderer.h"
15#include "GrRenderTarget.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080016#include "GrRenderTargetPriv.h"
bsalomon473addf2015-10-02 07:49:05 -070017#include "GrResourceProvider.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////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +000030// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000031// stage matrix this also alters the vertex layout
bsalomon0ba8c242015-10-07 09:20:28 -070032static const GrFragmentProcessor* create_fp_for_mask(GrTexture* result, const SkIRect &devBound) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +000033 SkMatrix mat;
bsalomon309d4d52014-12-18 10:17:44 -080034 // We use device coords to compute the texture coordinates. We set our matrix to be a
35 // translation to the devBound, and then a scaling matrix to normalized coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000036 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000037 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000038 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000039
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000040 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon0ba8c242015-10-07 09:20:28 -070041 return GrTextureDomainEffect::Create(result,
42 mat,
43 GrTextureDomain::MakeTexelDomain(result, domainTexels),
44 GrTextureDomain::kDecal_Mode,
45 GrTextureParams::kNone_FilterMode,
46 kDevice_GrCoordSet);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000047}
48
bsalomon0ba8c242015-10-07 09:20:28 -070049static bool path_needs_SW_renderer(GrContext* context,
50 const GrDrawTarget* gpu,
51 const GrPipelineBuilder& pipelineBuilder,
52 const SkMatrix& viewMatrix,
53 const SkPath& origPath,
54 const GrStrokeInfo& stroke,
55 bool doAA) {
robertphillips@google.come79f3202014-02-11 16:30:21 +000056 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
57 SkTCopyOnFirstWrite<SkPath> path(origPath);
58 if (path->isInverseFillType()) {
59 path.writable()->toggleInverseFillType();
60 }
61 // last (false) parameter disallows use of the SW path renderer
bsalomon@google.com45a15f52012-12-10 19:10:17 +000062 GrPathRendererChain::DrawType type = doAA ?
63 GrPathRendererChain::kColorAntiAlias_DrawType :
64 GrPathRendererChain::kColor_DrawType;
65
halcanary96fcdcc2015-08-27 07:41:13 -070066 return nullptr == context->getPathRenderer(gpu, &pipelineBuilder, viewMatrix, *path, stroke,
egdaniel8dd688b2015-01-22 10:16:09 -080067 false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +000068}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000069
bsalomonb3b9aec2015-09-10 11:16:35 -070070GrClipMaskManager::GrClipMaskManager(GrDrawTarget* drawTarget)
bsalomonc988d2c2015-10-07 09:30:05 -070071 : fDrawTarget(drawTarget)
bsalomonedd77a12015-05-29 09:45:57 -070072 , fClipMode(kIgnoreClip_StencilClipMode) {
73}
74
bsalomonb3b9aec2015-09-10 11:16:35 -070075GrContext* GrClipMaskManager::getContext() { return fDrawTarget->cmmAccess().context(); }
bsalomonedd77a12015-05-29 09:45:57 -070076
robertphillips@google.comfa662942012-05-17 12:20:22 +000077/*
78 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
79 * will be used on any element. If so, it returns true to indicate that the
80 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
81 */
joshualitt5e6ba212015-07-13 07:35:05 -070082bool GrClipMaskManager::useSWOnlyPath(const GrPipelineBuilder& pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -080083 const SkVector& clipToMaskOffset,
joshualitt9853cce2014-11-17 14:22:48 -080084 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000085 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000086 // a clip gets complex enough it can just be done in SW regardless
87 // of whether it would invoke the GrSoftwarePathRenderer.
kkinnunen18996512015-04-26 23:18:49 -070088 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000089
joshualitt8059eb92014-12-29 15:10:07 -080090 // Set the matrix so that rendered clip elements are transformed to mask space from clip
91 // space.
92 SkMatrix translate;
93 translate.setTranslate(clipToMaskOffset);
94
tfarinabf54e492014-10-23 17:47:18 -070095 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000096 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000097 // rects can always be drawn directly w/o using the software path
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000098 // Skip rrects once we're drawing them directly.
99 if (Element::kRect_Type != element->getType()) {
100 SkPath path;
101 element->asPath(&path);
bsalomonb3b9aec2015-09-10 11:16:35 -0700102 if (path_needs_SW_renderer(this->getContext(), fDrawTarget, pipelineBuilder, translate,
joshualitt8059eb92014-12-29 15:10:07 -0800103 path, stroke, element->isAA())) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000104 return true;
105 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000106 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000107 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000108 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000109}
110
bsalomon0b5b6b22015-10-14 08:31:34 -0700111bool GrClipMaskManager::getAnalyticClipProcessor(const GrReducedClip::ElementList& elements,
bsalomona912dde2015-10-14 15:01:50 -0700112 bool abortIfAA,
bsalomon0b5b6b22015-10-14 08:31:34 -0700113 SkVector& clipToRTOffset,
114 const SkRect* drawBounds,
115 const GrFragmentProcessor** resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000116 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700117 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000118 boundsInClipSpace = *drawBounds;
119 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
120 }
bsalomon0ba8c242015-10-07 09:20:28 -0700121 SkASSERT(elements.count() <= kMaxAnalyticElements);
122 const GrFragmentProcessor* fps[kMaxAnalyticElements];
123 for (int i = 0; i < kMaxAnalyticElements; ++i) {
124 fps[i] = nullptr;
125 }
126 int fpCnt = 0;
tfarinabf54e492014-10-23 17:47:18 -0700127 GrReducedClip::ElementList::Iter iter(elements);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000128 bool failed = false;
bsalomon49f085d2014-09-05 13:34:00 -0700129 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000130 SkRegion::Op op = iter.get()->getOp();
131 bool invert;
132 bool skip = false;
133 switch (op) {
134 case SkRegion::kReplace_Op:
135 SkASSERT(iter.get() == elements.head());
136 // Fallthrough, handled same as intersect.
137 case SkRegion::kIntersect_Op:
138 invert = false;
bsalomon49f085d2014-09-05 13:34:00 -0700139 if (drawBounds && iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000140 skip = true;
141 }
142 break;
143 case SkRegion::kDifference_Op:
144 invert = true;
145 // We don't currently have a cheap test for whether a rect is fully outside an
146 // element's primitive, so don't attempt to set skip.
147 break;
148 default:
149 failed = true;
150 break;
151 }
152 if (failed) {
153 break;
154 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000155 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700156 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800157 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700158 if (abortIfAA) {
159 failed = true;
160 break;
161 }
joshualittb0a8a372014-09-23 09:50:21 -0700162 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700163 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000164 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700165 edgeType =
166 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000167 }
bsalomona912dde2015-10-14 15:01:50 -0700168
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000169 switch (iter.get()->getType()) {
170 case SkClipStack::Element::kPath_Type:
bsalomon0ba8c242015-10-07 09:20:28 -0700171 fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
172 &clipToRTOffset);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000173 break;
174 case SkClipStack::Element::kRRect_Type: {
175 SkRRect rrect = iter.get()->getRRect();
176 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700177 fps[fpCnt] = GrRRectEffect::Create(edgeType, rrect);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000178 break;
179 }
180 case SkClipStack::Element::kRect_Type: {
181 SkRect rect = iter.get()->getRect();
182 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700183 fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, rect);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000184 break;
185 }
186 default:
187 break;
188 }
bsalomon0ba8c242015-10-07 09:20:28 -0700189 if (!fps[fpCnt]) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000190 failed = true;
191 break;
192 }
bsalomon0ba8c242015-10-07 09:20:28 -0700193 fpCnt++;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000194 }
mtklein217daa72014-07-02 12:55:21 -0700195 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000196 }
197
bsalomon0b5b6b22015-10-14 08:31:34 -0700198 *resultFP = nullptr;
199 if (!failed && fpCnt) {
200 *resultFP = GrFragmentProcessor::RunInSeries(fps, fpCnt);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000201 }
bsalomon0ba8c242015-10-07 09:20:28 -0700202 for (int i = 0; i < fpCnt; ++i) {
203 fps[i]->unref();
204 }
bsalomon0b5b6b22015-10-14 08:31:34 -0700205 return !failed;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000206}
207
robertphillips@google.comf294b772012-04-27 14:29:26 +0000208////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000209// sort out what kind of clip mask needs to be created: alpha, stencil,
210// scissor, or entirely software
joshualitt5e6ba212015-07-13 07:35:05 -0700211bool GrClipMaskManager::setupClipping(const GrPipelineBuilder& pipelineBuilder,
egdaniel8dd688b2015-01-22 10:16:09 -0800212 GrPipelineBuilder::AutoRestoreStencil* ars,
bsalomon3e791242014-12-17 13:43:13 -0800213 GrScissorState* scissorState,
bsalomon0ba8c242015-10-07 09:20:28 -0700214 const SkRect* devBounds,
215 GrAppliedClip* out) {
joshualitt7a6184f2014-10-29 18:29:27 -0700216 if (kRespectClip_StencilClipMode == fClipMode) {
217 fClipMode = kIgnoreClip_StencilClipMode;
218 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000219
tfarinabf54e492014-10-23 17:47:18 -0700220 GrReducedClip::ElementList elements(16);
brucedawson71d7f7f2015-02-26 13:28:53 -0800221 int32_t genID = 0;
222 GrReducedClip::InitialState initialState = GrReducedClip::kAllIn_InitialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000223 SkIRect clipSpaceIBounds;
brucedawson71d7f7f2015-02-26 13:28:53 -0800224 bool requiresAA = false;
joshualitt5e6ba212015-07-13 07:35:05 -0700225 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000226
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000227 // GrDrawTarget should have filtered this for us
bsalomon49f085d2014-09-05 13:34:00 -0700228 SkASSERT(rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000229
joshualitt44701df2015-02-23 14:44:57 -0800230 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
joshualitt5e6ba212015-07-13 07:35:05 -0700231 const GrClip& clip = pipelineBuilder.clip();
bsalomon96e02a82015-03-06 07:13:01 -0800232 if (clip.isWideOpen(clipSpaceRTIBounds)) {
egdaniel8dd688b2015-01-22 10:16:09 -0800233 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000234 return true;
235 }
236
bsalomon96e02a82015-03-06 07:13:01 -0800237 // The clip mask manager always draws with a single IRect so we special case that logic here
238 // Image filters just use a rect, so we also special case that logic
239 switch (clip.clipType()) {
240 case GrClip::kWideOpen_ClipType:
241 SkFAIL("Should have caught this with clip.isWideOpen()");
242 return true;
bsalomon9ce30e12015-03-06 08:42:34 -0800243 case GrClip::kIRect_ClipType: {
244 SkIRect scissor = clip.irect();
245 if (scissor.intersect(clipSpaceRTIBounds)) {
246 scissorState->set(scissor);
247 this->setPipelineBuilderStencil(pipelineBuilder, ars);
248 return true;
249 }
250 return false;
251 }
bsalomon96e02a82015-03-06 07:13:01 -0800252 case GrClip::kClipStack_ClipType: {
253 clipSpaceRTIBounds.offset(clip.origin());
254 GrReducedClip::ReduceClipStack(*clip.clipStack(),
255 clipSpaceRTIBounds,
256 &elements,
257 &genID,
258 &initialState,
259 &clipSpaceIBounds,
260 &requiresAA);
261 if (elements.isEmpty()) {
262 if (GrReducedClip::kAllIn_InitialState == initialState) {
263 if (clipSpaceIBounds == clipSpaceRTIBounds) {
264 this->setPipelineBuilderStencil(pipelineBuilder, ars);
265 return true;
266 }
267 } else {
268 return false;
269 }
270 }
271 } break;
272 }
273
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000274 // An element count of 4 was chosen because of the common pattern in Blink of:
275 // isect RR
276 // diff RR
277 // isect convex_poly
278 // isect convex_poly
279 // when drawing rounded div borders. This could probably be tuned based on a
280 // configuration's relative costs of switching RTs to generate a mask vs
281 // longer shaders.
bsalomon0ba8c242015-10-07 09:20:28 -0700282 if (elements.count() <= kMaxAnalyticElements) {
joshualitt44701df2015-02-23 14:44:57 -0800283 SkVector clipToRTOffset = { SkIntToScalar(-clip.origin().fX),
284 SkIntToScalar(-clip.origin().fY) };
bsalomon0ba8c242015-10-07 09:20:28 -0700285 // When there are multiple color samples we want to do per-sample clipping, not compute
286 // a fractional pixel coverage.
287 bool disallowAnalyticAA = pipelineBuilder.getRenderTarget()->isUnifiedMultisampled();
288 const GrFragmentProcessor* clipFP = nullptr;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000289 if (elements.isEmpty() ||
bsalomona912dde2015-10-14 15:01:50 -0700290 (requiresAA &&
291 this->getAnalyticClipProcessor(elements, disallowAnalyticAA, clipToRTOffset, devBounds,
292 &clipFP))) {
mtklein217daa72014-07-02 12:55:21 -0700293 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
joshualitt44701df2015-02-23 14:44:57 -0800294 scissorSpaceIBounds.offset(-clip.origin());
halcanary96fcdcc2015-08-27 07:41:13 -0700295 if (nullptr == devBounds ||
mtklein217daa72014-07-02 12:55:21 -0700296 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
joshualitt77b13072014-10-27 14:51:01 -0700297 scissorState->set(scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000298 }
egdaniel8dd688b2015-01-22 10:16:09 -0800299 this->setPipelineBuilderStencil(pipelineBuilder, ars);
bsalomon0ba8c242015-10-07 09:20:28 -0700300 out->fClipCoverageFP.reset(clipFP);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000301 return true;
302 }
303 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000304
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000305 // If MSAA is enabled we can do everything in the stencil buffer.
vbuzinov3e77ba92015-09-30 23:02:06 -0700306 if (0 == rt->numStencilSamples() && requiresAA) {
robertphillips588b9ca2015-10-04 08:40:31 -0700307 SkAutoTUnref<GrTexture> result;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000308
joshualitt8059eb92014-12-29 15:10:07 -0800309 // The top-left of the mask corresponds to the top-left corner of the bounds.
310 SkVector clipToMaskOffset = {
311 SkIntToScalar(-clipSpaceIBounds.fLeft),
312 SkIntToScalar(-clipSpaceIBounds.fTop)
313 };
314
egdaniel8dd688b2015-01-22 10:16:09 -0800315 if (this->useSWOnlyPath(pipelineBuilder, clipToMaskOffset, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000316 // The clip geometry is complex enough that it will be more efficient to create it
317 // entirely in software
robertphillips588b9ca2015-10-04 08:40:31 -0700318 result.reset(this->createSoftwareClipMask(genID,
319 initialState,
320 elements,
321 clipToMaskOffset,
322 clipSpaceIBounds));
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000323 } else {
robertphillips588b9ca2015-10-04 08:40:31 -0700324 result.reset(this->createAlphaClipMask(genID,
325 initialState,
326 elements,
327 clipToMaskOffset,
328 clipSpaceIBounds));
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000329 }
330
bsalomon49f085d2014-09-05 13:34:00 -0700331 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000332 // The mask's top left coord should be pinned to the rounded-out top left corner of
333 // clipSpace bounds. We determine the mask's position WRT to the render target here.
334 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
joshualitt44701df2015-02-23 14:44:57 -0800335 rtSpaceMaskBounds.offset(-clip.origin());
bsalomon0ba8c242015-10-07 09:20:28 -0700336 out->fClipCoverageFP.reset(create_fp_for_mask(result, rtSpaceMaskBounds));
egdaniel8dd688b2015-01-22 10:16:09 -0800337 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000338 return true;
339 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000340 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000341 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000342
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000343 // use the stencil clip if we can't represent the clip as a rectangle.
joshualitt44701df2015-02-23 14:44:57 -0800344 SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin();
joshualitt9853cce2014-11-17 14:22:48 -0800345 this->createStencilClipMask(rt,
346 genID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000347 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000348 elements,
349 clipSpaceIBounds,
350 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000351
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000352 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
353 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
354 // use both stencil and scissor test to the bounds for the final draw.
355 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
356 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
joshualitt77b13072014-10-27 14:51:01 -0700357 scissorState->set(scissorSpaceIBounds);
egdaniel8dd688b2015-01-22 10:16:09 -0800358 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000359 return true;
360}
361
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000362namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000363////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800364// Set a coverage drawing XPF on the pipelineBuilder for the given op and invertCoverage mode
365void set_coverage_drawing_xpf(SkRegion::Op op, bool invertCoverage,
366 GrPipelineBuilder* pipelineBuilder) {
egdaniel87509242014-12-17 13:37:13 -0800367 SkASSERT(op <= SkRegion::kLastOp);
egdaniel8dd688b2015-01-22 10:16:09 -0800368 pipelineBuilder->setCoverageSetOpXPFactory(op, invertCoverage);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000369}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000370}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000371
372////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800373bool GrClipMaskManager::drawElement(GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800374 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800375 GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000376 const SkClipStack::Element* element,
377 GrPathRenderer* pr) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000378
egdaniel8dd688b2015-01-22 10:16:09 -0800379 pipelineBuilder->setRenderTarget(target->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000380
egdaniel87509242014-12-17 13:37:13 -0800381 // The color we use to draw does not matter since we will always be using a GrCoverageSetOpXP
382 // which ignores color.
383 GrColor color = GrColor_WHITE;
384
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000385 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000386 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000387 case Element::kEmpty_Type:
388 SkDEBUGFAIL("Should never get here with an empty element.");
389 break;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000390 case Element::kRect_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700391 // TODO: Do rects directly to the accumulator using a aa-rect GrProcessor that covers
392 // the entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000393 if (element->isAA()) {
joshualitt8059eb92014-12-29 15:10:07 -0800394 SkRect devRect = element->getRect();
395 viewMatrix.mapRect(&devRect);
robertphillipsea461502015-05-26 11:38:03 -0700396
bsalomonb3b9aec2015-09-10 11:16:35 -0700397 fDrawTarget->drawAARect(*pipelineBuilder, color, viewMatrix,
robertphillipsea461502015-05-26 11:38:03 -0700398 element->getRect(), devRect);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000399 } else {
bsalomonb3b9aec2015-09-10 11:16:35 -0700400 fDrawTarget->drawNonAARect(*pipelineBuilder, color, viewMatrix,
joshualittd2b23e02015-08-21 10:53:34 -0700401 element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000402 }
403 return true;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000404 default: {
405 SkPath path;
406 element->asPath(&path);
jvanverthb3eb6872014-10-24 07:12:51 -0700407 path.setIsVolatile(true);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000408 if (path.isInverseFillType()) {
409 path.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000410 }
kkinnunen18996512015-04-26 23:18:49 -0700411 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
halcanary96fcdcc2015-08-27 07:41:13 -0700412 if (nullptr == pr) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000413 GrPathRendererChain::DrawType type;
414 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
415 GrPathRendererChain::kColor_DrawType;
bsalomonb3b9aec2015-09-10 11:16:35 -0700416 pr = this->getContext()->getPathRenderer(fDrawTarget, pipelineBuilder, viewMatrix,
egdaniel8dd688b2015-01-22 10:16:09 -0800417 path, stroke, false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000418 }
halcanary96fcdcc2015-08-27 07:41:13 -0700419 if (nullptr == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000420 return false;
421 }
bsalomon0aff2fa2015-07-31 06:48:27 -0700422 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700423 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700424 args.fResourceProvider = this->getContext()->resourceProvider();
425 args.fPipelineBuilder = pipelineBuilder;
426 args.fColor = color;
427 args.fViewMatrix = &viewMatrix;
428 args.fPath = &path;
429 args.fStroke = &stroke;
430 args.fAntiAlias = element->isAA();
431 pr->drawPath(args);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000432 break;
433 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000434 }
435 return true;
436}
437
egdaniel8dd688b2015-01-22 10:16:09 -0800438bool GrClipMaskManager::canStencilAndDrawElement(GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800439 GrTexture* target,
440 GrPathRenderer** pr,
441 const SkClipStack::Element* element) {
egdaniel8dd688b2015-01-22 10:16:09 -0800442 pipelineBuilder->setRenderTarget(target->asRenderTarget());
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000443
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000444 if (Element::kRect_Type == element->getType()) {
445 return true;
446 } else {
447 // We shouldn't get here with an empty clip element.
448 SkASSERT(Element::kEmpty_Type != element->getType());
449 SkPath path;
450 element->asPath(&path);
451 if (path.isInverseFillType()) {
452 path.toggleInverseFillType();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000453 }
kkinnunen18996512015-04-26 23:18:49 -0700454 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000455 GrPathRendererChain::DrawType type = element->isAA() ?
456 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
457 GrPathRendererChain::kStencilAndColor_DrawType;
bsalomonb3b9aec2015-09-10 11:16:35 -0700458 *pr = this->getContext()->getPathRenderer(fDrawTarget, pipelineBuilder, SkMatrix::I(), path,
joshualitt8059eb92014-12-29 15:10:07 -0800459 stroke, false, type);
bsalomon49f085d2014-09-05 13:34:00 -0700460 return SkToBool(*pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000461 }
462}
463
egdaniel8dd688b2015-01-22 10:16:09 -0800464void GrClipMaskManager::mergeMask(GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800465 GrTexture* dstMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000466 GrTexture* srcMask,
467 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000468 const SkIRect& dstBound,
469 const SkIRect& srcBound) {
egdaniel8dd688b2015-01-22 10:16:09 -0800470 pipelineBuilder->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000471
egdaniel87509242014-12-17 13:37:13 -0800472 // We want to invert the coverage here
egdaniel8dd688b2015-01-22 10:16:09 -0800473 set_coverage_drawing_xpf(op, false, pipelineBuilder);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000474
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000475 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000476 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000477
bsalomonac856c92015-08-27 06:30:17 -0700478 pipelineBuilder->addCoverageFragmentProcessor(
bsalomon4a339522015-10-06 08:40:50 -0700479 GrTextureDomainEffect::Create(srcMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000480 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000481 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
482 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000483 GrTextureParams::kNone_FilterMode))->unref();
joshualitt73bb4562015-03-25 07:16:21 -0700484
egdaniel87509242014-12-17 13:37:13 -0800485 // The color passed in here does not matter since the coverageSetOpXP won't read it.
bsalomonb3b9aec2015-09-10 11:16:35 -0700486 fDrawTarget->drawNonAARect(*pipelineBuilder,
joshualittd2b23e02015-08-21 10:53:34 -0700487 GrColor_WHITE,
488 SkMatrix::I(),
489 SkRect::Make(dstBound));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000490}
491
bsalomon427cf282014-10-16 13:41:43 -0700492GrTexture* GrClipMaskManager::createTempMask(int width, int height) {
bsalomonf2703d82014-10-28 14:33:06 -0700493 GrSurfaceDesc desc;
bsalomon3f490a02014-12-18 06:20:52 -0800494 desc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000495 desc.fWidth = width;
496 desc.fHeight = height;
bsalomon76228632015-05-29 08:02:10 -0700497 if (this->getContext()->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
bsalomon51d1f7e2014-12-22 08:40:49 -0800498 desc.fConfig = kAlpha_8_GrPixelConfig;
499 } else {
500 desc.fConfig = kRGBA_8888_GrPixelConfig;
501 }
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000502
bsalomoneae62002015-07-31 13:59:30 -0700503 return this->getContext()->textureProvider()->createApproxTexture(desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000504}
505
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000506////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700507// Create a 8-bit clip mask in alpha
508
509static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
510 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
511 GrUniqueKey::Builder builder(key, kDomain, 3);
512 builder[0] = clipGenID;
513 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
514 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
515}
516
517GrTexture* GrClipMaskManager::createCachedMask(int width, int height, const GrUniqueKey& key,
518 bool renderTarget) {
519 GrSurfaceDesc desc;
520 desc.fWidth = width;
521 desc.fHeight = height;
522 desc.fFlags = renderTarget ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
523 if (!renderTarget || fDrawTarget->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
524 desc.fConfig = kAlpha_8_GrPixelConfig;
525 } else {
526 desc.fConfig = kRGBA_8888_GrPixelConfig;
527 }
528
529 GrTexture* texture = fDrawTarget->cmmAccess().resourceProvider()->createApproxTexture(desc, 0);
530 if (!texture) {
halcanary96fcdcc2015-08-27 07:41:13 -0700531 return nullptr;
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000532 }
bsalomon473addf2015-10-02 07:49:05 -0700533 texture->resourcePriv().setUniqueKey(key);
534 return texture;
krajcevskiad1dc582014-06-10 15:06:47 -0700535}
536
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000537GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700538 GrReducedClip::InitialState initialState,
539 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800540 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000541 const SkIRect& clipSpaceIBounds) {
bsalomon473addf2015-10-02 07:49:05 -0700542 GrResourceProvider* resourceProvider = fDrawTarget->cmmAccess().resourceProvider();
543 GrUniqueKey key;
544 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
545 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
bsalomon473addf2015-10-02 07:49:05 -0700546 return texture;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000547 }
548
bsalomon473addf2015-10-02 07:49:05 -0700549 SkAutoTUnref<GrTexture> texture(this->createCachedMask(
550 clipSpaceIBounds.width(), clipSpaceIBounds.height(), key, true));
551
krajcevskiad1dc582014-06-10 15:06:47 -0700552 // There's no texture in the cache. Let's try to allocate it then.
bsalomon473addf2015-10-02 07:49:05 -0700553 if (!texture) {
halcanary96fcdcc2015-08-27 07:41:13 -0700554 return nullptr;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000555 }
556
joshualitt8059eb92014-12-29 15:10:07 -0800557 // Set the matrix so that rendered clip elements are transformed to mask space from clip
558 // space.
559 SkMatrix translate;
560 translate.setTranslate(clipToMaskOffset);
561
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000562 // The texture may be larger than necessary, this rect represents the part of the texture
563 // we populate with a rasterization of the clip.
564 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
565
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000566 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
567 // clear the part that we care about.
bsalomonb3b9aec2015-09-10 11:16:35 -0700568 fDrawTarget->clear(&maskSpaceIBounds,
joshualitt329bf482014-10-29 12:31:28 -0700569 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
570 true,
bsalomon473addf2015-10-02 07:49:05 -0700571 texture->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000572
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000573 // When we use the stencil in the below loop it is important to have this clip installed.
574 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
575 // pass must not set values outside of this bounds or stencil values outside the rect won't be
576 // cleared.
joshualitt44701df2015-02-23 14:44:57 -0800577 GrClip clip(maskSpaceIBounds);
bsalomon427cf282014-10-16 13:41:43 -0700578 SkAutoTUnref<GrTexture> temp;
joshualitt9853cce2014-11-17 14:22:48 -0800579
robertphillips@google.comf294b772012-04-27 14:29:26 +0000580 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700581 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000582 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000583 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000584 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000585 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
egdaniel8dd688b2015-01-22 10:16:09 -0800586 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800587
joshualitt44701df2015-02-23 14:44:57 -0800588 pipelineBuilder.setClip(clip);
halcanary96fcdcc2015-08-27 07:41:13 -0700589 GrPathRenderer* pr = nullptr;
bsalomon473addf2015-10-02 07:49:05 -0700590 bool useTemp = !this->canStencilAndDrawElement(&pipelineBuilder, texture, &pr, element);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000591 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000592 // 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 +0000593 // mask buffer can be substantially larger than the actually clip stack element. We
594 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000595 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000596 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000597
598 if (useTemp) {
599 if (invert) {
600 maskSpaceElementIBounds = maskSpaceIBounds;
601 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000602 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000603 elementBounds.offset(clipToMaskOffset);
604 elementBounds.roundOut(&maskSpaceElementIBounds);
605 }
606
bsalomon427cf282014-10-16 13:41:43 -0700607 if (!temp) {
608 temp.reset(this->createTempMask(maskSpaceIBounds.fRight,
609 maskSpaceIBounds.fBottom));
610 if (!temp) {
bsalomon473addf2015-10-02 07:49:05 -0700611 texture->resourcePriv().removeUniqueKey();
halcanary96fcdcc2015-08-27 07:41:13 -0700612 return nullptr;
bsalomon427cf282014-10-16 13:41:43 -0700613 }
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000614 }
bsalomon427cf282014-10-16 13:41:43 -0700615 dst = temp;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000616 // clear the temp target and set blend to replace
bsalomonb3b9aec2015-09-10 11:16:35 -0700617 fDrawTarget->clear(&maskSpaceElementIBounds,
joshualitt9853cce2014-11-17 14:22:48 -0800618 invert ? 0xffffffff : 0x00000000,
619 true,
620 dst->asRenderTarget());
egdaniel8dd688b2015-01-22 10:16:09 -0800621 set_coverage_drawing_xpf(SkRegion::kReplace_Op, invert, &pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000622 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000623 // draw directly into the result with the stencil set to make the pixels affected
624 // by the clip shape be non-zero.
bsalomon473addf2015-10-02 07:49:05 -0700625 dst = texture;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000626 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
627 kReplace_StencilOp,
628 kReplace_StencilOp,
629 kAlways_StencilFunc,
630 0xffff,
631 0xffff,
632 0xffff);
egdaniel8dd688b2015-01-22 10:16:09 -0800633 pipelineBuilder.setStencil(kStencilInElement);
634 set_coverage_drawing_xpf(op, invert, &pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000635 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000636
egdaniel8dd688b2015-01-22 10:16:09 -0800637 if (!this->drawElement(&pipelineBuilder, translate, dst, element, pr)) {
bsalomon473addf2015-10-02 07:49:05 -0700638 texture->resourcePriv().removeUniqueKey();
halcanary96fcdcc2015-08-27 07:41:13 -0700639 return nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000640 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000641
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000642 if (useTemp) {
egdaniel8dd688b2015-01-22 10:16:09 -0800643 GrPipelineBuilder backgroundPipelineBuilder;
bsalomon473addf2015-10-02 07:49:05 -0700644 backgroundPipelineBuilder.setRenderTarget(texture->asRenderTarget());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800645
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000646 // Now draw into the accumulator using the real operation and the temp buffer as a
647 // texture
egdaniel8dd688b2015-01-22 10:16:09 -0800648 this->mergeMask(&backgroundPipelineBuilder,
bsalomon473addf2015-10-02 07:49:05 -0700649 texture,
bsalomon427cf282014-10-16 13:41:43 -0700650 temp,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000651 op,
652 maskSpaceIBounds,
653 maskSpaceElementIBounds);
654 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800655 GrPipelineBuilder backgroundPipelineBuilder;
bsalomon473addf2015-10-02 07:49:05 -0700656 backgroundPipelineBuilder.setRenderTarget(texture->asRenderTarget());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800657
egdaniel8dd688b2015-01-22 10:16:09 -0800658 set_coverage_drawing_xpf(op, !invert, &backgroundPipelineBuilder);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000659 // Draw to the exterior pixels (those with a zero stencil value).
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000660 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
661 kZero_StencilOp,
662 kZero_StencilOp,
663 kEqual_StencilFunc,
664 0xffff,
665 0x0000,
666 0xffff);
egdaniel8dd688b2015-01-22 10:16:09 -0800667 backgroundPipelineBuilder.setStencil(kDrawOutsideElement);
joshualitt73bb4562015-03-25 07:16:21 -0700668
egdaniel87509242014-12-17 13:37:13 -0800669 // The color passed in here does not matter since the coverageSetOpXP won't read it.
bsalomonb3b9aec2015-09-10 11:16:35 -0700670 fDrawTarget->drawNonAARect(backgroundPipelineBuilder, GrColor_WHITE, translate,
joshualittd2b23e02015-08-21 10:53:34 -0700671 clipSpaceIBounds);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000672 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000673 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800674 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800675
robertphillips@google.come79f3202014-02-11 16:30:21 +0000676 // all the remaining ops can just be directly draw into the accumulation buffer
egdaniel8dd688b2015-01-22 10:16:09 -0800677 set_coverage_drawing_xpf(op, false, &pipelineBuilder);
egdaniel87509242014-12-17 13:37:13 -0800678 // The color passed in here does not matter since the coverageSetOpXP won't read it.
bsalomon473addf2015-10-02 07:49:05 -0700679 this->drawElement(&pipelineBuilder, translate, texture, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000680 }
681 }
682
bsalomon473addf2015-10-02 07:49:05 -0700683 return texture.detach();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000684}
685
686////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000687// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000688// (as opposed to canvas) coordinates
joshualitt9853cce2014-11-17 14:22:48 -0800689bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt,
690 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700691 GrReducedClip::InitialState initialState,
692 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000693 const SkIRect& clipSpaceIBounds,
694 const SkIPoint& clipSpaceToStencilOffset) {
bsalomon49f085d2014-09-05 13:34:00 -0700695 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000696
egdanielec00d942015-09-14 12:56:10 -0700697 GrStencilAttachment* stencilAttachment =
698 fDrawTarget->cmmAccess().resourceProvider()->attachStencilAttachment(rt);
halcanary96fcdcc2015-08-27 07:41:13 -0700699 if (nullptr == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000700 return false;
701 }
702
egdaniel8dc7c3a2015-04-16 11:22:42 -0700703 if (stencilAttachment->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
704 stencilAttachment->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000705 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
706 SkVector translate = {
707 SkIntToScalar(clipSpaceToStencilOffset.fX),
708 SkIntToScalar(clipSpaceToStencilOffset.fY)
709 };
joshualitt8059eb92014-12-29 15:10:07 -0800710 SkMatrix viewMatrix;
711 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000712
bsalomon@google.com9f131742012-12-13 20:43:56 +0000713 // We set the current clip to the bounds so that our recursive draws are scissored to them.
714 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
715 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
joshualitt44701df2015-02-23 14:44:57 -0800716 GrClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000717
egdaniel8dc7c3a2015-04-16 11:22:42 -0700718 int clipBit = stencilAttachment->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000719 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000720 clipBit = (1 << (clipBit-1));
721
bsalomonb3b9aec2015-09-10 11:16:35 -0700722 fDrawTarget->cmmAccess().clearStencilClip(stencilSpaceIBounds,
723 GrReducedClip::kAllIn_InitialState == initialState, rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000724
725 // walk through each clip element and perform its set op
726 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700727 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000728 const Element* element = iter.get();
joshualitt9853cce2014-11-17 14:22:48 -0800729
egdaniel8dd688b2015-01-22 10:16:09 -0800730 GrPipelineBuilder pipelineBuilder;
joshualitt44701df2015-02-23 14:44:57 -0800731 pipelineBuilder.setClip(clip);
egdaniel8dd688b2015-01-22 10:16:09 -0800732 pipelineBuilder.setRenderTarget(rt);
egdaniel080e6732014-12-22 07:35:52 -0800733
egdaniel8dd688b2015-01-22 10:16:09 -0800734 pipelineBuilder.setDisableColorXPFactory();
joshualitt9853cce2014-11-17 14:22:48 -0800735
736 // if the target is MSAA then we want MSAA enabled when the clip is soft
vbuzinov3e77ba92015-09-30 23:02:06 -0700737 if (rt->isStencilBufferMultisampled()) {
bsalomond79c5492015-04-27 10:07:04 -0700738 pipelineBuilder.setState(GrPipelineBuilder::kHWAntialias_Flag, element->isAA());
joshualitt9853cce2014-11-17 14:22:48 -0800739 }
740
tomhudson@google.com8afae612012-08-14 15:03:35 +0000741 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000742 // enabled at bottom of loop
joshualitt7a6184f2014-10-29 18:29:27 -0700743 fClipMode = kIgnoreClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000744
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000745 // This will be used to determine whether the clip shape can be rendered into the
746 // stencil with arbitrary stencil settings.
747 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000748
kkinnunen18996512015-04-26 23:18:49 -0700749 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000750 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000751
halcanary96fcdcc2015-08-27 07:41:13 -0700752 GrPathRenderer* pr = nullptr;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000753 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000754 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000755 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000756 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000757 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000758 element->asPath(&clipPath);
759 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000760 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000761 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000762 }
bsalomonb3b9aec2015-09-10 11:16:35 -0700763 pr = this->getContext()->getPathRenderer(fDrawTarget,
egdaniel8dd688b2015-01-22 10:16:09 -0800764 &pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800765 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800766 clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000767 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000768 false,
769 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000770 &stencilSupport);
halcanary96fcdcc2015-08-27 07:41:13 -0700771 if (nullptr == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000772 return false;
773 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000774 }
775
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000776 int passes;
777 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
778
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000779 bool canRenderDirectToStencil =
780 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000781 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000782 // fill rule, and set operation can
783 // we render the element directly to
784 // stencil bit used for clipping.
785 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
786 canRenderDirectToStencil,
787 clipBit,
788 fillInverted,
789 &passes,
790 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000791
792 // draw the element to the client stencil bits if necessary
793 if (!canDrawDirectToClip) {
794 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000795 kIncClamp_StencilOp,
796 kIncClamp_StencilOp,
797 kAlways_StencilFunc,
798 0xffff,
799 0x0000,
800 0xffff);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000801 if (Element::kRect_Type == element->getType()) {
egdaniel8dd688b2015-01-22 10:16:09 -0800802 *pipelineBuilder.stencil() = gDrawToStencil;
joshualitt73bb4562015-03-25 07:16:21 -0700803
804 // We need this AGP until everything is in GrBatch
bsalomonb3b9aec2015-09-10 11:16:35 -0700805 fDrawTarget->drawNonAARect(pipelineBuilder,
joshualittd2b23e02015-08-21 10:53:34 -0700806 GrColor_WHITE,
807 viewMatrix,
808 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000809 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000810 if (!clipPath.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000811 if (canRenderDirectToStencil) {
egdaniel8dd688b2015-01-22 10:16:09 -0800812 *pipelineBuilder.stencil() = gDrawToStencil;
bsalomon0aff2fa2015-07-31 06:48:27 -0700813
814 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700815 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700816 args.fResourceProvider = this->getContext()->resourceProvider();
817 args.fPipelineBuilder = &pipelineBuilder;
818 args.fColor = GrColor_WHITE;
819 args.fViewMatrix = &viewMatrix;
820 args.fPath = &clipPath;
821 args.fStroke = &stroke;
822 args.fAntiAlias = false;
823 pr->drawPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000824 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700825 GrPathRenderer::StencilPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700826 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700827 args.fResourceProvider = this->getContext()->resourceProvider();
828 args.fPipelineBuilder = &pipelineBuilder;
829 args.fViewMatrix = &viewMatrix;
830 args.fPath = &clipPath;
831 args.fStroke = &stroke;
832 pr->stencilPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000833 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000834 }
835 }
836 }
837
838 // now we modify the clip bit by rendering either the clip
839 // element directly or a bounding rect of the entire clip.
joshualitt7a6184f2014-10-29 18:29:27 -0700840 fClipMode = kModifyClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000841 for (int p = 0; p < passes; ++p) {
joshualitt4f6dc522015-07-09 12:17:44 -0700842 *pipelineBuilder.stencil() = stencilSettings[p];
joshualitt9853cce2014-11-17 14:22:48 -0800843
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000844 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000845 if (Element::kRect_Type == element->getType()) {
joshualitt73bb4562015-03-25 07:16:21 -0700846 // We need this AGP until everything is in GrBatch
bsalomonb3b9aec2015-09-10 11:16:35 -0700847 fDrawTarget->drawNonAARect(pipelineBuilder,
joshualittd2b23e02015-08-21 10:53:34 -0700848 GrColor_WHITE,
849 viewMatrix,
850 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000851 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700852 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700853 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700854 args.fResourceProvider = this->getContext()->resourceProvider();
855 args.fPipelineBuilder = &pipelineBuilder;
856 args.fColor = GrColor_WHITE;
857 args.fViewMatrix = &viewMatrix;
858 args.fPath = &clipPath;
859 args.fStroke = &stroke;
860 args.fAntiAlias = false;
861 pr->drawPath(args);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000862 }
863 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000864 // The view matrix is setup to do clip space -> stencil space translation, so
865 // draw rect in clip space.
bsalomonb3b9aec2015-09-10 11:16:35 -0700866 fDrawTarget->drawNonAARect(pipelineBuilder,
joshualittd2b23e02015-08-21 10:53:34 -0700867 GrColor_WHITE,
868 viewMatrix,
869 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000870 }
871 }
872 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000873 }
joshualitt7a6184f2014-10-29 18:29:27 -0700874 fClipMode = kRespectClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000875 return true;
876}
877
bsalomon@google.com411dad02012-06-05 20:24:20 +0000878// mapping of clip-respecting stencil funcs to normal stencil funcs
879// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000880static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000881 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
882 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
883 // In the Clip Funcs
884 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
885 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
886 kLess_StencilFunc, // kLessIfInClip_StencilFunc
887 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
888 // Special in the clip func that forces user's ref to be 0.
889 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
890 // make ref 0 and do normal nequal.
891 },
892 {// Stencil-Clipping is ENABLED
893 // In the Clip Funcs
894 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
895 // eq stencil clip bit, mask
896 // out user bits.
897
898 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
899 // add stencil bit to mask and ref
900
901 kLess_StencilFunc, // kLessIfInClip_StencilFunc
902 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
903 // for both of these we can add
904 // the clip bit to the mask and
905 // ref and compare as normal
906 // Special in the clip func that forces user's ref to be 0.
907 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
908 // make ref have only the clip bit set
909 // and make comparison be less
910 // 10..0 < 1..user_bits..
911 }
912};
913
bsalomon@google.coma3201942012-06-21 19:58:20 +0000914namespace {
915// Sets the settings to clip against the stencil buffer clip while ignoring the
916// client bits.
917const GrStencilSettings& basic_apply_stencil_clip_settings() {
918 // stencil settings to use when clip is in stencil
919 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
920 kKeep_StencilOp,
921 kKeep_StencilOp,
922 kAlwaysIfInClip_StencilFunc,
923 0x0000,
924 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000925 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000926 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
927}
928}
929
joshualitt5e6ba212015-07-13 07:35:05 -0700930void GrClipMaskManager::setPipelineBuilderStencil(const GrPipelineBuilder& pipelineBuilder,
egdaniel8dd688b2015-01-22 10:16:09 -0800931 GrPipelineBuilder::AutoRestoreStencil* ars) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000932 // We make two copies of the StencilSettings here (except in the early
933 // exit scenario. One copy from draw state to the stack var. Then another
934 // from the stack var to the gpu. We could make this class hold a ptr to
935 // GrGpu's fStencilSettings and eliminate the stack copy here.
936
bsalomon@google.coma3201942012-06-21 19:58:20 +0000937 // use stencil for clipping if clipping is enabled and the clip
938 // has been written into the stencil.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000939 GrStencilSettings settings;
joshualitt9853cce2014-11-17 14:22:48 -0800940
bsalomon@google.coma3201942012-06-21 19:58:20 +0000941 // The GrGpu client may not be using the stencil buffer but we may need to
942 // enable it in order to respect a stencil clip.
joshualitt5e6ba212015-07-13 07:35:05 -0700943 if (pipelineBuilder.getStencil().isDisabled()) {
joshualitt7a6184f2014-10-29 18:29:27 -0700944 if (GrClipMaskManager::kRespectClip_StencilClipMode == fClipMode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000945 settings = basic_apply_stencil_clip_settings();
946 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000947 return;
948 }
949 } else {
joshualitt5e6ba212015-07-13 07:35:05 -0700950 settings = pipelineBuilder.getStencil();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000951 }
952
bsalomon@google.coma3201942012-06-21 19:58:20 +0000953 int stencilBits = 0;
joshualitt5e6ba212015-07-13 07:35:05 -0700954 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700955 GrStencilAttachment* stencilAttachment =
956 fDrawTarget->cmmAccess().resourceProvider()->attachStencilAttachment(rt);
egdaniel8dc7c3a2015-04-16 11:22:42 -0700957 if (stencilAttachment) {
958 stencilBits = stencilAttachment->bits();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000959 }
960
bsalomonb3b9aec2015-09-10 11:16:35 -0700961 SkASSERT(fDrawTarget->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
962 SkASSERT(fDrawTarget->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
joshualitt7a6184f2014-10-29 18:29:27 -0700963 this->adjustStencilParams(&settings, fClipMode, stencilBits);
joshualitt5e6ba212015-07-13 07:35:05 -0700964 ars->set(&pipelineBuilder);
965 ars->setStencil(settings);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000966}
967
968void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
969 StencilClipMode mode,
970 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000971 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000972
973 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000974 // We assume that this clip manager itself is drawing to the GrGpu and
975 // has already setup the correct values.
976 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000977 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000978
bsalomon@google.com411dad02012-06-05 20:24:20 +0000979 unsigned int clipBit = (1 << (stencilBitCnt - 1));
980 unsigned int userBits = clipBit - 1;
981
bsalomon@google.coma3201942012-06-21 19:58:20 +0000982 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomonb3b9aec2015-09-10 11:16:35 -0700983 bool twoSided = fDrawTarget->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000984
bsalomon@google.coma3201942012-06-21 19:58:20 +0000985 bool finished = false;
986 while (!finished) {
987 GrStencilFunc func = settings->func(face);
988 uint16_t writeMask = settings->writeMask(face);
989 uint16_t funcMask = settings->funcMask(face);
990 uint16_t funcRef = settings->funcRef(face);
991
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000992 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000993
994 writeMask &= userBits;
995
996 if (func >= kBasicStencilFuncCount) {
997 int respectClip = kRespectClip_StencilClipMode == mode;
998 if (respectClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000999 switch (func) {
1000 case kAlwaysIfInClip_StencilFunc:
1001 funcMask = clipBit;
1002 funcRef = clipBit;
1003 break;
1004 case kEqualIfInClip_StencilFunc:
1005 case kLessIfInClip_StencilFunc:
1006 case kLEqualIfInClip_StencilFunc:
1007 funcMask = (funcMask & userBits) | clipBit;
1008 funcRef = (funcRef & userBits) | clipBit;
1009 break;
1010 case kNonZeroIfInClip_StencilFunc:
1011 funcMask = (funcMask & userBits) | clipBit;
1012 funcRef = clipBit;
1013 break;
1014 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001015 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +00001016 }
1017 } else {
1018 funcMask &= userBits;
1019 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001020 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001021 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001022 gSpecialToBasicStencilFunc[respectClip];
1023 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001024 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001025 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001026 funcMask &= userBits;
1027 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001028 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001029
1030 settings->setFunc(face, func);
1031 settings->setWriteMask(face, writeMask);
1032 settings->setFuncMask(face, funcMask);
1033 settings->setFuncRef(face, funcRef);
1034
1035 if (GrStencilSettings::kFront_Face == face) {
1036 face = GrStencilSettings::kBack_Face;
1037 finished = !twoSided;
1038 } else {
1039 finished = true;
1040 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001041 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001042 if (!twoSided) {
1043 settings->copyFrontSettingsToBack();
1044 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001045}
1046
1047////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +00001048GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001049 GrReducedClip::InitialState initialState,
1050 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -08001051 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001052 const SkIRect& clipSpaceIBounds) {
bsalomon473addf2015-10-02 07:49:05 -07001053 GrUniqueKey key;
1054 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
1055 GrResourceProvider* resourceProvider = fDrawTarget->cmmAccess().resourceProvider();
1056 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
1057 return texture;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001058 }
1059
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001060 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1061 // the top left corner of the resulting rect to the top left of the texture.
1062 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1063
robertphillips@google.com2c756812012-05-22 20:28:23 +00001064 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001065
joshualitt8059eb92014-12-29 15:10:07 -08001066 // Set the matrix so that rendered clip elements are transformed to mask space from clip
1067 // space.
1068 SkMatrix translate;
1069 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -08001070
joshualitt8059eb92014-12-29 15:10:07 -08001071 helper.init(maskSpaceIBounds, &translate, false);
tfarinabf54e492014-10-23 17:47:18 -07001072 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001073 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001074
tfarinabf54e492014-10-23 17:47:18 -07001075 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001076 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001077 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001078
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001079 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1080 // Intersect and reverse difference require modifying pixels outside of the geometry
1081 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1082 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1083 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001084 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001085 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001086 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001087 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001088 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001089 SkPath clipPath;
1090 element->asPath(&clipPath);
1091 clipPath.toggleInverseFillType();
1092 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001093 continue;
1094 }
1095
1096 // The other ops (union, xor, diff) only affect pixels inside
1097 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001098 if (Element::kRect_Type == element->getType()) {
1099 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1100 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001101 SkPath path;
1102 element->asPath(&path);
1103 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001104 }
1105 }
1106
krajcevskiad1dc582014-06-10 15:06:47 -07001107 // Allocate clip mask texture
bsalomon473addf2015-10-02 07:49:05 -07001108 GrTexture* result = this->createCachedMask(clipSpaceIBounds.width(), clipSpaceIBounds.height(),
1109 key, false);
halcanary96fcdcc2015-08-27 07:41:13 -07001110 if (nullptr == result) {
halcanary96fcdcc2015-08-27 07:41:13 -07001111 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -07001112 }
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001113 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001114
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001115 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001116}
1117
robertphillips@google.comf294b772012-04-27 14:29:26 +00001118////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001119
egdaniel8dc7c3a2015-04-16 11:22:42 -07001120void GrClipMaskManager::adjustPathStencilParams(const GrStencilAttachment* stencilAttachment,
joshualitt9853cce2014-11-17 14:22:48 -08001121 GrStencilSettings* settings) {
egdaniel8dc7c3a2015-04-16 11:22:42 -07001122 if (stencilAttachment) {
1123 int stencilBits = stencilAttachment->bits();
joshualitt7a6184f2014-10-29 18:29:27 -07001124 this->adjustStencilParams(settings, fClipMode, stencilBits);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001125 }
1126}