blob: 0738108ab8b6cd015776a60e09359d10a920e51c [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "GrClipMaskManager.h"
robertphillips@google.comfa662942012-05-17 12:20:22 +000010#include "GrAAConvexPathRenderer.h"
11#include "GrAAHairLinePathRenderer.h"
jvanverth@google.combfe2b9d2013-09-06 16:57:29 +000012#include "GrAARectRenderer.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000013#include "GrDrawTargetCaps.h"
14#include "GrGpu.h"
15#include "GrPaint.h"
16#include "GrPathRenderer.h"
17#include "GrRenderTarget.h"
18#include "GrStencilBuffer.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000019#include "GrSWMaskHelper.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000020#include "SkRasterClip.h"
21#include "SkStrokeRec.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000022#include "SkTLazy.h"
joshualitt7afb5aa2014-10-27 07:17:17 -070023#include "effects/GrTextureDomain.h"
24#include "effects/GrConvexPolyEffect.h"
25#include "effects/GrRRectEffect.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000026
robertphillips@google.comba998f22012-10-12 11:33:56 +000027#define GR_AA_CLIP 1
robertphillips@google.coma72eef32012-05-01 17:22:59 +000028
bsalomon@google.com8182fa02012-12-04 14:06:06 +000029typedef SkClipStack::Element Element;
bsalomon@google.com51a62862012-11-26 21:19:43 +000030
31////////////////////////////////////////////////////////////////////////////////
robertphillips@google.come79f3202014-02-11 16:30:21 +000032namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000033// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000034// stage matrix this also alters the vertex layout
robertphillips@google.come79f3202014-02-11 16:30:21 +000035void setup_drawstate_aaclip(GrGpu* gpu,
36 GrTexture* result,
37 const SkIRect &devBound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000038 GrDrawState* drawState = gpu->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000039 SkASSERT(drawState);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000040
bsalomon@google.comb9086a02012-11-01 18:02:54 +000041 SkMatrix mat;
bsalomon@google.comc7818882013-03-20 19:19:53 +000042 // We want to use device coords to compute the texture coordinates. We set our matrix to be
43 // equal to the view matrix followed by an offset to the devBound, and then a scaling matrix to
44 // normalized coords. We apply this matrix to the vertex positions rather than local coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000045 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000046 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000047 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000048 mat.preConcat(drawState->getViewMatrix());
49
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000050 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000051 // This could be a long-lived effect that is cached with the alpha-mask.
joshualittb0a8a372014-09-23 09:50:21 -070052 drawState->addCoverageProcessor(
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000053 GrTextureDomainEffect::Create(result,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000054 mat,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000055 GrTextureDomain::MakeTexelDomain(result, domainTexels),
56 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +000057 GrTextureParams::kNone_FilterMode,
bsalomon@google.com77af6802013-10-02 13:04:56 +000058 kPosition_GrCoordSet))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000059}
60
robertphillips@google.come79f3202014-02-11 16:30:21 +000061bool path_needs_SW_renderer(GrContext* context,
62 GrGpu* gpu,
63 const SkPath& origPath,
64 const SkStrokeRec& stroke,
65 bool doAA) {
66 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
67 SkTCopyOnFirstWrite<SkPath> path(origPath);
68 if (path->isInverseFillType()) {
69 path.writable()->toggleInverseFillType();
70 }
71 // last (false) parameter disallows use of the SW path renderer
bsalomon@google.com45a15f52012-12-10 19:10:17 +000072 GrPathRendererChain::DrawType type = doAA ?
73 GrPathRendererChain::kColorAntiAlias_DrawType :
74 GrPathRendererChain::kColor_DrawType;
75
robertphillips@google.come79f3202014-02-11 16:30:21 +000076 return NULL == context->getPathRenderer(*path, stroke, gpu, false, type);
77}
78
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000079}
80
robertphillips@google.comfa662942012-05-17 12:20:22 +000081/*
82 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
83 * will be used on any element. If so, it returns true to indicate that the
84 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
85 */
tfarinabf54e492014-10-23 17:47:18 -070086bool GrClipMaskManager::useSWOnlyPath(const GrReducedClip::ElementList& elements) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +000087
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
tfarinabf54e492014-10-23 17:47:18 -070093 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000094 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000095 // rects can always be drawn directly w/o using the software path
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000096 // Skip rrects once we're drawing them directly.
97 if (Element::kRect_Type != element->getType()) {
98 SkPath path;
99 element->asPath(&path);
100 if (path_needs_SW_renderer(this->getContext(), fGpu, path, stroke, element->isAA())) {
101 return true;
102 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000103 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000104 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000105 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000106}
107
tfarinabf54e492014-10-23 17:47:18 -0700108bool GrClipMaskManager::installClipEffects(const GrReducedClip::ElementList& elements,
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000109 GrDrawState::AutoRestoreEffects* are,
110 const SkVector& clipToRTOffset,
mtklein217daa72014-07-02 12:55:21 -0700111 const SkRect* drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000112
113 GrDrawState* drawState = fGpu->drawState();
114 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700115 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000116 boundsInClipSpace = *drawBounds;
117 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
118 }
119
120 are->set(drawState);
121 GrRenderTarget* rt = drawState->getRenderTarget();
tfarinabf54e492014-10-23 17:47:18 -0700122 GrReducedClip::ElementList::Iter iter(elements);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000123
124 bool setARE = false;
125 bool failed = false;
126
bsalomon49f085d2014-09-05 13:34:00 -0700127 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000128 SkRegion::Op op = iter.get()->getOp();
129 bool invert;
130 bool skip = false;
131 switch (op) {
132 case SkRegion::kReplace_Op:
133 SkASSERT(iter.get() == elements.head());
134 // Fallthrough, handled same as intersect.
135 case SkRegion::kIntersect_Op:
136 invert = false;
bsalomon49f085d2014-09-05 13:34:00 -0700137 if (drawBounds && iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000138 skip = true;
139 }
140 break;
141 case SkRegion::kDifference_Op:
142 invert = true;
143 // We don't currently have a cheap test for whether a rect is fully outside an
144 // element's primitive, so don't attempt to set skip.
145 break;
146 default:
147 failed = true;
148 break;
149 }
150 if (failed) {
151 break;
152 }
153
154 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700155 GrPrimitiveEdgeType edgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000156 if (GR_AA_CLIP && iter.get()->isAA()) {
157 if (rt->isMultisampled()) {
mtklein217daa72014-07-02 12:55:21 -0700158 // Coverage based AA clips don't place nicely with MSAA.
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000159 failed = true;
160 break;
161 }
joshualittb0a8a372014-09-23 09:50:21 -0700162 edgeType =
163 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000164 } else {
joshualittb0a8a372014-09-23 09:50:21 -0700165 edgeType =
166 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000167 }
joshualittb0a8a372014-09-23 09:50:21 -0700168 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000169 switch (iter.get()->getType()) {
170 case SkClipStack::Element::kPath_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700171 fp.reset(GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000172 &clipToRTOffset));
173 break;
174 case SkClipStack::Element::kRRect_Type: {
175 SkRRect rrect = iter.get()->getRRect();
176 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
joshualittb0a8a372014-09-23 09:50:21 -0700177 fp.reset(GrRRectEffect::Create(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000178 break;
179 }
180 case SkClipStack::Element::kRect_Type: {
181 SkRect rect = iter.get()->getRect();
182 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
joshualittb0a8a372014-09-23 09:50:21 -0700183 fp.reset(GrConvexPolyEffect::Create(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000184 break;
185 }
186 default:
187 break;
188 }
joshualittb0a8a372014-09-23 09:50:21 -0700189 if (fp) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000190 if (!setARE) {
191 are->set(fGpu->drawState());
192 setARE = true;
193 }
joshualittb0a8a372014-09-23 09:50:21 -0700194 fGpu->drawState()->addCoverageProcessor(fp);
mtklein217daa72014-07-02 12:55:21 -0700195 } else {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000196 failed = true;
197 break;
198 }
199 }
mtklein217daa72014-07-02 12:55:21 -0700200 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000201 }
202
203 if (failed) {
204 are->set(NULL);
205 }
206
207 return !failed;
208}
209
robertphillips@google.comf294b772012-04-27 14:29:26 +0000210////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000211// sort out what kind of clip mask needs to be created: alpha, stencil,
212// scissor, or entirely software
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000213bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn,
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000214 GrDrawState::AutoRestoreEffects* are,
joshualitt7afb5aa2014-10-27 07:17:17 -0700215 GrDrawState::AutoRestoreStencil* asr,
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000216 const SkRect* devBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000217 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000218
tfarinabf54e492014-10-23 17:47:18 -0700219 GrReducedClip::ElementList elements(16);
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000220 int32_t genID;
tfarinabf54e492014-10-23 17:47:18 -0700221 GrReducedClip::InitialState initialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000222 SkIRect clipSpaceIBounds;
223 bool requiresAA;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000224
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000225 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000226
227 const GrRenderTarget* rt = drawState->getRenderTarget();
228 // GrDrawTarget should have filtered this for us
bsalomon49f085d2014-09-05 13:34:00 -0700229 SkASSERT(rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000230
231 bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000232 if (!ignoreClip) {
233 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
234 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
tfarinabf54e492014-10-23 17:47:18 -0700235 GrReducedClip::ReduceClipStack(*clipDataIn->fClipStack,
236 clipSpaceRTIBounds,
237 &elements,
238 &genID,
239 &initialState,
240 &clipSpaceIBounds,
241 &requiresAA);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000242 if (elements.isEmpty()) {
tfarinabf54e492014-10-23 17:47:18 -0700243 if (GrReducedClip::kAllIn_InitialState == initialState) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000244 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000245 } else {
246 return false;
247 }
248 }
249 }
250
251 if (ignoreClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000252 fGpu->disableScissor();
joshualitt7afb5aa2014-10-27 07:17:17 -0700253 this->setDrawStateStencil(asr);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000254 return true;
255 }
256
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000257 // An element count of 4 was chosen because of the common pattern in Blink of:
258 // isect RR
259 // diff RR
260 // isect convex_poly
261 // isect convex_poly
262 // when drawing rounded div borders. This could probably be tuned based on a
263 // configuration's relative costs of switching RTs to generate a mask vs
264 // longer shaders.
265 if (elements.count() <= 4) {
266 SkVector clipToRTOffset = { SkIntToScalar(-clipDataIn->fOrigin.fX),
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000267 SkIntToScalar(-clipDataIn->fOrigin.fY) };
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000268 if (elements.isEmpty() ||
mtklein217daa72014-07-02 12:55:21 -0700269 (requiresAA && this->installClipEffects(elements, are, clipToRTOffset, devBounds))) {
270 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
271 scissorSpaceIBounds.offset(-clipDataIn->fOrigin);
272 if (NULL == devBounds ||
273 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
274 fGpu->enableScissor(scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000275 } else {
mtklein217daa72014-07-02 12:55:21 -0700276 fGpu->disableScissor();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000277 }
joshualitt7afb5aa2014-10-27 07:17:17 -0700278 this->setDrawStateStencil(asr);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000279 return true;
280 }
281 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000282
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000283#if GR_AA_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000284 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000285 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000286 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000287
288 if (this->useSWOnlyPath(elements)) {
289 // The clip geometry is complex enough that it will be more efficient to create it
290 // entirely in software
291 result = this->createSoftwareClipMask(genID,
292 initialState,
293 elements,
294 clipSpaceIBounds);
295 } else {
296 result = this->createAlphaClipMask(genID,
297 initialState,
298 elements,
299 clipSpaceIBounds);
300 }
301
bsalomon49f085d2014-09-05 13:34:00 -0700302 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000303 // The mask's top left coord should be pinned to the rounded-out top left corner of
304 // clipSpace bounds. We determine the mask's position WRT to the render target here.
305 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
306 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000307 are->set(fGpu->drawState());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000308 setup_drawstate_aaclip(fGpu, result, rtSpaceMaskBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000309 fGpu->disableScissor();
joshualitt7afb5aa2014-10-27 07:17:17 -0700310 this->setDrawStateStencil(asr);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000311 return true;
312 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000313 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000314 }
315#endif // GR_AA_CLIP
316
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000317 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
318 // be created. In either case, free up the texture in the anti-aliased mask cache.
319 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
320 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
321 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000322 fAACache.reset();
323
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000324 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000325 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000326 this->createStencilClipMask(genID,
327 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000328 elements,
329 clipSpaceIBounds,
330 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000331
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000332 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
333 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
334 // use both stencil and scissor test to the bounds for the final draw.
335 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
336 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
337 fGpu->enableScissor(scissorSpaceIBounds);
joshualitt7afb5aa2014-10-27 07:17:17 -0700338 this->setDrawStateStencil(asr);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000339 return true;
340}
341
342#define VISUALIZE_COMPLEX_CLIP 0
343
344#if VISUALIZE_COMPLEX_CLIP
tfarina@chromium.org223137f2012-11-21 22:38:36 +0000345 #include "SkRandom.h"
346 SkRandom gRandom;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000347 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
348#else
349 #define SET_RANDOM_COLOR
350#endif
351
352namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000353
354////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000355// set up the OpenGL blend function to perform the specified
356// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000357void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000358
359 switch (op) {
360 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000361 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000362 break;
363 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000364 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000365 break;
366 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000367 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000368 break;
369 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000370 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000371 break;
372 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000373 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000374 break;
375 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000376 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000377 break;
378 default:
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000379 SkASSERT(false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000380 break;
381 }
382}
383
robertphillips@google.com72176b22012-05-23 13:19:12 +0000384}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000385
386////////////////////////////////////////////////////////////////////////////////
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000387bool GrClipMaskManager::drawElement(GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000388 const SkClipStack::Element* element,
389 GrPathRenderer* pr) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000390 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000391
392 drawState->setRenderTarget(target->asRenderTarget());
393
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000394 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000395 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000396 case Element::kEmpty_Type:
397 SkDEBUGFAIL("Should never get here with an empty element.");
398 break;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000399 case Element::kRect_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700400 // TODO: Do rects directly to the accumulator using a aa-rect GrProcessor that covers
401 // the entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000402 if (element->isAA()) {
joshualitt7afb5aa2014-10-27 07:17:17 -0700403 this->getContext()->getAARectRenderer()->fillAARect(fGpu,
404 fGpu,
405 element->getRect(),
406 SkMatrix::I(),
407 element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000408 } else {
bsalomon01c8da12014-08-04 09:21:30 -0700409 fGpu->drawSimpleRect(element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000410 }
411 return true;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000412 default: {
413 SkPath path;
414 element->asPath(&path);
jvanverthb3eb6872014-10-24 07:12:51 -0700415 path.setIsVolatile(true);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000416 if (path.isInverseFillType()) {
417 path.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000418 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000419 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000420 if (NULL == pr) {
421 GrPathRendererChain::DrawType type;
422 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
423 GrPathRendererChain::kColor_DrawType;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000424 pr = this->getContext()->getPathRenderer(path, stroke, fGpu, false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000425 }
426 if (NULL == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000427 return false;
428 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000429 pr->drawPath(path, stroke, fGpu, 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
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000436bool GrClipMaskManager::canStencilAndDrawElement(GrTexture* target,
437 const SkClipStack::Element* element,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000438 GrPathRenderer** pr) {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000439 GrDrawState* drawState = fGpu->drawState();
440 drawState->setRenderTarget(target->asRenderTarget());
441
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;
456 *pr = this->getContext()->getPathRenderer(path, stroke, fGpu, false, type);
bsalomon49f085d2014-09-05 13:34:00 -0700457 return SkToBool(*pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000458 }
459}
460
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000461void GrClipMaskManager::mergeMask(GrTexture* dstMask,
462 GrTexture* srcMask,
463 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000464 const SkIRect& dstBound,
465 const SkIRect& srcBound) {
bsalomon@google.com137f1342013-05-29 21:27:53 +0000466 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000467 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000468 SkAssertResult(avmr.setIdentity(drawState));
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000469 GrDrawState::AutoRestoreEffects are(drawState);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000470
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000471 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000472
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000473 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000474
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000475 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000476 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000477
joshualittb0a8a372014-09-23 09:50:21 -0700478 drawState->addColorProcessor(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000479 GrTextureDomainEffect::Create(srcMask,
480 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000481 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
482 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000483 GrTextureParams::kNone_FilterMode))->unref();
bsalomon01c8da12014-08-04 09:21:30 -0700484 fGpu->drawSimpleRect(SkRect::Make(dstBound));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000485}
486
bsalomon427cf282014-10-16 13:41:43 -0700487GrTexture* GrClipMaskManager::createTempMask(int width, int height) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000488 GrTextureDesc desc;
489 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000490 desc.fWidth = width;
491 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000492 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000493
bsalomon427cf282014-10-16 13:41:43 -0700494 return fGpu->getContext()->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000495}
496
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000497////////////////////////////////////////////////////////////////////////////////
krajcevskiad1dc582014-06-10 15:06:47 -0700498// Return the texture currently in the cache if it exists. Otherwise, return NULL
499GrTexture* GrClipMaskManager::getCachedMaskTexture(int32_t elementsGenID,
500 const SkIRect& clipSpaceIBounds) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000501 bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000502 if (!cached) {
krajcevskiad1dc582014-06-10 15:06:47 -0700503 return NULL;
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000504 }
505
krajcevskiad1dc582014-06-10 15:06:47 -0700506 return fAACache.getLastMask();
507}
508
509////////////////////////////////////////////////////////////////////////////////
510// Allocate a texture in the texture cache. This function returns the texture
511// allocated (or NULL on error).
512GrTexture* GrClipMaskManager::allocMaskTexture(int32_t elementsGenID,
513 const SkIRect& clipSpaceIBounds,
514 bool willUpload) {
515 // Since we are setting up the cache we should free up the
516 // currently cached mask so it can be reused.
517 fAACache.reset();
518
519 GrTextureDesc desc;
520 desc.fFlags = willUpload ? kNone_GrTextureFlags : kRenderTarget_GrTextureFlagBit;
521 desc.fWidth = clipSpaceIBounds.width();
522 desc.fHeight = clipSpaceIBounds.height();
523 desc.fConfig = kRGBA_8888_GrPixelConfig;
524 if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
525 // We would always like A8 but it isn't supported on all platforms
526 desc.fConfig = kAlpha_8_GrPixelConfig;
527 }
528
529 fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds);
530 return fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000531}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000532
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000533////////////////////////////////////////////////////////////////////////////////
534// Create a 8-bit clip mask in alpha
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000535GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700536 GrReducedClip::InitialState initialState,
537 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000538 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000539 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000540
krajcevskiad1dc582014-06-10 15:06:47 -0700541 // First, check for cached texture
542 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -0700543 if (result) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000544 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000545 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000546 }
547
krajcevskiad1dc582014-06-10 15:06:47 -0700548 // There's no texture in the cache. Let's try to allocate it then.
549 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, false);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000550 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000551 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000552 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000553 }
554
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000555 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000556 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000557 SkIntToScalar(-clipSpaceIBounds.fLeft),
558 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000559 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000560 // The texture may be larger than necessary, this rect represents the part of the texture
561 // we populate with a rasterization of the clip.
562 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
563
bsalomon@google.com137f1342013-05-29 21:27:53 +0000564 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
565 SkMatrix translate;
566 translate.setTranslate(clipToMaskOffset);
567 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &translate);
568
569 GrDrawState* drawState = fGpu->drawState();
570
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000571 // We're drawing a coverage mask and want coverage to be run through the blend function.
572 drawState->enableState(GrDrawState::kCoverageDrawing_StateBit);
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.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000576 fGpu->clear(&maskSpaceIBounds,
tfarinabf54e492014-10-23 17:47:18 -0700577 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000578 true,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000579 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.
585 GrDrawTarget::AutoClipRestore acr(fGpu, maskSpaceIBounds);
586 drawState->enableState(GrDrawState::kClip_StateBit);
587
bsalomon427cf282014-10-16 13:41:43 -0700588 SkAutoTUnref<GrTexture> temp;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000589 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700590 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000591 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000592 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000593 bool invert = element->isInverseFilled();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000594
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000595 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000596 GrPathRenderer* pr = NULL;
597 bool useTemp = !this->canStencilAndDrawElement(result, element, &pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000598 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000599 // 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 +0000600 // mask buffer can be substantially larger than the actually clip stack element. We
601 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000602 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000603 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000604
605 if (useTemp) {
606 if (invert) {
607 maskSpaceElementIBounds = maskSpaceIBounds;
608 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000609 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000610 elementBounds.offset(clipToMaskOffset);
611 elementBounds.roundOut(&maskSpaceElementIBounds);
612 }
613
bsalomon427cf282014-10-16 13:41:43 -0700614 if (!temp) {
615 temp.reset(this->createTempMask(maskSpaceIBounds.fRight,
616 maskSpaceIBounds.fBottom));
617 if (!temp) {
618 fAACache.reset();
619 return NULL;
620 }
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000621 }
bsalomon427cf282014-10-16 13:41:43 -0700622 dst = temp;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000623 // clear the temp target and set blend to replace
624 fGpu->clear(&maskSpaceElementIBounds,
625 invert ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000626 true,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000627 dst->asRenderTarget());
628 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000629
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);
641 drawState->setStencil(kStencilInElement);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000642 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000643 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000644
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000645 drawState->setAlpha(invert ? 0x00 : 0xff);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000646
robertphillips@google.come79f3202014-02-11 16:30:21 +0000647 if (!this->drawElement(dst, element, pr)) {
648 fAACache.reset();
649 return NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000650 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000651
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000652 if (useTemp) {
653 // Now draw into the accumulator using the real operation and the temp buffer as a
654 // texture
655 this->mergeMask(result,
bsalomon427cf282014-10-16 13:41:43 -0700656 temp,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000657 op,
658 maskSpaceIBounds,
659 maskSpaceElementIBounds);
660 } else {
661 // Draw to the exterior pixels (those with a zero stencil value).
662 drawState->setAlpha(invert ? 0xff : 0x00);
663 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
664 kZero_StencilOp,
665 kZero_StencilOp,
666 kEqual_StencilFunc,
667 0xffff,
668 0x0000,
669 0xffff);
670 drawState->setStencil(kDrawOutsideElement);
671 fGpu->drawSimpleRect(clipSpaceIBounds);
672 drawState->disableStencil();
673 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000674 } else {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000675 // all the remaining ops can just be directly draw into the accumulation buffer
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000676 drawState->setAlpha(0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000677 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000678 this->drawElement(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000679 }
680 }
681
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000682 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000683 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000684}
685
686////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000687// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000688// (as opposed to canvas) coordinates
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000689bool GrClipMaskManager::createStencilClipMask(int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700690 GrReducedClip::InitialState initialState,
691 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000692 const SkIRect& clipSpaceIBounds,
693 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000694
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000695 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000696
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000697 GrDrawState* drawState = fGpu->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000698 SkASSERT(drawState->isClipState());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000699
700 GrRenderTarget* rt = drawState->getRenderTarget();
bsalomon49f085d2014-09-05 13:34:00 -0700701 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000702
703 // TODO: dynamically attach a SB when needed.
704 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
705 if (NULL == stencilBuffer) {
706 return false;
707 }
708
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000709 if (stencilBuffer->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000710 stencilBuffer->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000711
bsalomon@google.com137f1342013-05-29 21:27:53 +0000712 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
713 SkVector translate = {
714 SkIntToScalar(clipSpaceToStencilOffset.fX),
715 SkIntToScalar(clipSpaceToStencilOffset.fY)
716 };
717 SkMatrix matrix;
718 matrix.setTranslate(translate);
719 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &matrix);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000720 drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000721
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000722 drawState->setRenderTarget(rt);
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);
727 GrDrawTarget::AutoClipRestore acr(fGpu, stencilSpaceIBounds);
728 drawState->enableState(GrDrawState::kClip_StateBit);
729
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000730#if !VISUALIZE_COMPLEX_CLIP
731 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
732#endif
733
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000734 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000735 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000736 clipBit = (1 << (clipBit-1));
737
tfarinabf54e492014-10-23 17:47:18 -0700738 fGpu->clearStencilClip(rt, stencilSpaceIBounds,
739 GrReducedClip::kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000740
741 // walk through each clip element and perform its set op
742 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700743 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000744 const Element* element = iter.get();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000745 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000746 // enabled at bottom of loop
747 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000748 // if the target is MSAA then we want MSAA enabled when the clip is soft
749 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000750 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000751 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000752
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000753 // This will be used to determine whether the clip shape can be rendered into the
754 // stencil with arbitrary stencil settings.
755 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000756
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000757 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000758
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000759 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000760
robertphillips@google.come79f3202014-02-11 16:30:21 +0000761 GrPathRenderer* pr = NULL;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000762 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000763 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000764 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000765 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000766 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000767 element->asPath(&clipPath);
768 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000769 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000770 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000771 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000772 pr = this->getContext()->getPathRenderer(clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000773 stroke,
774 fGpu,
775 false,
776 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000777 &stencilSupport);
778 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000779 return false;
780 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000781 }
782
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000783 int passes;
784 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
785
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000786 bool canRenderDirectToStencil =
787 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000788 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000789 // fill rule, and set operation can
790 // we render the element directly to
791 // stencil bit used for clipping.
792 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
793 canRenderDirectToStencil,
794 clipBit,
795 fillInverted,
796 &passes,
797 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000798
799 // draw the element to the client stencil bits if necessary
800 if (!canDrawDirectToClip) {
801 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000802 kIncClamp_StencilOp,
803 kIncClamp_StencilOp,
804 kAlways_StencilFunc,
805 0xffff,
806 0x0000,
807 0xffff);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000808 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000809 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000810 *drawState->stencil() = gDrawToStencil;
bsalomon01c8da12014-08-04 09:21:30 -0700811 fGpu->drawSimpleRect(element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000812 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000813 if (!clipPath.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000814 if (canRenderDirectToStencil) {
815 *drawState->stencil() = gDrawToStencil;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000816 pr->drawPath(clipPath, stroke, fGpu, false);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000817 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000818 pr->stencilPath(clipPath, stroke, fGpu);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000819 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000820 }
821 }
822 }
823
824 // now we modify the clip bit by rendering either the clip
825 // element directly or a bounding rect of the entire clip.
826 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
827 for (int p = 0; p < passes; ++p) {
828 *drawState->stencil() = stencilSettings[p];
829 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000830 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000831 SET_RANDOM_COLOR
bsalomon01c8da12014-08-04 09:21:30 -0700832 fGpu->drawSimpleRect(element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000833 } else {
834 SET_RANDOM_COLOR
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000835 pr->drawPath(clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000836 }
837 } else {
838 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000839 // The view matrix is setup to do clip space -> stencil space translation, so
840 // draw rect in clip space.
bsalomon01c8da12014-08-04 09:21:30 -0700841 fGpu->drawSimpleRect(SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000842 }
843 }
844 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000845 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000846 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000847 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000848 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000849 return true;
850}
851
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000852
bsalomon@google.com411dad02012-06-05 20:24:20 +0000853// mapping of clip-respecting stencil funcs to normal stencil funcs
854// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000855static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000856 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
857 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
858 // In the Clip Funcs
859 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
860 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
861 kLess_StencilFunc, // kLessIfInClip_StencilFunc
862 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
863 // Special in the clip func that forces user's ref to be 0.
864 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
865 // make ref 0 and do normal nequal.
866 },
867 {// Stencil-Clipping is ENABLED
868 // In the Clip Funcs
869 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
870 // eq stencil clip bit, mask
871 // out user bits.
872
873 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
874 // add stencil bit to mask and ref
875
876 kLess_StencilFunc, // kLessIfInClip_StencilFunc
877 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
878 // for both of these we can add
879 // the clip bit to the mask and
880 // ref and compare as normal
881 // Special in the clip func that forces user's ref to be 0.
882 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
883 // make ref have only the clip bit set
884 // and make comparison be less
885 // 10..0 < 1..user_bits..
886 }
887};
888
bsalomon@google.coma3201942012-06-21 19:58:20 +0000889namespace {
890// Sets the settings to clip against the stencil buffer clip while ignoring the
891// client bits.
892const GrStencilSettings& basic_apply_stencil_clip_settings() {
893 // stencil settings to use when clip is in stencil
894 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
895 kKeep_StencilOp,
896 kKeep_StencilOp,
897 kAlwaysIfInClip_StencilFunc,
898 0x0000,
899 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000900 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000901 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
902}
903}
904
joshualitt7afb5aa2014-10-27 07:17:17 -0700905void GrClipMaskManager::setDrawStateStencil(GrDrawState::AutoRestoreStencil* ars) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000906 // We make two copies of the StencilSettings here (except in the early
907 // exit scenario. One copy from draw state to the stack var. Then another
908 // from the stack var to the gpu. We could make this class hold a ptr to
909 // GrGpu's fStencilSettings and eliminate the stack copy here.
910
911 const GrDrawState& drawState = fGpu->getDrawState();
912
913 // use stencil for clipping if clipping is enabled and the clip
914 // has been written into the stencil.
915 GrClipMaskManager::StencilClipMode clipMode;
916 if (this->isClipInStencil() && drawState.isClipState()) {
917 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
918 // We can't be modifying the clip and respecting it at the same time.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000919 SkASSERT(!drawState.isStateFlagEnabled(
bsalomon@google.coma3201942012-06-21 19:58:20 +0000920 GrGpu::kModifyStencilClip_StateBit));
921 } else if (drawState.isStateFlagEnabled(
922 GrGpu::kModifyStencilClip_StateBit)) {
923 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
924 } else {
925 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
926 }
927
928 GrStencilSettings settings;
929 // The GrGpu client may not be using the stencil buffer but we may need to
930 // enable it in order to respect a stencil clip.
931 if (drawState.getStencil().isDisabled()) {
932 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
933 settings = basic_apply_stencil_clip_settings();
934 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000935 return;
936 }
937 } else {
938 settings = drawState.getStencil();
939 }
940
941 // TODO: dynamically attach a stencil buffer
942 int stencilBits = 0;
joshualitt7afb5aa2014-10-27 07:17:17 -0700943 GrStencilBuffer* stencilBuffer = drawState.getRenderTarget()->getStencilBuffer();
bsalomon49f085d2014-09-05 13:34:00 -0700944 if (stencilBuffer) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000945 stencilBits = stencilBuffer->bits();
946 }
947
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000948 SkASSERT(fGpu->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
949 SkASSERT(fGpu->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000950 this->adjustStencilParams(&settings, clipMode, stencilBits);
joshualitt7afb5aa2014-10-27 07:17:17 -0700951 ars->set(fGpu->drawState());
952 fGpu->drawState()->setStencil(settings);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000953}
954
955void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
956 StencilClipMode mode,
957 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000958 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000959
960 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000961 // We assume that this clip manager itself is drawing to the GrGpu and
962 // has already setup the correct values.
963 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000964 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000965
bsalomon@google.com411dad02012-06-05 20:24:20 +0000966 unsigned int clipBit = (1 << (stencilBitCnt - 1));
967 unsigned int userBits = clipBit - 1;
968
bsalomon@google.coma3201942012-06-21 19:58:20 +0000969 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000970 bool twoSided = fGpu->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000971
bsalomon@google.coma3201942012-06-21 19:58:20 +0000972 bool finished = false;
973 while (!finished) {
974 GrStencilFunc func = settings->func(face);
975 uint16_t writeMask = settings->writeMask(face);
976 uint16_t funcMask = settings->funcMask(face);
977 uint16_t funcRef = settings->funcRef(face);
978
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000979 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000980
981 writeMask &= userBits;
982
983 if (func >= kBasicStencilFuncCount) {
984 int respectClip = kRespectClip_StencilClipMode == mode;
985 if (respectClip) {
986 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000987 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000988 switch (func) {
989 case kAlwaysIfInClip_StencilFunc:
990 funcMask = clipBit;
991 funcRef = clipBit;
992 break;
993 case kEqualIfInClip_StencilFunc:
994 case kLessIfInClip_StencilFunc:
995 case kLEqualIfInClip_StencilFunc:
996 funcMask = (funcMask & userBits) | clipBit;
997 funcRef = (funcRef & userBits) | clipBit;
998 break;
999 case kNonZeroIfInClip_StencilFunc:
1000 funcMask = (funcMask & userBits) | clipBit;
1001 funcRef = clipBit;
1002 break;
1003 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001004 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +00001005 }
1006 } else {
1007 funcMask &= userBits;
1008 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001009 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001010 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001011 gSpecialToBasicStencilFunc[respectClip];
1012 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001013 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001014 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001015 funcMask &= userBits;
1016 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001017 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001018
1019 settings->setFunc(face, func);
1020 settings->setWriteMask(face, writeMask);
1021 settings->setFuncMask(face, funcMask);
1022 settings->setFuncRef(face, funcRef);
1023
1024 if (GrStencilSettings::kFront_Face == face) {
1025 face = GrStencilSettings::kBack_Face;
1026 finished = !twoSided;
1027 } else {
1028 finished = true;
1029 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001030 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001031 if (!twoSided) {
1032 settings->copyFrontSettingsToBack();
1033 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001034}
1035
1036////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +00001037GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001038 GrReducedClip::InitialState initialState,
1039 const GrReducedClip::ElementList& elements,
1040 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001041 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001042
krajcevskiad1dc582014-06-10 15:06:47 -07001043 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -07001044 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001045 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001046 }
1047
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001048 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1049 // the top left corner of the resulting rect to the top left of the texture.
1050 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1051
robertphillips@google.com2c756812012-05-22 20:28:23 +00001052 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001053
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001054 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001055 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
1056 SkIntToScalar(-clipSpaceIBounds.fTop));
krajcevski71614ac2014-08-13 10:36:18 -07001057 helper.init(maskSpaceIBounds, &matrix, false);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001058
tfarinabf54e492014-10-23 17:47:18 -07001059 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001060
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001061 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001062
tfarinabf54e492014-10-23 17:47:18 -07001063 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001064
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001065 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001066 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001067
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001068 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1069 // Intersect and reverse difference require modifying pixels outside of the geometry
1070 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1071 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1072 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001073 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001074 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001075 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001076 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001077 }
1078
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001079 SkPath clipPath;
1080 element->asPath(&clipPath);
1081 clipPath.toggleInverseFillType();
1082 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001083
1084 continue;
1085 }
1086
1087 // The other ops (union, xor, diff) only affect pixels inside
1088 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001089 if (Element::kRect_Type == element->getType()) {
1090 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1091 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001092 SkPath path;
1093 element->asPath(&path);
1094 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001095 }
1096 }
1097
krajcevskiad1dc582014-06-10 15:06:47 -07001098 // Allocate clip mask texture
1099 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, true);
1100 if (NULL == result) {
1101 fAACache.reset();
1102 return NULL;
1103 }
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001104 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001105
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001106 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001107 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001108}
1109
robertphillips@google.comf294b772012-04-27 14:29:26 +00001110////////////////////////////////////////////////////////////////////////////////
bsalomonc8dc1f72014-08-21 13:02:13 -07001111void GrClipMaskManager::purgeResources() {
1112 fAACache.purgeResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001113}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001114
1115void GrClipMaskManager::setGpu(GrGpu* gpu) {
1116 fGpu = gpu;
1117 fAACache.setContext(gpu->getContext());
1118}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001119
1120void GrClipMaskManager::adjustPathStencilParams(GrStencilSettings* settings) {
1121 const GrDrawState& drawState = fGpu->getDrawState();
1122 GrClipMaskManager::StencilClipMode clipMode;
1123 if (this->isClipInStencil() && drawState.isClipState()) {
1124 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
1125 // We can't be modifying the clip and respecting it at the same time.
1126 SkASSERT(!drawState.isStateFlagEnabled(
1127 GrGpu::kModifyStencilClip_StateBit));
1128 } else if (drawState.isStateFlagEnabled(
1129 GrGpu::kModifyStencilClip_StateBit)) {
1130 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
1131 } else {
1132 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
1133 }
1134
1135 // TODO: dynamically attach a stencil buffer
1136 int stencilBits = 0;
1137 GrStencilBuffer* stencilBuffer =
1138 drawState.getRenderTarget()->getStencilBuffer();
bsalomon49f085d2014-09-05 13:34:00 -07001139 if (stencilBuffer) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001140 stencilBits = stencilBuffer->bits();
1141 this->adjustStencilParams(settings, clipMode, stencilBits);
1142 }
1143}