blob: 1ddcef1b36c3cc4c48c4f00d8a6f401172736995 [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////////////////////////////////////////////////////////////////////////////////
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
bsalomonb3b9aec2015-09-10 11:16:35 -070081GrClipMaskManager::GrClipMaskManager(GrDrawTarget* drawTarget)
bsalomonedd77a12015-05-29 09:45:57 -070082 : fCurrClipMaskType(kNone_ClipMaskType)
bsalomonb3b9aec2015-09-10 11:16:35 -070083 , fDrawTarget(drawTarget)
bsalomonedd77a12015-05-29 09:45:57 -070084 , fClipMode(kIgnoreClip_StencilClipMode) {
85}
86
bsalomonb3b9aec2015-09-10 11:16:35 -070087GrContext* GrClipMaskManager::getContext() { return fDrawTarget->cmmAccess().context(); }
bsalomonedd77a12015-05-29 09:45:57 -070088
robertphillips@google.comfa662942012-05-17 12:20:22 +000089/*
90 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
91 * will be used on any element. If so, it returns true to indicate that the
92 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
93 */
joshualitt5e6ba212015-07-13 07:35:05 -070094bool GrClipMaskManager::useSWOnlyPath(const GrPipelineBuilder& pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -080095 const SkVector& clipToMaskOffset,
joshualitt9853cce2014-11-17 14:22:48 -080096 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000097 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000098 // a clip gets complex enough it can just be done in SW regardless
99 // of whether it would invoke the GrSoftwarePathRenderer.
kkinnunen18996512015-04-26 23:18:49 -0700100 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000101
joshualitt8059eb92014-12-29 15:10:07 -0800102 // Set the matrix so that rendered clip elements are transformed to mask space from clip
103 // space.
104 SkMatrix translate;
105 translate.setTranslate(clipToMaskOffset);
106
tfarinabf54e492014-10-23 17:47:18 -0700107 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000108 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000109 // rects can always be drawn directly w/o using the software path
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000110 // Skip rrects once we're drawing them directly.
111 if (Element::kRect_Type != element->getType()) {
112 SkPath path;
113 element->asPath(&path);
bsalomonb3b9aec2015-09-10 11:16:35 -0700114 if (path_needs_SW_renderer(this->getContext(), fDrawTarget, pipelineBuilder, translate,
joshualitt8059eb92014-12-29 15:10:07 -0800115 path, stroke, element->isAA())) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000116 return true;
117 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000118 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000119 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000120 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000121}
122
joshualitt4421a4c2015-07-13 09:36:41 -0700123bool GrClipMaskManager::installClipEffects(
124 const GrPipelineBuilder& pipelineBuilder,
125 GrPipelineBuilder::AutoRestoreFragmentProcessorState* arfps,
126 const GrReducedClip::ElementList& elements,
127 const SkVector& clipToRTOffset,
128 const SkRect* drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000129 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700130 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000131 boundsInClipSpace = *drawBounds;
132 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
133 }
134
joshualitt4421a4c2015-07-13 09:36:41 -0700135 arfps->set(&pipelineBuilder);
joshualitt5e6ba212015-07-13 07:35:05 -0700136 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
tfarinabf54e492014-10-23 17:47:18 -0700137 GrReducedClip::ElementList::Iter iter(elements);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000138 bool failed = false;
bsalomon49f085d2014-09-05 13:34:00 -0700139 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000140 SkRegion::Op op = iter.get()->getOp();
141 bool invert;
142 bool skip = false;
143 switch (op) {
144 case SkRegion::kReplace_Op:
145 SkASSERT(iter.get() == elements.head());
146 // Fallthrough, handled same as intersect.
147 case SkRegion::kIntersect_Op:
148 invert = false;
bsalomon49f085d2014-09-05 13:34:00 -0700149 if (drawBounds && iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000150 skip = true;
151 }
152 break;
153 case SkRegion::kDifference_Op:
154 invert = true;
155 // We don't currently have a cheap test for whether a rect is fully outside an
156 // element's primitive, so don't attempt to set skip.
157 break;
158 default:
159 failed = true;
160 break;
161 }
162 if (failed) {
163 break;
164 }
165
166 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700167 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800168 if (iter.get()->isAA()) {
vbuzinovdded6962015-06-12 08:59:45 -0700169 if (rt->isUnifiedMultisampled()) {
mtklein217daa72014-07-02 12:55:21 -0700170 // Coverage based AA clips don't place nicely with MSAA.
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000171 failed = true;
172 break;
173 }
joshualittb0a8a372014-09-23 09:50:21 -0700174 edgeType =
175 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000176 } else {
joshualitt5e6ba212015-07-13 07:35:05 -0700177 edgeType = invert ? kInverseFillBW_GrProcessorEdgeType :
178 kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000179 }
joshualittb0a8a372014-09-23 09:50:21 -0700180 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000181 switch (iter.get()->getType()) {
182 case SkClipStack::Element::kPath_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700183 fp.reset(GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
joshualitt642429e2015-02-26 08:47:52 -0800184 &clipToRTOffset));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000185 break;
186 case SkClipStack::Element::kRRect_Type: {
187 SkRRect rrect = iter.get()->getRRect();
188 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
joshualittb0a8a372014-09-23 09:50:21 -0700189 fp.reset(GrRRectEffect::Create(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000190 break;
191 }
192 case SkClipStack::Element::kRect_Type: {
193 SkRect rect = iter.get()->getRect();
194 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
joshualittb0a8a372014-09-23 09:50:21 -0700195 fp.reset(GrConvexPolyEffect::Create(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000196 break;
197 }
198 default:
199 break;
200 }
joshualittb0a8a372014-09-23 09:50:21 -0700201 if (fp) {
bsalomonac856c92015-08-27 06:30:17 -0700202 arfps->addCoverageFragmentProcessor(fp);
mtklein217daa72014-07-02 12:55:21 -0700203 } else {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000204 failed = true;
205 break;
206 }
207 }
mtklein217daa72014-07-02 12:55:21 -0700208 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000209 }
210
211 if (failed) {
halcanary96fcdcc2015-08-27 07:41:13 -0700212 arfps->set(nullptr);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000213 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000214 return !failed;
215}
216
robertphillips@google.comf294b772012-04-27 14:29:26 +0000217////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000218// sort out what kind of clip mask needs to be created: alpha, stencil,
219// scissor, or entirely software
joshualitt5e6ba212015-07-13 07:35:05 -0700220bool GrClipMaskManager::setupClipping(const GrPipelineBuilder& pipelineBuilder,
joshualitt4421a4c2015-07-13 09:36:41 -0700221 GrPipelineBuilder::AutoRestoreFragmentProcessorState* arfps,
egdaniel8dd688b2015-01-22 10:16:09 -0800222 GrPipelineBuilder::AutoRestoreStencil* ars,
bsalomon3e791242014-12-17 13:43:13 -0800223 GrScissorState* scissorState,
joshualitt9853cce2014-11-17 14:22:48 -0800224 const SkRect* devBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000225 fCurrClipMaskType = kNone_ClipMaskType;
joshualitt7a6184f2014-10-29 18:29:27 -0700226 if (kRespectClip_StencilClipMode == fClipMode) {
227 fClipMode = kIgnoreClip_StencilClipMode;
228 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000229
tfarinabf54e492014-10-23 17:47:18 -0700230 GrReducedClip::ElementList elements(16);
brucedawson71d7f7f2015-02-26 13:28:53 -0800231 int32_t genID = 0;
232 GrReducedClip::InitialState initialState = GrReducedClip::kAllIn_InitialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000233 SkIRect clipSpaceIBounds;
brucedawson71d7f7f2015-02-26 13:28:53 -0800234 bool requiresAA = false;
joshualitt5e6ba212015-07-13 07:35:05 -0700235 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000236
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000237 // GrDrawTarget should have filtered this for us
bsalomon49f085d2014-09-05 13:34:00 -0700238 SkASSERT(rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000239
joshualitt44701df2015-02-23 14:44:57 -0800240 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
joshualitt5e6ba212015-07-13 07:35:05 -0700241 const GrClip& clip = pipelineBuilder.clip();
bsalomon96e02a82015-03-06 07:13:01 -0800242 if (clip.isWideOpen(clipSpaceRTIBounds)) {
egdaniel8dd688b2015-01-22 10:16:09 -0800243 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000244 return true;
245 }
246
bsalomon96e02a82015-03-06 07:13:01 -0800247 // The clip mask manager always draws with a single IRect so we special case that logic here
248 // Image filters just use a rect, so we also special case that logic
249 switch (clip.clipType()) {
250 case GrClip::kWideOpen_ClipType:
251 SkFAIL("Should have caught this with clip.isWideOpen()");
252 return true;
bsalomon9ce30e12015-03-06 08:42:34 -0800253 case GrClip::kIRect_ClipType: {
254 SkIRect scissor = clip.irect();
255 if (scissor.intersect(clipSpaceRTIBounds)) {
256 scissorState->set(scissor);
257 this->setPipelineBuilderStencil(pipelineBuilder, ars);
258 return true;
259 }
260 return false;
261 }
bsalomon96e02a82015-03-06 07:13:01 -0800262 case GrClip::kClipStack_ClipType: {
263 clipSpaceRTIBounds.offset(clip.origin());
264 GrReducedClip::ReduceClipStack(*clip.clipStack(),
265 clipSpaceRTIBounds,
266 &elements,
267 &genID,
268 &initialState,
269 &clipSpaceIBounds,
270 &requiresAA);
271 if (elements.isEmpty()) {
272 if (GrReducedClip::kAllIn_InitialState == initialState) {
273 if (clipSpaceIBounds == clipSpaceRTIBounds) {
274 this->setPipelineBuilderStencil(pipelineBuilder, ars);
275 return true;
276 }
277 } else {
278 return false;
279 }
280 }
281 } break;
282 }
283
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000284 // An element count of 4 was chosen because of the common pattern in Blink of:
285 // isect RR
286 // diff RR
287 // isect convex_poly
288 // isect convex_poly
289 // when drawing rounded div borders. This could probably be tuned based on a
290 // configuration's relative costs of switching RTs to generate a mask vs
291 // longer shaders.
292 if (elements.count() <= 4) {
joshualitt44701df2015-02-23 14:44:57 -0800293 SkVector clipToRTOffset = { SkIntToScalar(-clip.origin().fX),
294 SkIntToScalar(-clip.origin().fY) };
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000295 if (elements.isEmpty() ||
joshualitt4421a4c2015-07-13 09:36:41 -0700296 (requiresAA && this->installClipEffects(pipelineBuilder, arfps, elements, clipToRTOffset,
bsalomon55f0b182015-03-05 17:43:09 -0800297 devBounds))) {
mtklein217daa72014-07-02 12:55:21 -0700298 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
joshualitt44701df2015-02-23 14:44:57 -0800299 scissorSpaceIBounds.offset(-clip.origin());
halcanary96fcdcc2015-08-27 07:41:13 -0700300 if (nullptr == devBounds ||
mtklein217daa72014-07-02 12:55:21 -0700301 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
joshualitt77b13072014-10-27 14:51:01 -0700302 scissorState->set(scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000303 }
egdaniel8dd688b2015-01-22 10:16:09 -0800304 this->setPipelineBuilderStencil(pipelineBuilder, ars);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000305 return true;
306 }
307 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000308
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000309 // If MSAA is enabled we can do everything in the stencil buffer.
vbuzinov3e77ba92015-09-30 23:02:06 -0700310 if (0 == rt->numStencilSamples() && requiresAA) {
halcanary96fcdcc2015-08-27 07:41:13 -0700311 GrTexture* result = nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000312
joshualitt8059eb92014-12-29 15:10:07 -0800313 // The top-left of the mask corresponds to the top-left corner of the bounds.
314 SkVector clipToMaskOffset = {
315 SkIntToScalar(-clipSpaceIBounds.fLeft),
316 SkIntToScalar(-clipSpaceIBounds.fTop)
317 };
318
egdaniel8dd688b2015-01-22 10:16:09 -0800319 if (this->useSWOnlyPath(pipelineBuilder, clipToMaskOffset, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000320 // The clip geometry is complex enough that it will be more efficient to create it
321 // entirely in software
322 result = this->createSoftwareClipMask(genID,
323 initialState,
324 elements,
joshualitt8059eb92014-12-29 15:10:07 -0800325 clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000326 clipSpaceIBounds);
327 } else {
328 result = this->createAlphaClipMask(genID,
329 initialState,
330 elements,
joshualitt8059eb92014-12-29 15:10:07 -0800331 clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000332 clipSpaceIBounds);
333 }
334
bsalomon49f085d2014-09-05 13:34:00 -0700335 if (result) {
joshualitt4421a4c2015-07-13 09:36:41 -0700336 arfps->set(&pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000337 // The mask's top left coord should be pinned to the rounded-out top left corner of
338 // clipSpace bounds. We determine the mask's position WRT to the render target here.
339 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
joshualitt44701df2015-02-23 14:44:57 -0800340 rtSpaceMaskBounds.offset(-clip.origin());
joshualitt4421a4c2015-07-13 09:36:41 -0700341 setup_drawstate_aaclip(pipelineBuilder, result, arfps, rtSpaceMaskBounds);
egdaniel8dd688b2015-01-22 10:16:09 -0800342 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000343 return true;
344 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000345 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000346 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000347
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000348 // use the stencil clip if we can't represent the clip as a rectangle.
joshualitt44701df2015-02-23 14:44:57 -0800349 SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin();
joshualitt9853cce2014-11-17 14:22:48 -0800350 this->createStencilClipMask(rt,
351 genID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000352 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000353 elements,
354 clipSpaceIBounds,
355 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000356
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000357 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
358 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
359 // use both stencil and scissor test to the bounds for the final draw.
360 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
361 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
joshualitt77b13072014-10-27 14:51:01 -0700362 scissorState->set(scissorSpaceIBounds);
egdaniel8dd688b2015-01-22 10:16:09 -0800363 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000364 return true;
365}
366
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000367namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000368////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800369// Set a coverage drawing XPF on the pipelineBuilder for the given op and invertCoverage mode
370void set_coverage_drawing_xpf(SkRegion::Op op, bool invertCoverage,
371 GrPipelineBuilder* pipelineBuilder) {
egdaniel87509242014-12-17 13:37:13 -0800372 SkASSERT(op <= SkRegion::kLastOp);
egdaniel8dd688b2015-01-22 10:16:09 -0800373 pipelineBuilder->setCoverageSetOpXPFactory(op, invertCoverage);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000374}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000375}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000376
377////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800378bool GrClipMaskManager::drawElement(GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800379 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800380 GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000381 const SkClipStack::Element* element,
382 GrPathRenderer* pr) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000383
egdaniel8dd688b2015-01-22 10:16:09 -0800384 pipelineBuilder->setRenderTarget(target->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000385
egdaniel87509242014-12-17 13:37:13 -0800386 // The color we use to draw does not matter since we will always be using a GrCoverageSetOpXP
387 // which ignores color.
388 GrColor color = GrColor_WHITE;
389
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000390 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000391 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000392 case Element::kEmpty_Type:
393 SkDEBUGFAIL("Should never get here with an empty element.");
394 break;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000395 case Element::kRect_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700396 // TODO: Do rects directly to the accumulator using a aa-rect GrProcessor that covers
397 // the entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000398 if (element->isAA()) {
joshualitt8059eb92014-12-29 15:10:07 -0800399 SkRect devRect = element->getRect();
400 viewMatrix.mapRect(&devRect);
robertphillipsea461502015-05-26 11:38:03 -0700401
bsalomonb3b9aec2015-09-10 11:16:35 -0700402 fDrawTarget->drawAARect(*pipelineBuilder, color, viewMatrix,
robertphillipsea461502015-05-26 11:38:03 -0700403 element->getRect(), devRect);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000404 } else {
bsalomonb3b9aec2015-09-10 11:16:35 -0700405 fDrawTarget->drawNonAARect(*pipelineBuilder, color, viewMatrix,
joshualittd2b23e02015-08-21 10:53:34 -0700406 element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000407 }
408 return true;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000409 default: {
410 SkPath path;
411 element->asPath(&path);
jvanverthb3eb6872014-10-24 07:12:51 -0700412 path.setIsVolatile(true);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000413 if (path.isInverseFillType()) {
414 path.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000415 }
kkinnunen18996512015-04-26 23:18:49 -0700416 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
halcanary96fcdcc2015-08-27 07:41:13 -0700417 if (nullptr == pr) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000418 GrPathRendererChain::DrawType type;
419 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
420 GrPathRendererChain::kColor_DrawType;
bsalomonb3b9aec2015-09-10 11:16:35 -0700421 pr = this->getContext()->getPathRenderer(fDrawTarget, pipelineBuilder, viewMatrix,
egdaniel8dd688b2015-01-22 10:16:09 -0800422 path, stroke, false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000423 }
halcanary96fcdcc2015-08-27 07:41:13 -0700424 if (nullptr == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000425 return false;
426 }
bsalomon0aff2fa2015-07-31 06:48:27 -0700427 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700428 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700429 args.fResourceProvider = this->getContext()->resourceProvider();
430 args.fPipelineBuilder = pipelineBuilder;
431 args.fColor = color;
432 args.fViewMatrix = &viewMatrix;
433 args.fPath = &path;
434 args.fStroke = &stroke;
435 args.fAntiAlias = element->isAA();
436 pr->drawPath(args);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000437 break;
438 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000439 }
440 return true;
441}
442
egdaniel8dd688b2015-01-22 10:16:09 -0800443bool GrClipMaskManager::canStencilAndDrawElement(GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800444 GrTexture* target,
445 GrPathRenderer** pr,
446 const SkClipStack::Element* element) {
egdaniel8dd688b2015-01-22 10:16:09 -0800447 pipelineBuilder->setRenderTarget(target->asRenderTarget());
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000448
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000449 if (Element::kRect_Type == element->getType()) {
450 return true;
451 } else {
452 // We shouldn't get here with an empty clip element.
453 SkASSERT(Element::kEmpty_Type != element->getType());
454 SkPath path;
455 element->asPath(&path);
456 if (path.isInverseFillType()) {
457 path.toggleInverseFillType();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000458 }
kkinnunen18996512015-04-26 23:18:49 -0700459 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000460 GrPathRendererChain::DrawType type = element->isAA() ?
461 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
462 GrPathRendererChain::kStencilAndColor_DrawType;
bsalomonb3b9aec2015-09-10 11:16:35 -0700463 *pr = this->getContext()->getPathRenderer(fDrawTarget, pipelineBuilder, SkMatrix::I(), path,
joshualitt8059eb92014-12-29 15:10:07 -0800464 stroke, false, type);
bsalomon49f085d2014-09-05 13:34:00 -0700465 return SkToBool(*pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000466 }
467}
468
egdaniel8dd688b2015-01-22 10:16:09 -0800469void GrClipMaskManager::mergeMask(GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800470 GrTexture* dstMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000471 GrTexture* srcMask,
472 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000473 const SkIRect& dstBound,
474 const SkIRect& srcBound) {
egdaniel8dd688b2015-01-22 10:16:09 -0800475 pipelineBuilder->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000476
egdaniel87509242014-12-17 13:37:13 -0800477 // We want to invert the coverage here
egdaniel8dd688b2015-01-22 10:16:09 -0800478 set_coverage_drawing_xpf(op, false, pipelineBuilder);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000479
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000480 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000481 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000482
bsalomonac856c92015-08-27 06:30:17 -0700483 pipelineBuilder->addCoverageFragmentProcessor(
joshualitt5f10b5c2015-07-09 10:24:35 -0700484 GrTextureDomainEffect::Create(pipelineBuilder->getProcessorDataManager(),
485 srcMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000486 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000487 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
488 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000489 GrTextureParams::kNone_FilterMode))->unref();
joshualitt73bb4562015-03-25 07:16:21 -0700490
egdaniel87509242014-12-17 13:37:13 -0800491 // The color passed in here does not matter since the coverageSetOpXP won't read it.
bsalomonb3b9aec2015-09-10 11:16:35 -0700492 fDrawTarget->drawNonAARect(*pipelineBuilder,
joshualittd2b23e02015-08-21 10:53:34 -0700493 GrColor_WHITE,
494 SkMatrix::I(),
495 SkRect::Make(dstBound));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000496}
497
bsalomon427cf282014-10-16 13:41:43 -0700498GrTexture* GrClipMaskManager::createTempMask(int width, int height) {
bsalomonf2703d82014-10-28 14:33:06 -0700499 GrSurfaceDesc desc;
bsalomon3f490a02014-12-18 06:20:52 -0800500 desc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000501 desc.fWidth = width;
502 desc.fHeight = height;
bsalomon76228632015-05-29 08:02:10 -0700503 if (this->getContext()->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
bsalomon51d1f7e2014-12-22 08:40:49 -0800504 desc.fConfig = kAlpha_8_GrPixelConfig;
505 } else {
506 desc.fConfig = kRGBA_8888_GrPixelConfig;
507 }
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000508
bsalomoneae62002015-07-31 13:59:30 -0700509 return this->getContext()->textureProvider()->createApproxTexture(desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000510}
511
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000512////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700513// Create a 8-bit clip mask in alpha
514
515static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
516 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
517 GrUniqueKey::Builder builder(key, kDomain, 3);
518 builder[0] = clipGenID;
519 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
520 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
521}
522
523GrTexture* GrClipMaskManager::createCachedMask(int width, int height, const GrUniqueKey& key,
524 bool renderTarget) {
525 GrSurfaceDesc desc;
526 desc.fWidth = width;
527 desc.fHeight = height;
528 desc.fFlags = renderTarget ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
529 if (!renderTarget || fDrawTarget->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
530 desc.fConfig = kAlpha_8_GrPixelConfig;
531 } else {
532 desc.fConfig = kRGBA_8888_GrPixelConfig;
533 }
534
535 GrTexture* texture = fDrawTarget->cmmAccess().resourceProvider()->createApproxTexture(desc, 0);
536 if (!texture) {
halcanary96fcdcc2015-08-27 07:41:13 -0700537 return nullptr;
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000538 }
bsalomon473addf2015-10-02 07:49:05 -0700539 texture->resourcePriv().setUniqueKey(key);
540 return texture;
krajcevskiad1dc582014-06-10 15:06:47 -0700541}
542
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000543GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700544 GrReducedClip::InitialState initialState,
545 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800546 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000547 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000548 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon473addf2015-10-02 07:49:05 -0700549 GrResourceProvider* resourceProvider = fDrawTarget->cmmAccess().resourceProvider();
550 GrUniqueKey key;
551 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
552 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000553 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon473addf2015-10-02 07:49:05 -0700554 return texture;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000555 }
556
bsalomon473addf2015-10-02 07:49:05 -0700557 SkAutoTUnref<GrTexture> texture(this->createCachedMask(
558 clipSpaceIBounds.width(), clipSpaceIBounds.height(), key, true));
559
krajcevskiad1dc582014-06-10 15:06:47 -0700560 // There's no texture in the cache. Let's try to allocate it then.
bsalomon473addf2015-10-02 07:49:05 -0700561 if (!texture) {
halcanary96fcdcc2015-08-27 07:41:13 -0700562 return nullptr;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000563 }
564
joshualitt8059eb92014-12-29 15:10:07 -0800565 // Set the matrix so that rendered clip elements are transformed to mask space from clip
566 // space.
567 SkMatrix translate;
568 translate.setTranslate(clipToMaskOffset);
569
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000570 // The texture may be larger than necessary, this rect represents the part of the texture
571 // we populate with a rasterization of the clip.
572 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
573
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000574 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
575 // clear the part that we care about.
bsalomonb3b9aec2015-09-10 11:16:35 -0700576 fDrawTarget->clear(&maskSpaceIBounds,
joshualitt329bf482014-10-29 12:31:28 -0700577 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
578 true,
bsalomon473addf2015-10-02 07:49:05 -0700579 texture->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000580
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000581 // When we use the stencil in the below loop it is important to have this clip installed.
582 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
583 // pass must not set values outside of this bounds or stencil values outside the rect won't be
584 // cleared.
joshualitt44701df2015-02-23 14:44:57 -0800585 GrClip clip(maskSpaceIBounds);
bsalomon427cf282014-10-16 13:41:43 -0700586 SkAutoTUnref<GrTexture> temp;
joshualitt9853cce2014-11-17 14:22:48 -0800587
robertphillips@google.comf294b772012-04-27 14:29:26 +0000588 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700589 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000590 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000591 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000592 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000593 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
egdaniel8dd688b2015-01-22 10:16:09 -0800594 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800595
joshualitt44701df2015-02-23 14:44:57 -0800596 pipelineBuilder.setClip(clip);
halcanary96fcdcc2015-08-27 07:41:13 -0700597 GrPathRenderer* pr = nullptr;
bsalomon473addf2015-10-02 07:49:05 -0700598 bool useTemp = !this->canStencilAndDrawElement(&pipelineBuilder, texture, &pr, element);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000599 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000600 // 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 +0000601 // mask buffer can be substantially larger than the actually clip stack element. We
602 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000603 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000604 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000605
606 if (useTemp) {
607 if (invert) {
608 maskSpaceElementIBounds = maskSpaceIBounds;
609 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000610 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000611 elementBounds.offset(clipToMaskOffset);
612 elementBounds.roundOut(&maskSpaceElementIBounds);
613 }
614
bsalomon427cf282014-10-16 13:41:43 -0700615 if (!temp) {
616 temp.reset(this->createTempMask(maskSpaceIBounds.fRight,
617 maskSpaceIBounds.fBottom));
618 if (!temp) {
bsalomon473addf2015-10-02 07:49:05 -0700619 texture->resourcePriv().removeUniqueKey();
halcanary96fcdcc2015-08-27 07:41:13 -0700620 return nullptr;
bsalomon427cf282014-10-16 13:41:43 -0700621 }
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000622 }
bsalomon427cf282014-10-16 13:41:43 -0700623 dst = temp;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000624 // clear the temp target and set blend to replace
bsalomonb3b9aec2015-09-10 11:16:35 -0700625 fDrawTarget->clear(&maskSpaceElementIBounds,
joshualitt9853cce2014-11-17 14:22:48 -0800626 invert ? 0xffffffff : 0x00000000,
627 true,
628 dst->asRenderTarget());
egdaniel8dd688b2015-01-22 10:16:09 -0800629 set_coverage_drawing_xpf(SkRegion::kReplace_Op, invert, &pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000630 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000631 // draw directly into the result with the stencil set to make the pixels affected
632 // by the clip shape be non-zero.
bsalomon473addf2015-10-02 07:49:05 -0700633 dst = texture;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000634 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
635 kReplace_StencilOp,
636 kReplace_StencilOp,
637 kAlways_StencilFunc,
638 0xffff,
639 0xffff,
640 0xffff);
egdaniel8dd688b2015-01-22 10:16:09 -0800641 pipelineBuilder.setStencil(kStencilInElement);
642 set_coverage_drawing_xpf(op, invert, &pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000643 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000644
egdaniel8dd688b2015-01-22 10:16:09 -0800645 if (!this->drawElement(&pipelineBuilder, translate, dst, element, pr)) {
bsalomon473addf2015-10-02 07:49:05 -0700646 texture->resourcePriv().removeUniqueKey();
halcanary96fcdcc2015-08-27 07:41:13 -0700647 return nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000648 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000649
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000650 if (useTemp) {
egdaniel8dd688b2015-01-22 10:16:09 -0800651 GrPipelineBuilder backgroundPipelineBuilder;
bsalomon473addf2015-10-02 07:49:05 -0700652 backgroundPipelineBuilder.setRenderTarget(texture->asRenderTarget());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800653
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000654 // Now draw into the accumulator using the real operation and the temp buffer as a
655 // texture
egdaniel8dd688b2015-01-22 10:16:09 -0800656 this->mergeMask(&backgroundPipelineBuilder,
bsalomon473addf2015-10-02 07:49:05 -0700657 texture,
bsalomon427cf282014-10-16 13:41:43 -0700658 temp,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000659 op,
660 maskSpaceIBounds,
661 maskSpaceElementIBounds);
662 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800663 GrPipelineBuilder backgroundPipelineBuilder;
bsalomon473addf2015-10-02 07:49:05 -0700664 backgroundPipelineBuilder.setRenderTarget(texture->asRenderTarget());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800665
egdaniel8dd688b2015-01-22 10:16:09 -0800666 set_coverage_drawing_xpf(op, !invert, &backgroundPipelineBuilder);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000667 // Draw to the exterior pixels (those with a zero stencil value).
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000668 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
669 kZero_StencilOp,
670 kZero_StencilOp,
671 kEqual_StencilFunc,
672 0xffff,
673 0x0000,
674 0xffff);
egdaniel8dd688b2015-01-22 10:16:09 -0800675 backgroundPipelineBuilder.setStencil(kDrawOutsideElement);
joshualitt73bb4562015-03-25 07:16:21 -0700676
egdaniel87509242014-12-17 13:37:13 -0800677 // The color passed in here does not matter since the coverageSetOpXP won't read it.
bsalomonb3b9aec2015-09-10 11:16:35 -0700678 fDrawTarget->drawNonAARect(backgroundPipelineBuilder, GrColor_WHITE, translate,
joshualittd2b23e02015-08-21 10:53:34 -0700679 clipSpaceIBounds);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000680 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000681 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800682 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800683
robertphillips@google.come79f3202014-02-11 16:30:21 +0000684 // all the remaining ops can just be directly draw into the accumulation buffer
egdaniel8dd688b2015-01-22 10:16:09 -0800685 set_coverage_drawing_xpf(op, false, &pipelineBuilder);
egdaniel87509242014-12-17 13:37:13 -0800686 // The color passed in here does not matter since the coverageSetOpXP won't read it.
bsalomon473addf2015-10-02 07:49:05 -0700687 this->drawElement(&pipelineBuilder, translate, texture, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000688 }
689 }
690
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000691 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon473addf2015-10-02 07:49:05 -0700692 return texture.detach();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000693}
694
695////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000696// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000697// (as opposed to canvas) coordinates
joshualitt9853cce2014-11-17 14:22:48 -0800698bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt,
699 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700700 GrReducedClip::InitialState initialState,
701 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000702 const SkIRect& clipSpaceIBounds,
703 const SkIPoint& clipSpaceToStencilOffset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000704 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon49f085d2014-09-05 13:34:00 -0700705 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000706
egdanielec00d942015-09-14 12:56:10 -0700707 GrStencilAttachment* stencilAttachment =
708 fDrawTarget->cmmAccess().resourceProvider()->attachStencilAttachment(rt);
halcanary96fcdcc2015-08-27 07:41:13 -0700709 if (nullptr == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000710 return false;
711 }
712
egdaniel8dc7c3a2015-04-16 11:22:42 -0700713 if (stencilAttachment->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
714 stencilAttachment->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000715 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
716 SkVector translate = {
717 SkIntToScalar(clipSpaceToStencilOffset.fX),
718 SkIntToScalar(clipSpaceToStencilOffset.fY)
719 };
joshualitt8059eb92014-12-29 15:10:07 -0800720 SkMatrix viewMatrix;
721 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000722
bsalomon@google.com9f131742012-12-13 20:43:56 +0000723 // We set the current clip to the bounds so that our recursive draws are scissored to them.
724 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
725 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
joshualitt44701df2015-02-23 14:44:57 -0800726 GrClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000727
egdaniel8dc7c3a2015-04-16 11:22:42 -0700728 int clipBit = stencilAttachment->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000729 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000730 clipBit = (1 << (clipBit-1));
731
bsalomonb3b9aec2015-09-10 11:16:35 -0700732 fDrawTarget->cmmAccess().clearStencilClip(stencilSpaceIBounds,
733 GrReducedClip::kAllIn_InitialState == initialState, rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000734
735 // walk through each clip element and perform its set op
736 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700737 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000738 const Element* element = iter.get();
joshualitt9853cce2014-11-17 14:22:48 -0800739
egdaniel8dd688b2015-01-22 10:16:09 -0800740 GrPipelineBuilder pipelineBuilder;
joshualitt44701df2015-02-23 14:44:57 -0800741 pipelineBuilder.setClip(clip);
egdaniel8dd688b2015-01-22 10:16:09 -0800742 pipelineBuilder.setRenderTarget(rt);
egdaniel080e6732014-12-22 07:35:52 -0800743
egdaniel8dd688b2015-01-22 10:16:09 -0800744 pipelineBuilder.setDisableColorXPFactory();
joshualitt9853cce2014-11-17 14:22:48 -0800745
746 // if the target is MSAA then we want MSAA enabled when the clip is soft
vbuzinov3e77ba92015-09-30 23:02:06 -0700747 if (rt->isStencilBufferMultisampled()) {
bsalomond79c5492015-04-27 10:07:04 -0700748 pipelineBuilder.setState(GrPipelineBuilder::kHWAntialias_Flag, element->isAA());
joshualitt9853cce2014-11-17 14:22:48 -0800749 }
750
tomhudson@google.com8afae612012-08-14 15:03:35 +0000751 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000752 // enabled at bottom of loop
joshualitt7a6184f2014-10-29 18:29:27 -0700753 fClipMode = kIgnoreClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000754
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000755 // This will be used to determine whether the clip shape can be rendered into the
756 // stencil with arbitrary stencil settings.
757 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000758
kkinnunen18996512015-04-26 23:18:49 -0700759 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000760 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000761
halcanary96fcdcc2015-08-27 07:41:13 -0700762 GrPathRenderer* pr = nullptr;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000763 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000764 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000765 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000766 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000767 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000768 element->asPath(&clipPath);
769 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000770 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000771 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000772 }
bsalomonb3b9aec2015-09-10 11:16:35 -0700773 pr = this->getContext()->getPathRenderer(fDrawTarget,
egdaniel8dd688b2015-01-22 10:16:09 -0800774 &pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800775 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800776 clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000777 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000778 false,
779 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000780 &stencilSupport);
halcanary96fcdcc2015-08-27 07:41:13 -0700781 if (nullptr == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000782 return false;
783 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000784 }
785
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000786 int passes;
787 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
788
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000789 bool canRenderDirectToStencil =
790 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000791 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000792 // fill rule, and set operation can
793 // we render the element directly to
794 // stencil bit used for clipping.
795 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
796 canRenderDirectToStencil,
797 clipBit,
798 fillInverted,
799 &passes,
800 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000801
802 // draw the element to the client stencil bits if necessary
803 if (!canDrawDirectToClip) {
804 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000805 kIncClamp_StencilOp,
806 kIncClamp_StencilOp,
807 kAlways_StencilFunc,
808 0xffff,
809 0x0000,
810 0xffff);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000811 if (Element::kRect_Type == element->getType()) {
egdaniel8dd688b2015-01-22 10:16:09 -0800812 *pipelineBuilder.stencil() = gDrawToStencil;
joshualitt73bb4562015-03-25 07:16:21 -0700813
814 // We need this AGP until everything is in GrBatch
bsalomonb3b9aec2015-09-10 11:16:35 -0700815 fDrawTarget->drawNonAARect(pipelineBuilder,
joshualittd2b23e02015-08-21 10:53:34 -0700816 GrColor_WHITE,
817 viewMatrix,
818 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000819 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000820 if (!clipPath.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000821 if (canRenderDirectToStencil) {
egdaniel8dd688b2015-01-22 10:16:09 -0800822 *pipelineBuilder.stencil() = gDrawToStencil;
bsalomon0aff2fa2015-07-31 06:48:27 -0700823
824 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700825 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700826 args.fResourceProvider = this->getContext()->resourceProvider();
827 args.fPipelineBuilder = &pipelineBuilder;
828 args.fColor = GrColor_WHITE;
829 args.fViewMatrix = &viewMatrix;
830 args.fPath = &clipPath;
831 args.fStroke = &stroke;
832 args.fAntiAlias = false;
833 pr->drawPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000834 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700835 GrPathRenderer::StencilPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700836 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700837 args.fResourceProvider = this->getContext()->resourceProvider();
838 args.fPipelineBuilder = &pipelineBuilder;
839 args.fViewMatrix = &viewMatrix;
840 args.fPath = &clipPath;
841 args.fStroke = &stroke;
842 pr->stencilPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000843 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000844 }
845 }
846 }
847
848 // now we modify the clip bit by rendering either the clip
849 // element directly or a bounding rect of the entire clip.
joshualitt7a6184f2014-10-29 18:29:27 -0700850 fClipMode = kModifyClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000851 for (int p = 0; p < passes; ++p) {
joshualitt4f6dc522015-07-09 12:17:44 -0700852 *pipelineBuilder.stencil() = stencilSettings[p];
joshualitt9853cce2014-11-17 14:22:48 -0800853
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000854 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000855 if (Element::kRect_Type == element->getType()) {
joshualitt73bb4562015-03-25 07:16:21 -0700856 // We need this AGP until everything is in GrBatch
bsalomonb3b9aec2015-09-10 11:16:35 -0700857 fDrawTarget->drawNonAARect(pipelineBuilder,
joshualittd2b23e02015-08-21 10:53:34 -0700858 GrColor_WHITE,
859 viewMatrix,
860 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000861 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700862 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700863 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700864 args.fResourceProvider = this->getContext()->resourceProvider();
865 args.fPipelineBuilder = &pipelineBuilder;
866 args.fColor = GrColor_WHITE;
867 args.fViewMatrix = &viewMatrix;
868 args.fPath = &clipPath;
869 args.fStroke = &stroke;
870 args.fAntiAlias = false;
871 pr->drawPath(args);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000872 }
873 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000874 // The view matrix is setup to do clip space -> stencil space translation, so
875 // draw rect in clip space.
bsalomonb3b9aec2015-09-10 11:16:35 -0700876 fDrawTarget->drawNonAARect(pipelineBuilder,
joshualittd2b23e02015-08-21 10:53:34 -0700877 GrColor_WHITE,
878 viewMatrix,
879 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000880 }
881 }
882 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000883 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000884 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000885 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000886 fCurrClipMaskType = kStencil_ClipMaskType;
joshualitt7a6184f2014-10-29 18:29:27 -0700887 fClipMode = kRespectClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000888 return true;
889}
890
bsalomon@google.com411dad02012-06-05 20:24:20 +0000891// mapping of clip-respecting stencil funcs to normal stencil funcs
892// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000893static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000894 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
895 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
896 // In the Clip Funcs
897 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
898 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
899 kLess_StencilFunc, // kLessIfInClip_StencilFunc
900 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
901 // Special in the clip func that forces user's ref to be 0.
902 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
903 // make ref 0 and do normal nequal.
904 },
905 {// Stencil-Clipping is ENABLED
906 // In the Clip Funcs
907 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
908 // eq stencil clip bit, mask
909 // out user bits.
910
911 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
912 // add stencil bit to mask and ref
913
914 kLess_StencilFunc, // kLessIfInClip_StencilFunc
915 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
916 // for both of these we can add
917 // the clip bit to the mask and
918 // ref and compare as normal
919 // Special in the clip func that forces user's ref to be 0.
920 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
921 // make ref have only the clip bit set
922 // and make comparison be less
923 // 10..0 < 1..user_bits..
924 }
925};
926
bsalomon@google.coma3201942012-06-21 19:58:20 +0000927namespace {
928// Sets the settings to clip against the stencil buffer clip while ignoring the
929// client bits.
930const GrStencilSettings& basic_apply_stencil_clip_settings() {
931 // stencil settings to use when clip is in stencil
932 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
933 kKeep_StencilOp,
934 kKeep_StencilOp,
935 kAlwaysIfInClip_StencilFunc,
936 0x0000,
937 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000938 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000939 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
940}
941}
942
joshualitt5e6ba212015-07-13 07:35:05 -0700943void GrClipMaskManager::setPipelineBuilderStencil(const GrPipelineBuilder& pipelineBuilder,
egdaniel8dd688b2015-01-22 10:16:09 -0800944 GrPipelineBuilder::AutoRestoreStencil* ars) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000945 // We make two copies of the StencilSettings here (except in the early
946 // exit scenario. One copy from draw state to the stack var. Then another
947 // from the stack var to the gpu. We could make this class hold a ptr to
948 // GrGpu's fStencilSettings and eliminate the stack copy here.
949
bsalomon@google.coma3201942012-06-21 19:58:20 +0000950 // use stencil for clipping if clipping is enabled and the clip
951 // has been written into the stencil.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000952 GrStencilSettings settings;
joshualitt9853cce2014-11-17 14:22:48 -0800953
bsalomon@google.coma3201942012-06-21 19:58:20 +0000954 // The GrGpu client may not be using the stencil buffer but we may need to
955 // enable it in order to respect a stencil clip.
joshualitt5e6ba212015-07-13 07:35:05 -0700956 if (pipelineBuilder.getStencil().isDisabled()) {
joshualitt7a6184f2014-10-29 18:29:27 -0700957 if (GrClipMaskManager::kRespectClip_StencilClipMode == fClipMode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000958 settings = basic_apply_stencil_clip_settings();
959 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000960 return;
961 }
962 } else {
joshualitt5e6ba212015-07-13 07:35:05 -0700963 settings = pipelineBuilder.getStencil();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000964 }
965
bsalomon@google.coma3201942012-06-21 19:58:20 +0000966 int stencilBits = 0;
joshualitt5e6ba212015-07-13 07:35:05 -0700967 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700968 GrStencilAttachment* stencilAttachment =
969 fDrawTarget->cmmAccess().resourceProvider()->attachStencilAttachment(rt);
egdaniel8dc7c3a2015-04-16 11:22:42 -0700970 if (stencilAttachment) {
971 stencilBits = stencilAttachment->bits();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000972 }
973
bsalomonb3b9aec2015-09-10 11:16:35 -0700974 SkASSERT(fDrawTarget->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
975 SkASSERT(fDrawTarget->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
joshualitt7a6184f2014-10-29 18:29:27 -0700976 this->adjustStencilParams(&settings, fClipMode, stencilBits);
joshualitt5e6ba212015-07-13 07:35:05 -0700977 ars->set(&pipelineBuilder);
978 ars->setStencil(settings);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000979}
980
981void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
982 StencilClipMode mode,
983 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000984 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000985
986 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000987 // We assume that this clip manager itself is drawing to the GrGpu and
988 // has already setup the correct values.
989 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000990 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000991
bsalomon@google.com411dad02012-06-05 20:24:20 +0000992 unsigned int clipBit = (1 << (stencilBitCnt - 1));
993 unsigned int userBits = clipBit - 1;
994
bsalomon@google.coma3201942012-06-21 19:58:20 +0000995 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomonb3b9aec2015-09-10 11:16:35 -0700996 bool twoSided = fDrawTarget->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000997
bsalomon@google.coma3201942012-06-21 19:58:20 +0000998 bool finished = false;
999 while (!finished) {
1000 GrStencilFunc func = settings->func(face);
1001 uint16_t writeMask = settings->writeMask(face);
1002 uint16_t funcMask = settings->funcMask(face);
1003 uint16_t funcRef = settings->funcRef(face);
1004
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001005 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001006
1007 writeMask &= userBits;
1008
1009 if (func >= kBasicStencilFuncCount) {
1010 int respectClip = kRespectClip_StencilClipMode == mode;
1011 if (respectClip) {
1012 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001013 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001014 switch (func) {
1015 case kAlwaysIfInClip_StencilFunc:
1016 funcMask = clipBit;
1017 funcRef = clipBit;
1018 break;
1019 case kEqualIfInClip_StencilFunc:
1020 case kLessIfInClip_StencilFunc:
1021 case kLEqualIfInClip_StencilFunc:
1022 funcMask = (funcMask & userBits) | clipBit;
1023 funcRef = (funcRef & userBits) | clipBit;
1024 break;
1025 case kNonZeroIfInClip_StencilFunc:
1026 funcMask = (funcMask & userBits) | clipBit;
1027 funcRef = clipBit;
1028 break;
1029 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001030 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +00001031 }
1032 } else {
1033 funcMask &= userBits;
1034 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001035 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001036 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001037 gSpecialToBasicStencilFunc[respectClip];
1038 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001039 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001040 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001041 funcMask &= userBits;
1042 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001043 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001044
1045 settings->setFunc(face, func);
1046 settings->setWriteMask(face, writeMask);
1047 settings->setFuncMask(face, funcMask);
1048 settings->setFuncRef(face, funcRef);
1049
1050 if (GrStencilSettings::kFront_Face == face) {
1051 face = GrStencilSettings::kBack_Face;
1052 finished = !twoSided;
1053 } else {
1054 finished = true;
1055 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001056 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001057 if (!twoSided) {
1058 settings->copyFrontSettingsToBack();
1059 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001060}
1061
1062////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +00001063GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001064 GrReducedClip::InitialState initialState,
1065 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -08001066 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001067 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001068 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon473addf2015-10-02 07:49:05 -07001069 GrUniqueKey key;
1070 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
1071 GrResourceProvider* resourceProvider = fDrawTarget->cmmAccess().resourceProvider();
1072 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
1073 return texture;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001074 }
1075
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001076 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1077 // the top left corner of the resulting rect to the top left of the texture.
1078 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1079
robertphillips@google.com2c756812012-05-22 20:28:23 +00001080 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001081
joshualitt8059eb92014-12-29 15:10:07 -08001082 // Set the matrix so that rendered clip elements are transformed to mask space from clip
1083 // space.
1084 SkMatrix translate;
1085 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -08001086
joshualitt8059eb92014-12-29 15:10:07 -08001087 helper.init(maskSpaceIBounds, &translate, false);
tfarinabf54e492014-10-23 17:47:18 -07001088 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001089 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001090
tfarinabf54e492014-10-23 17:47:18 -07001091 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001092 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001093 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001094
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001095 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1096 // Intersect and reverse difference require modifying pixels outside of the geometry
1097 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1098 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1099 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001100 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001101 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001102 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001103 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001104 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001105 SkPath clipPath;
1106 element->asPath(&clipPath);
1107 clipPath.toggleInverseFillType();
1108 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001109 continue;
1110 }
1111
1112 // The other ops (union, xor, diff) only affect pixels inside
1113 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001114 if (Element::kRect_Type == element->getType()) {
1115 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1116 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001117 SkPath path;
1118 element->asPath(&path);
1119 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001120 }
1121 }
1122
krajcevskiad1dc582014-06-10 15:06:47 -07001123 // Allocate clip mask texture
bsalomon473addf2015-10-02 07:49:05 -07001124 GrTexture* result = this->createCachedMask(clipSpaceIBounds.width(), clipSpaceIBounds.height(),
1125 key, false);
halcanary96fcdcc2015-08-27 07:41:13 -07001126 if (nullptr == result) {
halcanary96fcdcc2015-08-27 07:41:13 -07001127 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -07001128 }
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001129 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001130
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001131 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001132 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001133}
1134
robertphillips@google.comf294b772012-04-27 14:29:26 +00001135////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001136
egdaniel8dc7c3a2015-04-16 11:22:42 -07001137void GrClipMaskManager::adjustPathStencilParams(const GrStencilAttachment* stencilAttachment,
joshualitt9853cce2014-11-17 14:22:48 -08001138 GrStencilSettings* settings) {
egdaniel8dc7c3a2015-04-16 11:22:42 -07001139 if (stencilAttachment) {
1140 int stencilBits = stencilAttachment->bits();
joshualitt7a6184f2014-10-29 18:29:27 -07001141 this->adjustStencilParams(settings, fClipMode, stencilBits);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001142 }
1143}