blob: 66d817ec00c74f44c04300e4b546dd268a0bf4d8 [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"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000017#include "GrStencilBuffer.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();
egdaniel87509242014-12-17 13:37:13 -0800482 // The color passed in here does not matter since the coverageSetOpXP won't read it.
joshualitt44701df2015-02-23 14:44:57 -0800483 fClipTarget->drawSimpleRect(pipelineBuilder,
484 GrColor_WHITE,
485 SkMatrix::I(),
egdaniel8dd688b2015-01-22 10:16:09 -0800486 SkRect::Make(dstBound));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000487}
488
bsalomon427cf282014-10-16 13:41:43 -0700489GrTexture* GrClipMaskManager::createTempMask(int width, int height) {
bsalomonf2703d82014-10-28 14:33:06 -0700490 GrSurfaceDesc desc;
bsalomon3f490a02014-12-18 06:20:52 -0800491 desc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000492 desc.fWidth = width;
493 desc.fHeight = height;
bsalomon51d1f7e2014-12-22 08:40:49 -0800494 if (this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
495 desc.fConfig = kAlpha_8_GrPixelConfig;
496 } else {
497 desc.fConfig = kRGBA_8888_GrPixelConfig;
498 }
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000499
joshualitt329bf482014-10-29 12:31:28 -0700500 return this->getContext()->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000501}
502
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000503////////////////////////////////////////////////////////////////////////////////
krajcevskiad1dc582014-06-10 15:06:47 -0700504// Return the texture currently in the cache if it exists. Otherwise, return NULL
505GrTexture* GrClipMaskManager::getCachedMaskTexture(int32_t elementsGenID,
506 const SkIRect& clipSpaceIBounds) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000507 bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000508 if (!cached) {
krajcevskiad1dc582014-06-10 15:06:47 -0700509 return NULL;
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000510 }
511
krajcevskiad1dc582014-06-10 15:06:47 -0700512 return fAACache.getLastMask();
513}
514
515////////////////////////////////////////////////////////////////////////////////
516// Allocate a texture in the texture cache. This function returns the texture
517// allocated (or NULL on error).
518GrTexture* GrClipMaskManager::allocMaskTexture(int32_t elementsGenID,
519 const SkIRect& clipSpaceIBounds,
520 bool willUpload) {
521 // Since we are setting up the cache we should free up the
522 // currently cached mask so it can be reused.
523 fAACache.reset();
524
bsalomonf2703d82014-10-28 14:33:06 -0700525 GrSurfaceDesc desc;
526 desc.fFlags = willUpload ? kNone_GrSurfaceFlags : kRenderTarget_GrSurfaceFlag;
krajcevskiad1dc582014-06-10 15:06:47 -0700527 desc.fWidth = clipSpaceIBounds.width();
528 desc.fHeight = clipSpaceIBounds.height();
529 desc.fConfig = kRGBA_8888_GrPixelConfig;
530 if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
531 // We would always like A8 but it isn't supported on all platforms
532 desc.fConfig = kAlpha_8_GrPixelConfig;
533 }
534
535 fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds);
536 return fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000537}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000538
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000539////////////////////////////////////////////////////////////////////////////////
540// Create a 8-bit clip mask in alpha
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000541GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700542 GrReducedClip::InitialState initialState,
543 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800544 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000545 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000546 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000547
krajcevskiad1dc582014-06-10 15:06:47 -0700548 // First, check for cached texture
549 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -0700550 if (result) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000551 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000552 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000553 }
554
krajcevskiad1dc582014-06-10 15:06:47 -0700555 // There's no texture in the cache. Let's try to allocate it then.
556 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, false);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000557 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000558 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000559 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000560 }
561
joshualitt8059eb92014-12-29 15:10:07 -0800562 // Set the matrix so that rendered clip elements are transformed to mask space from clip
563 // space.
564 SkMatrix translate;
565 translate.setTranslate(clipToMaskOffset);
566
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000567 // The texture may be larger than necessary, this rect represents the part of the texture
568 // we populate with a rasterization of the clip.
569 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
570
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000571 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
572 // clear the part that we care about.
joshualitt329bf482014-10-29 12:31:28 -0700573 fClipTarget->clear(&maskSpaceIBounds,
574 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
575 true,
576 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000577
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000578 // When we use the stencil in the below loop it is important to have this clip installed.
579 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
580 // pass must not set values outside of this bounds or stencil values outside the rect won't be
581 // cleared.
joshualitt44701df2015-02-23 14:44:57 -0800582 GrClip clip(maskSpaceIBounds);
bsalomon427cf282014-10-16 13:41:43 -0700583 SkAutoTUnref<GrTexture> temp;
joshualitt9853cce2014-11-17 14:22:48 -0800584
robertphillips@google.comf294b772012-04-27 14:29:26 +0000585 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700586 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000587 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000588 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000589 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000590 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
egdaniel8dd688b2015-01-22 10:16:09 -0800591 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800592
joshualitt44701df2015-02-23 14:44:57 -0800593 pipelineBuilder.setClip(clip);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000594 GrPathRenderer* pr = NULL;
egdaniel8dd688b2015-01-22 10:16:09 -0800595 bool useTemp = !this->canStencilAndDrawElement(&pipelineBuilder, result, &pr, element);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000596 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000597 // 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 +0000598 // mask buffer can be substantially larger than the actually clip stack element. We
599 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000600 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000601 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000602
603 if (useTemp) {
604 if (invert) {
605 maskSpaceElementIBounds = maskSpaceIBounds;
606 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000607 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000608 elementBounds.offset(clipToMaskOffset);
609 elementBounds.roundOut(&maskSpaceElementIBounds);
610 }
611
bsalomon427cf282014-10-16 13:41:43 -0700612 if (!temp) {
613 temp.reset(this->createTempMask(maskSpaceIBounds.fRight,
614 maskSpaceIBounds.fBottom));
615 if (!temp) {
616 fAACache.reset();
617 return NULL;
618 }
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000619 }
bsalomon427cf282014-10-16 13:41:43 -0700620 dst = temp;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000621 // clear the temp target and set blend to replace
joshualitt329bf482014-10-29 12:31:28 -0700622 fClipTarget->clear(&maskSpaceElementIBounds,
joshualitt9853cce2014-11-17 14:22:48 -0800623 invert ? 0xffffffff : 0x00000000,
624 true,
625 dst->asRenderTarget());
egdaniel8dd688b2015-01-22 10:16:09 -0800626 set_coverage_drawing_xpf(SkRegion::kReplace_Op, invert, &pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000627 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000628 // draw directly into the result with the stencil set to make the pixels affected
629 // by the clip shape be non-zero.
630 dst = result;
631 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
632 kReplace_StencilOp,
633 kReplace_StencilOp,
634 kAlways_StencilFunc,
635 0xffff,
636 0xffff,
637 0xffff);
egdaniel8dd688b2015-01-22 10:16:09 -0800638 pipelineBuilder.setStencil(kStencilInElement);
639 set_coverage_drawing_xpf(op, invert, &pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000640 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000641
egdaniel8dd688b2015-01-22 10:16:09 -0800642 if (!this->drawElement(&pipelineBuilder, translate, dst, element, pr)) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000643 fAACache.reset();
644 return NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000645 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000646
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000647 if (useTemp) {
egdaniel8dd688b2015-01-22 10:16:09 -0800648 GrPipelineBuilder backgroundPipelineBuilder;
egdaniel8dd688b2015-01-22 10:16:09 -0800649 backgroundPipelineBuilder.setRenderTarget(result->asRenderTarget());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800650
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000651 // Now draw into the accumulator using the real operation and the temp buffer as a
652 // texture
egdaniel8dd688b2015-01-22 10:16:09 -0800653 this->mergeMask(&backgroundPipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800654 result,
bsalomon427cf282014-10-16 13:41:43 -0700655 temp,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000656 op,
657 maskSpaceIBounds,
658 maskSpaceElementIBounds);
659 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800660 GrPipelineBuilder backgroundPipelineBuilder;
egdaniel8dd688b2015-01-22 10:16:09 -0800661 backgroundPipelineBuilder.setRenderTarget(result->asRenderTarget());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800662
egdaniel8dd688b2015-01-22 10:16:09 -0800663 set_coverage_drawing_xpf(op, !invert, &backgroundPipelineBuilder);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000664 // Draw to the exterior pixels (those with a zero stencil value).
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000665 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
666 kZero_StencilOp,
667 kZero_StencilOp,
668 kEqual_StencilFunc,
669 0xffff,
670 0x0000,
671 0xffff);
egdaniel8dd688b2015-01-22 10:16:09 -0800672 backgroundPipelineBuilder.setStencil(kDrawOutsideElement);
egdaniel87509242014-12-17 13:37:13 -0800673 // The color passed in here does not matter since the coverageSetOpXP won't read it.
egdaniel8dd688b2015-01-22 10:16:09 -0800674 fClipTarget->drawSimpleRect(&backgroundPipelineBuilder, GrColor_WHITE, translate,
joshualitt8059eb92014-12-29 15:10:07 -0800675 clipSpaceIBounds);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000676 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000677 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800678 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800679
robertphillips@google.come79f3202014-02-11 16:30:21 +0000680 // all the remaining ops can just be directly draw into the accumulation buffer
egdaniel8dd688b2015-01-22 10:16:09 -0800681 set_coverage_drawing_xpf(op, false, &pipelineBuilder);
egdaniel87509242014-12-17 13:37:13 -0800682 // The color passed in here does not matter since the coverageSetOpXP won't read it.
egdaniel8dd688b2015-01-22 10:16:09 -0800683 this->drawElement(&pipelineBuilder, translate, result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000684 }
685 }
686
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000687 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000688 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000689}
690
691////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000692// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000693// (as opposed to canvas) coordinates
joshualitt9853cce2014-11-17 14:22:48 -0800694bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt,
695 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700696 GrReducedClip::InitialState initialState,
697 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000698 const SkIRect& clipSpaceIBounds,
699 const SkIPoint& clipSpaceToStencilOffset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000700 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon49f085d2014-09-05 13:34:00 -0700701 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000702
bsalomon6bc1b5f2015-02-23 09:06:38 -0800703 GrStencilBuffer* stencilBuffer = rt->renderTargetPriv().attachStencilBuffer();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000704 if (NULL == stencilBuffer) {
705 return false;
706 }
707
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000708 if (stencilBuffer->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000709 stencilBuffer->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000710 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
711 SkVector translate = {
712 SkIntToScalar(clipSpaceToStencilOffset.fX),
713 SkIntToScalar(clipSpaceToStencilOffset.fY)
714 };
joshualitt8059eb92014-12-29 15:10:07 -0800715 SkMatrix viewMatrix;
716 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000717
bsalomon@google.com9f131742012-12-13 20:43:56 +0000718 // We set the current clip to the bounds so that our recursive draws are scissored to them.
719 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
720 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
joshualitt44701df2015-02-23 14:44:57 -0800721 GrClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000722
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000723 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000724 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000725 clipBit = (1 << (clipBit-1));
726
joshualitt329bf482014-10-29 12:31:28 -0700727 fClipTarget->clearStencilClip(stencilSpaceIBounds,
728 GrReducedClip::kAllIn_InitialState == initialState,
729 rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000730
731 // walk through each clip element and perform its set op
732 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700733 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000734 const Element* element = iter.get();
joshualitt9853cce2014-11-17 14:22:48 -0800735
egdaniel8dd688b2015-01-22 10:16:09 -0800736 GrPipelineBuilder pipelineBuilder;
joshualitt44701df2015-02-23 14:44:57 -0800737 pipelineBuilder.setClip(clip);
egdaniel8dd688b2015-01-22 10:16:09 -0800738 pipelineBuilder.setRenderTarget(rt);
egdaniel080e6732014-12-22 07:35:52 -0800739
egdaniel8dd688b2015-01-22 10:16:09 -0800740 pipelineBuilder.setDisableColorXPFactory();
joshualitt9853cce2014-11-17 14:22:48 -0800741
742 // if the target is MSAA then we want MSAA enabled when the clip is soft
743 if (rt->isMultisampled()) {
egdaniel8dd688b2015-01-22 10:16:09 -0800744 pipelineBuilder.setState(GrPipelineBuilder::kHWAntialias_StateBit, element->isAA());
joshualitt9853cce2014-11-17 14:22:48 -0800745 }
746
tomhudson@google.com8afae612012-08-14 15:03:35 +0000747 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000748 // enabled at bottom of loop
joshualitt7a6184f2014-10-29 18:29:27 -0700749 fClipMode = kIgnoreClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000750
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000751 // This will be used to determine whether the clip shape can be rendered into the
752 // stencil with arbitrary stencil settings.
753 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000754
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000755 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000756 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000757
robertphillips@google.come79f3202014-02-11 16:30:21 +0000758 GrPathRenderer* pr = NULL;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000759 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000760 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000761 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000762 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000763 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000764 element->asPath(&clipPath);
765 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000766 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000767 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000768 }
joshualitt9853cce2014-11-17 14:22:48 -0800769 pr = this->getContext()->getPathRenderer(fClipTarget,
egdaniel8dd688b2015-01-22 10:16:09 -0800770 &pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800771 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800772 clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000773 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000774 false,
775 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000776 &stencilSupport);
777 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000778 return false;
779 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000780 }
781
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000782 int passes;
783 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
784
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000785 bool canRenderDirectToStencil =
786 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000787 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000788 // fill rule, and set operation can
789 // we render the element directly to
790 // stencil bit used for clipping.
791 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
792 canRenderDirectToStencil,
793 clipBit,
794 fillInverted,
795 &passes,
796 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000797
798 // draw the element to the client stencil bits if necessary
799 if (!canDrawDirectToClip) {
800 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000801 kIncClamp_StencilOp,
802 kIncClamp_StencilOp,
803 kAlways_StencilFunc,
804 0xffff,
805 0x0000,
806 0xffff);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000807 if (Element::kRect_Type == element->getType()) {
egdaniel8dd688b2015-01-22 10:16:09 -0800808 *pipelineBuilder.stencil() = gDrawToStencil;
joshualitt44701df2015-02-23 14:44:57 -0800809 fClipTarget->drawSimpleRect(&pipelineBuilder,
810 GrColor_WHITE,
811 viewMatrix,
joshualitt8059eb92014-12-29 15:10:07 -0800812 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000813 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000814 if (!clipPath.isEmpty()) {
joshualitt9853cce2014-11-17 14:22:48 -0800815 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000816 if (canRenderDirectToStencil) {
egdaniel8dd688b2015-01-22 10:16:09 -0800817 *pipelineBuilder.stencil() = gDrawToStencil;
joshualitt44701df2015-02-23 14:44:57 -0800818 pr->drawPath(fClipTarget, &pipelineBuilder, GrColor_WHITE,
819 viewMatrix, clipPath, stroke, false);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000820 } else {
joshualitt44701df2015-02-23 14:44:57 -0800821 pr->stencilPath(fClipTarget, &pipelineBuilder, viewMatrix,
822 clipPath, stroke);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000823 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000824 }
825 }
826 }
827
828 // now we modify the clip bit by rendering either the clip
829 // element directly or a bounding rect of the entire clip.
joshualitt7a6184f2014-10-29 18:29:27 -0700830 fClipMode = kModifyClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000831 for (int p = 0; p < passes; ++p) {
egdaniel8dd688b2015-01-22 10:16:09 -0800832 GrPipelineBuilder pipelineBuilderCopy(pipelineBuilder);
833 *pipelineBuilderCopy.stencil() = stencilSettings[p];
joshualitt9853cce2014-11-17 14:22:48 -0800834
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000835 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000836 if (Element::kRect_Type == element->getType()) {
joshualitt44701df2015-02-23 14:44:57 -0800837 fClipTarget->drawSimpleRect(&pipelineBuilderCopy,
838 GrColor_WHITE,
839 viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800840 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000841 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800842 GrDrawTarget::AutoGeometryPush agp(fClipTarget);
joshualitt44701df2015-02-23 14:44:57 -0800843 pr->drawPath(fClipTarget, &pipelineBuilderCopy, GrColor_WHITE,
844 viewMatrix, clipPath, stroke, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000845 }
846 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000847 // The view matrix is setup to do clip space -> stencil space translation, so
848 // draw rect in clip space.
joshualitt44701df2015-02-23 14:44:57 -0800849 fClipTarget->drawSimpleRect(&pipelineBuilderCopy,
850 GrColor_WHITE,
851 viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800852 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000853 }
854 }
855 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000856 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000857 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000858 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000859 fCurrClipMaskType = kStencil_ClipMaskType;
joshualitt7a6184f2014-10-29 18:29:27 -0700860 fClipMode = kRespectClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000861 return true;
862}
863
bsalomon@google.com411dad02012-06-05 20:24:20 +0000864// mapping of clip-respecting stencil funcs to normal stencil funcs
865// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000866static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000867 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
868 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
869 // In the Clip Funcs
870 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
871 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
872 kLess_StencilFunc, // kLessIfInClip_StencilFunc
873 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
874 // Special in the clip func that forces user's ref to be 0.
875 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
876 // make ref 0 and do normal nequal.
877 },
878 {// Stencil-Clipping is ENABLED
879 // In the Clip Funcs
880 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
881 // eq stencil clip bit, mask
882 // out user bits.
883
884 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
885 // add stencil bit to mask and ref
886
887 kLess_StencilFunc, // kLessIfInClip_StencilFunc
888 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
889 // for both of these we can add
890 // the clip bit to the mask and
891 // ref and compare as normal
892 // Special in the clip func that forces user's ref to be 0.
893 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
894 // make ref have only the clip bit set
895 // and make comparison be less
896 // 10..0 < 1..user_bits..
897 }
898};
899
bsalomon@google.coma3201942012-06-21 19:58:20 +0000900namespace {
901// Sets the settings to clip against the stencil buffer clip while ignoring the
902// client bits.
903const GrStencilSettings& basic_apply_stencil_clip_settings() {
904 // stencil settings to use when clip is in stencil
905 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
906 kKeep_StencilOp,
907 kKeep_StencilOp,
908 kAlwaysIfInClip_StencilFunc,
909 0x0000,
910 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000911 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000912 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
913}
914}
915
egdaniel8dd688b2015-01-22 10:16:09 -0800916void GrClipMaskManager::setPipelineBuilderStencil(GrPipelineBuilder* pipelineBuilder,
917 GrPipelineBuilder::AutoRestoreStencil* ars) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000918 // We make two copies of the StencilSettings here (except in the early
919 // exit scenario. One copy from draw state to the stack var. Then another
920 // from the stack var to the gpu. We could make this class hold a ptr to
921 // GrGpu's fStencilSettings and eliminate the stack copy here.
922
bsalomon@google.coma3201942012-06-21 19:58:20 +0000923 // use stencil for clipping if clipping is enabled and the clip
924 // has been written into the stencil.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000925 GrStencilSettings settings;
joshualitt9853cce2014-11-17 14:22:48 -0800926
bsalomon@google.coma3201942012-06-21 19:58:20 +0000927 // The GrGpu client may not be using the stencil buffer but we may need to
928 // enable it in order to respect a stencil clip.
egdaniel8dd688b2015-01-22 10:16:09 -0800929 if (pipelineBuilder->getStencil().isDisabled()) {
joshualitt7a6184f2014-10-29 18:29:27 -0700930 if (GrClipMaskManager::kRespectClip_StencilClipMode == fClipMode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000931 settings = basic_apply_stencil_clip_settings();
932 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000933 return;
934 }
935 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800936 settings = pipelineBuilder->getStencil();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000937 }
938
bsalomon@google.coma3201942012-06-21 19:58:20 +0000939 int stencilBits = 0;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800940 GrRenderTarget* rt = pipelineBuilder->getRenderTarget();
941 GrStencilBuffer* stencilBuffer = rt->renderTargetPriv().attachStencilBuffer();
bsalomon49f085d2014-09-05 13:34:00 -0700942 if (stencilBuffer) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000943 stencilBits = stencilBuffer->bits();
944 }
945
joshualitt329bf482014-10-29 12:31:28 -0700946 SkASSERT(fClipTarget->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
947 SkASSERT(fClipTarget->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
joshualitt7a6184f2014-10-29 18:29:27 -0700948 this->adjustStencilParams(&settings, fClipMode, stencilBits);
egdaniel8dd688b2015-01-22 10:16:09 -0800949 ars->set(pipelineBuilder);
950 pipelineBuilder->setStencil(settings);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000951}
952
953void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
954 StencilClipMode mode,
955 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000956 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000957
958 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000959 // We assume that this clip manager itself is drawing to the GrGpu and
960 // has already setup the correct values.
961 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000962 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000963
bsalomon@google.com411dad02012-06-05 20:24:20 +0000964 unsigned int clipBit = (1 << (stencilBitCnt - 1));
965 unsigned int userBits = clipBit - 1;
966
bsalomon@google.coma3201942012-06-21 19:58:20 +0000967 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
joshualitt329bf482014-10-29 12:31:28 -0700968 bool twoSided = fClipTarget->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000969
bsalomon@google.coma3201942012-06-21 19:58:20 +0000970 bool finished = false;
971 while (!finished) {
972 GrStencilFunc func = settings->func(face);
973 uint16_t writeMask = settings->writeMask(face);
974 uint16_t funcMask = settings->funcMask(face);
975 uint16_t funcRef = settings->funcRef(face);
976
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000977 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000978
979 writeMask &= userBits;
980
981 if (func >= kBasicStencilFuncCount) {
982 int respectClip = kRespectClip_StencilClipMode == mode;
983 if (respectClip) {
984 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000985 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000986 switch (func) {
987 case kAlwaysIfInClip_StencilFunc:
988 funcMask = clipBit;
989 funcRef = clipBit;
990 break;
991 case kEqualIfInClip_StencilFunc:
992 case kLessIfInClip_StencilFunc:
993 case kLEqualIfInClip_StencilFunc:
994 funcMask = (funcMask & userBits) | clipBit;
995 funcRef = (funcRef & userBits) | clipBit;
996 break;
997 case kNonZeroIfInClip_StencilFunc:
998 funcMask = (funcMask & userBits) | clipBit;
999 funcRef = clipBit;
1000 break;
1001 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001002 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +00001003 }
1004 } else {
1005 funcMask &= userBits;
1006 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001007 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001008 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001009 gSpecialToBasicStencilFunc[respectClip];
1010 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001011 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001012 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001013 funcMask &= userBits;
1014 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001015 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001016
1017 settings->setFunc(face, func);
1018 settings->setWriteMask(face, writeMask);
1019 settings->setFuncMask(face, funcMask);
1020 settings->setFuncRef(face, funcRef);
1021
1022 if (GrStencilSettings::kFront_Face == face) {
1023 face = GrStencilSettings::kBack_Face;
1024 finished = !twoSided;
1025 } else {
1026 finished = true;
1027 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001028 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001029 if (!twoSided) {
1030 settings->copyFrontSettingsToBack();
1031 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001032}
1033
1034////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +00001035GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001036 GrReducedClip::InitialState initialState,
1037 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -08001038 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001039 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001040 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001041
krajcevskiad1dc582014-06-10 15:06:47 -07001042 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -07001043 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001044 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001045 }
1046
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001047 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1048 // the top left corner of the resulting rect to the top left of the texture.
1049 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1050
robertphillips@google.com2c756812012-05-22 20:28:23 +00001051 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001052
joshualitt8059eb92014-12-29 15:10:07 -08001053 // Set the matrix so that rendered clip elements are transformed to mask space from clip
1054 // space.
1055 SkMatrix translate;
1056 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -08001057
joshualitt8059eb92014-12-29 15:10:07 -08001058 helper.init(maskSpaceIBounds, &translate, false);
tfarinabf54e492014-10-23 17:47:18 -07001059 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001060 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001061
tfarinabf54e492014-10-23 17:47:18 -07001062 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001063 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001064 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001065
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001066 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1067 // Intersect and reverse difference require modifying pixels outside of the geometry
1068 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1069 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1070 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001071 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001072 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001073 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001074 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001075 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001076 SkPath clipPath;
1077 element->asPath(&clipPath);
1078 clipPath.toggleInverseFillType();
1079 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001080 continue;
1081 }
1082
1083 // The other ops (union, xor, diff) only affect pixels inside
1084 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001085 if (Element::kRect_Type == element->getType()) {
1086 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1087 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001088 SkPath path;
1089 element->asPath(&path);
1090 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001091 }
1092 }
1093
krajcevskiad1dc582014-06-10 15:06:47 -07001094 // Allocate clip mask texture
1095 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, true);
1096 if (NULL == result) {
1097 fAACache.reset();
1098 return NULL;
1099 }
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001100 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001101
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001102 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001103 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001104}
1105
robertphillips@google.comf294b772012-04-27 14:29:26 +00001106////////////////////////////////////////////////////////////////////////////////
bsalomonc8dc1f72014-08-21 13:02:13 -07001107void GrClipMaskManager::purgeResources() {
1108 fAACache.purgeResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001109}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001110
joshualitt329bf482014-10-29 12:31:28 -07001111void GrClipMaskManager::setClipTarget(GrClipTarget* clipTarget) {
1112 fClipTarget = clipTarget;
1113 fAACache.setContext(clipTarget->getContext());
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001114}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001115
joshualitt9853cce2014-11-17 14:22:48 -08001116void GrClipMaskManager::adjustPathStencilParams(const GrStencilBuffer* stencilBuffer,
1117 GrStencilSettings* settings) {
bsalomon49f085d2014-09-05 13:34:00 -07001118 if (stencilBuffer) {
joshualitt9853cce2014-11-17 14:22:48 -08001119 int stencilBits = stencilBuffer->bits();
joshualitt7a6184f2014-10-29 18:29:27 -07001120 this->adjustStencilParams(settings, fClipMode, stencilBits);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001121 }
1122}