blob: 4af96ac421538a136e0dfce7c832bc22e096e3b4 [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)
bsalomonedd77a12015-05-29 09:45:57 -070071 : fCurrClipMaskType(kNone_ClipMaskType)
bsalomonb3b9aec2015-09-10 11:16:35 -070072 , fDrawTarget(drawTarget)
bsalomonedd77a12015-05-29 09:45:57 -070073 , fClipMode(kIgnoreClip_StencilClipMode) {
74}
75
bsalomonb3b9aec2015-09-10 11:16:35 -070076GrContext* GrClipMaskManager::getContext() { return fDrawTarget->cmmAccess().context(); }
bsalomonedd77a12015-05-29 09:45:57 -070077
robertphillips@google.comfa662942012-05-17 12:20:22 +000078/*
79 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
80 * will be used on any element. If so, it returns true to indicate that the
81 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
82 */
joshualitt5e6ba212015-07-13 07:35:05 -070083bool GrClipMaskManager::useSWOnlyPath(const GrPipelineBuilder& pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -080084 const SkVector& clipToMaskOffset,
joshualitt9853cce2014-11-17 14:22:48 -080085 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000086 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000087 // a clip gets complex enough it can just be done in SW regardless
88 // of whether it would invoke the GrSoftwarePathRenderer.
kkinnunen18996512015-04-26 23:18:49 -070089 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000090
joshualitt8059eb92014-12-29 15:10:07 -080091 // Set the matrix so that rendered clip elements are transformed to mask space from clip
92 // space.
93 SkMatrix translate;
94 translate.setTranslate(clipToMaskOffset);
95
tfarinabf54e492014-10-23 17:47:18 -070096 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000097 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000098 // rects can always be drawn directly w/o using the software path
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000099 // Skip rrects once we're drawing them directly.
100 if (Element::kRect_Type != element->getType()) {
101 SkPath path;
102 element->asPath(&path);
bsalomonb3b9aec2015-09-10 11:16:35 -0700103 if (path_needs_SW_renderer(this->getContext(), fDrawTarget, pipelineBuilder, translate,
joshualitt8059eb92014-12-29 15:10:07 -0800104 path, stroke, element->isAA())) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000105 return true;
106 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000107 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000108 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000109 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000110}
111
bsalomon0ba8c242015-10-07 09:20:28 -0700112const GrFragmentProcessor* GrClipMaskManager::getAnalyticClipProcessor(
113 const GrReducedClip::ElementList& elements,
114 const SkVector& clipToRTOffset,
115 const SkRect* drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000116 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700117 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000118 boundsInClipSpace = *drawBounds;
119 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
120 }
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 }
155
156 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700157 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800158 if (iter.get()->isAA()) {
joshualittb0a8a372014-09-23 09:50:21 -0700159 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700160 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000161 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700162 edgeType =
163 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000164 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000165 switch (iter.get()->getType()) {
166 case SkClipStack::Element::kPath_Type:
bsalomon0ba8c242015-10-07 09:20:28 -0700167 fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
168 &clipToRTOffset);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000169 break;
170 case SkClipStack::Element::kRRect_Type: {
171 SkRRect rrect = iter.get()->getRRect();
172 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700173 fps[fpCnt] = GrRRectEffect::Create(edgeType, rrect);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000174 break;
175 }
176 case SkClipStack::Element::kRect_Type: {
177 SkRect rect = iter.get()->getRect();
178 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700179 fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, rect);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000180 break;
181 }
182 default:
183 break;
184 }
bsalomon0ba8c242015-10-07 09:20:28 -0700185 if (!fps[fpCnt]) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000186 failed = true;
187 break;
188 }
bsalomon0ba8c242015-10-07 09:20:28 -0700189 fpCnt++;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000190 }
mtklein217daa72014-07-02 12:55:21 -0700191 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000192 }
193
bsalomon0ba8c242015-10-07 09:20:28 -0700194 const GrFragmentProcessor* resultFP = nullptr;
195 if (!failed) {
196 resultFP = GrFragmentProcessor::RunInSeries(fps, fpCnt);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000197 }
bsalomon0ba8c242015-10-07 09:20:28 -0700198 for (int i = 0; i < fpCnt; ++i) {
199 fps[i]->unref();
200 }
201 return resultFP;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000202}
203
robertphillips@google.comf294b772012-04-27 14:29:26 +0000204////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000205// sort out what kind of clip mask needs to be created: alpha, stencil,
206// scissor, or entirely software
joshualitt5e6ba212015-07-13 07:35:05 -0700207bool GrClipMaskManager::setupClipping(const GrPipelineBuilder& pipelineBuilder,
egdaniel8dd688b2015-01-22 10:16:09 -0800208 GrPipelineBuilder::AutoRestoreStencil* ars,
bsalomon3e791242014-12-17 13:43:13 -0800209 GrScissorState* scissorState,
bsalomon0ba8c242015-10-07 09:20:28 -0700210 const SkRect* devBounds,
211 GrAppliedClip* out) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000212 fCurrClipMaskType = kNone_ClipMaskType;
joshualitt7a6184f2014-10-29 18:29:27 -0700213 if (kRespectClip_StencilClipMode == fClipMode) {
214 fClipMode = kIgnoreClip_StencilClipMode;
215 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000216
tfarinabf54e492014-10-23 17:47:18 -0700217 GrReducedClip::ElementList elements(16);
brucedawson71d7f7f2015-02-26 13:28:53 -0800218 int32_t genID = 0;
219 GrReducedClip::InitialState initialState = GrReducedClip::kAllIn_InitialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000220 SkIRect clipSpaceIBounds;
brucedawson71d7f7f2015-02-26 13:28:53 -0800221 bool requiresAA = false;
joshualitt5e6ba212015-07-13 07:35:05 -0700222 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000223
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000224 // GrDrawTarget should have filtered this for us
bsalomon49f085d2014-09-05 13:34:00 -0700225 SkASSERT(rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000226
joshualitt44701df2015-02-23 14:44:57 -0800227 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
joshualitt5e6ba212015-07-13 07:35:05 -0700228 const GrClip& clip = pipelineBuilder.clip();
bsalomon96e02a82015-03-06 07:13:01 -0800229 if (clip.isWideOpen(clipSpaceRTIBounds)) {
egdaniel8dd688b2015-01-22 10:16:09 -0800230 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000231 return true;
232 }
233
bsalomon96e02a82015-03-06 07:13:01 -0800234 // The clip mask manager always draws with a single IRect so we special case that logic here
235 // Image filters just use a rect, so we also special case that logic
236 switch (clip.clipType()) {
237 case GrClip::kWideOpen_ClipType:
238 SkFAIL("Should have caught this with clip.isWideOpen()");
239 return true;
bsalomon9ce30e12015-03-06 08:42:34 -0800240 case GrClip::kIRect_ClipType: {
241 SkIRect scissor = clip.irect();
242 if (scissor.intersect(clipSpaceRTIBounds)) {
243 scissorState->set(scissor);
244 this->setPipelineBuilderStencil(pipelineBuilder, ars);
245 return true;
246 }
247 return false;
248 }
bsalomon96e02a82015-03-06 07:13:01 -0800249 case GrClip::kClipStack_ClipType: {
250 clipSpaceRTIBounds.offset(clip.origin());
251 GrReducedClip::ReduceClipStack(*clip.clipStack(),
252 clipSpaceRTIBounds,
253 &elements,
254 &genID,
255 &initialState,
256 &clipSpaceIBounds,
257 &requiresAA);
258 if (elements.isEmpty()) {
259 if (GrReducedClip::kAllIn_InitialState == initialState) {
260 if (clipSpaceIBounds == clipSpaceRTIBounds) {
261 this->setPipelineBuilderStencil(pipelineBuilder, ars);
262 return true;
263 }
264 } else {
265 return false;
266 }
267 }
268 } break;
269 }
270
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000271 // An element count of 4 was chosen because of the common pattern in Blink of:
272 // isect RR
273 // diff RR
274 // isect convex_poly
275 // isect convex_poly
276 // when drawing rounded div borders. This could probably be tuned based on a
277 // configuration's relative costs of switching RTs to generate a mask vs
278 // longer shaders.
bsalomon0ba8c242015-10-07 09:20:28 -0700279 if (elements.count() <= kMaxAnalyticElements) {
joshualitt44701df2015-02-23 14:44:57 -0800280 SkVector clipToRTOffset = { SkIntToScalar(-clip.origin().fX),
281 SkIntToScalar(-clip.origin().fY) };
bsalomon0ba8c242015-10-07 09:20:28 -0700282 // When there are multiple color samples we want to do per-sample clipping, not compute
283 // a fractional pixel coverage.
284 bool disallowAnalyticAA = pipelineBuilder.getRenderTarget()->isUnifiedMultisampled();
285 const GrFragmentProcessor* clipFP = nullptr;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000286 if (elements.isEmpty() ||
bsalomon0ba8c242015-10-07 09:20:28 -0700287 (requiresAA && !disallowAnalyticAA &&
288 SkToBool(clipFP = this->getAnalyticClipProcessor(elements, clipToRTOffset, devBounds)))) {
mtklein217daa72014-07-02 12:55:21 -0700289 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
joshualitt44701df2015-02-23 14:44:57 -0800290 scissorSpaceIBounds.offset(-clip.origin());
halcanary96fcdcc2015-08-27 07:41:13 -0700291 if (nullptr == devBounds ||
mtklein217daa72014-07-02 12:55:21 -0700292 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
joshualitt77b13072014-10-27 14:51:01 -0700293 scissorState->set(scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000294 }
egdaniel8dd688b2015-01-22 10:16:09 -0800295 this->setPipelineBuilderStencil(pipelineBuilder, ars);
bsalomon0ba8c242015-10-07 09:20:28 -0700296 out->fClipCoverageFP.reset(clipFP);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000297 return true;
298 }
299 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000300
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000301 // If MSAA is enabled we can do everything in the stencil buffer.
vbuzinov3e77ba92015-09-30 23:02:06 -0700302 if (0 == rt->numStencilSamples() && requiresAA) {
robertphillips588b9ca2015-10-04 08:40:31 -0700303 SkAutoTUnref<GrTexture> result;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000304
joshualitt8059eb92014-12-29 15:10:07 -0800305 // The top-left of the mask corresponds to the top-left corner of the bounds.
306 SkVector clipToMaskOffset = {
307 SkIntToScalar(-clipSpaceIBounds.fLeft),
308 SkIntToScalar(-clipSpaceIBounds.fTop)
309 };
310
egdaniel8dd688b2015-01-22 10:16:09 -0800311 if (this->useSWOnlyPath(pipelineBuilder, clipToMaskOffset, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000312 // The clip geometry is complex enough that it will be more efficient to create it
313 // entirely in software
robertphillips588b9ca2015-10-04 08:40:31 -0700314 result.reset(this->createSoftwareClipMask(genID,
315 initialState,
316 elements,
317 clipToMaskOffset,
318 clipSpaceIBounds));
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000319 } else {
robertphillips588b9ca2015-10-04 08:40:31 -0700320 result.reset(this->createAlphaClipMask(genID,
321 initialState,
322 elements,
323 clipToMaskOffset,
324 clipSpaceIBounds));
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000325 }
326
bsalomon49f085d2014-09-05 13:34:00 -0700327 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000328 // The mask's top left coord should be pinned to the rounded-out top left corner of
329 // clipSpace bounds. We determine the mask's position WRT to the render target here.
330 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
joshualitt44701df2015-02-23 14:44:57 -0800331 rtSpaceMaskBounds.offset(-clip.origin());
bsalomon0ba8c242015-10-07 09:20:28 -0700332 out->fClipCoverageFP.reset(create_fp_for_mask(result, rtSpaceMaskBounds));
egdaniel8dd688b2015-01-22 10:16:09 -0800333 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000334 return true;
335 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000336 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000337 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000338
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000339 // use the stencil clip if we can't represent the clip as a rectangle.
joshualitt44701df2015-02-23 14:44:57 -0800340 SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin();
joshualitt9853cce2014-11-17 14:22:48 -0800341 this->createStencilClipMask(rt,
342 genID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000343 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000344 elements,
345 clipSpaceIBounds,
346 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000347
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000348 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
349 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
350 // use both stencil and scissor test to the bounds for the final draw.
351 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
352 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
joshualitt77b13072014-10-27 14:51:01 -0700353 scissorState->set(scissorSpaceIBounds);
egdaniel8dd688b2015-01-22 10:16:09 -0800354 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000355 return true;
356}
357
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000358namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000359////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800360// Set a coverage drawing XPF on the pipelineBuilder for the given op and invertCoverage mode
361void set_coverage_drawing_xpf(SkRegion::Op op, bool invertCoverage,
362 GrPipelineBuilder* pipelineBuilder) {
egdaniel87509242014-12-17 13:37:13 -0800363 SkASSERT(op <= SkRegion::kLastOp);
egdaniel8dd688b2015-01-22 10:16:09 -0800364 pipelineBuilder->setCoverageSetOpXPFactory(op, invertCoverage);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000365}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000366}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000367
368////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800369bool GrClipMaskManager::drawElement(GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800370 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800371 GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000372 const SkClipStack::Element* element,
373 GrPathRenderer* pr) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000374
egdaniel8dd688b2015-01-22 10:16:09 -0800375 pipelineBuilder->setRenderTarget(target->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000376
egdaniel87509242014-12-17 13:37:13 -0800377 // The color we use to draw does not matter since we will always be using a GrCoverageSetOpXP
378 // which ignores color.
379 GrColor color = GrColor_WHITE;
380
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000381 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000382 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000383 case Element::kEmpty_Type:
384 SkDEBUGFAIL("Should never get here with an empty element.");
385 break;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000386 case Element::kRect_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700387 // TODO: Do rects directly to the accumulator using a aa-rect GrProcessor that covers
388 // the entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000389 if (element->isAA()) {
joshualitt8059eb92014-12-29 15:10:07 -0800390 SkRect devRect = element->getRect();
391 viewMatrix.mapRect(&devRect);
robertphillipsea461502015-05-26 11:38:03 -0700392
bsalomonb3b9aec2015-09-10 11:16:35 -0700393 fDrawTarget->drawAARect(*pipelineBuilder, color, viewMatrix,
robertphillipsea461502015-05-26 11:38:03 -0700394 element->getRect(), devRect);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000395 } else {
bsalomonb3b9aec2015-09-10 11:16:35 -0700396 fDrawTarget->drawNonAARect(*pipelineBuilder, color, viewMatrix,
joshualittd2b23e02015-08-21 10:53:34 -0700397 element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000398 }
399 return true;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000400 default: {
401 SkPath path;
402 element->asPath(&path);
jvanverthb3eb6872014-10-24 07:12:51 -0700403 path.setIsVolatile(true);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000404 if (path.isInverseFillType()) {
405 path.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000406 }
kkinnunen18996512015-04-26 23:18:49 -0700407 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
halcanary96fcdcc2015-08-27 07:41:13 -0700408 if (nullptr == pr) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000409 GrPathRendererChain::DrawType type;
410 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
411 GrPathRendererChain::kColor_DrawType;
bsalomonb3b9aec2015-09-10 11:16:35 -0700412 pr = this->getContext()->getPathRenderer(fDrawTarget, pipelineBuilder, viewMatrix,
egdaniel8dd688b2015-01-22 10:16:09 -0800413 path, stroke, false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000414 }
halcanary96fcdcc2015-08-27 07:41:13 -0700415 if (nullptr == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000416 return false;
417 }
bsalomon0aff2fa2015-07-31 06:48:27 -0700418 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700419 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700420 args.fResourceProvider = this->getContext()->resourceProvider();
421 args.fPipelineBuilder = pipelineBuilder;
422 args.fColor = color;
423 args.fViewMatrix = &viewMatrix;
424 args.fPath = &path;
425 args.fStroke = &stroke;
426 args.fAntiAlias = element->isAA();
427 pr->drawPath(args);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000428 break;
429 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000430 }
431 return true;
432}
433
egdaniel8dd688b2015-01-22 10:16:09 -0800434bool GrClipMaskManager::canStencilAndDrawElement(GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800435 GrTexture* target,
436 GrPathRenderer** pr,
437 const SkClipStack::Element* element) {
egdaniel8dd688b2015-01-22 10:16:09 -0800438 pipelineBuilder->setRenderTarget(target->asRenderTarget());
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000439
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000440 if (Element::kRect_Type == element->getType()) {
441 return true;
442 } else {
443 // We shouldn't get here with an empty clip element.
444 SkASSERT(Element::kEmpty_Type != element->getType());
445 SkPath path;
446 element->asPath(&path);
447 if (path.isInverseFillType()) {
448 path.toggleInverseFillType();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000449 }
kkinnunen18996512015-04-26 23:18:49 -0700450 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000451 GrPathRendererChain::DrawType type = element->isAA() ?
452 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
453 GrPathRendererChain::kStencilAndColor_DrawType;
bsalomonb3b9aec2015-09-10 11:16:35 -0700454 *pr = this->getContext()->getPathRenderer(fDrawTarget, pipelineBuilder, SkMatrix::I(), path,
joshualitt8059eb92014-12-29 15:10:07 -0800455 stroke, false, type);
bsalomon49f085d2014-09-05 13:34:00 -0700456 return SkToBool(*pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000457 }
458}
459
egdaniel8dd688b2015-01-22 10:16:09 -0800460void GrClipMaskManager::mergeMask(GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800461 GrTexture* dstMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000462 GrTexture* srcMask,
463 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000464 const SkIRect& dstBound,
465 const SkIRect& srcBound) {
egdaniel8dd688b2015-01-22 10:16:09 -0800466 pipelineBuilder->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000467
egdaniel87509242014-12-17 13:37:13 -0800468 // We want to invert the coverage here
egdaniel8dd688b2015-01-22 10:16:09 -0800469 set_coverage_drawing_xpf(op, false, pipelineBuilder);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000470
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000471 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000472 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000473
bsalomonac856c92015-08-27 06:30:17 -0700474 pipelineBuilder->addCoverageFragmentProcessor(
bsalomon4a339522015-10-06 08:40:50 -0700475 GrTextureDomainEffect::Create(srcMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000476 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000477 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
478 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000479 GrTextureParams::kNone_FilterMode))->unref();
joshualitt73bb4562015-03-25 07:16:21 -0700480
egdaniel87509242014-12-17 13:37:13 -0800481 // The color passed in here does not matter since the coverageSetOpXP won't read it.
bsalomonb3b9aec2015-09-10 11:16:35 -0700482 fDrawTarget->drawNonAARect(*pipelineBuilder,
joshualittd2b23e02015-08-21 10:53:34 -0700483 GrColor_WHITE,
484 SkMatrix::I(),
485 SkRect::Make(dstBound));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000486}
487
bsalomon427cf282014-10-16 13:41:43 -0700488GrTexture* GrClipMaskManager::createTempMask(int width, int height) {
bsalomonf2703d82014-10-28 14:33:06 -0700489 GrSurfaceDesc desc;
bsalomon3f490a02014-12-18 06:20:52 -0800490 desc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000491 desc.fWidth = width;
492 desc.fHeight = height;
bsalomon76228632015-05-29 08:02:10 -0700493 if (this->getContext()->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
bsalomon51d1f7e2014-12-22 08:40:49 -0800494 desc.fConfig = kAlpha_8_GrPixelConfig;
495 } else {
496 desc.fConfig = kRGBA_8888_GrPixelConfig;
497 }
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000498
bsalomoneae62002015-07-31 13:59:30 -0700499 return this->getContext()->textureProvider()->createApproxTexture(desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000500}
501
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000502////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700503// Create a 8-bit clip mask in alpha
504
505static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
506 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
507 GrUniqueKey::Builder builder(key, kDomain, 3);
508 builder[0] = clipGenID;
509 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
510 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
511}
512
513GrTexture* GrClipMaskManager::createCachedMask(int width, int height, const GrUniqueKey& key,
514 bool renderTarget) {
515 GrSurfaceDesc desc;
516 desc.fWidth = width;
517 desc.fHeight = height;
518 desc.fFlags = renderTarget ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
519 if (!renderTarget || fDrawTarget->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
520 desc.fConfig = kAlpha_8_GrPixelConfig;
521 } else {
522 desc.fConfig = kRGBA_8888_GrPixelConfig;
523 }
524
525 GrTexture* texture = fDrawTarget->cmmAccess().resourceProvider()->createApproxTexture(desc, 0);
526 if (!texture) {
halcanary96fcdcc2015-08-27 07:41:13 -0700527 return nullptr;
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000528 }
bsalomon473addf2015-10-02 07:49:05 -0700529 texture->resourcePriv().setUniqueKey(key);
530 return texture;
krajcevskiad1dc582014-06-10 15:06:47 -0700531}
532
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000533GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700534 GrReducedClip::InitialState initialState,
535 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800536 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000537 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000538 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon473addf2015-10-02 07:49:05 -0700539 GrResourceProvider* resourceProvider = fDrawTarget->cmmAccess().resourceProvider();
540 GrUniqueKey key;
541 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
542 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000543 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon473addf2015-10-02 07:49:05 -0700544 return texture;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000545 }
546
bsalomon473addf2015-10-02 07:49:05 -0700547 SkAutoTUnref<GrTexture> texture(this->createCachedMask(
548 clipSpaceIBounds.width(), clipSpaceIBounds.height(), key, true));
549
krajcevskiad1dc582014-06-10 15:06:47 -0700550 // There's no texture in the cache. Let's try to allocate it then.
bsalomon473addf2015-10-02 07:49:05 -0700551 if (!texture) {
halcanary96fcdcc2015-08-27 07:41:13 -0700552 return nullptr;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000553 }
554
joshualitt8059eb92014-12-29 15:10:07 -0800555 // Set the matrix so that rendered clip elements are transformed to mask space from clip
556 // space.
557 SkMatrix translate;
558 translate.setTranslate(clipToMaskOffset);
559
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000560 // The texture may be larger than necessary, this rect represents the part of the texture
561 // we populate with a rasterization of the clip.
562 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
563
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000564 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
565 // clear the part that we care about.
bsalomonb3b9aec2015-09-10 11:16:35 -0700566 fDrawTarget->clear(&maskSpaceIBounds,
joshualitt329bf482014-10-29 12:31:28 -0700567 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
568 true,
bsalomon473addf2015-10-02 07:49:05 -0700569 texture->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000570
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000571 // When we use the stencil in the below loop it is important to have this clip installed.
572 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
573 // pass must not set values outside of this bounds or stencil values outside the rect won't be
574 // cleared.
joshualitt44701df2015-02-23 14:44:57 -0800575 GrClip clip(maskSpaceIBounds);
bsalomon427cf282014-10-16 13:41:43 -0700576 SkAutoTUnref<GrTexture> temp;
joshualitt9853cce2014-11-17 14:22:48 -0800577
robertphillips@google.comf294b772012-04-27 14:29:26 +0000578 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700579 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000580 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000581 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000582 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000583 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
egdaniel8dd688b2015-01-22 10:16:09 -0800584 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800585
joshualitt44701df2015-02-23 14:44:57 -0800586 pipelineBuilder.setClip(clip);
halcanary96fcdcc2015-08-27 07:41:13 -0700587 GrPathRenderer* pr = nullptr;
bsalomon473addf2015-10-02 07:49:05 -0700588 bool useTemp = !this->canStencilAndDrawElement(&pipelineBuilder, texture, &pr, element);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000589 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000590 // 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 +0000591 // mask buffer can be substantially larger than the actually clip stack element. We
592 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000593 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000594 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000595
596 if (useTemp) {
597 if (invert) {
598 maskSpaceElementIBounds = maskSpaceIBounds;
599 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000600 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000601 elementBounds.offset(clipToMaskOffset);
602 elementBounds.roundOut(&maskSpaceElementIBounds);
603 }
604
bsalomon427cf282014-10-16 13:41:43 -0700605 if (!temp) {
606 temp.reset(this->createTempMask(maskSpaceIBounds.fRight,
607 maskSpaceIBounds.fBottom));
608 if (!temp) {
bsalomon473addf2015-10-02 07:49:05 -0700609 texture->resourcePriv().removeUniqueKey();
halcanary96fcdcc2015-08-27 07:41:13 -0700610 return nullptr;
bsalomon427cf282014-10-16 13:41:43 -0700611 }
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000612 }
bsalomon427cf282014-10-16 13:41:43 -0700613 dst = temp;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000614 // clear the temp target and set blend to replace
bsalomonb3b9aec2015-09-10 11:16:35 -0700615 fDrawTarget->clear(&maskSpaceElementIBounds,
joshualitt9853cce2014-11-17 14:22:48 -0800616 invert ? 0xffffffff : 0x00000000,
617 true,
618 dst->asRenderTarget());
egdaniel8dd688b2015-01-22 10:16:09 -0800619 set_coverage_drawing_xpf(SkRegion::kReplace_Op, invert, &pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000620 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000621 // draw directly into the result with the stencil set to make the pixels affected
622 // by the clip shape be non-zero.
bsalomon473addf2015-10-02 07:49:05 -0700623 dst = texture;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000624 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
625 kReplace_StencilOp,
626 kReplace_StencilOp,
627 kAlways_StencilFunc,
628 0xffff,
629 0xffff,
630 0xffff);
egdaniel8dd688b2015-01-22 10:16:09 -0800631 pipelineBuilder.setStencil(kStencilInElement);
632 set_coverage_drawing_xpf(op, invert, &pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000633 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000634
egdaniel8dd688b2015-01-22 10:16:09 -0800635 if (!this->drawElement(&pipelineBuilder, translate, dst, element, pr)) {
bsalomon473addf2015-10-02 07:49:05 -0700636 texture->resourcePriv().removeUniqueKey();
halcanary96fcdcc2015-08-27 07:41:13 -0700637 return nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000638 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000639
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000640 if (useTemp) {
egdaniel8dd688b2015-01-22 10:16:09 -0800641 GrPipelineBuilder backgroundPipelineBuilder;
bsalomon473addf2015-10-02 07:49:05 -0700642 backgroundPipelineBuilder.setRenderTarget(texture->asRenderTarget());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800643
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000644 // Now draw into the accumulator using the real operation and the temp buffer as a
645 // texture
egdaniel8dd688b2015-01-22 10:16:09 -0800646 this->mergeMask(&backgroundPipelineBuilder,
bsalomon473addf2015-10-02 07:49:05 -0700647 texture,
bsalomon427cf282014-10-16 13:41:43 -0700648 temp,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000649 op,
650 maskSpaceIBounds,
651 maskSpaceElementIBounds);
652 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800653 GrPipelineBuilder backgroundPipelineBuilder;
bsalomon473addf2015-10-02 07:49:05 -0700654 backgroundPipelineBuilder.setRenderTarget(texture->asRenderTarget());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800655
egdaniel8dd688b2015-01-22 10:16:09 -0800656 set_coverage_drawing_xpf(op, !invert, &backgroundPipelineBuilder);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000657 // Draw to the exterior pixels (those with a zero stencil value).
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000658 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
659 kZero_StencilOp,
660 kZero_StencilOp,
661 kEqual_StencilFunc,
662 0xffff,
663 0x0000,
664 0xffff);
egdaniel8dd688b2015-01-22 10:16:09 -0800665 backgroundPipelineBuilder.setStencil(kDrawOutsideElement);
joshualitt73bb4562015-03-25 07:16:21 -0700666
egdaniel87509242014-12-17 13:37:13 -0800667 // The color passed in here does not matter since the coverageSetOpXP won't read it.
bsalomonb3b9aec2015-09-10 11:16:35 -0700668 fDrawTarget->drawNonAARect(backgroundPipelineBuilder, GrColor_WHITE, translate,
joshualittd2b23e02015-08-21 10:53:34 -0700669 clipSpaceIBounds);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000670 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000671 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800672 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800673
robertphillips@google.come79f3202014-02-11 16:30:21 +0000674 // all the remaining ops can just be directly draw into the accumulation buffer
egdaniel8dd688b2015-01-22 10:16:09 -0800675 set_coverage_drawing_xpf(op, false, &pipelineBuilder);
egdaniel87509242014-12-17 13:37:13 -0800676 // The color passed in here does not matter since the coverageSetOpXP won't read it.
bsalomon473addf2015-10-02 07:49:05 -0700677 this->drawElement(&pipelineBuilder, translate, texture, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000678 }
679 }
680
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000681 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon473addf2015-10-02 07:49:05 -0700682 return texture.detach();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000683}
684
685////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000686// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000687// (as opposed to canvas) coordinates
joshualitt9853cce2014-11-17 14:22:48 -0800688bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt,
689 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700690 GrReducedClip::InitialState initialState,
691 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000692 const SkIRect& clipSpaceIBounds,
693 const SkIPoint& clipSpaceToStencilOffset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000694 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
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 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000874 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000875 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000876 fCurrClipMaskType = kStencil_ClipMaskType;
joshualitt7a6184f2014-10-29 18:29:27 -0700877 fClipMode = kRespectClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000878 return true;
879}
880
bsalomon@google.com411dad02012-06-05 20:24:20 +0000881// mapping of clip-respecting stencil funcs to normal stencil funcs
882// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000883static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000884 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
885 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
886 // In the Clip Funcs
887 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
888 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
889 kLess_StencilFunc, // kLessIfInClip_StencilFunc
890 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
891 // Special in the clip func that forces user's ref to be 0.
892 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
893 // make ref 0 and do normal nequal.
894 },
895 {// Stencil-Clipping is ENABLED
896 // In the Clip Funcs
897 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
898 // eq stencil clip bit, mask
899 // out user bits.
900
901 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
902 // add stencil bit to mask and ref
903
904 kLess_StencilFunc, // kLessIfInClip_StencilFunc
905 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
906 // for both of these we can add
907 // the clip bit to the mask and
908 // ref and compare as normal
909 // Special in the clip func that forces user's ref to be 0.
910 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
911 // make ref have only the clip bit set
912 // and make comparison be less
913 // 10..0 < 1..user_bits..
914 }
915};
916
bsalomon@google.coma3201942012-06-21 19:58:20 +0000917namespace {
918// Sets the settings to clip against the stencil buffer clip while ignoring the
919// client bits.
920const GrStencilSettings& basic_apply_stencil_clip_settings() {
921 // stencil settings to use when clip is in stencil
922 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
923 kKeep_StencilOp,
924 kKeep_StencilOp,
925 kAlwaysIfInClip_StencilFunc,
926 0x0000,
927 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000928 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000929 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
930}
931}
932
joshualitt5e6ba212015-07-13 07:35:05 -0700933void GrClipMaskManager::setPipelineBuilderStencil(const GrPipelineBuilder& pipelineBuilder,
egdaniel8dd688b2015-01-22 10:16:09 -0800934 GrPipelineBuilder::AutoRestoreStencil* ars) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000935 // We make two copies of the StencilSettings here (except in the early
936 // exit scenario. One copy from draw state to the stack var. Then another
937 // from the stack var to the gpu. We could make this class hold a ptr to
938 // GrGpu's fStencilSettings and eliminate the stack copy here.
939
bsalomon@google.coma3201942012-06-21 19:58:20 +0000940 // use stencil for clipping if clipping is enabled and the clip
941 // has been written into the stencil.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000942 GrStencilSettings settings;
joshualitt9853cce2014-11-17 14:22:48 -0800943
bsalomon@google.coma3201942012-06-21 19:58:20 +0000944 // The GrGpu client may not be using the stencil buffer but we may need to
945 // enable it in order to respect a stencil clip.
joshualitt5e6ba212015-07-13 07:35:05 -0700946 if (pipelineBuilder.getStencil().isDisabled()) {
joshualitt7a6184f2014-10-29 18:29:27 -0700947 if (GrClipMaskManager::kRespectClip_StencilClipMode == fClipMode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000948 settings = basic_apply_stencil_clip_settings();
949 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000950 return;
951 }
952 } else {
joshualitt5e6ba212015-07-13 07:35:05 -0700953 settings = pipelineBuilder.getStencil();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000954 }
955
bsalomon@google.coma3201942012-06-21 19:58:20 +0000956 int stencilBits = 0;
joshualitt5e6ba212015-07-13 07:35:05 -0700957 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700958 GrStencilAttachment* stencilAttachment =
959 fDrawTarget->cmmAccess().resourceProvider()->attachStencilAttachment(rt);
egdaniel8dc7c3a2015-04-16 11:22:42 -0700960 if (stencilAttachment) {
961 stencilBits = stencilAttachment->bits();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000962 }
963
bsalomonb3b9aec2015-09-10 11:16:35 -0700964 SkASSERT(fDrawTarget->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
965 SkASSERT(fDrawTarget->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
joshualitt7a6184f2014-10-29 18:29:27 -0700966 this->adjustStencilParams(&settings, fClipMode, stencilBits);
joshualitt5e6ba212015-07-13 07:35:05 -0700967 ars->set(&pipelineBuilder);
968 ars->setStencil(settings);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000969}
970
971void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
972 StencilClipMode mode,
973 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000974 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000975
976 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000977 // We assume that this clip manager itself is drawing to the GrGpu and
978 // has already setup the correct values.
979 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000980 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000981
bsalomon@google.com411dad02012-06-05 20:24:20 +0000982 unsigned int clipBit = (1 << (stencilBitCnt - 1));
983 unsigned int userBits = clipBit - 1;
984
bsalomon@google.coma3201942012-06-21 19:58:20 +0000985 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomonb3b9aec2015-09-10 11:16:35 -0700986 bool twoSided = fDrawTarget->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000987
bsalomon@google.coma3201942012-06-21 19:58:20 +0000988 bool finished = false;
989 while (!finished) {
990 GrStencilFunc func = settings->func(face);
991 uint16_t writeMask = settings->writeMask(face);
992 uint16_t funcMask = settings->funcMask(face);
993 uint16_t funcRef = settings->funcRef(face);
994
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000995 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000996
997 writeMask &= userBits;
998
999 if (func >= kBasicStencilFuncCount) {
1000 int respectClip = kRespectClip_StencilClipMode == mode;
1001 if (respectClip) {
1002 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001003 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001004 switch (func) {
1005 case kAlwaysIfInClip_StencilFunc:
1006 funcMask = clipBit;
1007 funcRef = clipBit;
1008 break;
1009 case kEqualIfInClip_StencilFunc:
1010 case kLessIfInClip_StencilFunc:
1011 case kLEqualIfInClip_StencilFunc:
1012 funcMask = (funcMask & userBits) | clipBit;
1013 funcRef = (funcRef & userBits) | clipBit;
1014 break;
1015 case kNonZeroIfInClip_StencilFunc:
1016 funcMask = (funcMask & userBits) | clipBit;
1017 funcRef = clipBit;
1018 break;
1019 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001020 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +00001021 }
1022 } else {
1023 funcMask &= userBits;
1024 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001025 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001026 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001027 gSpecialToBasicStencilFunc[respectClip];
1028 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001029 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001030 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001031 funcMask &= userBits;
1032 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001033 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001034
1035 settings->setFunc(face, func);
1036 settings->setWriteMask(face, writeMask);
1037 settings->setFuncMask(face, funcMask);
1038 settings->setFuncRef(face, funcRef);
1039
1040 if (GrStencilSettings::kFront_Face == face) {
1041 face = GrStencilSettings::kBack_Face;
1042 finished = !twoSided;
1043 } else {
1044 finished = true;
1045 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001046 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001047 if (!twoSided) {
1048 settings->copyFrontSettingsToBack();
1049 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001050}
1051
1052////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +00001053GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001054 GrReducedClip::InitialState initialState,
1055 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -08001056 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001057 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001058 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon473addf2015-10-02 07:49:05 -07001059 GrUniqueKey key;
1060 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
1061 GrResourceProvider* resourceProvider = fDrawTarget->cmmAccess().resourceProvider();
1062 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
1063 return texture;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001064 }
1065
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001066 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1067 // the top left corner of the resulting rect to the top left of the texture.
1068 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1069
robertphillips@google.com2c756812012-05-22 20:28:23 +00001070 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001071
joshualitt8059eb92014-12-29 15:10:07 -08001072 // Set the matrix so that rendered clip elements are transformed to mask space from clip
1073 // space.
1074 SkMatrix translate;
1075 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -08001076
joshualitt8059eb92014-12-29 15:10:07 -08001077 helper.init(maskSpaceIBounds, &translate, false);
tfarinabf54e492014-10-23 17:47:18 -07001078 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001079 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001080
tfarinabf54e492014-10-23 17:47:18 -07001081 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001082 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001083 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001084
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001085 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1086 // Intersect and reverse difference require modifying pixels outside of the geometry
1087 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1088 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1089 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001090 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001091 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001092 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001093 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001094 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001095 SkPath clipPath;
1096 element->asPath(&clipPath);
1097 clipPath.toggleInverseFillType();
1098 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001099 continue;
1100 }
1101
1102 // The other ops (union, xor, diff) only affect pixels inside
1103 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001104 if (Element::kRect_Type == element->getType()) {
1105 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1106 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001107 SkPath path;
1108 element->asPath(&path);
1109 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001110 }
1111 }
1112
krajcevskiad1dc582014-06-10 15:06:47 -07001113 // Allocate clip mask texture
bsalomon473addf2015-10-02 07:49:05 -07001114 GrTexture* result = this->createCachedMask(clipSpaceIBounds.width(), clipSpaceIBounds.height(),
1115 key, false);
halcanary96fcdcc2015-08-27 07:41:13 -07001116 if (nullptr == result) {
halcanary96fcdcc2015-08-27 07:41:13 -07001117 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -07001118 }
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001119 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001120
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001121 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001122 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001123}
1124
robertphillips@google.comf294b772012-04-27 14:29:26 +00001125////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001126
egdaniel8dc7c3a2015-04-16 11:22:42 -07001127void GrClipMaskManager::adjustPathStencilParams(const GrStencilAttachment* stencilAttachment,
joshualitt9853cce2014-11-17 14:22:48 -08001128 GrStencilSettings* settings) {
egdaniel8dc7c3a2015-04-16 11:22:42 -07001129 if (stencilAttachment) {
1130 int stencilBits = stencilAttachment->bits();
joshualitt7a6184f2014-10-29 18:29:27 -07001131 this->adjustStencilParams(settings, fClipMode, stencilBits);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001132 }
1133}