blob: 8bd4fabac47be94a9853c09876336f5230675e89 [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"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000014#include "GrPaint.h"
15#include "GrPathRenderer.h"
16#include "GrRenderTarget.h"
17#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"
joshualitta58fe352014-10-27 08:39:00 -070022#include "effects/GrTextureDomain.h"
23#include "effects/GrConvexPolyEffect.h"
24#include "effects/GrRRectEffect.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000025
robertphillips@google.comba998f22012-10-12 11:33:56 +000026#define GR_AA_CLIP 1
robertphillips@google.coma72eef32012-05-01 17:22:59 +000027
bsalomon@google.com8182fa02012-12-04 14:06:06 +000028typedef SkClipStack::Element Element;
bsalomon@google.com51a62862012-11-26 21:19:43 +000029
30////////////////////////////////////////////////////////////////////////////////
robertphillips@google.come79f3202014-02-11 16:30:21 +000031namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000032// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000033// stage matrix this also alters the vertex layout
joshualitt329bf482014-10-29 12:31:28 -070034void setup_drawstate_aaclip(GrDrawTarget* gpu,
robertphillips@google.come79f3202014-02-11 16:30:21 +000035 GrTexture* result,
36 const SkIRect &devBound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000037 GrDrawState* drawState = gpu->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000038 SkASSERT(drawState);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000039
bsalomon@google.comb9086a02012-11-01 18:02:54 +000040 SkMatrix mat;
bsalomon@google.comc7818882013-03-20 19:19:53 +000041 // We want to use device coords to compute the texture coordinates. We set our matrix to be
42 // equal to the view matrix followed by an offset to the devBound, and then a scaling matrix to
43 // normalized coords. We apply this matrix to the vertex positions rather than local coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000044 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000045 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000046 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000047 mat.preConcat(drawState->getViewMatrix());
48
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000049 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000050 // This could be a long-lived effect that is cached with the alpha-mask.
joshualittb0a8a372014-09-23 09:50:21 -070051 drawState->addCoverageProcessor(
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000052 GrTextureDomainEffect::Create(result,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000053 mat,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000054 GrTextureDomain::MakeTexelDomain(result, domainTexels),
55 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +000056 GrTextureParams::kNone_FilterMode,
bsalomon@google.com77af6802013-10-02 13:04:56 +000057 kPosition_GrCoordSet))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000058}
59
robertphillips@google.come79f3202014-02-11 16:30:21 +000060bool path_needs_SW_renderer(GrContext* context,
joshualitt329bf482014-10-29 12:31:28 -070061 GrDrawTarget* gpu,
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
robertphillips@google.come79f3202014-02-11 16:30:21 +000075 return NULL == context->getPathRenderer(*path, stroke, gpu, false, type);
76}
77
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 */
tfarinabf54e492014-10-23 17:47:18 -070085bool GrClipMaskManager::useSWOnlyPath(const GrReducedClip::ElementList& elements) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +000086
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000087 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000088 // a clip gets complex enough it can just be done in SW regardless
89 // of whether it would invoke the GrSoftwarePathRenderer.
sugoi@google.com5f74cf82012-12-17 21:16:45 +000090 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000091
tfarinabf54e492014-10-23 17:47:18 -070092 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000093 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000094 // rects can always be drawn directly w/o using the software path
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000095 // Skip rrects once we're drawing them directly.
96 if (Element::kRect_Type != element->getType()) {
97 SkPath path;
98 element->asPath(&path);
joshualitt329bf482014-10-29 12:31:28 -070099 if (path_needs_SW_renderer(this->getContext(), fClipTarget, path, stroke,
100 element->isAA())) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000101 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
joshualitt329bf482014-10-29 12:31:28 -0700113 GrDrawState* drawState = fClipTarget->drawState();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000114 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) {
joshualitt329bf482014-10-29 12:31:28 -0700191 are->set(fClipTarget->drawState());
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000192 setARE = true;
193 }
joshualitt329bf482014-10-29 12:31:28 -0700194 fClipTarget->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,
joshualitt77b13072014-10-27 14:51:01 -0700214 const SkRect* devBounds,
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000215 GrDrawState::AutoRestoreEffects* are,
joshualitt77b13072014-10-27 14:51:01 -0700216 GrDrawState::AutoRestoreStencil* ars,
joshualitt6db519c2014-10-29 08:48:18 -0700217 ScissorState* scissorState) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000218 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000219
tfarinabf54e492014-10-23 17:47:18 -0700220 GrReducedClip::ElementList elements(16);
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000221 int32_t genID;
tfarinabf54e492014-10-23 17:47:18 -0700222 GrReducedClip::InitialState initialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000223 SkIRect clipSpaceIBounds;
224 bool requiresAA;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000225
joshualitt329bf482014-10-29 12:31:28 -0700226 GrDrawState* drawState = fClipTarget->drawState();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000227
228 const GrRenderTarget* rt = drawState->getRenderTarget();
229 // GrDrawTarget should have filtered this for us
bsalomon49f085d2014-09-05 13:34:00 -0700230 SkASSERT(rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000231
232 bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000233 if (!ignoreClip) {
234 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
235 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
tfarinabf54e492014-10-23 17:47:18 -0700236 GrReducedClip::ReduceClipStack(*clipDataIn->fClipStack,
237 clipSpaceRTIBounds,
238 &elements,
239 &genID,
240 &initialState,
241 &clipSpaceIBounds,
242 &requiresAA);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000243 if (elements.isEmpty()) {
tfarinabf54e492014-10-23 17:47:18 -0700244 if (GrReducedClip::kAllIn_InitialState == initialState) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000245 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000246 } else {
247 return false;
248 }
249 }
250 }
251
252 if (ignoreClip) {
joshualitt77b13072014-10-27 14:51:01 -0700253 this->setDrawStateStencil(ars);
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)) {
joshualitt77b13072014-10-27 14:51:01 -0700274 scissorState->set(scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000275 }
joshualitt77b13072014-10-27 14:51:01 -0700276 this->setDrawStateStencil(ars);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000277 return true;
278 }
279 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000280
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000281#if GR_AA_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000282 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000283 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000284 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000285
286 if (this->useSWOnlyPath(elements)) {
287 // The clip geometry is complex enough that it will be more efficient to create it
288 // entirely in software
289 result = this->createSoftwareClipMask(genID,
290 initialState,
291 elements,
292 clipSpaceIBounds);
293 } else {
294 result = this->createAlphaClipMask(genID,
295 initialState,
296 elements,
297 clipSpaceIBounds);
298 }
299
bsalomon49f085d2014-09-05 13:34:00 -0700300 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000301 // The mask's top left coord should be pinned to the rounded-out top left corner of
302 // clipSpace bounds. We determine the mask's position WRT to the render target here.
303 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
304 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
joshualitt329bf482014-10-29 12:31:28 -0700305 are->set(fClipTarget->drawState());
306 setup_drawstate_aaclip(fClipTarget, result, rtSpaceMaskBounds);
joshualitt77b13072014-10-27 14:51:01 -0700307 this->setDrawStateStencil(ars);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000308 return true;
309 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000310 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000311 }
312#endif // GR_AA_CLIP
313
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000314 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
315 // be created. In either case, free up the texture in the anti-aliased mask cache.
316 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
317 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
318 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000319 fAACache.reset();
320
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000321 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000322 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000323 this->createStencilClipMask(genID,
324 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000325 elements,
326 clipSpaceIBounds,
327 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000328
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000329 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
330 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
331 // use both stencil and scissor test to the bounds for the final draw.
332 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
333 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
joshualitt77b13072014-10-27 14:51:01 -0700334 scissorState->set(scissorSpaceIBounds);
335 this->setDrawStateStencil(ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000336 return true;
337}
338
339#define VISUALIZE_COMPLEX_CLIP 0
340
341#if VISUALIZE_COMPLEX_CLIP
tfarina@chromium.org223137f2012-11-21 22:38:36 +0000342 #include "SkRandom.h"
343 SkRandom gRandom;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000344 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
345#else
346 #define SET_RANDOM_COLOR
347#endif
348
349namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000350
351////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000352// set up the OpenGL blend function to perform the specified
353// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000354void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000355
356 switch (op) {
357 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000358 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000359 break;
360 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000361 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000362 break;
363 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000364 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000365 break;
366 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000367 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000368 break;
369 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000370 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000371 break;
372 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000373 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000374 break;
375 default:
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000376 SkASSERT(false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000377 break;
378 }
379}
380
robertphillips@google.com72176b22012-05-23 13:19:12 +0000381}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000382
383////////////////////////////////////////////////////////////////////////////////
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000384bool GrClipMaskManager::drawElement(GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000385 const SkClipStack::Element* element,
386 GrPathRenderer* pr) {
joshualitt329bf482014-10-29 12:31:28 -0700387 GrDrawState* drawState = fClipTarget->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000388
389 drawState->setRenderTarget(target->asRenderTarget());
390
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000391 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000392 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000393 case Element::kEmpty_Type:
394 SkDEBUGFAIL("Should never get here with an empty element.");
395 break;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000396 case Element::kRect_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700397 // TODO: Do rects directly to the accumulator using a aa-rect GrProcessor that covers
398 // the entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000399 if (element->isAA()) {
joshualitt329bf482014-10-29 12:31:28 -0700400 this->getContext()->getAARectRenderer()->fillAARect(fClipTarget,
joshualitta58fe352014-10-27 08:39:00 -0700401 element->getRect(),
402 SkMatrix::I(),
403 element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000404 } else {
joshualitt329bf482014-10-29 12:31:28 -0700405 fClipTarget->drawSimpleRect(element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000406 }
407 return true;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000408 default: {
409 SkPath path;
410 element->asPath(&path);
jvanverthb3eb6872014-10-24 07:12:51 -0700411 path.setIsVolatile(true);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000412 if (path.isInverseFillType()) {
413 path.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000414 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000415 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000416 if (NULL == pr) {
417 GrPathRendererChain::DrawType type;
418 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
419 GrPathRendererChain::kColor_DrawType;
joshualitt329bf482014-10-29 12:31:28 -0700420 pr = this->getContext()->getPathRenderer(path, stroke, fClipTarget, false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000421 }
422 if (NULL == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000423 return false;
424 }
joshualitt329bf482014-10-29 12:31:28 -0700425 pr->drawPath(path, stroke, fClipTarget, element->isAA());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000426 break;
427 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000428 }
429 return true;
430}
431
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000432bool GrClipMaskManager::canStencilAndDrawElement(GrTexture* target,
433 const SkClipStack::Element* element,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000434 GrPathRenderer** pr) {
joshualitt329bf482014-10-29 12:31:28 -0700435 GrDrawState* drawState = fClipTarget->drawState();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000436 drawState->setRenderTarget(target->asRenderTarget());
437
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000438 if (Element::kRect_Type == element->getType()) {
439 return true;
440 } else {
441 // We shouldn't get here with an empty clip element.
442 SkASSERT(Element::kEmpty_Type != element->getType());
443 SkPath path;
444 element->asPath(&path);
445 if (path.isInverseFillType()) {
446 path.toggleInverseFillType();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000447 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000448 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
449 GrPathRendererChain::DrawType type = element->isAA() ?
450 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
451 GrPathRendererChain::kStencilAndColor_DrawType;
joshualitt329bf482014-10-29 12:31:28 -0700452 *pr = this->getContext()->getPathRenderer(path, stroke, fClipTarget, false, type);
bsalomon49f085d2014-09-05 13:34:00 -0700453 return SkToBool(*pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000454 }
455}
456
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000457void GrClipMaskManager::mergeMask(GrTexture* dstMask,
458 GrTexture* srcMask,
459 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000460 const SkIRect& dstBound,
461 const SkIRect& srcBound) {
bsalomon@google.com137f1342013-05-29 21:27:53 +0000462 GrDrawState::AutoViewMatrixRestore avmr;
joshualitt329bf482014-10-29 12:31:28 -0700463 GrDrawState* drawState = fClipTarget->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000464 SkAssertResult(avmr.setIdentity(drawState));
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000465 GrDrawState::AutoRestoreEffects are(drawState);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000466
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000467 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000468
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000469 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000470
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000471 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000472 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000473
joshualittb0a8a372014-09-23 09:50:21 -0700474 drawState->addColorProcessor(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000475 GrTextureDomainEffect::Create(srcMask,
476 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000477 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
478 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000479 GrTextureParams::kNone_FilterMode))->unref();
joshualitt329bf482014-10-29 12:31:28 -0700480 fClipTarget->drawSimpleRect(SkRect::Make(dstBound));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000481}
482
bsalomon427cf282014-10-16 13:41:43 -0700483GrTexture* GrClipMaskManager::createTempMask(int width, int height) {
bsalomonf2703d82014-10-28 14:33:06 -0700484 GrSurfaceDesc desc;
485 desc.fFlags = kRenderTarget_GrSurfaceFlag|kNoStencil_GrSurfaceFlag;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000486 desc.fWidth = width;
487 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000488 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000489
joshualitt329bf482014-10-29 12:31:28 -0700490 return this->getContext()->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000491}
492
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000493////////////////////////////////////////////////////////////////////////////////
krajcevskiad1dc582014-06-10 15:06:47 -0700494// Return the texture currently in the cache if it exists. Otherwise, return NULL
495GrTexture* GrClipMaskManager::getCachedMaskTexture(int32_t elementsGenID,
496 const SkIRect& clipSpaceIBounds) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000497 bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000498 if (!cached) {
krajcevskiad1dc582014-06-10 15:06:47 -0700499 return NULL;
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000500 }
501
krajcevskiad1dc582014-06-10 15:06:47 -0700502 return fAACache.getLastMask();
503}
504
505////////////////////////////////////////////////////////////////////////////////
506// Allocate a texture in the texture cache. This function returns the texture
507// allocated (or NULL on error).
508GrTexture* GrClipMaskManager::allocMaskTexture(int32_t elementsGenID,
509 const SkIRect& clipSpaceIBounds,
510 bool willUpload) {
511 // Since we are setting up the cache we should free up the
512 // currently cached mask so it can be reused.
513 fAACache.reset();
514
bsalomonf2703d82014-10-28 14:33:06 -0700515 GrSurfaceDesc desc;
516 desc.fFlags = willUpload ? kNone_GrSurfaceFlags : kRenderTarget_GrSurfaceFlag;
krajcevskiad1dc582014-06-10 15:06:47 -0700517 desc.fWidth = clipSpaceIBounds.width();
518 desc.fHeight = clipSpaceIBounds.height();
519 desc.fConfig = kRGBA_8888_GrPixelConfig;
520 if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
521 // We would always like A8 but it isn't supported on all platforms
522 desc.fConfig = kAlpha_8_GrPixelConfig;
523 }
524
525 fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds);
526 return fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000527}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000528
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000529////////////////////////////////////////////////////////////////////////////////
530// Create a 8-bit clip mask in alpha
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000531GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700532 GrReducedClip::InitialState initialState,
533 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000534 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000535 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000536
krajcevskiad1dc582014-06-10 15:06:47 -0700537 // First, check for cached texture
538 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -0700539 if (result) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000540 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000541 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000542 }
543
krajcevskiad1dc582014-06-10 15:06:47 -0700544 // There's no texture in the cache. Let's try to allocate it then.
545 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, false);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000546 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000547 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000548 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000549 }
550
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000551 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000552 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000553 SkIntToScalar(-clipSpaceIBounds.fLeft),
554 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000555 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000556 // The texture may be larger than necessary, this rect represents the part of the texture
557 // we populate with a rasterization of the clip.
558 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
559
bsalomon@google.com137f1342013-05-29 21:27:53 +0000560 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
561 SkMatrix translate;
562 translate.setTranslate(clipToMaskOffset);
joshualitt329bf482014-10-29 12:31:28 -0700563 GrDrawTarget::AutoGeometryAndStatePush agasp(fClipTarget, GrDrawTarget::kReset_ASRInit,
564 &translate);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000565
joshualitt329bf482014-10-29 12:31:28 -0700566 GrDrawState* drawState = fClipTarget->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000567
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000568 // We're drawing a coverage mask and want coverage to be run through the blend function.
569 drawState->enableState(GrDrawState::kCoverageDrawing_StateBit);
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.
joshualitt329bf482014-10-29 12:31:28 -0700582 GrDrawTarget::AutoClipRestore acr(fClipTarget, maskSpaceIBounds);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000583 drawState->enableState(GrDrawState::kClip_StateBit);
584
bsalomon427cf282014-10-16 13:41:43 -0700585 SkAutoTUnref<GrTexture> temp;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000586 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700587 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000588 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000589 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000590 bool invert = element->isInverseFilled();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000591
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000592 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000593 GrPathRenderer* pr = NULL;
594 bool useTemp = !this->canStencilAndDrawElement(result, element, &pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000595 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000596 // 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 +0000597 // mask buffer can be substantially larger than the actually clip stack element. We
598 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000599 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000600 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000601
602 if (useTemp) {
603 if (invert) {
604 maskSpaceElementIBounds = maskSpaceIBounds;
605 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000606 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000607 elementBounds.offset(clipToMaskOffset);
608 elementBounds.roundOut(&maskSpaceElementIBounds);
609 }
610
bsalomon427cf282014-10-16 13:41:43 -0700611 if (!temp) {
612 temp.reset(this->createTempMask(maskSpaceIBounds.fRight,
613 maskSpaceIBounds.fBottom));
614 if (!temp) {
615 fAACache.reset();
616 return NULL;
617 }
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000618 }
bsalomon427cf282014-10-16 13:41:43 -0700619 dst = temp;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000620 // clear the temp target and set blend to replace
joshualitt329bf482014-10-29 12:31:28 -0700621 fClipTarget->clear(&maskSpaceElementIBounds,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000622 invert ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000623 true,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000624 dst->asRenderTarget());
625 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000626
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);
638 drawState->setStencil(kStencilInElement);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000639 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000640 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000641
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000642 drawState->setAlpha(invert ? 0x00 : 0xff);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000643
robertphillips@google.come79f3202014-02-11 16:30:21 +0000644 if (!this->drawElement(dst, element, pr)) {
645 fAACache.reset();
646 return NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000647 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000648
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000649 if (useTemp) {
650 // Now draw into the accumulator using the real operation and the temp buffer as a
651 // texture
652 this->mergeMask(result,
bsalomon427cf282014-10-16 13:41:43 -0700653 temp,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000654 op,
655 maskSpaceIBounds,
656 maskSpaceElementIBounds);
657 } else {
658 // Draw to the exterior pixels (those with a zero stencil value).
659 drawState->setAlpha(invert ? 0xff : 0x00);
660 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
661 kZero_StencilOp,
662 kZero_StencilOp,
663 kEqual_StencilFunc,
664 0xffff,
665 0x0000,
666 0xffff);
667 drawState->setStencil(kDrawOutsideElement);
joshualitt329bf482014-10-29 12:31:28 -0700668 fClipTarget->drawSimpleRect(clipSpaceIBounds);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000669 drawState->disableStencil();
670 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000671 } else {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000672 // all the remaining ops can just be directly draw into the accumulation buffer
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000673 drawState->setAlpha(0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000674 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000675 this->drawElement(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000676 }
677 }
678
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000679 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000680 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000681}
682
683////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000684// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000685// (as opposed to canvas) coordinates
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000686bool GrClipMaskManager::createStencilClipMask(int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700687 GrReducedClip::InitialState initialState,
688 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000689 const SkIRect& clipSpaceIBounds,
690 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000691
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000692 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000693
joshualitt329bf482014-10-29 12:31:28 -0700694 GrDrawState* drawState = fClipTarget->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000695 SkASSERT(drawState->isClipState());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000696
697 GrRenderTarget* rt = drawState->getRenderTarget();
bsalomon49f085d2014-09-05 13:34:00 -0700698 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000699
700 // TODO: dynamically attach a SB when needed.
701 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
702 if (NULL == stencilBuffer) {
703 return false;
704 }
705
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000706 if (stencilBuffer->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000707 stencilBuffer->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000708
bsalomon@google.com137f1342013-05-29 21:27:53 +0000709 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
710 SkVector translate = {
711 SkIntToScalar(clipSpaceToStencilOffset.fX),
712 SkIntToScalar(clipSpaceToStencilOffset.fY)
713 };
714 SkMatrix matrix;
715 matrix.setTranslate(translate);
joshualitt329bf482014-10-29 12:31:28 -0700716 GrDrawTarget::AutoGeometryAndStatePush agasp(fClipTarget, GrDrawTarget::kReset_ASRInit,
717 &matrix);
718 drawState = fClipTarget->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000719
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000720 drawState->setRenderTarget(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000721
bsalomon@google.com9f131742012-12-13 20:43:56 +0000722 // We set the current clip to the bounds so that our recursive draws are scissored to them.
723 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
724 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
joshualitt329bf482014-10-29 12:31:28 -0700725 GrDrawTarget::AutoClipRestore acr(fClipTarget, stencilSpaceIBounds);
bsalomon@google.com9f131742012-12-13 20:43:56 +0000726 drawState->enableState(GrDrawState::kClip_StateBit);
727
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000728#if !VISUALIZE_COMPLEX_CLIP
729 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
730#endif
731
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000732 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000733 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000734 clipBit = (1 << (clipBit-1));
735
joshualitt329bf482014-10-29 12:31:28 -0700736 fClipTarget->clearStencilClip(stencilSpaceIBounds,
737 GrReducedClip::kAllIn_InitialState == initialState,
738 rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000739
740 // walk through each clip element and perform its set op
741 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700742 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000743 const Element* element = iter.get();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000744 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000745 // enabled at bottom of loop
joshualitt329bf482014-10-29 12:31:28 -0700746 drawState->disableState(kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000747 // if the target is MSAA then we want MSAA enabled when the clip is soft
748 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000749 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000750 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000751
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000752 // This will be used to determine whether the clip shape can be rendered into the
753 // stencil with arbitrary stencil settings.
754 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000755
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000756 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000757
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000758 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000759
robertphillips@google.come79f3202014-02-11 16:30:21 +0000760 GrPathRenderer* pr = NULL;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000761 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000762 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000763 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000764 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000765 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000766 element->asPath(&clipPath);
767 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000768 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000769 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000770 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000771 pr = this->getContext()->getPathRenderer(clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000772 stroke,
joshualitt329bf482014-10-29 12:31:28 -0700773 fClipTarget,
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);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000807 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000808 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000809 *drawState->stencil() = gDrawToStencil;
joshualitt329bf482014-10-29 12:31:28 -0700810 fClipTarget->drawSimpleRect(element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000811 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000812 if (!clipPath.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000813 if (canRenderDirectToStencil) {
814 *drawState->stencil() = gDrawToStencil;
joshualitt329bf482014-10-29 12:31:28 -0700815 pr->drawPath(clipPath, stroke, fClipTarget, false);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000816 } else {
joshualitt329bf482014-10-29 12:31:28 -0700817 pr->stencilPath(clipPath, stroke, fClipTarget);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000818 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000819 }
820 }
821 }
822
823 // now we modify the clip bit by rendering either the clip
824 // element directly or a bounding rect of the entire clip.
joshualitt329bf482014-10-29 12:31:28 -0700825 drawState->enableState(kModifyStencilClip_StateBit);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000826 for (int p = 0; p < passes; ++p) {
827 *drawState->stencil() = stencilSettings[p];
828 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000829 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000830 SET_RANDOM_COLOR
joshualitt329bf482014-10-29 12:31:28 -0700831 fClipTarget->drawSimpleRect(element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000832 } else {
833 SET_RANDOM_COLOR
joshualitt329bf482014-10-29 12:31:28 -0700834 pr->drawPath(clipPath, stroke, fClipTarget, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000835 }
836 } else {
837 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000838 // The view matrix is setup to do clip space -> stencil space translation, so
839 // draw rect in clip space.
joshualitt329bf482014-10-29 12:31:28 -0700840 fClipTarget->drawSimpleRect(SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000841 }
842 }
843 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000844 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000845 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000846 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000847 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000848 return true;
849}
850
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000851
bsalomon@google.com411dad02012-06-05 20:24:20 +0000852// mapping of clip-respecting stencil funcs to normal stencil funcs
853// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000854static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000855 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
856 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
857 // In the Clip Funcs
858 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
859 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
860 kLess_StencilFunc, // kLessIfInClip_StencilFunc
861 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
862 // Special in the clip func that forces user's ref to be 0.
863 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
864 // make ref 0 and do normal nequal.
865 },
866 {// Stencil-Clipping is ENABLED
867 // In the Clip Funcs
868 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
869 // eq stencil clip bit, mask
870 // out user bits.
871
872 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
873 // add stencil bit to mask and ref
874
875 kLess_StencilFunc, // kLessIfInClip_StencilFunc
876 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
877 // for both of these we can add
878 // the clip bit to the mask and
879 // ref and compare as normal
880 // Special in the clip func that forces user's ref to be 0.
881 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
882 // make ref have only the clip bit set
883 // and make comparison be less
884 // 10..0 < 1..user_bits..
885 }
886};
887
bsalomon@google.coma3201942012-06-21 19:58:20 +0000888namespace {
889// Sets the settings to clip against the stencil buffer clip while ignoring the
890// client bits.
891const GrStencilSettings& basic_apply_stencil_clip_settings() {
892 // stencil settings to use when clip is in stencil
893 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
894 kKeep_StencilOp,
895 kKeep_StencilOp,
896 kAlwaysIfInClip_StencilFunc,
897 0x0000,
898 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000899 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000900 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
901}
902}
903
joshualitta58fe352014-10-27 08:39:00 -0700904void GrClipMaskManager::setDrawStateStencil(GrDrawState::AutoRestoreStencil* ars) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000905 // We make two copies of the StencilSettings here (except in the early
906 // exit scenario. One copy from draw state to the stack var. Then another
907 // from the stack var to the gpu. We could make this class hold a ptr to
908 // GrGpu's fStencilSettings and eliminate the stack copy here.
909
joshualitt329bf482014-10-29 12:31:28 -0700910 const GrDrawState& drawState = fClipTarget->getDrawState();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000911
912 // use stencil for clipping if clipping is enabled and the clip
913 // has been written into the stencil.
914 GrClipMaskManager::StencilClipMode clipMode;
915 if (this->isClipInStencil() && drawState.isClipState()) {
916 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
917 // We can't be modifying the clip and respecting it at the same time.
joshualitt329bf482014-10-29 12:31:28 -0700918 SkASSERT(!drawState.isStateFlagEnabled(kModifyStencilClip_StateBit));
919 } else if (drawState.isStateFlagEnabled(kModifyStencilClip_StateBit)) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000920 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
921 } else {
922 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
923 }
924
925 GrStencilSettings settings;
926 // The GrGpu client may not be using the stencil buffer but we may need to
927 // enable it in order to respect a stencil clip.
928 if (drawState.getStencil().isDisabled()) {
929 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
930 settings = basic_apply_stencil_clip_settings();
931 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000932 return;
933 }
934 } else {
935 settings = drawState.getStencil();
936 }
937
938 // TODO: dynamically attach a stencil buffer
939 int stencilBits = 0;
joshualitta58fe352014-10-27 08:39:00 -0700940 GrStencilBuffer* stencilBuffer = drawState.getRenderTarget()->getStencilBuffer();
bsalomon49f085d2014-09-05 13:34:00 -0700941 if (stencilBuffer) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000942 stencilBits = stencilBuffer->bits();
943 }
944
joshualitt329bf482014-10-29 12:31:28 -0700945 SkASSERT(fClipTarget->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
946 SkASSERT(fClipTarget->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000947 this->adjustStencilParams(&settings, clipMode, stencilBits);
joshualitt329bf482014-10-29 12:31:28 -0700948 ars->set(fClipTarget->drawState());
949 fClipTarget->drawState()->setStencil(settings);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000950}
951
952void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
953 StencilClipMode mode,
954 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000955 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000956
957 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000958 // We assume that this clip manager itself is drawing to the GrGpu and
959 // has already setup the correct values.
960 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000961 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000962
bsalomon@google.com411dad02012-06-05 20:24:20 +0000963 unsigned int clipBit = (1 << (stencilBitCnt - 1));
964 unsigned int userBits = clipBit - 1;
965
bsalomon@google.coma3201942012-06-21 19:58:20 +0000966 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
joshualitt329bf482014-10-29 12:31:28 -0700967 bool twoSided = fClipTarget->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000968
bsalomon@google.coma3201942012-06-21 19:58:20 +0000969 bool finished = false;
970 while (!finished) {
971 GrStencilFunc func = settings->func(face);
972 uint16_t writeMask = settings->writeMask(face);
973 uint16_t funcMask = settings->funcMask(face);
974 uint16_t funcRef = settings->funcRef(face);
975
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000976 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000977
978 writeMask &= userBits;
979
980 if (func >= kBasicStencilFuncCount) {
981 int respectClip = kRespectClip_StencilClipMode == mode;
982 if (respectClip) {
983 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000984 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000985 switch (func) {
986 case kAlwaysIfInClip_StencilFunc:
987 funcMask = clipBit;
988 funcRef = clipBit;
989 break;
990 case kEqualIfInClip_StencilFunc:
991 case kLessIfInClip_StencilFunc:
992 case kLEqualIfInClip_StencilFunc:
993 funcMask = (funcMask & userBits) | clipBit;
994 funcRef = (funcRef & userBits) | clipBit;
995 break;
996 case kNonZeroIfInClip_StencilFunc:
997 funcMask = (funcMask & userBits) | clipBit;
998 funcRef = clipBit;
999 break;
1000 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001001 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +00001002 }
1003 } else {
1004 funcMask &= userBits;
1005 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001006 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001007 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001008 gSpecialToBasicStencilFunc[respectClip];
1009 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001010 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001011 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001012 funcMask &= userBits;
1013 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001014 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001015
1016 settings->setFunc(face, func);
1017 settings->setWriteMask(face, writeMask);
1018 settings->setFuncMask(face, funcMask);
1019 settings->setFuncRef(face, funcRef);
1020
1021 if (GrStencilSettings::kFront_Face == face) {
1022 face = GrStencilSettings::kBack_Face;
1023 finished = !twoSided;
1024 } else {
1025 finished = true;
1026 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001027 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001028 if (!twoSided) {
1029 settings->copyFrontSettingsToBack();
1030 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001031}
1032
1033////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +00001034GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001035 GrReducedClip::InitialState initialState,
1036 const GrReducedClip::ElementList& elements,
1037 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001038 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001039
krajcevskiad1dc582014-06-10 15:06:47 -07001040 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -07001041 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001042 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001043 }
1044
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001045 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1046 // the top left corner of the resulting rect to the top left of the texture.
1047 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1048
robertphillips@google.com2c756812012-05-22 20:28:23 +00001049 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001050
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001051 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001052 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
1053 SkIntToScalar(-clipSpaceIBounds.fTop));
krajcevski71614ac2014-08-13 10:36:18 -07001054 helper.init(maskSpaceIBounds, &matrix, false);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001055
tfarinabf54e492014-10-23 17:47:18 -07001056 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001057
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001058 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001059
tfarinabf54e492014-10-23 17:47:18 -07001060 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001061
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001062 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001063 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001064
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001065 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1066 // Intersect and reverse difference require modifying pixels outside of the geometry
1067 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1068 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1069 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001070 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001071 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001072 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001073 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001074 }
1075
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
1081 continue;
1082 }
1083
1084 // The other ops (union, xor, diff) only affect pixels inside
1085 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001086 if (Element::kRect_Type == element->getType()) {
1087 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1088 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001089 SkPath path;
1090 element->asPath(&path);
1091 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001092 }
1093 }
1094
krajcevskiad1dc582014-06-10 15:06:47 -07001095 // Allocate clip mask texture
1096 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, true);
1097 if (NULL == result) {
1098 fAACache.reset();
1099 return NULL;
1100 }
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001101 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001102
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001103 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001104 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001105}
1106
robertphillips@google.comf294b772012-04-27 14:29:26 +00001107////////////////////////////////////////////////////////////////////////////////
bsalomonc8dc1f72014-08-21 13:02:13 -07001108void GrClipMaskManager::purgeResources() {
1109 fAACache.purgeResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001110}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001111
joshualitt329bf482014-10-29 12:31:28 -07001112void GrClipMaskManager::setClipTarget(GrClipTarget* clipTarget) {
1113 fClipTarget = clipTarget;
1114 fAACache.setContext(clipTarget->getContext());
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001115}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001116
1117void GrClipMaskManager::adjustPathStencilParams(GrStencilSettings* settings) {
joshualitt329bf482014-10-29 12:31:28 -07001118 const GrDrawState& drawState = fClipTarget->getDrawState();
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001119 GrClipMaskManager::StencilClipMode clipMode;
1120 if (this->isClipInStencil() && drawState.isClipState()) {
1121 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
1122 // We can't be modifying the clip and respecting it at the same time.
joshualitt329bf482014-10-29 12:31:28 -07001123 SkASSERT(!drawState.isStateFlagEnabled(kModifyStencilClip_StateBit));
1124 } else if (drawState.isStateFlagEnabled(kModifyStencilClip_StateBit)) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001125 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
1126 } else {
1127 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
1128 }
1129
1130 // TODO: dynamically attach a stencil buffer
1131 int stencilBits = 0;
joshualitt329bf482014-10-29 12:31:28 -07001132 GrStencilBuffer* stencilBuffer = drawState.getRenderTarget()->getStencilBuffer();
bsalomon49f085d2014-09-05 13:34:00 -07001133 if (stencilBuffer) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001134 stencilBits = stencilBuffer->bits();
1135 this->adjustStencilParams(settings, clipMode, stencilBits);
1136 }
1137}