blob: caee6eb0337357e2f6318458503fde18c51708c7 [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"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000022#include "SkRasterClip.h"
23#include "SkStrokeRec.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000024#include "SkTLazy.h"
25
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
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000030using namespace GrReducedClip;
31
bsalomon@google.com51a62862012-11-26 21:19:43 +000032////////////////////////////////////////////////////////////////////////////////
robertphillips@google.come79f3202014-02-11 16:30:21 +000033namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000034// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000035// stage matrix this also alters the vertex layout
robertphillips@google.come79f3202014-02-11 16:30:21 +000036void setup_drawstate_aaclip(GrGpu* gpu,
37 GrTexture* result,
38 const SkIRect &devBound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000039 GrDrawState* drawState = gpu->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000040 SkASSERT(drawState);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000041
bsalomon@google.comb9086a02012-11-01 18:02:54 +000042 SkMatrix mat;
bsalomon@google.comc7818882013-03-20 19:19:53 +000043 // We want to use device coords to compute the texture coordinates. We set our matrix to be
44 // equal to the view matrix followed by an offset to the devBound, and then a scaling matrix to
45 // normalized coords. We apply this matrix to the vertex positions rather than local coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000046 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000047 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000048 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000049 mat.preConcat(drawState->getViewMatrix());
50
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000051 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000052 // This could be a long-lived effect that is cached with the alpha-mask.
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000053 drawState->addCoverageEffect(
54 GrTextureDomainEffect::Create(result,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000055 mat,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000056 GrTextureDomain::MakeTexelDomain(result, domainTexels),
57 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +000058 GrTextureParams::kNone_FilterMode,
bsalomon@google.com77af6802013-10-02 13:04:56 +000059 kPosition_GrCoordSet))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000060}
61
robertphillips@google.come79f3202014-02-11 16:30:21 +000062bool path_needs_SW_renderer(GrContext* context,
63 GrGpu* gpu,
64 const SkPath& origPath,
65 const SkStrokeRec& stroke,
66 bool doAA) {
67 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
68 SkTCopyOnFirstWrite<SkPath> path(origPath);
69 if (path->isInverseFillType()) {
70 path.writable()->toggleInverseFillType();
71 }
72 // last (false) parameter disallows use of the SW path renderer
bsalomon@google.com45a15f52012-12-10 19:10:17 +000073 GrPathRendererChain::DrawType type = doAA ?
74 GrPathRendererChain::kColorAntiAlias_DrawType :
75 GrPathRendererChain::kColor_DrawType;
76
robertphillips@google.come79f3202014-02-11 16:30:21 +000077 return NULL == context->getPathRenderer(*path, stroke, gpu, false, type);
78}
79
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000080}
81
robertphillips@google.comfa662942012-05-17 12:20:22 +000082/*
83 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
84 * will be used on any element. If so, it returns true to indicate that the
85 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
86 */
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000087bool GrClipMaskManager::useSWOnlyPath(const ElementList& elements) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +000088
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000089 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000090 // a clip gets complex enough it can just be done in SW regardless
91 // of whether it would invoke the GrSoftwarePathRenderer.
sugoi@google.com5f74cf82012-12-17 21:16:45 +000092 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000093
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000094 for (ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
95 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000096 // rects can always be drawn directly w/o using the software path
97 // so only paths need to be checked
bsalomon@google.com8182fa02012-12-04 14:06:06 +000098 if (Element::kPath_Type == element->getType() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +000099 path_needs_SW_renderer(this->getContext(), fGpu,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000100 element->getPath(),
sugoi@google.com12b4e272012-12-06 20:13:11 +0000101 stroke,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000102 element->isAA())) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000103 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000104 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000105 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000106 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000107}
108
robertphillips@google.comf294b772012-04-27 14:29:26 +0000109////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000110// sort out what kind of clip mask needs to be created: alpha, stencil,
111// scissor, or entirely software
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000112bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn,
113 GrDrawState::AutoRestoreEffects* are) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000114 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000115
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000116 ElementList elements(16);
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000117 int32_t genID;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000118 InitialState initialState;
119 SkIRect clipSpaceIBounds;
120 bool requiresAA;
121 bool isRect = false;
122
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000123 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000124
125 const GrRenderTarget* rt = drawState->getRenderTarget();
126 // GrDrawTarget should have filtered this for us
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000127 SkASSERT(NULL != rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000128
129 bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen();
130
131 if (!ignoreClip) {
132 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
133 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
134 ReduceClipStack(*clipDataIn->fClipStack,
135 clipSpaceRTIBounds,
136 &elements,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000137 &genID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000138 &initialState,
139 &clipSpaceIBounds,
140 &requiresAA);
141 if (elements.isEmpty()) {
142 if (kAllIn_InitialState == initialState) {
143 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
144 isRect = true;
145 } else {
146 return false;
147 }
148 }
149 }
150
151 if (ignoreClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000152 fGpu->disableScissor();
153 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000154 return true;
155 }
156
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000157 // If there is only one clip element and it is a convex polygon we just install an effect that
158 // clips against the edges.
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000159 if (1 == elements.count() && SkRegion::kReplace_Op == elements.tail()->getOp()) {
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000160 SkAutoTUnref<GrEffectRef> effect;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000161 if (SkClipStack::Element::kPath_Type == elements.tail()->getType()) {
162 const SkPath& path = elements.tail()->getPath();
163 bool isAA = GR_AA_CLIP && elements.tail()->isAA();
164 if (rt->isMultisampled()) {
165 // A coverage effect for AA clipping won't play nicely with MSAA.
166 if (!isAA) {
167 SkVector offset = { SkIntToScalar(-clipDataIn->fOrigin.fX),
168 SkIntToScalar(-clipDataIn->fOrigin.fY) };
169 effect.reset(GrConvexPolyEffect::Create(GrConvexPolyEffect::kFillNoAA_EdgeType,
170 path, &offset));
171 }
172 } else {
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000173 SkVector offset = { SkIntToScalar(-clipDataIn->fOrigin.fX),
174 SkIntToScalar(-clipDataIn->fOrigin.fY) };
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000175 GrConvexPolyEffect::EdgeType type = isAA ? GrConvexPolyEffect::kFillAA_EdgeType :
176 GrConvexPolyEffect::kFillNoAA_EdgeType;
177 effect.reset(GrConvexPolyEffect::Create(type, path, &offset));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000178 }
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000179 } else if (GR_AA_CLIP && elements.tail()->isAA() && !rt->isMultisampled()) {
180 // We only handle AA/non-MSAA rects here. Coverage effect AA isn't MSAA friendly and
181 // non-AA rect clips are handled by the scissor.
182 SkASSERT(SkClipStack::Element::kRect_Type == elements.tail()->getType());
183 SkRect rect = elements.tail()->getRect();
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000184 SkVector offset = { SkIntToScalar(-clipDataIn->fOrigin.fX),
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000185 SkIntToScalar(-clipDataIn->fOrigin.fY) };
186 rect.offset(offset);
187 effect.reset(GrConvexPolyEffect::CreateForAAFillRect(rect));
188 // This should never fail.
189 SkASSERT(effect);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000190 }
191 if (effect) {
192 are->set(fGpu->drawState());
193 fGpu->drawState()->addCoverageEffect(effect);
commit-bot@chromium.org6516d4b2014-02-07 14:04:48 +0000194 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
195 scissorSpaceIBounds.offset(-clipDataIn->fOrigin);
196 fGpu->enableScissor(scissorSpaceIBounds);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000197 this->setGpuStencil();
198 return true;
199 }
200 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000201
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000202#if GR_AA_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000203 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000204 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000205 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000206
207 if (this->useSWOnlyPath(elements)) {
208 // The clip geometry is complex enough that it will be more efficient to create it
209 // entirely in software
210 result = this->createSoftwareClipMask(genID,
211 initialState,
212 elements,
213 clipSpaceIBounds);
214 } else {
215 result = this->createAlphaClipMask(genID,
216 initialState,
217 elements,
218 clipSpaceIBounds);
219 }
220
221 if (NULL != result) {
222 // The mask's top left coord should be pinned to the rounded-out top left corner of
223 // clipSpace bounds. We determine the mask's position WRT to the render target here.
224 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
225 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000226 are->set(fGpu->drawState());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000227 setup_drawstate_aaclip(fGpu, result, rtSpaceMaskBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000228 fGpu->disableScissor();
229 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000230 return true;
231 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000232 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000233 }
234#endif // GR_AA_CLIP
235
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000236 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
237 // be created. In either case, free up the texture in the anti-aliased mask cache.
238 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
239 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
240 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000241 fAACache.reset();
242
bsalomon@google.coma3201942012-06-21 19:58:20 +0000243 // If the clip is a rectangle then just set the scissor. Otherwise, create
244 // a stencil mask.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000245 if (isRect) {
246 SkIRect clipRect = clipSpaceIBounds;
247 clipRect.offset(-clipDataIn->fOrigin);
248 fGpu->enableScissor(clipRect);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000249 this->setGpuStencil();
250 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000251 }
252
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000253 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000254 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000255 this->createStencilClipMask(genID,
256 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000257 elements,
258 clipSpaceIBounds,
259 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000260
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000261 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
262 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
263 // use both stencil and scissor test to the bounds for the final draw.
264 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
265 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
266 fGpu->enableScissor(scissorSpaceIBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000267 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000268 return true;
269}
270
271#define VISUALIZE_COMPLEX_CLIP 0
272
273#if VISUALIZE_COMPLEX_CLIP
tfarina@chromium.org223137f2012-11-21 22:38:36 +0000274 #include "SkRandom.h"
275 SkRandom gRandom;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000276 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
277#else
278 #define SET_RANDOM_COLOR
279#endif
280
281namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000282
283////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000284// set up the OpenGL blend function to perform the specified
285// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000286void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000287
288 switch (op) {
289 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000290 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000291 break;
292 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000293 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000294 break;
295 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000296 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000297 break;
298 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000299 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000300 break;
301 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000302 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000303 break;
304 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000305 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000306 break;
307 default:
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000308 SkASSERT(false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000309 break;
310 }
311}
312
robertphillips@google.com72176b22012-05-23 13:19:12 +0000313}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000314
315////////////////////////////////////////////////////////////////////////////////
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000316bool GrClipMaskManager::drawElement(GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000317 const SkClipStack::Element* element,
318 GrPathRenderer* pr) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000319 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000320
321 drawState->setRenderTarget(target->asRenderTarget());
322
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000323 switch (element->getType()) {
324 case Element::kRect_Type:
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000325 // TODO: Do rects directly to the accumulator using a aa-rect GrEffect that covers the
326 // entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000327 if (element->isAA()) {
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000328 getContext()->getAARectRenderer()->fillAARect(fGpu,
329 fGpu,
330 element->getRect(),
robertphillips@google.comb19cb7f2013-05-02 15:37:20 +0000331 SkMatrix::I(),
robertphillips@google.comafd1cba2013-05-14 19:47:47 +0000332 element->getRect(),
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000333 false);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000334 } else {
335 fGpu->drawSimpleRect(element->getRect(), NULL);
336 }
337 return true;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000338 case Element::kPath_Type: {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000339 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
340 if (path->isInverseFillType()) {
341 path.writable()->toggleInverseFillType();
342 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000343 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000344 if (NULL == pr) {
345 GrPathRendererChain::DrawType type;
346 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
347 GrPathRendererChain::kColor_DrawType;
348 pr = this->getContext()->getPathRenderer(*path, stroke, fGpu, false, type);
349 }
350 if (NULL == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000351 return false;
352 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000353 pr->drawPath(element->getPath(), stroke, fGpu, element->isAA());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000354 break;
355 }
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000356 default:
357 // something is wrong if we're trying to draw an empty element.
358 GrCrash("Unexpected element type");
359 return false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000360 }
361 return true;
362}
363
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000364bool GrClipMaskManager::canStencilAndDrawElement(GrTexture* target,
365 const SkClipStack::Element* element,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000366 GrPathRenderer** pr) {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000367 GrDrawState* drawState = fGpu->drawState();
368 drawState->setRenderTarget(target->asRenderTarget());
369
370 switch (element->getType()) {
371 case Element::kRect_Type:
372 return true;
373 case Element::kPath_Type: {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000374 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
375 if (path->isInverseFillType()) {
376 path.writable()->toggleInverseFillType();
377 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000378 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000379 GrPathRendererChain::DrawType type = element->isAA() ?
380 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
381 GrPathRendererChain::kStencilAndColor_DrawType;
robertphillips@google.come79f3202014-02-11 16:30:21 +0000382 *pr = this->getContext()->getPathRenderer(*path, stroke, fGpu, false, type);
383 return NULL != *pr;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000384 }
385 default:
386 // something is wrong if we're trying to draw an empty element.
387 GrCrash("Unexpected element type");
388 return false;
389 }
390}
391
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000392void GrClipMaskManager::mergeMask(GrTexture* dstMask,
393 GrTexture* srcMask,
394 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000395 const SkIRect& dstBound,
396 const SkIRect& srcBound) {
bsalomon@google.com137f1342013-05-29 21:27:53 +0000397 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000398 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000399 SkAssertResult(avmr.setIdentity(drawState));
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000400 GrDrawState::AutoRestoreEffects are(drawState);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000401
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000402 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000403
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000404 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000405
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000406 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000407 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000408
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000409 drawState->addColorEffect(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000410 GrTextureDomainEffect::Create(srcMask,
411 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000412 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
413 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000414 GrTextureParams::kNone_FilterMode))->unref();
reed@google.com44699382013-10-31 17:28:30 +0000415 fGpu->drawSimpleRect(SkRect::Make(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000416}
417
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000418// get a texture to act as a temporary buffer for AA clip boolean operations
419// TODO: given the expense of createTexture we may want to just cache this too
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000420void GrClipMaskManager::getTemp(int width, int height, GrAutoScratchTexture* temp) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000421 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000422 // we've already allocated the temp texture
423 return;
424 }
425
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000426 GrTextureDesc desc;
427 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000428 desc.fWidth = width;
429 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000430 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000431
robertphillips@google.com2c756812012-05-22 20:28:23 +0000432 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000433}
434
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000435////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000436// Handles caching & allocation (if needed) of a clip alpha-mask texture for both the sw-upload
437// or gpu-rendered cases. Returns true if there is no more work to be done (i.e., we got a cache
438// hit)
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000439bool GrClipMaskManager::getMaskTexture(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000440 const SkIRect& clipSpaceIBounds,
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000441 GrTexture** result,
442 bool willUpload) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000443 bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000444 if (!cached) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000445
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000446 // There isn't a suitable entry in the cache so we create a new texture to store the mask.
447 // Since we are setting up the cache we know the last lookup was a miss. Free up the
448 // currently cached mask so it can be reused.
449 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000450
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000451 GrTextureDesc desc;
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000452 desc.fFlags = willUpload ? kNone_GrTextureFlags : kRenderTarget_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000453 desc.fWidth = clipSpaceIBounds.width();
454 desc.fHeight = clipSpaceIBounds.height();
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000455 desc.fConfig = kRGBA_8888_GrPixelConfig;
robertphillips@google.com94bdd7e2013-10-31 15:50:43 +0000456 if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000457 // We would always like A8 but it isn't supported on all platforms
458 desc.fConfig = kAlpha_8_GrPixelConfig;
459 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000460
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000461 fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000462 }
463
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000464 *result = fAACache.getLastMask();
465 return cached;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000466}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000467
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000468////////////////////////////////////////////////////////////////////////////////
469// Create a 8-bit clip mask in alpha
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000470GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000471 InitialState initialState,
472 const ElementList& elements,
473 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000474 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000475
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000476 GrTexture* result;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000477 if (this->getMaskTexture(elementsGenID, clipSpaceIBounds, &result, false)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000478 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000479 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000480 }
481
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000482 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000483 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000484 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000485 }
486
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000487 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000488 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000489 SkIntToScalar(-clipSpaceIBounds.fLeft),
490 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000491 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000492 // The texture may be larger than necessary, this rect represents the part of the texture
493 // we populate with a rasterization of the clip.
494 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
495
bsalomon@google.com137f1342013-05-29 21:27:53 +0000496 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
497 SkMatrix translate;
498 translate.setTranslate(clipToMaskOffset);
499 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &translate);
500
501 GrDrawState* drawState = fGpu->drawState();
502
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000503 // We're drawing a coverage mask and want coverage to be run through the blend function.
504 drawState->enableState(GrDrawState::kCoverageDrawing_StateBit);
505
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000506 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
507 // clear the part that we care about.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000508 fGpu->clear(&maskSpaceIBounds,
509 kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000510 true,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000511 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000512
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000513 // When we use the stencil in the below loop it is important to have this clip installed.
514 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
515 // pass must not set values outside of this bounds or stencil values outside the rect won't be
516 // cleared.
517 GrDrawTarget::AutoClipRestore acr(fGpu, maskSpaceIBounds);
518 drawState->enableState(GrDrawState::kClip_StateBit);
519
robertphillips@google.comf105b102012-05-14 12:18:26 +0000520 GrAutoScratchTexture temp;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000521 // walk through each clip element and perform its set op
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000522 for (ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000523 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000524 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000525 bool invert = element->isInverseFilled();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000526
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000527 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000528 GrPathRenderer* pr = NULL;
529 bool useTemp = !this->canStencilAndDrawElement(result, element, &pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000530 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000531 // 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 +0000532 // mask buffer can be substantially larger than the actually clip stack element. We
533 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000534 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000535 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000536
537 if (useTemp) {
538 if (invert) {
539 maskSpaceElementIBounds = maskSpaceIBounds;
540 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000541 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000542 elementBounds.offset(clipToMaskOffset);
543 elementBounds.roundOut(&maskSpaceElementIBounds);
544 }
545
546 this->getTemp(maskSpaceIBounds.fRight, maskSpaceIBounds.fBottom, &temp);
547 if (NULL == temp.texture()) {
548 fAACache.reset();
549 return NULL;
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000550 }
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000551 dst = temp.texture();
552 // clear the temp target and set blend to replace
553 fGpu->clear(&maskSpaceElementIBounds,
554 invert ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000555 true,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000556 dst->asRenderTarget());
557 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000558
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000559 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000560 // draw directly into the result with the stencil set to make the pixels affected
561 // by the clip shape be non-zero.
562 dst = result;
563 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
564 kReplace_StencilOp,
565 kReplace_StencilOp,
566 kAlways_StencilFunc,
567 0xffff,
568 0xffff,
569 0xffff);
570 drawState->setStencil(kStencilInElement);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000571 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000572 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000573
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000574 drawState->setAlpha(invert ? 0x00 : 0xff);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000575
robertphillips@google.come79f3202014-02-11 16:30:21 +0000576 if (!this->drawElement(dst, element, pr)) {
577 fAACache.reset();
578 return NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000579 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000580
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000581 if (useTemp) {
582 // Now draw into the accumulator using the real operation and the temp buffer as a
583 // texture
584 this->mergeMask(result,
585 temp.texture(),
586 op,
587 maskSpaceIBounds,
588 maskSpaceElementIBounds);
589 } else {
590 // Draw to the exterior pixels (those with a zero stencil value).
591 drawState->setAlpha(invert ? 0xff : 0x00);
592 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
593 kZero_StencilOp,
594 kZero_StencilOp,
595 kEqual_StencilFunc,
596 0xffff,
597 0x0000,
598 0xffff);
599 drawState->setStencil(kDrawOutsideElement);
600 fGpu->drawSimpleRect(clipSpaceIBounds);
601 drawState->disableStencil();
602 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000603 } else {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000604 // all the remaining ops can just be directly draw into the accumulation buffer
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000605 drawState->setAlpha(0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000606 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000607 this->drawElement(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000608 }
609 }
610
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000611 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000612 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000613}
614
615////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000616// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000617// (as opposed to canvas) coordinates
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000618bool GrClipMaskManager::createStencilClipMask(int32_t elementsGenID,
619 InitialState initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000620 const ElementList& elements,
621 const SkIRect& clipSpaceIBounds,
622 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000623
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000624 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000625
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000626 GrDrawState* drawState = fGpu->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000627 SkASSERT(drawState->isClipState());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000628
629 GrRenderTarget* rt = drawState->getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000630 SkASSERT(NULL != rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000631
632 // TODO: dynamically attach a SB when needed.
633 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
634 if (NULL == stencilBuffer) {
635 return false;
636 }
637
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000638 if (stencilBuffer->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000639
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000640 stencilBuffer->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000641
bsalomon@google.com137f1342013-05-29 21:27:53 +0000642 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
643 SkVector translate = {
644 SkIntToScalar(clipSpaceToStencilOffset.fX),
645 SkIntToScalar(clipSpaceToStencilOffset.fY)
646 };
647 SkMatrix matrix;
648 matrix.setTranslate(translate);
649 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &matrix);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000650 drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000651
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000652 drawState->setRenderTarget(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000653
bsalomon@google.com9f131742012-12-13 20:43:56 +0000654 // We set the current clip to the bounds so that our recursive draws are scissored to them.
655 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
656 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
657 GrDrawTarget::AutoClipRestore acr(fGpu, stencilSpaceIBounds);
658 drawState->enableState(GrDrawState::kClip_StateBit);
659
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000660#if !VISUALIZE_COMPLEX_CLIP
661 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
662#endif
663
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000664 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000665 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000666 clipBit = (1 << (clipBit-1));
667
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000668 fGpu->clearStencilClip(stencilSpaceIBounds, kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000669
670 // walk through each clip element and perform its set op
671 // with the existing clip.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000672 for (ElementList::Iter iter(elements.headIter()); NULL != iter.get(); iter.next()) {
673 const Element* element = iter.get();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000674 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000675 // enabled at bottom of loop
676 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000677 // if the target is MSAA then we want MSAA enabled when the clip is soft
678 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000679 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000680 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000681
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000682 // This will be used to determine whether the clip shape can be rendered into the
683 // stencil with arbitrary stencil settings.
684 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000685
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000686 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000687
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000688 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000689
robertphillips@google.come79f3202014-02-11 16:30:21 +0000690 GrPathRenderer* pr = NULL;
691 SkTCopyOnFirstWrite<SkPath> clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000692 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000693 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000694 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000695 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000696 SkASSERT(Element::kPath_Type == element->getType());
robertphillips@google.come79f3202014-02-11 16:30:21 +0000697 clipPath.init(element->getPath());
698 fillInverted = clipPath->isInverseFillType();
699 if (fillInverted) {
700 clipPath.writable()->toggleInverseFillType();
701 }
702 pr = this->getContext()->getPathRenderer(*clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000703 stroke,
704 fGpu,
705 false,
706 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000707 &stencilSupport);
708 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000709 return false;
710 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000711 }
712
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000713 int passes;
714 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
715
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000716 bool canRenderDirectToStencil =
717 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000718 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000719 // fill rule, and set operation can
720 // we render the element directly to
721 // stencil bit used for clipping.
722 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
723 canRenderDirectToStencil,
724 clipBit,
725 fillInverted,
726 &passes,
727 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000728
729 // draw the element to the client stencil bits if necessary
730 if (!canDrawDirectToClip) {
731 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000732 kIncClamp_StencilOp,
733 kIncClamp_StencilOp,
734 kAlways_StencilFunc,
735 0xffff,
736 0x0000,
737 0xffff);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000738 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000739 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000740 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000741 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000742 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000743 SkASSERT(Element::kPath_Type == element->getType());
robertphillips@google.come79f3202014-02-11 16:30:21 +0000744 if (!clipPath->isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000745 if (canRenderDirectToStencil) {
746 *drawState->stencil() = gDrawToStencil;
robertphillips@google.come79f3202014-02-11 16:30:21 +0000747 pr->drawPath(*clipPath, stroke, fGpu, false);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000748 } else {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000749 pr->stencilPath(*clipPath, stroke, fGpu);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000750 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000751 }
752 }
753 }
754
755 // now we modify the clip bit by rendering either the clip
756 // element directly or a bounding rect of the entire clip.
757 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
758 for (int p = 0; p < passes; ++p) {
759 *drawState->stencil() = stencilSettings[p];
760 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000761 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000762 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000763 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000764 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000765 SkASSERT(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000766 SET_RANDOM_COLOR
robertphillips@google.come79f3202014-02-11 16:30:21 +0000767 pr->drawPath(*clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000768 }
769 } else {
770 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000771 // The view matrix is setup to do clip space -> stencil space translation, so
772 // draw rect in clip space.
reed@google.com44699382013-10-31 17:28:30 +0000773 fGpu->drawSimpleRect(SkRect::Make(clipSpaceIBounds), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000774 }
775 }
776 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000777 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000778 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000779 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000780 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000781 return true;
782}
783
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000784
bsalomon@google.com411dad02012-06-05 20:24:20 +0000785// mapping of clip-respecting stencil funcs to normal stencil funcs
786// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000787static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000788 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
789 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
790 // In the Clip Funcs
791 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
792 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
793 kLess_StencilFunc, // kLessIfInClip_StencilFunc
794 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
795 // Special in the clip func that forces user's ref to be 0.
796 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
797 // make ref 0 and do normal nequal.
798 },
799 {// Stencil-Clipping is ENABLED
800 // In the Clip Funcs
801 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
802 // eq stencil clip bit, mask
803 // out user bits.
804
805 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
806 // add stencil bit to mask and ref
807
808 kLess_StencilFunc, // kLessIfInClip_StencilFunc
809 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
810 // for both of these we can add
811 // the clip bit to the mask and
812 // ref and compare as normal
813 // Special in the clip func that forces user's ref to be 0.
814 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
815 // make ref have only the clip bit set
816 // and make comparison be less
817 // 10..0 < 1..user_bits..
818 }
819};
820
bsalomon@google.coma3201942012-06-21 19:58:20 +0000821namespace {
822// Sets the settings to clip against the stencil buffer clip while ignoring the
823// client bits.
824const GrStencilSettings& basic_apply_stencil_clip_settings() {
825 // stencil settings to use when clip is in stencil
826 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
827 kKeep_StencilOp,
828 kKeep_StencilOp,
829 kAlwaysIfInClip_StencilFunc,
830 0x0000,
831 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000832 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000833 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
834}
835}
836
837void GrClipMaskManager::setGpuStencil() {
838 // We make two copies of the StencilSettings here (except in the early
839 // exit scenario. One copy from draw state to the stack var. Then another
840 // from the stack var to the gpu. We could make this class hold a ptr to
841 // GrGpu's fStencilSettings and eliminate the stack copy here.
842
843 const GrDrawState& drawState = fGpu->getDrawState();
844
845 // use stencil for clipping if clipping is enabled and the clip
846 // has been written into the stencil.
847 GrClipMaskManager::StencilClipMode clipMode;
848 if (this->isClipInStencil() && drawState.isClipState()) {
849 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
850 // We can't be modifying the clip and respecting it at the same time.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000851 SkASSERT(!drawState.isStateFlagEnabled(
bsalomon@google.coma3201942012-06-21 19:58:20 +0000852 GrGpu::kModifyStencilClip_StateBit));
853 } else if (drawState.isStateFlagEnabled(
854 GrGpu::kModifyStencilClip_StateBit)) {
855 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
856 } else {
857 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
858 }
859
860 GrStencilSettings settings;
861 // The GrGpu client may not be using the stencil buffer but we may need to
862 // enable it in order to respect a stencil clip.
863 if (drawState.getStencil().isDisabled()) {
864 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
865 settings = basic_apply_stencil_clip_settings();
866 } else {
867 fGpu->disableStencil();
868 return;
869 }
870 } else {
871 settings = drawState.getStencil();
872 }
873
874 // TODO: dynamically attach a stencil buffer
875 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000876 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000877 drawState.getRenderTarget()->getStencilBuffer();
878 if (NULL != stencilBuffer) {
879 stencilBits = stencilBuffer->bits();
880 }
881
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000882 SkASSERT(fGpu->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
883 SkASSERT(fGpu->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000884 this->adjustStencilParams(&settings, clipMode, stencilBits);
885 fGpu->setStencilSettings(settings);
886}
887
888void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
889 StencilClipMode mode,
890 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000891 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000892
893 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000894 // We assume that this clip manager itself is drawing to the GrGpu and
895 // has already setup the correct values.
896 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000897 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000898
bsalomon@google.com411dad02012-06-05 20:24:20 +0000899 unsigned int clipBit = (1 << (stencilBitCnt - 1));
900 unsigned int userBits = clipBit - 1;
901
bsalomon@google.coma3201942012-06-21 19:58:20 +0000902 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000903 bool twoSided = fGpu->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000904
bsalomon@google.coma3201942012-06-21 19:58:20 +0000905 bool finished = false;
906 while (!finished) {
907 GrStencilFunc func = settings->func(face);
908 uint16_t writeMask = settings->writeMask(face);
909 uint16_t funcMask = settings->funcMask(face);
910 uint16_t funcRef = settings->funcRef(face);
911
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000912 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000913
914 writeMask &= userBits;
915
916 if (func >= kBasicStencilFuncCount) {
917 int respectClip = kRespectClip_StencilClipMode == mode;
918 if (respectClip) {
919 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000920 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000921 switch (func) {
922 case kAlwaysIfInClip_StencilFunc:
923 funcMask = clipBit;
924 funcRef = clipBit;
925 break;
926 case kEqualIfInClip_StencilFunc:
927 case kLessIfInClip_StencilFunc:
928 case kLEqualIfInClip_StencilFunc:
929 funcMask = (funcMask & userBits) | clipBit;
930 funcRef = (funcRef & userBits) | clipBit;
931 break;
932 case kNonZeroIfInClip_StencilFunc:
933 funcMask = (funcMask & userBits) | clipBit;
934 funcRef = clipBit;
935 break;
936 default:
937 GrCrash("Unknown stencil func");
938 }
939 } else {
940 funcMask &= userBits;
941 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000942 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000943 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000944 gSpecialToBasicStencilFunc[respectClip];
945 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000946 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000947 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000948 funcMask &= userBits;
949 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000950 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000951
952 settings->setFunc(face, func);
953 settings->setWriteMask(face, writeMask);
954 settings->setFuncMask(face, funcMask);
955 settings->setFuncRef(face, funcRef);
956
957 if (GrStencilSettings::kFront_Face == face) {
958 face = GrStencilSettings::kBack_Face;
959 finished = !twoSided;
960 } else {
961 finished = true;
962 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000963 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000964 if (!twoSided) {
965 settings->copyFrontSettingsToBack();
966 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000967}
968
969////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000970GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000971 GrReducedClip::InitialState initialState,
972 const GrReducedClip::ElementList& elements,
973 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000974 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000975
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000976 GrTexture* result;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000977 if (this->getMaskTexture(elementsGenID, clipSpaceIBounds, &result, true)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000978 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000979 }
980
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000981 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000982 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000983 return NULL;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000984 }
985
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000986 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
987 // the top left corner of the resulting rect to the top left of the texture.
988 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
989
robertphillips@google.com2c756812012-05-22 20:28:23 +0000990 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000991
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000992 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000993 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
994 SkIntToScalar(-clipSpaceIBounds.fTop));
995 helper.init(maskSpaceIBounds, &matrix);
996
997 helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000998
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000999 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001000
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001001 for (ElementList::Iter iter(elements.headIter()) ; NULL != iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001002
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001003 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001004 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001005
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001006 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1007 // Intersect and reverse difference require modifying pixels outside of the geometry
1008 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1009 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1010 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001011 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001012 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001013 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001014 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001015 }
1016
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001017 if (Element::kRect_Type == element->getType()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001018 // convert the rect to a path so we can invert the fill
1019 SkPath temp;
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001020 temp.addRect(element->getRect());
sugoi@google.com12b4e272012-12-06 20:13:11 +00001021 temp.setFillType(SkPath::kInverseEvenOdd_FillType);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001022
sugoi@google.com12b4e272012-12-06 20:13:11 +00001023 helper.draw(temp, stroke, SkRegion::kReplace_Op,
1024 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001025 0x00);
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001026 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001027 SkASSERT(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +00001028 SkPath clipPath = element->getPath();
1029 clipPath.toggleInverseFillType();
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +00001030 helper.draw(clipPath, stroke,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001031 SkRegion::kReplace_Op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001032 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001033 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001034 }
1035
1036 continue;
1037 }
1038
1039 // The other ops (union, xor, diff) only affect pixels inside
1040 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001041 if (Element::kRect_Type == element->getType()) {
1042 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1043 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001044 SkASSERT(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +00001045 helper.draw(element->getPath(), stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001046 }
1047 }
1048
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001049 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001050
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001051 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001052 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001053}
1054
robertphillips@google.comf294b772012-04-27 14:29:26 +00001055////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001056void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001057 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001058}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001059
1060void GrClipMaskManager::setGpu(GrGpu* gpu) {
1061 fGpu = gpu;
1062 fAACache.setContext(gpu->getContext());
1063}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001064
1065void GrClipMaskManager::adjustPathStencilParams(GrStencilSettings* settings) {
1066 const GrDrawState& drawState = fGpu->getDrawState();
1067 GrClipMaskManager::StencilClipMode clipMode;
1068 if (this->isClipInStencil() && drawState.isClipState()) {
1069 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
1070 // We can't be modifying the clip and respecting it at the same time.
1071 SkASSERT(!drawState.isStateFlagEnabled(
1072 GrGpu::kModifyStencilClip_StateBit));
1073 } else if (drawState.isStateFlagEnabled(
1074 GrGpu::kModifyStencilClip_StateBit)) {
1075 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
1076 } else {
1077 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
1078 }
1079
1080 // TODO: dynamically attach a stencil buffer
1081 int stencilBits = 0;
1082 GrStencilBuffer* stencilBuffer =
1083 drawState.getRenderTarget()->getStencilBuffer();
1084 if (NULL != stencilBuffer) {
1085 stencilBits = stencilBuffer->bits();
1086 this->adjustStencilParams(settings, clipMode, stencilBits);
1087 }
1088}