blob: 3ecc922999d34d139c2b82303b8b8da28ad183a1 [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"
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000020#include "effects/GrTextureDomain.h"
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +000021#include "effects/GrConvexPolyEffect.h"
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +000022#include "effects/GrRRectEffect.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000023#include "SkRasterClip.h"
24#include "SkStrokeRec.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000025#include "SkTLazy.h"
26
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
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000031using namespace GrReducedClip;
32
bsalomon@google.com51a62862012-11-26 21:19:43 +000033////////////////////////////////////////////////////////////////////////////////
robertphillips@google.come79f3202014-02-11 16:30:21 +000034namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000035// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000036// stage matrix this also alters the vertex layout
robertphillips@google.come79f3202014-02-11 16:30:21 +000037void setup_drawstate_aaclip(GrGpu* gpu,
38 GrTexture* result,
39 const SkIRect &devBound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000040 GrDrawState* drawState = gpu->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000041 SkASSERT(drawState);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000042
bsalomon@google.comb9086a02012-11-01 18:02:54 +000043 SkMatrix mat;
bsalomon@google.comc7818882013-03-20 19:19:53 +000044 // We want to use device coords to compute the texture coordinates. We set our matrix to be
45 // equal to the view matrix followed by an offset to the devBound, and then a scaling matrix to
46 // normalized coords. We apply this matrix to the vertex positions rather than local coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000047 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000048 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000049 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000050 mat.preConcat(drawState->getViewMatrix());
51
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000052 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000053 // This could be a long-lived effect that is cached with the alpha-mask.
joshualittb0a8a372014-09-23 09:50:21 -070054 drawState->addCoverageProcessor(
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000055 GrTextureDomainEffect::Create(result,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000056 mat,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000057 GrTextureDomain::MakeTexelDomain(result, domainTexels),
58 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +000059 GrTextureParams::kNone_FilterMode,
bsalomon@google.com77af6802013-10-02 13:04:56 +000060 kPosition_GrCoordSet))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000061}
62
robertphillips@google.come79f3202014-02-11 16:30:21 +000063bool path_needs_SW_renderer(GrContext* context,
64 GrGpu* gpu,
65 const SkPath& origPath,
66 const SkStrokeRec& stroke,
67 bool doAA) {
68 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
69 SkTCopyOnFirstWrite<SkPath> path(origPath);
70 if (path->isInverseFillType()) {
71 path.writable()->toggleInverseFillType();
72 }
73 // last (false) parameter disallows use of the SW path renderer
bsalomon@google.com45a15f52012-12-10 19:10:17 +000074 GrPathRendererChain::DrawType type = doAA ?
75 GrPathRendererChain::kColorAntiAlias_DrawType :
76 GrPathRendererChain::kColor_DrawType;
77
robertphillips@google.come79f3202014-02-11 16:30:21 +000078 return NULL == context->getPathRenderer(*path, stroke, gpu, false, type);
79}
80
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000081}
82
robertphillips@google.comfa662942012-05-17 12:20:22 +000083/*
84 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
85 * will be used on any element. If so, it returns true to indicate that the
86 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
87 */
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000088bool GrClipMaskManager::useSWOnlyPath(const ElementList& elements) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +000089
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000090 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000091 // a clip gets complex enough it can just be done in SW regardless
92 // of whether it would invoke the GrSoftwarePathRenderer.
sugoi@google.com5f74cf82012-12-17 21:16:45 +000093 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000094
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000095 for (ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
96 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000097 // rects can always be drawn directly w/o using the software path
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000098 // Skip rrects once we're drawing them directly.
99 if (Element::kRect_Type != element->getType()) {
100 SkPath path;
101 element->asPath(&path);
102 if (path_needs_SW_renderer(this->getContext(), fGpu, path, stroke, element->isAA())) {
103 return true;
104 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000105 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000106 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000107 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000108}
109
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000110bool GrClipMaskManager::installClipEffects(const ElementList& elements,
111 GrDrawState::AutoRestoreEffects* are,
112 const SkVector& clipToRTOffset,
mtklein217daa72014-07-02 12:55:21 -0700113 const SkRect* drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000114
115 GrDrawState* drawState = fGpu->drawState();
116 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700117 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000118 boundsInClipSpace = *drawBounds;
119 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
120 }
121
122 are->set(drawState);
123 GrRenderTarget* rt = drawState->getRenderTarget();
mtklein217daa72014-07-02 12:55:21 -0700124 ElementList::Iter iter(elements);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000125
126 bool setARE = false;
127 bool failed = false;
128
bsalomon49f085d2014-09-05 13:34:00 -0700129 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000130 SkRegion::Op op = iter.get()->getOp();
131 bool invert;
132 bool skip = false;
133 switch (op) {
134 case SkRegion::kReplace_Op:
135 SkASSERT(iter.get() == elements.head());
136 // Fallthrough, handled same as intersect.
137 case SkRegion::kIntersect_Op:
138 invert = false;
bsalomon49f085d2014-09-05 13:34:00 -0700139 if (drawBounds && iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000140 skip = true;
141 }
142 break;
143 case SkRegion::kDifference_Op:
144 invert = true;
145 // We don't currently have a cheap test for whether a rect is fully outside an
146 // element's primitive, so don't attempt to set skip.
147 break;
148 default:
149 failed = true;
150 break;
151 }
152 if (failed) {
153 break;
154 }
155
156 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700157 GrPrimitiveEdgeType edgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000158 if (GR_AA_CLIP && iter.get()->isAA()) {
159 if (rt->isMultisampled()) {
mtklein217daa72014-07-02 12:55:21 -0700160 // Coverage based AA clips don't place nicely with MSAA.
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000161 failed = true;
162 break;
163 }
joshualittb0a8a372014-09-23 09:50:21 -0700164 edgeType =
165 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000166 } else {
joshualittb0a8a372014-09-23 09:50:21 -0700167 edgeType =
168 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000169 }
joshualittb0a8a372014-09-23 09:50:21 -0700170 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000171 switch (iter.get()->getType()) {
172 case SkClipStack::Element::kPath_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700173 fp.reset(GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000174 &clipToRTOffset));
175 break;
176 case SkClipStack::Element::kRRect_Type: {
177 SkRRect rrect = iter.get()->getRRect();
178 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
joshualittb0a8a372014-09-23 09:50:21 -0700179 fp.reset(GrRRectEffect::Create(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000180 break;
181 }
182 case SkClipStack::Element::kRect_Type: {
183 SkRect rect = iter.get()->getRect();
184 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
joshualittb0a8a372014-09-23 09:50:21 -0700185 fp.reset(GrConvexPolyEffect::Create(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000186 break;
187 }
188 default:
189 break;
190 }
joshualittb0a8a372014-09-23 09:50:21 -0700191 if (fp) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000192 if (!setARE) {
193 are->set(fGpu->drawState());
194 setARE = true;
195 }
joshualittb0a8a372014-09-23 09:50:21 -0700196 fGpu->drawState()->addCoverageProcessor(fp);
mtklein217daa72014-07-02 12:55:21 -0700197 } else {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000198 failed = true;
199 break;
200 }
201 }
mtklein217daa72014-07-02 12:55:21 -0700202 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000203 }
204
205 if (failed) {
206 are->set(NULL);
207 }
208
209 return !failed;
210}
211
robertphillips@google.comf294b772012-04-27 14:29:26 +0000212////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000213// sort out what kind of clip mask needs to be created: alpha, stencil,
214// scissor, or entirely software
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000215bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn,
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000216 GrDrawState::AutoRestoreEffects* are,
217 const SkRect* devBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000218 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000219
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000220 ElementList elements(16);
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000221 int32_t genID;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000222 InitialState initialState;
223 SkIRect clipSpaceIBounds;
224 bool requiresAA;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000225
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000226 GrDrawState* drawState = fGpu->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();
233
234 if (!ignoreClip) {
235 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
236 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
237 ReduceClipStack(*clipDataIn->fClipStack,
238 clipSpaceRTIBounds,
239 &elements,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000240 &genID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000241 &initialState,
242 &clipSpaceIBounds,
243 &requiresAA);
244 if (elements.isEmpty()) {
245 if (kAllIn_InitialState == initialState) {
246 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000247 } else {
248 return false;
249 }
250 }
251 }
252
253 if (ignoreClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000254 fGpu->disableScissor();
255 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000256 return true;
257 }
258
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000259 // An element count of 4 was chosen because of the common pattern in Blink of:
260 // isect RR
261 // diff RR
262 // isect convex_poly
263 // isect convex_poly
264 // when drawing rounded div borders. This could probably be tuned based on a
265 // configuration's relative costs of switching RTs to generate a mask vs
266 // longer shaders.
267 if (elements.count() <= 4) {
268 SkVector clipToRTOffset = { SkIntToScalar(-clipDataIn->fOrigin.fX),
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000269 SkIntToScalar(-clipDataIn->fOrigin.fY) };
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000270 if (elements.isEmpty() ||
mtklein217daa72014-07-02 12:55:21 -0700271 (requiresAA && this->installClipEffects(elements, are, clipToRTOffset, devBounds))) {
272 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
273 scissorSpaceIBounds.offset(-clipDataIn->fOrigin);
274 if (NULL == devBounds ||
275 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
276 fGpu->enableScissor(scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000277 } else {
mtklein217daa72014-07-02 12:55:21 -0700278 fGpu->disableScissor();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000279 }
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000280 this->setGpuStencil();
281 return true;
282 }
283 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000284
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000285#if GR_AA_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000286 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000287 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000288 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000289
290 if (this->useSWOnlyPath(elements)) {
291 // The clip geometry is complex enough that it will be more efficient to create it
292 // entirely in software
293 result = this->createSoftwareClipMask(genID,
294 initialState,
295 elements,
296 clipSpaceIBounds);
297 } else {
298 result = this->createAlphaClipMask(genID,
299 initialState,
300 elements,
301 clipSpaceIBounds);
302 }
303
bsalomon49f085d2014-09-05 13:34:00 -0700304 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000305 // The mask's top left coord should be pinned to the rounded-out top left corner of
306 // clipSpace bounds. We determine the mask's position WRT to the render target here.
307 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
308 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000309 are->set(fGpu->drawState());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000310 setup_drawstate_aaclip(fGpu, result, rtSpaceMaskBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000311 fGpu->disableScissor();
312 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000313 return true;
314 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000315 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000316 }
317#endif // GR_AA_CLIP
318
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000319 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
320 // be created. In either case, free up the texture in the anti-aliased mask cache.
321 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
322 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
323 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000324 fAACache.reset();
325
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000326 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000327 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000328 this->createStencilClipMask(genID,
329 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000330 elements,
331 clipSpaceIBounds,
332 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000333
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000334 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
335 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
336 // use both stencil and scissor test to the bounds for the final draw.
337 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
338 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
339 fGpu->enableScissor(scissorSpaceIBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000340 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000341 return true;
342}
343
344#define VISUALIZE_COMPLEX_CLIP 0
345
346#if VISUALIZE_COMPLEX_CLIP
tfarina@chromium.org223137f2012-11-21 22:38:36 +0000347 #include "SkRandom.h"
348 SkRandom gRandom;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000349 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
350#else
351 #define SET_RANDOM_COLOR
352#endif
353
354namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000355
356////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000357// set up the OpenGL blend function to perform the specified
358// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000359void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000360
361 switch (op) {
362 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000363 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000364 break;
365 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000366 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000367 break;
368 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000369 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000370 break;
371 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000372 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000373 break;
374 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000375 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000376 break;
377 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000378 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000379 break;
380 default:
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000381 SkASSERT(false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000382 break;
383 }
384}
385
robertphillips@google.com72176b22012-05-23 13:19:12 +0000386}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000387
388////////////////////////////////////////////////////////////////////////////////
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000389bool GrClipMaskManager::drawElement(GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000390 const SkClipStack::Element* element,
391 GrPathRenderer* pr) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000392 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000393
394 drawState->setRenderTarget(target->asRenderTarget());
395
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000396 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000397 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000398 case Element::kEmpty_Type:
399 SkDEBUGFAIL("Should never get here with an empty element.");
400 break;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000401 case Element::kRect_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700402 // TODO: Do rects directly to the accumulator using a aa-rect GrProcessor that covers
403 // the entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000404 if (element->isAA()) {
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000405 getContext()->getAARectRenderer()->fillAARect(fGpu,
406 fGpu,
407 element->getRect(),
robertphillips@google.comb19cb7f2013-05-02 15:37:20 +0000408 SkMatrix::I(),
bsalomon9c0822a2014-08-11 11:07:48 -0700409 element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000410 } else {
bsalomon01c8da12014-08-04 09:21:30 -0700411 fGpu->drawSimpleRect(element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000412 }
413 return true;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000414 default: {
415 SkPath path;
416 element->asPath(&path);
417 if (path.isInverseFillType()) {
418 path.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000419 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000420 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000421 if (NULL == pr) {
422 GrPathRendererChain::DrawType type;
423 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
424 GrPathRendererChain::kColor_DrawType;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000425 pr = this->getContext()->getPathRenderer(path, stroke, fGpu, false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000426 }
427 if (NULL == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000428 return false;
429 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000430 pr->drawPath(path, stroke, fGpu, element->isAA());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000431 break;
432 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000433 }
434 return true;
435}
436
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000437bool GrClipMaskManager::canStencilAndDrawElement(GrTexture* target,
438 const SkClipStack::Element* element,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000439 GrPathRenderer** pr) {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000440 GrDrawState* drawState = fGpu->drawState();
441 drawState->setRenderTarget(target->asRenderTarget());
442
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000443 if (Element::kRect_Type == element->getType()) {
444 return true;
445 } else {
446 // We shouldn't get here with an empty clip element.
447 SkASSERT(Element::kEmpty_Type != element->getType());
448 SkPath path;
449 element->asPath(&path);
450 if (path.isInverseFillType()) {
451 path.toggleInverseFillType();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000452 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000453 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
454 GrPathRendererChain::DrawType type = element->isAA() ?
455 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
456 GrPathRendererChain::kStencilAndColor_DrawType;
457 *pr = this->getContext()->getPathRenderer(path, stroke, fGpu, false, type);
bsalomon49f085d2014-09-05 13:34:00 -0700458 return SkToBool(*pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000459 }
460}
461
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000462void GrClipMaskManager::mergeMask(GrTexture* dstMask,
463 GrTexture* srcMask,
464 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000465 const SkIRect& dstBound,
466 const SkIRect& srcBound) {
bsalomon@google.com137f1342013-05-29 21:27:53 +0000467 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000468 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000469 SkAssertResult(avmr.setIdentity(drawState));
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000470 GrDrawState::AutoRestoreEffects are(drawState);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000471
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000472 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000473
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000474 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000475
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000476 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000477 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000478
joshualittb0a8a372014-09-23 09:50:21 -0700479 drawState->addColorProcessor(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000480 GrTextureDomainEffect::Create(srcMask,
481 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000482 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
483 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000484 GrTextureParams::kNone_FilterMode))->unref();
bsalomon01c8da12014-08-04 09:21:30 -0700485 fGpu->drawSimpleRect(SkRect::Make(dstBound));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000486}
487
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000488// get a texture to act as a temporary buffer for AA clip boolean operations
489// TODO: given the expense of createTexture we may want to just cache this too
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000490void GrClipMaskManager::getTemp(int width, int height, GrAutoScratchTexture* temp) {
bsalomon49f085d2014-09-05 13:34:00 -0700491 if (temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000492 // we've already allocated the temp texture
493 return;
494 }
495
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000496 GrTextureDesc desc;
497 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000498 desc.fWidth = width;
499 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000500 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000501
robertphillips@google.com2c756812012-05-22 20:28:23 +0000502 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000503}
504
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000505////////////////////////////////////////////////////////////////////////////////
krajcevskiad1dc582014-06-10 15:06:47 -0700506// Return the texture currently in the cache if it exists. Otherwise, return NULL
507GrTexture* GrClipMaskManager::getCachedMaskTexture(int32_t elementsGenID,
508 const SkIRect& clipSpaceIBounds) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000509 bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000510 if (!cached) {
krajcevskiad1dc582014-06-10 15:06:47 -0700511 return NULL;
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000512 }
513
krajcevskiad1dc582014-06-10 15:06:47 -0700514 return fAACache.getLastMask();
515}
516
517////////////////////////////////////////////////////////////////////////////////
518// Allocate a texture in the texture cache. This function returns the texture
519// allocated (or NULL on error).
520GrTexture* GrClipMaskManager::allocMaskTexture(int32_t elementsGenID,
521 const SkIRect& clipSpaceIBounds,
522 bool willUpload) {
523 // Since we are setting up the cache we should free up the
524 // currently cached mask so it can be reused.
525 fAACache.reset();
526
527 GrTextureDesc desc;
528 desc.fFlags = willUpload ? kNone_GrTextureFlags : kRenderTarget_GrTextureFlagBit;
529 desc.fWidth = clipSpaceIBounds.width();
530 desc.fHeight = clipSpaceIBounds.height();
531 desc.fConfig = kRGBA_8888_GrPixelConfig;
532 if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
533 // We would always like A8 but it isn't supported on all platforms
534 desc.fConfig = kAlpha_8_GrPixelConfig;
535 }
536
537 fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds);
538 return fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000539}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000540
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000541////////////////////////////////////////////////////////////////////////////////
542// Create a 8-bit clip mask in alpha
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000543GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000544 InitialState initialState,
545 const ElementList& elements,
546 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000547 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000548
krajcevskiad1dc582014-06-10 15:06:47 -0700549 // First, check for cached texture
550 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -0700551 if (result) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000552 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000553 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000554 }
555
krajcevskiad1dc582014-06-10 15:06:47 -0700556 // There's no texture in the cache. Let's try to allocate it then.
557 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, false);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000558 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000559 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000560 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000561 }
562
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000563 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000564 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000565 SkIntToScalar(-clipSpaceIBounds.fLeft),
566 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000567 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000568 // The texture may be larger than necessary, this rect represents the part of the texture
569 // we populate with a rasterization of the clip.
570 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
571
bsalomon@google.com137f1342013-05-29 21:27:53 +0000572 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
573 SkMatrix translate;
574 translate.setTranslate(clipToMaskOffset);
575 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &translate);
576
577 GrDrawState* drawState = fGpu->drawState();
578
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000579 // We're drawing a coverage mask and want coverage to be run through the blend function.
580 drawState->enableState(GrDrawState::kCoverageDrawing_StateBit);
581
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000582 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
583 // clear the part that we care about.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000584 fGpu->clear(&maskSpaceIBounds,
585 kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000586 true,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000587 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000588
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000589 // When we use the stencil in the below loop it is important to have this clip installed.
590 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
591 // pass must not set values outside of this bounds or stencil values outside the rect won't be
592 // cleared.
593 GrDrawTarget::AutoClipRestore acr(fGpu, maskSpaceIBounds);
594 drawState->enableState(GrDrawState::kClip_StateBit);
595
robertphillips@google.comf105b102012-05-14 12:18:26 +0000596 GrAutoScratchTexture temp;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000597 // walk through each clip element and perform its set op
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000598 for (ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000599 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000600 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000601 bool invert = element->isInverseFilled();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000602
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000603 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000604 GrPathRenderer* pr = NULL;
605 bool useTemp = !this->canStencilAndDrawElement(result, element, &pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000606 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000607 // 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 +0000608 // mask buffer can be substantially larger than the actually clip stack element. We
609 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000610 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000611 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000612
613 if (useTemp) {
614 if (invert) {
615 maskSpaceElementIBounds = maskSpaceIBounds;
616 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000617 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000618 elementBounds.offset(clipToMaskOffset);
619 elementBounds.roundOut(&maskSpaceElementIBounds);
620 }
621
622 this->getTemp(maskSpaceIBounds.fRight, maskSpaceIBounds.fBottom, &temp);
623 if (NULL == temp.texture()) {
624 fAACache.reset();
625 return NULL;
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000626 }
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000627 dst = temp.texture();
628 // clear the temp target and set blend to replace
629 fGpu->clear(&maskSpaceElementIBounds,
630 invert ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000631 true,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000632 dst->asRenderTarget());
633 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000634
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000635 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000636 // draw directly into the result with the stencil set to make the pixels affected
637 // by the clip shape be non-zero.
638 dst = result;
639 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
640 kReplace_StencilOp,
641 kReplace_StencilOp,
642 kAlways_StencilFunc,
643 0xffff,
644 0xffff,
645 0xffff);
646 drawState->setStencil(kStencilInElement);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000647 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000648 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000649
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000650 drawState->setAlpha(invert ? 0x00 : 0xff);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000651
robertphillips@google.come79f3202014-02-11 16:30:21 +0000652 if (!this->drawElement(dst, element, pr)) {
653 fAACache.reset();
654 return NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000655 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000656
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000657 if (useTemp) {
658 // Now draw into the accumulator using the real operation and the temp buffer as a
659 // texture
660 this->mergeMask(result,
661 temp.texture(),
662 op,
663 maskSpaceIBounds,
664 maskSpaceElementIBounds);
665 } else {
666 // Draw to the exterior pixels (those with a zero stencil value).
667 drawState->setAlpha(invert ? 0xff : 0x00);
668 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
669 kZero_StencilOp,
670 kZero_StencilOp,
671 kEqual_StencilFunc,
672 0xffff,
673 0x0000,
674 0xffff);
675 drawState->setStencil(kDrawOutsideElement);
676 fGpu->drawSimpleRect(clipSpaceIBounds);
677 drawState->disableStencil();
678 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000679 } else {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000680 // all the remaining ops can just be directly draw into the accumulation buffer
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000681 drawState->setAlpha(0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000682 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000683 this->drawElement(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000684 }
685 }
686
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000687 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000688 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000689}
690
691////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000692// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000693// (as opposed to canvas) coordinates
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000694bool GrClipMaskManager::createStencilClipMask(int32_t elementsGenID,
695 InitialState initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000696 const ElementList& elements,
697 const SkIRect& clipSpaceIBounds,
698 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000699
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000700 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000701
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000702 GrDrawState* drawState = fGpu->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000703 SkASSERT(drawState->isClipState());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000704
705 GrRenderTarget* rt = drawState->getRenderTarget();
bsalomon49f085d2014-09-05 13:34:00 -0700706 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000707
708 // TODO: dynamically attach a SB when needed.
709 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
710 if (NULL == stencilBuffer) {
711 return false;
712 }
713
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000714 if (stencilBuffer->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000715
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000716 stencilBuffer->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000717
bsalomon@google.com137f1342013-05-29 21:27:53 +0000718 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
719 SkVector translate = {
720 SkIntToScalar(clipSpaceToStencilOffset.fX),
721 SkIntToScalar(clipSpaceToStencilOffset.fY)
722 };
723 SkMatrix matrix;
724 matrix.setTranslate(translate);
725 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &matrix);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000726 drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000727
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000728 drawState->setRenderTarget(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000729
bsalomon@google.com9f131742012-12-13 20:43:56 +0000730 // We set the current clip to the bounds so that our recursive draws are scissored to them.
731 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
732 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
733 GrDrawTarget::AutoClipRestore acr(fGpu, stencilSpaceIBounds);
734 drawState->enableState(GrDrawState::kClip_StateBit);
735
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000736#if !VISUALIZE_COMPLEX_CLIP
737 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
738#endif
739
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000740 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000741 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000742 clipBit = (1 << (clipBit-1));
743
bsalomonb0bd4f62014-09-03 07:19:50 -0700744 fGpu->clearStencilClip(rt, stencilSpaceIBounds, kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000745
746 // walk through each clip element and perform its set op
747 // with the existing clip.
bsalomon49f085d2014-09-05 13:34:00 -0700748 for (ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000749 const Element* element = iter.get();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000750 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000751 // enabled at bottom of loop
752 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000753 // if the target is MSAA then we want MSAA enabled when the clip is soft
754 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000755 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000756 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000757
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000758 // This will be used to determine whether the clip shape can be rendered into the
759 // stencil with arbitrary stencil settings.
760 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000761
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000762 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000763
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000764 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000765
robertphillips@google.come79f3202014-02-11 16:30:21 +0000766 GrPathRenderer* pr = NULL;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000767 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000768 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000769 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000770 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000771 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000772 element->asPath(&clipPath);
773 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000774 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000775 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000776 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000777 pr = this->getContext()->getPathRenderer(clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000778 stroke,
779 fGpu,
780 false,
781 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000782 &stencilSupport);
783 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000784 return false;
785 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000786 }
787
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000788 int passes;
789 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
790
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000791 bool canRenderDirectToStencil =
792 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000793 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000794 // fill rule, and set operation can
795 // we render the element directly to
796 // stencil bit used for clipping.
797 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
798 canRenderDirectToStencil,
799 clipBit,
800 fillInverted,
801 &passes,
802 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000803
804 // draw the element to the client stencil bits if necessary
805 if (!canDrawDirectToClip) {
806 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000807 kIncClamp_StencilOp,
808 kIncClamp_StencilOp,
809 kAlways_StencilFunc,
810 0xffff,
811 0x0000,
812 0xffff);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000813 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000814 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000815 *drawState->stencil() = gDrawToStencil;
bsalomon01c8da12014-08-04 09:21:30 -0700816 fGpu->drawSimpleRect(element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000817 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000818 if (!clipPath.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000819 if (canRenderDirectToStencil) {
820 *drawState->stencil() = gDrawToStencil;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000821 pr->drawPath(clipPath, stroke, fGpu, false);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000822 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000823 pr->stencilPath(clipPath, stroke, fGpu);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000824 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000825 }
826 }
827 }
828
829 // now we modify the clip bit by rendering either the clip
830 // element directly or a bounding rect of the entire clip.
831 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
832 for (int p = 0; p < passes; ++p) {
833 *drawState->stencil() = stencilSettings[p];
834 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000835 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000836 SET_RANDOM_COLOR
bsalomon01c8da12014-08-04 09:21:30 -0700837 fGpu->drawSimpleRect(element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000838 } else {
839 SET_RANDOM_COLOR
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000840 pr->drawPath(clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000841 }
842 } else {
843 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000844 // The view matrix is setup to do clip space -> stencil space translation, so
845 // draw rect in clip space.
bsalomon01c8da12014-08-04 09:21:30 -0700846 fGpu->drawSimpleRect(SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000847 }
848 }
849 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000850 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000851 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000852 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000853 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000854 return true;
855}
856
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000857
bsalomon@google.com411dad02012-06-05 20:24:20 +0000858// mapping of clip-respecting stencil funcs to normal stencil funcs
859// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000860static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000861 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
862 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
863 // In the Clip Funcs
864 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
865 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
866 kLess_StencilFunc, // kLessIfInClip_StencilFunc
867 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
868 // Special in the clip func that forces user's ref to be 0.
869 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
870 // make ref 0 and do normal nequal.
871 },
872 {// Stencil-Clipping is ENABLED
873 // In the Clip Funcs
874 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
875 // eq stencil clip bit, mask
876 // out user bits.
877
878 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
879 // add stencil bit to mask and ref
880
881 kLess_StencilFunc, // kLessIfInClip_StencilFunc
882 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
883 // for both of these we can add
884 // the clip bit to the mask and
885 // ref and compare as normal
886 // Special in the clip func that forces user's ref to be 0.
887 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
888 // make ref have only the clip bit set
889 // and make comparison be less
890 // 10..0 < 1..user_bits..
891 }
892};
893
bsalomon@google.coma3201942012-06-21 19:58:20 +0000894namespace {
895// Sets the settings to clip against the stencil buffer clip while ignoring the
896// client bits.
897const GrStencilSettings& basic_apply_stencil_clip_settings() {
898 // stencil settings to use when clip is in stencil
899 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
900 kKeep_StencilOp,
901 kKeep_StencilOp,
902 kAlwaysIfInClip_StencilFunc,
903 0x0000,
904 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000905 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000906 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
907}
908}
909
910void GrClipMaskManager::setGpuStencil() {
911 // We make two copies of the StencilSettings here (except in the early
912 // exit scenario. One copy from draw state to the stack var. Then another
913 // from the stack var to the gpu. We could make this class hold a ptr to
914 // GrGpu's fStencilSettings and eliminate the stack copy here.
915
916 const GrDrawState& drawState = fGpu->getDrawState();
917
918 // use stencil for clipping if clipping is enabled and the clip
919 // has been written into the stencil.
920 GrClipMaskManager::StencilClipMode clipMode;
921 if (this->isClipInStencil() && drawState.isClipState()) {
922 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
923 // We can't be modifying the clip and respecting it at the same time.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000924 SkASSERT(!drawState.isStateFlagEnabled(
bsalomon@google.coma3201942012-06-21 19:58:20 +0000925 GrGpu::kModifyStencilClip_StateBit));
926 } else if (drawState.isStateFlagEnabled(
927 GrGpu::kModifyStencilClip_StateBit)) {
928 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
929 } else {
930 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
931 }
932
933 GrStencilSettings settings;
934 // The GrGpu client may not be using the stencil buffer but we may need to
935 // enable it in order to respect a stencil clip.
936 if (drawState.getStencil().isDisabled()) {
937 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
938 settings = basic_apply_stencil_clip_settings();
939 } else {
940 fGpu->disableStencil();
941 return;
942 }
943 } else {
944 settings = drawState.getStencil();
945 }
946
947 // TODO: dynamically attach a stencil buffer
948 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000949 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000950 drawState.getRenderTarget()->getStencilBuffer();
bsalomon49f085d2014-09-05 13:34:00 -0700951 if (stencilBuffer) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000952 stencilBits = stencilBuffer->bits();
953 }
954
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000955 SkASSERT(fGpu->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
956 SkASSERT(fGpu->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000957 this->adjustStencilParams(&settings, clipMode, stencilBits);
958 fGpu->setStencilSettings(settings);
959}
960
961void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
962 StencilClipMode mode,
963 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000964 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000965
966 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000967 // We assume that this clip manager itself is drawing to the GrGpu and
968 // has already setup the correct values.
969 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000970 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000971
bsalomon@google.com411dad02012-06-05 20:24:20 +0000972 unsigned int clipBit = (1 << (stencilBitCnt - 1));
973 unsigned int userBits = clipBit - 1;
974
bsalomon@google.coma3201942012-06-21 19:58:20 +0000975 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000976 bool twoSided = fGpu->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000977
bsalomon@google.coma3201942012-06-21 19:58:20 +0000978 bool finished = false;
979 while (!finished) {
980 GrStencilFunc func = settings->func(face);
981 uint16_t writeMask = settings->writeMask(face);
982 uint16_t funcMask = settings->funcMask(face);
983 uint16_t funcRef = settings->funcRef(face);
984
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000985 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000986
987 writeMask &= userBits;
988
989 if (func >= kBasicStencilFuncCount) {
990 int respectClip = kRespectClip_StencilClipMode == mode;
991 if (respectClip) {
992 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000993 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000994 switch (func) {
995 case kAlwaysIfInClip_StencilFunc:
996 funcMask = clipBit;
997 funcRef = clipBit;
998 break;
999 case kEqualIfInClip_StencilFunc:
1000 case kLessIfInClip_StencilFunc:
1001 case kLEqualIfInClip_StencilFunc:
1002 funcMask = (funcMask & userBits) | clipBit;
1003 funcRef = (funcRef & userBits) | clipBit;
1004 break;
1005 case kNonZeroIfInClip_StencilFunc:
1006 funcMask = (funcMask & userBits) | clipBit;
1007 funcRef = clipBit;
1008 break;
1009 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001010 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +00001011 }
1012 } else {
1013 funcMask &= userBits;
1014 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001015 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001016 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001017 gSpecialToBasicStencilFunc[respectClip];
1018 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001019 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001020 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001021 funcMask &= userBits;
1022 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001023 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001024
1025 settings->setFunc(face, func);
1026 settings->setWriteMask(face, writeMask);
1027 settings->setFuncMask(face, funcMask);
1028 settings->setFuncRef(face, funcRef);
1029
1030 if (GrStencilSettings::kFront_Face == face) {
1031 face = GrStencilSettings::kBack_Face;
1032 finished = !twoSided;
1033 } else {
1034 finished = true;
1035 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001036 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001037 if (!twoSided) {
1038 settings->copyFrontSettingsToBack();
1039 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001040}
1041
1042////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +00001043GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001044 GrReducedClip::InitialState initialState,
1045 const GrReducedClip::ElementList& elements,
1046 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001047 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001048
krajcevskiad1dc582014-06-10 15:06:47 -07001049 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
bsalomon49f085d2014-09-05 13:34:00 -07001050 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001051 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001052 }
1053
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001054 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1055 // the top left corner of the resulting rect to the top left of the texture.
1056 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1057
robertphillips@google.com2c756812012-05-22 20:28:23 +00001058 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001059
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001060 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001061 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
1062 SkIntToScalar(-clipSpaceIBounds.fTop));
krajcevski71614ac2014-08-13 10:36:18 -07001063 helper.init(maskSpaceIBounds, &matrix, false);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001064
1065 helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001066
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001067 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001068
bsalomon49f085d2014-09-05 13:34:00 -07001069 for (ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001070
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001071 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001072 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001073
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001074 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1075 // Intersect and reverse difference require modifying pixels outside of the geometry
1076 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1077 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1078 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001079 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001080 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001081 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001082 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001083 }
1084
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001085 SkPath clipPath;
1086 element->asPath(&clipPath);
1087 clipPath.toggleInverseFillType();
1088 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001089
1090 continue;
1091 }
1092
1093 // The other ops (union, xor, diff) only affect pixels inside
1094 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001095 if (Element::kRect_Type == element->getType()) {
1096 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1097 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001098 SkPath path;
1099 element->asPath(&path);
1100 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001101 }
1102 }
1103
krajcevskiad1dc582014-06-10 15:06:47 -07001104 // Allocate clip mask texture
1105 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, true);
1106 if (NULL == result) {
1107 fAACache.reset();
1108 return NULL;
1109 }
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001110 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001111
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001112 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001113 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001114}
1115
robertphillips@google.comf294b772012-04-27 14:29:26 +00001116////////////////////////////////////////////////////////////////////////////////
bsalomonc8dc1f72014-08-21 13:02:13 -07001117void GrClipMaskManager::purgeResources() {
1118 fAACache.purgeResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001119}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001120
1121void GrClipMaskManager::setGpu(GrGpu* gpu) {
1122 fGpu = gpu;
1123 fAACache.setContext(gpu->getContext());
1124}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001125
1126void GrClipMaskManager::adjustPathStencilParams(GrStencilSettings* settings) {
1127 const GrDrawState& drawState = fGpu->getDrawState();
1128 GrClipMaskManager::StencilClipMode clipMode;
1129 if (this->isClipInStencil() && drawState.isClipState()) {
1130 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
1131 // We can't be modifying the clip and respecting it at the same time.
1132 SkASSERT(!drawState.isStateFlagEnabled(
1133 GrGpu::kModifyStencilClip_StateBit));
1134 } else if (drawState.isStateFlagEnabled(
1135 GrGpu::kModifyStencilClip_StateBit)) {
1136 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
1137 } else {
1138 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
1139 }
1140
1141 // TODO: dynamically attach a stencil buffer
1142 int stencilBits = 0;
1143 GrStencilBuffer* stencilBuffer =
1144 drawState.getRenderTarget()->getStencilBuffer();
bsalomon49f085d2014-09-05 13:34:00 -07001145 if (stencilBuffer) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001146 stencilBits = stencilBuffer->bits();
1147 this->adjustStencilParams(settings, clipMode, stencilBits);
1148 }
1149}