blob: 078895d7e3e8f216854b62099909df5fc22896a4 [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrClipMaskManager.h"
robertphillips@google.comfa662942012-05-17 12:20:22 +00009#include "GrAAConvexPathRenderer.h"
10#include "GrAAHairLinePathRenderer.h"
jvanverth@google.combfe2b9d2013-09-06 16:57:29 +000011#include "GrAARectRenderer.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000012#include "GrDrawTargetCaps.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"
egdaniel8dc7c3a2015-04-16 11:22:42 -070017#include "GrStencilAttachment.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000018#include "GrSWMaskHelper.h"
joshualitt3a0cfeb2014-10-27 07:38:01 -070019#include "SkRasterClip.h"
20#include "SkStrokeRec.h"
21#include "SkTLazy.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080022#include "effects/GrConvexPolyEffect.h"
egdaniel95131432014-12-09 11:15:43 -080023#include "effects/GrPorterDuffXferProcessor.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080024#include "effects/GrRRectEffect.h"
egdaniel95131432014-12-09 11:15:43 -080025#include "effects/GrTextureDomain.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000026
bsalomon@google.com8182fa02012-12-04 14:06:06 +000027typedef SkClipStack::Element Element;
bsalomon@google.com51a62862012-11-26 21:19:43 +000028
29////////////////////////////////////////////////////////////////////////////////
robertphillips@google.come79f3202014-02-11 16:30:21 +000030namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000031// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000032// stage matrix this also alters the vertex layout
joshualitt642429e2015-02-26 08:47:52 -080033void setup_drawstate_aaclip(GrPipelineBuilder* pipelineBuilder,
34 GrTexture* result,
bsalomon6be6f7c2015-02-26 13:05:21 -080035 GrPipelineBuilder::AutoRestoreFragmentProcessors* arfp,
joshualitt642429e2015-02-26 08:47:52 -080036 const SkIRect &devBound) {
bsalomon6be6f7c2015-02-26 13:05:21 -080037 SkASSERT(pipelineBuilder && arfp);
38 arfp->set(pipelineBuilder);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000039
bsalomon@google.comb9086a02012-11-01 18:02:54 +000040 SkMatrix mat;
bsalomon309d4d52014-12-18 10:17:44 -080041 // We use device coords to compute the texture coordinates. We set our matrix to be a
42 // translation to the devBound, and then a scaling matrix to normalized coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000043 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000044 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000045 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000046
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000047 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000048 // This could be a long-lived effect that is cached with the alpha-mask.
egdaniel8dd688b2015-01-22 10:16:09 -080049 pipelineBuilder->addCoverageProcessor(
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000050 GrTextureDomainEffect::Create(result,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000051 mat,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000052 GrTextureDomain::MakeTexelDomain(result, domainTexels),
53 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +000054 GrTextureParams::kNone_FilterMode,
bsalomon309d4d52014-12-18 10:17:44 -080055 kDevice_GrCoordSet))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000056}
57
robertphillips@google.come79f3202014-02-11 16:30:21 +000058bool path_needs_SW_renderer(GrContext* context,
joshualitt9853cce2014-11-17 14:22:48 -080059 const GrDrawTarget* gpu,
egdaniel8dd688b2015-01-22 10:16:09 -080060 const GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -080061 const SkMatrix& viewMatrix,
robertphillips@google.come79f3202014-02-11 16:30:21 +000062 const SkPath& origPath,
63 const SkStrokeRec& stroke,
64 bool doAA) {
65 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
66 SkTCopyOnFirstWrite<SkPath> path(origPath);
67 if (path->isInverseFillType()) {
68 path.writable()->toggleInverseFillType();
69 }
70 // last (false) parameter disallows use of the SW path renderer
bsalomon@google.com45a15f52012-12-10 19:10:17 +000071 GrPathRendererChain::DrawType type = doAA ?
72 GrPathRendererChain::kColorAntiAlias_DrawType :
73 GrPathRendererChain::kColor_DrawType;
74
egdaniel8dd688b2015-01-22 10:16:09 -080075 return NULL == context->getPathRenderer(gpu, pipelineBuilder, viewMatrix, *path, stroke,
76 false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +000077}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000078}
79
robertphillips@google.comfa662942012-05-17 12:20:22 +000080/*
81 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
82 * will be used on any element. If so, it returns true to indicate that the
83 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
84 */
egdaniel8dd688b2015-01-22 10:16:09 -080085bool GrClipMaskManager::useSWOnlyPath(const GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -080086 const SkVector& clipToMaskOffset,
joshualitt9853cce2014-11-17 14:22:48 -080087 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000088 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000089 // a clip gets complex enough it can just be done in SW regardless
90 // of whether it would invoke the GrSoftwarePathRenderer.
sugoi@google.com5f74cf82012-12-17 21:16:45 +000091 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000092
joshualitt8059eb92014-12-29 15:10:07 -080093 // Set the matrix so that rendered clip elements are transformed to mask space from clip
94 // space.
95 SkMatrix translate;
96 translate.setTranslate(clipToMaskOffset);
97
tfarinabf54e492014-10-23 17:47:18 -070098 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000099 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000100 // rects can always be drawn directly w/o using the software path
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000101 // Skip rrects once we're drawing them directly.
102 if (Element::kRect_Type != element->getType()) {
103 SkPath path;
104 element->asPath(&path);
egdaniel8dd688b2015-01-22 10:16:09 -0800105 if (path_needs_SW_renderer(this->getContext(), fClipTarget, pipelineBuilder, translate,
joshualitt8059eb92014-12-29 15:10:07 -0800106 path, stroke, element->isAA())) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000107 return true;
108 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000109 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000110 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000111 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000112}
113
egdaniel8dd688b2015-01-22 10:16:09 -0800114bool GrClipMaskManager::installClipEffects(GrPipelineBuilder* pipelineBuilder,
bsalomon6be6f7c2015-02-26 13:05:21 -0800115 GrPipelineBuilder::AutoRestoreFragmentProcessors* arfp,
joshualitt9853cce2014-11-17 14:22:48 -0800116 const GrReducedClip::ElementList& elements,
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000117 const SkVector& clipToRTOffset,
mtklein217daa72014-07-02 12:55:21 -0700118 const SkRect* drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000119 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700120 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000121 boundsInClipSpace = *drawBounds;
122 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
123 }
124
bsalomon6be6f7c2015-02-26 13:05:21 -0800125 arfp->set(pipelineBuilder);
egdaniel8dd688b2015-01-22 10:16:09 -0800126 GrRenderTarget* rt = pipelineBuilder->getRenderTarget();
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()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000159 if (rt->isMultisampled()) {
mtklein217daa72014-07-02 12:55:21 -0700160 // Coverage based AA clips don't place nicely with MSAA.
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000161 failed = true;
162 break;
163 }
joshualittb0a8a372014-09-23 09:50:21 -0700164 edgeType =
165 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000166 } else {
joshualittb0a8a372014-09-23 09:50:21 -0700167 edgeType =
bsalomon96e02a82015-03-06 07:13:01 -0800168invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000169 }
joshualittb0a8a372014-09-23 09:50:21 -0700170 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000171 switch (iter.get()->getType()) {
172 case SkClipStack::Element::kPath_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700173 fp.reset(GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
joshualitt642429e2015-02-26 08:47:52 -0800174 &clipToRTOffset));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000175 break;
176 case SkClipStack::Element::kRRect_Type: {
177 SkRRect rrect = iter.get()->getRRect();
178 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
joshualittb0a8a372014-09-23 09:50:21 -0700179 fp.reset(GrRRectEffect::Create(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000180 break;
181 }
182 case SkClipStack::Element::kRect_Type: {
183 SkRect rect = iter.get()->getRect();
184 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
joshualittb0a8a372014-09-23 09:50:21 -0700185 fp.reset(GrConvexPolyEffect::Create(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000186 break;
187 }
188 default:
189 break;
190 }
joshualittb0a8a372014-09-23 09:50:21 -0700191 if (fp) {
egdaniel8dd688b2015-01-22 10:16:09 -0800192 pipelineBuilder->addCoverageProcessor(fp);
mtklein217daa72014-07-02 12:55:21 -0700193 } else {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000194 failed = true;
195 break;
196 }
197 }
mtklein217daa72014-07-02 12:55:21 -0700198 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000199 }
200
201 if (failed) {
bsalomon6be6f7c2015-02-26 13:05:21 -0800202 arfp->set(NULL);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000203 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000204 return !failed;
205}
206
robertphillips@google.comf294b772012-04-27 14:29:26 +0000207////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000208// sort out what kind of clip mask needs to be created: alpha, stencil,
209// scissor, or entirely software
egdaniel8dd688b2015-01-22 10:16:09 -0800210bool GrClipMaskManager::setupClipping(GrPipelineBuilder* pipelineBuilder,
bsalomon6be6f7c2015-02-26 13:05:21 -0800211 GrPipelineBuilder::AutoRestoreFragmentProcessors* arfp,
egdaniel8dd688b2015-01-22 10:16:09 -0800212 GrPipelineBuilder::AutoRestoreStencil* ars,
bsalomon3e791242014-12-17 13:43:13 -0800213 GrScissorState* scissorState,
joshualitt9853cce2014-11-17 14:22:48 -0800214 const SkRect* devBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000215 fCurrClipMaskType = kNone_ClipMaskType;
joshualitt7a6184f2014-10-29 18:29:27 -0700216 if (kRespectClip_StencilClipMode == fClipMode) {
217 fClipMode = kIgnoreClip_StencilClipMode;
218 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000219
tfarinabf54e492014-10-23 17:47:18 -0700220 GrReducedClip::ElementList elements(16);
brucedawson71d7f7f2015-02-26 13:28:53 -0800221 int32_t genID = 0;
222 GrReducedClip::InitialState initialState = GrReducedClip::kAllIn_InitialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000223 SkIRect clipSpaceIBounds;
brucedawson71d7f7f2015-02-26 13:28:53 -0800224 bool requiresAA = false;
egdaniel8dd688b2015-01-22 10:16:09 -0800225 GrRenderTarget* rt = pipelineBuilder->getRenderTarget();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000226
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000227 // GrDrawTarget should have filtered this for us
bsalomon49f085d2014-09-05 13:34:00 -0700228 SkASSERT(rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000229
joshualitt44701df2015-02-23 14:44:57 -0800230 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
231 const GrClip& clip = pipelineBuilder->clip();
bsalomon96e02a82015-03-06 07:13:01 -0800232 if (clip.isWideOpen(clipSpaceRTIBounds)) {
egdaniel8dd688b2015-01-22 10:16:09 -0800233 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000234 return true;
235 }
236
bsalomon96e02a82015-03-06 07:13:01 -0800237 // The clip mask manager always draws with a single IRect so we special case that logic here
238 // Image filters just use a rect, so we also special case that logic
239 switch (clip.clipType()) {
240 case GrClip::kWideOpen_ClipType:
241 SkFAIL("Should have caught this with clip.isWideOpen()");
242 return true;
bsalomon9ce30e12015-03-06 08:42:34 -0800243 case GrClip::kIRect_ClipType: {
244 SkIRect scissor = clip.irect();
245 if (scissor.intersect(clipSpaceRTIBounds)) {
246 scissorState->set(scissor);
247 this->setPipelineBuilderStencil(pipelineBuilder, ars);
248 return true;
249 }
250 return false;
251 }
bsalomon96e02a82015-03-06 07:13:01 -0800252 case GrClip::kClipStack_ClipType: {
253 clipSpaceRTIBounds.offset(clip.origin());
254 GrReducedClip::ReduceClipStack(*clip.clipStack(),
255 clipSpaceRTIBounds,
256 &elements,
257 &genID,
258 &initialState,
259 &clipSpaceIBounds,
260 &requiresAA);
261 if (elements.isEmpty()) {
262 if (GrReducedClip::kAllIn_InitialState == initialState) {
263 if (clipSpaceIBounds == clipSpaceRTIBounds) {
264 this->setPipelineBuilderStencil(pipelineBuilder, ars);
265 return true;
266 }
267 } else {
268 return false;
269 }
270 }
271 } break;
272 }
273
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000274 // An element count of 4 was chosen because of the common pattern in Blink of:
275 // isect RR
276 // diff RR
277 // isect convex_poly
278 // isect convex_poly
279 // when drawing rounded div borders. This could probably be tuned based on a
280 // configuration's relative costs of switching RTs to generate a mask vs
281 // longer shaders.
282 if (elements.count() <= 4) {
joshualitt44701df2015-02-23 14:44:57 -0800283 SkVector clipToRTOffset = { SkIntToScalar(-clip.origin().fX),
284 SkIntToScalar(-clip.origin().fY) };
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000285 if (elements.isEmpty() ||
bsalomon55f0b182015-03-05 17:43:09 -0800286 (requiresAA && this->installClipEffects(pipelineBuilder, arfp, elements, clipToRTOffset,
287 devBounds))) {
mtklein217daa72014-07-02 12:55:21 -0700288 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
joshualitt44701df2015-02-23 14:44:57 -0800289 scissorSpaceIBounds.offset(-clip.origin());
mtklein217daa72014-07-02 12:55:21 -0700290 if (NULL == devBounds ||
291 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
joshualitt77b13072014-10-27 14:51:01 -0700292 scissorState->set(scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000293 }
egdaniel8dd688b2015-01-22 10:16:09 -0800294 this->setPipelineBuilderStencil(pipelineBuilder, ars);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000295 return true;
296 }
297 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000298
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000299 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000300 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000301 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000302
joshualitt8059eb92014-12-29 15:10:07 -0800303 // The top-left of the mask corresponds to the top-left corner of the bounds.
304 SkVector clipToMaskOffset = {
305 SkIntToScalar(-clipSpaceIBounds.fLeft),
306 SkIntToScalar(-clipSpaceIBounds.fTop)
307 };
308
egdaniel8dd688b2015-01-22 10:16:09 -0800309 if (this->useSWOnlyPath(pipelineBuilder, clipToMaskOffset, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000310 // The clip geometry is complex enough that it will be more efficient to create it
311 // entirely in software
312 result = this->createSoftwareClipMask(genID,
313 initialState,
314 elements,
joshualitt8059eb92014-12-29 15:10:07 -0800315 clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000316 clipSpaceIBounds);
317 } else {
318 result = this->createAlphaClipMask(genID,
319 initialState,
320 elements,
joshualitt8059eb92014-12-29 15:10:07 -0800321 clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000322 clipSpaceIBounds);
323 }
324
bsalomon49f085d2014-09-05 13:34:00 -0700325 if (result) {
bsalomon6be6f7c2015-02-26 13:05:21 -0800326 arfp->set(pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000327 // The mask's top left coord should be pinned to the rounded-out top left corner of
328 // clipSpace bounds. We determine the mask's position WRT to the render target here.
329 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
joshualitt44701df2015-02-23 14:44:57 -0800330 rtSpaceMaskBounds.offset(-clip.origin());
bsalomon6be6f7c2015-02-26 13:05:21 -0800331 setup_drawstate_aaclip(pipelineBuilder, result, arfp, rtSpaceMaskBounds);
egdaniel8dd688b2015-01-22 10:16:09 -0800332 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000333 return true;
334 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000335 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000336 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000337
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000338 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
339 // be created. In either case, free up the texture in the anti-aliased mask cache.
340 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
341 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
342 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000343 fAACache.reset();
344
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000345 // use the stencil clip if we can't represent the clip as a rectangle.
joshualitt44701df2015-02-23 14:44:57 -0800346 SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin();
joshualitt9853cce2014-11-17 14:22:48 -0800347 this->createStencilClipMask(rt,
348 genID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000349 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000350 elements,
351 clipSpaceIBounds,
352 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000353
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000354 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
355 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
356 // use both stencil and scissor test to the bounds for the final draw.
357 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
358 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
joshualitt77b13072014-10-27 14:51:01 -0700359 scissorState->set(scissorSpaceIBounds);
egdaniel8dd688b2015-01-22 10:16:09 -0800360 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000361 return true;
362}
363
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000364namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000365////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800366// Set a coverage drawing XPF on the pipelineBuilder for the given op and invertCoverage mode
367void set_coverage_drawing_xpf(SkRegion::Op op, bool invertCoverage,
368 GrPipelineBuilder* pipelineBuilder) {
egdaniel87509242014-12-17 13:37:13 -0800369 SkASSERT(op <= SkRegion::kLastOp);
egdaniel8dd688b2015-01-22 10:16:09 -0800370 pipelineBuilder->setCoverageSetOpXPFactory(op, invertCoverage);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000371}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000372}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000373
374////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800375bool GrClipMaskManager::drawElement(GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800376 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800377 GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000378 const SkClipStack::Element* element,
379 GrPathRenderer* pr) {
joshualitt9853cce2014-11-17 14:22:48 -0800380 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000381
egdaniel8dd688b2015-01-22 10:16:09 -0800382 pipelineBuilder->setRenderTarget(target->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000383
egdaniel87509242014-12-17 13:37:13 -0800384 // The color we use to draw does not matter since we will always be using a GrCoverageSetOpXP
385 // which ignores color.
386 GrColor color = GrColor_WHITE;
387
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000388 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000389 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000390 case Element::kEmpty_Type:
391 SkDEBUGFAIL("Should never get here with an empty element.");
392 break;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000393 case Element::kRect_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700394 // TODO: Do rects directly to the accumulator using a aa-rect GrProcessor that covers
395 // the entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000396 if (element->isAA()) {
joshualitt8059eb92014-12-29 15:10:07 -0800397 SkRect devRect = element->getRect();
398 viewMatrix.mapRect(&devRect);
joshualitt329bf482014-10-29 12:31:28 -0700399 this->getContext()->getAARectRenderer()->fillAARect(fClipTarget,
egdaniel8dd688b2015-01-22 10:16:09 -0800400 pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -0800401 color,
joshualitt8059eb92014-12-29 15:10:07 -0800402 viewMatrix,
joshualitta58fe352014-10-27 08:39:00 -0700403 element->getRect(),
joshualitt8059eb92014-12-29 15:10:07 -0800404 devRect);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000405 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800406 fClipTarget->drawSimpleRect(pipelineBuilder, color, viewMatrix, 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 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000416 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000417 if (NULL == pr) {
418 GrPathRendererChain::DrawType type;
419 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
420 GrPathRendererChain::kColor_DrawType;
egdaniel8dd688b2015-01-22 10:16:09 -0800421 pr = this->getContext()->getPathRenderer(fClipTarget, pipelineBuilder, viewMatrix,
422 path, stroke, false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000423 }
424 if (NULL == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000425 return false;
426 }
joshualitt9853cce2014-11-17 14:22:48 -0800427
egdaniel8dd688b2015-01-22 10:16:09 -0800428 pr->drawPath(fClipTarget, pipelineBuilder, color, viewMatrix, path, stroke,
429 element->isAA());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000430 break;
431 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000432 }
433 return true;
434}
435
egdaniel8dd688b2015-01-22 10:16:09 -0800436bool GrClipMaskManager::canStencilAndDrawElement(GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800437 GrTexture* target,
438 GrPathRenderer** pr,
439 const SkClipStack::Element* element) {
egdaniel8dd688b2015-01-22 10:16:09 -0800440 pipelineBuilder->setRenderTarget(target->asRenderTarget());
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000441
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000442 if (Element::kRect_Type == element->getType()) {
443 return true;
444 } else {
445 // We shouldn't get here with an empty clip element.
446 SkASSERT(Element::kEmpty_Type != element->getType());
447 SkPath path;
448 element->asPath(&path);
449 if (path.isInverseFillType()) {
450 path.toggleInverseFillType();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000451 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000452 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
453 GrPathRendererChain::DrawType type = element->isAA() ?
454 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
455 GrPathRendererChain::kStencilAndColor_DrawType;
egdaniel8dd688b2015-01-22 10:16:09 -0800456 *pr = this->getContext()->getPathRenderer(fClipTarget, pipelineBuilder, SkMatrix::I(), path,
joshualitt8059eb92014-12-29 15:10:07 -0800457 stroke, false, type);
bsalomon49f085d2014-09-05 13:34:00 -0700458 return SkToBool(*pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000459 }
460}
461
egdaniel8dd688b2015-01-22 10:16:09 -0800462void GrClipMaskManager::mergeMask(GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800463 GrTexture* dstMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000464 GrTexture* srcMask,
465 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000466 const SkIRect& dstBound,
467 const SkIRect& srcBound) {
egdaniel8dd688b2015-01-22 10:16:09 -0800468 pipelineBuilder->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000469
egdaniel87509242014-12-17 13:37:13 -0800470 // We want to invert the coverage here
egdaniel8dd688b2015-01-22 10:16:09 -0800471 set_coverage_drawing_xpf(op, false, pipelineBuilder);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000472
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000473 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000474 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000475
egdaniel8dd688b2015-01-22 10:16:09 -0800476 pipelineBuilder->addCoverageProcessor(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000477 GrTextureDomainEffect::Create(srcMask,
478 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000479 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
480 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000481 GrTextureParams::kNone_FilterMode))->unref();
joshualitt73bb4562015-03-25 07:16:21 -0700482 // We need this AGP until everything is in GrBatch
483 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
484
egdaniel87509242014-12-17 13:37:13 -0800485 // The color passed in here does not matter since the coverageSetOpXP won't read it.
joshualitt44701df2015-02-23 14:44:57 -0800486 fClipTarget->drawSimpleRect(pipelineBuilder,
487 GrColor_WHITE,
488 SkMatrix::I(),
egdaniel8dd688b2015-01-22 10:16:09 -0800489 SkRect::Make(dstBound));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000490}
491
bsalomon427cf282014-10-16 13:41:43 -0700492GrTexture* GrClipMaskManager::createTempMask(int width, int height) {
bsalomonf2703d82014-10-28 14:33:06 -0700493 GrSurfaceDesc desc;
bsalomon3f490a02014-12-18 06:20:52 -0800494 desc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000495 desc.fWidth = width;
496 desc.fHeight = height;
bsalomon51d1f7e2014-12-22 08:40:49 -0800497 if (this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
498 desc.fConfig = kAlpha_8_GrPixelConfig;
499 } else {
500 desc.fConfig = kRGBA_8888_GrPixelConfig;
501 }
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000502
joshualitt329bf482014-10-29 12:31:28 -0700503 return this->getContext()->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000504}
505
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000506////////////////////////////////////////////////////////////////////////////////
krajcevskiad1dc582014-06-10 15:06:47 -0700507// Return the texture currently in the cache if it exists. Otherwise, return NULL
508GrTexture* GrClipMaskManager::getCachedMaskTexture(int32_t elementsGenID,
509 const SkIRect& clipSpaceIBounds) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000510 bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000511 if (!cached) {
krajcevskiad1dc582014-06-10 15:06:47 -0700512 return NULL;
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000513 }
514
krajcevskiad1dc582014-06-10 15:06:47 -0700515 return fAACache.getLastMask();
516}
517
518////////////////////////////////////////////////////////////////////////////////
519// Allocate a texture in the texture cache. This function returns the texture
520// allocated (or NULL on error).
521GrTexture* GrClipMaskManager::allocMaskTexture(int32_t elementsGenID,
522 const SkIRect& clipSpaceIBounds,
523 bool willUpload) {
524 // Since we are setting up the cache we should free up the
525 // currently cached mask so it can be reused.
526 fAACache.reset();
527
bsalomonf2703d82014-10-28 14:33:06 -0700528 GrSurfaceDesc desc;
529 desc.fFlags = willUpload ? kNone_GrSurfaceFlags : kRenderTarget_GrSurfaceFlag;
krajcevskiad1dc582014-06-10 15:06:47 -0700530 desc.fWidth = clipSpaceIBounds.width();
531 desc.fHeight = clipSpaceIBounds.height();
532 desc.fConfig = kRGBA_8888_GrPixelConfig;
533 if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
534 // We would always like A8 but it isn't supported on all platforms
535 desc.fConfig = kAlpha_8_GrPixelConfig;
536 }
537
538 fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds);
539 return fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000540}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000541
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000542////////////////////////////////////////////////////////////////////////////////
543// Create a 8-bit clip mask in alpha
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000544GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700545 GrReducedClip::InitialState initialState,
546 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800547 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000548 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000549 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000550
krajcevskiad1dc582014-06-10 15:06:47 -0700551 // First, check for cached texture
552 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -0700553 if (result) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000554 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000555 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000556 }
557
krajcevskiad1dc582014-06-10 15:06:47 -0700558 // There's no texture in the cache. Let's try to allocate it then.
559 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, false);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000560 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000561 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000562 return NULL;
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.
joshualitt329bf482014-10-29 12:31:28 -0700576 fClipTarget->clear(&maskSpaceIBounds,
577 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
578 true,
579 result->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);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000597 GrPathRenderer* pr = NULL;
egdaniel8dd688b2015-01-22 10:16:09 -0800598 bool useTemp = !this->canStencilAndDrawElement(&pipelineBuilder, result, &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) {
619 fAACache.reset();
620 return NULL;
621 }
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
joshualitt329bf482014-10-29 12:31:28 -0700625 fClipTarget->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.
633 dst = result;
634 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)) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000646 fAACache.reset();
647 return NULL;
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;
egdaniel8dd688b2015-01-22 10:16:09 -0800652 backgroundPipelineBuilder.setRenderTarget(result->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,
joshualitt9853cce2014-11-17 14:22:48 -0800657 result,
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;
egdaniel8dd688b2015-01-22 10:16:09 -0800664 backgroundPipelineBuilder.setRenderTarget(result->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
677 // We need this AGP until everything is in GrBatch
678 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
egdaniel87509242014-12-17 13:37:13 -0800679 // The color passed in here does not matter since the coverageSetOpXP won't read it.
egdaniel8dd688b2015-01-22 10:16:09 -0800680 fClipTarget->drawSimpleRect(&backgroundPipelineBuilder, GrColor_WHITE, translate,
joshualitt8059eb92014-12-29 15:10:07 -0800681 clipSpaceIBounds);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000682 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000683 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800684 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800685
robertphillips@google.come79f3202014-02-11 16:30:21 +0000686 // all the remaining ops can just be directly draw into the accumulation buffer
egdaniel8dd688b2015-01-22 10:16:09 -0800687 set_coverage_drawing_xpf(op, false, &pipelineBuilder);
egdaniel87509242014-12-17 13:37:13 -0800688 // The color passed in here does not matter since the coverageSetOpXP won't read it.
egdaniel8dd688b2015-01-22 10:16:09 -0800689 this->drawElement(&pipelineBuilder, translate, result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000690 }
691 }
692
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000693 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000694 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000695}
696
697////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000698// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000699// (as opposed to canvas) coordinates
joshualitt9853cce2014-11-17 14:22:48 -0800700bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt,
701 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700702 GrReducedClip::InitialState initialState,
703 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000704 const SkIRect& clipSpaceIBounds,
705 const SkIPoint& clipSpaceToStencilOffset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000706 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon49f085d2014-09-05 13:34:00 -0700707 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000708
egdaniel8dc7c3a2015-04-16 11:22:42 -0700709 GrStencilAttachment* stencilAttachment = rt->renderTargetPriv().attachStencilAttachment();
710 if (NULL == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000711 return false;
712 }
713
egdaniel8dc7c3a2015-04-16 11:22:42 -0700714 if (stencilAttachment->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
715 stencilAttachment->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000716 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
717 SkVector translate = {
718 SkIntToScalar(clipSpaceToStencilOffset.fX),
719 SkIntToScalar(clipSpaceToStencilOffset.fY)
720 };
joshualitt8059eb92014-12-29 15:10:07 -0800721 SkMatrix viewMatrix;
722 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000723
bsalomon@google.com9f131742012-12-13 20:43:56 +0000724 // We set the current clip to the bounds so that our recursive draws are scissored to them.
725 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
726 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
joshualitt44701df2015-02-23 14:44:57 -0800727 GrClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000728
egdaniel8dc7c3a2015-04-16 11:22:42 -0700729 int clipBit = stencilAttachment->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000730 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000731 clipBit = (1 << (clipBit-1));
732
joshualitt329bf482014-10-29 12:31:28 -0700733 fClipTarget->clearStencilClip(stencilSpaceIBounds,
734 GrReducedClip::kAllIn_InitialState == initialState,
735 rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000736
737 // walk through each clip element and perform its set op
738 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700739 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000740 const Element* element = iter.get();
joshualitt9853cce2014-11-17 14:22:48 -0800741
egdaniel8dd688b2015-01-22 10:16:09 -0800742 GrPipelineBuilder pipelineBuilder;
joshualitt44701df2015-02-23 14:44:57 -0800743 pipelineBuilder.setClip(clip);
egdaniel8dd688b2015-01-22 10:16:09 -0800744 pipelineBuilder.setRenderTarget(rt);
egdaniel080e6732014-12-22 07:35:52 -0800745
egdaniel8dd688b2015-01-22 10:16:09 -0800746 pipelineBuilder.setDisableColorXPFactory();
joshualitt9853cce2014-11-17 14:22:48 -0800747
748 // if the target is MSAA then we want MSAA enabled when the clip is soft
749 if (rt->isMultisampled()) {
egdaniel8dd688b2015-01-22 10:16:09 -0800750 pipelineBuilder.setState(GrPipelineBuilder::kHWAntialias_StateBit, element->isAA());
joshualitt9853cce2014-11-17 14:22:48 -0800751 }
752
tomhudson@google.com8afae612012-08-14 15:03:35 +0000753 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000754 // enabled at bottom of loop
joshualitt7a6184f2014-10-29 18:29:27 -0700755 fClipMode = kIgnoreClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000756
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000757 // This will be used to determine whether the clip shape can be rendered into the
758 // stencil with arbitrary stencil settings.
759 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000760
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000761 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000762 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000763
robertphillips@google.come79f3202014-02-11 16:30:21 +0000764 GrPathRenderer* pr = NULL;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000765 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000766 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000767 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000768 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000769 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000770 element->asPath(&clipPath);
771 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000772 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000773 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000774 }
joshualitt9853cce2014-11-17 14:22:48 -0800775 pr = this->getContext()->getPathRenderer(fClipTarget,
egdaniel8dd688b2015-01-22 10:16:09 -0800776 &pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800777 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800778 clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000779 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000780 false,
781 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000782 &stencilSupport);
783 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000784 return false;
785 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000786 }
787
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000788 int passes;
789 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
790
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000791 bool canRenderDirectToStencil =
792 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000793 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000794 // fill rule, and set operation can
795 // we render the element directly to
796 // stencil bit used for clipping.
797 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
798 canRenderDirectToStencil,
799 clipBit,
800 fillInverted,
801 &passes,
802 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000803
804 // draw the element to the client stencil bits if necessary
805 if (!canDrawDirectToClip) {
806 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000807 kIncClamp_StencilOp,
808 kIncClamp_StencilOp,
809 kAlways_StencilFunc,
810 0xffff,
811 0x0000,
812 0xffff);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000813 if (Element::kRect_Type == element->getType()) {
egdaniel8dd688b2015-01-22 10:16:09 -0800814 *pipelineBuilder.stencil() = gDrawToStencil;
joshualitt73bb4562015-03-25 07:16:21 -0700815
816 // We need this AGP until everything is in GrBatch
817 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
joshualitt44701df2015-02-23 14:44:57 -0800818 fClipTarget->drawSimpleRect(&pipelineBuilder,
819 GrColor_WHITE,
820 viewMatrix,
joshualitt8059eb92014-12-29 15:10:07 -0800821 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000822 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000823 if (!clipPath.isEmpty()) {
joshualitt9853cce2014-11-17 14:22:48 -0800824 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000825 if (canRenderDirectToStencil) {
egdaniel8dd688b2015-01-22 10:16:09 -0800826 *pipelineBuilder.stencil() = gDrawToStencil;
joshualitt44701df2015-02-23 14:44:57 -0800827 pr->drawPath(fClipTarget, &pipelineBuilder, GrColor_WHITE,
828 viewMatrix, clipPath, stroke, false);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000829 } else {
joshualitt44701df2015-02-23 14:44:57 -0800830 pr->stencilPath(fClipTarget, &pipelineBuilder, viewMatrix,
831 clipPath, stroke);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000832 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000833 }
834 }
835 }
836
837 // now we modify the clip bit by rendering either the clip
838 // element directly or a bounding rect of the entire clip.
joshualitt7a6184f2014-10-29 18:29:27 -0700839 fClipMode = kModifyClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000840 for (int p = 0; p < passes; ++p) {
egdaniel8dd688b2015-01-22 10:16:09 -0800841 GrPipelineBuilder pipelineBuilderCopy(pipelineBuilder);
842 *pipelineBuilderCopy.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
847 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
joshualitt44701df2015-02-23 14:44:57 -0800848 fClipTarget->drawSimpleRect(&pipelineBuilderCopy,
849 GrColor_WHITE,
850 viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800851 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000852 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800853 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
joshualitt44701df2015-02-23 14:44:57 -0800854 pr->drawPath(fClipTarget, &pipelineBuilderCopy, GrColor_WHITE,
855 viewMatrix, clipPath, stroke, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000856 }
857 } else {
joshualitt73bb4562015-03-25 07:16:21 -0700858 // We need this AGP until everything is in GrBatch
859 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
860
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000861 // The view matrix is setup to do clip space -> stencil space translation, so
862 // draw rect in clip space.
joshualitt44701df2015-02-23 14:44:57 -0800863 fClipTarget->drawSimpleRect(&pipelineBuilderCopy,
864 GrColor_WHITE,
865 viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800866 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000867 }
868 }
869 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000870 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000871 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000872 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000873 fCurrClipMaskType = kStencil_ClipMaskType;
joshualitt7a6184f2014-10-29 18:29:27 -0700874 fClipMode = kRespectClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000875 return true;
876}
877
bsalomon@google.com411dad02012-06-05 20:24:20 +0000878// mapping of clip-respecting stencil funcs to normal stencil funcs
879// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000880static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000881 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
882 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
883 // In the Clip Funcs
884 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
885 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
886 kLess_StencilFunc, // kLessIfInClip_StencilFunc
887 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
888 // Special in the clip func that forces user's ref to be 0.
889 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
890 // make ref 0 and do normal nequal.
891 },
892 {// Stencil-Clipping is ENABLED
893 // In the Clip Funcs
894 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
895 // eq stencil clip bit, mask
896 // out user bits.
897
898 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
899 // add stencil bit to mask and ref
900
901 kLess_StencilFunc, // kLessIfInClip_StencilFunc
902 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
903 // for both of these we can add
904 // the clip bit to the mask and
905 // ref and compare as normal
906 // Special in the clip func that forces user's ref to be 0.
907 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
908 // make ref have only the clip bit set
909 // and make comparison be less
910 // 10..0 < 1..user_bits..
911 }
912};
913
bsalomon@google.coma3201942012-06-21 19:58:20 +0000914namespace {
915// Sets the settings to clip against the stencil buffer clip while ignoring the
916// client bits.
917const GrStencilSettings& basic_apply_stencil_clip_settings() {
918 // stencil settings to use when clip is in stencil
919 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
920 kKeep_StencilOp,
921 kKeep_StencilOp,
922 kAlwaysIfInClip_StencilFunc,
923 0x0000,
924 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000925 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000926 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
927}
928}
929
egdaniel8dd688b2015-01-22 10:16:09 -0800930void GrClipMaskManager::setPipelineBuilderStencil(GrPipelineBuilder* pipelineBuilder,
931 GrPipelineBuilder::AutoRestoreStencil* ars) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000932 // We make two copies of the StencilSettings here (except in the early
933 // exit scenario. One copy from draw state to the stack var. Then another
934 // from the stack var to the gpu. We could make this class hold a ptr to
935 // GrGpu's fStencilSettings and eliminate the stack copy here.
936
bsalomon@google.coma3201942012-06-21 19:58:20 +0000937 // use stencil for clipping if clipping is enabled and the clip
938 // has been written into the stencil.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000939 GrStencilSettings settings;
joshualitt9853cce2014-11-17 14:22:48 -0800940
bsalomon@google.coma3201942012-06-21 19:58:20 +0000941 // The GrGpu client may not be using the stencil buffer but we may need to
942 // enable it in order to respect a stencil clip.
egdaniel8dd688b2015-01-22 10:16:09 -0800943 if (pipelineBuilder->getStencil().isDisabled()) {
joshualitt7a6184f2014-10-29 18:29:27 -0700944 if (GrClipMaskManager::kRespectClip_StencilClipMode == fClipMode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000945 settings = basic_apply_stencil_clip_settings();
946 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000947 return;
948 }
949 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800950 settings = pipelineBuilder->getStencil();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000951 }
952
bsalomon@google.coma3201942012-06-21 19:58:20 +0000953 int stencilBits = 0;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800954 GrRenderTarget* rt = pipelineBuilder->getRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700955 GrStencilAttachment* stencilAttachment = rt->renderTargetPriv().attachStencilAttachment();
956 if (stencilAttachment) {
957 stencilBits = stencilAttachment->bits();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000958 }
959
joshualitt329bf482014-10-29 12:31:28 -0700960 SkASSERT(fClipTarget->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
961 SkASSERT(fClipTarget->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
joshualitt7a6184f2014-10-29 18:29:27 -0700962 this->adjustStencilParams(&settings, fClipMode, stencilBits);
egdaniel8dd688b2015-01-22 10:16:09 -0800963 ars->set(pipelineBuilder);
964 pipelineBuilder->setStencil(settings);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000965}
966
967void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
968 StencilClipMode mode,
969 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000970 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000971
972 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000973 // We assume that this clip manager itself is drawing to the GrGpu and
974 // has already setup the correct values.
975 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000976 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000977
bsalomon@google.com411dad02012-06-05 20:24:20 +0000978 unsigned int clipBit = (1 << (stencilBitCnt - 1));
979 unsigned int userBits = clipBit - 1;
980
bsalomon@google.coma3201942012-06-21 19:58:20 +0000981 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
joshualitt329bf482014-10-29 12:31:28 -0700982 bool twoSided = fClipTarget->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000983
bsalomon@google.coma3201942012-06-21 19:58:20 +0000984 bool finished = false;
985 while (!finished) {
986 GrStencilFunc func = settings->func(face);
987 uint16_t writeMask = settings->writeMask(face);
988 uint16_t funcMask = settings->funcMask(face);
989 uint16_t funcRef = settings->funcRef(face);
990
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000991 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000992
993 writeMask &= userBits;
994
995 if (func >= kBasicStencilFuncCount) {
996 int respectClip = kRespectClip_StencilClipMode == mode;
997 if (respectClip) {
998 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000999 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001000 switch (func) {
1001 case kAlwaysIfInClip_StencilFunc:
1002 funcMask = clipBit;
1003 funcRef = clipBit;
1004 break;
1005 case kEqualIfInClip_StencilFunc:
1006 case kLessIfInClip_StencilFunc:
1007 case kLEqualIfInClip_StencilFunc:
1008 funcMask = (funcMask & userBits) | clipBit;
1009 funcRef = (funcRef & userBits) | clipBit;
1010 break;
1011 case kNonZeroIfInClip_StencilFunc:
1012 funcMask = (funcMask & userBits) | clipBit;
1013 funcRef = clipBit;
1014 break;
1015 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001016 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +00001017 }
1018 } else {
1019 funcMask &= userBits;
1020 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001021 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001022 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001023 gSpecialToBasicStencilFunc[respectClip];
1024 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001025 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001026 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001027 funcMask &= userBits;
1028 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001029 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001030
1031 settings->setFunc(face, func);
1032 settings->setWriteMask(face, writeMask);
1033 settings->setFuncMask(face, funcMask);
1034 settings->setFuncRef(face, funcRef);
1035
1036 if (GrStencilSettings::kFront_Face == face) {
1037 face = GrStencilSettings::kBack_Face;
1038 finished = !twoSided;
1039 } else {
1040 finished = true;
1041 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001042 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001043 if (!twoSided) {
1044 settings->copyFrontSettingsToBack();
1045 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001046}
1047
1048////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +00001049GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001050 GrReducedClip::InitialState initialState,
1051 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -08001052 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001053 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001054 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001055
krajcevskiad1dc582014-06-10 15:06:47 -07001056 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -07001057 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001058 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001059 }
1060
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001061 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1062 // the top left corner of the resulting rect to the top left of the texture.
1063 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1064
robertphillips@google.com2c756812012-05-22 20:28:23 +00001065 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001066
joshualitt8059eb92014-12-29 15:10:07 -08001067 // Set the matrix so that rendered clip elements are transformed to mask space from clip
1068 // space.
1069 SkMatrix translate;
1070 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -08001071
joshualitt8059eb92014-12-29 15:10:07 -08001072 helper.init(maskSpaceIBounds, &translate, false);
tfarinabf54e492014-10-23 17:47:18 -07001073 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001074 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001075
tfarinabf54e492014-10-23 17:47:18 -07001076 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001077 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001078 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001079
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001080 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1081 // Intersect and reverse difference require modifying pixels outside of the geometry
1082 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1083 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1084 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001085 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001086 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001087 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001088 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001089 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001090 SkPath clipPath;
1091 element->asPath(&clipPath);
1092 clipPath.toggleInverseFillType();
1093 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001094 continue;
1095 }
1096
1097 // The other ops (union, xor, diff) only affect pixels inside
1098 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001099 if (Element::kRect_Type == element->getType()) {
1100 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1101 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001102 SkPath path;
1103 element->asPath(&path);
1104 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001105 }
1106 }
1107
krajcevskiad1dc582014-06-10 15:06:47 -07001108 // Allocate clip mask texture
1109 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, true);
1110 if (NULL == result) {
1111 fAACache.reset();
1112 return NULL;
1113 }
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001114 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001115
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001116 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001117 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001118}
1119
robertphillips@google.comf294b772012-04-27 14:29:26 +00001120////////////////////////////////////////////////////////////////////////////////
bsalomonc8dc1f72014-08-21 13:02:13 -07001121void GrClipMaskManager::purgeResources() {
1122 fAACache.purgeResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001123}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001124
joshualitt329bf482014-10-29 12:31:28 -07001125void GrClipMaskManager::setClipTarget(GrClipTarget* clipTarget) {
1126 fClipTarget = clipTarget;
1127 fAACache.setContext(clipTarget->getContext());
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001128}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001129
egdaniel8dc7c3a2015-04-16 11:22:42 -07001130void GrClipMaskManager::adjustPathStencilParams(const GrStencilAttachment* stencilAttachment,
joshualitt9853cce2014-11-17 14:22:48 -08001131 GrStencilSettings* settings) {
egdaniel8dc7c3a2015-04-16 11:22:42 -07001132 if (stencilAttachment) {
1133 int stencilBits = stencilAttachment->bits();
joshualitt7a6184f2014-10-29 18:29:27 -07001134 this->adjustStencilParams(settings, fClipMode, stencilBits);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001135 }
1136}